Skip to content

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

chore: schema diff for cockroach (#15330)

boojack8c7aea5a5ea4
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index fe0d335ed9..cf4c20e65c 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -578,11 +578,9 @@ func (s *DatabaseService) DiffSchema(ctx context.Context, request *v1pb.DiffSche
}
strictMode := true
-
if engine == storepb.Engine_ORACLE {
strictMode = false
}
-
diff, err := base.SchemaDiff(engine, base.DiffContext{
IgnoreCaseSensitive: false,
StrictMode: strictMode,
@@ -680,6 +678,8 @@ func (s *DatabaseService) getParserEngine(ctx context.Context, request *v1pb.Dif
engine = storepb.Engine_ORACLE
case storepb.Engine_MSSQL:
engine = storepb.Engine_MSSQL
+ case storepb.Engine_COCKROACHDB:
+ engine = storepb.Engine_COCKROACHDB
default:
return engine, status.Errorf(codes.InvalidArgument, "invalid engine type %v", instance.Engine)
}
diff --git a/backend/plugin/parser/cockroachdb/differ.go b/backend/plugin/parser/cockroachdb/differ.go
new file mode 100644
index 0000000000..cb698e99f1
--- /dev/null
+++ b/backend/plugin/parser/cockroachdb/differ.go
@@ -0,0 +1,48 @@
+package cockroachdb
+
+import (
+ "strings"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func init() {
+ base.RegisterSchemaDiffFunc(storepb.Engine_COCKROACHDB, SchemaDiff)
+}
+
+// SchemaDiff computes the schema differences between two SQL statements for CockroachDB.
+func SchemaDiff(_ base.DiffContext, oldStmt, newStmt string) (string, error) {
+ oldStmt = sanitizeCockroachSyntax(oldStmt)
+ newStmt = sanitizeCockroachSyntax(newStmt)
+ // Reuse the PostgreSQL schema diff implementation.
+ return pgparser.SchemaDiff(base.DiffContext{}, oldStmt, newStmt)
+}
+
+// sanitizeCockroachSyntax sanitizes the CockroachDB-specific syntax before reusing the PostgreSQL schema diff implementation.
+// CockroachDB has some syntax that is not supported by PostgreSQL, but we want to reuse the PostgreSQL schema diff.
+// Currently, we handle this with simple strings.ReplaceAll calls.
+func sanitizeCockroachSyntax(sql string) string {
+ sql = removeVisible(sql)
+ sql = removePrimaryKeyASC(sql)
+ return sql
+}
+
+// removeVisible removes "NOT VISIBLE" and "VISIBLE" clauses.
+// Postgres does not support "VISIBLE" clause for certain statements.
+func removeVisible(sql string) string {
+ sql = remove(sql, " NOT VISIBLE")
+ sql = remove(sql, " VISIBLE")
+ return sql
+}
+
+// removePrimaryKeyASC removes "ASC" from PRIMARY KEY definitions.
+// PRIMARY KEY (rowid ASC) -> PRIMARY KEY (rowid).
+func removePrimaryKeyASC(sql string) string {
+ return remove(sql, " ASC")
+}
+
+func remove(sql, keyword string) string {
+ return strings.ReplaceAll(sql, keyword, "")
+}
diff --git a/backend/plugin/parser/pg/differ.go b/backend/plugin/parser/pg/differ.go
index ab50b32189..2c09cc9f7a 100644
--- a/backend/plugin/parser/pg/differ.go
+++ b/backend/plugin/parser/pg/differ.go
@@ -23,7 +23,6 @@ import (
func init() {
base.RegisterSchemaDiffFunc(storepb.Engine_POSTGRES, SchemaDiff)
- base.RegisterSchemaDiffFunc(storepb.Engine_COCKROACHDB, SchemaDiff)
}
// diffNode defines different modification types as the safe change order.
diff --git a/frontend/src/components/SyncDatabaseSchemaV1/types.ts b/frontend/src/components/SyncDatabaseSchemaV1/types.ts
index fd670c4fc7..de0f91c58e 100644
--- a/frontend/src/components/SyncDatabaseSchemaV1/types.ts
+++ b/frontend/src/components/SyncDatabaseSchemaV1/types.ts
@@ -22,4 +22,5 @@ export const ALLOWED_ENGINES: Engine[] = [
Engine.TIDB,
Engine.ORACLE,
Engine.MSSQL,
+ Engine.COCKROACHDB,
];