Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: edit database api (#3388)

boojack83731c542a18
diff --git a/api/database.go b/api/database.go
index 93c2d2997d..bb0c7685a5 100644
--- a/api/database.go
+++ b/api/database.go
@@ -127,66 +127,65 @@ type DatabasePatch struct {
// DatabaseEdit is the API message for updating a database in UI editor.
type DatabaseEdit struct {
- DatabaseID int
+ DatabaseID int `json:"databaseId"`
- CreateTableList []*CreateTableContext
- AlterTableList []*AlterTableContext
- DropTableList []*DropTableContext
+ CreateTableList []*CreateTableContext `json:"createTableList"`
+ AlterTableList []*AlterTableContext `json:"alterTableList"`
+ DropTableList []*DropTableContext `json:"dropTableList"`
}
// CreateTableContext is the edit database context to create a table.
type CreateTableContext struct {
- Name string
- Type string
- Engine string
- CharacterSet string
- Collation string
- Comment string
-
- AddColumnList []*AddColumnContext
+ Name string `json:"name"`
+ Type string `json:"type"`
+ Engine string `json:"engine"`
+ CharacterSet string `json:"characterSet"`
+ Collation string `json:"collation"`
+ Comment string `json:"comment"`
+
+ AddColumnList []*AddColumnContext `json:"addColumnList"`
}
// AlterTableContext is the edit database context to alter a table.
type AlterTableContext struct {
- TableID int
+ TableID int `json:"tableId"`
// ColumnNameList should be the final order of columns in UI editor and is used to confirm column positions.
- ColumnNameList []string
+ ColumnNameList []string `json:"columnNameList"`
- AddColumnList []*AddColumnContext
- AlterColumnList []*AlterColumnContext
- DropColumnList []*DropColumnContext
+ AddColumnList []*AddColumnContext `json:"addColumnList"`
+ AlterColumnList []*AlterColumnContext `json:"alterColumnList"`
+ DropColumnList []*DropColumnContext `json:"dropColumnList"`
}
// DropTableContext is the edit database context to drop a table.
type DropTableContext struct {
- TableID int
+ TableID int `json:"tableId"`
}
// AddColumnContext is the create/alter table context to add a column.
type AddColumnContext struct {
- Name string
- Type string
- CharacterSet string
- Collation string
- Comment string
- Nullable bool
- Default *string
+ Name string `json:"name"`
+ Type string `json:"type"`
+ CharacterSet string `json:"characterSet"`
+ Collation string `json:"collation"`
+ Comment string `json:"comment"`
+ Nullable bool `json:"nullable"`
+ Default *string `json:"default"`
}
// AlterColumnContext is the alter table context to alter a column.
type AlterColumnContext struct {
- Name string
- Type *string
- CharacterSet *string
- Collation *string
- Comment *string
- Nullable *bool
- Default *string
- DropDefault *bool
+ Name string `json:"name"`
+ Type *string `json:"type"`
+ CharacterSet *string `json:"characterSet"`
+ Collation *string `json:"collation"`
+ Comment *string `json:"comment"`
+ Nullable *bool `json:"nullable"`
+ Default *string `json:"default"`
}
// DropColumnContext is the alter table context to drop a column.
type DropColumnContext struct {
- Name string
+ Name string `json:"name"`
}
diff --git a/server/database.go b/server/database.go
index 5bdbf83f60..360527ea70 100644
--- a/server/database.go
+++ b/server/database.go
@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "io"
"net/http"
"strconv"
@@ -17,6 +18,8 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/parser"
+ "github.com/bytebase/bytebase/plugin/parser/edit"
)
func (s *Server) registerDatabaseRoutes(g *echo.Group) {
@@ -790,6 +793,44 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
c.Response().WriteHeader(http.StatusOK)
return nil
})
+
+ g.POST("/database/:databaseID/edit", func(c echo.Context) error {
+ ctx := c.Request().Context()
+ databaseID, err := strconv.Atoi(c.Param("databaseID"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Database ID is not a number: %s", c.Param("databaseID"))).SetInternal(err)
+ }
+
+ database, err := s.store.GetDatabase(ctx, &api.DatabaseFind{
+ ID: &databaseID,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find database").SetInternal(err)
+ }
+ if database == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database not found with ID %d", databaseID))
+ }
+
+ body, err := io.ReadAll(c.Request().Body)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)
+ }
+ databaseEdit := &api.DatabaseEdit{}
+ if err := json.Unmarshal(body, databaseEdit); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformed post database edit request").SetInternal(err)
+ }
+
+ engineType := parser.EngineType(database.Instance.Engine)
+ statement, err := edit.DeparseDatabaseEdit(engineType, databaseEdit)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to deparse DatabaseEdit").SetInternal(err)
+ }
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
+ if _, err := c.Response().Write([]byte(statement)); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to write DDL statement response for database %v", databaseID)).SetInternal(err)
+ }
+ return nil
+ })
}
func (s *Server) setDatabaseLabels(ctx context.Context, labelsJSON string, database *api.Database, project *api.Project, updaterID int, validateOnly bool) error {