db: added drop table endpoint (#271)

* db: added drop table endpoint

* F

* Update db.proto

Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
Janos Dobronszki
2021-11-16 13:34:56 +00:00
committed by GitHub
parent 93e92b808c
commit cc7d1911d5
5 changed files with 241 additions and 80 deletions

View File

@@ -23,6 +23,7 @@ import (
const idKey = "id"
const stmt = "create table if not exists %v(id text not null, data jsonb, primary key(id)); alter table %v add created_at timestamptz; alter table %v add updated_at timestamptz"
const truncateStmt = `truncate table "%v"`
const dropTableStmt = `drop table "%v"`
const renameTableStmt = `ALTER TABLE "%v" RENAME TO "%v"`
var re = regexp.MustCompile("^[a-zA-Z0-9_]*$")
@@ -330,6 +331,20 @@ func (e *Db) Truncate(ctx context.Context, req *db.TruncateRequest, rsp *db.Trun
return db.Exec(fmt.Sprintf(truncateStmt, tableName)).Error
}
func (e *Db) DropTable(ctx context.Context, req *db.DropTableRequest, rsp *db.DropTableResponse) error {
tableName, err := e.tableName(ctx, req.Table)
if err != nil {
return err
}
logger.Infof("Dropping table '%v'", tableName)
db, err := e.GetDBConn(ctx)
if err != nil {
return err
}
return db.Exec(fmt.Sprintf(dropTableStmt, tableName)).Error
}
func (e *Db) Count(ctx context.Context, req *db.CountRequest, rsp *db.CountResponse) error {
if req.Table == "" {
req.Table = "default"