Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add schema diff openapi (#2645)
boojack7e30226a2df2
diff --git a/plugin/parser/schemadiff.go b/plugin/parser/schema_diff.gosimilarity index 100%rename from plugin/parser/schemadiff.gorename to plugin/parser/schema_diff.godiff --git a/plugin/parser/schemadiff_test.go b/plugin/parser/schema_diff_test.gosimilarity index 100%rename from plugin/parser/schemadiff_test.gorename to plugin/parser/schema_diff_test.godiff --git a/server/openapi.go b/server/openapi.goindex 4cd56d4d44..f4114b52b4 100644--- a/server/openapi.go+++ b/server/openapi.go@@ -13,7 +13,9 @@ import (metricAPI "github.com/bytebase/bytebase/metric""github.com/bytebase/bytebase/plugin/advisor/catalog"advisorDB "github.com/bytebase/bytebase/plugin/advisor/db"+ "github.com/bytebase/bytebase/plugin/db""github.com/bytebase/bytebase/plugin/metric"+ "github.com/bytebase/bytebase/plugin/parser")var (@@ -35,6 +37,11 @@ func (*catalogService) GetFinder() *catalog.Finder {return catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false})}+func (s *Server) registerOpenAPIRoutes(g *echo.Group) {+ g.POST("/sql/advise", s.sqlCheckController)+ g.POST("/sql/schema/diff", schemaDiff)+}+type sqlCheckRequestBody struct {Statement string `json:"statement"`DatabaseType string `json:"databaseType"`@@ -44,10 +51,6 @@ type sqlCheckRequestBody struct {Port string `json:"port"`}-func (s *Server) registerOpenAPIRoutes(g *echo.Group) {- g.POST("/sql/advise", s.sqlCheckController)-}-// sqlCheckController godoc// @Summary Check the SQL statement.// @Description Parse and check the SQL statement according to the SQL review policy.@@ -173,3 +176,60 @@ func (s *Server) findDatabase(ctx context.Context, host string, port string, datreturn nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Cannot find database %s in instance %s:%s", databaseName, host, port))}++type schemaDiffRequestBody struct {+ EngineType parser.EngineType `json:"engineType"`+ SourceSchema string `json:"sourceSchema"`+ TargetSchema string `json:"targetSchema"`+}++// schemaDiff godoc+// @Summary Get the diff statement between source and target schema.+// @Description Parse and diff the schema statements.+// @Accept */*+// @Tags SQL schema diff+// @Produce json+// @Param engineType body string true "The database engine type."+// @Param sourceSchema body string true "The source schema statement."+// @Param targetSchema body string false "The target schema statement."+// @Success 200 {array} the target diff string of schemas+// @Failure 400 {object} echo.HTTPError+// @Failure 500 {object} echo.HTTPError+// @Router /sql/schema/diff [post].+func schemaDiff(c echo.Context) error {+ request := &schemaDiffRequestBody{}+ body, err := io.ReadAll(c.Request().Body)+ if err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)+ }+ if err := json.Unmarshal(body, request); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Cannot format request body").SetInternal(err)+ }++ var engine parser.EngineType+ switch request.EngineType {+ case parser.EngineType(db.Postgres):+ engine = parser.Postgres+ case parser.EngineType(db.MySQL):+ engine = parser.MySQL+ default:+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid database engine %s", request.EngineType))+ }++ sourceSchemaNodes, err := parser.Parse(engine, parser.ParseContext{}, request.SourceSchema)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to parse source schema to AST nodes: %s", request.SourceSchema)).SetInternal(err)+ }++ targetSchemaNodes, err := parser.Parse(engine, parser.ParseContext{}, request.TargetSchema)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to parse target schema to AST nodes: %s", request.TargetSchema)).SetInternal(err)+ }++ diff, err := parser.SchemaDiff(sourceSchemaNodes, targetSchemaNodes)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compute diff between source and target schemas").SetInternal(err)+ }++ return c.JSON(http.StatusOK, diff)+}diff --git a/server/server.go b/server/server.goindex 95a8f0112d..3a76e3e5ff 100644--- a/server/server.go+++ b/server/server.go@@ -308,6 +308,7 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {openAPIGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {return openAPIMetricMiddleware(s, next)})+ s.registerOpenAPIRoutes(openAPIGroup)apiGroup := e.Group("/api")apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {@@ -354,7 +355,6 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {s.registerSheetRoutes(apiGroup)s.registerSheetOrganizerRoutes(apiGroup)s.registerAnomalyRoutes(apiGroup)- s.registerOpenAPIRoutes(openAPIGroup)// Register healthz endpoint.e.GET("/healthz", func(c echo.Context) error {