Skip to content

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

refactor(oceanbase): give OceanBase its own schema plugin (#21727)

Danny Xu4dcf0bc19f3f
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 6b7b87e1a7..f55feeee7f 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -14,6 +14,7 @@ import (
// Register walk-through implementations
_ "github.com/bytebase/bytebase/backend/plugin/schema/mysql"
+ _ "github.com/bytebase/bytebase/backend/plugin/schema/oceanbase"
_ "github.com/bytebase/bytebase/backend/plugin/schema/pg"
_ "github.com/bytebase/bytebase/backend/plugin/schema/tidb"
)
diff --git a/backend/plugin/parser/mysql/omni.go b/backend/plugin/parser/mysql/omni.go
index ef43801146..5e52688814 100644
--- a/backend/plugin/parser/mysql/omni.go
+++ b/backend/plugin/parser/mysql/omni.go
@@ -20,8 +20,8 @@ type OmniAST struct {
StartPosition *storepb.Position
}
-// ASTSource returns the statement's own text, which consumers shared with the other
-// MySQL-family engines read to re-derive a node.
+// ASTSource returns the statement's own text, which the schema walk-through reads
+// when it needs the statement as written.
func (a *OmniAST) ASTSource() string {
return a.Text
}
diff --git a/backend/plugin/parser/oceanbase/omni.go b/backend/plugin/parser/oceanbase/omni.go
index 7c1fffe578..87fe39382d 100644
--- a/backend/plugin/parser/oceanbase/omni.go
+++ b/backend/plugin/parser/oceanbase/omni.go
@@ -20,9 +20,8 @@ type OmniAST struct {
StartPosition *storepb.Position
}
-// ASTSource returns the statement's own text. Consumers shared with MySQL, such as
-// backend/plugin/schema/mysql's walk-through, read it to re-derive a MySQL AST node
-// for an OceanBase statement.
+// ASTSource returns the statement's own text, which the schema walk-through reads
+// when it needs the statement as written.
func (a *OmniAST) ASTSource() string {
return a.Text
}
diff --git a/backend/plugin/schema/mysql/generate_migration.go b/backend/plugin/schema/mysql/generate_migration.go
index 684c0a19e2..46d2177ace 100644
--- a/backend/plugin/schema/mysql/generate_migration.go
+++ b/backend/plugin/schema/mysql/generate_migration.go
@@ -22,7 +22,6 @@ type tableDrop struct {
func init() {
schema.RegisterGenerateMigration(storepb.Engine_MYSQL, generateMigration)
- schema.RegisterGenerateMigration(storepb.Engine_OCEANBASE, generateMigration)
}
func generateMigration(diff *schema.MetadataDiff) (string, error) {
diff --git a/backend/plugin/schema/mysql/generate_migration_test.go b/backend/plugin/schema/mysql/generate_migration_test.go
index 70f932013f..6631d0677e 100644
--- a/backend/plugin/schema/mysql/generate_migration_test.go
+++ b/backend/plugin/schema/mysql/generate_migration_test.go
@@ -110,24 +110,6 @@ func databaseOf(tables ...*metadatapb.TableMetadata) *model.DatabaseMetadata {
}, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)
}
-func TestDiffMigrationEventWithoutDefinition(t *testing.T) {
- oceanBaseDatabaseOf := func(events ...*metadatapb.EventMetadata) *model.DatabaseMetadata {
- return model.NewDatabaseMetadata(&metadatapb.DatabaseSchemaMetadata{
- Name: "db",
- Schemas: []*metadatapb.SchemaMetadata{{Name: "", Events: events}},
- }, nil, &storepb.DatabaseConfig{}, storepb.Engine_OCEANBASE, true)
- }
- withEvent := oceanBaseDatabaseOf(&metadatapb.EventMetadata{Name: "e"})
-
- migration, err := schema.DiffMigration(storepb.Engine_OCEANBASE, oceanBaseDatabaseOf(), withEvent)
- require.NoError(t, err)
- require.Empty(t, migration)
-
- migration, err = schema.DiffMigration(storepb.Engine_OCEANBASE, withEvent, oceanBaseDatabaseOf())
- require.NoError(t, err)
- require.Equal(t, "DROP EVENT IF EXISTS `e`;\n\n", migration)
-}
-
func TestDiffMigrationPartitionedTableEmittedOnce(t *testing.T) {
for _, partitionCount := range []int{0, 1, 3, 180} {
t.Run(fmt.Sprintf("partitions=%d", partitionCount), func(t *testing.T) {
diff --git a/backend/plugin/schema/mysql/get_database_definition.go b/backend/plugin/schema/mysql/get_database_definition.go
index 4ac693df34..cb56876af0 100644
--- a/backend/plugin/schema/mysql/get_database_definition.go
+++ b/backend/plugin/schema/mysql/get_database_definition.go
@@ -52,19 +52,14 @@ const (
func init() {
schema.RegisterGetDatabaseDefinition(storepb.Engine_MYSQL, GetDatabaseDefinition)
- schema.RegisterGetDatabaseDefinition(storepb.Engine_OCEANBASE, GetDatabaseDefinition)
schema.RegisterGetTableDefinition(storepb.Engine_MYSQL, GetTableDefinition)
- schema.RegisterGetTableDefinition(storepb.Engine_OCEANBASE, GetTableDefinition)
schema.RegisterGetViewDefinition(storepb.Engine_MYSQL, GetViewDefinition)
- schema.RegisterGetViewDefinition(storepb.Engine_OCEANBASE, GetViewDefinition)
schema.RegisterGetFunctionDefinition(storepb.Engine_MYSQL, GetFunctionDefinition)
- schema.RegisterGetFunctionDefinition(storepb.Engine_OCEANBASE, GetFunctionDefinition)
schema.RegisterGetProcedureDefinition(storepb.Engine_MYSQL, GetProcedureDefinition)
- schema.RegisterGetProcedureDefinition(storepb.Engine_OCEANBASE, GetProcedureDefinition)
}
func GetDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *metadatapb.DatabaseSchemaMetadata) (string, error) {
diff --git a/backend/plugin/schema/mysql/get_database_metadata.go b/backend/plugin/schema/mysql/get_database_metadata.go
index cf01530ca0..208fcfb6ed 100644
--- a/backend/plugin/schema/mysql/get_database_metadata.go
+++ b/backend/plugin/schema/mysql/get_database_metadata.go
@@ -13,7 +13,6 @@ import (
func init() {
schema.RegisterGetDatabaseMetadata(storepb.Engine_MYSQL, GetDatabaseMetadata)
- schema.RegisterGetDatabaseMetadata(storepb.Engine_OCEANBASE, GetDatabaseMetadata)
}
// GetDatabaseMetadata parses MySQL schema DDL text and returns database metadata
diff --git a/backend/plugin/schema/mysql/walk_through.go b/backend/plugin/schema/mysql/walk_through.go
index 445f1c0ad4..94757c1e53 100644
--- a/backend/plugin/schema/mysql/walk_through.go
+++ b/backend/plugin/schema/mysql/walk_through.go
@@ -23,7 +23,6 @@ import (
func init() {
schema.RegisterWalkThroughWithContext(storepb.Engine_MYSQL, WalkThroughWithContext)
schema.RegisterWalkThroughWithContext(storepb.Engine_MARIADB, WalkThroughWithContext)
- schema.RegisterWalkThroughWithContext(storepb.Engine_OCEANBASE, WalkThroughWithContext)
}
// WalkThroughWithContext performs DDL simulation using the omni MySQL catalog.
@@ -240,34 +239,11 @@ func precheckMySQLRawWalkThrough(d *model.DatabaseMetadata, rawSQL string, asts
return mySQLWalkThroughAdvice{}
}
-// walkThroughOmniNode returns the MySQL AST node for a statement this walk-through is
-// asked to inspect. OceanBase registers this walk-through but parses to
-// omni/oceanbase/ast, which mysqlparser.GetOmniNode rejects, so an OceanBase statement
-// is re-parsed from its own text here. That keeps the AST-driven steps — the precheck
-// and the cross-database RENAME targets — working as they did before OceanBase had its
-// own parser, and it is why a schema/oceanbase fork (pending omni/oceanbase/catalog)
-// should replace this. A statement only OceanBase's grammar accepts does not re-parse,
-// and its auxiliary check is skipped rather than reported.
-func walkThroughOmniNode(a base.AST) (ast.Node, bool) {
- if node, ok := mysqlparser.GetOmniNode(a); ok {
- return node, true
- }
- source, ok := a.(interface{ ASTSource() string })
- if !ok {
- return nil, false
- }
- list, err := mysqlparser.ParseMySQL(source.ASTSource())
- if err != nil || list == nil || len(list.Items) != 1 {
- return nil, false
- }
- return list.Items[0], true
-}
-
func mySQLOmniASTSkipsCreateTableAsAdvice(d *model.DatabaseMetadata, asts []base.AST, index int) bool {
if index >= len(asts) {
return false
}
- node, ok := walkThroughOmniNode(asts[index])
+ node, ok := mysqlparser.GetOmniNode(asts[index])
if !ok {
return false
}
@@ -367,7 +343,7 @@ func isMySQLIdentifierChar(ch byte) bool {
func precheckMySQLOmniWalkThrough(d *model.DatabaseMetadata, asts []base.AST) mySQLWalkThroughAdvice {
for i, unifiedAST := range asts {
- node, ok := walkThroughOmniNode(unifiedAST)
+ node, ok := mysqlparser.GetOmniNode(unifiedAST)
if !ok {
continue
}
@@ -475,7 +451,7 @@ func mysqlRenameTargetDatabases(d *model.DatabaseMetadata, asts []base.AST) []st
targets = append(targets, targetTable.Schema)
}
for _, unifiedAST := range asts {
- node, ok := walkThroughOmniNode(unifiedAST)
+ node, ok := mysqlparser.GetOmniNode(unifiedAST)
if !ok {
continue
}
diff --git a/backend/plugin/schema/mysql/walk_through_test.go b/backend/plugin/schema/mysql/walk_through_test.go
index 59ba17bb91..3ea733e8fe 100644
--- a/backend/plugin/schema/mysql/walk_through_test.go
+++ b/backend/plugin/schema/mysql/walk_through_test.go
@@ -20,7 +20,6 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
- _ "github.com/bytebase/bytebase/backend/plugin/parser/oceanbase"
"github.com/bytebase/bytebase/backend/plugin/schema"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -138,22 +137,6 @@ func TestWalkThroughSkipsMariaDBOnlyStatements(t *testing.T) {
require.Empty(t, state.GetProto().GetSchemas())
}
-// TestWalkThroughCrossDatabaseRenameOnOceanBase pins that this shared walk-through still
-// reads an OceanBase statement. OceanBase parses to omni/oceanbase/ast, which
-// mysqlparser.GetOmniNode rejects, so without walkThroughOmniNode's re-parse
-// mysqlRenameTargetDatabases would collect no target and the simulation would fail a
-// cross-database RENAME that the server accepts.
-func TestWalkThroughCrossDatabaseRenameOnOceanBase(t *testing.T) {
- state := model.NewDatabaseMetadata(&metadatapb.DatabaseSchemaMetadata{Name: "test"}, nil, nil, storepb.Engine_OCEANBASE, true)
- statement := "CREATE TABLE `test`.`t1` (a INT);\nRENAME TABLE `test`.`t1` TO `other`.`t1`;"
- sm := sheet.NewManager()
- stmts, advices := sm.GetStatementsForChecks(storepb.Engine_OCEANBASE, statement)
- require.Empty(t, advices)
-
- advice := WalkThroughWithContext(context.Background(), schema.WalkThroughContext{RawSQL: statement}, state, base.ExtractASTs(stmts))
- require.Nil(t, advice, "a cross-database RENAME the server accepts must not fail the simulation")
-}
-
// TestWalkThroughSRIDInvisible verifies the omni catalog -> metadatapb.ColumnMetadata
// conversion (tableToProto) carries the spatial SRID (presence + value, including the
// valid explicit SRID 0) and the INVISIBLE flag. Without this the WalkThroughWithContext
diff --git a/backend/plugin/schema/oceanbase/database.go b/backend/plugin/schema/oceanbase/database.go
new file mode 100644
index 0000000000..5ad4120a47
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/database.go
@@ -0,0 +1,15 @@
+package oceanbase
+
+import (
+ "strings"
+
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+// isCurrentDatabase returns true if the given database is the current database.
+func isCurrentDatabase(d *model.DatabaseMetadata, database string) bool {
+ if !d.GetIsObjectCaseSensitive() {
+ return strings.EqualFold(d.DatabaseName(), database)
+ }
+ return d.DatabaseName() == database
+}
diff --git a/backend/plugin/schema/oceanbase/format_view_body.go b/backend/plugin/schema/oceanbase/format_view_body.go
new file mode 100644
index 0000000000..2af68b8358
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/format_view_body.go
@@ -0,0 +1,431 @@
+package oceanbase
+
+import (
+ "strings"
+)
+
+// formatViewBodySDL pretty-prints a MySQL stored view body (the one-line SELECT text
+// SHOW CREATE VIEW emits) for the SDL dump. It is a WHITESPACE-ONLY transformation: the
+// token stream is copied verbatim and only the whitespace BETWEEN tokens is rewritten
+// (runs collapse to a single space, a newline, or a newline+indent). That keeps the
+// omni no-op invariant intact — the SDL loader routes view bodies through parse→deparse
+// (deparseViewSelect), so any two whitespace-equivalent bodies land on the same
+// canonical Definition — while making the dumped statement readable and git-diffable.
+//
+// The scanner mirrors stripViewBodyDatabaseQualifier's string-literal awareness and
+// extends it to backtick identifiers and parentheses:
+// - '…' / "…" literals are opaque tokens (backslash and doubled-delimiter escapes),
+// so a literal containing " from " or "union all" is never split;
+// - `…` identifiers are opaque tokens (doubled-backtick escape, NO backslash escape),
+// so an alias like `CONCAT('INDEX (', INDEX_TYPE, ')')` is never split;
+// - '(' frames are classified as subquery (next word is select/with) or plain (join
+// trees, expression grouping), so breaks never land inside a subquery or a
+// function/expression argument list.
+//
+// Break rules (MySQL's stored grammar is a fixed shape — select … from … where …
+// group by … having … order by … limit …, unions between members, parenthesized
+// left-deep join trees, an optional trailing WITH … CHECK OPTION):
+//
+// token condition break
+// ------------------------------------------------------------------------------------
+// select top level (no open paren) newline before
+// from/where/having/limit/union top level newline before
+// group by / order by top level newline before
+// with [cascaded|local] check option top level, at end of body newline before
+// first select-list item after select [+modifiers] newline+indent
+// comma between select-list items top level select list newline+indent after
+// [natural|left|right|inner|cross…] join in from clause, not inside newline+indent before
+// any subquery
+//
+// Everything else keeps its single-space (or no-space) joining, so long expressions,
+// on(…) conditions, scalar subqueries, and group-by lists stay on their line. The
+// function is deterministic and idempotent (formatViewBodySDL(f(x)) == f(x)): the A6/A8
+// dump-stability guards rely on same-body-in → same-bytes-out.
+//
+// FAIL-SAFE: the scanner assumes the backslash-escaped canonical literal form that
+// SHOW CREATE VIEW / information_schema.VIEWS always re-print (live-verified on 5.7.25
+// and 8.0.32: the server re-serializes literals with backslash escapes even for views
+// CREATED under NO_BACKSLASH_ESCAPES). Should a body from some other origin still
+// mis-tokenize, whitespace INSIDE a mis-scanned literal would be rewritten — data
+// corruption, not just bad formatting. Two runtime guards convert every such present or
+// future scanner gap into "not pretty" instead of "corrupted":
+// 1. the body must tokenize cleanly — a literal/identifier that never closes (the
+// smoking-gun signature of an escape-mode mismatch swallowing a quote, or of a
+// malformed body) refuses formatting;
+// 2. the formatted output's token stream must byte-match the input's (the
+// whitespace-only property the tests assert, re-checked at runtime).
+//
+// On any mismatch the original stored body is returned unformatted, which is still
+// canonical for the omni diff (whitespace washes out through parse→deparse).
+func formatViewBodySDL(body string) string {
+ inTokens, clean := viewBodyTokenStream(body)
+ if !clean {
+ return body
+ }
+ formatted := formatViewBodyUnchecked(body)
+ outTokens, clean := viewBodyTokenStream(formatted)
+ if !clean || !viewBodyTokensEqual(inTokens, outTokens) {
+ return body
+ }
+ return formatted
+}
+
+// viewBodyTokenStream splits body into its token stream (whitespace between tokens
+// discarded). clean is false when any '…'/"…" literal or `…` identifier runs to the end
+// of the body without its closing delimiter.
+func viewBodyTokenStream(body string) (tokens []string, clean bool) {
+ for i := 0; i < len(body); {
+ if isViewBodySpaceByte(body[i]) {
+ i++
+ continue
+ }
+ end, closed := scanViewBodyTokenClosed(body, i)
+ if !closed {
+ return nil, false
+ }
+ tokens = append(tokens, body[i:end])
+ i = end
+ }
+ return tokens, true
+}
+
+// viewBodyTokensEqual reports whether two token streams are element-wise identical.
+func viewBodyTokensEqual(a, b []string) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for i := range a {
+ if a[i] != b[i] {
+ return false
+ }
+ }
+ return true
+}
+
+// formatViewBodyUnchecked is the formatting core; formatViewBodySDL wraps it with the
+// fail-safe guards above.
+func formatViewBodyUnchecked(body string) string {
+ var out strings.Builder
+ out.Grow(len(body) + 64)
+
+ // stack tracks open parens; true marks a subquery frame ("(" directly followed by
+ // select/with). subq counts open subquery frames for O(1) "inside a subquery" tests.
+ var stack []bool
+ subq := 0
+ // selectHead is the region between a top-level `select` and its first list item
+ // (modifiers like distinct/straight_join stay attached to the select line).
+ // selectList is the top-level select list (comma breaks); fromRegion is the
+ // top-level from clause (join breaks).
+ selectHead, selectList, fromRegion := false, false, false
+ // forced, when non-empty, is the joiner the NEXT token must use (set after a
+ // select-list comma, where the input has no whitespace to rewrite).
+ forced := ""
+ // glue counts the remaining words of a multi-word join operator ("left outer
+ // join") after its head broke, so the phrase stays on one line.
+ glue := 0
+ pendingWS := false
+
+ for i := 0; i < len(body); {
+ if isViewBodySpaceByte(body[i]) {
+ pendingWS = true
+ i++
+ continue
+ }
+ end := scanViewBodyToken(body, i)
+ tok := body[i:end]
+
+ joiner := ""
+ switch {
+ case out.Len() == 0:
+ // Never emit whitespace before the first token.
+ case forced != "":
+ joiner = forced
+ case glue > 0:
+ // Token is a continuation word of a broken join operator: keep it glued.
+ if pendingWS {
+ joiner = " "
+ }
+ glue--
+ case pendingWS:
+ var joinWords int
+ joiner, joinWords = viewBodyJoiner(body, tok, end, stack, subq, selectHead, fromRegion)
+ if joiner == "\n"+viewSDLIndent && selectHead {
+ selectHead = false
+ selectList = true
+ }
+ glue = joinWords
+ default:
+ }
+ forced = ""
+ pendingWS = false
+ out.WriteString(joiner)
+ out.WriteString(tok)
+
+ // State updates AFTER emitting the token.
+ switch {
+ case tok == "(":
+ sub := viewBodyParenIsSubquery(body, end)
+ stack = append(stack, sub)
+ if sub {
+ subq++
+ }
+ case tok == ")":
+ if n := len(stack); n > 0 {
+ if stack[n-1] {
+ subq--
+ }
+ stack = stack[:n-1]
+ }
+ case tok == ",":
+ if len(stack) == 0 && selectList {
+ forced = "\n" + viewSDLIndent
+ }
+ case isViewBodyWord(tok):
+ if len(stack) == 0 {
+ switch strings.ToLower(tok) {
+ case "select":
+ selectHead, selectList, fromRegion = true, false, false
+ case "from":
+ selectHead, selectList, fromRegion = false, false, true
+ case "where", "having", "limit", "union":
+ selectHead, selectList, fromRegion = false, false, false
+ case "group", "order":
+ if w, _ := peekViewBodyWord(body, end); w == "by" {
+ selectHead, selectList, fromRegion = false, false, false
+ }
+ default:
+ }
+ }
+ default:
+ }
+ i = end
+ }
+ return out.String()
+}
+
+// viewSDLIndent is the continuation indent for select-list items and join lines.
+const viewSDLIndent = " "
+
+// viewBodyJoiner decides what a whitespace run before tok becomes: a clause-keyword
+// newline, an item/join newline+indent, or a single space. The second result is the
+// number of FOLLOWING words that belong to a broken join operator (e.g. 2 for
+// "left outer join"), which the caller keeps glued to the head with plain spaces.
+func viewBodyJoiner(body, tok string, end int, stack []bool, subq int, selectHead, fromRegion bool) (string, int) {
+ if selectHead {
+ if isViewBodyWord(tok) && viewSelectModifiers[strings.ToLower(tok)] {
+ return " ", 0
+ }
+ // First select-list item (any token kind: word, literal, `(`, backtick, …).
+ return "\n" + viewSDLIndent, 0
+ }
+ if !isViewBodyWord(tok) {
+ return " ", 0
+ }
+ w := strings.ToLower(tok)
+ if len(stack) == 0 && isViewClauseBreak(body, end, w) {
+ return "\n", 0
+ }
+ if fromRegion && subq == 0 {
+ if ok, rest := viewJoinPhraseRest(body, end, w); ok {
+ return "\n" + viewSDLIndent, rest
+ }
+ }
+ return " ", 0
+}
+
+// viewSelectModifiers are the tokens that may sit between SELECT and its first list
+// item in the stored form; they stay attached to the select line.
+var viewSelectModifiers = map[string]bool{
+ "all": true,
+ "distinct": true,
+ "distinctrow": true,
+ "high_priority": true,
+ "straight_join": true,
+ "sql_small_result": true,
+ "sql_big_result": true,
+ "sql_buffer_result": true,
+ "sql_cache": true,
+ "sql_no_cache": true,
+ "sql_calc_found_rows": true,
+}
+
+// isViewClauseBreak reports whether the lower-cased word w, whose token ends at end,
+// starts a top-level clause that gets its own line.
+func isViewClauseBreak(body string, end int, w string) bool {
+ switch w {
+ case "select", "from", "where", "having", "limit", "union":
+ return true
+ case "group", "order":
+ next, _ := peekViewBodyWord(body, end)
+ return next == "by"
+ case "with":
+ // Only the trailing WITH [CASCADED|LOCAL] CHECK OPTION breaks; a leading CTE
+ // `with` is body-initial and never reaches a joiner decision.
+ next, rest := peekViewBodyWord(body, end)
+ if next == "cascaded" || next == "local" {
+ next, rest = peekViewBodyWord(body, rest)
+ }
+ if next != "check" {
+ return false
+ }
+ next, rest = peekViewBodyWord(body, rest)
+ if next != "option" {
+ return false
+ }
+ return strings.TrimSpace(body[rest:]) == ""
+ default:
+ return false
+ }
+}
+
+// viewJoinPhraseRest reports whether the lower-cased word w, whose token ends at end,
+// heads a join operator ([natural] [left|right|inner|cross] [outer] join /
+// straight_join) — and, when it does, how many words FOLLOW w in the phrase (they must
+// stay glued to the broken head).
+func viewJoinPhraseRest(body string, end int, w string) (bool, int) {
+ switch w {
+ case "join", "straight_join":
+ return true, 0
+ case "inner", "cross":
+ next, _ := peekViewBodyWord(body, end)
+ if next == "join" {
+ return true, 1
+ }
+ return false, 0
+ case "left", "right":
+ rest := 1
+ next, pos := peekViewBodyWord(body, end)
+ if next == "outer" {
+ next, _ = peekViewBodyWord(body, pos)
+ rest = 2
+ }
+ if next == "join" {
+ return true, rest
+ }
+ return false, 0
+ case "natural":
+ rest := 1
+ next, pos := peekViewBodyWord(body, end)
+ if next == "left" || next == "right" || next == "inner" {
+ next, pos = peekViewBodyWord(body, pos)
+ rest = 2
+ }
+ if next == "outer" {
+ next, _ = peekViewBodyWord(body, pos)
+ rest++
+ }
+ if next == "join" {
+ return true, rest
+ }
+ return false, 0
+ default:
+ return false, 0
+ }
+}
+
+// viewBodyParenIsSubquery classifies the "(" whose token ends at end: true when the
+// next token is the select (or CTE with) keyword, i.e. the paren opens a subquery whose
+// interior must never be broken.
+func viewBodyParenIsSubquery(body string, end int) bool {
+ w, _ := peekViewBodyWord(body, end)
+ return w == "select" || w == "with"
+}
+
+// peekViewBodyWord returns the lower-cased next word token after pos (skipping
+// whitespace) and the offset just past it. It returns "" when the next token is not a
+// bare word (a literal, an identifier, punctuation, or end of body).
+func peekViewBodyWord(body string, pos int) (string, int) {
+ for pos < len(body) && isViewBodySpaceByte(body[pos]) {
+ pos++
+ }
+ if pos >= len(body) || !isViewBodyWordByte(body[pos]) {
+ return "", pos
+ }
+ end := scanViewBodyToken(body, pos)
+ return strings.ToLower(body[pos:end]), end
+}
+
+// isViewBodySpaceByte reports whether b is whitespace between tokens.
+func isViewBodySpaceByte(b byte) bool {
+ return b == ' ' || b == '\t' || b == '\n' || b == '\r'
+}
+
+// scanViewBodyToken returns the end offset of the token starting at i (a non-space
+// byte): a whole '…'/"…" string literal, a whole `…` identifier, a word run, or a
+// single punctuation byte. Literals and identifiers are opaque — breaks can never land
+// inside them. String literals honor backslash escapes — the canonical form SHOW CREATE
+// VIEW always re-prints (see the formatViewBodySDL fail-safe).
+func scanViewBodyToken(body string, i int) int {
+ end, _ := scanViewBodyTokenClosed(body, i)
+ return end
+}
+
+// scanViewBodyTokenClosed is scanViewBodyToken plus a closed flag: false when the token
+// is a string literal or backtick identifier that reaches the end of the body without
+// its closing delimiter (a malformed or mis-tokenized body).
+func scanViewBodyTokenClosed(body string, i int) (int, bool) {
+ switch c := body[i]; c {
+ case '\'', '"':
+ // String literal: backslash escapes the next byte; a doubled delimiter stays
+ // inside. Mirrors stripViewBodyDatabaseQualifier.
+ for j := i + 1; j < len(body); {
+ switch {
+ case body[j] == '\\' && j+1 < len(body):
+ j += 2
+ case body[j] == c:
+ if j+1 < len(body) && body[j+1] == c {
+ j += 2
+ continue
+ }
+ return j + 1, true
+ default:
+ j++
+ }
+ }
+ return len(body), false
+ case '`':
+ // Backtick identifier: only a doubled backtick escapes (no backslash escape).
+ for j := i + 1; j < len(body); {
+ if body[j] == '`' {
+ if j+1 < len(body) && body[j+1] == '`' {
+ j += 2
+ continue
+ }
+ return j + 1, true
+ }
+ j++
+ }
+ return len(body), false
+ default:
+ if !isViewBodyWordByte(body[i]) {
+ return i + 1, true
+ }
+ j := i + 1
+ for j < len(body) && isViewBodyWordByte(body[j]) {
+ j++
+ }
+ return j, true
+ }
+}
+
+// isViewBodyWord reports whether tok is a bare word token (unquoted keyword,
+// function name, or number). Literal and identifier tokens start with their quote
+// byte, which is never a word byte.
+func isViewBodyWord(tok string) bool {
+ return tok != "" && isViewBodyWordByte(tok[0])
+}
+
+// isViewBodyWordByte reports whether b can be part of a bare word. Multibyte (>= 0x80)
+// bytes are treated as word bytes so an unquoted multibyte identifier stays one token.
+func isViewBodyWordByte(b byte) bool {
+ switch {
+ case b >= 'a' && b <= 'z', b >= 'A' && b <= 'Z', b >= '0' && b <= '9':
+ return true
+ case b == '_' || b == '$':
+ return true
+ case b >= 0x80:
+ return true
+ default:
+ return false
+ }
+}
diff --git a/backend/plugin/schema/oceanbase/generate_migration.go b/backend/plugin/schema/oceanbase/generate_migration.go
new file mode 100644
index 0000000000..091c874ca6
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/generate_migration.go
@@ -0,0 +1,1141 @@
+package oceanbase
+
+import (
+ "fmt"
+ "maps"
+ "slices"
+ "strings"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+// tableDrop holds drop operations for deduplication
+type tableDrop struct {
+ checkConstraints map[string]string
+ indexes map[string]*metadatapb.IndexMetadata
+ columns map[string]string
+}
+
+func init() {
+ schema.RegisterGenerateMigration(storepb.Engine_OCEANBASE, generateMigration)
+}
+
+func generateMigration(diff *schema.MetadataDiff) (string, error) {
+ var buf strings.Builder
+
+ // MySQL doesn't have schemas like PostgreSQL, so we skip schema-level changes
+ // We'll focus on table-level changes
+
+ // Phase 1: Drop dependent objects first
+ if err := dropObjectsInOrder(diff, &buf); err != nil {
+ return "", err
+ }
+
+ // Only add blank line if we have drops AND we're about to create something
+ dropPhaseHasContent := buf.Len() > 0
+ createPhaseWillHaveContent := hasCreateOrAlterObjects(diff)
+
+ if dropPhaseHasContent && createPhaseWillHaveContent {
+ _, _ = buf.WriteString("\n")
+ }
+
+ // Phase 2: Create/Alter objects
+ if err := createObjectsInOrder(diff, &buf); err != nil {
+ return "", err
+ }
+
+ return buf.String(), nil
+}
+
+// dropObjectsInOrder drops all objects in the correct order
+func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
+ // Drop triggers first (they depend on tables)
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionDrop && tableDiff.OldTable != nil {
+ for _, trigger := range tableDiff.OldTable.Triggers {
+ if err := writeDropTrigger(buf, trigger.Name); err != nil {
+ return err
+ }
+ }
+ } else if tableDiff.Action == schema.MetadataDiffActionAlter {
+ // Drop triggers that are being removed from altered tables
+ for _, triggerDiff := range tableDiff.TriggerChanges {
+ if triggerDiff.Action == schema.MetadataDiffActionDrop {
+ if err := writeDropTrigger(buf, triggerDiff.OldTrigger.Name); err != nil {
+ return err
+ }
+ }
+ }
+ }
+ }
+
+ // Drop foreign keys from tables being altered
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionAlter {
+ for _, fkDiff := range tableDiff.ForeignKeyChanges {
+ if fkDiff.Action == schema.MetadataDiffActionDrop {
+ if err := writeDropForeignKey(buf, tableDiff.TableName, fkDiff.OldForeignKey.Name); err != nil {
+ return err
+ }
+ }
+ }
+ }
+ }
+
+ // Create temporary views for views being dropped to handle dependencies
+ for _, viewDiff := range diff.ViewChanges {
+ if viewDiff.Action == schema.MetadataDiffActionDrop && viewDiff.OldView != nil {
+ if err := writeTemporaryViewForDrop(buf, viewDiff.ViewName, viewDiff.OldView); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Drop events first (they can reference tables/views)
+ for _, eventDiff := range diff.EventChanges {
+ if eventDiff.Action == schema.MetadataDiffActionDrop {
+ if err := writeDropEvent(buf, eventDiff.EventName); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Drop views
+ for _, viewDiff := range diff.ViewChanges {
+ if viewDiff.Action == schema.MetadataDiffActionDrop {
+ if err := writeDropView(buf, viewDiff.ViewName); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Drop procedures
+ for _, procDiff := range diff.ProcedureChanges {
+ if procDiff.Action == schema.MetadataDiffActionDrop {
+ if err := writeDropProcedure(buf, procDiff.ProcedureName); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Drop functions
+ for _, funcDiff := range diff.FunctionChanges {
+ if funcDiff.Action == schema.MetadataDiffActionDrop {
+ if err := writeDropFunction(buf, funcDiff.FunctionName); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Drop tables in topologically sorted order (respecting foreign key dependencies)
+ if err := dropTablesInDependencyOrder(diff, buf); err != nil {
+ return err
+ }
+
+ // Handle ALTER table drops (constraints, indexes, columns)
+ // Collect all drop operations by table to avoid duplicates
+ dropsPerTable := make(map[string]*tableDrop)
+
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionAlter {
+ tableName := tableDiff.TableName
+ if _, exists := dropsPerTable[tableName]; !exists {
+ dropsPerTable[tableName] = &tableDrop{
+ checkConstraints: make(map[string]string),
+ indexes: make(map[string]*metadatapb.IndexMetadata),
+ columns: make(map[string]string),
+ }
+ }
+
+ // Collect check constraints to drop
+ for _, checkDiff := range tableDiff.CheckConstraintChanges {
+ if checkDiff.Action == schema.MetadataDiffActionDrop {
+ dropsPerTable[tableName].checkConstraints[checkDiff.OldCheckConstraint.Name] = checkDiff.OldCheckConstraint.Name
+ }
+ }
+
+ // Collect indexes to drop
+ for _, indexDiff := range tableDiff.IndexChanges {
+ if indexDiff.Action == schema.MetadataDiffActionDrop {
+ dropsPerTable[tableName].indexes[indexDiff.OldIndex.Name] = indexDiff.OldIndex
+ }
+ }
+
+ // Collect columns to drop
+ for _, colDiff := range tableDiff.ColumnChanges {
+ if colDiff.Action == schema.MetadataDiffActionDrop {
+ dropsPerTable[tableName].columns[colDiff.OldColumn.Name] = colDiff.OldColumn.Name
+ }
+ }
+ }
+ }
+
+ // Execute the deduplicated drop operations. Every map here is walked in name
+ // order: the same diff has to produce the same DDL, byte for byte, or a
+ // generated migration is unreviewable and unrepeatable.
+ for _, tableName := range slices.Sorted(maps.Keys(dropsPerTable)) {
+ drops := dropsPerTable[tableName]
+
+ // Drop check constraints
+ for _, constraintName := range slices.Sorted(maps.Keys(drops.checkConstraints)) {
+ if err := writeDropCheckConstraint(buf, tableName, constraintName); err != nil {
+ return err
+ }
+ }
+
+ // Drop indexes (handle primary key specially)
+ for _, indexName := range slices.Sorted(maps.Keys(drops.indexes)) {
+ index := drops.indexes[indexName]
+ if index.Primary {
+ if err := writeDropPrimaryKey(buf, tableName); err != nil {
+ return err
+ }
+ } else {
+ if err := writeDropIndex(buf, tableName, index.Name); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Drop columns
+ for _, columnName := range slices.Sorted(maps.Keys(drops.columns)) {
+ if err := writeDropColumn(buf, tableName, columnName); err != nil {
+ return err
+ }
+ }
+ }
+
+ return nil
+}
+
+// dropTablesInDependencyOrder drops tables in the correct order based on foreign key dependencies
+func dropTablesInDependencyOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
+ // Build a graph of table dependencies
+ graph := parserbase.NewGraph()
+ tablesToDrop := make(map[string]*schema.TableDiff)
+
+ // Add all tables to be dropped to the graph
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionDrop {
+ graph.AddNode(tableDiff.TableName)
+ tablesToDrop[tableDiff.TableName] = tableDiff
+ }
+ }
+
+ // Add edges for foreign key dependencies
+ // Edge from table with FK to referenced table (for dropping order)
+ for _, name := range slices.Sorted(maps.Keys(tablesToDrop)) {
+ tableDiff := tablesToDrop[name]
+ if tableDiff.OldTable != nil {
+ for _, fk := range tableDiff.OldTable.ForeignKeys {
+ if _, exists := tablesToDrop[fk.ReferencedTable]; exists {
+ // Add edge from this table to referenced table
+ // This ensures this table is dropped before referenced table
+ graph.AddEdge(tableDiff.TableName, fk.ReferencedTable)
+ }
+ }
+ }
+ }
+
+ // Get topological order
+ orderedTables, err := graph.TopologicalSort()
+ if err != nil {
+ // If there's a cycle, fall back to dropping foreign keys first
+ // Drop all foreign keys from tables being dropped
+ for _, name := range slices.Sorted(maps.Keys(tablesToDrop)) {
+ tableDiff := tablesToDrop[name]
+ if tableDiff.OldTable != nil {
+ for _, fk := range tableDiff.OldTable.ForeignKeys {
+ if err := writeDropForeignKey(buf, tableDiff.TableName, fk.Name); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
+ // Then drop tables in name order; the cycle leaves no better one.
+ for _, name := range slices.Sorted(maps.Keys(tablesToDrop)) {
+ if err := writeDropTable(buf, tablesToDrop[name].TableName); err != nil {
+ return err
+ }
+ }
+ } else {
+ // Drop tables in topological order
+ for _, tableName := range orderedTables {
+ if err := writeDropTable(buf, tableName); err != nil {
+ return err
+ }
+ }
+ }
+
+ return nil
+}
+
+// createObjectsInOrder creates all objects in the correct order
+func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
+ // Create dummy views first for all new views to handle dependencies
+ // This must be done before creating tables as triggers might reference views
+ for _, viewDiff := range diff.ViewChanges {
+ if viewDiff.Action == schema.MetadataDiffActionCreate && viewDiff.NewView != nil {
+ if err := writeCreateTemporaryView(buf, viewDiff.ViewName, viewDiff.NewView); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Create tables (without foreign keys first)
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionCreate {
+ if err := writeCreateTableWithoutForeignKeys(buf, tableDiff.TableName, tableDiff.NewTable); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Add foreign keys after all tables are created
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionCreate && tableDiff.NewTable != nil {
+ for _, fk := range tableDiff.NewTable.ForeignKeys {
+ if err := writeAddForeignKey(buf, tableDiff.TableName, fk); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
+ // Handle ALTER table operations
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionAlter {
+ if err := generateAlterTable(tableDiff, buf); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Create or replace views with actual definitions
+ for _, viewDiff := range diff.ViewChanges {
+ switch viewDiff.Action {
+ case schema.MetadataDiffActionCreate:
+ // Use CREATE OR REPLACE since we already have a dummy view
+ if err := writeCreateOrReplaceView(buf, viewDiff.ViewName, viewDiff.NewView); err != nil {
+ return err
+ }
+ case schema.MetadataDiffActionAlter:
+ // MySQL requires CREATE OR REPLACE for views
+ if err := writeCreateOrReplaceView(buf, viewDiff.ViewName, viewDiff.NewView); err != nil {
+ return err
+ }
+ default:
+ // No action needed for other diff actions
+ }
+ }
+
+ // Create functions
+ for _, funcDiff := range diff.FunctionChanges {
+ if funcDiff.Action == schema.MetadataDiffActionCreate || funcDiff.Action == schema.MetadataDiffActionAlter {
+ if err := writeFunctionDiff(buf, funcDiff); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Create procedures
+ for _, procDiff := range diff.ProcedureChanges {
+ if procDiff.Action == schema.MetadataDiffActionCreate || procDiff.Action == schema.MetadataDiffActionAlter {
+ if err := writeProcedureDiff(buf, procDiff); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Create events
+ for _, eventDiff := range diff.EventChanges {
+ if eventDiff.Action == schema.MetadataDiffActionCreate || eventDiff.Action == schema.MetadataDiffActionAlter {
+ if err := writeEventDiff(buf, eventDiff); err != nil {
+ return err
+ }
+ }
+ }
+
+ return nil
+}
+
+func generateAlterTable(tableDiff *schema.TableDiff, buf *strings.Builder) error {
+ // Add columns first
+ for _, colDiff := range tableDiff.ColumnChanges {
+ if colDiff.Action == schema.MetadataDiffActionCreate {
+ if err := writeAddColumn(buf, tableDiff.TableName, colDiff.NewColumn); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Modify columns
+ for _, colDiff := range tableDiff.ColumnChanges {
+ if colDiff.Action == schema.MetadataDiffActionAlter {
+ if err := writeModifyColumn(buf, tableDiff.TableName, colDiff.NewColumn); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Add indexes
+ for _, indexDiff := range tableDiff.IndexChanges {
+ if indexDiff.Action == schema.MetadataDiffActionCreate {
+ if indexDiff.NewIndex.Primary {
+ if err := writeAddPrimaryKey(buf, tableDiff.TableName, indexDiff.NewIndex); err != nil {
+ return err
+ }
+ } else if indexDiff.NewIndex.Unique {
+ if err := writeAddUniqueKey(buf, tableDiff.TableName, indexDiff.NewIndex); err != nil {
+ return err
+ }
+ } else {
+ if err := writeCreateIndex(buf, tableDiff.TableName, indexDiff.NewIndex); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
+ // Add check constraints
+ for _, checkDiff := range tableDiff.CheckConstraintChanges {
+ if checkDiff.Action == schema.MetadataDiffActionCreate {
+ if err := writeAddCheckConstraint(buf, tableDiff.TableName, checkDiff.NewCheckConstraint); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Add foreign keys last
+ for _, fkDiff := range tableDiff.ForeignKeyChanges {
+ if fkDiff.Action == schema.MetadataDiffActionCreate {
+ if err := writeAddForeignKey(buf, tableDiff.TableName, fkDiff.NewForeignKey); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Add triggers last
+ for _, triggerDiff := range tableDiff.TriggerChanges {
+ if triggerDiff.Action == schema.MetadataDiffActionCreate {
+ if err := writeCreateTrigger(buf, tableDiff.TableName, triggerDiff.NewTrigger); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Handle table comment changes
+ if tableDiff.OldTable != nil && tableDiff.NewTable != nil &&
+ tableDiff.OldTable.Comment != tableDiff.NewTable.Comment {
+ if err := writeAlterTableComment(buf, tableDiff.TableName, tableDiff.NewTable.Comment); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// Write functions for various DDL statements
+
+func writeDropTrigger(buf *strings.Builder, trigger string) error {
+ _, _ = buf.WriteString("DROP TRIGGER IF EXISTS `")
+ _, _ = buf.WriteString(trigger)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropForeignKey(buf *strings.Builder, table, constraint string) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` DROP FOREIGN KEY `")
+ _, _ = buf.WriteString(constraint)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropView(buf *strings.Builder, view string) error {
+ _, _ = buf.WriteString("DROP VIEW IF EXISTS `")
+ _, _ = buf.WriteString(view)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropProcedure(buf *strings.Builder, procedure string) error {
+ _, _ = buf.WriteString("DROP PROCEDURE IF EXISTS `")
+ _, _ = buf.WriteString(procedure)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropFunction(buf *strings.Builder, function string) error {
+ _, _ = buf.WriteString("DROP FUNCTION IF EXISTS `")
+ _, _ = buf.WriteString(function)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropEvent(buf *strings.Builder, event string) error {
+ _, _ = buf.WriteString("DROP EVENT IF EXISTS `")
+ _, _ = buf.WriteString(event)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropTable(buf *strings.Builder, table string) error {
+ _, _ = buf.WriteString("DROP TABLE IF EXISTS `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropCheckConstraint(buf *strings.Builder, table, constraint string) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` DROP CONSTRAINT `")
+ _, _ = buf.WriteString(constraint)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropIndex(buf *strings.Builder, table, index string) error {
+ _, _ = buf.WriteString("DROP INDEX `")
+ _, _ = buf.WriteString(index)
+ _, _ = buf.WriteString("` ON `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeDropPrimaryKey(buf *strings.Builder, table string) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` DROP PRIMARY KEY;\n\n")
+ return nil
+}
+
+func writeDropColumn(buf *strings.Builder, table, column string) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` DROP COLUMN `")
+ _, _ = buf.WriteString(column)
+ _, _ = buf.WriteString("`;\n\n")
+ return nil
+}
+
+func writeCreateTableWithoutForeignKeys(buf *strings.Builder, tableName string, table *metadatapb.TableMetadata) error {
+ _, _ = buf.WriteString("CREATE TABLE IF NOT EXISTS `")
+ _, _ = buf.WriteString(tableName)
+ _, _ = buf.WriteString("` (\n")
+
+ // Write columns
+ for i, col := range table.Columns {
+ if i > 0 {
+ _, _ = buf.WriteString(",\n")
+ }
+ _, _ = buf.WriteString(" `")
+ _, _ = buf.WriteString(col.Name)
+ _, _ = buf.WriteString("` ")
+ // Reuse the shared column-definition body so the inline CREATE TABLE and the
+ // ADD/MODIFY COLUMN paths emit the same canonical attribute order (notably the
+ // generated-column ordering: GENERATED ALWAYS AS (...) precedes NOT NULL/SRID).
+ writeColumnDefinitionBody(buf, col)
+ }
+ writeSystemTimePeriod(buf, table)
+
+ // Write primary key constraint inline if exists
+ for _, index := range table.Indexes {
+ if index.Primary {
+ _, _ = buf.WriteString(",\n PRIMARY KEY (")
+ for i, expr := range index.Expressions {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+ _, _ = buf.WriteString("`")
+ _, _ = buf.WriteString(expr)
+ _, _ = buf.WriteString("`")
+ }
+ _, _ = buf.WriteString(")")
+ break
+ }
+ }
+
+ // Write unique constraints inline
+ for _, index := range table.Indexes {
+ if index.Unique && !index.Primary {
+ _, _ = buf.WriteString(",\n UNIQUE KEY `")
+ _, _ = buf.WriteString(index.Name)
+ _, _ = buf.WriteString("` (")
+ for i, expr := range index.Expressions {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+ _, _ = buf.WriteString("`")
+ _, _ = buf.WriteString(expr)
+ _, _ = buf.WriteString("`")
+ }
+ _, _ = buf.WriteString(")")
+ }
+ }
+
+ // Write check constraints
+ for _, check := range table.CheckConstraints {
+ _, _ = buf.WriteString(",\n CONSTRAINT `")
+ _, _ = buf.WriteString(check.Name)
+ _, _ = buf.WriteString("` CHECK (")
+ _, _ = buf.WriteString(check.Expression)
+ _, _ = buf.WriteString(")")
+ }
+
+ _, _ = buf.WriteString("\n)")
+
+ // Write table options
+ if table.Engine != "" {
+ _, _ = buf.WriteString(" ENGINE=")
+ _, _ = buf.WriteString(table.Engine)
+ }
+ if table.Charset != "" {
+ _, _ = buf.WriteString(" DEFAULT CHARSET=")
+ _, _ = buf.WriteString(table.Charset)
+ }
+ if table.Collation != "" {
+ _, _ = buf.WriteString(" COLLATE=")
+ _, _ = buf.WriteString(table.Collation)
+ }
+ if table.Comment != "" {
+ _, _ = buf.WriteString(" COMMENT='")
+ _, _ = buf.WriteString(table.Comment)
+ _, _ = buf.WriteString("'")
+ }
+ writeSystemVersioningOption(buf, table)
+
+ // Partitioning is part of the table definition, so it has to be emitted here: MySQL
+ // cannot partition a table after the fact without rewriting it. printPartitionClause
+ // writes a self-contained /*!50100 ... */ block, matching the schema dump.
+ if len(table.Partitions) > 0 {
+ if err := printPartitionClause(buf, table.Partitions, table.Engine); err != nil {
+ return err
+ }
+ }
+
+ _, _ = buf.WriteString(";\n")
+
+ // Create non-unique indexes separately
+ for _, index := range table.Indexes {
+ if !index.Primary && !index.Unique {
+ if err := writeCreateIndex(buf, tableName, index); err != nil {
+ return err
+ }
+ }
+ }
+
+ // Add extra newline after table creation for readability
+ _, _ = buf.WriteString("\n")
+
+ // Note: Foreign keys are NOT created here - they will be added separately
+
+ return nil
+}
+
+// writeColumnDefinitionBody emits the column definition following the leading
+// “ `name` “ — the type and every attribute in canonical order. MySQL's grammar puts
+// the attributes in a different order for a generated column (the GENERATED ALWAYS AS
+// clause precedes NOT NULL and SRID) than for a regular column, so the body branches on
+// column.Generation. Both branches emit INVISIBLE before COMMENT, matching MySQL's
+// canonical SHOW CREATE order. Shared by writeAddColumn (ADD COLUMN) and writeModifyColumn
+// (MODIFY COLUMN), which differ only in the leading clause.
+func writeColumnDefinitionBody(buf *strings.Builder, column *metadatapb.ColumnMetadata) {
+ _, _ = buf.WriteString(column.Type)
+
+ if column.CharacterSet != "" {
+ _, _ = buf.WriteString(" CHARACTER SET ")
+ _, _ = buf.WriteString(column.CharacterSet)
+ }
+ if column.Collation != "" {
+ _, _ = buf.WriteString(" COLLATE ")
+ _, _ = buf.WriteString(column.Collation)
+ }
+
+ if column.Generation != nil && column.Generation.Expression != "" {
+ writeGeneratedColumnAttributes(buf, column)
+ return
+ }
+
+ if !column.Nullable {
+ _, _ = buf.WriteString(" NOT NULL")
+ }
+
+ // Spatial SRID (MySQL 8.0): after NOT NULL, before DEFAULT — the SDL dumper's
+ // canonical position (see printColumnClause).
+ writeColumnSRIDAttribute(buf, column)
+
+ // Handle AUTO_INCREMENT before default value
+ if hasAutoIncrement(column) {
+ _, _ = buf.WriteString(" AUTO_INCREMENT")
+ } else if hasDefaultValue(column) && !hasAutoIncrement(column) {
+ _, _ = buf.WriteString(" DEFAULT ")
+ _, _ = buf.WriteString(getDefaultExpression(column))
+ }
+
+ // Handle ON UPDATE
+ if column.OnUpdate != "" {
+ _, _ = buf.WriteString(" ON UPDATE ")
+ _, _ = buf.WriteString(column.OnUpdate)
+ }
+
+ // INVISIBLE column (MySQL 8.0.23+): before COMMENT, matching the SHOW CREATE canonical
+ // order and the SDL dumper (printColumnClause).
+ writeColumnInvisibleAttribute(buf, column)
+
+ if column.Comment != "" {
+ _, _ = buf.WriteString(" COMMENT '")
+ _, _ = buf.WriteString(column.Comment)
+ _, _ = buf.WriteString("'")
+ }
+}
+
+// writeGeneratedColumnAttributes emits the attributes of a generated column (type and
+// charset/collation already written) in MySQL's canonical order:
+//
+// GENERATED ALWAYS AS (expr) STORED|VIRTUAL [NOT NULL] [SRID] [INVISIBLE] [COMMENT]
+//
+// The GENERATED ALWAYS AS clause must precede NOT NULL and the SRID attribute — the
+// order the current MySQL grammar accepts and that SHOW CREATE emits (verified against
+// 8.0.32). INVISIBLE precedes COMMENT, matching MySQL's canonical order for both regular
+// and generated columns. Generated columns never carry DEFAULT / AUTO_INCREMENT / ON
+// UPDATE, so those clauses are intentionally omitted.
+func writeGeneratedColumnAttributes(buf *strings.Builder, column *metadatapb.ColumnMetadata) {
+ if isSystemTimeColumn(column) {
+ writeSystemTimeColumnAttribute(buf, column)
+ } else {
+ _, _ = buf.WriteString(" GENERATED ALWAYS AS (")
+ _, _ = buf.WriteString(column.Generation.Expression)
+ _, _ = buf.WriteString(") ")
+ switch column.Generation.Type {
+ case metadatapb.GenerationMetadata_TYPE_STORED:
+ _, _ = buf.WriteString("STORED")
+ case metadatapb.GenerationMetadata_TYPE_VIRTUAL:
+ _, _ = buf.WriteString("VIRTUAL")
+ default:
+ // Default to VIRTUAL for unknown types
+ _, _ = buf.WriteString("VIRTUAL")
+ }
+ }
+
+ if !column.Nullable {
+ _, _ = buf.WriteString(" NOT NULL")
+ }
+
+ // Spatial SRID (MySQL 8.0): after NOT NULL for a generated column, matching the
+ // SHOW CREATE canonical position.
+ writeColumnSRIDAttribute(buf, column)
+
+ // INVISIBLE column (MySQL 8.0.23+): for a generated column this precedes COMMENT,
+ // matching the SHOW CREATE canonical order.
+ writeColumnInvisibleAttribute(buf, column)
+
+ if column.Comment != "" {
+ _, _ = buf.WriteString(" COMMENT '")
+ _, _ = buf.WriteString(column.Comment)
+ _, _ = buf.WriteString("'")
+ }
+}
+
+func writeAddColumn(buf *strings.Builder, table string, column *metadatapb.ColumnMetadata) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` ADD COLUMN `")
+ _, _ = buf.WriteString(column.Name)
+ _, _ = buf.WriteString("` ")
+ writeColumnDefinitionBody(buf, column)
+
+ // TODO: Add column position support (FIRST, AFTER column_name)
+
+ _, _ = buf.WriteString(";\n\n")
+ return nil
+}
+
+func writeModifyColumn(buf *strings.Builder, table string, column *metadatapb.ColumnMetadata) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` MODIFY COLUMN `")
+ _, _ = buf.WriteString(column.Name)
+ _, _ = buf.WriteString("` ")
+ writeColumnDefinitionBody(buf, column)
+
+ _, _ = buf.WriteString(";\n\n")
+ return nil
+}
+
+func writeCreateIndex(buf *strings.Builder, table string, index *metadatapb.IndexMetadata) error {
+ _, _ = buf.WriteString("CREATE ")
+ // Handle special index types
+ if strings.ToUpper(index.Type) == "FULLTEXT" {
+ _, _ = buf.WriteString("FULLTEXT ")
+ } else if strings.ToUpper(index.Type) == "SPATIAL" {
+ _, _ = buf.WriteString("SPATIAL ")
+ }
+
+ _, _ = buf.WriteString("INDEX `")
+ _, _ = buf.WriteString(index.Name)
+ _, _ = buf.WriteString("` ON `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` (")
+
+ for i, expr := range index.Expressions {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+
+ // Check if this is a functional expression or a column name
+ // Functional expressions start with ( and end with )
+ if strings.HasPrefix(expr, "(") && strings.HasSuffix(expr, ")") {
+ // This is a functional expression, unescape quotes and write it as-is
+ unescapedExpr := strings.ReplaceAll(expr, "\\'", "'")
+ _, _ = buf.WriteString(unescapedExpr)
+ } else {
+ // This is a column name, wrap it in backticks
+ _, _ = buf.WriteString("`")
+ _, _ = buf.WriteString(expr)
+ _, _ = buf.WriteString("`")
+ }
+
+ // Handle column length for text/blob columns
+ // SPATIAL indexes do not support key lengths, and functional expressions don't use key lengths
+ if i < len(index.KeyLength) && index.KeyLength[i] > 0 && strings.ToUpper(index.Type) != "SPATIAL" &&
+ (!strings.HasPrefix(expr, "(") || !strings.HasSuffix(expr, ")")) {
+ _, _ = fmt.Fprintf(buf, "(%d)", index.KeyLength[i])
+ }
+ }
+
+ _, _ = buf.WriteString(")")
+
+ // Add index type if specified and not default
+ if index.Type != "" && index.Type != "BTREE" &&
+ strings.ToUpper(index.Type) != "FULLTEXT" &&
+ strings.ToUpper(index.Type) != "SPATIAL" {
+ _, _ = buf.WriteString(" USING ")
+ _, _ = buf.WriteString(index.Type)
+ }
+
+ if index.Comment != "" {
+ _, _ = buf.WriteString(" COMMENT '")
+ _, _ = buf.WriteString(index.Comment)
+ _, _ = buf.WriteString("'")
+ }
+
+ _, _ = buf.WriteString(";\n\n")
+ return nil
+}
+
+func writeAddPrimaryKey(buf *strings.Builder, table string, index *metadatapb.IndexMetadata) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` ADD PRIMARY KEY (")
+
+ for i, expr := range index.Expressions {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+ _, _ = buf.WriteString("`")
+ _, _ = buf.WriteString(expr)
+ _, _ = buf.WriteString("`")
+ }
+
+ _, _ = buf.WriteString(");\n\n")
+ return nil
+}
+
+func writeAddUniqueKey(buf *strings.Builder, table string, index *metadatapb.IndexMetadata) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` ADD UNIQUE KEY `")
+ _, _ = buf.WriteString(index.Name)
+ _, _ = buf.WriteString("` (")
+
+ for i, expr := range index.Expressions {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+ _, _ = buf.WriteString("`")
+ _, _ = buf.WriteString(expr)
+ _, _ = buf.WriteString("`")
+ }
+
+ _, _ = buf.WriteString(");\n\n")
+ return nil
+}
+
+func writeAddCheckConstraint(buf *strings.Builder, table string, check *metadatapb.CheckConstraintMetadata) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` ADD CONSTRAINT `")
+ _, _ = buf.WriteString(check.Name)
+ _, _ = buf.WriteString("` CHECK ")
+ // The expression already includes parentheses from the parser
+ _, _ = buf.WriteString(check.Expression)
+ _, _ = buf.WriteString(";\n\n")
+ return nil
+}
+
+func writeAddForeignKey(buf *strings.Builder, table string, fk *metadatapb.ForeignKeyMetadata) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(table)
+ _, _ = buf.WriteString("` ADD CONSTRAINT `")
+ _, _ = buf.WriteString(fk.Name)
+ _, _ = buf.WriteString("` FOREIGN KEY (")
+
+ for i, col := range fk.Columns {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+ _, _ = buf.WriteString("`")
+ _, _ = buf.WriteString(col)
+ _, _ = buf.WriteString("`")
+ }
+
+ _, _ = buf.WriteString(") REFERENCES `")
+ _, _ = buf.WriteString(fk.ReferencedTable)
+ _, _ = buf.WriteString("` (")
+
+ for i, col := range fk.ReferencedColumns {
+ if i > 0 {
+ _, _ = buf.WriteString(", ")
+ }
+ _, _ = buf.WriteString("`")
+ _, _ = buf.WriteString(col)
+ _, _ = buf.WriteString("`")
+ }
+
+ _, _ = buf.WriteString(")")
+
+ if fk.OnUpdate != "" {
+ _, _ = buf.WriteString(" ON UPDATE ")
+ _, _ = buf.WriteString(fk.OnUpdate)
+ }
+ if fk.OnDelete != "" {
+ _, _ = buf.WriteString(" ON DELETE ")
+ _, _ = buf.WriteString(fk.OnDelete)
+ }
+
+ _, _ = buf.WriteString(";\n\n")
+ return nil
+}
+
+func writeCreateOrReplaceView(buf *strings.Builder, viewName string, view *metadatapb.ViewMetadata) error {
+ _, _ = buf.WriteString("CREATE OR REPLACE VIEW `")
+ _, _ = buf.WriteString(viewName)
+ _, _ = buf.WriteString("` AS ")
+ _, _ = buf.WriteString(view.Definition)
+
+ if !strings.HasSuffix(strings.TrimSpace(view.Definition), ";") {
+ _, _ = buf.WriteString(";")
+ }
+ _, _ = buf.WriteString("\n\n")
+
+ // Note: MySQL doesn't support adding comments directly to views via DDL
+ // View comments are stored in information_schema.VIEWS but cannot be set via CREATE VIEW
+ // The comment field is read-only
+ return nil
+}
+
+func writeFunctionDiff(buf *strings.Builder, funcDiff *schema.FunctionDiff) error {
+ if funcDiff.Action == schema.MetadataDiffActionCreate || funcDiff.Action == schema.MetadataDiffActionAlter {
+ // Don't add DELIMITER statements - the definition should already be complete
+ _, _ = buf.WriteString(funcDiff.NewFunction.Definition)
+ if !strings.HasSuffix(strings.TrimSpace(funcDiff.NewFunction.Definition), ";") {
+ _, _ = buf.WriteString(";")
+ }
+ _, _ = buf.WriteString("\n\n")
+
+ // Note: MySQL doesn't support ALTER FUNCTION ... COMMENT syntax
+ // Function comments must be set during CREATE FUNCTION statement
+ // The definition should already include the comment if needed
+ }
+ return nil
+}
+
+func writeProcedureDiff(buf *strings.Builder, procDiff *schema.ProcedureDiff) error {
+ if procDiff.Action == schema.MetadataDiffActionCreate || procDiff.Action == schema.MetadataDiffActionAlter {
+ // Don't add DELIMITER statements - the definition should already be complete
+ _, _ = buf.WriteString(procDiff.NewProcedure.Definition)
+ if !strings.HasSuffix(strings.TrimSpace(procDiff.NewProcedure.Definition), ";") {
+ _, _ = buf.WriteString(";")
+ }
+ _, _ = buf.WriteString("\n\n")
+
+ // Note: MySQL doesn't support ALTER PROCEDURE ... COMMENT syntax
+ // Procedure comments must be set during CREATE PROCEDURE statement
+ // The definition should already include the comment if needed
+ }
+ return nil
+}
+
+func writeEventDiff(buf *strings.Builder, eventDiff *schema.EventDiff) error {
+ if eventDiff.Action == schema.MetadataDiffActionCreate || eventDiff.Action == schema.MetadataDiffActionAlter {
+ // An event synced without its CREATE EVENT, as on OceanBase, has nothing faithful to write.
+ if eventDiff.NewEvent.Definition == "" {
+ return nil
+ }
+ // Write the event definition
+ _, _ = buf.WriteString(eventDiff.NewEvent.Definition)
+ if !strings.HasSuffix(strings.TrimSpace(eventDiff.NewEvent.Definition), ";") {
+ _, _ = buf.WriteString(";")
+ }
+ _, _ = buf.WriteString("\n\n")
+
+ // Note: MySQL doesn't support ALTER EVENT ... COMMENT syntax
+ // Event comments must be set during CREATE EVENT statement
+ // The definition should already include the comment if needed
+ }
+ return nil
+}
+
+func writeAlterTableComment(buf *strings.Builder, tableName, comment string) error {
+ _, _ = buf.WriteString("ALTER TABLE `")
+ _, _ = buf.WriteString(tableName)
+ _, _ = buf.WriteString("` COMMENT = '")
+ _, _ = buf.WriteString(comment)
+ _, _ = buf.WriteString("';\n\n")
+ return nil
+}
+
+// Helper functions
+
+func hasCreateOrAlterObjects(diff *schema.MetadataDiff) bool {
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.Action == schema.MetadataDiffActionCreate || tableDiff.Action == schema.MetadataDiffActionAlter {
+ return true
+ }
+ }
+ for _, viewDiff := range diff.ViewChanges {
+ if viewDiff.Action == schema.MetadataDiffActionCreate || viewDiff.Action == schema.MetadataDiffActionAlter {
+ return true
+ }
+ }
+ for _, funcDiff := range diff.FunctionChanges {
+ if funcDiff.Action == schema.MetadataDiffActionCreate || funcDiff.Action == schema.MetadataDiffActionAlter {
+ return true
+ }
+ }
+ for _, procDiff := range diff.ProcedureChanges {
+ if procDiff.Action == schema.MetadataDiffActionCreate || procDiff.Action == schema.MetadataDiffActionAlter {
+ return true
+ }
+ }
+ for _, eventDiff := range diff.EventChanges {
+ if eventDiff.Action == schema.MetadataDiffActionCreate || eventDiff.Action == schema.MetadataDiffActionAlter {
+ return true
+ }
+ }
+ return false
+}
+
+func getDefaultExpression(column *metadatapb.ColumnMetadata) string {
+ if column == nil {
+ return ""
+ }
+
+ // Use the unified Default field
+ if column.Default != "" {
+ return column.Default
+ }
+
+ return ""
+}
+
+func hasDefaultValue(column *metadatapb.ColumnMetadata) bool {
+ if column == nil {
+ return false
+ }
+ // Don't treat AUTO_INCREMENT as a default value
+ if strings.EqualFold(column.GetDefault(), "AUTO_INCREMENT") {
+ return false
+ }
+ return column.Default != ""
+}
+
+func hasAutoIncrement(column *metadatapb.ColumnMetadata) bool {
+ if column == nil {
+ return false
+ }
+ // Check if column has AUTO_INCREMENT in default field
+ return strings.EqualFold(column.GetDefault(), "AUTO_INCREMENT")
+}
+
+func writeTemporaryViewForDrop(buf *strings.Builder, viewName string, view *metadatapb.ViewMetadata) error {
+ // Create a temporary view with SELECT 1 AS column_name structure
+ // to satisfy other views that depend on this view
+ _, _ = buf.WriteString("CREATE OR REPLACE VIEW `")
+ _, _ = buf.WriteString(viewName)
+ _, _ = buf.WriteString("` AS SELECT")
+
+ for i, column := range view.Columns {
+ if i > 0 {
+ _, _ = buf.WriteString(",")
+ }
+ _, _ = buf.WriteString(" 1 AS `")
+ _, _ = buf.WriteString(column.Name)
+ _, _ = buf.WriteString("`")
+ }
+
+ // If no columns, create a dummy view
+ if len(view.Columns) == 0 {
+ _, _ = buf.WriteString(" 1")
+ }
+
+ _, _ = buf.WriteString(";\n\n")
+ return nil
+}
+
+func writeCreateTemporaryView(buf *strings.Builder, viewName string, view *metadatapb.ViewMetadata) error {
+ // Create a temporary view with SELECT 1 AS column_name structure
+ // to satisfy views that depend on this view before we create the real one
+ _, _ = buf.WriteString("CREATE VIEW `")
+ _, _ = buf.WriteString(viewName)
+ _, _ = buf.WriteString("` AS SELECT")
+
+ for i, column := range view.Columns {
+ if i > 0 {
+ _, _ = buf.WriteString(",")
+ }
+ _, _ = buf.WriteString(" 1 AS `")
+ _, _ = buf.WriteString(column.Name)
+ _, _ = buf.WriteString("`")
+ }
+
+ // If no columns, create a dummy view
+ if len(view.Columns) == 0 {
+ _, _ = buf.WriteString(" 1")
+ }
+
+ _, _ = buf.WriteString(";\n\n")
+ return nil
+}
+
+func writeCreateTrigger(buf *strings.Builder, tableName string, trigger *metadatapb.TriggerMetadata) error {
+ // Don't add DELIMITER statements - construct the complete trigger statement
+ _, _ = buf.WriteString("CREATE TRIGGER `")
+ _, _ = buf.WriteString(trigger.Name)
+ _, _ = buf.WriteString("` ")
+ _, _ = buf.WriteString(trigger.Timing)
+ _, _ = buf.WriteString(" ")
+ _, _ = buf.WriteString(trigger.Event)
+ _, _ = buf.WriteString(" ON `")
+ _, _ = buf.WriteString(tableName)
+ _, _ = buf.WriteString("` FOR EACH ROW ")
+ _, _ = buf.WriteString(trigger.Body)
+ if !strings.HasSuffix(strings.TrimSpace(trigger.Body), ";") {
+ _, _ = buf.WriteString(";")
+ }
+ _, _ = buf.WriteString("\n\n")
+
+ // Note: MySQL doesn't support specifying comments in CREATE TRIGGER syntax
+ // Trigger comments are retrieved from INFORMATION_SCHEMA.TRIGGERS but cannot be set via DDL
+ // The comment field is read-only and set by MySQL itself
+ return nil
+}
diff --git a/backend/plugin/schema/oceanbase/generate_migration_test.go b/backend/plugin/schema/oceanbase/generate_migration_test.go
new file mode 100644
index 0000000000..ec346eba43
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/generate_migration_test.go
@@ -0,0 +1,30 @@
+package oceanbase
+
+import (
+ "testing"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+func TestDiffMigrationEventWithoutDefinition(t *testing.T) {
+ databaseOf := func(events ...*metadatapb.EventMetadata) *model.DatabaseMetadata {
+ return model.NewDatabaseMetadata(&metadatapb.DatabaseSchemaMetadata{
+ Name: "db",
+ Schemas: []*metadatapb.SchemaMetadata{{Name: "", Events: events}},
+ }, nil, &storepb.DatabaseConfig{}, storepb.Engine_OCEANBASE, true)
+ }
+ withEvent := databaseOf(&metadatapb.EventMetadata{Name: "e"})
+
+ migration, err := schema.DiffMigration(storepb.Engine_OCEANBASE, databaseOf(), withEvent)
+ require.NoError(t, err)
+ require.Empty(t, migration)
+
+ migration, err = schema.DiffMigration(storepb.Engine_OCEANBASE, withEvent, databaseOf())
+ require.NoError(t, err)
+ require.Equal(t, "DROP EVENT IF EXISTS `e`;\n\n", migration)
+}
diff --git a/backend/plugin/schema/oceanbase/get_database_definition.go b/backend/plugin/schema/oceanbase/get_database_definition.go
new file mode 100644
index 0000000000..b2fc63e92b
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/get_database_definition.go
@@ -0,0 +1,2078 @@
+package oceanbase
+
+import (
+ "cmp"
+ "fmt"
+ "io"
+ "slices"
+ "strconv"
+ "strings"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+ "github.com/pkg/errors"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+const (
+ disableUniqueAndForeignKeyCheckStmt = "SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;\n"
+ restoreUniqueAndForeignKeyCheckStmt = "SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;\nSET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;\n"
+
+ emptyCommentLine = "--\n"
+ tempViewHeader = "-- Temporary view structure for "
+ tableHeader = "-- Table structure for "
+ viewHeader = "-- View structure for "
+ functionHeader = "-- Function structure for "
+ procedureHeader = "-- Procedure structure for "
+ triggerHeader = "-- Trigger structure for "
+ eventHeader = "-- Event structure for "
+ setCharacterSetClient = "SET character_set_client = "
+ setCharacterSetResult = "SET character_set_results = "
+ setCollation = "SET collation_connection = "
+ setSQLMode = "SET sql_mode = "
+ setTimezone = "SET time_zone = "
+ delimiterDoubleSemi = "DELIMITER ;;\n"
+ delimiterSemi = "DELIMITER ;\n"
+
+ mysqlTypeBlob = "blob"
+ mysqlTypeTinyBob = "tinyblob"
+ mysqlTypeMediumBlob = "mediumblob"
+ mysqlTypeLongBlob = "longblob"
+ mysqlTypeJSON = "json"
+ mysqlTypeGeometry = "geometry"
+
+ mysqlIndexFullText = "FULLTEXT"
+ mysqlIndexSpatial = "SPATIAL"
+
+ mysqlNoAction = "NO ACTION"
+
+ autoIncrementSymbol = "AUTO_INCREMENT"
+)
+
+func init() {
+ schema.RegisterGetDatabaseDefinition(storepb.Engine_OCEANBASE, GetDatabaseDefinition)
+
+ schema.RegisterGetTableDefinition(storepb.Engine_OCEANBASE, GetTableDefinition)
+
+ schema.RegisterGetViewDefinition(storepb.Engine_OCEANBASE, GetViewDefinition)
+
+ schema.RegisterGetFunctionDefinition(storepb.Engine_OCEANBASE, GetFunctionDefinition)
+
+ schema.RegisterGetProcedureDefinition(storepb.Engine_OCEANBASE, GetProcedureDefinition)
+}
+
+func GetDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *metadatapb.DatabaseSchemaMetadata) (string, error) {
+ if len(metadata.Schemas) == 0 {
+ return "", nil
+ }
+
+ if ctx.SDLFormat {
+ return getSDLFormat(metadata)
+ }
+
+ var buf strings.Builder
+
+ if ctx.PrintHeader {
+ if _, err := buf.WriteString(disableUniqueAndForeignKeyCheckStmt); err != nil {
+ return "", err
+ }
+ }
+
+ schema := metadata.Schemas[0]
+
+ // Construct temporal views.
+ // Create a temporary view with the same name as the view and with columns of
+ // the same name in order to satisfy views that depend on this view.
+ // This temporary view will be removed when the actual view is created.
+ // The properties of each column, are not preserved in this temporary
+ // view. They are not necessary because other views only need to reference
+ // the column name, thus we generate SELECT 1 AS colName1, 1 AS colName2.
+ // This will not be necessary once we can determine dependencies
+ // between views and can simply dump them in the appropriate order.
+ // https://sourcegraph.com/github.com/mysql/mysql-server/-/blob/client/mysqldump.cc?L2781
+ for _, view := range schema.Views {
+ if len(view.Columns) == 0 {
+ if err := writeInvalidTemporaryView(&buf, view); err != nil {
+ return "", err
+ }
+ continue
+ }
+ if err := writeTemporaryView(&buf, view); err != nil {
+ return "", err
+ }
+ }
+
+ // Construct tables.
+ for _, table := range schema.Tables {
+ if err := writeTable(&buf, table); err != nil {
+ return "", err
+ }
+ }
+
+ // Construct views.
+ for _, view := range schema.Views {
+ if err := writeView(&buf, view); err != nil {
+ return "", err
+ }
+ }
+
+ // Construct functions.
+ for _, function := range schema.Functions {
+ if err := writeFunction(&buf, function); err != nil {
+ return "", err
+ }
+ }
+
+ // Construct procedures.
+ for _, procedure := range schema.Procedures {
+ if err := writeProcedure(&buf, procedure); err != nil {
+ return "", err
+ }
+ }
+
+ // Construct events.
+ for _, event := range schema.Events {
+ if err := writeEvent(&buf, event); err != nil {
+ return "", err
+ }
+ }
+
+ // Construct triggers.
+ for _, table := range schema.Tables {
+ for _, trigger := range table.Triggers {
+ if err := writeTrigger(&buf, table.Name, trigger); err != nil {
+ return "", err
+ }
+ }
+ }
+
+ if ctx.PrintHeader {
+ if _, err := buf.WriteString(restoreUniqueAndForeignKeyCheckStmt); err != nil {
+ return "", err
+ }
+ }
+
+ return buf.String(), nil
+}
+
+func GetTableDefinition(_ string, table *metadatapb.TableMetadata, _ []*metadatapb.SequenceMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeTable(&buf, table); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+func GetViewDefinition(_ string, view *metadatapb.ViewMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeView(&buf, view); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+func GetFunctionDefinition(_ string, function *metadatapb.FunctionMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeFunction(&buf, function); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+func GetProcedureDefinition(_ string, procedure *metadatapb.ProcedureMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeProcedure(&buf, procedure); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+func writeEvent(out io.Writer, event *metadatapb.EventMetadata) error {
+ // An event synced without its CREATE EVENT, as on OceanBase, has nothing faithful to write.
+ if event.Definition == "" {
+ return nil
+ }
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, eventHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, event.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+
+ // Set charset, collation, sql mode and timezone.
+ if _, err := io.WriteString(out, setCharacterSetClient); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, event.CharacterSetClient); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, ";\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, setCharacterSetResult); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, event.CharacterSetClient); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, ";\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, setCollation); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, event.CollationConnection); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, ";\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, setSQLMode); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "'"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, event.SqlMode); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "';\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, setTimezone); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "'"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, event.TimeZone); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "';\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, delimiterDoubleSemi); err != nil {
+ return err
+ }
+
+ // Definition.
+ if _, err := io.WriteString(out, event.Definition); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, ";;\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, delimiterSemi); err != nil {
+ return err
+ }
+
+ _, err := io.WriteString(out, "\n")
+ return err
+}
+
+func writeTrigger(out io.Writer, tableName string, trigger *metadatapb.TriggerMetadata) error {
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, triggerHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+
+ // Set charset, collation, and sql mode.
+ if _, err := io.WriteString(out, setCharacterSetClient); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.CharacterSetClient); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, ";\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, setCharacterSetResult); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.CharacterSetClient); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, ";\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, setCollation); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.CollationConnection); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, ";\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, setSQLMode); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "'"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.SqlMode); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "';\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, delimiterDoubleSemi); err != nil {
+ return err
+ }
+
+ // Definition.
+ if _, err := io.WriteString(out, "CREATE TRIGGER `"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.Timing); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, " "); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.Event); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, " ON `"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, tableName); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "` FOR EACH ROW\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, trigger.Body); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, ";;\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, delimiterSemi); err != nil {
+ return err
+ }
+
+ _, err := io.WriteString(out, "\n")
+ return err
+}
+
+func writeProcedure(out io.Writer, procedure *metadatapb.ProcedureMetadata) error {
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, procedureHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, procedure.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+
+ // Set charset, collation, and sql mode.
+ if err := writeAdditionalEventsIfSet(out, procedure.CharacterSetClient, procedure.CharacterSetClient, procedure.CollationConnection, procedure.SqlMode); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, delimiterDoubleSemi); err != nil {
+ return err
+ }
+
+ // Definition.
+ if _, err := io.WriteString(out, procedure.Definition); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, ";;\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, delimiterSemi); err != nil {
+ return err
+ }
+
+ _, err := io.WriteString(out, "\n")
+ return err
+}
+
+func writeFunction(out io.Writer, function *metadatapb.FunctionMetadata) error {
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, functionHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, function.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+
+ // Set charset, collation, and sql mode.
+ if err := writeAdditionalEventsIfSet(out, function.CharacterSetClient, function.CharacterSetClient, function.CollationConnection, function.SqlMode); err != nil {
+ return err
+ }
+
+ // Definition.
+ if _, err := io.WriteString(out, function.Definition); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, ";;\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, delimiterSemi); err != nil {
+ return err
+ }
+
+ _, err := io.WriteString(out, "\n")
+ return err
+}
+
+func writeView(out io.Writer, view *metadatapb.ViewMetadata) error {
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, viewHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+
+ // Definition.
+ if _, err := io.WriteString(out, "CREATE OR REPLACE VIEW `"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "` AS "); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Definition); err != nil {
+ return err
+ }
+ _, err := io.WriteString(out, ";\n\n")
+ return err
+}
+
+func writeTable(out *strings.Builder, table *metadatapb.TableMetadata) error {
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, tableHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, table.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+
+ // Definition.
+ if _, err := fmt.Fprintf(out, "CREATE TABLE `%s` (\n", table.Name); err != nil {
+ return err
+ }
+
+ for i, column := range table.Columns {
+ if i != 0 {
+ if _, err := fmt.Fprint(out, ",\n"); err != nil {
+ return err
+ }
+ }
+ if err := printColumnClause(out, column, table); err != nil {
+ return err
+ }
+ }
+ writeSystemTimePeriod(out, table)
+
+ if err := printPrimaryKeyClause(out, table.Indexes); err != nil {
+ return err
+ }
+
+ for _, index := range table.Indexes {
+ if index.Primary {
+ continue
+ }
+ if err := printIndexClause(out, index); err != nil {
+ return err
+ }
+ }
+
+ for _, fk := range table.ForeignKeys {
+ if err := printForeignKeyClause(out, fk); err != nil {
+ return err
+ }
+ }
+
+ for _, check := range table.CheckConstraints {
+ if err := printCheckClause(out, check); err != nil {
+ return err
+ }
+ }
+
+ if _, err := out.WriteString("\n)"); err != nil {
+ return err
+ }
+ if table.Engine != "" {
+ if _, err := fmt.Fprintf(out, " ENGINE=%s", table.Engine); err != nil {
+ return err
+ }
+ }
+
+ if table.Charset != "" {
+ if _, err := fmt.Fprintf(out, " DEFAULT CHARSET=%s", table.Charset); err != nil {
+ return err
+ }
+ }
+
+ if table.Collation != "" {
+ if _, err := fmt.Fprintf(out, " COLLATE=%s", table.Collation); err != nil {
+ return err
+ }
+ }
+
+ if table.Comment != "" {
+ if _, err := fmt.Fprintf(out, " COMMENT='%s'", table.Comment); err != nil {
+ return err
+ }
+ }
+
+ writeSystemVersioningOption(out, table)
+
+ if len(table.Partitions) > 0 {
+ if err := printPartitionClause(out, table.Partitions, table.Engine); err != nil {
+ return err
+ }
+ }
+
+ _, err := io.WriteString(out, ";\n\n")
+ return err
+}
+
+// Copy the logic from backend/plugin/schema/mysql/state.go.
+func printPartitionClause(buf *strings.Builder, partitions []*metadatapb.TablePartitionMetadata, engine string) error {
+ if len(partitions) == 0 {
+ return nil
+ }
+ vsc := getVersionSpecificComment(partitions)
+ curComment := vsc
+ if _, err := fmt.Fprintf(buf, "%s PARTITION BY ", curComment); err != nil {
+ return err
+ }
+ switch partitions[0].Type {
+ case metadatapb.TablePartitionMetadata_RANGE:
+ if _, err := fmt.Fprintf(buf, "RANGE (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_RANGE_COLUMNS:
+ if _, err := fmt.Fprintf(buf, "RANGE COLUMNS (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_LIST:
+ if _, err := fmt.Fprintf(buf, "LIST (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_LIST_COLUMNS:
+ if _, err := fmt.Fprintf(buf, "LIST COLUMNS (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_HASH:
+ if _, err := fmt.Fprintf(buf, "HASH (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_KEY:
+ if _, err := fmt.Fprintf(buf, "KEY (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_LINEAR_HASH:
+ if _, err := fmt.Fprintf(buf, "LINEAR HASH (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_LINEAR_KEY:
+ if _, err := fmt.Fprintf(buf, "LINEAR KEY (%s)", partitions[0].Expression); err != nil {
+ return err
+ }
+ default:
+ return errors.Errorf("unknown partition type: %v", partitions[0].Type)
+ }
+
+ useDefault := int64(0)
+ if partitions[0].UseDefault != "" {
+ var err error
+ useDefault, err = strconv.ParseInt(partitions[0].UseDefault, 10, 64)
+ if err != nil {
+ return err
+ }
+ }
+ if useDefault != 0 {
+ if _, err := fmt.Fprintf(buf, "\nPARTITIONS %d", useDefault); err != nil {
+ return err
+ }
+ }
+
+ if len(partitions[0].Subpartitions) > 0 {
+ if _, err := fmt.Fprint(buf, "\nSUBPARTITION BY "); err != nil {
+ return err
+ }
+ switch partitions[0].Subpartitions[0].Type {
+ case metadatapb.TablePartitionMetadata_HASH:
+ if _, err := fmt.Fprintf(buf, "HASH (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_LINEAR_HASH:
+ if _, err := fmt.Fprintf(buf, "LINEAR HASH (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_KEY:
+ if _, err := fmt.Fprintf(buf, "KEY (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ case metadatapb.TablePartitionMetadata_LINEAR_KEY:
+ if _, err := fmt.Fprintf(buf, "LINEAR KEY (%s)", partitions[0].Subpartitions[0].Expression); err != nil {
+ return err
+ }
+ default:
+ return errors.Errorf("invalid subpartition type: %v", partitions[0].Subpartitions[0].Type)
+ }
+ }
+
+ subUseDefault := 0
+ if len(partitions[0].Subpartitions) > 0 && partitions[0].Subpartitions[0].UseDefault != "" {
+ var err error
+ subUseDefault, err = strconv.Atoi(partitions[0].Subpartitions[0].UseDefault)
+ if err != nil {
+ return err
+ }
+ }
+
+ if subUseDefault != 0 {
+ if _, err := fmt.Fprintf(buf, "\nSUBPARTITIONS %d", subUseDefault); err != nil {
+ return err
+ }
+ }
+
+ if useDefault == 0 {
+ if _, err := fmt.Fprint(buf, "\n("); err != nil {
+ return err
+ }
+ preposition, err := getPrepositionByType(partitions[0].Type)
+ if err != nil {
+ return err
+ }
+ for i, partition := range partitions {
+ if i != 0 {
+ if _, err := fmt.Fprint(buf, ",\n "); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprintf(buf, "PARTITION %s", mysqlQuoteIdentifier(partition.Name)); err != nil {
+ return err
+ }
+ if preposition != "" {
+ if partition.Value != "MAXVALUE" {
+ if _, err := fmt.Fprintf(buf, " VALUES %s (%s)", preposition, partition.Value); err != nil {
+ return err
+ }
+ } else {
+ if _, err := fmt.Fprintf(buf, " VALUES %s %s", preposition, partition.Value); err != nil {
+ return err
+ }
+ }
+ }
+
+ if subUseDefault == 0 && len(partition.Subpartitions) > 0 {
+ if _, err := fmt.Fprint(buf, "\n ("); err != nil {
+ return err
+ }
+ for j, subPartition := range partition.Subpartitions {
+ if _, err := fmt.Fprintf(buf, "SUBPARTITION %s", mysqlQuoteIdentifier(subPartition.Name)); err != nil {
+ return err
+ }
+ if err := writePartitionOptions(buf, engine); err != nil {
+ return err
+ }
+ if j == len(partition.Subpartitions)-1 {
+ if _, err := fmt.Fprint(buf, ")"); err != nil {
+ return err
+ }
+ } else {
+ if _, err := fmt.Fprint(buf, ",\n "); err != nil {
+ return err
+ }
+ }
+ }
+ } else {
+ if err := writePartitionOptions(buf, engine); err != nil {
+ return err
+ }
+ }
+
+ if i == len(partitions)-1 {
+ if _, err := fmt.Fprint(buf, ")"); err != nil {
+ return err
+ }
+ }
+ }
+ }
+
+ if _, err := fmt.Fprint(buf, " */"); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func getPrepositionByType(tp metadatapb.TablePartitionMetadata_Type) (string, error) {
+ switch tp {
+ case metadatapb.TablePartitionMetadata_RANGE:
+ return "LESS THAN", nil
+ case metadatapb.TablePartitionMetadata_RANGE_COLUMNS:
+ return "LESS THAN", nil
+ case metadatapb.TablePartitionMetadata_LIST:
+ return "IN", nil
+ case metadatapb.TablePartitionMetadata_LIST_COLUMNS:
+ return "IN", nil
+ case metadatapb.TablePartitionMetadata_HASH, metadatapb.TablePartitionMetadata_KEY, metadatapb.TablePartitionMetadata_LINEAR_HASH, metadatapb.TablePartitionMetadata_LINEAR_KEY:
+ return "", nil
+ default:
+ return "", errors.Errorf("unsupported partition type: %v", tp)
+ }
+}
+
+func writePartitionOptions(buf io.StringWriter, engine string) error {
+ /*
+ int err = 0;
+ err += add_space(fptr);
+ if (p_elem->tablespace_name) {
+ err += add_string(fptr, "TABLESPACE = ");
+ err += add_ident_string(fptr, p_elem->tablespace_name);
+ err += add_space(fptr);
+ }
+ if (p_elem->nodegroup_id != UNDEF_NODEGROUP)
+ err += add_keyword_int(fptr, "NODEGROUP", (longlong)p_elem->nodegroup_id);
+ if (p_elem->part_max_rows)
+ err += add_keyword_int(fptr, "MAX_ROWS", (longlong)p_elem->part_max_rows);
+ if (p_elem->part_min_rows)
+ err += add_keyword_int(fptr, "MIN_ROWS", (longlong)p_elem->part_min_rows);
+ if (!(current_thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE)) {
+ if (p_elem->data_file_name)
+ err += add_keyword_path(fptr, "DATA DIRECTORY", p_elem->data_file_name);
+ if (p_elem->index_file_name)
+ err += add_keyword_path(fptr, "INDEX DIRECTORY", p_elem->index_file_name);
+ }
+ if (p_elem->part_comment)
+ err += add_keyword_string(fptr, "COMMENT", true, p_elem->part_comment);
+ return err + add_engine(fptr, p_elem->engine_type);
+ */
+ // TODO(zp): Get all the partition options from the metadata is too complex, just write
+ // the engine for now. It must be the table's own engine: MySQL rejects a partitioned
+ // table whose partitions name a different one with "ERROR 1497: The mix of handlers in
+ // the partitions is not allowed in this version of MySQL", and partitioning a
+ // non-InnoDB table is still legal on 5.7.
+ if engine == "" {
+ engine = "InnoDB"
+ }
+ if _, err := buf.WriteString(" ENGINE=" + engine); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func getVersionSpecificComment(partitions []*metadatapb.TablePartitionMetadata) string {
+ if len(partitions) == 0 {
+ return ""
+ }
+ partition := partitions[0]
+ if partition.Type == metadatapb.TablePartitionMetadata_RANGE_COLUMNS || partition.Type == metadatapb.TablePartitionMetadata_LIST_COLUMNS {
+ // MySQL introduce columns partitioning in 5.5+
+ return "\n/*!50500"
+ }
+ return "\n/*!50100"
+}
+
+func printCheckClause(buf *strings.Builder, check *metadatapb.CheckConstraintMetadata) error {
+ if _, err := fmt.Fprintf(buf, ",\n CONSTRAINT `%s` CHECK %s", check.Name, check.Expression); err != nil {
+ return err
+ }
+ return nil
+}
+
+func printForeignKeyClause(buf *strings.Builder, fk *metadatapb.ForeignKeyMetadata) error {
+ if _, err := fmt.Fprintf(buf, ",\n CONSTRAINT `%s` FOREIGN KEY (", fk.Name); err != nil {
+ return err
+ }
+
+ for i, column := range fk.Columns {
+ if i != 0 {
+ if _, err := fmt.Fprint(buf, ", "); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprintf(buf, "`%s`", column); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprintf(buf, ") REFERENCES `%s` (", fk.ReferencedTable); err != nil {
+ return err
+ }
+
+ for i, column := range fk.ReferencedColumns {
+ if i != 0 {
+ if _, err := fmt.Fprint(buf, ", "); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprintf(buf, "`%s`", column); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprint(buf, ")"); err != nil {
+ return err
+ }
+
+ if fk.OnDelete != "" && !strings.EqualFold(fk.OnDelete, mysqlNoAction) {
+ if _, err := fmt.Fprintf(buf, " ON DELETE %s", fk.OnDelete); err != nil {
+ return err
+ }
+ }
+
+ if fk.OnUpdate != "" && !strings.EqualFold(fk.OnUpdate, mysqlNoAction) {
+ if _, err := fmt.Fprintf(buf, " ON UPDATE %s", fk.OnUpdate); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func printIndexClause(buf *strings.Builder, index *metadatapb.IndexMetadata) error {
+ if index.Primary {
+ return nil
+ }
+
+ if _, err := fmt.Fprint(buf, ",\n "); err != nil {
+ return err
+ }
+
+ isFullText := strings.EqualFold(index.Type, mysqlIndexFullText)
+ isSpatial := strings.EqualFold(index.Type, mysqlIndexSpatial)
+ if index.Unique {
+ if _, err := fmt.Fprint(buf, "UNIQUE "); err != nil {
+ return err
+ }
+ } else if isFullText {
+ if _, err := fmt.Fprint(buf, "FULLTEXT "); err != nil {
+ return err
+ }
+ } else if isSpatial {
+ if _, err := fmt.Fprint(buf, "SPATIAL "); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprintf(buf, "KEY `%s` (", index.Name); err != nil {
+ return err
+ }
+
+ for i, expr := range index.Expressions {
+ if i != 0 {
+ if _, err := fmt.Fprint(buf, ", "); err != nil {
+ return err
+ }
+ }
+ keyLength := int64(-1)
+ descending := false
+ if len(index.KeyLength) > i {
+ keyLength = index.KeyLength[i]
+ }
+ if len(index.Descending) > i {
+ descending = index.Descending[i]
+ }
+ // SPATIAL and FULLTEXT index parts do not take a key-part prefix length. The sync
+ // records SUB_PART for spatial parts (e.g. 32 for a POINT) even though SHOW CREATE
+ // renders none; emitting `(32)` would diverge from the user form and DROP+ADD the
+ // index on every no-op. Suppress the prefix for those index types.
+ if err := printIndexKeyPart(buf, expr, keyLength, descending, isSpatial || isFullText); err != nil {
+ return err
+ }
+ }
+
+ if _, err := fmt.Fprint(buf, ")"); err != nil {
+ return err
+ }
+
+ // INVISIBLE secondary index (MySQL 8.0). The version-gated executable comment is parsed
+ // back by omni's loader, so the dumped `from` carries invisibility and the diff against
+ // an INVISIBLE user `to` is a no-op instead of a DROP+ADD.
+ if !index.Visible {
+ if _, err := fmt.Fprint(buf, " /*!80000 INVISIBLE */"); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func printIndexKeyPart(buf *strings.Builder, expr string, length int64, descending bool, suppressPrefix bool) error {
+ if len(expr) == 0 {
+ return errors.New("index key part expression is empty")
+ }
+ if expr[0] == '(' && expr[len(expr)-1] == ')' {
+ if _, err := buf.WriteString(normalizeFunctionalIndexExpr(expr)); err != nil {
+ return err
+ }
+ } else {
+ if _, err := buf.WriteString("`"); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(expr); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString("`"); err != nil {
+ return err
+ }
+ }
+ if length > 0 && !suppressPrefix {
+ if _, err := buf.WriteString("("); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(strconv.FormatInt(length, 10)); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(")"); err != nil {
+ return err
+ }
+ }
+ if descending {
+ if _, err := buf.WriteString(" DESC"); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// normalizeFunctionalIndexExpr rewrites a synced functional (expression) index key part into
+// the form the omni SDL loader can parse. The information_schema.STATISTICS.EXPRESSION text
+// MySQL records for an `((expr))` key part:
+// - backslash-escapes the single quotes around string literals (e.g. json_extract(`tags`,
+// _utf8mb4\'$.ids\')), which the omni lexer rejects (`syntax error at or near "\"`); and
+// - prefixes string literals with a charset introducer (`_utf8mb4'…'`, `_latin1'…'`, …)
+// reflecting the connection charset, which the omni expression parser does not accept in
+// this position (`unexpected token`).
+//
+// Both make LoadSDL fail outright on the dumped source even though omni accepts the canonical
+// user spelling (e.g. `((cast(json_extract(`tags`,'$.ids') as unsigned array)))`). This
+// unescapes the quotes and strips the introducer so the emitted expression is omni-parseable;
+// omni's functional-index normalizer then canonicalizes the dumped and user forms identically,
+// yielding an empty no-op. The transform is string-literal aware: the introducer is removed
+// only when it immediately precedes a string literal, and literal contents are left untouched.
+func normalizeFunctionalIndexExpr(expr string) string {
+ return stripCharsetIntroducers(unescapeFunctionalIndexQuotes(expr))
+}
+
+// unescapeFunctionalIndexQuotes undoes the backslash escaping that
+// information_schema.STATISTICS.EXPRESSION applies to single quotes (\' -> ') and backslashes
+// (\\ -> \). MySQL stores the expression text escaped; the omni lexer wants the raw SQL.
+func unescapeFunctionalIndexQuotes(expr string) string {
+ if !strings.Contains(expr, `\`) {
+ return expr
+ }
+ var b strings.Builder
+ b.Grow(len(expr))
+ for i := 0; i < len(expr); i++ {
+ if expr[i] == '\\' && i+1 < len(expr) && (expr[i+1] == '\'' || expr[i+1] == '\\') {
+ b.WriteByte(expr[i+1])
+ i++
+ continue
+ }
+ b.WriteByte(expr[i])
+ }
+ return b.String()
+}
+
+// stripCharsetIntroducers removes a MySQL charset introducer token (an underscore-prefixed
+// identifier such as `_utf8mb4` or `_latin1`) when it immediately precedes a single-quoted
+// string literal, e.g. `_utf8mb4'$.ids'` -> `'$.ids'`. The scan tracks whether it is inside a
+// '…'/"…" string literal (honoring backslash escapes and the doubled-quote escape) so that an
+// underscore sequence occurring inside literal text is never mistaken for an introducer and
+// literal contents are preserved byte for byte. Backtick-quoted identifiers are passed through
+// untouched. Only an introducer attached to a string literal is stripped; underscores that are
+// part of an ordinary identifier (`my_col`, a leading-underscore name not followed by a quote)
+// are left intact.
+func stripCharsetIntroducers(expr string) string {
+ if !strings.Contains(expr, "_") {
+ return expr
+ }
+ var b strings.Builder
+ b.Grow(len(expr))
+ var quote byte // open delimiter of the literal currently being copied, or 0 when outside one
+ for i := 0; i < len(expr); {
+ c := expr[i]
+ if quote != 0 {
+ b.WriteByte(c)
+ switch {
+ case c == '\\' && i+1 < len(expr):
+ b.WriteByte(expr[i+1])
+ i += 2
+ continue
+ case c == quote:
+ if i+1 < len(expr) && expr[i+1] == quote {
+ b.WriteByte(expr[i+1])
+ i += 2
+ continue
+ }
+ quote = 0
+ default:
+ }
+ i++
+ continue
+ }
+ switch c {
+ case '\'', '"':
+ quote = c
+ b.WriteByte(c)
+ i++
+ case '`':
+ // Copy a backtick-quoted identifier verbatim so an underscore inside it is never
+ // treated as an introducer.
+ b.WriteByte(c)
+ i++
+ for i < len(expr) {
+ b.WriteByte(expr[i])
+ if expr[i] == '`' {
+ i++
+ break
+ }
+ i++
+ }
+ case '_':
+ if n := charsetIntroducerLen(expr[i:]); n > 0 {
+ // Drop the `_<charset>` introducer; the following string literal is copied on the
+ // next iterations.
+ i += n
+ continue
+ }
+ b.WriteByte(c)
+ i++
+ default:
+ b.WriteByte(c)
+ i++
+ }
+ }
+ return b.String()
+}
+
+// charsetIntroducerLen returns the byte length of a leading charset-introducer token in s
+// when s begins with `_<charset>` immediately followed by a single-quoted string literal,
+// or 0 otherwise. The charset name accepts the ASCII letters/digits MySQL charset names use
+// (e.g. utf8mb4, latin1, big5); the terminating quote is required so a bare leading-underscore
+// identifier is not stripped.
+func charsetIntroducerLen(s string) int {
+ if len(s) < 2 || s[0] != '_' {
+ return 0
+ }
+ i := 1
+ for i < len(s) && isCharsetNameByte(s[i]) {
+ i++
+ }
+ if i == 1 || i >= len(s) || s[i] != '\'' {
+ return 0
+ }
+ return i
+}
+
+func isCharsetNameByte(c byte) bool {
+ return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'
+}
+
+func printPrimaryKeyClause(buf *strings.Builder, indexes []*metadatapb.IndexMetadata) error {
+ for _, index := range indexes {
+ if index.Primary {
+ if _, err := fmt.Fprint(buf, ",\n PRIMARY KEY ("); err != nil {
+ return err
+ }
+ for i, column := range index.Expressions {
+ if i != 0 {
+ if _, err := fmt.Fprint(buf, ", "); err != nil {
+ return err
+ }
+ }
+ keyLength := int64(-1)
+ descending := false
+ if len(index.KeyLength) > i {
+ keyLength = index.KeyLength[i]
+ }
+ if len(index.Descending) > i {
+ descending = index.Descending[i]
+ }
+ // Primary keys are never SPATIAL/FULLTEXT, so prefix lengths are valid here.
+ if err := printIndexKeyPart(buf, column, keyLength, descending, false); err != nil {
+ return err
+ }
+ }
+ if _, err := fmt.Fprint(buf, ")"); err != nil {
+ return err
+ }
+ return nil
+ }
+ }
+
+ return nil
+}
+
+func isAutoIncrement(column *metadatapb.ColumnMetadata) bool {
+ return strings.EqualFold(column.GetDefault(), autoIncrementSymbol)
+}
+
+func printColumnClause(buf *strings.Builder, column *metadatapb.ColumnMetadata, table *metadatapb.TableMetadata) error {
+ if _, err := fmt.Fprintf(buf, " `%s` %s", column.Name, normalizeColumnType(column.Type)); err != nil {
+ return err
+ }
+
+ if column.CharacterSet != "" && column.CharacterSet != table.Charset {
+ if _, err := fmt.Fprintf(buf, " CHARACTER SET %s", column.CharacterSet); err != nil {
+ return err
+ }
+ }
+
+ if column.Collation != "" && column.Collation != table.Collation {
+ if _, err := fmt.Fprintf(buf, " COLLATE %s", column.Collation); err != nil {
+ return err
+ }
+ }
+
+ if isSystemTimeColumn(column) {
+ writeSystemTimeColumnAttribute(buf, column)
+ } else if column.Generation != nil && column.Generation.Expression != "" {
+ if _, err := fmt.Fprintf(buf, " GENERATED ALWAYS AS (%s) ", column.Generation.Expression); err != nil {
+ return err
+ }
+ switch column.Generation.Type {
+ case metadatapb.GenerationMetadata_TYPE_STORED:
+ if _, err := fmt.Fprint(buf, "STORED"); err != nil {
+ return err
+ }
+ case metadatapb.GenerationMetadata_TYPE_VIRTUAL:
+ if _, err := fmt.Fprint(buf, "VIRTUAL"); err != nil {
+ return err
+ }
+ default:
+ // Unknown generation type
+ }
+ }
+
+ if !column.Nullable {
+ if _, err := fmt.Fprint(buf, " NOT NULL"); err != nil {
+ return err
+ }
+ }
+
+ // SRID for spatial columns (MySQL 8.0). Emitted after NOT NULL and before DEFAULT to
+ // match the SHOW CREATE / omni-canonical position; the version-gated executable comment
+ // is spliced back as live SQL by omni's loader so the dumped `from` carries the SRID and
+ // the diff against a user `to` with the same SRID is a no-op.
+ writeColumnSRIDAttribute(buf, column)
+
+ if err := printDefaultClause(buf, column); err != nil {
+ return err
+ }
+
+ // Handle auto_increment.
+ if isAutoIncrement(column) {
+ if _, err := fmt.Fprintf(buf, " %s", autoIncrementSymbol); err != nil {
+ return err
+ }
+ }
+
+ if column.OnUpdate != "" {
+ if _, err := fmt.Fprintf(buf, " ON UPDATE %s", column.OnUpdate); err != nil {
+ return err
+ }
+ }
+
+ // INVISIBLE column (MySQL 8.0.23+). Emitted before COMMENT to match the SHOW CREATE /
+ // omni-canonical order (verified against 8.0.32 for both regular and generated columns);
+ // like SRID the version-gated comment is parsed back by omni's loader so the dumped `from`
+ // carries invisibility and the diff against an INVISIBLE user `to` is a no-op.
+ writeColumnInvisibleAttribute(buf, column)
+
+ if column.Comment != "" {
+ if _, err := fmt.Fprintf(buf, " COMMENT '%s'", column.Comment); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// writeColumnSRIDAttribute emits the ` /*!80003 SRID n */` column attribute when the
+// column carries an explicit SRID — including the valid SRID 0, which presence (not a
+// zero sentinel) distinguishes from "no SRID". Shared by the SDL dumper
+// (printColumnClause) and the legacy migration generator (writeAddColumn /
+// writeModifyColumn) so both render the same canonical form.
+func writeColumnSRIDAttribute(buf *strings.Builder, column *metadatapb.ColumnMetadata) {
+ if column.Srid != nil {
+ _, _ = fmt.Fprintf(buf, " /*!80003 SRID %d */", column.GetSrid())
+ }
+}
+
+// writeColumnInvisibleAttribute emits the ` /*!80023 INVISIBLE */` column attribute for
+// invisible columns (MySQL 8.0.23+). MySQL's canonical SHOW CREATE order places INVISIBLE
+// before COMMENT for both regular and generated columns (verified against 8.0.32), so callers
+// emit it before COMMENT. Shared by the SDL dumper and the legacy migration generator.
+func writeColumnInvisibleAttribute(buf *strings.Builder, column *metadatapb.ColumnMetadata) {
+ if column.IsInvisible {
+ _, _ = buf.WriteString(" /*!80023 INVISIBLE */")
+ }
+}
+
+// normalizeColumnType rewrites engine-specific column-type spellings the omni SDL parser
+// does not accept into the canonical spelling it does. MySQL 8.0's SHOW CREATE /
+// information_schema render GEOMETRYCOLLECTION as the synonym `geomcollection`, which the
+// omni loader rejects with "expected data type"; the canonical `geometrycollection`
+// (what 5.7 already dumps) round-trips. The two spellings denote the same type, so this is
+// a pure rename with no semantic change.
+func normalizeColumnType(columnType string) string {
+ if strings.EqualFold(columnType, "geomcollection") {
+ return "geometrycollection"
+ }
+ return columnType
+}
+
+func printDefaultClause(buf *strings.Builder, column *metadatapb.ColumnMetadata) error {
+ // Check if column has any default value
+ hasDefault := column.Default != ""
+ if !hasDefault {
+ return nil
+ }
+
+ if column.Default == "NULL" {
+ if !column.Nullable || !typeSupportsDefaultValue(column.Type) {
+ // If the column is not nullable, then the default value should not be null.
+ // For this case, we should not print the default clause.
+ return nil
+ }
+ if column.Generation != nil && column.Generation.Expression != "" {
+ return nil
+ }
+ if _, err := fmt.Fprint(buf, " DEFAULT NULL"); err != nil {
+ return err
+ }
+ return nil
+ }
+
+ if column.Default != "" {
+ if isAutoIncrement(column) {
+ // If the default value is auto_increment, then we should not print the default clause.
+ // We'll handle this in the following AUTO_INCREMENT clause.
+ return nil
+ }
+ if _, err := fmt.Fprintf(buf, " DEFAULT %s", renderColumnDefault(column)); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// renderColumnDefault returns the DEFAULT expression text to emit for a column.
+//
+// For a BIT column, the synced COLUMN_DEFAULT is the bit-literal text (e.g. b'0'),
+// which the sync stores QUOTE()-escaped as a string literal in ColumnMetadata.Default
+// (the bit literal wrapped in single quotes with the inner quotes backslash-escaped).
+// Emitting that verbatim produces a quoted string default that the omni loader takes as a
+// string — never matching the user's BIT literal b'0', so the no-op phantom-re-emits
+// MODIFY ... DEFAULT b'0' forever. Real mysqldump renders the bit literal unquoted
+// (DEFAULT b'0'); match it by recovering the inner literal and emitting it without the
+// surrounding quotes when the column is BIT and the recovered text is a bit/hex literal
+// (b'…'/0x…/x'…'). Any other default (including a genuine string) is emitted verbatim.
+func renderColumnDefault(column *metadatapb.ColumnMetadata) string {
+ if isBitColumnType(column.Type) {
+ if lit, ok := bitLiteralFromQuotedDefault(column.Default); ok {
+ return lit
+ }
+ }
+ return column.Default
+}
+
+// isBitColumnType reports whether a column type spelling is BIT (with or without a
+// display width, e.g. "bit" or "bit(8)").
+func isBitColumnType(columnType string) bool {
+ lower := strings.ToLower(strings.TrimSpace(columnType))
+ return lower == "bit" || strings.HasPrefix(lower, "bit(")
+}
+
+// bitLiteralFromQuotedDefault recovers a bit/hex literal from a stored column default and
+// reports whether the default denotes one. The default may already be a bare literal
+// (b'0', 0x0A, x'1f') or the QUOTE()-escaped string form the MySQL sync stores for static
+// defaults (the literal wrapped in single quotes with inner quotes backslash-escaped). It
+// strips at most one layer of surrounding single quotes and the QUOTE() backslash escaping,
+// then recognizes the MySQL bit/hex literal spellings
+// (b'…' / B'…' bit, 0x… hex, x'…' / X'…' hex). On success it returns the canonical literal
+// text to emit unquoted; otherwise it returns ("", false) and the caller emits the default
+// verbatim.
+func bitLiteralFromQuotedDefault(def string) (string, bool) {
+ if isBitOrHexLiteral(def) {
+ return def, true
+ }
+ if len(def) >= 2 && def[0] == '\'' && def[len(def)-1] == '\'' {
+ inner := unescapeQuotedDefault(def[1 : len(def)-1])
+ if isBitOrHexLiteral(inner) {
+ return inner, true
+ }
+ }
+ return "", false
+}
+
+// isBitOrHexLiteral reports whether s is a MySQL bit literal (b'…' / B'…') or hex literal
+// (0x… / 0X… / x'…' / X'…') with only valid digits between the delimiters.
+func isBitOrHexLiteral(s string) bool {
+ switch {
+ case len(s) >= 3 && (s[0] == 'b' || s[0] == 'B') && s[1] == '\'' && s[len(s)-1] == '\'':
+ return allDigitsIn(s[2:len(s)-1], "01")
+ case len(s) >= 3 && (s[0] == 'x' || s[0] == 'X') && s[1] == '\'' && s[len(s)-1] == '\'':
+ return allDigitsIn(s[2:len(s)-1], "0123456789abcdefABCDEF")
+ case len(s) >= 3 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'):
+ return allDigitsIn(s[2:], "0123456789abcdefABCDEF")
+ default:
+ return false
+ }
+}
+
+func allDigitsIn(s, allowed string) bool {
+ if len(s) == 0 {
+ return false
+ }
+ for i := 0; i < len(s); i++ {
+ if !strings.ContainsRune(allowed, rune(s[i])) {
+ return false
+ }
+ }
+ return true
+}
+
+// unescapeQuotedDefault undoes the QUOTE() backslash escaping (\' -> ', \\ -> \) the MySQL
+// sync applies to static defaults. Only these two sequences appear inside a bit/hex literal,
+// so the full QUOTE() escape set is intentionally not reproduced here.
+func unescapeQuotedDefault(s string) string {
+ if !strings.Contains(s, `\`) {
+ return s
+ }
+ var b strings.Builder
+ b.Grow(len(s))
+ for i := 0; i < len(s); i++ {
+ if s[i] == '\\' && i+1 < len(s) && (s[i+1] == '\'' || s[i+1] == '\\') {
+ b.WriteByte(s[i+1])
+ i++
+ continue
+ }
+ b.WriteByte(s[i])
+ }
+ return b.String()
+}
+
+func typeSupportsDefaultValue(tp string) bool {
+ switch strings.ToLower(tp) {
+ case mysqlTypeBlob, mysqlTypeTinyBob, mysqlTypeMediumBlob, mysqlTypeLongBlob, mysqlTypeJSON, mysqlTypeGeometry:
+ return false
+ default:
+ return true
+ }
+}
+
+func writeInvalidTemporaryView(out io.Writer, view *metadatapb.ViewMetadata) error {
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, tempViewHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "-- `"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+ _, err := io.WriteString(out, "` references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them\n\n")
+ return err
+}
+
+func writeTemporaryView(out io.Writer, view *metadatapb.ViewMetadata) error {
+ // Header.
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, tempViewHeader); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`\n"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, emptyCommentLine); err != nil {
+ return err
+ }
+
+ // Definition.
+ if _, err := io.WriteString(out, "CREATE VIEW `"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "` AS SELECT\n "); err != nil {
+ return err
+ }
+ for i, column := range view.Columns {
+ if i != 0 {
+ if _, err := io.WriteString(out, ",\n "); err != nil {
+ return err
+ }
+ }
+ if _, err := io.WriteString(out, "1 AS `"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, column.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "`"); err != nil {
+ return err
+ }
+ }
+ _, err := io.WriteString(out, ";\n\n")
+ return err
+}
+
+func writeAdditionalEventsIfSet(out io.Writer, characterSetClient, characterSetResult, collationConnection, sqlMode string) error {
+ events := []struct {
+ condition bool
+ prefix string
+ value string
+ }{
+ {characterSetClient != "", setCharacterSetClient, characterSetClient},
+ {characterSetResult != "", setCharacterSetResult, characterSetResult},
+ {collationConnection != "", setCollation, collationConnection},
+ }
+
+ for _, event := range events {
+ if event.condition {
+ if _, err := io.WriteString(out, event.prefix); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, event.value); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, ";\n"); err != nil {
+ return err
+ }
+ }
+ }
+ if sqlMode != "" {
+ if _, err := io.WriteString(out, setSQLMode); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "'"); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, sqlMode); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, "';\n"); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// getSDLFormat emits a canonical, deterministic SDL dump of the database metadata
+// such that the omni catalog LoadSDL round-trips it. Unlike the mysqldump-style
+// output produced by GetDatabaseDefinition, the SDL dump:
+// - contains only the constructive CREATE statements LoadSDL accepts (no SET
+// headers, no DELIMITER blocks, no temporary-view scaffolding, no version-gated
+// /*!50100 PARTITION */ executable comments);
+// - emits objects and intra-table clauses in a stable order (tables and views
+// sorted by name; secondary indexes, foreign keys, and check constraints sorted
+// by name) so the same metadata always produces byte-identical SDL;
+// - strips the volatile AUTO_INCREMENT=N table option (a live counter, never
+// schema);
+// - emits stored routines, triggers, and events as their canonical CREATE forms
+// (DEFINER omitted, no DELIMITER blocks — omni's LoadSDL splitter is BEGIN…END
+// aware and the routine/trigger/event differs ignore DEFINER).
+//
+// Emission order is dependency-safe so every CREATE resolves against objects loaded
+// before it: tables → functions → procedures → views → triggers → events. Functions
+// precede views/triggers because a view or trigger body may call a stored function;
+// triggers and events follow their tables because the omni loader requires a
+// trigger's owning table (and any table an event/trigger body touches) to exist.
+//
+// Type, charset/collation, and default canonicalization are intentionally NOT done
+// here: the omni Diff path resolves both the dumped source and the user target
+// through the same Normalizer (CanonicalColumn), so faithful emission is sufficient
+// for the no-op idempotence property — the canonical comparison happens in omni.
+func getSDLFormat(metadata *metadatapb.DatabaseSchemaMetadata) (string, error) {
+ var buf strings.Builder
+
+ schema := metadata.Schemas[0]
+
+ tables := make([]*metadatapb.TableMetadata, 0, len(schema.Tables))
+ for _, table := range schema.Tables {
+ if table.SkipDump {
+ continue
+ }
+ tables = append(tables, table)
+ }
+ slices.SortFunc(tables, func(a, b *metadatapb.TableMetadata) int { return cmp.Compare(a.Name, b.Name) })
+
+ for _, table := range tables {
+ if err := writeTableSDL(&buf, table); err != nil {
+ return "", err
+ }
+ }
+
+ functions := make([]*metadatapb.FunctionMetadata, 0, len(schema.Functions))
+ for _, function := range schema.Functions {
+ if function.SkipDump {
+ continue
+ }
+ functions = append(functions, function)
+ }
+ slices.SortFunc(functions, func(a, b *metadatapb.FunctionMetadata) int { return cmp.Compare(a.Name, b.Name) })
+ for _, function := range functions {
+ if err := writeRoutineSDL(&buf, function.Definition); err != nil {
+ return "", err
+ }
+ }
+
+ procedures := make([]*metadatapb.ProcedureMetadata, 0, len(schema.Procedures))
+ for _, procedure := range schema.Procedures {
+ if procedure.SkipDump {
+ continue
+ }
+ procedures = append(procedures, procedure)
+ }
+ slices.SortFunc(procedures, func(a, b *metadatapb.ProcedureMetadata) int { return cmp.Compare(a.Name, b.Name) })
+ for _, procedure := range procedures {
+ if err := writeRoutineSDL(&buf, procedure.Definition); err != nil {
+ return "", err
+ }
+ }
+
+ views := make([]*metadatapb.ViewMetadata, 0, len(schema.Views))
+ for _, view := range schema.Views {
+ if view.SkipDump {
+ continue
+ }
+ views = append(views, view)
+ }
+ slices.SortFunc(views, func(a, b *metadatapb.ViewMetadata) int { return cmp.Compare(a.Name, b.Name) })
+
+ for _, view := range views {
+ if err := writeViewSDL(&buf, metadata.Name, view); err != nil {
+ return "", err
+ }
+ }
+
+ // Triggers hang off tables (TableMetadata.Triggers); emit them after every table
+ // in deterministic (table, trigger) order.
+ for _, table := range tables {
+ triggers := make([]*metadatapb.TriggerMetadata, 0, len(table.Triggers))
+ for _, trigger := range table.Triggers {
+ if trigger.SkipDump {
+ continue
+ }
+ triggers = append(triggers, trigger)
+ }
+ slices.SortFunc(triggers, func(a, b *metadatapb.TriggerMetadata) int { return cmp.Compare(a.Name, b.Name) })
+ for _, trigger := range triggers {
+ if err := writeTriggerSDL(&buf, table.Name, trigger); err != nil {
+ return "", err
+ }
+ }
+ }
+
+ events := make([]*metadatapb.EventMetadata, 0, len(schema.Events))
+ events = append(events, schema.Events...)
+ slices.SortFunc(events, func(a, b *metadatapb.EventMetadata) int { return cmp.Compare(a.Name, b.Name) })
+ for _, event := range events {
+ if err := writeEventSDL(&buf, event); err != nil {
+ return "", err
+ }
+ }
+
+ return buf.String(), nil
+}
+
+// writeTableSDL emits a single CREATE TABLE statement in canonical SDL form with
+// deterministic clause ordering. It reuses the same column/index/foreign-key/check
+// clause writers as the mysqldump path; only the statement framing and the sorting
+// of sub-objects differ.
+func writeTableSDL(buf *strings.Builder, table *metadatapb.TableMetadata) error {
+ if _, err := fmt.Fprintf(buf, "CREATE TABLE `%s` (\n", table.Name); err != nil {
+ return err
+ }
+
+ for i, column := range table.Columns {
+ if i != 0 {
+ if _, err := fmt.Fprint(buf, ",\n"); err != nil {
+ return err
+ }
+ }
+ if err := printColumnClause(buf, column, table); err != nil {
+ return err
+ }
+ }
+ writeSystemTimePeriod(buf, table)
+
+ if err := printPrimaryKeyClause(buf, table.Indexes); err != nil {
+ return err
+ }
+
+ secondaryIndexes := make([]*metadatapb.IndexMetadata, 0, len(table.Indexes))
+ for _, index := range table.Indexes {
+ if index.Primary {
+ continue
+ }
+ secondaryIndexes = append(secondaryIndexes, index)
+ }
+ slices.SortFunc(secondaryIndexes, func(a, b *metadatapb.IndexMetadata) int { return cmp.Compare(a.Name, b.Name) })
+ for _, index := range secondaryIndexes {
+ if err := printIndexClause(buf, index); err != nil {
+ return err
+ }
+ }
+
+ foreignKeys := make([]*metadatapb.ForeignKeyMetadata, len(table.ForeignKeys))
+ copy(foreignKeys, table.ForeignKeys)
+ slices.SortFunc(foreignKeys, func(a, b *metadatapb.ForeignKeyMetadata) int { return cmp.Compare(a.Name, b.Name) })
+ for _, fk := range foreignKeys {
+ if err := printForeignKeyClause(buf, fk); err != nil {
+ return err
+ }
+ }
+
+ checks := make([]*metadatapb.CheckConstraintMetadata, len(table.CheckConstraints))
+ copy(checks, table.CheckConstraints)
+ slices.SortFunc(checks, func(a, b *metadatapb.CheckConstraintMetadata) int { return cmp.Compare(a.Name, b.Name) })
+ for _, check := range checks {
+ if err := printCheckClause(buf, check); err != nil {
+ return err
+ }
+ }
+
+ if _, err := buf.WriteString("\n)"); err != nil {
+ return err
+ }
+ if table.Engine != "" {
+ if _, err := fmt.Fprintf(buf, " ENGINE=%s", table.Engine); err != nil {
+ return err
+ }
+ }
+ // CREATE_OPTIONS (ROW_FORMAT, KEY_BLOCK_SIZE, COMPRESSION, STATS_PERSISTENT, ...) are
+ // recorded schema the sync captures into TableMetadata.CreateOptions; emit them in the
+ // canonical SHOW CREATE position (after ENGINE/AUTO_INCREMENT, before DEFAULT CHARSET) so
+ // the dumped `from` keeps an explicit ROW_FORMAT and the omni diff against a user `to`
+ // carrying the same option is a no-op (entry: TestSDLStressTableCreateOptions). The
+ // synthetic `partitioned` token is dropped — partitioning is surfaced via Partitions, not
+ // as a literal table option, and emitting it would be invalid DDL.
+ if opts := filterCreateOptions(table.CreateOptions); opts != "" {
+ if _, err := fmt.Fprintf(buf, " %s", opts); err != nil {
+ return err
+ }
+ }
+ if table.Charset != "" {
+ if _, err := fmt.Fprintf(buf, " DEFAULT CHARSET=%s", table.Charset); err != nil {
+ return err
+ }
+ }
+ if table.Collation != "" {
+ if _, err := fmt.Fprintf(buf, " COLLATE=%s", table.Collation); err != nil {
+ return err
+ }
+ }
+ if table.Comment != "" {
+ if _, err := fmt.Fprintf(buf, " COMMENT='%s'", table.Comment); err != nil {
+ return err
+ }
+ }
+ writeSystemVersioningOption(buf, table)
+
+ // Partitioning is a table-level clause that follows the table options. The omni
+ // loader accepts the version-gated /*!50100 PARTITION BY … */ executable comment
+ // (its lexer splices the inner text back as live SQL), and the omni partition
+ // differ canonicalizes the expression and per-partition ENGINE, so reusing the
+ // mysqldump partition writer round-trips as a no-op without an SDL-specific form.
+ if len(table.Partitions) > 0 {
+ if err := printPartitionClause(buf, table.Partitions, table.Engine); err != nil {
+ return err
+ }
+ }
+
+ _, err := io.WriteString(buf, ";\n\n")
+ return err
+}
+
+// filterCreateOptions returns the table CREATE_OPTIONS string with the synthetic
+// `partitioned` token removed. MySQL's information_schema.TABLES.CREATE_OPTIONS folds the
+// fact that a table is partitioned into a literal `partitioned` token alongside the real
+// declared options (e.g. `partitioned`, or `row_format=DYNAMIC partitioned`). Partitioning
+// is emitted separately via the PARTITION BY clause from TableMetadata.Partitions, so the
+// token must not be rendered as a table option (it is not valid table-option DDL). The
+// remaining tokens (row_format=…, key_block_size=…, …) are already valid `name=value`
+// table-option syntax that the omni loader parses case-insensitively, so they are returned
+// verbatim and joined by single spaces.
+func filterCreateOptions(createOptions string) string {
+ if createOptions == "" {
+ return ""
+ }
+ kept := make([]string, 0, 4)
+ // The system-versioning marker is written separately by writeSystemVersioningOption.
+ for _, tok := range strings.Fields(strings.Replace(createOptions, systemVersioningOption, "", 1)) {
+ if strings.EqualFold(tok, "partitioned") {
+ continue
+ }
+ kept = append(kept, tok)
+ }
+ return strings.Join(kept, " ")
+}
+
+// writeViewSDL emits a single CREATE OR REPLACE VIEW statement. LoadSDL accepts
+// CREATE OR REPLACE VIEW and is order-tolerant, so no temporary-view scaffolding is
+// needed. dbName is the database being dumped; its own-database qualifier is stripped
+// from the body so the emitted form matches the user's unqualified spelling (see
+// stripViewBodyDatabaseQualifier — required for 5.7 derived-table view idempotence,
+// entry TestSDLStressViewDerivedTable57). The stored one-line body is then
+// pretty-printed by formatViewBodySDL — a whitespace-only, deterministic rewrite, so
+// the omni no-op invariant and the dump-cycle byte stability both hold.
+func writeViewSDL(buf *strings.Builder, dbName string, view *metadatapb.ViewMetadata) error {
+ body := formatViewBodySDL(stripViewBodyDatabaseQualifier(view.Definition, dbName))
+ if body == "" {
+ // Defensive: an empty definition keeps the historical inline framing.
+ if _, err := fmt.Fprintf(buf, "CREATE OR REPLACE VIEW `%s` AS ;\n\n", view.Name); err != nil {
+ return err
+ }
+ return nil
+ }
+ if _, err := fmt.Fprintf(buf, "CREATE OR REPLACE VIEW `%s` AS\n%s;\n\n", view.Name, body); err != nil {
+ return err
+ }
+ return nil
+}
+
+// stripViewBodyDatabaseQualifier removes the dumped database's own-database qualifier
+// (the leading “ `<dbName>`. “ prefix on a table/column reference) from a view body.
+//
+// MySQL 5.7's SHOW CREATE VIEW / information_schema.VIEWS.VIEW_DEFINITION fully qualifies
+// references — including the inner refs of a FROM-clause derived table — with the schema
+// name (e.g. “ `mydb`.`assignment` “), whereas 8.0 stores them unqualified and the user
+// authors them unqualified. The omni SDL diff loads both inputs under the synthetic
+// database bbcatalog and its canonicalViewBody only folds the loaded own-database prefix
+// (bbcatalog), so a body still carrying the real synced DB name phantom-diffs against the
+// unqualified user body. Stripping the qualifier at dump time yields the db-neutral form
+// both 5.7 and 8.0 share.
+//
+// Only the current database's own qualifier is removed (genuine cross-database references,
+// which MySQL views in this single-database scope effectively never carry, are left
+// intact). The body is walked with scanViewBodyToken — the same quote/identifier-aware
+// scanner formatViewBodySDL uses — so ONE scanner owns the literal rules: a
+// “ `<dbName>`. “ sequence inside a '…' or "…" string literal (or inside a backtick
+// identifier such as an alias) is NOT a qualifier and is left untouched.
+//
+// A qualifier is recognized only where one can grammatically start: a backtick
+// identifier spelling dbName, immediately followed by "." and another backtick
+// identifier, and NOT itself preceded by "." (then it is the table/column part of an
+// enclosing dotted reference, e.g. the table part of `otherdb`.`<dbName>`.`c`). After a
+// strip, the following identifier is copied verbatim BEFORE re-arming, so a table named
+// like the database (`<dbName>`.`<dbName>`.`col`, the 5.7 three-part form) loses only
+// the database part.
+func stripViewBodyDatabaseQualifier(body, dbName string) string {
+ if dbName == "" || body == "" {
+ return body
+ }
+ quoted := "`" + strings.ReplaceAll(dbName, "`", "``") + "`"
+ if !strings.Contains(body, quoted+".") {
+ return body
+ }
+
+ var out strings.Builder
+ out.Grow(len(body))
+ // prevTok is the previous non-whitespace token; "." marks the current identifier as
+ // a continuation part of a dotted reference, never a qualifier start.
+ prevTok := ""
+ for i := 0; i < len(body); {
+ if isViewBodySpaceByte(body[i]) {
+ out.WriteByte(body[i])
+ i++
+ continue
+ }
+ end := scanViewBodyToken(body, i)
+ tok := body[i:end]
+ if tok == quoted && prevTok != "." && end < len(body) && body[end] == '.' && end+1 < len(body) && body[end+1] == '`' {
+ // Own-database qualifier: drop it and the dot, then copy the following
+ // identifier verbatim so it is not re-tested as a qualifier.
+ identEnd := scanViewBodyToken(body, end+1)
+ ident := body[end+1 : identEnd]
+ out.WriteString(ident)
+ prevTok = ident
+ i = identEnd
+ continue
+ }
+ out.WriteString(tok)
+ prevTok = tok
+ i = end
+ }
+ return out.String()
+}
+
+// writeRoutineSDL emits a stored function or procedure. The synced Definition is
+// already a complete, canonical CREATE FUNCTION/PROCEDURE statement (the MySQL sync
+// rewrites SHOW CREATE to drop the DEFINER and the RETURNS charset attribute), so the
+// only framing the SDL form adds is a terminating ";". The body is emitted verbatim:
+// omni's loader splitter is BEGIN…END aware, so internal ";" inside a compound body
+// do not need DELIMITER wrapping, and omni's routine differ compares the body byte for
+// byte after trimming — reformatting would phantom-diff. A leading DEFINER, if one
+// somehow survives, is stripped to match omni's DEFINER-agnostic routine identity.
+func writeRoutineSDL(buf *strings.Builder, definition string) error {
+ def := strings.TrimSpace(stripLeadingDefiner(definition))
+ if def == "" {
+ return nil
+ }
+ if _, err := fmt.Fprintf(buf, "%s;\n\n", def); err != nil {
+ return err
+ }
+ return nil
+}
+
+// writeTriggerSDL constructs a canonical CREATE TRIGGER from the trigger's metadata.
+// MySQL stores only the action statement (TriggerMetadata.Body) plus the timing,
+// event, and owning table, so the CREATE wrapper is rebuilt here exactly as omni's
+// loader expects: `CREATE TRIGGER <name> <timing> <event> ON <table> FOR EACH ROW
+// <body>`. DEFINER is omitted (omni's trigger differ ignores it) and the body is
+// emitted verbatim (compared byte for byte after trimming).
+func writeTriggerSDL(buf *strings.Builder, tableName string, trigger *metadatapb.TriggerMetadata) error {
+ if _, err := fmt.Fprintf(buf, "CREATE TRIGGER `%s` %s %s ON `%s` FOR EACH ROW\n%s;\n\n",
+ trigger.Name, trigger.Timing, trigger.Event, tableName, strings.TrimSpace(trigger.Body)); err != nil {
+ return err
+ }
+ return nil
+}
+
+// writeEventSDL emits a scheduled event. The synced Definition is the full
+// SHOW CREATE EVENT text, which (unlike functions/procedures) still carries the
+// DEFINER clause, so it is stripped here to match omni's DEFINER-agnostic event
+// identity. The schedule's auto-injected STARTS '<create-time>' is left intact: omni's
+// event differ normalizes STARTS out of the canonical schedule key, so it does not
+// perturb the no-op.
+func writeEventSDL(buf *strings.Builder, event *metadatapb.EventMetadata) error {
+ def := strings.TrimSpace(stripLeadingDefiner(event.Definition))
+ if def == "" {
+ return nil
+ }
+ if _, err := fmt.Fprintf(buf, "%s;\n\n", def); err != nil {
+ return err
+ }
+ return nil
+}
+
+// stripLeadingDefiner removes a leading `CREATE DEFINER=<account> ` clause from a
+// CREATE statement, collapsing it back to a bare `CREATE <OBJECT> …`. The account is
+// parsed properly — `user`@`host` with each part backtick, single- or double-quoted
+// (quoted parts may contain spaces, '@', and doubled/escaped quotes), an unquoted
+// user@host, or CURRENT_USER[()] — so a legal quoted account with a space
+// (DEFINER=`my user`@`%`) does not get cut at its first space. Statements without a
+// DEFINER, or with an account this scan cannot parse, are returned unchanged (omni
+// tolerates a DEFINER and never diffs on it, so keeping one is safe while corrupting
+// the statement is not). Stripping keeps the dump canonical and matches the
+// routine/trigger emission, which carry no DEFINER at all.
+func stripLeadingDefiner(definition string) string {
+ const createKW = "CREATE "
+ trimmed := strings.TrimLeft(definition, " \t\r\n")
+ if !strings.HasPrefix(strings.ToUpper(trimmed), createKW) {
+ return definition
+ }
+ rest := strings.TrimLeft(trimmed[len(createKW):], " \t")
+ const definerKW = "DEFINER"
+ if !strings.HasPrefix(strings.ToUpper(rest), definerKW) {
+ return definition
+ }
+ pos := len(definerKW)
+ pos = skipSpacesAndTabs(rest, pos)
+ if pos >= len(rest) || rest[pos] != '=' {
+ return definition
+ }
+ pos = skipSpacesAndTabs(rest, pos+1)
+ end, ok := scanDefinerAccount(rest, pos)
+ if !ok {
+ return definition
+ }
+ // The clause must be followed by whitespace and the object keyword.
+ afterAccount := skipSpacesAndTabs(rest, end)
+ if afterAccount == end || afterAccount >= len(rest) {
+ return definition
+ }
+ return createKW + rest[afterAccount:]
+}
+
+// skipSpacesAndTabs returns the first offset >= pos in s that is not a space or tab.
+func skipSpacesAndTabs(s string, pos int) int {
+ for pos < len(s) && (s[pos] == ' ' || s[pos] == '\t') {
+ pos++
+ }
+ return pos
+}
+
+// scanDefinerAccount scans a MySQL account name starting at pos: CURRENT_USER[()] or
+// <part>[@<part>] where each part is backtick/single/double quoted (doubled-delimiter
+// and, for '/'"', backslash escapes) or an unquoted run. Returns the offset just past
+// the account and whether the scan succeeded.
+func scanDefinerAccount(s string, pos int) (int, bool) {
+ const currentUser = "CURRENT_USER"
+ if len(s)-pos >= len(currentUser) && strings.EqualFold(s[pos:pos+len(currentUser)], currentUser) {
+ end := pos + len(currentUser)
+ if strings.HasPrefix(s[end:], "()") {
+ end += 2
+ }
+ return end, true
+ }
+ end, ok := scanAccountPart(s, pos)
+ if !ok {
+ return 0, false
+ }
+ if end < len(s) && s[end] == '@' {
+ hostEnd, ok := scanAccountPart(s, end+1)
+ if !ok {
+ return 0, false
+ }
+ end = hostEnd
+ }
+ return end, true
+}
+
+// scanAccountPart scans one user or host part of an account name starting at pos.
+func scanAccountPart(s string, pos int) (int, bool) {
+ if pos >= len(s) {
+ return 0, false
+ }
+ switch q := s[pos]; q {
+ case '`', '\'', '"':
+ for i := pos + 1; i < len(s); {
+ switch {
+ case q != '`' && s[i] == '\\' && i+1 < len(s):
+ // Backslash escapes apply inside '…' and "…" (not backticks).
+ i += 2
+ case s[i] == q:
+ if i+1 < len(s) && s[i+1] == q {
+ // Doubled delimiter stays inside the part.
+ i += 2
+ continue
+ }
+ return i + 1, true
+ default:
+ i++
+ }
+ }
+ return 0, false // Unterminated quote.
+ default:
+ // Unquoted part: a run of bytes up to the '@' separator or whitespace.
+ i := pos
+ for i < len(s) && s[i] != '@' && s[i] != ' ' && s[i] != '\t' && s[i] != '\r' && s[i] != '\n' {
+ i++
+ }
+ if i == pos {
+ return 0, false
+ }
+ return i, true
+ }
+}
diff --git a/backend/plugin/schema/oceanbase/get_database_definition_test.go b/backend/plugin/schema/oceanbase/get_database_definition_test.go
new file mode 100644
index 0000000000..feb02c64f4
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/get_database_definition_test.go
@@ -0,0 +1,87 @@
+package oceanbase
+
+import (
+ "testing"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+// TestGetDatabaseDefinition pins the DDL this package writes for each kind of object,
+// and that OceanBase reaches it through the registry. The input is hand-written
+// metadata rather than a schema text, so no generator branch is capped by what the
+// parser happens to extract; parsing is covered in get_database_metadata_test.go.
+func TestGetDatabaseDefinition(t *testing.T) {
+ table := &metadatapb.TableMetadata{
+ Name: "t",
+ Columns: []*metadatapb.ColumnMetadata{
+ {Name: "id", Type: "int"},
+ {Name: "name", Type: "varchar(64)", Nullable: true, Default: "'anon'"},
+ },
+ Indexes: []*metadatapb.IndexMetadata{
+ {Name: "PRIMARY", Expressions: []string{"id"}, Primary: true, Unique: true},
+ },
+ }
+ const tableDDL = "--\n" +
+ "-- Table structure for `t`\n" +
+ "--\n" +
+ "CREATE TABLE `t` (\n" +
+ " `id` int NOT NULL,\n" +
+ " `name` varchar(64) DEFAULT 'anon',\n" +
+ " PRIMARY KEY (`id`)\n" +
+ ");\n\n"
+
+ for _, tc := range []struct {
+ name string
+ want string
+ get func() (string, error)
+ }{
+ {
+ name: "table",
+ want: tableDDL,
+ get: func() (string, error) {
+ return schema.GetTableDefinition(storepb.Engine_OCEANBASE, "", table, nil)
+ },
+ },
+ {
+ name: "view",
+ want: "--\n-- View structure for `v`\n--\nCREATE OR REPLACE VIEW `v` AS SELECT `id` FROM `t`;\n\n",
+ get: func() (string, error) {
+ return schema.GetViewDefinition(storepb.Engine_OCEANBASE, "", &metadatapb.ViewMetadata{Name: "v", Definition: "SELECT `id` FROM `t`"})
+ },
+ },
+ {
+ name: "function",
+ want: "--\n-- Function structure for `f`\n--\nCREATE FUNCTION `f`() RETURNS int RETURN 1;;\nDELIMITER ;\n\n",
+ get: func() (string, error) {
+ return schema.GetFunctionDefinition(storepb.Engine_OCEANBASE, "", &metadatapb.FunctionMetadata{Name: "f", Definition: "CREATE FUNCTION `f`() RETURNS int RETURN 1"})
+ },
+ },
+ {
+ name: "procedure",
+ want: "--\n-- Procedure structure for `p`\n--\nDELIMITER ;;\nCREATE PROCEDURE `p`() BEGIN END;;\nDELIMITER ;\n\n",
+ get: func() (string, error) {
+ return schema.GetProcedureDefinition(storepb.Engine_OCEANBASE, "", &metadatapb.ProcedureMetadata{Name: "p", Definition: "CREATE PROCEDURE `p`() BEGIN END"})
+ },
+ },
+ {
+ name: "database",
+ want: tableDDL,
+ get: func() (string, error) {
+ return schema.GetDatabaseDefinition(storepb.Engine_OCEANBASE, schema.GetDefinitionContext{}, &metadatapb.DatabaseSchemaMetadata{
+ Name: "db",
+ Schemas: []*metadatapb.SchemaMetadata{{Name: "", Tables: []*metadatapb.TableMetadata{table}}},
+ })
+ },
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ definition, err := tc.get()
+ require.NoError(t, err)
+ require.Equal(t, tc.want, definition)
+ })
+ }
+}
diff --git a/backend/plugin/schema/oceanbase/get_database_metadata.go b/backend/plugin/schema/oceanbase/get_database_metadata.go
new file mode 100644
index 0000000000..29c40d8ec2
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/get_database_metadata.go
@@ -0,0 +1,46 @@
+package oceanbase
+
+import (
+ "fmt"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+ "github.com/bytebase/omni/oceanbase/catalog"
+ "github.com/pkg/errors"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+func init() {
+ schema.RegisterGetDatabaseMetadata(storepb.Engine_OCEANBASE, GetDatabaseMetadata)
+}
+
+// GetDatabaseMetadata parses MySQL schema DDL text and returns database metadata
+// using the omni catalog.
+func GetDatabaseMetadata(schemaText string) (*metadatapb.DatabaseSchemaMetadata, error) {
+ if schemaText == "" {
+ return &metadatapb.DatabaseSchemaMetadata{}, nil
+ }
+
+ const dbName = "tmp"
+ c := catalog.New()
+ initSQL := fmt.Sprintf("SET foreign_key_checks = 0;\nCREATE DATABASE IF NOT EXISTS `%s`;\nUSE `%s`;", dbName, dbName)
+ if _, err := c.Exec(initSQL, nil); err != nil {
+ return nil, errors.Wrap(err, "failed to initialize catalog")
+ }
+
+ results, err := c.Exec(schemaText, &catalog.ExecOptions{ContinueOnError: true})
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to parse schema DDL")
+ }
+
+ // Check for hard errors (not per-statement errors).
+ for _, r := range results {
+ if r.Error != nil {
+ return nil, errors.Wrapf(r.Error, "failed to execute schema DDL")
+ }
+ }
+
+ proto := catalogToProto(c, dbName)
+ return proto, nil
+}
diff --git a/backend/plugin/schema/oceanbase/get_database_metadata_test.go b/backend/plugin/schema/oceanbase/get_database_metadata_test.go
new file mode 100644
index 0000000000..c47c9258ec
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/get_database_metadata_test.go
@@ -0,0 +1,40 @@
+package oceanbase
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+// TestGetDatabaseMetadata pins that OceanBase reaches this package's parser through
+// the registry. What the parser extracts is pinned by the goldens in schema/mysql,
+// which this package's copy mirrors.
+func TestGetDatabaseMetadata(t *testing.T) {
+ metadata, err := schema.GetDatabaseMetadata(storepb.Engine_OCEANBASE, "CREATE TABLE `t` (`a` int NOT NULL, PRIMARY KEY (`a`));")
+ require.NoError(t, err)
+
+ tables := metadata.GetSchemas()[0].GetTables()
+ require.Len(t, tables, 1)
+ require.Equal(t, "t", tables[0].GetName())
+ require.Len(t, tables[0].GetColumns(), 1)
+}
+
+// TestGetDatabaseMetadataSyncedView pins that this package parses the canonical form
+// an engine writes back for a view over a derived table joined to a table, which is
+// what Bytebase reads when it syncs a view. It needs omni's oceanbase/parser at #570
+// or later.
+func TestGetDatabaseMetadataSyncedView(t *testing.T) {
+ const synced = "CREATE TABLE `j1` (`k1` int);\n" +
+ "CREATE TABLE `j2` (`k1` int);\n" +
+ "CREATE VIEW `v` AS select `d`.`k1` AS `k1` from ((select `j1`.`k1` AS `k1` from `j1`) `d` join `j2` on((`d`.`k1` = `j2`.`k1`)));\n"
+
+ metadata, err := schema.GetDatabaseMetadata(storepb.Engine_OCEANBASE, synced)
+ require.NoError(t, err)
+
+ views := metadata.GetSchemas()[0].GetViews()
+ require.Len(t, views, 1)
+ require.Equal(t, "v", views[0].GetName())
+}
diff --git a/backend/plugin/schema/oceanbase/system_versioning.go b/backend/plugin/schema/oceanbase/system_versioning.go
new file mode 100644
index 0000000000..007ac32c6c
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/system_versioning.go
@@ -0,0 +1,64 @@
+package oceanbase
+
+import (
+ "strings"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+)
+
+// MariaDB system versioning is written as plain MariaDB syntax, not inside a
+// /*M! */ comment: the omni MySQL loader that parses schema text for diffs would
+// skip the comment and load the table unversioned, so a diff would silently
+// recreate it without history. It rejects the plain syntax instead.
+
+// systemVersioningOption is what the MariaDB sync appends to
+// TableMetadata.CreateOptions for a system-versioned table. information_schema
+// reports versioning only as a TABLE_TYPE, which TableMetadata has no field for.
+const systemVersioningOption = "WITH SYSTEM VERSIONING"
+
+func isSystemVersioned(table *metadatapb.TableMetadata) bool {
+ return strings.Contains(table.CreateOptions, systemVersioningOption)
+}
+
+func writeSystemVersioningOption(buf *strings.Builder, table *metadatapb.TableMetadata) {
+ if isSystemVersioned(table) {
+ _, _ = buf.WriteString(" " + systemVersioningOption)
+ }
+}
+
+// isSystemTimeColumn reports a MariaDB column declared GENERATED ALWAYS AS ROW START
+// or ROW END, which information_schema reports as a stored generated column with that
+// text as its expression.
+func isSystemTimeColumn(column *metadatapb.ColumnMetadata) bool {
+ generation := column.GetGeneration()
+ if generation == nil || generation.Type != metadatapb.GenerationMetadata_TYPE_STORED {
+ return false
+ }
+ return generation.Expression == "ROW START" || generation.Expression == "ROW END"
+}
+
+// writeSystemTimeColumnAttribute writes the attribute without the parentheses and
+// storage kind of an ordinary generated column, which MariaDB rejects here.
+func writeSystemTimeColumnAttribute(buf *strings.Builder, column *metadatapb.ColumnMetadata) {
+ _, _ = buf.WriteString(" GENERATED ALWAYS AS " + column.Generation.Expression)
+}
+
+// writeSystemTimePeriod writes the PERIOD FOR SYSTEM_TIME table element that MariaDB
+// requires alongside explicit ROW START and ROW END columns.
+func writeSystemTimePeriod(buf *strings.Builder, table *metadatapb.TableMetadata) {
+ var start, end string
+ for _, column := range table.Columns {
+ if !isSystemTimeColumn(column) {
+ continue
+ }
+ if column.Generation.Expression == "ROW START" {
+ start = column.Name
+ } else {
+ end = column.Name
+ }
+ }
+ if start == "" || end == "" {
+ return
+ }
+ _, _ = buf.WriteString(",\n PERIOD FOR SYSTEM_TIME (`" + start + "`, `" + end + "`)")
+}
diff --git a/backend/plugin/schema/oceanbase/walk_through.go b/backend/plugin/schema/oceanbase/walk_through.go
new file mode 100644
index 0000000000..edebdb4a53
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/walk_through.go
@@ -0,0 +1,946 @@
+package oceanbase
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "slices"
+ "strings"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+ "github.com/bytebase/omni/oceanbase/ast"
+ "github.com/bytebase/omni/oceanbase/catalog"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ oceanbaseparser "github.com/bytebase/bytebase/backend/plugin/parser/oceanbase"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+func init() {
+ schema.RegisterWalkThroughWithContext(storepb.Engine_OCEANBASE, WalkThroughWithContext)
+}
+
+// WalkThroughWithContext performs DDL simulation using the omni OceanBase catalog.
+// Flow:
+// 1. Create the catalog.
+// 2. catalog.LoadMetadata: install the snapshot, standing in for what fails.
+// 3. catalog.Exec(userSQL) → execute user DDL
+// 4. Map errors → *storepb.Advice
+// 5. Convert updated catalog → DatabaseMetadata (for downstream rules)
+func WalkThroughWithContext(ctx context.Context, wtCtx schema.WalkThroughContext, d *model.DatabaseMetadata, asts []base.AST) *storepb.Advice {
+ if wtCtx.RawSQL == "" {
+ return nil
+ }
+ dbName := d.GetProto().GetName()
+ precheck := firstMySQLWalkThroughAdvice(
+ precheckMySQLOmniWalkThrough(d, asts),
+ precheckMySQLRawWalkThrough(d, wtCtx.RawSQL, asts),
+ )
+
+ // Step 1: Create the catalog. LoadMetadata creates and selects the target
+ // database itself; only a RENAME destination needs one made up front.
+ c := catalog.New()
+ initSQL := "SET foreign_key_checks = 0;"
+ for _, targetDB := range mysqlRenameTargetDatabases(d, asts) {
+ initSQL += fmt.Sprintf("\nCREATE DATABASE IF NOT EXISTS %s;", mysqlQuoteIdentifier(targetDB))
+ }
+ if _, err := c.Exec(initSQL, &catalog.ExecOptions{ContinueOnError: true}); err != nil {
+ return &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: code.DDLSimulationFailed.Int32(),
+ Title: "Failed to initialize catalog",
+ Content: err.Error(),
+ StartPosition: &storepb.Position{Line: 0},
+ }
+ }
+
+ // Step 2: Install every schema object, with stand-ins for the ones that fail.
+ if _, err := c.LoadMetadata(ctx, d.GetProto()); err != nil {
+ return &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: code.DDLSimulationFailed.Int32(),
+ Title: "Failed to load schema",
+ Content: err.Error(),
+ StartPosition: &storepb.Position{Line: 0},
+ }
+ }
+
+ // Step 3: Execute user SQL.
+ results, execErr := c.Exec(wtCtx.RawSQL, &catalog.ExecOptions{ContinueOnError: true})
+ if execErr != nil {
+ errCode := code.DDLSimulationFailed
+ content := execErr.Error()
+ var catErr *catalog.Error
+ if errors.As(execErr, &catErr) {
+ errCode = mapMySQLErrorToCode(catErr)
+ content = mysqlCatalogErrorAdviceContent(d, catErr)
+ }
+ if strings.Contains(content, "BLOB, TEXT, GEOMETRY or JSON column ") {
+ errCode = code.InvalidColumnDefault
+ content = fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", mysqlCatalogErrorName(content))
+ }
+ execAdvice := mySQLWalkThroughAdvice{
+ index: -1,
+ advice: &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: errCode.Int32(),
+ Title: content,
+ Content: content,
+ StartPosition: &storepb.Position{Line: 0},
+ },
+ }
+ if precheck.beforeOrSame(execAdvice) {
+ return precheck.advice
+ }
+ return execAdvice.advice
+ }
+
+ // Step 4: Report the first error from the simulation.
+ var execAdvice mySQLWalkThroughAdvice
+ for _, r := range results {
+ if r.Error == nil {
+ continue
+ }
+ errCode := mapMySQLErrorToCode(r.Error)
+ content := r.Error.Error()
+ if catErr, ok := r.Error.(*catalog.Error); ok {
+ content = mysqlCatalogErrorAdviceContent(d, catErr)
+ }
+ if strings.Contains(content, "BLOB, TEXT, GEOMETRY or JSON column ") {
+ errCode = code.InvalidColumnDefault
+ content = fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", mysqlCatalogErrorName(content))
+ }
+ execAdvice = mySQLWalkThroughAdvice{
+ index: r.Index,
+ advice: &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: errCode.Int32(),
+ Title: content,
+ Content: content,
+ StartPosition: &storepb.Position{
+ Line: int32(r.Line),
+ },
+ },
+ }
+ break
+ }
+ if precheck.beforeOrSame(execAdvice) {
+ return precheck.advice
+ }
+ if execAdvice.advice != nil {
+ return execAdvice.advice
+ }
+
+ // Step 5: Convert catalog state back to DatabaseMetadata for downstream rules.
+ newProto := catalogToProto(c, dbName)
+ newMetadata := model.NewDatabaseMetadata(newProto, nil, d.GetConfig(), storepb.Engine_OCEANBASE, true)
+ d.ReplaceFrom(newMetadata)
+
+ return nil
+}
+
+type mySQLWalkThroughAdvice struct {
+ index int
+ advice *storepb.Advice
+}
+
+func firstMySQLWalkThroughAdvice(advices ...mySQLWalkThroughAdvice) mySQLWalkThroughAdvice {
+ var result mySQLWalkThroughAdvice
+ for _, advice := range advices {
+ if advice.beforeOrSame(result) {
+ result = advice
+ }
+ }
+ return result
+}
+
+func (a mySQLWalkThroughAdvice) beforeOrSame(b mySQLWalkThroughAdvice) bool {
+ if a.advice == nil {
+ return false
+ }
+ if b.advice == nil {
+ return true
+ }
+ if a.index >= 0 && b.index >= 0 {
+ if a.index != b.index {
+ return a.index < b.index
+ }
+ if a.advice.Status != b.advice.Status {
+ return a.advice.Status > b.advice.Status
+ }
+ return true
+ }
+ if a.index >= 0 && b.index < 0 {
+ return true
+ }
+ if a.index < 0 && b.index >= 0 {
+ return false
+ }
+ aLine := mySQLWalkThroughAdviceLine(a.advice)
+ bLine := mySQLWalkThroughAdviceLine(b.advice)
+ if aLine <= 0 || bLine <= 0 {
+ return true
+ }
+ return aLine <= bLine
+}
+
+func mySQLWalkThroughAdviceLine(advice *storepb.Advice) int32 {
+ if advice == nil || advice.StartPosition == nil {
+ return 0
+ }
+ return advice.StartPosition.Line
+}
+
+// Keep a token-aware fallback for CTAS forms that do not produce an omni AST yet.
+func precheckMySQLRawWalkThrough(d *model.DatabaseMetadata, rawSQL string, asts []base.AST) mySQLWalkThroughAdvice {
+ statements, err := oceanbaseparser.SplitSQL(rawSQL)
+ if err != nil {
+ return mySQLWalkThroughAdvice{}
+ }
+ statementIndex := 0
+ for _, statement := range statements {
+ if statement.Empty {
+ continue
+ }
+ currentIndex := statementIndex
+ statementIndex++
+ text := strings.TrimSpace(statement.Text)
+ if !isMySQLCreateTableAsStatement(text) {
+ continue
+ }
+ if mySQLOmniASTSkipsCreateTableAsAdvice(d, asts, currentIndex) {
+ continue
+ }
+ content := fmt.Sprintf("CREATE TABLE AS statement is used in \"%s\"", text)
+ return mySQLWalkThroughAdvice{
+ index: currentIndex,
+ advice: &storepb.Advice{
+ Status: storepb.Advice_WARNING,
+ Code: code.StatementCreateTableAs.Int32(),
+ Title: content,
+ Content: content,
+ StartPosition: mySQLStatementStartPosition(statement.Start),
+ },
+ }
+ }
+ return mySQLWalkThroughAdvice{}
+}
+
+func mySQLOmniASTSkipsCreateTableAsAdvice(d *model.DatabaseMetadata, asts []base.AST, index int) bool {
+ if index >= len(asts) {
+ return false
+ }
+ node, ok := oceanbaseparser.GetOmniNode(asts[index])
+ if !ok {
+ return false
+ }
+ createTable, ok := node.(*ast.CreateTableStmt)
+ return ok && createTable.Select != nil && createTable.IfNotExists && mySQLOmniTableExistsInCurrentDatabase(d, createTable.Table)
+}
+
+func isMySQLCreateTableAsStatement(statement string) bool {
+ tokens := mySQLStatementTokens(statement)
+ if len(tokens) < 3 || tokens[0] != "CREATE" {
+ return false
+ }
+ idx := 1
+ if tokens[idx] == "TEMPORARY" {
+ idx++
+ }
+ if idx >= len(tokens) || tokens[idx] != "TABLE" {
+ return false
+ }
+ return slices.Contains(tokens[idx+1:], "SELECT")
+}
+
+func mySQLStatementTokens(statement string) []string {
+ var tokens []string
+ for i := 0; i < len(statement); {
+ switch statement[i] {
+ case '\'', '"', '`':
+ i = skipMySQLQuotedText(statement, i, statement[i])
+ case '#':
+ i = skipMySQLLineComment(statement, i+1)
+ case '-':
+ if i+1 < len(statement) && statement[i+1] == '-' {
+ i = skipMySQLLineComment(statement, i+2)
+ } else {
+ i++
+ }
+ case '/':
+ if i+1 < len(statement) && statement[i+1] == '*' {
+ i = skipMySQLBlockComment(statement, i+2)
+ } else {
+ i++
+ }
+ default:
+ if !isMySQLIdentifierChar(statement[i]) {
+ i++
+ continue
+ }
+ start := i
+ for i < len(statement) && isMySQLIdentifierChar(statement[i]) {
+ i++
+ }
+ tokens = append(tokens, strings.ToUpper(statement[start:i]))
+ }
+ }
+ return tokens
+}
+
+func skipMySQLQuotedText(statement string, start int, quote byte) int {
+ for i := start + 1; i < len(statement); i++ {
+ if statement[i] == '\\' {
+ i++
+ continue
+ }
+ if statement[i] != quote {
+ continue
+ }
+ if i+1 < len(statement) && statement[i+1] == quote {
+ i++
+ continue
+ }
+ return i + 1
+ }
+ return len(statement)
+}
+
+func skipMySQLLineComment(statement string, start int) int {
+ for i := start; i < len(statement); i++ {
+ if statement[i] == '\n' || statement[i] == '\r' {
+ return i + 1
+ }
+ }
+ return len(statement)
+}
+
+func skipMySQLBlockComment(statement string, start int) int {
+ for i := start; i+1 < len(statement); i++ {
+ if statement[i] == '*' && statement[i+1] == '/' {
+ return i + 2
+ }
+ }
+ return len(statement)
+}
+
+func isMySQLIdentifierChar(ch byte) bool {
+ return ch == '_' || ch == '$' || ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'
+}
+
+func precheckMySQLOmniWalkThrough(d *model.DatabaseMetadata, asts []base.AST) mySQLWalkThroughAdvice {
+ for i, unifiedAST := range asts {
+ node, ok := oceanbaseparser.GetOmniNode(unifiedAST)
+ if !ok {
+ continue
+ }
+ text := ""
+ if source, ok := unifiedAST.(interface{ ASTSource() string }); ok {
+ text = strings.TrimSpace(source.ASTSource())
+ }
+ if advice := precheckMySQLOmniNode(d, node, text); advice != nil {
+ advice.StartPosition = mySQLStatementStartPosition(unifiedAST.ASTStartPosition())
+ return mySQLWalkThroughAdvice{index: i, advice: advice}
+ }
+ }
+ return mySQLWalkThroughAdvice{}
+}
+
+func precheckMySQLOmniNode(d *model.DatabaseMetadata, node ast.Node, text string) *storepb.Advice {
+ switch n := node.(type) {
+ case *ast.CreateDatabaseStmt:
+ if n.Name != "" && !isCurrentDatabase(d, n.Name) {
+ return notCurrentMySQLDatabaseAdvice(d, n.Name)
+ }
+ case *ast.AlterDatabaseStmt:
+ if n.Name != "" && !isCurrentDatabase(d, n.Name) {
+ return notCurrentMySQLDatabaseAdvice(d, n.Name)
+ }
+ case *ast.DropDatabaseStmt:
+ if n.Name != "" && !isCurrentDatabase(d, n.Name) {
+ return notCurrentMySQLDatabaseAdvice(d, n.Name)
+ }
+ if n.Name != "" {
+ content := fmt.Sprintf("Database `%s` is deleted", n.Name)
+ return &storepb.Advice{
+ Status: storepb.Advice_WARNING,
+ Code: code.DatabaseIsDeleted.Int32(),
+ Title: content,
+ Content: content,
+ StartPosition: &storepb.Position{Line: 0},
+ }
+ }
+ case *ast.CreateTableStmt:
+ if advice := precheckMySQLTableRefDatabase(d, n.Table); advice != nil {
+ return advice
+ }
+ if n.IfNotExists && mySQLOmniTableExistsInCurrentDatabase(d, n.Table) {
+ return nil
+ }
+ if n.Select != nil {
+ content := fmt.Sprintf("CREATE TABLE AS statement is used in \"%s\"", text)
+ return &storepb.Advice{
+ Status: storepb.Advice_WARNING,
+ Code: code.StatementCreateTableAs.Int32(),
+ Title: content,
+ Content: content,
+ StartPosition: &storepb.Position{Line: 0},
+ }
+ }
+ case *ast.AlterTableStmt:
+ return precheckMySQLTableRefDatabase(d, n.Table)
+ case *ast.DropTableStmt:
+ for _, table := range n.Tables {
+ if advice := precheckMySQLTableRefDatabase(d, table); advice != nil {
+ return advice
+ }
+ }
+ case *ast.CreateIndexStmt:
+ return precheckMySQLTableRefDatabase(d, n.Table)
+ case *ast.DropIndexStmt:
+ return precheckMySQLTableRefDatabase(d, n.Table)
+ case *ast.TruncateStmt:
+ for _, table := range n.Tables {
+ if advice := precheckMySQLTableRefDatabase(d, table); advice != nil {
+ return advice
+ }
+ }
+ case *ast.RenameTableStmt:
+ for _, pair := range n.Pairs {
+ if pair == nil {
+ continue
+ }
+ if advice := precheckMySQLTableRefDatabase(d, pair.Old); advice != nil {
+ return advice
+ }
+ }
+ case *ast.CreateViewStmt:
+ return precheckMySQLTableRefDatabase(d, n.Name)
+ default:
+ }
+ return nil
+}
+
+func mysqlRenameTargetDatabases(d *model.DatabaseMetadata, asts []base.AST) []string {
+ seen := make(map[string]bool)
+ var targets []string
+ addTarget := func(sourceTable, targetTable *ast.TableRef) {
+ if sourceTable == nil || targetTable == nil {
+ return
+ }
+ if sourceTable.Schema != "" && !isCurrentDatabase(d, sourceTable.Schema) {
+ return
+ }
+ if targetTable.Schema == "" || isCurrentDatabase(d, targetTable.Schema) || seen[targetTable.Schema] {
+ return
+ }
+ seen[targetTable.Schema] = true
+ targets = append(targets, targetTable.Schema)
+ }
+ for _, unifiedAST := range asts {
+ node, ok := oceanbaseparser.GetOmniNode(unifiedAST)
+ if !ok {
+ continue
+ }
+ switch stmt := node.(type) {
+ case *ast.RenameTableStmt:
+ for _, pair := range stmt.Pairs {
+ if pair == nil {
+ continue
+ }
+ addTarget(pair.Old, pair.New)
+ }
+ case *ast.AlterTableStmt:
+ for _, cmd := range stmt.Commands {
+ if cmd == nil || cmd.Type != ast.ATRenameTable {
+ continue
+ }
+ addTarget(stmt.Table, cmd.NewTable)
+ }
+ default:
+ }
+ }
+ return targets
+}
+
+func mySQLStatementStartPosition(pos *storepb.Position) *storepb.Position {
+ if pos == nil {
+ return &storepb.Position{Line: 0}
+ }
+ return &storepb.Position{Line: pos.Line, Column: pos.Column}
+}
+
+func mysqlQuoteIdentifier(identifier string) string {
+ return "`" + strings.ReplaceAll(identifier, "`", "``") + "`"
+}
+
+func precheckMySQLTableRefDatabase(d *model.DatabaseMetadata, table *ast.TableRef) *storepb.Advice {
+ if table == nil || table.Schema == "" || isCurrentDatabase(d, table.Schema) {
+ return nil
+ }
+ return notCurrentMySQLDatabaseAdvice(d, table.Schema)
+}
+
+func mySQLOmniTableExistsInCurrentDatabase(d *model.DatabaseMetadata, table *ast.TableRef) bool {
+ if table == nil || (table.Schema != "" && !isCurrentDatabase(d, table.Schema)) {
+ return false
+ }
+ schema := d.GetSchemaMetadata("")
+ return schema.GetTable(table.Name) != nil
+}
+
+func notCurrentMySQLDatabaseAdvice(d *model.DatabaseMetadata, databaseName string) *storepb.Advice {
+ content := fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, d.DatabaseName())
+ return &storepb.Advice{
+ Status: storepb.Advice_WARNING,
+ Code: code.NotCurrentDatabase.Int32(),
+ Title: content,
+ Content: content,
+ StartPosition: &storepb.Position{Line: 0},
+ }
+}
+
+// mapMySQLErrorToCode converts an omni OceanBase catalog error to a bytebase error code.
+func mapMySQLErrorToCode(err error) code.Code {
+ catErr, ok := err.(*catalog.Error)
+ if !ok {
+ return code.DDLSimulationFailed
+ }
+
+ switch catErr.Code {
+ case catalog.ErrDupDatabase:
+ return code.DDLSimulationFailed
+ case catalog.ErrUnknownDatabase:
+ return code.NotCurrentDatabase
+ case catalog.ErrDupTable:
+ return code.TableExists
+ case catalog.ErrUnknownTable, catalog.ErrNoSuchTable:
+ return code.TableNotExists
+ case catalog.ErrDupColumn:
+ return code.ColumnExists
+ case catalog.ErrNoSuchColumn:
+ return code.ColumnNotExists
+ case catalog.ErrInvalidDefault:
+ return code.InvalidColumnDefault
+ case catalog.ErrDupKeyName, catalog.ErrDupIndex:
+ return code.IndexExists
+ case catalog.ErrMultiplePriKey:
+ return code.PrimaryKeyExists
+ case catalog.ErrDupEntry:
+ return code.IndexExists
+ case 1252:
+ return code.SpatialIndexKeyNullable
+ case catalog.ErrCantDropKey:
+ return code.IndexNotExists
+ case catalog.ErrIncorrectTableDefinition:
+ return code.AutoIncrementExists
+ case catalog.ErrInvalidOnUpdate:
+ return code.OnUpdateColumnNotDatetimeOrTimestamp
+ case catalog.ErrFKNoRefTable:
+ return code.TableHasFK
+ case catalog.ErrFKCannotDropParent:
+ return code.TableHasFK
+ case catalog.ErrFKMissingIndex, catalog.ErrFKIncompatibleColumns:
+ return code.TableHasFK
+ case catalog.ErrCheckConstraintViolated:
+ return code.DDLSimulationFailed
+ default:
+ return code.DDLSimulationFailed
+ }
+}
+
+func mysqlCatalogErrorAdviceContent(d *model.DatabaseMetadata, catErr *catalog.Error) string {
+ name := mysqlCatalogErrorName(catErr.Message)
+ switch catErr.Code {
+ case catalog.ErrUnknownDatabase:
+ return fmt.Sprintf("Database `%s` is not the current database `%s`", name, d.DatabaseName())
+ case catalog.ErrDupTable:
+ return fmt.Sprintf("Table `%s` already exists", mysqlUnqualifiedName(name))
+ case catalog.ErrUnknownTable, catalog.ErrNoSuchTable:
+ return fmt.Sprintf("Table `%s` does not exist", mysqlUnqualifiedName(name))
+ case catalog.ErrDupColumn:
+ return fmt.Sprintf("Column `%s` already exists", mysqlUnqualifiedName(name))
+ case catalog.ErrNoSuchColumn:
+ return fmt.Sprintf("Column `%s` does not exist", mysqlUnqualifiedName(name))
+ case catalog.ErrInvalidDefault:
+ return fmt.Sprintf("Invalid default value for column `%s`", mysqlUnqualifiedName(name))
+ case catalog.ErrDupKeyName, catalog.ErrDupIndex, catalog.ErrDupEntry:
+ return fmt.Sprintf("Index `%s` already exists", mysqlUnqualifiedName(name))
+ case catalog.ErrCantDropKey:
+ return fmt.Sprintf("Index `%s` does not exist", mysqlUnqualifiedName(name))
+ default:
+ return catErr.Message
+ }
+}
+
+func mysqlCatalogErrorName(message string) string {
+ start := strings.Index(message, "'")
+ if start < 0 {
+ return message
+ }
+ end := strings.Index(message[start+1:], "'")
+ if end < 0 {
+ return message[start+1:]
+ }
+ return message[start+1 : start+1+end]
+}
+
+func mysqlUnqualifiedName(name string) string {
+ if idx := strings.LastIndex(name, "."); idx >= 0 {
+ return name[idx+1:]
+ }
+ return name
+}
+
+// catalogToProto converts the omni catalog state to a metadatapb.DatabaseSchemaMetadata.
+func catalogToProto(c *catalog.Catalog, dbName string) *metadatapb.DatabaseSchemaMetadata {
+ dbMeta := &metadatapb.DatabaseSchemaMetadata{
+ Name: dbName,
+ }
+
+ db := c.GetDatabase(dbName)
+ if db == nil {
+ return dbMeta
+ }
+
+ dbMeta.CharacterSet = db.Charset
+ dbMeta.Collation = db.Collation
+
+ // MySQL uses a single empty-name schema.
+ schemaMeta := &metadatapb.SchemaMetadata{
+ Name: "",
+ }
+
+ // Tables.
+ tableNames := make([]string, 0, len(db.Tables))
+ for name := range db.Tables {
+ tableNames = append(tableNames, name)
+ }
+ slices.Sort(tableNames)
+
+ // tableProtoByLowerName lets the trigger pass below attach each trigger to its
+ // owning table's metadata. The omni catalog keys tables case-insensitively, and a
+ // trigger records its table via Trigger.Table, so the lookup is lower-cased.
+ tableProtoByLowerName := make(map[string]*metadatapb.TableMetadata, len(tableNames))
+ for _, tName := range tableNames {
+ t := db.Tables[tName]
+ tableProto := tableToProto(t)
+ schemaMeta.Tables = append(schemaMeta.Tables, tableProto)
+ tableProtoByLowerName[strings.ToLower(t.Name)] = tableProto
+ }
+
+ // Triggers are database-level in MySQL (db.Triggers), each bound to a table via
+ // Trigger.Table; the proto nests them under their owning TableMetadata.Triggers,
+ // matching the live MySQL sync. Emit in deterministic name order.
+ triggerNames := make([]string, 0, len(db.Triggers))
+ for name := range db.Triggers {
+ triggerNames = append(triggerNames, name)
+ }
+ slices.Sort(triggerNames)
+ for _, trName := range triggerNames {
+ tr := db.Triggers[trName]
+ tableProto := tableProtoByLowerName[strings.ToLower(tr.Table)]
+ if tableProto == nil {
+ // A trigger on a table the catalog does not know about cannot be hung
+ // off any TableMetadata; skip it rather than fabricate an orphan.
+ continue
+ }
+ tableProto.Triggers = append(tableProto.Triggers, triggerToProto(tr))
+ }
+
+ // Views.
+ viewNames := make([]string, 0, len(db.Views))
+ for name := range db.Views {
+ viewNames = append(viewNames, name)
+ }
+ slices.Sort(viewNames)
+
+ for _, vName := range viewNames {
+ v := db.Views[vName]
+ schemaMeta.Views = append(schemaMeta.Views, &metadatapb.ViewMetadata{
+ Name: v.Name,
+ Definition: v.Definition,
+ })
+ }
+
+ // Functions.
+ funcNames := make([]string, 0, len(db.Functions))
+ for name := range db.Functions {
+ funcNames = append(funcNames, name)
+ }
+ slices.Sort(funcNames)
+
+ for _, fName := range funcNames {
+ f := db.Functions[fName]
+ schemaMeta.Functions = append(schemaMeta.Functions, routineToFunctionProto(f))
+ }
+
+ // Procedures.
+ procNames := make([]string, 0, len(db.Procedures))
+ for name := range db.Procedures {
+ procNames = append(procNames, name)
+ }
+ slices.Sort(procNames)
+
+ for _, pName := range procNames {
+ p := db.Procedures[pName]
+ schemaMeta.Procedures = append(schemaMeta.Procedures, routineToProcedureProto(p))
+ }
+
+ // Events (database-level scheduled events).
+ eventNames := make([]string, 0, len(db.Events))
+ for name := range db.Events {
+ eventNames = append(eventNames, name)
+ }
+ slices.Sort(eventNames)
+
+ for _, eName := range eventNames {
+ e := db.Events[eName]
+ schemaMeta.Events = append(schemaMeta.Events, eventToProto(c, dbName, e))
+ }
+
+ dbMeta.Schemas = append(dbMeta.Schemas, schemaMeta)
+ return dbMeta
+}
+
+func tableToProto(t *catalog.Table) *metadatapb.TableMetadata {
+ table := &metadatapb.TableMetadata{
+ Name: t.Name,
+ Engine: t.Engine,
+ Charset: t.Charset,
+ Collation: t.Collation,
+ Comment: t.Comment,
+ }
+
+ // Columns.
+ for _, col := range t.Columns {
+ if col.Hidden == catalog.ColumnHiddenSystem {
+ // System-hidden columns (e.g. backing columns of functional
+ // indexes) are not reported by information_schema.
+ continue
+ }
+ colMeta := &metadatapb.ColumnMetadata{
+ Name: col.Name,
+ Position: int32(col.Position),
+ Type: col.ColumnType,
+ Nullable: col.Nullable,
+ CharacterSet: col.Charset,
+ Collation: col.Collation,
+ Comment: col.Comment,
+ }
+ switch {
+ case col.AutoIncrement:
+ // Match the driver convention: AUTO_INCREMENT columns report
+ // "AUTO_INCREMENT" as the default (see backend/plugin/db/mysql/sync.go).
+ colMeta.Default = "AUTO_INCREMENT"
+ case col.Default != nil:
+ colMeta.Default = normalizeOmniDefault(*col.Default, col.DefaultKind)
+ case col.Nullable:
+ // Match the driver convention: a nullable column without an explicit
+ // default has an implicit default of NULL.
+ colMeta.Default = "NULL"
+ default:
+ }
+ if col.OnUpdate != "" {
+ colMeta.OnUpdate = col.OnUpdate
+ }
+ if col.Generated != nil {
+ genType := metadatapb.GenerationMetadata_TYPE_VIRTUAL
+ if col.Generated.Stored {
+ genType = metadatapb.GenerationMetadata_TYPE_STORED
+ }
+ colMeta.Generation = &metadatapb.GenerationMetadata{
+ Type: genType,
+ Expression: col.Generated.Expr,
+ }
+ }
+ // Spatial SRID (MySQL 8.0): presence-carrying — an explicit SRID (including the
+ // valid SRID 0) is distinct from "no SRID". SRS_IDs are unsigned 32-bit, so the
+ // omni catalog's int value is widened to uint32 to match the store proto.
+ if col.HasSRID {
+ srid := uint32(col.SRID)
+ colMeta.Srid = &srid
+ }
+ // INVISIBLE column (MySQL 8.0.23+): user-declared invisibility (distinct from
+ // system-hidden functional-index backing columns filtered above).
+ colMeta.IsInvisible = col.Invisible
+ table.Columns = append(table.Columns, colMeta)
+ }
+
+ // Indexes.
+ for _, idx := range t.Indexes {
+ indexType := strings.ToUpper(idx.IndexType)
+ if indexType == "" {
+ // A key without USING gets the engine's default access method.
+ indexType = "BTREE"
+ if strings.EqualFold(t.Engine, "MEMORY") || strings.EqualFold(t.Engine, "HEAP") {
+ indexType = "HASH"
+ }
+ }
+ idxMeta := &metadatapb.IndexMetadata{
+ Name: idx.Name,
+ Type: indexType,
+ Unique: idx.Unique,
+ Primary: idx.Primary,
+ Visible: idx.Visible,
+ Comment: idx.Comment,
+ }
+ for _, col := range idx.Columns {
+ if col.Expr != "" {
+ idxMeta.Expressions = append(idxMeta.Expressions, col.Expr)
+ } else {
+ idxMeta.Expressions = append(idxMeta.Expressions, col.Name)
+ }
+ keyLength := int64(col.Length)
+ if keyLength == 0 {
+ // Match the driver convention: -1 means no prefix length specified.
+ keyLength = -1
+ }
+ if keyLength == -1 && indexType == "SPATIAL" {
+ // MySQL reports a key length of 32 for spatial index columns.
+ keyLength = 32
+ }
+ idxMeta.KeyLength = append(idxMeta.KeyLength, keyLength)
+ idxMeta.Descending = append(idxMeta.Descending, col.Descending)
+ }
+ table.Indexes = append(table.Indexes, idxMeta)
+ }
+
+ // Foreign keys from constraints.
+ for _, con := range t.Constraints {
+ switch con.Type {
+ case catalog.ConForeignKey:
+ fk := &metadatapb.ForeignKeyMetadata{
+ Name: con.Name,
+ Columns: con.Columns,
+ ReferencedTable: con.RefTable,
+ ReferencedColumns: con.RefColumns,
+ OnUpdate: con.OnUpdate,
+ OnDelete: con.OnDelete,
+ MatchType: con.MatchType,
+ }
+ if con.RefDatabase != "" {
+ fk.ReferencedSchema = con.RefDatabase
+ }
+ table.ForeignKeys = append(table.ForeignKeys, fk)
+ case catalog.ConCheck:
+ table.CheckConstraints = append(table.CheckConstraints, &metadatapb.CheckConstraintMetadata{
+ Name: con.Name,
+ Expression: con.CheckExpr,
+ })
+ default:
+ }
+ }
+
+ // Partition info.
+ if t.Partitioning != nil {
+ table.Partitions = partitionsToProto(t.Partitioning)
+ }
+
+ return table
+}
+
+// normalizeOmniDefault converts an omni catalog default value to the driver
+// representation produced by SyncDBSchema (see backend/plugin/db/mysql/sync.go):
+// static defaults are kept single-quoted for mysqldump compatibility and
+// expression defaults are wrapped in parentheses.
+func normalizeOmniDefault(value string, kind catalog.ColumnDefaultKind) string {
+ switch kind {
+ case catalog.ColumnDefaultConstant:
+ if strings.EqualFold(value, "NULL") {
+ return "NULL"
+ }
+ if len(value) >= 2 && strings.HasPrefix(value, "'") && strings.HasSuffix(value, "'") {
+ return value
+ }
+ return fmt.Sprintf("'%s'", value)
+ case catalog.ColumnDefaultExpression:
+ return fmt.Sprintf("(%s)", value)
+ default:
+ return value
+ }
+}
+
+func partitionsToProto(p *catalog.PartitionInfo) []*metadatapb.TablePartitionMetadata {
+ expr := p.Expr
+ if expr == "" && len(p.Columns) > 0 {
+ // RANGE COLUMNS / LIST COLUMNS / KEY partitioning carries a column
+ // list instead of an expression; the driver reports the joined list.
+ expr = strings.Join(p.Columns, ",")
+ }
+ var result []*metadatapb.TablePartitionMetadata
+ for _, pd := range p.Partitions {
+ result = append(result, &metadatapb.TablePartitionMetadata{
+ Name: pd.Name,
+ Type: partitionTypeToProto(p.Type),
+ Expression: expr,
+ Value: pd.ValueExpr,
+ })
+ }
+ return result
+}
+
+func partitionTypeToProto(t string) metadatapb.TablePartitionMetadata_Type {
+ switch strings.ToUpper(t) {
+ case "RANGE":
+ return metadatapb.TablePartitionMetadata_RANGE
+ case "RANGE COLUMNS":
+ return metadatapb.TablePartitionMetadata_RANGE_COLUMNS
+ case "LIST":
+ return metadatapb.TablePartitionMetadata_LIST
+ case "LIST COLUMNS":
+ return metadatapb.TablePartitionMetadata_LIST_COLUMNS
+ case "HASH":
+ return metadatapb.TablePartitionMetadata_HASH
+ case "LINEAR HASH":
+ return metadatapb.TablePartitionMetadata_LINEAR_HASH
+ case "KEY":
+ return metadatapb.TablePartitionMetadata_KEY
+ case "LINEAR KEY":
+ return metadatapb.TablePartitionMetadata_LINEAR_KEY
+ default:
+ return metadatapb.TablePartitionMetadata_TYPE_UNSPECIFIED
+ }
+}
+
+func routineToFunctionProto(r *catalog.Routine) *metadatapb.FunctionMetadata {
+ return &metadatapb.FunctionMetadata{
+ Name: r.Name,
+ Definition: r.Body,
+ }
+}
+
+func routineToProcedureProto(r *catalog.Routine) *metadatapb.ProcedureMetadata {
+ return &metadatapb.ProcedureMetadata{
+ Name: r.Name,
+ Definition: r.Body,
+ }
+}
+
+// triggerToProto maps an omni catalog Trigger to TriggerMetadata. The live MySQL sync
+// stores only the action statement (ACTION_STATEMENT) in Body plus the timing/event,
+// not a full CREATE TRIGGER; this mirrors that shape so a parsed-DDL catalog and a
+// synced catalog produce the same TriggerMetadata. DEFINER and session charset state
+// are intentionally dropped: omni's trigger differ ignores them.
+func triggerToProto(tr *catalog.Trigger) *metadatapb.TriggerMetadata {
+ return &metadatapb.TriggerMetadata{
+ Name: tr.Name,
+ Event: tr.Event,
+ Timing: tr.Timing,
+ Body: tr.Body,
+ }
+}
+
+// eventToProto maps an omni catalog Event to EventMetadata. The live MySQL sync stores
+// the full SHOW CREATE EVENT text in Definition, so the catalog's ShowCreateEvent is
+// used to reproduce that canonical form (DEFINER included to match the sync; the SDL
+// dump and omni's event differ both ignore it).
+func eventToProto(c *catalog.Catalog, dbName string, e *catalog.Event) *metadatapb.EventMetadata {
+ return &metadatapb.EventMetadata{
+ Name: e.Name,
+ Definition: c.ShowCreateEvent(dbName, e.Name),
+ }
+}
diff --git a/backend/plugin/schema/oceanbase/walk_through_test.go b/backend/plugin/schema/oceanbase/walk_through_test.go
new file mode 100644
index 0000000000..4bec0df220
--- /dev/null
+++ b/backend/plugin/schema/oceanbase/walk_through_test.go
@@ -0,0 +1,27 @@
+package oceanbase
+
+import (
+ "context"
+ "testing"
+
+ metadatapb "github.com/bytebase/omni/metadata"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/component/sheet"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ _ "github.com/bytebase/bytebase/backend/plugin/parser/oceanbase"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+func TestWalkThroughCrossDatabaseRename(t *testing.T) {
+ state := model.NewDatabaseMetadata(&metadatapb.DatabaseSchemaMetadata{Name: "test"}, nil, nil, storepb.Engine_OCEANBASE, true)
+ statement := "CREATE TABLE `test`.`t1` (a INT);\nRENAME TABLE `test`.`t1` TO `other`.`t1`;"
+ sm := sheet.NewManager()
+ stmts, advices := sm.GetStatementsForChecks(storepb.Engine_OCEANBASE, statement)
+ require.Empty(t, advices)
+
+ advice := WalkThroughWithContext(context.Background(), schema.WalkThroughContext{RawSQL: statement}, state, base.ExtractASTs(stmts))
+ require.Nil(t, advice, "a cross-database RENAME the server accepts must not fail the simulation")
+}
diff --git a/backend/plugin/schema/schema.go b/backend/plugin/schema/schema.go
index d7128afb7e..137ef56ac3 100644
--- a/backend/plugin/schema/schema.go
+++ b/backend/plugin/schema/schema.go
@@ -26,7 +26,6 @@ var (
getSequenceDefinitions = make(map[storepb.Engine]getSequenceDefinition)
getDatabaseMetadataMap = make(map[storepb.Engine]getDatabaseMetadata)
generateMigrations = make(map[storepb.Engine]generateMigration)
- getSDLDiffs = make(map[storepb.Engine]getSDLDiff)
sdlDropAdvicesFns = make(map[storepb.Engine]sdlDropAdvices)
diffSDLMigrations = make(map[storepb.Engine]diffSDLMigration)
diffMetadataMigrations = make(map[storepb.Engine]diffMetadataMigration)
@@ -46,7 +45,6 @@ type getProcedureDefinition func(string, *metadatapb.ProcedureMetadata) (string,
type getSequenceDefinition func(string, *metadatapb.SequenceMetadata) (string, error)
type getDatabaseMetadata func(string) (*metadatapb.DatabaseSchemaMetadata, error)
type generateMigration func(*MetadataDiff) (string, error)
-type getSDLDiff func(currentSDLText, previousUserSDLText string, currentSchema *model.DatabaseMetadata) (*MetadataDiff, error)
type sdlDropAdvices func(userSDLText string, currentSchema *model.DatabaseMetadata, engineVersion string) ([]*storepb.Advice, error)
// diffSDLMigration computes migration SQL between two SDL texts. engineVersion is the
@@ -266,23 +264,6 @@ func GenerateMigration(engine storepb.Engine, diff *MetadataDiff) (string, error
return f(diff)
}
-func RegisterGetSDLDiff(engine storepb.Engine, f getSDLDiff) {
- mux.Lock()
- defer mux.Unlock()
- if _, dup := getSDLDiffs[engine]; dup {
- panic(fmt.Sprintf("Register called twice %s", engine))
- }
- getSDLDiffs[engine] = f
-}
-
-func GetSDLDiff(engine storepb.Engine, currentSDLText, previousUserSDLText string, currentSchema *model.DatabaseMetadata) (*MetadataDiff, error) {
- f, ok := getSDLDiffs[engine]
- if !ok {
- return nil, errors.Errorf("engine %s is not supported", engine)
- }
- return f(currentSDLText, previousUserSDLText, currentSchema)
-}
-
// SDLSessionContext is the session state a routine/trigger/event was created under, as
// stored in the synced metadata: sql_mode, character_set_client, collation_connection,
// and — events only — the session time_zone. It is the engine-neutral mirror of omni's
diff --git a/backend/runner/reviewrun/review_funcs.go b/backend/runner/reviewrun/review_funcs.go
index 8ad5956842..b4f62184a9 100644
--- a/backend/runner/reviewrun/review_funcs.go
+++ b/backend/runner/reviewrun/review_funcs.go
@@ -4,6 +4,7 @@ import (
mariadbreview "github.com/bytebase/omni/mariadb/review"
mssqlreview "github.com/bytebase/omni/mssql/review"
mysqlreview "github.com/bytebase/omni/mysql/review"
+ oceanbasereview "github.com/bytebase/omni/oceanbase/review"
oraclereview "github.com/bytebase/omni/oracle/review"
pgreview "github.com/bytebase/omni/pg/review"
"github.com/bytebase/omni/review"
@@ -20,7 +21,7 @@ import (
var reviewFuncs = map[storepb.Engine]review.ReviewFunc{
storepb.Engine_POSTGRES: pgreview.Review,
storepb.Engine_MYSQL: mysqlreview.Review,
- storepb.Engine_OCEANBASE: mysqlreview.ReviewOceanBase,
+ storepb.Engine_OCEANBASE: oceanbasereview.ReviewOceanBase,
storepb.Engine_MARIADB: mariadbreview.Review,
storepb.Engine_TIDB: tidbreview.Review,
storepb.Engine_MSSQL: mssqlreview.Review,
diff --git a/backend/server/ultimate.go b/backend/server/ultimate.go
index 7d612d5aad..139f81a2ee 100644
--- a/backend/server/ultimate.go
+++ b/backend/server/ultimate.go
@@ -63,6 +63,7 @@ import (
_ "github.com/bytebase/bytebase/backend/plugin/schema/clickhouse"
_ "github.com/bytebase/bytebase/backend/plugin/schema/mssql"
_ "github.com/bytebase/bytebase/backend/plugin/schema/mysql"
+ _ "github.com/bytebase/bytebase/backend/plugin/schema/oceanbase"
_ "github.com/bytebase/bytebase/backend/plugin/schema/oracle"
_ "github.com/bytebase/bytebase/backend/plugin/schema/pg"
_ "github.com/bytebase/bytebase/backend/plugin/schema/redshift"
diff --git a/go.mod b/go.mod
index 268be3593d..7f9458eb63 100644
--- a/go.mod
+++ b/go.mod
@@ -36,7 +36,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.10.0
github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0
- github.com/bytebase/omni v0.0.0-20261030092755-1abac16bd0ca
+ github.com/bytebase/omni v0.0.0-20261030181342-3f8520a4550f
github.com/caarlos0/env/v11 v11.4.1
github.com/cenkalti/backoff/v7 v7.0.0
github.com/cockroachdb/cockroach-go/v2 v2.4.3
diff --git a/go.sum b/go.sum
index 02a8a2de7f..f9866801d3 100644
--- a/go.sum
+++ b/go.sum
@@ -252,8 +252,8 @@ github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735 h1:v8y6mXeHmYVsdM
github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735/go.mod h1:vTHjcvfn8Pmf65WjnV1ytTCYyC7xNhFjrniv4YWvtA8=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=
-github.com/bytebase/omni v0.0.0-20261030092755-1abac16bd0ca h1:tb3TsMYgbKertKITDS6rIsmAlxVVrLGMBEnRQVAN5H4=
-github.com/bytebase/omni v0.0.0-20261030092755-1abac16bd0ca/go.mod h1:9XZE1hqtvr09Oc+DdLu1/yCb8MDcoy/EPdA4AONDHlE=
+github.com/bytebase/omni v0.0.0-20261030181342-3f8520a4550f h1:Af5nKHX+D0MSHqKXzi/UjQp7lGlwFx6PL8G9WpiS2I0=
+github.com/bytebase/omni v0.0.0-20261030181342-3f8520a4550f/go.mod h1:9XZE1hqtvr09Oc+DdLu1/yCb8MDcoy/EPdA4AONDHlE=
github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=
github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94 h1:6PNsuNmSqCuR8Z616uQLzDIvujWCFsKzeVc3ECmVubk=