Skip to content

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

refactor(tests): pair every plugin test file with its source file (#21410)

Danny Xu27c4a3bc126a
diff --git a/backend/common/util_test.go b/backend/common/util_test.go
index 7cf755b92e..d4656c06cb 100644
--- a/backend/common/util_test.go
+++ b/backend/common/util_test.go
@@ -3,9 +3,15 @@ package common
import (
"testing"
+ "unicode/utf8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/reflect/protoreflect"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
func TestHasPrefixes(t *testing.T) {
@@ -271,3 +277,189 @@ func TestValidatePhone(t *testing.T) {
}
}
}
+
+// TestSanitizeUTF8String pins the replacement shape: the invalid bytes survive
+// as their hex escape so a corrupted value stays diagnosable after sanitizing.
+func TestSanitizeUTF8String(t *testing.T) {
+ // Vietnamese text encoded in Windows-1258 and stored in an AL32UTF8
+ // database by a misconfigured client: 0xe1 is a valid lead byte with no
+ // continuation.
+ const corrupted = "Tr\xe1ng th\xe1i"
+ require.False(t, utf8.ValidString(corrupted), "test precondition")
+
+ sanitized := SanitizeUTF8String(corrupted)
+
+ require.True(t, utf8.ValidString(sanitized))
+ require.Contains(t, sanitized, "\\xe1")
+ require.Equal(t, "\u6d4b\u8bd5", SanitizeUTF8String("\u6d4b\u8bd5"), "valid UTF-8 must pass through unchanged")
+}
+
+// fillAllStringFields recursively sets every string field reachable from m
+// (including nested messages, repeated fields, and map keys/values) to the
+// given value, instantiating one element for each nested message, list, and
+// map so that no string field in the schema shape stays unvisited.
+func fillAllStringFields(m protoreflect.Message, value string, depth int) {
+ if depth <= 0 {
+ return
+ }
+ fields := m.Descriptor().Fields()
+ for i := 0; i < fields.Len(); i++ {
+ fd := fields.Get(i)
+ switch {
+ case fd.IsMap():
+ mp := m.Mutable(fd).Map()
+ var key protoreflect.MapKey
+ if fd.MapKey().Kind() == protoreflect.StringKind {
+ key = protoreflect.ValueOfString(value).MapKey()
+ } else {
+ key = protoreflect.ValueOfInt64(1).MapKey()
+ }
+ switch fd.MapValue().Kind() {
+ case protoreflect.StringKind:
+ mp.Set(key, protoreflect.ValueOfString(value))
+ case protoreflect.MessageKind:
+ nv := mp.NewValue()
+ fillAllStringFields(nv.Message(), value, depth-1)
+ mp.Set(key, nv)
+ default:
+ mp.Set(key, mp.NewValue())
+ }
+ case fd.IsList():
+ list := m.Mutable(fd).List()
+ switch fd.Kind() {
+ case protoreflect.StringKind:
+ list.Append(protoreflect.ValueOfString(value))
+ case protoreflect.MessageKind:
+ nv := list.NewElement()
+ fillAllStringFields(nv.Message(), value, depth-1)
+ list.Append(nv)
+ default:
+ }
+ case fd.Kind() == protoreflect.StringKind:
+ m.Set(fd, protoreflect.ValueOfString(value))
+ case fd.Kind() == protoreflect.MessageKind:
+ fillAllStringFields(m.Mutable(fd).Message(), value, depth-1)
+ default:
+ }
+ }
+}
+
+// countInvalidStringFields walks m and returns how many string values
+// (fields, list elements, map keys and values) hold invalid UTF-8.
+
+// countInvalidStringFields walks m and returns how many string values
+// (fields, list elements, map keys and values) hold invalid UTF-8.
+func countInvalidStringFields(m protoreflect.Message) int {
+ count := 0
+ m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
+ switch {
+ case fd.IsMap():
+ v.Map().Range(func(k protoreflect.MapKey, mv protoreflect.Value) bool {
+ if fd.MapKey().Kind() == protoreflect.StringKind && !utf8.ValidString(k.String()) {
+ count++
+ }
+ switch fd.MapValue().Kind() {
+ case protoreflect.StringKind:
+ if !utf8.ValidString(mv.String()) {
+ count++
+ }
+ case protoreflect.MessageKind:
+ count += countInvalidStringFields(mv.Message())
+ default:
+ }
+ return true
+ })
+ case fd.IsList():
+ list := v.List()
+ for i := 0; i < list.Len(); i++ {
+ switch fd.Kind() {
+ case protoreflect.StringKind:
+ if !utf8.ValidString(list.Get(i).String()) {
+ count++
+ }
+ case protoreflect.MessageKind:
+ count += countInvalidStringFields(list.Get(i).Message())
+ default:
+ }
+ }
+ case fd.Kind() == protoreflect.StringKind:
+ if !utf8.ValidString(v.String()) {
+ count++
+ }
+ case fd.Kind() == protoreflect.MessageKind:
+ count += countInvalidStringFields(v.Message())
+ default:
+ }
+ return true
+ })
+ return count
+}
+
+// TestSanitizeUTF8MessageCoversEveryStringField proves the BYT-9916 fix
+// covers every string field of the database metadata — names, types,
+// defaults, definitions — not just comments: fill each one with the raw-GBK
+// shape go-ora leaks for values ending in a dangling lead byte, then assert
+// sanitization leaves zero invalid strings and proto marshaling succeeds.
+
+// TestSanitizeUTF8MessageCoversEveryStringField proves the BYT-9916 fix
+// covers every string field of the database metadata — names, types,
+// defaults, definitions — not just comments: fill each one with the raw-GBK
+// shape go-ora leaks for values ending in a dangling lead byte, then assert
+// sanitization leaves zero invalid strings and proto marshaling succeeds.
+func TestSanitizeUTF8MessageCoversEveryStringField(t *testing.T) {
+ // 测试 + dangling lead byte 0xb1: the exact wholly-unconverted raw GBK
+ // shape go-ora returns (engine-verified against Oracle 11gR2/ZHS16GBK).
+ rawGBK := "\xb2\xe2\xca\xd4\xb1"
+ require.False(t, utf8.ValidString(rawGBK))
+
+ metadata := &storepb.DatabaseSchemaMetadata{}
+ fillAllStringFields(metadata.ProtoReflect(), rawGBK, 8)
+
+ invalidBefore := countInvalidStringFields(metadata.ProtoReflect())
+ require.Greater(t, invalidBefore, 100,
+ "filler must reach the full schema shape; got only %d string fields", invalidBefore)
+ _, err := proto.Marshal(metadata)
+ require.Error(t, err, "pre-sanitize marshal must fail — otherwise this test proves nothing")
+
+ SanitizeUTF8Message(metadata)
+
+ require.Equal(t, 0, countInvalidStringFields(metadata.ProtoReflect()),
+ "sanitization must leave zero invalid string values anywhere in the message")
+ _, err = proto.Marshal(metadata)
+ require.NoError(t, err)
+ _, err = protojson.Marshal(metadata)
+ require.NoError(t, err)
+}
+
+// TestSanitizeUTF8MessageNameFields pins the customer-visible BYT-9916 shape:
+// object names (schema/table/column) carrying raw GBK bytes must marshal
+// after sanitization, and valid names must pass through untouched.
+
+// TestSanitizeUTF8MessageNameFields pins the customer-visible BYT-9916 shape:
+// object names (schema/table/column) carrying raw GBK bytes must marshal
+// after sanitization, and valid names must pass through untouched.
+func TestSanitizeUTF8MessageNameFields(t *testing.T) {
+ rawGBK := "AB\xe6"
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: rawGBK,
+ Schemas: []*storepb.SchemaMetadata{{
+ Name: rawGBK,
+ Tables: []*storepb.TableMetadata{{
+ Name: rawGBK,
+ Columns: []*storepb.ColumnMetadata{
+ {Name: rawGBK, Type: rawGBK, Default: rawGBK},
+ {Name: "测试列", Type: "VARCHAR2(50)"},
+ },
+ }},
+ }},
+ }
+ _, err := proto.Marshal(metadata)
+ require.Error(t, err)
+
+ SanitizeUTF8Message(metadata)
+ _, err = proto.Marshal(metadata)
+ require.NoError(t, err)
+ require.Equal(t, "测试列", metadata.Schemas[0].Tables[0].Columns[1].Name,
+ "valid UTF-8 must pass through unchanged")
+ require.True(t, utf8.ValidString(metadata.Schemas[0].Tables[0].Name))
+}
diff --git a/backend/plugin/advisor/oceanbase/antlr_dependency_test.go b/backend/plugin/advisor/oceanbase/antlr_dependency_test.go
deleted file mode 100644
index 6c8646347d..0000000000
--- a/backend/plugin/advisor/oceanbase/antlr_dependency_test.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package oceanbase
-
-import (
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestOceanBaseAdvisorsDoNotUseMySQLANTLRBridge(t *testing.T) {
- files := []string{
- "advisor_disallow_offline_ddl.go",
- "advisor_insert_row_limit.go",
- "advisor_statement_affected_row_limit.go",
- }
- for _, file := range files {
- t.Run(file, func(t *testing.T) {
- content, err := os.ReadFile(file)
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")
- require.NotContains(t, source, "github.com/bytebase/parser/mysql")
- require.NotContains(t, source, "base.GetANTLRAST")
- require.NotContains(t, source, "BaseMySQLParserListener")
- })
- }
-}
diff --git a/backend/plugin/db/cosmosdb/cosmosdb_integration_test.go b/backend/plugin/db/cosmosdb/cosmosdb_test.go
similarity index 100%
rename from backend/plugin/db/cosmosdb/cosmosdb_integration_test.go
rename to backend/plugin/db/cosmosdb/cosmosdb_test.go
diff --git a/backend/plugin/db/mongodb/mongodb_test.go b/backend/plugin/db/mongodb/mongodb_test.go
index 31c386e55c..44d8b1ac39 100644
--- a/backend/plugin/db/mongodb/mongodb_test.go
+++ b/backend/plugin/db/mongodb/mongodb_test.go
@@ -2,12 +2,18 @@ package mongodb
import (
"context"
+ "strings"
"testing"
"time"
+ "github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
+ "go.mongodb.org/mongo-driver/v2/bson"
+ "google.golang.org/protobuf/testing/protocmp"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -210,3 +216,263 @@ func TestExecuteLogsParseFailure(t *testing.T) {
require.Equal(t, statement, logs[0].CommandExecute.GetStatement())
})
}
+
+// TestQueryWithBracketNotation tests the critical user journey (CUJ) of querying
+// MongoDB collections using bracket notation with different quote styles.
+// This ensures the fix for PR #17282 (which changed to single-quote bracket notation
+// for special characters) works correctly.
+func TestQueryWithBracketNotation(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping MongoDB testcontainer test in short mode")
+ }
+
+ ctx := context.Background()
+
+ // Get MongoDB container from testcontainer utility
+ container := testcontainer.GetTestMongoDBContainer(ctx, t)
+ defer container.Close(ctx)
+
+ // Create test database and collection with some data
+ driver := &Driver{}
+ connConfig := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: container.GetHost(),
+ Port: container.GetPort(),
+ Username: container.GetUsername(),
+ },
+ ConnectionContext: db.ConnectionContext{
+ DatabaseName: "testdb",
+ },
+ Password: container.GetPassword(),
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_MONGODB, connConfig)
+ require.NoError(t, err)
+ defer openedDriver.Close(ctx)
+
+ // Wait for MongoDB to be fully ready
+ err = openedDriver.Ping(ctx)
+ require.NoError(t, err, "Failed to ping MongoDB")
+
+ // Create a collection with a special character in the name to test the real CUJ
+ collectionName := "test.collection"
+
+ // Insert test data
+ insertStatement := `db['` + collectionName + `'].insertMany([
+ {"name": "Alice", "age": 30},
+ {"name": "Bob", "age": 25},
+ {"name": "Charlie", "age": 35}
+ ]);`
+
+ _, err = openedDriver.Execute(ctx, insertStatement, db.ExecuteOptions{})
+ require.NoError(t, err, "Failed to insert test data")
+
+ // Test cases for different bracket notation styles
+ testCases := []struct {
+ name string
+ statement string
+ }{
+ {
+ name: "double_quote_bracket",
+ statement: `db["` + collectionName + `"].find().limit(50)`,
+ },
+ {
+ name: "single_quote_bracket",
+ statement: `db['` + collectionName + `'].find().limit(50)`,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ results, err := openedDriver.QueryConn(ctx, nil, tc.statement, db.QueryContext{
+ Limit: 50,
+ MaximumSQLResultSize: 10 * 1024 * 1024, // 10MB
+ })
+
+ // Verify the query succeeded
+ require.NoError(t, err, "Query should not return an error")
+ require.NotNil(t, results, "Results should not be nil")
+ require.Len(t, results, 1, "Should return one query result")
+
+ result := results[0]
+
+ // Verify no error in the result
+ require.Empty(t, result.Error, "Result should not contain an error")
+
+ // Verify the result structure matches expected format
+ require.Equal(t, []string{"result"}, result.ColumnNames, "Should have 'result' column")
+ require.Equal(t, []string{"TEXT"}, result.ColumnTypeNames, "Column type should be 'TEXT'")
+
+ // Verify we got exactly 3 rows back
+ require.NotNil(t, result.Rows, "Rows should not be nil")
+ require.Len(t, result.Rows, 3, "Should return 3 rows")
+
+ // Verify each row structure and content
+ for i, row := range result.Rows {
+ require.Len(t, row.Values, 1, "Each row should have one value")
+ require.NotNil(t, row.Values[0].GetStringValue(), "Value should be a string")
+
+ // Verify the content is valid JSON that contains expected fields
+ jsonStr := row.Values[0].GetStringValue()
+ require.Contains(t, jsonStr, `"name"`, "Row %d should contain 'name' field", i)
+ require.Contains(t, jsonStr, `"age"`, "Row %d should contain 'age' field", i)
+ require.True(t,
+ strings.Contains(jsonStr, "Alice") ||
+ strings.Contains(jsonStr, "Bob") ||
+ strings.Contains(jsonStr, "Charlie"),
+ "Row %d should contain a valid name", i)
+ }
+
+ // Verify the statement is recorded correctly
+ require.Equal(t, tc.statement, result.Statement, "Statement should match the input")
+ })
+ }
+}
+
+// TestQueryWithBracketNotationStructure tests the exact structure of query results
+// using protocmp to ensure the result format is correct.
+func TestQueryWithBracketNotationStructure(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping MongoDB testcontainer test in short mode")
+ }
+
+ ctx := context.Background()
+
+ // Get MongoDB container from testcontainer utility
+ container := testcontainer.GetTestMongoDBContainer(ctx, t)
+ defer container.Close(ctx)
+
+ // Create test database and collection with deterministic data
+ driver := &Driver{}
+ connConfig := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: container.GetHost(),
+ Port: container.GetPort(),
+ Username: container.GetUsername(),
+ },
+ ConnectionContext: db.ConnectionContext{
+ DatabaseName: "testdb",
+ },
+ Password: container.GetPassword(),
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_MONGODB, connConfig)
+ require.NoError(t, err)
+ defer openedDriver.Close(ctx)
+
+ // Wait for MongoDB to be fully ready
+ err = openedDriver.Ping(ctx)
+ require.NoError(t, err, "Failed to ping MongoDB")
+
+ collectionName := "users"
+
+ // Insert a single deterministic document for easier verification
+ insertStatement := `db['` + collectionName + `'].insertOne({"_id": 1, "name": "Test User", "age": 25});`
+ _, err = openedDriver.Execute(ctx, insertStatement, db.ExecuteOptions{})
+ require.NoError(t, err, "Failed to insert test data")
+
+ // Query with bracket notation
+ statement := `db['` + collectionName + `'].find().limit(1)`
+ results, err := openedDriver.QueryConn(ctx, nil, statement, db.QueryContext{
+ Limit: 50,
+ MaximumSQLResultSize: 10 * 1024 * 1024,
+ })
+
+ require.NoError(t, err)
+ require.Len(t, results, 1)
+
+ result := results[0]
+
+ // Build expected result structure (ignoring dynamic fields like Latency and RowsCount)
+ expected := &v1pb.QueryResult{
+ ColumnNames: []string{"result"},
+ ColumnTypeNames: []string{"TEXT"},
+ Rows: []*v1pb.QueryRow{
+ {
+ Values: []*v1pb.RowValue{
+ {
+ Kind: &v1pb.RowValue_StringValue{
+ StringValue: result.Rows[0].Values[0].GetStringValue(), // Use actual value for comparison
+ },
+ },
+ },
+ },
+ },
+ Statement: statement,
+ }
+
+ // Compare with protocmp, ignoring Latency and RowsCount which are non-deterministic
+ diff := cmp.Diff(expected, result,
+ protocmp.Transform(),
+ protocmp.IgnoreFields(&v1pb.QueryResult{}, "latency", "rows_count"),
+ )
+ require.Empty(t, diff, "Result structure should match expected format")
+
+ // Verify the actual JSON content
+ jsonStr := result.Rows[0].Values[0].GetStringValue()
+ require.Contains(t, jsonStr, `"_id"`)
+ require.Contains(t, jsonStr, `"name"`)
+ require.Contains(t, jsonStr, `"age"`)
+ require.Contains(t, jsonStr, "Test User")
+}
+
+// TestQueryDoubleNotation reproduces GitHub issue #20895: BSON doubles holding
+// large integers must render in mongosh notation (plain decimal), not the Go
+// driver's scientific notation (e.g. 1.779696815227E+12).
+func TestQueryDoubleNotation(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping MongoDB testcontainer test in short mode")
+ }
+
+ ctx := context.Background()
+
+ container := testcontainer.GetTestMongoDBContainer(ctx, t)
+ defer container.Close(ctx)
+
+ driver := &Driver{}
+ connConfig := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: container.GetHost(),
+ Port: container.GetPort(),
+ Username: container.GetUsername(),
+ },
+ ConnectionContext: db.ConnectionContext{
+ DatabaseName: "testdb",
+ },
+ Password: container.GetPassword(),
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_MONGODB, connConfig)
+ require.NoError(t, err)
+ defer openedDriver.Close(ctx)
+
+ err = openedDriver.Ping(ctx)
+ require.NoError(t, err, "Failed to ping MongoDB")
+
+ // Insert through the official driver so the field types are guaranteed
+ // BSON doubles, matching the issue's data shape.
+ mongoDriver, ok := openedDriver.(*Driver)
+ require.True(t, ok)
+ _, err = mongoDriver.client.Database("testdb").Collection("users").InsertOne(ctx, bson.D{
+ {Key: "_id", Value: int32(1129063441)},
+ {Key: "name", Value: "repro user 1"},
+ {Key: "created_at", Value: float64(1779696815227)},
+ {Key: "updated_at", Value: float64(1779803079861)},
+ {Key: "current_workspace_id", Value: float64(585723378473606)},
+ })
+ require.NoError(t, err, "Failed to insert test data")
+
+ results, err := openedDriver.QueryConn(ctx, nil, `db.users.find()`, db.QueryContext{
+ Limit: 50,
+ MaximumSQLResultSize: 10 * 1024 * 1024,
+ })
+ require.NoError(t, err)
+ require.Len(t, results, 1)
+ require.Empty(t, results[0].Error)
+ require.Len(t, results[0].Rows, 1)
+
+ jsonStr := results[0].Rows[0].Values[0].GetStringValue()
+ require.Equal(t,
+ `{"_id":1129063441,"name":"repro user 1","created_at":1779696815227,"updated_at":1779803079861,"current_workspace_id":585723378473606}`,
+ jsonStr)
+}
diff --git a/backend/plugin/db/mongodb/mongodb_testcontainer_test.go b/backend/plugin/db/mongodb/mongodb_testcontainer_test.go
deleted file mode 100644
index f79746bf55..0000000000
--- a/backend/plugin/db/mongodb/mongodb_testcontainer_test.go
+++ /dev/null
@@ -1,277 +0,0 @@
-package mongodb
-
-import (
- "context"
- "strings"
- "testing"
-
- "github.com/google/go-cmp/cmp"
- "github.com/stretchr/testify/require"
- "go.mongodb.org/mongo-driver/v2/bson"
- "google.golang.org/protobuf/testing/protocmp"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-// TestQueryWithBracketNotation tests the critical user journey (CUJ) of querying
-// MongoDB collections using bracket notation with different quote styles.
-// This ensures the fix for PR #17282 (which changed to single-quote bracket notation
-// for special characters) works correctly.
-func TestQueryWithBracketNotation(t *testing.T) {
- if testing.Short() {
- t.Skip("Skipping MongoDB testcontainer test in short mode")
- }
-
- ctx := context.Background()
-
- // Get MongoDB container from testcontainer utility
- container := testcontainer.GetTestMongoDBContainer(ctx, t)
- defer container.Close(ctx)
-
- // Create test database and collection with some data
- driver := &Driver{}
- connConfig := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Host: container.GetHost(),
- Port: container.GetPort(),
- Username: container.GetUsername(),
- },
- ConnectionContext: db.ConnectionContext{
- DatabaseName: "testdb",
- },
- Password: container.GetPassword(),
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_MONGODB, connConfig)
- require.NoError(t, err)
- defer openedDriver.Close(ctx)
-
- // Wait for MongoDB to be fully ready
- err = openedDriver.Ping(ctx)
- require.NoError(t, err, "Failed to ping MongoDB")
-
- // Create a collection with a special character in the name to test the real CUJ
- collectionName := "test.collection"
-
- // Insert test data
- insertStatement := `db['` + collectionName + `'].insertMany([
- {"name": "Alice", "age": 30},
- {"name": "Bob", "age": 25},
- {"name": "Charlie", "age": 35}
- ]);`
-
- _, err = openedDriver.Execute(ctx, insertStatement, db.ExecuteOptions{})
- require.NoError(t, err, "Failed to insert test data")
-
- // Test cases for different bracket notation styles
- testCases := []struct {
- name string
- statement string
- }{
- {
- name: "double_quote_bracket",
- statement: `db["` + collectionName + `"].find().limit(50)`,
- },
- {
- name: "single_quote_bracket",
- statement: `db['` + collectionName + `'].find().limit(50)`,
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- results, err := openedDriver.QueryConn(ctx, nil, tc.statement, db.QueryContext{
- Limit: 50,
- MaximumSQLResultSize: 10 * 1024 * 1024, // 10MB
- })
-
- // Verify the query succeeded
- require.NoError(t, err, "Query should not return an error")
- require.NotNil(t, results, "Results should not be nil")
- require.Len(t, results, 1, "Should return one query result")
-
- result := results[0]
-
- // Verify no error in the result
- require.Empty(t, result.Error, "Result should not contain an error")
-
- // Verify the result structure matches expected format
- require.Equal(t, []string{"result"}, result.ColumnNames, "Should have 'result' column")
- require.Equal(t, []string{"TEXT"}, result.ColumnTypeNames, "Column type should be 'TEXT'")
-
- // Verify we got exactly 3 rows back
- require.NotNil(t, result.Rows, "Rows should not be nil")
- require.Len(t, result.Rows, 3, "Should return 3 rows")
-
- // Verify each row structure and content
- for i, row := range result.Rows {
- require.Len(t, row.Values, 1, "Each row should have one value")
- require.NotNil(t, row.Values[0].GetStringValue(), "Value should be a string")
-
- // Verify the content is valid JSON that contains expected fields
- jsonStr := row.Values[0].GetStringValue()
- require.Contains(t, jsonStr, `"name"`, "Row %d should contain 'name' field", i)
- require.Contains(t, jsonStr, `"age"`, "Row %d should contain 'age' field", i)
- require.True(t,
- strings.Contains(jsonStr, "Alice") ||
- strings.Contains(jsonStr, "Bob") ||
- strings.Contains(jsonStr, "Charlie"),
- "Row %d should contain a valid name", i)
- }
-
- // Verify the statement is recorded correctly
- require.Equal(t, tc.statement, result.Statement, "Statement should match the input")
- })
- }
-}
-
-// TestQueryWithBracketNotationStructure tests the exact structure of query results
-// using protocmp to ensure the result format is correct.
-func TestQueryWithBracketNotationStructure(t *testing.T) {
- if testing.Short() {
- t.Skip("Skipping MongoDB testcontainer test in short mode")
- }
-
- ctx := context.Background()
-
- // Get MongoDB container from testcontainer utility
- container := testcontainer.GetTestMongoDBContainer(ctx, t)
- defer container.Close(ctx)
-
- // Create test database and collection with deterministic data
- driver := &Driver{}
- connConfig := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Host: container.GetHost(),
- Port: container.GetPort(),
- Username: container.GetUsername(),
- },
- ConnectionContext: db.ConnectionContext{
- DatabaseName: "testdb",
- },
- Password: container.GetPassword(),
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_MONGODB, connConfig)
- require.NoError(t, err)
- defer openedDriver.Close(ctx)
-
- // Wait for MongoDB to be fully ready
- err = openedDriver.Ping(ctx)
- require.NoError(t, err, "Failed to ping MongoDB")
-
- collectionName := "users"
-
- // Insert a single deterministic document for easier verification
- insertStatement := `db['` + collectionName + `'].insertOne({"_id": 1, "name": "Test User", "age": 25});`
- _, err = openedDriver.Execute(ctx, insertStatement, db.ExecuteOptions{})
- require.NoError(t, err, "Failed to insert test data")
-
- // Query with bracket notation
- statement := `db['` + collectionName + `'].find().limit(1)`
- results, err := openedDriver.QueryConn(ctx, nil, statement, db.QueryContext{
- Limit: 50,
- MaximumSQLResultSize: 10 * 1024 * 1024,
- })
-
- require.NoError(t, err)
- require.Len(t, results, 1)
-
- result := results[0]
-
- // Build expected result structure (ignoring dynamic fields like Latency and RowsCount)
- expected := &v1pb.QueryResult{
- ColumnNames: []string{"result"},
- ColumnTypeNames: []string{"TEXT"},
- Rows: []*v1pb.QueryRow{
- {
- Values: []*v1pb.RowValue{
- {
- Kind: &v1pb.RowValue_StringValue{
- StringValue: result.Rows[0].Values[0].GetStringValue(), // Use actual value for comparison
- },
- },
- },
- },
- },
- Statement: statement,
- }
-
- // Compare with protocmp, ignoring Latency and RowsCount which are non-deterministic
- diff := cmp.Diff(expected, result,
- protocmp.Transform(),
- protocmp.IgnoreFields(&v1pb.QueryResult{}, "latency", "rows_count"),
- )
- require.Empty(t, diff, "Result structure should match expected format")
-
- // Verify the actual JSON content
- jsonStr := result.Rows[0].Values[0].GetStringValue()
- require.Contains(t, jsonStr, `"_id"`)
- require.Contains(t, jsonStr, `"name"`)
- require.Contains(t, jsonStr, `"age"`)
- require.Contains(t, jsonStr, "Test User")
-}
-
-// TestQueryDoubleNotation reproduces GitHub issue #20895: BSON doubles holding
-// large integers must render in mongosh notation (plain decimal), not the Go
-// driver's scientific notation (e.g. 1.779696815227E+12).
-func TestQueryDoubleNotation(t *testing.T) {
- if testing.Short() {
- t.Skip("Skipping MongoDB testcontainer test in short mode")
- }
-
- ctx := context.Background()
-
- container := testcontainer.GetTestMongoDBContainer(ctx, t)
- defer container.Close(ctx)
-
- driver := &Driver{}
- connConfig := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Host: container.GetHost(),
- Port: container.GetPort(),
- Username: container.GetUsername(),
- },
- ConnectionContext: db.ConnectionContext{
- DatabaseName: "testdb",
- },
- Password: container.GetPassword(),
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_MONGODB, connConfig)
- require.NoError(t, err)
- defer openedDriver.Close(ctx)
-
- err = openedDriver.Ping(ctx)
- require.NoError(t, err, "Failed to ping MongoDB")
-
- // Insert through the official driver so the field types are guaranteed
- // BSON doubles, matching the issue's data shape.
- mongoDriver, ok := openedDriver.(*Driver)
- require.True(t, ok)
- _, err = mongoDriver.client.Database("testdb").Collection("users").InsertOne(ctx, bson.D{
- {Key: "_id", Value: int32(1129063441)},
- {Key: "name", Value: "repro user 1"},
- {Key: "created_at", Value: float64(1779696815227)},
- {Key: "updated_at", Value: float64(1779803079861)},
- {Key: "current_workspace_id", Value: float64(585723378473606)},
- })
- require.NoError(t, err, "Failed to insert test data")
-
- results, err := openedDriver.QueryConn(ctx, nil, `db.users.find()`, db.QueryContext{
- Limit: 50,
- MaximumSQLResultSize: 10 * 1024 * 1024,
- })
- require.NoError(t, err)
- require.Len(t, results, 1)
- require.Empty(t, results[0].Error)
- require.Len(t, results[0].Rows, 1)
-
- jsonStr := results[0].Rows[0].Values[0].GetStringValue()
- require.Equal(t,
- `{"_id":1129063441,"name":"repro user 1","created_at":1779696815227,"updated_at":1779803079861,"current_workspace_id":585723378473606}`,
- jsonStr)
-}
diff --git a/backend/plugin/db/mssql/resource_change_integration_testcontainer_test.go b/backend/plugin/db/mssql/resource_change_integration_testcontainer_test.go
deleted file mode 100644
index 150e7f5ac3..0000000000
--- a/backend/plugin/db/mssql/resource_change_integration_testcontainer_test.go
+++ /dev/null
@@ -1,177 +0,0 @@
-package mssql
-
-import (
- "context"
- "fmt"
- "strings"
- "testing"
- "time"
-
- _ "github.com/microsoft/go-mssqldb"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
- parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"
- _ "github.com/bytebase/bytebase/backend/plugin/parser/tsql" // register MSSQL statement parser
-)
-
-// TestResourceChangeIntegrationMSSQL verifies the resource_change.go migration
-// against a real SQL Server. It targets three claims made during review:
-// 1. INSERT ... DEFAULT VALUES: our new code skips EXPLAIN and directly
-// counts 1 row. Proving SHOWPLAN would also return 1 confirms the final
-// affected-rows number is unchanged.
-// 2. A statement whose text carries a leading SQL comment (from the
-// split-layer's gap-absorption behavior) must still be accepted by
-// SHOWPLAN_ALL so the downstream estimate is unaffected.
-// 3. DROP INDEX ... ON t registers t in ChangedResources (omni 51d40fb
-// exposed the target table; migration now consumes it).
-//
-//nolint:tparallel
-func TestResourceChangeIntegrationMSSQL(t *testing.T) {
- ctx := context.Background()
- container := testcontainer.SharedMSSQLContainer(t)
-
- host := container.GetHost()
- port := container.GetPort()
-
- dbName := fmt.Sprintf("resource_change_%d", time.Now().UnixNano())
- driver := openMSSQL(ctx, t, host, port, "master")
- _, err := driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", dbName), db.ExecuteOptions{CreateDatabase: true})
- require.NoError(t, err)
- driver.Close(ctx)
-
- defer func() {
- cleanup := openMSSQL(ctx, t, host, port, "master")
- defer cleanup.Close(ctx)
- _, _ = cleanup.Execute(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", dbName), db.ExecuteOptions{CreateDatabase: true})
- _, _ = cleanup.Execute(ctx, fmt.Sprintf("DROP DATABASE [%s]", dbName), db.ExecuteOptions{CreateDatabase: true})
- }()
-
- driver = openMSSQL(ctx, t, host, port, dbName)
- defer driver.Close(ctx)
- mssqlDriver, ok := driver.(*Driver)
- require.True(t, ok, "expected *Driver")
-
- // Seed a small table with 5 rows so UPDATE/DELETE have something to plan against.
- setup := []string{
- `CREATE TABLE dbo.t (id INT IDENTITY(1,1) PRIMARY KEY, c1 INT NULL)`,
- `INSERT INTO dbo.t (c1) VALUES (1),(2),(3),(4),(5)`,
- `CREATE INDEX idx_c1 ON dbo.t(c1)`,
- }
- for _, s := range setup {
- _, err := driver.Execute(ctx, s, db.ExecuteOptions{})
- require.NoError(t, err, "setup: %s", s)
- }
-
- t.Run("INSERT_DEFAULT_VALUES_parity", func(t *testing.T) {
- sql := `INSERT INTO dbo.t DEFAULT VALUES;`
-
- // Path 1 (new code): extractor must route this into InsertCount with no sample.
- summary := summarize(t, sql)
- require.Equal(t, 1, summary.InsertCount, "DEFAULT VALUES should be counted as 1 known insert")
- require.Equal(t, 0, summary.DMLCount, "DEFAULT VALUES must not be sent to EXPLAIN")
- require.Empty(t, summary.SampleDMLS, "no sample text should be emitted for DEFAULT VALUES")
-
- // Path 2 (proof of parity): if we HAD sent it through SHOWPLAN, what would it return?
- // Matches the old ANTLR code's dmlCount+sample path. Must be 1 so the final affected-rows
- // number is identical between old and new.
- showplanRows, err := mssqlDriver.CountAffectedRows(ctx, sql)
- require.NoError(t, err)
- require.EqualValues(t, 1, showplanRows, "SHOWPLAN_ALL must also estimate DEFAULT VALUES as 1 row")
- })
-
- t.Run("UPDATE_with_leading_comment_showplan_ok", func(t *testing.T) {
- // New code's SampleDMLS may contain a leading comment because
- // split.go absorbs inter-statement gaps into the next statement.
- // Assert that SQL Server's SHOWPLAN_ALL accepts such text verbatim
- // and returns a usable EstimateRows, so the downstream number is
- // not affected by the cosmetic regression.
- sampleAsSentToShowplan := "-- header comment\nUPDATE dbo.t SET c1 = 99 WHERE id = 1;"
- rows, err := mssqlDriver.CountAffectedRows(ctx, sampleAsSentToShowplan)
- require.NoError(t, err, "SHOWPLAN should accept leading comments")
- require.EqualValues(t, 1, rows, "UPDATE of id=1 should estimate 1 row regardless of comment prefix")
-
- // Control: same UPDATE with no comment prefix should yield the same estimate.
- rows2, err := mssqlDriver.CountAffectedRows(ctx, "UPDATE dbo.t SET c1 = 99 WHERE id = 1;")
- require.NoError(t, err)
- require.Equal(t, rows, rows2, "leading comment must not change the estimate")
- })
-
- t.Run("INSERT_VALUES_multi_row_insertCount", func(t *testing.T) {
- sql := `INSERT INTO dbo.t (c1) VALUES (10),(20),(30);`
- summary := summarize(t, sql)
- require.Equal(t, 3, summary.InsertCount)
- require.Equal(t, 0, summary.DMLCount)
- require.Empty(t, summary.SampleDMLS)
- })
-
- t.Run("DROP_INDEX_tracks_target_table", func(t *testing.T) {
- sql := `DROP INDEX idx_c1 ON dbo.t;`
- summary := summarize(t, sql)
-
- proto := summary.ChangedResources.Build()
- found := false
- for _, database := range proto.Databases {
- for _, schema := range database.Schemas {
- for _, tbl := range schema.Tables {
- if schema.Name == "dbo" && tbl.Name == "t" {
- found = true
- }
- }
- }
- }
- require.True(t, found, "DROP INDEX ... ON dbo.t should register dbo.t as a changed table (omni 51d40fb)")
-
- // Sanity: executing the DROP INDEX against the seeded DB should succeed,
- // proving the SQL we parsed is actually runnable.
- _, err := driver.Execute(ctx, sql, db.ExecuteOptions{})
- require.NoError(t, err)
- })
-
- t.Run("UPDATE_goes_through_explain", func(t *testing.T) {
- sql := `UPDATE dbo.t SET c1 = c1 + 1 WHERE id > 2;`
- summary := summarize(t, sql)
- require.Equal(t, 0, summary.InsertCount)
- require.Equal(t, 1, summary.DMLCount)
- require.Len(t, summary.SampleDMLS, 1)
- require.Equal(t, strings.TrimSpace(sql), summary.SampleDMLS[0])
-
- rows, err := mssqlDriver.CountAffectedRows(ctx, summary.SampleDMLS[0])
- require.NoError(t, err)
- // Seeded 5 rows with id 1..5; predicate matches ids 3,4,5 → 3 rows.
- require.EqualValues(t, 3, rows)
- })
-}
-
-func summarize(t *testing.T, sql string) *parserbase.ChangeSummary {
- t.Helper()
- stmts, err := parserbase.ParseStatements(storepb.Engine_MSSQL, sql)
- require.NoError(t, err)
- asts := parserbase.ExtractASTs(stmts)
- summary, err := parserbase.ExtractChangedResources(storepb.Engine_MSSQL, "resource_change_test", "dbo", nil, asts, sql)
- require.NoError(t, err)
- return summary
-}
-
-func openMSSQL(ctx context.Context, t *testing.T, host, port, database string) db.Driver {
- t.Helper()
- driverInstance := &Driver{}
- cfg := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: "sa",
- Host: host,
- Port: port,
- Database: database,
- },
- Password: "Test123!",
- ConnectionContext: db.ConnectionContext{
- DatabaseName: database,
- },
- }
- driver, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, cfg)
- require.NoError(t, err)
- return driver
-}
diff --git a/backend/plugin/db/mssql/stats_test.go b/backend/plugin/db/mssql/stats_test.go
new file mode 100644
index 0000000000..8f695eb5f5
--- /dev/null
+++ b/backend/plugin/db/mssql/stats_test.go
@@ -0,0 +1,93 @@
+package mssql
+
+import (
+ "context"
+ "testing"
+
+ _ "github.com/microsoft/go-mssqldb"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ _ "github.com/bytebase/bytebase/backend/plugin/parser/tsql"
+)
+
+// TestCountAffectedRows pins what SHOWPLAN_ALL estimates for the statement
+// shapes the resource-change extractor hands to it. INSERT ... DEFAULT VALUES
+// is counted by the extractor rather than planned, so the parity assertion
+// below is what proves skipping the plan does not change the reported number.
+// The split layer absorbs inter-statement gaps into the next statement, so a
+// sample can reach SHOWPLAN with a leading comment.
+func TestCountAffectedRows(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+ container := testcontainer.SharedMSSQLContainer(t)
+
+ driver := newSyncTestDatabase(ctx, t, container)
+ executeBatches(ctx, t, driver, `
+CREATE TABLE dbo.t (id INT IDENTITY(1,1) PRIMARY KEY, c1 INT NULL);
+GO
+
+INSERT INTO dbo.t (c1) VALUES (1),(2),(3),(4),(5);
+GO
+
+-- EstimateRows is a cardinality estimate, so it is only stable once the
+-- optimizer has statistics. Without them SQL Server falls back to guessing a
+-- fixed selectivity for a range predicate and the counts below drift.
+UPDATE STATISTICS dbo.t WITH FULLSCAN;
+GO
+`)
+
+ t.Run("insert_default_values_estimates_one_row", func(t *testing.T) {
+ t.Parallel()
+
+ rows, err := driver.CountAffectedRows(ctx, `INSERT INTO dbo.t DEFAULT VALUES;`)
+ require.NoError(t, err)
+ require.EqualValues(t, 1, rows)
+ })
+
+ t.Run("leading_comment_does_not_change_estimate", func(t *testing.T) {
+ t.Parallel()
+
+ const update = "UPDATE dbo.t SET c1 = 99 WHERE id = 1;"
+
+ withComment, err := driver.CountAffectedRows(ctx, "-- header comment\n"+update)
+ require.NoError(t, err)
+ bare, err := driver.CountAffectedRows(ctx, update)
+ require.NoError(t, err)
+ require.Equal(t, bare, withComment)
+ })
+
+ t.Run("predicate_narrows_the_estimate", func(t *testing.T) {
+ t.Parallel()
+
+ // Seeded ids are 1..5, so the predicate matches 3, 4 and 5.
+ rows, err := driver.CountAffectedRows(ctx, `UPDATE dbo.t SET c1 = c1 + 1 WHERE id > 2;`)
+ require.NoError(t, err)
+ require.EqualValues(t, 3, rows)
+ })
+
+ // The seam between the two halves: neither the tsql extractor tests nor the
+ // subtests above can catch a sample that the extractor emits but SHOWPLAN
+ // rejects, so feed the extractor's own output in rather than a literal.
+ t.Run("extractor_sample_is_accepted_by_showplan", func(t *testing.T) {
+ t.Parallel()
+
+ const bare = "UPDATE dbo.t SET c1 = c1 + 1 WHERE id > 2;"
+ const statement = "-- header comment\n" + bare
+
+ stmts, err := parserbase.ParseStatements(storepb.Engine_MSSQL, statement)
+ require.NoError(t, err)
+ summary, err := parserbase.ExtractChangedResources(
+ storepb.Engine_MSSQL, "resource_change_test", "dbo", nil, parserbase.ExtractASTs(stmts), statement)
+ require.NoError(t, err)
+ require.Len(t, summary.SampleDMLS, 1)
+
+ sampled, err := driver.CountAffectedRows(ctx, summary.SampleDMLS[0])
+ require.NoError(t, err, "SHOWPLAN must accept the sample the extractor emits")
+ want, err := driver.CountAffectedRows(ctx, bare)
+ require.NoError(t, err)
+ require.Equal(t, want, sampled)
+ })
+}
diff --git a/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go b/backend/plugin/db/mssql/sync_test.go
similarity index 84%
rename from backend/plugin/db/mssql/sync_spatial_testcontainer_test.go
rename to backend/plugin/db/mssql/sync_test.go
index b55e31ccbb..0e04128e5d 100644
--- a/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go
+++ b/backend/plugin/db/mssql/sync_test.go
@@ -14,6 +14,85 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
+// TestSyncUniqueIndex covers which synced indexes carry Unique. SQL Server
+// splits the answer across two catalog columns: is_unique_constraint for a
+// UNIQUE constraint and is_unique for a standalone CREATE UNIQUE INDEX. Sync
+// reads them in separate queries, and reading only the first reported every
+// CREATE UNIQUE INDEX as non-unique, which made the omni parser and sync
+// disagree and left every declared unique index looking like a pending change.
+func TestSyncUniqueIndex(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+ container := testcontainer.SharedMSSQLContainer(t)
+
+ driver := newSyncTestDatabase(ctx, t, container)
+ executeBatches(ctx, t, driver, `
+CREATE TABLE dbo.index_kinds (
+ id INT IDENTITY(1,1) PRIMARY KEY,
+ code VARCHAR(50) UNIQUE,
+ name NVARCHAR(100) NOT NULL,
+ status INT,
+ tag NVARCHAR(50)
+);
+GO
+
+CREATE UNIQUE INDEX idx_unique_name ON dbo.index_kinds(name);
+CREATE UNIQUE INDEX idx_unique_filtered ON dbo.index_kinds(status) WHERE status IS NOT NULL;
+CREATE INDEX idx_plain_tag ON dbo.index_kinds(tag);
+CREATE UNIQUE INDEX idx_unique_desc ON dbo.index_kinds(name DESC, status ASC);
+GO
+`)
+
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ table := requireTable(t, metadata, "dbo", "index_kinds")
+ byName := make(map[string]*storepb.IndexMetadata)
+ for _, index := range table.Indexes {
+ byName[index.Name] = index
+ }
+
+ // A standalone CREATE UNIQUE INDEX is unique but is not a constraint.
+ for _, name := range []string{"idx_unique_name", "idx_unique_filtered", "idx_unique_desc"} {
+ index := byName[name]
+ require.NotNilf(t, index, "index %s not synced", name)
+ require.Truef(t, index.Unique, "%s was created UNIQUE", name)
+ require.Falsef(t, index.Primary, "%s is not a primary key", name)
+ require.Falsef(t, index.IsConstraint, "%s is an index, not a constraint", name)
+ }
+
+ plain := byName["idx_plain_tag"]
+ require.NotNil(t, plain)
+ require.False(t, plain.Unique, "a plain CREATE INDEX must not be reported unique")
+
+ // The UNIQUE column constraint keeps arriving through the constraint query,
+ // which is the only path that sets IsConstraint.
+ var columnConstraint *storepb.IndexMetadata
+ for name, index := range byName {
+ if index.IsConstraint && !index.Primary {
+ require.Nilf(t, columnConstraint, "expected one unique constraint, also got %s", name)
+ columnConstraint = index
+ }
+ }
+ require.NotNil(t, columnConstraint, "the UNIQUE constraint on code should be synced")
+ require.True(t, columnConstraint.Unique)
+ require.Equal(t, []string{"code"}, columnConstraint.Expressions)
+
+ var primary *storepb.IndexMetadata
+ for _, index := range byName {
+ if index.Primary {
+ primary = index
+ }
+ }
+ require.NotNil(t, primary)
+ require.True(t, primary.Unique, "a primary key is unique")
+ require.Equal(t, []string{"id"}, primary.Expressions)
+
+ // Column order and direction must survive alongside the unique flag.
+ require.Equal(t, []string{"name", "status"}, byName["idx_unique_desc"].Expressions)
+ require.Equal(t, []bool{true, false}, byName["idx_unique_desc"].Descending)
+}
+
// TestSyncSpatialIndex pins what SyncDBSchema reports for SQL Server spatial
// indexes: tessellation scheme and grid levels, bounding box, storage options
// and the dimensional descriptor. sys.spatial_indexes exposes these through
diff --git a/backend/plugin/db/mssql/sync_unique_index_testcontainer_test.go b/backend/plugin/db/mssql/sync_unique_index_testcontainer_test.go
deleted file mode 100644
index 736962faea..0000000000
--- a/backend/plugin/db/mssql/sync_unique_index_testcontainer_test.go
+++ /dev/null
@@ -1,91 +0,0 @@
-package mssql
-
-import (
- "context"
- "testing"
-
- _ "github.com/microsoft/go-mssqldb"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-)
-
-// TestSyncUniqueIndex covers which synced indexes carry Unique. SQL Server
-// splits the answer across two catalog columns: is_unique_constraint for a
-// UNIQUE constraint and is_unique for a standalone CREATE UNIQUE INDEX. Sync
-// reads them in separate queries, and reading only the first reported every
-// CREATE UNIQUE INDEX as non-unique, which made the omni parser and sync
-// disagree and left every declared unique index looking like a pending change.
-func TestSyncUniqueIndex(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
- container := testcontainer.SharedMSSQLContainer(t)
-
- driver := newSyncTestDatabase(ctx, t, container)
- executeBatches(ctx, t, driver, `
-CREATE TABLE dbo.index_kinds (
- id INT IDENTITY(1,1) PRIMARY KEY,
- code VARCHAR(50) UNIQUE,
- name NVARCHAR(100) NOT NULL,
- status INT,
- tag NVARCHAR(50)
-);
-GO
-
-CREATE UNIQUE INDEX idx_unique_name ON dbo.index_kinds(name);
-CREATE UNIQUE INDEX idx_unique_filtered ON dbo.index_kinds(status) WHERE status IS NOT NULL;
-CREATE INDEX idx_plain_tag ON dbo.index_kinds(tag);
-CREATE UNIQUE INDEX idx_unique_desc ON dbo.index_kinds(name DESC, status ASC);
-GO
-`)
-
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- table := requireTable(t, metadata, "dbo", "index_kinds")
- byName := make(map[string]*storepb.IndexMetadata)
- for _, index := range table.Indexes {
- byName[index.Name] = index
- }
-
- // A standalone CREATE UNIQUE INDEX is unique but is not a constraint.
- for _, name := range []string{"idx_unique_name", "idx_unique_filtered", "idx_unique_desc"} {
- index := byName[name]
- require.NotNilf(t, index, "index %s not synced", name)
- require.Truef(t, index.Unique, "%s was created UNIQUE", name)
- require.Falsef(t, index.Primary, "%s is not a primary key", name)
- require.Falsef(t, index.IsConstraint, "%s is an index, not a constraint", name)
- }
-
- plain := byName["idx_plain_tag"]
- require.NotNil(t, plain)
- require.False(t, plain.Unique, "a plain CREATE INDEX must not be reported unique")
-
- // The UNIQUE column constraint keeps arriving through the constraint query,
- // which is the only path that sets IsConstraint.
- var columnConstraint *storepb.IndexMetadata
- for name, index := range byName {
- if index.IsConstraint && !index.Primary {
- require.Nilf(t, columnConstraint, "expected one unique constraint, also got %s", name)
- columnConstraint = index
- }
- }
- require.NotNil(t, columnConstraint, "the UNIQUE constraint on code should be synced")
- require.True(t, columnConstraint.Unique)
- require.Equal(t, []string{"code"}, columnConstraint.Expressions)
-
- var primary *storepb.IndexMetadata
- for _, index := range byName {
- if index.Primary {
- primary = index
- }
- }
- require.NotNil(t, primary)
- require.True(t, primary.Unique, "a primary key is unique")
- require.Equal(t, []string{"id"}, primary.Expressions)
-
- // Column order and direction must survive alongside the unique flag.
- require.Equal(t, []string{"name", "status"}, byName["idx_unique_desc"].Expressions)
- require.Equal(t, []bool{true, false}, byName["idx_unique_desc"].Descending)
-}
diff --git a/backend/plugin/db/mssql/sync_testutil_test.go b/backend/plugin/db/mssql/testutil_test.go
similarity index 81%
rename from backend/plugin/db/mssql/sync_testutil_test.go
rename to backend/plugin/db/mssql/testutil_test.go
index 84cf7b6426..dd120b2d6d 100644
--- a/backend/plugin/db/mssql/sync_testutil_test.go
+++ b/backend/plugin/db/mssql/testutil_test.go
@@ -14,6 +14,27 @@ import (
"github.com/bytebase/bytebase/backend/plugin/db"
)
+func openMSSQL(ctx context.Context, t *testing.T, host, port, database string) db.Driver {
+ t.Helper()
+ driverInstance := &Driver{}
+ cfg := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "sa",
+ Host: host,
+ Port: port,
+ Database: database,
+ },
+ Password: "Test123!",
+ ConnectionContext: db.ConnectionContext{
+ DatabaseName: database,
+ },
+ }
+ driver, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, cfg)
+ require.NoError(t, err)
+ return driver
+}
+
// newSyncTestDatabase gives the test a database of its own on the shared
// container so the cases can run in parallel, and returns a driver open on it.
func newSyncTestDatabase(ctx context.Context, t *testing.T, container *testcontainer.Container) *Driver {
diff --git a/backend/plugin/db/mysql/antlr_dependency_test.go b/backend/plugin/db/mysql/antlr_dependency_test.go
deleted file mode 100644
index 43aff0287b..0000000000
--- a/backend/plugin/db/mysql/antlr_dependency_test.go
+++ /dev/null
@@ -1,29 +0,0 @@
-package mysql
-
-import (
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestMySQLQueryLimitRewriteDoesNotFallbackToANTLR(t *testing.T) {
- content, err := os.ReadFile("query.go")
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")
- require.NotContains(t, source, "github.com/bytebase/parser/mysql")
- require.NotContains(t, source, "getStatementWithResultLimitInlineLegacy")
- require.NotContains(t, source, "ParseMySQL(")
- require.NotContains(t, source, "TokenStreamRewriter")
- require.NotContains(t, source, "BaseMySQLParserListener")
-}
-
-func TestMySQLExecuteDoesNotPreprocessDelimiterDirectives(t *testing.T) {
- content, err := os.ReadFile("mysql.go")
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "DealWithDelimiter")
-}
diff --git a/backend/plugin/db/oracle/sync_test.go b/backend/plugin/db/oracle/sync_test.go
new file mode 100644
index 0000000000..a1824e9403
--- /dev/null
+++ b/backend/plugin/db/oracle/sync_test.go
@@ -0,0 +1,154 @@
+package oracle
+
+import (
+ "bytes"
+ "log/slog"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/encoding/protojson"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
+
+// corruptedVietnamese simulates Vietnamese text encoded in Windows-1258
+// that was inserted into an AL32UTF8 Oracle database via a misconfigured client.
+// 0xe1 is 'á' in Win-1258 but an invalid UTF-8 lead byte without proper continuation.
+var corruptedVietnamese = "Tr\xe1ng th\xe1i"
+
+func TestOracleDefinitionSanitizedUTF8MarshalSuccess(t *testing.T) {
+ var logBuf bytes.Buffer
+ originalLogger := slog.Default()
+ slog.SetDefault(slog.New(slog.NewTextHandler(&logBuf, &slog.HandlerOptions{Level: slog.LevelWarn})))
+ defer slog.SetDefault(originalLogger)
+ corruptedDefinition := corruptedVietnamese + "\nSELECT 1 FROM DUAL"
+
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "TESTDB",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "TEST_SCHEMA",
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "V_CUSTOMER",
+ Definition: sanitizeOracleDefinition(
+ "TEST_SCHEMA",
+ "VIEW",
+ "V_CUSTOMER",
+ corruptedDefinition,
+ ),
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "MV_CUSTOMER",
+ Definition: sanitizeOracleDefinition(
+ "TEST_SCHEMA",
+ "MATERIALIZED VIEW",
+ "MV_CUSTOMER",
+ corruptedDefinition,
+ ),
+ },
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "FN_VALIDATE",
+ Definition: sanitizeOracleDefinition(
+ "TEST_SCHEMA",
+ "FUNCTION",
+ "FN_VALIDATE",
+ corruptedDefinition,
+ ),
+ },
+ },
+ Procedures: []*storepb.ProcedureMetadata{
+ {
+ Name: "PR_VALIDATE",
+ Definition: sanitizeOracleDefinition(
+ "TEST_SCHEMA",
+ "PROCEDURE",
+ "PR_VALIDATE",
+ corruptedDefinition,
+ ),
+ },
+ },
+ Packages: []*storepb.PackageMetadata{
+ {
+ Name: "PKG_VALIDATE",
+ Definition: sanitizeOracleDefinition(
+ "TEST_SCHEMA",
+ "PACKAGE",
+ "PKG_VALIDATE",
+ corruptedDefinition,
+ ),
+ },
+ },
+ },
+ },
+ }
+
+ _, err := protojson.Marshal(metadata)
+ require.NoError(t, err, "marshal must succeed after definition sanitization")
+ require.Contains(t, metadata.Schemas[0].Views[0].Definition, "\\xe1")
+ require.Contains(t, metadata.Schemas[0].Views[0].Definition, "\nSELECT 1 FROM DUAL")
+
+ logText := logBuf.String()
+ require.Contains(t, logText, "sanitized invalid UTF-8 in Oracle metadata")
+ require.Contains(t, logText, "schema=TEST_SCHEMA")
+ require.Contains(t, logText, "object_type=VIEW")
+ require.Contains(t, logText, "object_name=V_CUSTOMER")
+ require.Contains(t, logText, "field=definition")
+}
+
+func TestOracleTriggerBodySanitizedUTF8MarshalSuccess(t *testing.T) {
+ var logBuf bytes.Buffer
+ originalLogger := slog.Default()
+ slog.SetDefault(slog.New(slog.NewTextHandler(&logBuf, &slog.HandlerOptions{Level: slog.LevelWarn})))
+ defer slog.SetDefault(originalLogger)
+
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "TESTDB",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "TEST_SCHEMA",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "CUSTOMER",
+ Triggers: []*storepb.TriggerMetadata{
+ {
+ Name: "TRG_CUSTOMER",
+ Body: sanitizeOracleMetadataString(
+ "TEST_SCHEMA",
+ "TRIGGER",
+ "TRG_CUSTOMER",
+ "body",
+ constructTriggerBody(
+ "TRG_CUSTOMER BEFORE INSERT ON CUSTOMER\n",
+ corruptedVietnamese+"\nBEGIN NULL; END;",
+ ),
+ ),
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ _, err := protojson.Marshal(metadata)
+ require.NoError(t, err, "marshal must succeed after trigger body sanitization")
+ require.Contains(t, metadata.Schemas[0].Tables[0].Triggers[0].Body, "\\xe1")
+ require.Contains(t, metadata.Schemas[0].Tables[0].Triggers[0].Body, "\nBEGIN NULL; END;")
+
+ logText := logBuf.String()
+ require.Contains(t, logText, "sanitized invalid UTF-8 in Oracle metadata")
+ require.Contains(t, logText, "schema=TEST_SCHEMA")
+ require.Contains(t, logText, "object_type=TRIGGER")
+ require.Contains(t, logText, "object_name=TRG_CUSTOMER")
+ require.Contains(t, logText, "field=body")
+}
+
+// fillAllStringFields recursively sets every string field reachable from m
+// (including nested messages, repeated fields, and map keys/values) to the
+// given value, instantiating one element for each nested message, list, and
+// map so that no string field in the schema shape stays unvisited.
diff --git a/backend/plugin/db/oracle/sync_utf8_test.go b/backend/plugin/db/oracle/sync_utf8_test.go
deleted file mode 100644
index 2d43b360cb..0000000000
--- a/backend/plugin/db/oracle/sync_utf8_test.go
+++ /dev/null
@@ -1,459 +0,0 @@
-package oracle
-
-import (
- "bytes"
- "log/slog"
- "testing"
- "unicode/utf8"
-
- "github.com/stretchr/testify/require"
- "google.golang.org/protobuf/encoding/protojson"
- "google.golang.org/protobuf/proto"
- "google.golang.org/protobuf/reflect/protoreflect"
-
- "github.com/bytebase/bytebase/backend/common"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-)
-
-// corruptedVietnamese simulates Vietnamese text encoded in Windows-1258
-// that was inserted into an AL32UTF8 Oracle database via a misconfigured client.
-// 0xe1 is 'á' in Win-1258 but an invalid UTF-8 lead byte without proper continuation.
-var corruptedVietnamese = "Tr\xe1ng th\xe1i"
-
-func TestOracleCommentInvalidUTF8MarshalFailure(t *testing.T) {
- require.False(t, utf8.ValidString(corruptedVietnamese),
- "test precondition: input must be invalid UTF-8")
-
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "TESTDB",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "TEST_SCHEMA",
- Tables: []*storepb.TableMetadata{
- {
- Name: "CUSTOMER",
- Comment: corruptedVietnamese,
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "STATUS",
- Comment: corruptedVietnamese,
- },
- },
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "TRG_AUDIT",
- Comment: corruptedVietnamese,
- },
- },
- },
- },
- Views: []*storepb.ViewMetadata{
- {
- Name: "V_CUSTOMER",
- Comment: corruptedVietnamese,
- },
- },
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "MV_CUSTOMER",
- Comment: corruptedVietnamese,
- },
- },
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "SEQ_CUSTOMER",
- Comment: corruptedVietnamese,
- },
- },
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "FN_VALIDATE",
- Comment: corruptedVietnamese,
- },
- },
- },
- },
- }
-
- // Prove this is the exact failure the customer sees.
- _, err := protojson.Marshal(metadata)
- require.Error(t, err)
- require.Contains(t, err.Error(), "invalid UTF-8")
-}
-
-func TestOracleCommentSanitizedUTF8MarshalSuccess(t *testing.T) {
- sanitized := common.SanitizeUTF8String(corruptedVietnamese)
-
- require.True(t, utf8.ValidString(sanitized),
- "sanitized string must be valid UTF-8")
- require.Contains(t, sanitized, "\\xe1",
- "sanitized string should preserve original bytes as hex")
-
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "TESTDB",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "TEST_SCHEMA",
- Tables: []*storepb.TableMetadata{
- {
- Name: "CUSTOMER",
- Comment: sanitized,
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "STATUS",
- Comment: sanitized,
- },
- },
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "TRG_AUDIT",
- Comment: sanitized,
- },
- },
- },
- },
- Views: []*storepb.ViewMetadata{
- {
- Name: "V_CUSTOMER",
- Comment: sanitized,
- },
- },
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "MV_CUSTOMER",
- Comment: sanitized,
- },
- },
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "SEQ_CUSTOMER",
- Comment: sanitized,
- },
- },
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "FN_VALIDATE",
- Comment: sanitized,
- },
- },
- },
- },
- }
-
- _, err := protojson.Marshal(metadata)
- require.NoError(t, err, "marshal must succeed after sanitization")
-}
-
-func TestOracleDefinitionInvalidUTF8MarshalFailure(t *testing.T) {
- require.False(t, utf8.ValidString(corruptedVietnamese),
- "test precondition: input must be invalid UTF-8")
-
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "TESTDB",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "TEST_SCHEMA",
- Views: []*storepb.ViewMetadata{
- {
- Name: "V_CUSTOMER",
- Definition: corruptedVietnamese,
- },
- },
- },
- },
- }
-
- _, err := protojson.Marshal(metadata)
- require.Error(t, err)
- require.Contains(t, err.Error(), "ViewMetadata.definition")
- require.Contains(t, err.Error(), "invalid UTF-8")
-}
-
-func TestOracleDefinitionSanitizedUTF8MarshalSuccess(t *testing.T) {
- var logBuf bytes.Buffer
- originalLogger := slog.Default()
- slog.SetDefault(slog.New(slog.NewTextHandler(&logBuf, &slog.HandlerOptions{Level: slog.LevelWarn})))
- defer slog.SetDefault(originalLogger)
- corruptedDefinition := corruptedVietnamese + "\nSELECT 1 FROM DUAL"
-
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "TESTDB",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "TEST_SCHEMA",
- Views: []*storepb.ViewMetadata{
- {
- Name: "V_CUSTOMER",
- Definition: sanitizeOracleDefinition(
- "TEST_SCHEMA",
- "VIEW",
- "V_CUSTOMER",
- corruptedDefinition,
- ),
- },
- },
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "MV_CUSTOMER",
- Definition: sanitizeOracleDefinition(
- "TEST_SCHEMA",
- "MATERIALIZED VIEW",
- "MV_CUSTOMER",
- corruptedDefinition,
- ),
- },
- },
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "FN_VALIDATE",
- Definition: sanitizeOracleDefinition(
- "TEST_SCHEMA",
- "FUNCTION",
- "FN_VALIDATE",
- corruptedDefinition,
- ),
- },
- },
- Procedures: []*storepb.ProcedureMetadata{
- {
- Name: "PR_VALIDATE",
- Definition: sanitizeOracleDefinition(
- "TEST_SCHEMA",
- "PROCEDURE",
- "PR_VALIDATE",
- corruptedDefinition,
- ),
- },
- },
- Packages: []*storepb.PackageMetadata{
- {
- Name: "PKG_VALIDATE",
- Definition: sanitizeOracleDefinition(
- "TEST_SCHEMA",
- "PACKAGE",
- "PKG_VALIDATE",
- corruptedDefinition,
- ),
- },
- },
- },
- },
- }
-
- _, err := protojson.Marshal(metadata)
- require.NoError(t, err, "marshal must succeed after definition sanitization")
- require.Contains(t, metadata.Schemas[0].Views[0].Definition, "\\xe1")
- require.Contains(t, metadata.Schemas[0].Views[0].Definition, "\nSELECT 1 FROM DUAL")
-
- logText := logBuf.String()
- require.Contains(t, logText, "sanitized invalid UTF-8 in Oracle metadata")
- require.Contains(t, logText, "schema=TEST_SCHEMA")
- require.Contains(t, logText, "object_type=VIEW")
- require.Contains(t, logText, "object_name=V_CUSTOMER")
- require.Contains(t, logText, "field=definition")
-}
-
-func TestOracleTriggerBodySanitizedUTF8MarshalSuccess(t *testing.T) {
- var logBuf bytes.Buffer
- originalLogger := slog.Default()
- slog.SetDefault(slog.New(slog.NewTextHandler(&logBuf, &slog.HandlerOptions{Level: slog.LevelWarn})))
- defer slog.SetDefault(originalLogger)
-
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "TESTDB",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "TEST_SCHEMA",
- Tables: []*storepb.TableMetadata{
- {
- Name: "CUSTOMER",
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "TRG_CUSTOMER",
- Body: sanitizeOracleMetadataString(
- "TEST_SCHEMA",
- "TRIGGER",
- "TRG_CUSTOMER",
- "body",
- constructTriggerBody(
- "TRG_CUSTOMER BEFORE INSERT ON CUSTOMER\n",
- corruptedVietnamese+"\nBEGIN NULL; END;",
- ),
- ),
- },
- },
- },
- },
- },
- },
- }
-
- _, err := protojson.Marshal(metadata)
- require.NoError(t, err, "marshal must succeed after trigger body sanitization")
- require.Contains(t, metadata.Schemas[0].Tables[0].Triggers[0].Body, "\\xe1")
- require.Contains(t, metadata.Schemas[0].Tables[0].Triggers[0].Body, "\nBEGIN NULL; END;")
-
- logText := logBuf.String()
- require.Contains(t, logText, "sanitized invalid UTF-8 in Oracle metadata")
- require.Contains(t, logText, "schema=TEST_SCHEMA")
- require.Contains(t, logText, "object_type=TRIGGER")
- require.Contains(t, logText, "object_name=TRG_CUSTOMER")
- require.Contains(t, logText, "field=body")
-}
-
-// fillAllStringFields recursively sets every string field reachable from m
-// (including nested messages, repeated fields, and map keys/values) to the
-// given value, instantiating one element for each nested message, list, and
-// map so that no string field in the schema shape stays unvisited.
-func fillAllStringFields(m protoreflect.Message, value string, depth int) {
- if depth <= 0 {
- return
- }
- fields := m.Descriptor().Fields()
- for i := 0; i < fields.Len(); i++ {
- fd := fields.Get(i)
- switch {
- case fd.IsMap():
- mp := m.Mutable(fd).Map()
- var key protoreflect.MapKey
- if fd.MapKey().Kind() == protoreflect.StringKind {
- key = protoreflect.ValueOfString(value).MapKey()
- } else {
- key = protoreflect.ValueOfInt64(1).MapKey()
- }
- switch fd.MapValue().Kind() {
- case protoreflect.StringKind:
- mp.Set(key, protoreflect.ValueOfString(value))
- case protoreflect.MessageKind:
- nv := mp.NewValue()
- fillAllStringFields(nv.Message(), value, depth-1)
- mp.Set(key, nv)
- default:
- mp.Set(key, mp.NewValue())
- }
- case fd.IsList():
- list := m.Mutable(fd).List()
- switch fd.Kind() {
- case protoreflect.StringKind:
- list.Append(protoreflect.ValueOfString(value))
- case protoreflect.MessageKind:
- nv := list.NewElement()
- fillAllStringFields(nv.Message(), value, depth-1)
- list.Append(nv)
- default:
- }
- case fd.Kind() == protoreflect.StringKind:
- m.Set(fd, protoreflect.ValueOfString(value))
- case fd.Kind() == protoreflect.MessageKind:
- fillAllStringFields(m.Mutable(fd).Message(), value, depth-1)
- default:
- }
- }
-}
-
-// countInvalidStringFields walks m and returns how many string values
-// (fields, list elements, map keys and values) hold invalid UTF-8.
-func countInvalidStringFields(m protoreflect.Message) int {
- count := 0
- m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
- switch {
- case fd.IsMap():
- v.Map().Range(func(k protoreflect.MapKey, mv protoreflect.Value) bool {
- if fd.MapKey().Kind() == protoreflect.StringKind && !utf8.ValidString(k.String()) {
- count++
- }
- switch fd.MapValue().Kind() {
- case protoreflect.StringKind:
- if !utf8.ValidString(mv.String()) {
- count++
- }
- case protoreflect.MessageKind:
- count += countInvalidStringFields(mv.Message())
- default:
- }
- return true
- })
- case fd.IsList():
- list := v.List()
- for i := 0; i < list.Len(); i++ {
- switch fd.Kind() {
- case protoreflect.StringKind:
- if !utf8.ValidString(list.Get(i).String()) {
- count++
- }
- case protoreflect.MessageKind:
- count += countInvalidStringFields(list.Get(i).Message())
- default:
- }
- }
- case fd.Kind() == protoreflect.StringKind:
- if !utf8.ValidString(v.String()) {
- count++
- }
- case fd.Kind() == protoreflect.MessageKind:
- count += countInvalidStringFields(v.Message())
- default:
- }
- return true
- })
- return count
-}
-
-// TestSanitizeUTF8MessageCoversEveryStringField proves the BYT-9916 fix
-// covers every string field of the database metadata — names, types,
-// defaults, definitions — not just comments: fill each one with the raw-GBK
-// shape go-ora leaks for values ending in a dangling lead byte, then assert
-// sanitization leaves zero invalid strings and proto marshaling succeeds.
-func TestSanitizeUTF8MessageCoversEveryStringField(t *testing.T) {
- // 测试 + dangling lead byte 0xb1: the exact wholly-unconverted raw GBK
- // shape go-ora returns (engine-verified against Oracle 11gR2/ZHS16GBK).
- rawGBK := "\xb2\xe2\xca\xd4\xb1"
- require.False(t, utf8.ValidString(rawGBK))
-
- metadata := &storepb.DatabaseSchemaMetadata{}
- fillAllStringFields(metadata.ProtoReflect(), rawGBK, 8)
-
- invalidBefore := countInvalidStringFields(metadata.ProtoReflect())
- require.Greater(t, invalidBefore, 100,
- "filler must reach the full schema shape; got only %d string fields", invalidBefore)
- _, err := proto.Marshal(metadata)
- require.Error(t, err, "pre-sanitize marshal must fail — otherwise this test proves nothing")
-
- common.SanitizeUTF8Message(metadata)
-
- require.Equal(t, 0, countInvalidStringFields(metadata.ProtoReflect()),
- "sanitization must leave zero invalid string values anywhere in the message")
- _, err = proto.Marshal(metadata)
- require.NoError(t, err)
- _, err = protojson.Marshal(metadata)
- require.NoError(t, err)
-}
-
-// TestSanitizeUTF8MessageNameFields pins the customer-visible BYT-9916 shape:
-// object names (schema/table/column) carrying raw GBK bytes must marshal
-// after sanitization, and valid names must pass through untouched.
-func TestSanitizeUTF8MessageNameFields(t *testing.T) {
- rawGBK := "AB\xe6"
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: rawGBK,
- Schemas: []*storepb.SchemaMetadata{{
- Name: rawGBK,
- Tables: []*storepb.TableMetadata{{
- Name: rawGBK,
- Columns: []*storepb.ColumnMetadata{
- {Name: rawGBK, Type: rawGBK, Default: rawGBK},
- {Name: "测试列", Type: "VARCHAR2(50)"},
- },
- }},
- }},
- }
- _, err := proto.Marshal(metadata)
- require.Error(t, err)
-
- common.SanitizeUTF8Message(metadata)
- _, err = proto.Marshal(metadata)
- require.NoError(t, err)
- require.Equal(t, "测试列", metadata.Schemas[0].Tables[0].Columns[1].Name,
- "valid UTF-8 must pass through unchanged")
- require.True(t, utf8.ValidString(metadata.Schemas[0].Tables[0].Name))
-}
diff --git a/backend/plugin/db/pg/pg_test.go b/backend/plugin/db/pg/pg_test.go
index 7f9735c9f6..743fa42429 100644
--- a/backend/plugin/db/pg/pg_test.go
+++ b/backend/plugin/db/pg/pg_test.go
@@ -1,19 +1,23 @@
package pg
import (
+ "context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
+ "strings"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -240,3 +244,185 @@ func generateCAAndServerCertificates(t *testing.T, hosts []string) (string, map[
caPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: caDER})
return string(caPEM), serverCertDERByHost
}
+
+func TestQueryConnSearchPathIncludesPublicAfterSelectedSchema(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.SharedPgContainer(t)
+ dbName, rawDB := testcontainer.NewPgDatabase(t)
+ _, err := rawDB.ExecContext(ctx, `
+ CREATE SCHEMA app;
+ CREATE TABLE public.lookup_precedence (marker text);
+ CREATE TABLE app.lookup_precedence (marker text);
+ INSERT INTO public.lookup_precedence VALUES ('public');
+ INSERT INTO app.lookup_precedence VALUES ('app');
+ CREATE FUNCTION public.polar_osfs_extract_table_name(rel text) RETURNS text
+ LANGUAGE sql
+ AS $$ SELECT rel $$;
+ CREATE FUNCTION public.polar_alter_relation_to_oss_with_indexes(rel text) RETURNS text
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ RETURN polar_osfs_extract_table_name(rel);
+ END;
+ $$;
+ `)
+ require.NoError(t, err)
+
+ driver, err := (&Driver{}).Open(ctx, storepb.Engine_POSTGRES, db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Username: "postgres",
+ },
+ Password: "root-password",
+ ConnectionContext: db.ConnectionContext{DatabaseName: dbName},
+ })
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ conn, err := driver.GetDB().Conn(ctx)
+ require.NoError(t, err)
+ defer conn.Close()
+
+ queryContext := db.QueryContext{
+ Schema: "app",
+ Limit: 5000,
+ MaximumSQLResultSize: 1 << 30,
+ }
+ results, err := driver.QueryConn(ctx, conn, `SELECT public.polar_alter_relation_to_oss_with_indexes('content_messages_2025q1'::text);`, queryContext)
+ require.NoError(t, err)
+ require.Equal(t, "content_messages_2025q1", firstStringValue(t, results))
+
+ results, err = driver.QueryConn(ctx, conn, `SELECT marker FROM lookup_precedence;`, queryContext)
+ require.NoError(t, err)
+ require.Equal(t, "app", firstStringValue(t, results))
+}
+
+func TestQueryConnSearchPathEscapesSelectedSchemaName(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.SharedPgContainer(t)
+ dbName, rawDB := testcontainer.NewPgDatabase(t)
+ _, err := rawDB.ExecContext(ctx, `
+ CREATE SCHEMA "app""schema";
+ CREATE TABLE "app""schema".lookup_precedence (marker text);
+ INSERT INTO "app""schema".lookup_precedence VALUES ('quoted');
+ `)
+ require.NoError(t, err)
+
+ driver, err := (&Driver{}).Open(ctx, storepb.Engine_POSTGRES, db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Username: "postgres",
+ },
+ Password: "root-password",
+ ConnectionContext: db.ConnectionContext{DatabaseName: dbName},
+ })
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ conn, err := driver.GetDB().Conn(ctx)
+ require.NoError(t, err)
+ defer conn.Close()
+
+ results, err := driver.QueryConn(ctx, conn, `SELECT marker FROM lookup_precedence;`, db.QueryContext{
+ Schema: `app"schema`,
+ Limit: 5000,
+ MaximumSQLResultSize: 1 << 30,
+ })
+ require.NoError(t, err)
+ require.Equal(t, "quoted", firstStringValue(t, results))
+}
+
+func firstStringValue(t *testing.T, results []*v1pb.QueryResult) string {
+ t.Helper()
+ require.NotEmpty(t, results)
+ require.Empty(t, results[0].GetError())
+ require.NotEmpty(t, results[0].GetRows())
+ require.NotEmpty(t, results[0].GetRows()[0].GetValues())
+ return results[0].GetRows()[0].GetValues()[0].GetStringValue()
+}
+
+// TestQueryConnMergeCTEDoesNotLeakRows is a security regression test for the
+// PostgreSQL data-masking bypass where a MERGE smuggled into a data-modifying CTE
+//
+// WITH x AS (MERGE ... RETURNING masked_col) SELECT masked_col FROM x
+//
+// was misclassified as a read-only query (hasDMLInTree did not recognise MergeStmt
+// as a write). That routed it through QueryConn's row-returning QueryContext path,
+// so the RETURNING columns came back to the client and — because the query-span
+// extractor cannot trace lineage through a MERGE — were returned UNMASKED.
+//
+// The fix classifies MERGE as a write, so the editor routes the statement to
+// ExecContext, which discards the RETURNING rows. This test asserts the
+// security-relevant outcome at the driver boundary: the MERGE-CTE must NOT return
+// the secret value as data rows. MERGE ... RETURNING requires PostgreSQL 17.
+func TestQueryConnMergeCTEDoesNotLeakRows(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.GetTestPg17Container(ctx, t)
+ defer pgContainer.Close(ctx)
+
+ const secret = "TOP-SECRET-SSN-123-45-6789"
+ rawDB := pgContainer.GetDB()
+ require.NoError(t, rawDB.Ping())
+ _, err := rawDB.ExecContext(ctx, `CREATE TABLE secret_doc (id int PRIMARY KEY, secret text);`)
+ require.NoError(t, err)
+ _, err = rawDB.ExecContext(ctx, `INSERT INTO secret_doc VALUES (1, '`+secret+`');`)
+ require.NoError(t, err)
+
+ driver, err := (&Driver{}).Open(ctx, storepb.Engine_POSTGRES, db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Username: "postgres",
+ },
+ Password: "root-password",
+ ConnectionContext: db.ConnectionContext{DatabaseName: "postgres"},
+ })
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ conn, err := driver.GetDB().Conn(ctx)
+ require.NoError(t, err)
+ defer conn.Close()
+
+ // Baseline: a plain SELECT returns the secret as a data row. This proves the
+ // harness actually surfaces row data, so the negative assertion below is meaningful.
+ selectResults, err := driver.QueryConn(ctx, conn, `SELECT secret FROM secret_doc`, db.QueryContext{Limit: 5000, MaximumSQLResultSize: 1 << 30})
+ require.NoError(t, err)
+ require.True(t, resultsContainValue(selectResults, secret),
+ "baseline plain SELECT should return the secret as a data row")
+
+ // Exploit attempt: a MERGE smuggled into a data-modifying CTE. After the fix it
+ // is classified as a write and routed to Exec, so the RETURNING rows are dropped
+ // and the secret is never returned as data.
+ const mergeCTE = `WITH x AS (
+ MERGE INTO secret_doc t USING secret_doc s ON t.id = s.id
+ WHEN MATCHED THEN UPDATE SET id = t.id
+ RETURNING t.secret AS secret
+ ) SELECT secret FROM x`
+ mergeResults, err := driver.QueryConn(ctx, conn, mergeCTE, db.QueryContext{Limit: 5000, MaximumSQLResultSize: 1 << 30})
+ require.NoError(t, err)
+ require.False(t, resultsContainValue(mergeResults, secret),
+ "SECURITY: MERGE-in-CTE must not return the masked column as data rows; got %v", mergeResults)
+}
+
+// resultsContainValue reports whether any string cell in the results contains want.
+func resultsContainValue(results []*v1pb.QueryResult, want string) bool {
+ for _, r := range results {
+ for _, row := range r.GetRows() {
+ for _, v := range row.GetValues() {
+ if sv, ok := v.GetKind().(*v1pb.RowValue_StringValue); ok && strings.Contains(sv.StringValue, want) {
+ return true
+ }
+ }
+ }
+ }
+ return false
+}
diff --git a/backend/plugin/db/pg/query_limit_test.go b/backend/plugin/db/pg/query_limit_test.go
deleted file mode 100644
index 704f75cfe6..0000000000
--- a/backend/plugin/db/pg/query_limit_test.go
+++ /dev/null
@@ -1,392 +0,0 @@
-package pg
-
-import (
- "fmt"
- "strings"
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-
- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
-)
-
-func TestGetStatementWithResultLimit(t *testing.T) {
- tests := []struct {
- name string
- statement string
- limit int
- want string
- }{
- {
- name: "Simple SELECT without LIMIT",
- statement: "SELECT * FROM users",
- limit: 10,
- want: "SELECT * FROM users LIMIT 10",
- },
- {
- name: "SELECT with WHERE clause",
- statement: "SELECT id, name FROM users WHERE active = true",
- limit: 5,
- want: "SELECT id, name FROM users WHERE active = true LIMIT 5",
- },
- {
- name: "SELECT with existing LIMIT higher than requested",
- statement: "SELECT * FROM users LIMIT 100",
- limit: 10,
- want: "SELECT * FROM users LIMIT 10",
- },
- {
- name: "SELECT with existing LIMIT lower than requested",
- statement: "SELECT * FROM users LIMIT 5",
- limit: 10,
- want: "SELECT * FROM users LIMIT 5",
- },
- {
- name: "WITH query (CTE)",
- statement: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users",
- limit: 10,
- want: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users LIMIT 10",
- },
- {
- name: "SELECT with ORDER BY",
- statement: "SELECT * FROM users ORDER BY created_at DESC",
- limit: 10,
- want: "SELECT * FROM users ORDER BY created_at DESC LIMIT 10",
- },
- {
- name: "SELECT with GROUP BY and HAVING",
- statement: "SELECT department, COUNT(*) FROM users GROUP BY department HAVING COUNT(*) > 5",
- limit: 10,
- want: "SELECT department, COUNT(*) FROM users GROUP BY department HAVING COUNT(*) > 5 LIMIT 10",
- },
- {
- name: "UNION query",
- statement: "SELECT id FROM users UNION SELECT id FROM admins",
- limit: 10,
- want: "SELECT id FROM users UNION SELECT id FROM admins LIMIT 10",
- },
- {
- name: "SELECT with OFFSET",
- statement: "SELECT * FROM users LIMIT 20 OFFSET 10",
- limit: 5,
- want: "SELECT * FROM users LIMIT 5 OFFSET 10",
- },
- {
- name: "SELECT with FOR UPDATE",
- statement: "SELECT * FROM users WHERE id = 1 FOR UPDATE",
- limit: 10,
- want: "SELECT * FROM users WHERE id = 1 LIMIT 10 FOR UPDATE",
- },
- {
- name: "Non-SELECT statement (INSERT)",
- statement: "INSERT INTO users (name) VALUES ('John')",
- limit: 10,
- want: "INSERT INTO users (name) VALUES ('John')",
- },
- {
- name: "Non-SELECT statement (UPDATE)",
- statement: "UPDATE users SET active = false WHERE id = 1",
- limit: 10,
- want: "UPDATE users SET active = false WHERE id = 1",
- },
- {
- name: "Non-SELECT statement (DELETE)",
- statement: "DELETE FROM users WHERE active = false",
- limit: 10,
- want: "DELETE FROM users WHERE active = false",
- },
- {
- name: "SELECT with comments",
- statement: "SELECT * FROM users -- This is a comment\nWHERE active = true",
- limit: 10,
- want: "SELECT * FROM users -- This is a comment\nWHERE active = true LIMIT 10",
- },
- {
- name: "Complex nested SELECT",
- statement: "SELECT * FROM (SELECT * FROM users WHERE active = true) AS active_users WHERE created_at > '2023-01-01'",
- limit: 10,
- want: "SELECT * FROM (SELECT * FROM users WHERE active = true) AS active_users WHERE created_at > '2023-01-01' LIMIT 10",
- },
- {
- name: "SELECT with JOIN",
- statement: "SELECT u.*, d.name as dept_name FROM users u JOIN departments d ON u.dept_id = d.id",
- limit: 10,
- want: "SELECT u.*, d.name as dept_name FROM users u JOIN departments d ON u.dept_id = d.id LIMIT 10",
- },
- {
- name: "SELECT with parentheses",
- statement: "(SELECT * FROM users)",
- limit: 10,
- want: "(SELECT * FROM users) LIMIT 10",
- },
- {
- name: "SELECT with nested parentheses",
- statement: "((SELECT * FROM users))",
- limit: 10,
- want: "((SELECT * FROM users)) LIMIT 10",
- },
- {
- name: "SELECT with parentheses and existing LIMIT higher than requested",
- statement: "(SELECT * FROM users LIMIT 100)",
- limit: 10,
- want: "(SELECT * FROM users LIMIT 10)",
- },
- {
- name: "SELECT with parentheses and existing LIMIT lower than requested",
- statement: "(SELECT * FROM users LIMIT 5)",
- limit: 10,
- want: "(SELECT * FROM users LIMIT 5)",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got := getStatementWithResultLimit(tt.statement, tt.limit)
-
- // For non-SELECT statements, the parser might fail and fall back to CTE approach
- // which we should avoid for non-SELECT statements
- if tt.statement == tt.want {
- // Non-SELECT statements should remain unchanged
- assert.Equal(t, tt.want, got)
- } else {
- // For SELECT statements, check if LIMIT was properly added
- // The exact format might vary slightly due to parser normalization
- assert.Contains(t, got, "LIMIT")
- assert.Contains(t, got, fmt.Sprintf("%d", tt.limit))
- }
- })
- }
-}
-
-func TestGetStatementWithResultLimitInline(t *testing.T) {
- tests := []struct {
- name string
- statement string
- limit int
- wantErr bool
- want string
- }{
- {
- name: "Valid SELECT statement",
- statement: "SELECT * FROM users",
- limit: 10,
- wantErr: false,
- want: "SELECT * FROM users LIMIT 10",
- },
- {
- name: "Invalid SQL syntax",
- statement: "SELECT * FROM WHERE",
- limit: 10,
- wantErr: true,
- },
- {
- name: "Empty statement",
- statement: "",
- limit: 10,
- wantErr: true,
- },
- {
- name: "FOR UPDATE gets proper whitespace",
- statement: "SELECT * FROM users WHERE id = 1 FOR UPDATE",
- limit: 10,
- want: "SELECT * FROM users WHERE id = 1 LIMIT 10 FOR UPDATE",
- },
- {
- name: "FOR SHARE gets proper whitespace",
- statement: "SELECT * FROM users FOR SHARE",
- limit: 5,
- want: "SELECT * FROM users LIMIT 5 FOR SHARE",
- },
- {
- name: "SELECT ending with function call",
- statement: "SELECT now()",
- limit: 10,
- want: "SELECT now() LIMIT 10",
- },
- {
- name: "SELECT with subquery in WHERE",
- statement: "SELECT * FROM users WHERE id IN (SELECT id FROM admins)",
- limit: 10,
- want: "SELECT * FROM users WHERE id IN (SELECT id FROM admins) LIMIT 10",
- },
- {
- name: "Non-constant LIMIT expression falls back to error",
- statement: "SELECT * FROM t LIMIT (1+2)",
- limit: 5,
- wantErr: true,
- },
- {
- name: "CTE with lateral and ORDER BY",
- statement: `WITH params AS (
- SELECT 'resource.environment_id in []'::text AS env_condition
-), affected AS (
- SELECT
- p.resource,
- binding,
- COALESCE(binding->'condition'->>'expression', '') AS old_expression
- FROM policy p,
- LATERAL jsonb_array_elements(p.payload->'bindings') AS binding
- WHERE p.type = 'IAM'
- AND p.resource_type = 'PROJECT'
- AND binding->>'role' = 'roles/projectOwner'
- AND COALESCE(binding->'condition'->>'expression', '') NOT LIKE '%resource.environment_id%'
-)
-SELECT
- resource,
- binding->'members' AS members,
- binding->'condition' AS old_condition,
- CASE
- WHEN old_expression = '' THEN params.env_condition
- ELSE '(' || old_expression || ') && ' || params.env_condition
- END AS new_expression
-FROM affected
-CROSS JOIN params
-ORDER BY resource`,
- limit: 1000,
- want: `WITH params AS (
- SELECT 'resource.environment_id in []'::text AS env_condition
-), affected AS (
- SELECT
- p.resource,
- binding,
- COALESCE(binding->'condition'->>'expression', '') AS old_expression
- FROM policy p,
- LATERAL jsonb_array_elements(p.payload->'bindings') AS binding
- WHERE p.type = 'IAM'
- AND p.resource_type = 'PROJECT'
- AND binding->>'role' = 'roles/projectOwner'
- AND COALESCE(binding->'condition'->>'expression', '') NOT LIKE '%resource.environment_id%'
-)
-SELECT
- resource,
- binding->'members' AS members,
- binding->'condition' AS old_condition,
- CASE
- WHEN old_expression = '' THEN params.env_condition
- ELSE '(' || old_expression || ') && ' || params.env_condition
- END AS new_expression
-FROM affected
-CROSS JOIN params
-ORDER BY resource LIMIT 1000`,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := getStatementWithResultLimitInline(tt.statement, tt.limit)
- if tt.wantErr {
- require.Error(t, err)
- } else {
- require.NoError(t, err)
- assert.NotEmpty(t, got)
- if tt.want != "" {
- assert.Equal(t, tt.want, got)
- }
- }
- })
- }
-}
-
-func TestGetStatementWithResultLimitInlineClauseOrder(t *testing.T) {
- tests := []struct {
- name string
- statement string
- limit int
- want string
- clausesInOrder []string
- }{
- {
- name: "ORDER BY",
- statement: "SELECT * FROM users ORDER BY created_at DESC",
- limit: 10,
- want: "SELECT * FROM users ORDER BY created_at DESC LIMIT 10",
- clausesInOrder: []string{"ORDER BY", "LIMIT 10"},
- },
- {
- name: "OFFSET without LIMIT",
- statement: "SELECT * FROM users OFFSET 20",
- limit: 10,
- clausesInOrder: []string{"OFFSET 20", "LIMIT 10"},
- },
- {
- name: "ORDER BY with OFFSET without LIMIT",
- statement: "SELECT * FROM users ORDER BY created_at DESC OFFSET 20",
- limit: 10,
- clausesInOrder: []string{"ORDER BY", "OFFSET 20", "LIMIT 10"},
- },
- {
- name: "ORDER BY with FOR UPDATE",
- statement: "SELECT * FROM users ORDER BY id FOR UPDATE",
- limit: 10,
- want: "SELECT * FROM users ORDER BY id LIMIT 10 FOR UPDATE",
- clausesInOrder: []string{"ORDER BY", "LIMIT 10", "FOR UPDATE"},
- },
- {
- name: "ORDER BY with OFFSET and FOR UPDATE",
- statement: "SELECT * FROM users ORDER BY id OFFSET 20 FOR UPDATE",
- limit: 10,
- clausesInOrder: []string{"ORDER BY", "OFFSET 20", "LIMIT 10", "FOR UPDATE"},
- },
- {
- name: "GROUP BY HAVING WINDOW ORDER BY",
- statement: "SELECT department, count(*) AS n, rank() OVER w FROM users GROUP BY department HAVING count(*) > 1 WINDOW w AS (ORDER BY department) ORDER BY n DESC",
- limit: 10,
- want: "SELECT department, count(*) AS n, rank() OVER w FROM users GROUP BY department HAVING count(*) > 1 WINDOW w AS (ORDER BY department) ORDER BY n DESC LIMIT 10",
- clausesInOrder: []string{"GROUP BY", "HAVING", "WINDOW", "ORDER BY n", "LIMIT 10"},
- },
- {
- name: "UNION with outer ORDER BY",
- statement: "SELECT id FROM users UNION ALL SELECT id FROM admins ORDER BY id",
- limit: 10,
- want: "SELECT id FROM users UNION ALL SELECT id FROM admins ORDER BY id LIMIT 10",
- clausesInOrder: []string{"UNION ALL", "ORDER BY", "LIMIT 10"},
- },
- {
- name: "ORDER BY inside window only",
- statement: "SELECT row_number() OVER (ORDER BY created_at) FROM users",
- limit: 10,
- want: "SELECT row_number() OVER (ORDER BY created_at) FROM users LIMIT 10",
- clausesInOrder: []string{"FROM users", "LIMIT 10"},
- },
- {
- name: "ORDER BY inside subquery only",
- statement: "SELECT * FROM (SELECT * FROM users ORDER BY created_at) AS ordered_users",
- limit: 10,
- want: "SELECT * FROM (SELECT * FROM users ORDER BY created_at) AS ordered_users LIMIT 10",
- clausesInOrder: []string{"AS ordered_users", "LIMIT 10"},
- },
- {
- name: "WITH query with outer ORDER BY",
- statement: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users ORDER BY created_at",
- limit: 10,
- want: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users ORDER BY created_at LIMIT 10",
- clausesInOrder: []string{"WITH", "ORDER BY", "LIMIT 10"},
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := getStatementWithResultLimitInline(tt.statement, tt.limit)
- require.NoError(t, err)
- if tt.want != "" {
- assert.Equal(t, tt.want, got)
- }
- assertSubstringsInOrder(t, got, tt.clausesInOrder)
-
- _, err = pgparser.ParsePg(got)
- require.NoError(t, err, "rewritten SQL should remain parseable: %s", got)
- })
- }
-}
-
-func assertSubstringsInOrder(t *testing.T, s string, substrings []string) {
- t.Helper()
-
- start := 0
- for _, substring := range substrings {
- index := strings.Index(s[start:], substring)
- require.NotEqualf(t, -1, index, "expected %q after offset %d in %q", substring, start, s)
- start += index + len(substring)
- }
-}
diff --git a/backend/plugin/db/pg/query_merge_cte_test.go b/backend/plugin/db/pg/query_merge_cte_test.go
deleted file mode 100644
index bbf7e8277a..0000000000
--- a/backend/plugin/db/pg/query_merge_cte_test.go
+++ /dev/null
@@ -1,94 +0,0 @@
-package pg
-
-import (
- "context"
- "strings"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-// TestQueryConnMergeCTEDoesNotLeakRows is a security regression test for the
-// PostgreSQL data-masking bypass where a MERGE smuggled into a data-modifying CTE
-//
-// WITH x AS (MERGE ... RETURNING masked_col) SELECT masked_col FROM x
-//
-// was misclassified as a read-only query (hasDMLInTree did not recognise MergeStmt
-// as a write). That routed it through QueryConn's row-returning QueryContext path,
-// so the RETURNING columns came back to the client and — because the query-span
-// extractor cannot trace lineage through a MERGE — were returned UNMASKED.
-//
-// The fix classifies MERGE as a write, so the editor routes the statement to
-// ExecContext, which discards the RETURNING rows. This test asserts the
-// security-relevant outcome at the driver boundary: the MERGE-CTE must NOT return
-// the secret value as data rows. MERGE ... RETURNING requires PostgreSQL 17.
-func TestQueryConnMergeCTEDoesNotLeakRows(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.GetTestPg17Container(ctx, t)
- defer pgContainer.Close(ctx)
-
- const secret = "TOP-SECRET-SSN-123-45-6789"
- rawDB := pgContainer.GetDB()
- require.NoError(t, rawDB.Ping())
- _, err := rawDB.ExecContext(ctx, `CREATE TABLE secret_doc (id int PRIMARY KEY, secret text);`)
- require.NoError(t, err)
- _, err = rawDB.ExecContext(ctx, `INSERT INTO secret_doc VALUES (1, '`+secret+`');`)
- require.NoError(t, err)
-
- driver, err := (&Driver{}).Open(ctx, storepb.Engine_POSTGRES, db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Username: "postgres",
- },
- Password: "root-password",
- ConnectionContext: db.ConnectionContext{DatabaseName: "postgres"},
- })
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- conn, err := driver.GetDB().Conn(ctx)
- require.NoError(t, err)
- defer conn.Close()
-
- // Baseline: a plain SELECT returns the secret as a data row. This proves the
- // harness actually surfaces row data, so the negative assertion below is meaningful.
- selectResults, err := driver.QueryConn(ctx, conn, `SELECT secret FROM secret_doc`, db.QueryContext{Limit: 5000, MaximumSQLResultSize: 1 << 30})
- require.NoError(t, err)
- require.True(t, resultsContainValue(selectResults, secret),
- "baseline plain SELECT should return the secret as a data row")
-
- // Exploit attempt: a MERGE smuggled into a data-modifying CTE. After the fix it
- // is classified as a write and routed to Exec, so the RETURNING rows are dropped
- // and the secret is never returned as data.
- const mergeCTE = `WITH x AS (
- MERGE INTO secret_doc t USING secret_doc s ON t.id = s.id
- WHEN MATCHED THEN UPDATE SET id = t.id
- RETURNING t.secret AS secret
- ) SELECT secret FROM x`
- mergeResults, err := driver.QueryConn(ctx, conn, mergeCTE, db.QueryContext{Limit: 5000, MaximumSQLResultSize: 1 << 30})
- require.NoError(t, err)
- require.False(t, resultsContainValue(mergeResults, secret),
- "SECURITY: MERGE-in-CTE must not return the masked column as data rows; got %v", mergeResults)
-}
-
-// resultsContainValue reports whether any string cell in the results contains want.
-func resultsContainValue(results []*v1pb.QueryResult, want string) bool {
- for _, r := range results {
- for _, row := range r.GetRows() {
- for _, v := range row.GetValues() {
- if sv, ok := v.GetKind().(*v1pb.RowValue_StringValue); ok && strings.Contains(sv.StringValue, want) {
- return true
- }
- }
- }
- }
- return false
-}
diff --git a/backend/plugin/db/pg/query_search_path_test.go b/backend/plugin/db/pg/query_search_path_test.go
deleted file mode 100644
index b9fce07b8e..0000000000
--- a/backend/plugin/db/pg/query_search_path_test.go
+++ /dev/null
@@ -1,115 +0,0 @@
-package pg
-
-import (
- "context"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-func TestQueryConnSearchPathIncludesPublicAfterSelectedSchema(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.SharedPgContainer(t)
- dbName, rawDB := testcontainer.NewPgDatabase(t)
- _, err := rawDB.ExecContext(ctx, `
- CREATE SCHEMA app;
- CREATE TABLE public.lookup_precedence (marker text);
- CREATE TABLE app.lookup_precedence (marker text);
- INSERT INTO public.lookup_precedence VALUES ('public');
- INSERT INTO app.lookup_precedence VALUES ('app');
- CREATE FUNCTION public.polar_osfs_extract_table_name(rel text) RETURNS text
- LANGUAGE sql
- AS $$ SELECT rel $$;
- CREATE FUNCTION public.polar_alter_relation_to_oss_with_indexes(rel text) RETURNS text
- LANGUAGE plpgsql
- AS $$
- BEGIN
- RETURN polar_osfs_extract_table_name(rel);
- END;
- $$;
- `)
- require.NoError(t, err)
-
- driver, err := (&Driver{}).Open(ctx, storepb.Engine_POSTGRES, db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Username: "postgres",
- },
- Password: "root-password",
- ConnectionContext: db.ConnectionContext{DatabaseName: dbName},
- })
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- conn, err := driver.GetDB().Conn(ctx)
- require.NoError(t, err)
- defer conn.Close()
-
- queryContext := db.QueryContext{
- Schema: "app",
- Limit: 5000,
- MaximumSQLResultSize: 1 << 30,
- }
- results, err := driver.QueryConn(ctx, conn, `SELECT public.polar_alter_relation_to_oss_with_indexes('content_messages_2025q1'::text);`, queryContext)
- require.NoError(t, err)
- require.Equal(t, "content_messages_2025q1", firstStringValue(t, results))
-
- results, err = driver.QueryConn(ctx, conn, `SELECT marker FROM lookup_precedence;`, queryContext)
- require.NoError(t, err)
- require.Equal(t, "app", firstStringValue(t, results))
-}
-
-func TestQueryConnSearchPathEscapesSelectedSchemaName(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.SharedPgContainer(t)
- dbName, rawDB := testcontainer.NewPgDatabase(t)
- _, err := rawDB.ExecContext(ctx, `
- CREATE SCHEMA "app""schema";
- CREATE TABLE "app""schema".lookup_precedence (marker text);
- INSERT INTO "app""schema".lookup_precedence VALUES ('quoted');
- `)
- require.NoError(t, err)
-
- driver, err := (&Driver{}).Open(ctx, storepb.Engine_POSTGRES, db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Username: "postgres",
- },
- Password: "root-password",
- ConnectionContext: db.ConnectionContext{DatabaseName: dbName},
- })
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- conn, err := driver.GetDB().Conn(ctx)
- require.NoError(t, err)
- defer conn.Close()
-
- results, err := driver.QueryConn(ctx, conn, `SELECT marker FROM lookup_precedence;`, db.QueryContext{
- Schema: `app"schema`,
- Limit: 5000,
- MaximumSQLResultSize: 1 << 30,
- })
- require.NoError(t, err)
- require.Equal(t, "quoted", firstStringValue(t, results))
-}
-
-func firstStringValue(t *testing.T, results []*v1pb.QueryResult) string {
- t.Helper()
- require.NotEmpty(t, results)
- require.Empty(t, results[0].GetError())
- require.NotEmpty(t, results[0].GetRows())
- require.NotEmpty(t, results[0].GetRows()[0].GetValues())
- return results[0].GetRows()[0].GetValues()[0].GetStringValue()
-}
diff --git a/backend/plugin/db/pg/query_test.go b/backend/plugin/db/pg/query_test.go
index e8a3ed069e..282f9c0067 100644
--- a/backend/plugin/db/pg/query_test.go
+++ b/backend/plugin/db/pg/query_test.go
@@ -1,11 +1,16 @@
package pg
import (
+ "fmt"
+ "strings"
"testing"
"time"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
+
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
)
func TestBuildTimestamptzRowValue(t *testing.T) {
@@ -146,3 +151,383 @@ func TestPadZeroes(t *testing.T) {
require.Equal(t, test.expected, got)
}
}
+
+func TestGetStatementWithResultLimit(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ limit int
+ want string
+ }{
+ {
+ name: "Simple SELECT without LIMIT",
+ statement: "SELECT * FROM users",
+ limit: 10,
+ want: "SELECT * FROM users LIMIT 10",
+ },
+ {
+ name: "SELECT with WHERE clause",
+ statement: "SELECT id, name FROM users WHERE active = true",
+ limit: 5,
+ want: "SELECT id, name FROM users WHERE active = true LIMIT 5",
+ },
+ {
+ name: "SELECT with existing LIMIT higher than requested",
+ statement: "SELECT * FROM users LIMIT 100",
+ limit: 10,
+ want: "SELECT * FROM users LIMIT 10",
+ },
+ {
+ name: "SELECT with existing LIMIT lower than requested",
+ statement: "SELECT * FROM users LIMIT 5",
+ limit: 10,
+ want: "SELECT * FROM users LIMIT 5",
+ },
+ {
+ name: "WITH query (CTE)",
+ statement: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users",
+ limit: 10,
+ want: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users LIMIT 10",
+ },
+ {
+ name: "SELECT with ORDER BY",
+ statement: "SELECT * FROM users ORDER BY created_at DESC",
+ limit: 10,
+ want: "SELECT * FROM users ORDER BY created_at DESC LIMIT 10",
+ },
+ {
+ name: "SELECT with GROUP BY and HAVING",
+ statement: "SELECT department, COUNT(*) FROM users GROUP BY department HAVING COUNT(*) > 5",
+ limit: 10,
+ want: "SELECT department, COUNT(*) FROM users GROUP BY department HAVING COUNT(*) > 5 LIMIT 10",
+ },
+ {
+ name: "UNION query",
+ statement: "SELECT id FROM users UNION SELECT id FROM admins",
+ limit: 10,
+ want: "SELECT id FROM users UNION SELECT id FROM admins LIMIT 10",
+ },
+ {
+ name: "SELECT with OFFSET",
+ statement: "SELECT * FROM users LIMIT 20 OFFSET 10",
+ limit: 5,
+ want: "SELECT * FROM users LIMIT 5 OFFSET 10",
+ },
+ {
+ name: "SELECT with FOR UPDATE",
+ statement: "SELECT * FROM users WHERE id = 1 FOR UPDATE",
+ limit: 10,
+ want: "SELECT * FROM users WHERE id = 1 LIMIT 10 FOR UPDATE",
+ },
+ {
+ name: "Non-SELECT statement (INSERT)",
+ statement: "INSERT INTO users (name) VALUES ('John')",
+ limit: 10,
+ want: "INSERT INTO users (name) VALUES ('John')",
+ },
+ {
+ name: "Non-SELECT statement (UPDATE)",
+ statement: "UPDATE users SET active = false WHERE id = 1",
+ limit: 10,
+ want: "UPDATE users SET active = false WHERE id = 1",
+ },
+ {
+ name: "Non-SELECT statement (DELETE)",
+ statement: "DELETE FROM users WHERE active = false",
+ limit: 10,
+ want: "DELETE FROM users WHERE active = false",
+ },
+ {
+ name: "SELECT with comments",
+ statement: "SELECT * FROM users -- This is a comment\nWHERE active = true",
+ limit: 10,
+ want: "SELECT * FROM users -- This is a comment\nWHERE active = true LIMIT 10",
+ },
+ {
+ name: "Complex nested SELECT",
+ statement: "SELECT * FROM (SELECT * FROM users WHERE active = true) AS active_users WHERE created_at > '2023-01-01'",
+ limit: 10,
+ want: "SELECT * FROM (SELECT * FROM users WHERE active = true) AS active_users WHERE created_at > '2023-01-01' LIMIT 10",
+ },
+ {
+ name: "SELECT with JOIN",
+ statement: "SELECT u.*, d.name as dept_name FROM users u JOIN departments d ON u.dept_id = d.id",
+ limit: 10,
+ want: "SELECT u.*, d.name as dept_name FROM users u JOIN departments d ON u.dept_id = d.id LIMIT 10",
+ },
+ {
+ name: "SELECT with parentheses",
+ statement: "(SELECT * FROM users)",
+ limit: 10,
+ want: "(SELECT * FROM users) LIMIT 10",
+ },
+ {
+ name: "SELECT with nested parentheses",
+ statement: "((SELECT * FROM users))",
+ limit: 10,
+ want: "((SELECT * FROM users)) LIMIT 10",
+ },
+ {
+ name: "SELECT with parentheses and existing LIMIT higher than requested",
+ statement: "(SELECT * FROM users LIMIT 100)",
+ limit: 10,
+ want: "(SELECT * FROM users LIMIT 10)",
+ },
+ {
+ name: "SELECT with parentheses and existing LIMIT lower than requested",
+ statement: "(SELECT * FROM users LIMIT 5)",
+ limit: 10,
+ want: "(SELECT * FROM users LIMIT 5)",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := getStatementWithResultLimit(tt.statement, tt.limit)
+
+ // For non-SELECT statements, the parser might fail and fall back to CTE approach
+ // which we should avoid for non-SELECT statements
+ if tt.statement == tt.want {
+ // Non-SELECT statements should remain unchanged
+ assert.Equal(t, tt.want, got)
+ } else {
+ // For SELECT statements, check if LIMIT was properly added
+ // The exact format might vary slightly due to parser normalization
+ assert.Contains(t, got, "LIMIT")
+ assert.Contains(t, got, fmt.Sprintf("%d", tt.limit))
+ }
+ })
+ }
+}
+
+func TestGetStatementWithResultLimitInline(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ limit int
+ wantErr bool
+ want string
+ }{
+ {
+ name: "Valid SELECT statement",
+ statement: "SELECT * FROM users",
+ limit: 10,
+ wantErr: false,
+ want: "SELECT * FROM users LIMIT 10",
+ },
+ {
+ name: "Invalid SQL syntax",
+ statement: "SELECT * FROM WHERE",
+ limit: 10,
+ wantErr: true,
+ },
+ {
+ name: "Empty statement",
+ statement: "",
+ limit: 10,
+ wantErr: true,
+ },
+ {
+ name: "FOR UPDATE gets proper whitespace",
+ statement: "SELECT * FROM users WHERE id = 1 FOR UPDATE",
+ limit: 10,
+ want: "SELECT * FROM users WHERE id = 1 LIMIT 10 FOR UPDATE",
+ },
+ {
+ name: "FOR SHARE gets proper whitespace",
+ statement: "SELECT * FROM users FOR SHARE",
+ limit: 5,
+ want: "SELECT * FROM users LIMIT 5 FOR SHARE",
+ },
+ {
+ name: "SELECT ending with function call",
+ statement: "SELECT now()",
+ limit: 10,
+ want: "SELECT now() LIMIT 10",
+ },
+ {
+ name: "SELECT with subquery in WHERE",
+ statement: "SELECT * FROM users WHERE id IN (SELECT id FROM admins)",
+ limit: 10,
+ want: "SELECT * FROM users WHERE id IN (SELECT id FROM admins) LIMIT 10",
+ },
+ {
+ name: "Non-constant LIMIT expression falls back to error",
+ statement: "SELECT * FROM t LIMIT (1+2)",
+ limit: 5,
+ wantErr: true,
+ },
+ {
+ name: "CTE with lateral and ORDER BY",
+ statement: `WITH params AS (
+ SELECT 'resource.environment_id in []'::text AS env_condition
+), affected AS (
+ SELECT
+ p.resource,
+ binding,
+ COALESCE(binding->'condition'->>'expression', '') AS old_expression
+ FROM policy p,
+ LATERAL jsonb_array_elements(p.payload->'bindings') AS binding
+ WHERE p.type = 'IAM'
+ AND p.resource_type = 'PROJECT'
+ AND binding->>'role' = 'roles/projectOwner'
+ AND COALESCE(binding->'condition'->>'expression', '') NOT LIKE '%resource.environment_id%'
+)
+SELECT
+ resource,
+ binding->'members' AS members,
+ binding->'condition' AS old_condition,
+ CASE
+ WHEN old_expression = '' THEN params.env_condition
+ ELSE '(' || old_expression || ') && ' || params.env_condition
+ END AS new_expression
+FROM affected
+CROSS JOIN params
+ORDER BY resource`,
+ limit: 1000,
+ want: `WITH params AS (
+ SELECT 'resource.environment_id in []'::text AS env_condition
+), affected AS (
+ SELECT
+ p.resource,
+ binding,
+ COALESCE(binding->'condition'->>'expression', '') AS old_expression
+ FROM policy p,
+ LATERAL jsonb_array_elements(p.payload->'bindings') AS binding
+ WHERE p.type = 'IAM'
+ AND p.resource_type = 'PROJECT'
+ AND binding->>'role' = 'roles/projectOwner'
+ AND COALESCE(binding->'condition'->>'expression', '') NOT LIKE '%resource.environment_id%'
+)
+SELECT
+ resource,
+ binding->'members' AS members,
+ binding->'condition' AS old_condition,
+ CASE
+ WHEN old_expression = '' THEN params.env_condition
+ ELSE '(' || old_expression || ') && ' || params.env_condition
+ END AS new_expression
+FROM affected
+CROSS JOIN params
+ORDER BY resource LIMIT 1000`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := getStatementWithResultLimitInline(tt.statement, tt.limit)
+ if tt.wantErr {
+ require.Error(t, err)
+ } else {
+ require.NoError(t, err)
+ assert.NotEmpty(t, got)
+ if tt.want != "" {
+ assert.Equal(t, tt.want, got)
+ }
+ }
+ })
+ }
+}
+
+func TestGetStatementWithResultLimitInlineClauseOrder(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ limit int
+ want string
+ clausesInOrder []string
+ }{
+ {
+ name: "ORDER BY",
+ statement: "SELECT * FROM users ORDER BY created_at DESC",
+ limit: 10,
+ want: "SELECT * FROM users ORDER BY created_at DESC LIMIT 10",
+ clausesInOrder: []string{"ORDER BY", "LIMIT 10"},
+ },
+ {
+ name: "OFFSET without LIMIT",
+ statement: "SELECT * FROM users OFFSET 20",
+ limit: 10,
+ clausesInOrder: []string{"OFFSET 20", "LIMIT 10"},
+ },
+ {
+ name: "ORDER BY with OFFSET without LIMIT",
+ statement: "SELECT * FROM users ORDER BY created_at DESC OFFSET 20",
+ limit: 10,
+ clausesInOrder: []string{"ORDER BY", "OFFSET 20", "LIMIT 10"},
+ },
+ {
+ name: "ORDER BY with FOR UPDATE",
+ statement: "SELECT * FROM users ORDER BY id FOR UPDATE",
+ limit: 10,
+ want: "SELECT * FROM users ORDER BY id LIMIT 10 FOR UPDATE",
+ clausesInOrder: []string{"ORDER BY", "LIMIT 10", "FOR UPDATE"},
+ },
+ {
+ name: "ORDER BY with OFFSET and FOR UPDATE",
+ statement: "SELECT * FROM users ORDER BY id OFFSET 20 FOR UPDATE",
+ limit: 10,
+ clausesInOrder: []string{"ORDER BY", "OFFSET 20", "LIMIT 10", "FOR UPDATE"},
+ },
+ {
+ name: "GROUP BY HAVING WINDOW ORDER BY",
+ statement: "SELECT department, count(*) AS n, rank() OVER w FROM users GROUP BY department HAVING count(*) > 1 WINDOW w AS (ORDER BY department) ORDER BY n DESC",
+ limit: 10,
+ want: "SELECT department, count(*) AS n, rank() OVER w FROM users GROUP BY department HAVING count(*) > 1 WINDOW w AS (ORDER BY department) ORDER BY n DESC LIMIT 10",
+ clausesInOrder: []string{"GROUP BY", "HAVING", "WINDOW", "ORDER BY n", "LIMIT 10"},
+ },
+ {
+ name: "UNION with outer ORDER BY",
+ statement: "SELECT id FROM users UNION ALL SELECT id FROM admins ORDER BY id",
+ limit: 10,
+ want: "SELECT id FROM users UNION ALL SELECT id FROM admins ORDER BY id LIMIT 10",
+ clausesInOrder: []string{"UNION ALL", "ORDER BY", "LIMIT 10"},
+ },
+ {
+ name: "ORDER BY inside window only",
+ statement: "SELECT row_number() OVER (ORDER BY created_at) FROM users",
+ limit: 10,
+ want: "SELECT row_number() OVER (ORDER BY created_at) FROM users LIMIT 10",
+ clausesInOrder: []string{"FROM users", "LIMIT 10"},
+ },
+ {
+ name: "ORDER BY inside subquery only",
+ statement: "SELECT * FROM (SELECT * FROM users ORDER BY created_at) AS ordered_users",
+ limit: 10,
+ want: "SELECT * FROM (SELECT * FROM users ORDER BY created_at) AS ordered_users LIMIT 10",
+ clausesInOrder: []string{"AS ordered_users", "LIMIT 10"},
+ },
+ {
+ name: "WITH query with outer ORDER BY",
+ statement: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users ORDER BY created_at",
+ limit: 10,
+ want: "WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users ORDER BY created_at LIMIT 10",
+ clausesInOrder: []string{"WITH", "ORDER BY", "LIMIT 10"},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := getStatementWithResultLimitInline(tt.statement, tt.limit)
+ require.NoError(t, err)
+ if tt.want != "" {
+ assert.Equal(t, tt.want, got)
+ }
+ assertSubstringsInOrder(t, got, tt.clausesInOrder)
+
+ _, err = pgparser.ParsePg(got)
+ require.NoError(t, err, "rewritten SQL should remain parseable: %s", got)
+ })
+ }
+}
+
+func assertSubstringsInOrder(t *testing.T, s string, substrings []string) {
+ t.Helper()
+
+ start := 0
+ for _, substring := range substrings {
+ index := strings.Index(s[start:], substring)
+ require.NotEqualf(t, -1, index, "expected %q after offset %d in %q", substring, start, s)
+ start += index + len(substring)
+ }
+}
diff --git a/backend/plugin/db/pg/sync_column_default_test.go b/backend/plugin/db/pg/sync_column_default_test.go
deleted file mode 100644
index be8bd920f8..0000000000
--- a/backend/plugin/db/pg/sync_column_default_test.go
+++ /dev/null
@@ -1,344 +0,0 @@
-package pg
-
-import (
- "context"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-func TestSync_ColumnDefaultSchemaQualification(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.SharedPgContainer(t)
- dbName, pgDB := testcontainer.NewPgDatabase(t)
-
- // Set up test schema with various default value scenarios
- setupSQL := `
--- Create enum type for testing schema qualification
-CREATE TYPE test_size AS ENUM ('small', 'medium', 'large');
-
--- Create sequence for testing schema qualification
-CREATE SEQUENCE test_sequence START 1;
-
--- Create test table with various default scenarios
-CREATE TABLE test_defaults (
- -- No default
- id INTEGER,
-
- -- NULL default
- col_null_default TEXT DEFAULT NULL,
-
- -- String literal with schema-qualified type
- col_string_literal VARCHAR(50) DEFAULT 'hello',
-
- -- Numeric literal
- col_numeric INTEGER DEFAULT 42,
-
- -- Boolean literal
- col_boolean BOOLEAN DEFAULT true,
-
- -- Function expression
- col_function TIMESTAMP DEFAULT now(),
-
- -- Expression
- col_expression INTEGER DEFAULT (10 + 20),
-
- -- SERIAL (creates sequence automatically)
- col_serial SERIAL,
-
- -- Enum default (should be schema-qualified)
- col_enum test_size DEFAULT 'medium',
-
- -- Custom sequence (should be schema-qualified)
- col_sequence INTEGER DEFAULT nextval('test_sequence'),
-
- -- Array default
- col_array INTEGER[] DEFAULT '{1,2,3}',
-
- -- JSON default
- col_json JSONB DEFAULT '{"key": "value"}',
-
- -- Binary default
- col_binary BYTEA DEFAULT '\xDEADBEEF'
-);
-
--- Add comment to help identify the table
-COMMENT ON TABLE test_defaults IS 'Test table for column default schema qualification';
-`
-
- // Execute the DDL
- _, err := pgDB.Exec(setupSQL)
- require.NoError(t, err)
-
- // Create driver and get metadata using SyncDBSchema
- driver := &Driver{}
- config := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: "postgres",
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Database: dbName,
- },
- Password: "root-password",
- ConnectionContext: db.ConnectionContext{
- EngineVersion: "16.0",
- DatabaseName: dbName,
- },
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
- require.NoError(t, err)
- t.Cleanup(func() { require.NoError(t, openedDriver.Close(ctx)) })
-
- pgDriver, ok := openedDriver.(*Driver)
- require.True(t, ok)
-
- // Run sync operation to get metadata
- metadata, err := pgDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
- require.NotNil(t, metadata)
-
- // Find our test table
- var testTable *storepb.TableMetadata
- for _, schema := range metadata.Schemas {
- if schema.Name == "public" {
- for _, table := range schema.Tables {
- if table.Name == "test_defaults" {
- testTable = table
- break
- }
- }
- }
- }
- require.NotNil(t, testTable, "test_defaults table should be found")
-
- // Create a map for easier column lookup
- columnMap := make(map[string]*storepb.ColumnMetadata)
- for _, col := range testTable.Columns {
- columnMap[col.Name] = col
- }
-
- // Test cases to verify schema qualification
- testCases := []struct {
- columnName string
- expectedDefault string
- description string
- }{
- {
- columnName: "id",
- expectedDefault: "",
- description: "No default should be empty string",
- },
- {
- columnName: "col_null_default",
- expectedDefault: "",
- description: "DEFAULT NULL should be empty string (PostgreSQL limitation)",
- },
- {
- columnName: "col_string_literal",
- expectedDefault: "'hello'::character varying",
- description: "String literal should have type cast",
- },
- {
- columnName: "col_numeric",
- expectedDefault: "42",
- description: "Numeric literal should be unquoted",
- },
- {
- columnName: "col_boolean",
- expectedDefault: "true",
- description: "Boolean literal should be unquoted",
- },
- {
- columnName: "col_function",
- expectedDefault: "now()",
- description: "Function should be preserved",
- },
- {
- columnName: "col_expression",
- expectedDefault: "(10 + 20)",
- description: "Expression should preserve parentheses",
- },
- {
- columnName: "col_enum",
- expectedDefault: "'medium'::public.test_size",
- description: "Enum default should be SCHEMA-QUALIFIED with public.test_size",
- },
- {
- columnName: "col_sequence",
- expectedDefault: "nextval('public.test_sequence'::regclass)",
- description: "Custom sequence should be SCHEMA-QUALIFIED with public.test_sequence",
- },
- {
- columnName: "col_array",
- expectedDefault: "'{1,2,3}'::integer[]",
- description: "Array should have type cast",
- },
- {
- columnName: "col_json",
- expectedDefault: "'{\"key\": \"value\"}'::jsonb",
- description: "JSONB should have type cast",
- },
- {
- columnName: "col_binary",
- expectedDefault: "'\\xdeadbeef'::bytea",
- description: "Binary should have type cast and lowercase hex",
- },
- }
-
- // Verify each test case
- for _, tc := range testCases {
- t.Run(tc.columnName, func(t *testing.T) {
- t.Parallel()
- column, exists := columnMap[tc.columnName]
- require.True(t, exists, "Column %s should exist", tc.columnName)
-
- require.Equal(t, tc.expectedDefault, column.Default,
- "Column %s: %s. Expected: %q, Got: %q",
- tc.columnName, tc.description, tc.expectedDefault, column.Default)
- })
- }
-
- // Special test: Verify SERIAL column has schema-qualified sequence
- serialColumn, exists := columnMap["col_serial"]
- require.True(t, exists, "col_serial should exist")
- require.Contains(t, serialColumn.Default, "public.test_defaults_col_serial_seq",
- "SERIAL column should have schema-qualified sequence name. Got: %s", serialColumn.Default)
- require.Contains(t, serialColumn.Default, "nextval(",
- "SERIAL column should use nextval function. Got: %s", serialColumn.Default)
-
- // Verify that Default field is being used (consolidation from DefaultNull and DefaultExpression)
- for columnName, column := range columnMap {
- // The Default field should be used instead of the deprecated DefaultExpression and DefaultNull fields
- if column.Default != "" {
- t.Logf("Column %s uses Default field: %s", columnName, column.Default)
- }
- }
-}
-
-func TestSync_ColumnDefaultCrossSchemaQualification(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.SharedPgContainer(t)
- dbName, pgDB := testcontainer.NewPgDatabase(t)
-
- // Create schema with cross-schema references
- setupSQL := `
--- Create custom schema to test cross-schema qualification
-CREATE SCHEMA custom_schema;
-
--- Create enum in custom schema
-CREATE TYPE custom_schema.status_type AS ENUM ('active', 'inactive', 'pending');
-
--- Create sequence in custom schema
-CREATE SEQUENCE custom_schema.my_sequence START 100;
-
--- Create function in custom schema for testing
-CREATE OR REPLACE FUNCTION custom_schema.get_prefix() RETURNS TEXT AS $$
-BEGIN
- RETURN 'prefix_value';
-END;
-$$ LANGUAGE plpgsql IMMUTABLE;
-
--- Test table in public schema with references to custom schema objects
-CREATE TABLE critical_test (
- -- Cross-schema enum reference
- status custom_schema.status_type DEFAULT 'active',
-
- -- Cross-schema sequence reference
- counter INTEGER DEFAULT nextval('custom_schema.my_sequence'),
-
- -- Complex expression with cross-schema function
- computed TEXT DEFAULT custom_schema.get_prefix()
-);
-`
-
- // Execute the DDL
- _, err := pgDB.Exec(setupSQL)
- require.NoError(t, err)
-
- // Create driver and get metadata
- driver := &Driver{}
- config := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: "postgres",
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Database: dbName,
- },
- Password: "root-password",
- ConnectionContext: db.ConnectionContext{
- EngineVersion: "16.0",
- DatabaseName: dbName,
- },
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
- require.NoError(t, err)
- defer openedDriver.Close(ctx)
-
- pgDriver, ok := openedDriver.(*Driver)
- require.True(t, ok)
-
- // Run sync operation
- metadata, err := pgDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Find the test table
- var testTable *storepb.TableMetadata
- for _, schema := range metadata.Schemas {
- if schema.Name == "public" {
- for _, table := range schema.Tables {
- if table.Name == "critical_test" {
- testTable = table
- break
- }
- }
- }
- }
- require.NotNil(t, testTable)
-
- // Verify cross-schema qualification
- columnMap := make(map[string]*storepb.ColumnMetadata)
- for _, col := range testTable.Columns {
- columnMap[col.Name] = col
- }
-
- // Critical test: Cross-schema enum should be fully qualified
- statusCol := columnMap["status"]
- require.NotNil(t, statusCol)
- require.Equal(t, "'active'::custom_schema.status_type", statusCol.Default,
- "Cross-schema enum should be fully qualified")
-
- // Critical test: Cross-schema sequence should be fully qualified
- counterCol := columnMap["counter"]
- require.NotNil(t, counterCol)
- require.Equal(t, "nextval('custom_schema.my_sequence'::regclass)", counterCol.Default,
- "Cross-schema sequence should be fully qualified")
-
- // Critical test: Complex expression with cross-schema function
- computedCol := columnMap["computed"]
- require.NotNil(t, computedCol)
- require.Equal(t, "custom_schema.get_prefix()", computedCol.Default,
- "Cross-schema function should be fully qualified")
-
- // Cleanup: Drop test objects to avoid interference with other tests
- cleanupSQL := `
- DROP TABLE IF EXISTS critical_test CASCADE;
- DROP TABLE IF EXISTS test_defaults CASCADE;
- DROP SCHEMA IF EXISTS custom_schema CASCADE;
- DROP TYPE IF EXISTS test_size CASCADE;
- DROP SEQUENCE IF EXISTS test_sequence CASCADE;
- `
- _, err = pgDB.Exec(cleanupSQL)
- require.NoError(t, err)
-}
diff --git a/backend/plugin/db/pg/sync_column_test.go b/backend/plugin/db/pg/sync_column_test.go
deleted file mode 100644
index 3ffc0ed27f..0000000000
--- a/backend/plugin/db/pg/sync_column_test.go
+++ /dev/null
@@ -1,325 +0,0 @@
-package pg
-
-import (
- "context"
- "database/sql"
- "fmt"
- "slices"
- "strings"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
-)
-
-// legacyListColumnQuery is the previous INFORMATION_SCHEMA.COLUMNS-based column query.
-// It is kept here as the oracle for TestListColumnQueryMatchesInformationSchema: the
-// pg_catalog-based query must produce identical rows when the user has full privileges.
-var legacyListColumnQuery = `
-SELECT
- cols.table_schema,
- cols.table_name,
- cols.column_name,
- cols.data_type,
- cols.character_maximum_length,
- cols.numeric_precision,
- cols.numeric_scale,
- cols.datetime_precision,
- cols.ordinal_position,
- cols.column_default,
- cols.is_nullable,
- cols.collation_name,
- cols.udt_schema,
- cols.udt_name,
- cols.identity_generation,
- pg_catalog.col_description(c.oid, cols.ordinal_position::int) as column_comment
-FROM INFORMATION_SCHEMA.COLUMNS AS cols
-LEFT JOIN pg_namespace n ON n.nspname = cols.table_schema
-LEFT JOIN pg_class c ON c.relname = cols.table_name AND c.relnamespace = n.oid AND c.relkind IN ('r', 'v', 'm', 'p', 'f')` + fmt.Sprintf(`
-WHERE cols.table_schema NOT IN (%s)
- AND cols.table_schema NOT LIKE 'pg_temp%%'
- AND cols.table_schema NOT LIKE 'pg_toast%%'
-ORDER BY cols.table_schema, cols.table_name, cols.ordinal_position;`, pgparser.SystemSchemaWhereClause)
-
-type columnQueryRow struct {
- TableSchema string
- TableName string
- ColumnName string
- DataType string
- CharacterMaxLength sql.NullString
- NumericPrecision sql.NullString
- NumericScale sql.NullString
- DatetimePrecision sql.NullString
- OrdinalPosition int64
- ColumnDefault sql.NullString
- IsNullable string
- CollationName sql.NullString
- UdtSchema string
- UdtName string
- IdentityGeneration sql.NullString
- ColumnComment sql.NullString
-}
-
-func queryColumnRows(t *testing.T, txn *sql.Tx, query string) []columnQueryRow {
- t.Helper()
- rows, err := txn.Query(query)
- require.NoError(t, err)
- defer rows.Close()
-
- var result []columnQueryRow
- for rows.Next() {
- var r columnQueryRow
- require.NoError(t, rows.Scan(
- &r.TableSchema,
- &r.TableName,
- &r.ColumnName,
- &r.DataType,
- &r.CharacterMaxLength,
- &r.NumericPrecision,
- &r.NumericScale,
- &r.DatetimePrecision,
- &r.OrdinalPosition,
- &r.ColumnDefault,
- &r.IsNullable,
- &r.CollationName,
- &r.UdtSchema,
- &r.UdtName,
- &r.IdentityGeneration,
- &r.ColumnComment,
- ))
- result = append(result, r)
- }
- require.NoError(t, rows.Err())
-
- // The two queries may order rows differently for exotic identifier names
- // (information_schema sorts varchar with the database collation, pg_catalog sorts
- // the name type with C collation), so normalize the order before comparing.
- slices.SortFunc(result, func(a, b columnQueryRow) int {
- if c := strings.Compare(a.TableSchema, b.TableSchema); c != 0 {
- return c
- }
- if c := strings.Compare(a.TableName, b.TableName); c != 0 {
- return c
- }
- return int(a.OrdinalPosition - b.OrdinalPosition)
- })
- return result
-}
-
-// TestListColumnQueryMatchesInformationSchema verifies that the pg_catalog-based column
-// query produces exactly the same rows as the previous INFORMATION_SCHEMA.COLUMNS-based
-// query for a privileged user, across a wide range of column types. This guards the type
-// rendering contract: the Go-side formatting in getTableColumns depends on the exact
-// data_type / precision / udt values that information_schema used to produce.
-func TestListColumnQueryMatchesInformationSchema(t *testing.T) {
- t.Parallel()
-
- _, pgDB := testcontainer.NewPgDatabase(t)
-
- setupSQL := `
-CREATE SCHEMA app;
-CREATE TYPE app.mood AS ENUM ('happy', 'sad');
-CREATE TYPE public.shirt_size AS ENUM ('s', 'm', 'l');
-CREATE DOMAIN app.positive_int AS integer NOT NULL CHECK (VALUE > 0);
-CREATE DOMAIN public.us_zip AS varchar(10);
-CREATE DOMAIN app.int_array AS integer[];
-CREATE SEQUENCE app.seq1;
-
-CREATE TABLE app.kitchen_sink (
- id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
- id2 integer GENERATED BY DEFAULT AS IDENTITY,
- c_smallint smallint,
- c_int integer NOT NULL DEFAULT 42,
- c_bigint bigint DEFAULT nextval('app.seq1'),
- c_numeric numeric,
- c_numeric_p numeric(8),
- c_numeric_p0 numeric(8, 0),
- c_numeric_ps numeric(10, 2),
- c_real real,
- c_double double precision,
- c_money money,
- c_char char,
- c_char_n char(5),
- c_varchar varchar,
- c_varchar_n varchar(20) DEFAULT 'hello',
- c_text text COLLATE "C",
- c_bytea bytea,
- c_bool boolean DEFAULT true,
- c_date date,
- c_time time,
- c_time_p time(3),
- c_timetz time with time zone,
- c_timetz_p time(5) with time zone,
- c_ts timestamp,
- c_ts_p timestamp(2),
- c_tstz timestamp with time zone DEFAULT now(),
- c_tstz_p timestamp(4) with time zone,
- c_interval interval,
- c_interval_p interval(3),
- c_uuid uuid,
- c_json json,
- c_jsonb jsonb DEFAULT '{}'::jsonb,
- c_xml xml,
- c_inet inet,
- c_cidr cidr,
- c_macaddr macaddr,
- c_bit bit,
- c_bit_n bit(3),
- c_varbit varbit,
- c_varbit_n varbit(8),
- c_int_array integer[],
- c_text_matrix text[][],
- c_varchar_array varchar(5)[],
- c_enum app.mood DEFAULT 'happy',
- c_enum_public public.shirt_size,
- c_enum_array app.mood[],
- c_domain app.positive_int,
- c_domain_varchar public.us_zip DEFAULT 'x',
- c_domain_array app.int_array,
- c_point point,
- c_tsvector tsvector,
- c_int4range int4range,
- c_oid oid,
- c_name name,
- c_internal_char "char",
- c_generated integer GENERATED ALWAYS AS (c_int * 2) STORED,
- c_drop_me text
-);
-ALTER TABLE app.kitchen_sink DROP COLUMN c_drop_me;
-COMMENT ON COLUMN app.kitchen_sink.c_int IS 'int comment';
-
-CREATE TABLE app.parted (id integer, region text NOT NULL) PARTITION BY LIST (region);
-CREATE TABLE app.parted_us PARTITION OF app.parted FOR VALUES IN ('us');
-
-CREATE VIEW app.v_sink AS SELECT id, c_int, c_text FROM app.kitchen_sink;
-
-CREATE TABLE public.plain (a serial, b text);
-`
- _, err := pgDB.Exec(setupSQL)
- require.NoError(t, err)
-
- txn, err := pgDB.Begin()
- require.NoError(t, err)
- defer txn.Rollback()
-
- // SyncDBSchema clears the search path before the column sync; match it so default
- // expressions render identically.
- require.NoError(t, setTxSearchPath(txn, ""))
-
- legacyRows := queryColumnRows(t, txn, legacyListColumnQuery)
- newRows := queryColumnRows(t, txn, getListColumnQuery(true))
-
- // Sanity: the kitchen sink table alone has 50+ columns.
- require.Greater(t, len(legacyRows), 50)
- require.Equal(t, legacyRows, newRows)
-}
-
-// TestSyncColumnsWithoutTablePrivilege verifies that column sync no longer depends on the
-// sync user's table privileges. information_schema.columns privilege-filters its rows, so
-// a sync user without privileges on a table used to get table metadata with indexes and
-// constraints (read from pg_catalog) but zero columns. The pg_catalog-based column query
-// must return the columns regardless.
-func TestSyncColumnsWithoutTablePrivilege(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.SharedPgContainer(t)
- dbName, pgDB := testcontainer.NewPgDatabase(t)
-
- setupSQL := `
-CREATE SCHEMA restricted;
-CREATE TABLE restricted.secret_table (
- id integer PRIMARY KEY,
- name varchar(50) NOT NULL DEFAULT 'anon',
- amount numeric(10, 2),
- CONSTRAINT positive_amount CHECK (amount >= 0)
-);
-CREATE INDEX idx_secret_name ON restricted.secret_table (name);
-`
- _, err := pgDB.Exec(setupSQL)
- require.NoError(t, err)
-
- // pg_read_all_stats lets the sync read pg_table_size/pg_indexes_size; it grants no
- // table or column privileges, so information_schema.columns still hides the table.
- limitedUser := "limited_" + dbName
- _, err = pgDB.Exec(fmt.Sprintf("CREATE USER %s WITH LOGIN PASSWORD 'limited-password'; GRANT pg_read_all_stats TO %s;", limitedUser, limitedUser))
- require.NoError(t, err)
-
- limitedDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=%s password=limited-password database=%s", pgContainer.GetHost(), pgContainer.GetPort(), limitedUser, dbName))
- require.NoError(t, err)
- defer limitedDB.Close()
- require.NoError(t, limitedDB.Ping())
-
- // Confirm the privilege filtering this fix works around: information_schema.columns
- // returns nothing for the table when queried by the unprivileged user.
- var count int
- require.NoError(t, limitedDB.QueryRow(`
- SELECT COUNT(*) FROM information_schema.columns
- WHERE table_schema = 'restricted' AND table_name = 'secret_table'`).Scan(&count))
- require.Zero(t, count)
-
- driver := &Driver{}
- config := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: limitedUser,
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Database: dbName,
- },
- Password: "limited-password",
- ConnectionContext: db.ConnectionContext{
- EngineVersion: "16.0",
- DatabaseName: dbName,
- },
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
- require.NoError(t, err)
- defer openedDriver.Close(ctx)
-
- pgDriver, ok := openedDriver.(*Driver)
- require.True(t, ok)
-
- metadata, err := pgDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- var secretTable *storepb.TableMetadata
- for _, schema := range metadata.Schemas {
- if schema.Name != "restricted" {
- continue
- }
- for _, table := range schema.Tables {
- if table.Name == "secret_table" {
- secretTable = table
- break
- }
- }
- }
- require.NotNil(t, secretTable, "restricted.secret_table not found in synced metadata")
-
- // Indexes and check constraints were always visible via pg_catalog; columns must now
- // be visible too instead of leaving inconsistent table metadata.
- require.NotEmpty(t, secretTable.Indexes)
- require.NotEmpty(t, secretTable.CheckConstraints)
-
- require.Len(t, secretTable.Columns, 3)
- id, name, amount := secretTable.Columns[0], secretTable.Columns[1], secretTable.Columns[2]
-
- require.Equal(t, "id", id.Name)
- require.Equal(t, "integer", id.Type)
- require.False(t, id.Nullable)
-
- require.Equal(t, "name", name.Name)
- require.Equal(t, "character varying(50)", name.Type)
- require.False(t, name.Nullable)
- require.Equal(t, "'anon'::character varying", name.Default)
-
- require.Equal(t, "amount", amount.Name)
- require.Equal(t, "numeric(10,2)", amount.Type)
- require.True(t, amount.Nullable)
-}
diff --git a/backend/plugin/db/pg/sync_composite_type_test.go b/backend/plugin/db/pg/sync_composite_type_test.go
deleted file mode 100644
index 6865e1d280..0000000000
--- a/backend/plugin/db/pg/sync_composite_type_test.go
+++ /dev/null
@@ -1,117 +0,0 @@
-package pg
-
-import (
- "context"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-// TestSyncCompositeTypes pins the exact metadata sync produces for composite
-// types. The definition round-trip in backend/plugin/schema/pg compares two
-// synced databases against each other, so a sync that is wrong the same way on
-// both sides passes it vacuously; these assertions do not.
-func TestSyncCompositeTypes(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.SharedPgContainer(t)
- dbName, pgDB := testcontainer.NewPgDatabase(t)
-
- _, err := pgDB.ExecContext(ctx, `
-CREATE TYPE zz_base AS (street text COLLATE "C", city varchar(50));
-COMMENT ON TYPE zz_base IS 'base address type';
-COMMENT ON COLUMN zz_base.street IS 'street line';
-CREATE TYPE aa_nested AS (home zz_base, homes zz_base[], tags text[]);
-CREATE TYPE empty_type AS ();
-CREATE SCHEMA geo;
-CREATE TYPE geo.point2 AS (lat numeric(9,6), lng numeric(9,6));
-CREATE TYPE wrapper AS (p geo.point2);
-CREATE TABLE plain_table (id int);
-CREATE SCHEMA locale;
-CREATE COLLATION locale.mycoll (locale = 'C');
-CREATE TYPE coll_qualified AS (v text COLLATE locale.mycoll);
-`)
- require.NoError(t, err)
-
- driver := &Driver{}
- config := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: "postgres",
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Database: dbName,
- },
- Password: "root-password",
- ConnectionContext: db.ConnectionContext{
- EngineVersion: "16.0",
- DatabaseName: dbName,
- },
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
- require.NoError(t, err)
- t.Cleanup(func() { require.NoError(t, openedDriver.Close(ctx)) })
-
- pgDriver, ok := openedDriver.(*Driver)
- require.True(t, ok)
-
- metadata, err := pgDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- compositesBySchema := make(map[string]map[string]*storepb.CompositeTypeMetadata)
- for _, schemaMeta := range metadata.Schemas {
- m := make(map[string]*storepb.CompositeTypeMetadata)
- for _, composite := range schemaMeta.CompositeTypes {
- m[composite.Name] = composite
- }
- compositesBySchema[schemaMeta.Name] = m
- }
-
- public := compositesBySchema["public"]
- require.Len(t, public, 5, "public schema should have exactly the created composite types")
- require.NotContains(t, public, "plain_table", "table row types must not be synced as composite types")
-
- collQualified := public["coll_qualified"]
- require.NotNil(t, collQualified)
- require.Len(t, collQualified.Attributes, 1)
- require.Equal(t, "locale.mycoll", collQualified.Attributes[0].Collation,
- "non-pg_catalog collations must be schema-qualified")
-
- base := public["zz_base"]
- require.NotNil(t, base)
- require.Equal(t, "base address type", base.Comment)
- require.Len(t, base.Attributes, 2)
- require.Equal(t, "street", base.Attributes[0].Name)
- require.Equal(t, "text", base.Attributes[0].Type)
- require.Equal(t, `"C"`, base.Attributes[0].Collation)
- require.Equal(t, "street line", base.Attributes[0].Comment)
- require.Equal(t, "city", base.Attributes[1].Name)
- require.Equal(t, "character varying(50)", base.Attributes[1].Type)
- require.Empty(t, base.Attributes[1].Collation)
-
- nested := public["aa_nested"]
- require.NotNil(t, nested)
- require.Len(t, nested.Attributes, 3)
- require.Equal(t, "public.zz_base", nested.Attributes[0].Type, "user-defined attribute types must be schema-qualified")
- require.Equal(t, "public.zz_base[]", nested.Attributes[1].Type)
- require.Equal(t, "text[]", nested.Attributes[2].Type)
-
- empty := public["empty_type"]
- require.NotNil(t, empty, "zero-attribute composite types must be synced")
- require.Empty(t, empty.Attributes)
-
- wrapper := public["wrapper"]
- require.NotNil(t, wrapper)
- require.Len(t, wrapper.Attributes, 1)
- require.Equal(t, "geo.point2", wrapper.Attributes[0].Type, "cross-schema attribute types must be schema-qualified")
-
- geo := compositesBySchema["geo"]
- require.Len(t, geo, 1)
- require.NotNil(t, geo["point2"])
-}
diff --git a/backend/plugin/db/pg/sync_foreign_table_test.go b/backend/plugin/db/pg/sync_foreign_table_test.go
deleted file mode 100644
index bb93fefed8..0000000000
--- a/backend/plugin/db/pg/sync_foreign_table_test.go
+++ /dev/null
@@ -1,153 +0,0 @@
-package pg
-
-import (
- "context"
- "database/sql"
- "fmt"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-// legacyListForeignTableQuery is the previous information_schema.foreign_tables-based
-// query, kept as the oracle: for a privileged user the pg_catalog-based query must
-// produce identical rows.
-const legacyListForeignTableQuery = `SELECT
- foreign_table.foreign_table_schema,
- foreign_table.foreign_table_name,
- foreign_table.foreign_server_catalog,
- foreign_table.foreign_server_name
-FROM information_schema.foreign_tables AS foreign_table
-ORDER BY foreign_table.foreign_table_schema, foreign_table.foreign_table_name;`
-
-const foreignTableSetupSQL = `
-CREATE EXTENSION postgres_fdw;
-CREATE SERVER loopback_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'postgres');
-CREATE SCHEMA fdw_schema;
-CREATE FOREIGN TABLE fdw_schema.remote_orders (
- id integer NOT NULL,
- note varchar(30)
-) SERVER loopback_server OPTIONS (schema_name 'public', table_name 'orders');
-CREATE FOREIGN TABLE public.remote_items (
- item_id bigint
-) SERVER loopback_server OPTIONS (schema_name 'public', table_name 'items');
-`
-
-func queryForeignTableRows(t *testing.T, conn *sql.DB, query string) [][4]string {
- t.Helper()
- rows, err := conn.Query(query)
- require.NoError(t, err)
- defer rows.Close()
-
- var result [][4]string
- for rows.Next() {
- var r [4]string
- require.NoError(t, rows.Scan(&r[0], &r[1], &r[2], &r[3]))
- result = append(result, r)
- }
- require.NoError(t, rows.Err())
- return result
-}
-
-// TestListForeignTableQueryMatchesInformationSchema verifies that the pg_catalog-based
-// foreign table query produces exactly the same rows as the previous
-// information_schema.foreign_tables-based query for a privileged user.
-func TestListForeignTableQueryMatchesInformationSchema(t *testing.T) {
- t.Parallel()
-
- _, pgDB := testcontainer.NewPgDatabase(t)
-
- _, err := pgDB.Exec(foreignTableSetupSQL)
- require.NoError(t, err)
-
- legacyRows := queryForeignTableRows(t, pgDB, legacyListForeignTableQuery)
- newRows := queryForeignTableRows(t, pgDB, getListForeignTableQuery())
-
- require.Len(t, legacyRows, 2)
- require.Equal(t, legacyRows, newRows)
-}
-
-// TestSyncForeignTablesWithoutTablePrivilege verifies that foreign table sync no longer
-// depends on the sync user's privileges. information_schema.foreign_tables
-// privilege-filters its rows, so a sync user without privileges on a foreign table used
-// to silently lose the whole table from the synced metadata.
-func TestSyncForeignTablesWithoutTablePrivilege(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
-
- pgContainer := testcontainer.SharedPgContainer(t)
- dbName, pgDB := testcontainer.NewPgDatabase(t)
-
- _, err := pgDB.Exec(foreignTableSetupSQL)
- require.NoError(t, err)
-
- // pg_read_all_stats lets the sync read pg_table_size/pg_indexes_size; it grants no
- // table or column privileges, so information_schema.foreign_tables still filters.
- limitedUser := "limited_" + dbName
- _, err = pgDB.Exec(fmt.Sprintf("CREATE USER %s WITH LOGIN PASSWORD 'limited-password'; GRANT pg_read_all_stats TO %s;", limitedUser, limitedUser))
- require.NoError(t, err)
-
- limitedDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=%s password=limited-password database=%s", pgContainer.GetHost(), pgContainer.GetPort(), limitedUser, dbName))
- require.NoError(t, err)
- defer limitedDB.Close()
- require.NoError(t, limitedDB.Ping())
-
- // Confirm the privilege filtering this fix works around: the unprivileged user sees
- // no foreign tables through information_schema.
- var count int
- require.NoError(t, limitedDB.QueryRow(`SELECT COUNT(*) FROM information_schema.foreign_tables`).Scan(&count))
- require.Zero(t, count)
-
- driver := &Driver{}
- config := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: limitedUser,
- Host: pgContainer.GetHost(),
- Port: pgContainer.GetPort(),
- Database: dbName,
- },
- Password: "limited-password",
- ConnectionContext: db.ConnectionContext{
- EngineVersion: "16.0",
- DatabaseName: dbName,
- },
- }
-
- openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
- require.NoError(t, err)
- defer openedDriver.Close(ctx)
-
- pgDriver, ok := openedDriver.(*Driver)
- require.True(t, ok)
-
- metadata, err := pgDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- var fdwSchema *storepb.SchemaMetadata
- for _, schema := range metadata.Schemas {
- if schema.Name == "fdw_schema" {
- fdwSchema = schema
- break
- }
- }
- require.NotNil(t, fdwSchema, "fdw_schema not found in synced metadata")
- require.Len(t, fdwSchema.ExternalTables, 1)
-
- remoteOrders := fdwSchema.ExternalTables[0]
- require.Equal(t, "remote_orders", remoteOrders.Name)
- require.Equal(t, "loopback_server", remoteOrders.ExternalServerName)
- require.Equal(t, dbName, remoteOrders.ExternalDatabaseName)
-
- // Columns of foreign tables come from the privilege-independent column sync.
- require.Len(t, remoteOrders.Columns, 2)
- require.Equal(t, "id", remoteOrders.Columns[0].Name)
- require.Equal(t, "integer", remoteOrders.Columns[0].Type)
- require.False(t, remoteOrders.Columns[0].Nullable)
- require.Equal(t, "note", remoteOrders.Columns[1].Name)
- require.Equal(t, "character varying(30)", remoteOrders.Columns[1].Type)
-}
diff --git a/backend/plugin/db/pg/sync_test.go b/backend/plugin/db/pg/sync_test.go
index bec09e4c6e..8b975de850 100644
--- a/backend/plugin/db/pg/sync_test.go
+++ b/backend/plugin/db/pg/sync_test.go
@@ -1,9 +1,19 @@
package pg
import (
+ "context"
+ "database/sql"
+ "fmt"
+ "slices"
+ "strings"
"testing"
"github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
)
func TestIsAtLeastPG10(t *testing.T) {
@@ -42,3 +52,887 @@ func TestIsAtLeastPG10(t *testing.T) {
require.Equal(t, test.want, got)
}
}
+
+// legacyListColumnQuery is the previous INFORMATION_SCHEMA.COLUMNS-based column query.
+// It is kept here as the oracle for TestListColumnQueryMatchesInformationSchema: the
+// pg_catalog-based query must produce identical rows when the user has full privileges.
+var legacyListColumnQuery = `
+SELECT
+ cols.table_schema,
+ cols.table_name,
+ cols.column_name,
+ cols.data_type,
+ cols.character_maximum_length,
+ cols.numeric_precision,
+ cols.numeric_scale,
+ cols.datetime_precision,
+ cols.ordinal_position,
+ cols.column_default,
+ cols.is_nullable,
+ cols.collation_name,
+ cols.udt_schema,
+ cols.udt_name,
+ cols.identity_generation,
+ pg_catalog.col_description(c.oid, cols.ordinal_position::int) as column_comment
+FROM INFORMATION_SCHEMA.COLUMNS AS cols
+LEFT JOIN pg_namespace n ON n.nspname = cols.table_schema
+LEFT JOIN pg_class c ON c.relname = cols.table_name AND c.relnamespace = n.oid AND c.relkind IN ('r', 'v', 'm', 'p', 'f')` + fmt.Sprintf(`
+WHERE cols.table_schema NOT IN (%s)
+ AND cols.table_schema NOT LIKE 'pg_temp%%'
+ AND cols.table_schema NOT LIKE 'pg_toast%%'
+ORDER BY cols.table_schema, cols.table_name, cols.ordinal_position;`, pgparser.SystemSchemaWhereClause)
+
+type columnQueryRow struct {
+ TableSchema string
+ TableName string
+ ColumnName string
+ DataType string
+ CharacterMaxLength sql.NullString
+ NumericPrecision sql.NullString
+ NumericScale sql.NullString
+ DatetimePrecision sql.NullString
+ OrdinalPosition int64
+ ColumnDefault sql.NullString
+ IsNullable string
+ CollationName sql.NullString
+ UdtSchema string
+ UdtName string
+ IdentityGeneration sql.NullString
+ ColumnComment sql.NullString
+}
+
+func queryColumnRows(t *testing.T, txn *sql.Tx, query string) []columnQueryRow {
+ t.Helper()
+ rows, err := txn.Query(query)
+ require.NoError(t, err)
+ defer rows.Close()
+
+ var result []columnQueryRow
+ for rows.Next() {
+ var r columnQueryRow
+ require.NoError(t, rows.Scan(
+ &r.TableSchema,
+ &r.TableName,
+ &r.ColumnName,
+ &r.DataType,
+ &r.CharacterMaxLength,
+ &r.NumericPrecision,
+ &r.NumericScale,
+ &r.DatetimePrecision,
+ &r.OrdinalPosition,
+ &r.ColumnDefault,
+ &r.IsNullable,
+ &r.CollationName,
+ &r.UdtSchema,
+ &r.UdtName,
+ &r.IdentityGeneration,
+ &r.ColumnComment,
+ ))
+ result = append(result, r)
+ }
+ require.NoError(t, rows.Err())
+
+ // The two queries may order rows differently for exotic identifier names
+ // (information_schema sorts varchar with the database collation, pg_catalog sorts
+ // the name type with C collation), so normalize the order before comparing.
+ slices.SortFunc(result, func(a, b columnQueryRow) int {
+ if c := strings.Compare(a.TableSchema, b.TableSchema); c != 0 {
+ return c
+ }
+ if c := strings.Compare(a.TableName, b.TableName); c != 0 {
+ return c
+ }
+ return int(a.OrdinalPosition - b.OrdinalPosition)
+ })
+ return result
+}
+
+// TestListColumnQueryMatchesInformationSchema verifies that the pg_catalog-based column
+// query produces exactly the same rows as the previous INFORMATION_SCHEMA.COLUMNS-based
+// query for a privileged user, across a wide range of column types. This guards the type
+// rendering contract: the Go-side formatting in getTableColumns depends on the exact
+// data_type / precision / udt values that information_schema used to produce.
+func TestListColumnQueryMatchesInformationSchema(t *testing.T) {
+ t.Parallel()
+
+ _, pgDB := testcontainer.NewPgDatabase(t)
+
+ setupSQL := `
+CREATE SCHEMA app;
+CREATE TYPE app.mood AS ENUM ('happy', 'sad');
+CREATE TYPE public.shirt_size AS ENUM ('s', 'm', 'l');
+CREATE DOMAIN app.positive_int AS integer NOT NULL CHECK (VALUE > 0);
+CREATE DOMAIN public.us_zip AS varchar(10);
+CREATE DOMAIN app.int_array AS integer[];
+CREATE SEQUENCE app.seq1;
+
+CREATE TABLE app.kitchen_sink (
+ id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+ id2 integer GENERATED BY DEFAULT AS IDENTITY,
+ c_smallint smallint,
+ c_int integer NOT NULL DEFAULT 42,
+ c_bigint bigint DEFAULT nextval('app.seq1'),
+ c_numeric numeric,
+ c_numeric_p numeric(8),
+ c_numeric_p0 numeric(8, 0),
+ c_numeric_ps numeric(10, 2),
+ c_real real,
+ c_double double precision,
+ c_money money,
+ c_char char,
+ c_char_n char(5),
+ c_varchar varchar,
+ c_varchar_n varchar(20) DEFAULT 'hello',
+ c_text text COLLATE "C",
+ c_bytea bytea,
+ c_bool boolean DEFAULT true,
+ c_date date,
+ c_time time,
+ c_time_p time(3),
+ c_timetz time with time zone,
+ c_timetz_p time(5) with time zone,
+ c_ts timestamp,
+ c_ts_p timestamp(2),
+ c_tstz timestamp with time zone DEFAULT now(),
+ c_tstz_p timestamp(4) with time zone,
+ c_interval interval,
+ c_interval_p interval(3),
+ c_uuid uuid,
+ c_json json,
+ c_jsonb jsonb DEFAULT '{}'::jsonb,
+ c_xml xml,
+ c_inet inet,
+ c_cidr cidr,
+ c_macaddr macaddr,
+ c_bit bit,
+ c_bit_n bit(3),
+ c_varbit varbit,
+ c_varbit_n varbit(8),
+ c_int_array integer[],
+ c_text_matrix text[][],
+ c_varchar_array varchar(5)[],
+ c_enum app.mood DEFAULT 'happy',
+ c_enum_public public.shirt_size,
+ c_enum_array app.mood[],
+ c_domain app.positive_int,
+ c_domain_varchar public.us_zip DEFAULT 'x',
+ c_domain_array app.int_array,
+ c_point point,
+ c_tsvector tsvector,
+ c_int4range int4range,
+ c_oid oid,
+ c_name name,
+ c_internal_char "char",
+ c_generated integer GENERATED ALWAYS AS (c_int * 2) STORED,
+ c_drop_me text
+);
+ALTER TABLE app.kitchen_sink DROP COLUMN c_drop_me;
+COMMENT ON COLUMN app.kitchen_sink.c_int IS 'int comment';
+
+CREATE TABLE app.parted (id integer, region text NOT NULL) PARTITION BY LIST (region);
+CREATE TABLE app.parted_us PARTITION OF app.parted FOR VALUES IN ('us');
+
+CREATE VIEW app.v_sink AS SELECT id, c_int, c_text FROM app.kitchen_sink;
+
+CREATE TABLE public.plain (a serial, b text);
+`
+ _, err := pgDB.Exec(setupSQL)
+ require.NoError(t, err)
+
+ txn, err := pgDB.Begin()
+ require.NoError(t, err)
+ defer txn.Rollback()
+
+ // SyncDBSchema clears the search path before the column sync; match it so default
+ // expressions render identically.
+ require.NoError(t, setTxSearchPath(txn, ""))
+
+ legacyRows := queryColumnRows(t, txn, legacyListColumnQuery)
+ newRows := queryColumnRows(t, txn, getListColumnQuery(true))
+
+ // Sanity: the kitchen sink table alone has 50+ columns.
+ require.Greater(t, len(legacyRows), 50)
+ require.Equal(t, legacyRows, newRows)
+}
+
+// TestSyncColumnsWithoutTablePrivilege verifies that column sync no longer depends on the
+// sync user's table privileges. information_schema.columns privilege-filters its rows, so
+// a sync user without privileges on a table used to get table metadata with indexes and
+// constraints (read from pg_catalog) but zero columns. The pg_catalog-based column query
+// must return the columns regardless.
+func TestSyncColumnsWithoutTablePrivilege(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.SharedPgContainer(t)
+ dbName, pgDB := testcontainer.NewPgDatabase(t)
+
+ setupSQL := `
+CREATE SCHEMA restricted;
+CREATE TABLE restricted.secret_table (
+ id integer PRIMARY KEY,
+ name varchar(50) NOT NULL DEFAULT 'anon',
+ amount numeric(10, 2),
+ CONSTRAINT positive_amount CHECK (amount >= 0)
+);
+CREATE INDEX idx_secret_name ON restricted.secret_table (name);
+`
+ _, err := pgDB.Exec(setupSQL)
+ require.NoError(t, err)
+
+ // pg_read_all_stats lets the sync read pg_table_size/pg_indexes_size; it grants no
+ // table or column privileges, so information_schema.columns still hides the table.
+ limitedUser := "limited_" + dbName
+ _, err = pgDB.Exec(fmt.Sprintf("CREATE USER %s WITH LOGIN PASSWORD 'limited-password'; GRANT pg_read_all_stats TO %s;", limitedUser, limitedUser))
+ require.NoError(t, err)
+
+ limitedDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=%s password=limited-password database=%s", pgContainer.GetHost(), pgContainer.GetPort(), limitedUser, dbName))
+ require.NoError(t, err)
+ defer limitedDB.Close()
+ require.NoError(t, limitedDB.Ping())
+
+ // Confirm the privilege filtering this fix works around: information_schema.columns
+ // returns nothing for the table when queried by the unprivileged user.
+ var count int
+ require.NoError(t, limitedDB.QueryRow(`
+ SELECT COUNT(*) FROM information_schema.columns
+ WHERE table_schema = 'restricted' AND table_name = 'secret_table'`).Scan(&count))
+ require.Zero(t, count)
+
+ driver := &Driver{}
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: limitedUser,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Database: dbName,
+ },
+ Password: "limited-password",
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: "16.0",
+ DatabaseName: dbName,
+ },
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
+ require.NoError(t, err)
+ defer openedDriver.Close(ctx)
+
+ pgDriver, ok := openedDriver.(*Driver)
+ require.True(t, ok)
+
+ metadata, err := pgDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ var secretTable *storepb.TableMetadata
+ for _, schema := range metadata.Schemas {
+ if schema.Name != "restricted" {
+ continue
+ }
+ for _, table := range schema.Tables {
+ if table.Name == "secret_table" {
+ secretTable = table
+ break
+ }
+ }
+ }
+ require.NotNil(t, secretTable, "restricted.secret_table not found in synced metadata")
+
+ // Indexes and check constraints were always visible via pg_catalog; columns must now
+ // be visible too instead of leaving inconsistent table metadata.
+ require.NotEmpty(t, secretTable.Indexes)
+ require.NotEmpty(t, secretTable.CheckConstraints)
+
+ require.Len(t, secretTable.Columns, 3)
+ id, name, amount := secretTable.Columns[0], secretTable.Columns[1], secretTable.Columns[2]
+
+ require.Equal(t, "id", id.Name)
+ require.Equal(t, "integer", id.Type)
+ require.False(t, id.Nullable)
+
+ require.Equal(t, "name", name.Name)
+ require.Equal(t, "character varying(50)", name.Type)
+ require.False(t, name.Nullable)
+ require.Equal(t, "'anon'::character varying", name.Default)
+
+ require.Equal(t, "amount", amount.Name)
+ require.Equal(t, "numeric(10,2)", amount.Type)
+ require.True(t, amount.Nullable)
+}
+
+func TestSync_ColumnDefaultSchemaQualification(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.SharedPgContainer(t)
+ dbName, pgDB := testcontainer.NewPgDatabase(t)
+
+ // Set up test schema with various default value scenarios
+ setupSQL := `
+-- Create enum type for testing schema qualification
+CREATE TYPE test_size AS ENUM ('small', 'medium', 'large');
+
+-- Create sequence for testing schema qualification
+CREATE SEQUENCE test_sequence START 1;
+
+-- Create test table with various default scenarios
+CREATE TABLE test_defaults (
+ -- No default
+ id INTEGER,
+
+ -- NULL default
+ col_null_default TEXT DEFAULT NULL,
+
+ -- String literal with schema-qualified type
+ col_string_literal VARCHAR(50) DEFAULT 'hello',
+
+ -- Numeric literal
+ col_numeric INTEGER DEFAULT 42,
+
+ -- Boolean literal
+ col_boolean BOOLEAN DEFAULT true,
+
+ -- Function expression
+ col_function TIMESTAMP DEFAULT now(),
+
+ -- Expression
+ col_expression INTEGER DEFAULT (10 + 20),
+
+ -- SERIAL (creates sequence automatically)
+ col_serial SERIAL,
+
+ -- Enum default (should be schema-qualified)
+ col_enum test_size DEFAULT 'medium',
+
+ -- Custom sequence (should be schema-qualified)
+ col_sequence INTEGER DEFAULT nextval('test_sequence'),
+
+ -- Array default
+ col_array INTEGER[] DEFAULT '{1,2,3}',
+
+ -- JSON default
+ col_json JSONB DEFAULT '{"key": "value"}',
+
+ -- Binary default
+ col_binary BYTEA DEFAULT '\xDEADBEEF'
+);
+
+-- Add comment to help identify the table
+COMMENT ON TABLE test_defaults IS 'Test table for column default schema qualification';
+`
+
+ // Execute the DDL
+ _, err := pgDB.Exec(setupSQL)
+ require.NoError(t, err)
+
+ // Create driver and get metadata using SyncDBSchema
+ driver := &Driver{}
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "postgres",
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Database: dbName,
+ },
+ Password: "root-password",
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: "16.0",
+ DatabaseName: dbName,
+ },
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, openedDriver.Close(ctx)) })
+
+ pgDriver, ok := openedDriver.(*Driver)
+ require.True(t, ok)
+
+ // Run sync operation to get metadata
+ metadata, err := pgDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ require.NotNil(t, metadata)
+
+ // Find our test table
+ var testTable *storepb.TableMetadata
+ for _, schema := range metadata.Schemas {
+ if schema.Name == "public" {
+ for _, table := range schema.Tables {
+ if table.Name == "test_defaults" {
+ testTable = table
+ break
+ }
+ }
+ }
+ }
+ require.NotNil(t, testTable, "test_defaults table should be found")
+
+ // Create a map for easier column lookup
+ columnMap := make(map[string]*storepb.ColumnMetadata)
+ for _, col := range testTable.Columns {
+ columnMap[col.Name] = col
+ }
+
+ // Test cases to verify schema qualification
+ testCases := []struct {
+ columnName string
+ expectedDefault string
+ description string
+ }{
+ {
+ columnName: "id",
+ expectedDefault: "",
+ description: "No default should be empty string",
+ },
+ {
+ columnName: "col_null_default",
+ expectedDefault: "",
+ description: "DEFAULT NULL should be empty string (PostgreSQL limitation)",
+ },
+ {
+ columnName: "col_string_literal",
+ expectedDefault: "'hello'::character varying",
+ description: "String literal should have type cast",
+ },
+ {
+ columnName: "col_numeric",
+ expectedDefault: "42",
+ description: "Numeric literal should be unquoted",
+ },
+ {
+ columnName: "col_boolean",
+ expectedDefault: "true",
+ description: "Boolean literal should be unquoted",
+ },
+ {
+ columnName: "col_function",
+ expectedDefault: "now()",
+ description: "Function should be preserved",
+ },
+ {
+ columnName: "col_expression",
+ expectedDefault: "(10 + 20)",
+ description: "Expression should preserve parentheses",
+ },
+ {
+ columnName: "col_enum",
+ expectedDefault: "'medium'::public.test_size",
+ description: "Enum default should be SCHEMA-QUALIFIED with public.test_size",
+ },
+ {
+ columnName: "col_sequence",
+ expectedDefault: "nextval('public.test_sequence'::regclass)",
+ description: "Custom sequence should be SCHEMA-QUALIFIED with public.test_sequence",
+ },
+ {
+ columnName: "col_array",
+ expectedDefault: "'{1,2,3}'::integer[]",
+ description: "Array should have type cast",
+ },
+ {
+ columnName: "col_json",
+ expectedDefault: "'{\"key\": \"value\"}'::jsonb",
+ description: "JSONB should have type cast",
+ },
+ {
+ columnName: "col_binary",
+ expectedDefault: "'\\xdeadbeef'::bytea",
+ description: "Binary should have type cast and lowercase hex",
+ },
+ }
+
+ // Verify each test case
+ for _, tc := range testCases {
+ t.Run(tc.columnName, func(t *testing.T) {
+ t.Parallel()
+ column, exists := columnMap[tc.columnName]
+ require.True(t, exists, "Column %s should exist", tc.columnName)
+
+ require.Equal(t, tc.expectedDefault, column.Default,
+ "Column %s: %s. Expected: %q, Got: %q",
+ tc.columnName, tc.description, tc.expectedDefault, column.Default)
+ })
+ }
+
+ // Special test: Verify SERIAL column has schema-qualified sequence
+ serialColumn, exists := columnMap["col_serial"]
+ require.True(t, exists, "col_serial should exist")
+ require.Contains(t, serialColumn.Default, "public.test_defaults_col_serial_seq",
+ "SERIAL column should have schema-qualified sequence name. Got: %s", serialColumn.Default)
+ require.Contains(t, serialColumn.Default, "nextval(",
+ "SERIAL column should use nextval function. Got: %s", serialColumn.Default)
+
+ // Verify that Default field is being used (consolidation from DefaultNull and DefaultExpression)
+ for columnName, column := range columnMap {
+ // The Default field should be used instead of the deprecated DefaultExpression and DefaultNull fields
+ if column.Default != "" {
+ t.Logf("Column %s uses Default field: %s", columnName, column.Default)
+ }
+ }
+}
+
+func TestSync_ColumnDefaultCrossSchemaQualification(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.SharedPgContainer(t)
+ dbName, pgDB := testcontainer.NewPgDatabase(t)
+
+ // Create schema with cross-schema references
+ setupSQL := `
+-- Create custom schema to test cross-schema qualification
+CREATE SCHEMA custom_schema;
+
+-- Create enum in custom schema
+CREATE TYPE custom_schema.status_type AS ENUM ('active', 'inactive', 'pending');
+
+-- Create sequence in custom schema
+CREATE SEQUENCE custom_schema.my_sequence START 100;
+
+-- Create function in custom schema for testing
+CREATE OR REPLACE FUNCTION custom_schema.get_prefix() RETURNS TEXT AS $$
+BEGIN
+ RETURN 'prefix_value';
+END;
+$$ LANGUAGE plpgsql IMMUTABLE;
+
+-- Test table in public schema with references to custom schema objects
+CREATE TABLE critical_test (
+ -- Cross-schema enum reference
+ status custom_schema.status_type DEFAULT 'active',
+
+ -- Cross-schema sequence reference
+ counter INTEGER DEFAULT nextval('custom_schema.my_sequence'),
+
+ -- Complex expression with cross-schema function
+ computed TEXT DEFAULT custom_schema.get_prefix()
+);
+`
+
+ // Execute the DDL
+ _, err := pgDB.Exec(setupSQL)
+ require.NoError(t, err)
+
+ // Create driver and get metadata
+ driver := &Driver{}
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "postgres",
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Database: dbName,
+ },
+ Password: "root-password",
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: "16.0",
+ DatabaseName: dbName,
+ },
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
+ require.NoError(t, err)
+ defer openedDriver.Close(ctx)
+
+ pgDriver, ok := openedDriver.(*Driver)
+ require.True(t, ok)
+
+ // Run sync operation
+ metadata, err := pgDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Find the test table
+ var testTable *storepb.TableMetadata
+ for _, schema := range metadata.Schemas {
+ if schema.Name == "public" {
+ for _, table := range schema.Tables {
+ if table.Name == "critical_test" {
+ testTable = table
+ break
+ }
+ }
+ }
+ }
+ require.NotNil(t, testTable)
+
+ // Verify cross-schema qualification
+ columnMap := make(map[string]*storepb.ColumnMetadata)
+ for _, col := range testTable.Columns {
+ columnMap[col.Name] = col
+ }
+
+ // Critical test: Cross-schema enum should be fully qualified
+ statusCol := columnMap["status"]
+ require.NotNil(t, statusCol)
+ require.Equal(t, "'active'::custom_schema.status_type", statusCol.Default,
+ "Cross-schema enum should be fully qualified")
+
+ // Critical test: Cross-schema sequence should be fully qualified
+ counterCol := columnMap["counter"]
+ require.NotNil(t, counterCol)
+ require.Equal(t, "nextval('custom_schema.my_sequence'::regclass)", counterCol.Default,
+ "Cross-schema sequence should be fully qualified")
+
+ // Critical test: Complex expression with cross-schema function
+ computedCol := columnMap["computed"]
+ require.NotNil(t, computedCol)
+ require.Equal(t, "custom_schema.get_prefix()", computedCol.Default,
+ "Cross-schema function should be fully qualified")
+
+ // Cleanup: Drop test objects to avoid interference with other tests
+ cleanupSQL := `
+ DROP TABLE IF EXISTS critical_test CASCADE;
+ DROP TABLE IF EXISTS test_defaults CASCADE;
+ DROP SCHEMA IF EXISTS custom_schema CASCADE;
+ DROP TYPE IF EXISTS test_size CASCADE;
+ DROP SEQUENCE IF EXISTS test_sequence CASCADE;
+ `
+ _, err = pgDB.Exec(cleanupSQL)
+ require.NoError(t, err)
+}
+
+// TestSyncCompositeTypes pins the exact metadata sync produces for composite
+// types. The definition round-trip in backend/plugin/schema/pg compares two
+// synced databases against each other, so a sync that is wrong the same way on
+// both sides passes it vacuously; these assertions do not.
+func TestSyncCompositeTypes(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.SharedPgContainer(t)
+ dbName, pgDB := testcontainer.NewPgDatabase(t)
+
+ _, err := pgDB.ExecContext(ctx, `
+CREATE TYPE zz_base AS (street text COLLATE "C", city varchar(50));
+COMMENT ON TYPE zz_base IS 'base address type';
+COMMENT ON COLUMN zz_base.street IS 'street line';
+CREATE TYPE aa_nested AS (home zz_base, homes zz_base[], tags text[]);
+CREATE TYPE empty_type AS ();
+CREATE SCHEMA geo;
+CREATE TYPE geo.point2 AS (lat numeric(9,6), lng numeric(9,6));
+CREATE TYPE wrapper AS (p geo.point2);
+CREATE TABLE plain_table (id int);
+CREATE SCHEMA locale;
+CREATE COLLATION locale.mycoll (locale = 'C');
+CREATE TYPE coll_qualified AS (v text COLLATE locale.mycoll);
+`)
+ require.NoError(t, err)
+
+ driver := &Driver{}
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "postgres",
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Database: dbName,
+ },
+ Password: "root-password",
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: "16.0",
+ DatabaseName: dbName,
+ },
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, openedDriver.Close(ctx)) })
+
+ pgDriver, ok := openedDriver.(*Driver)
+ require.True(t, ok)
+
+ metadata, err := pgDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ compositesBySchema := make(map[string]map[string]*storepb.CompositeTypeMetadata)
+ for _, schemaMeta := range metadata.Schemas {
+ m := make(map[string]*storepb.CompositeTypeMetadata)
+ for _, composite := range schemaMeta.CompositeTypes {
+ m[composite.Name] = composite
+ }
+ compositesBySchema[schemaMeta.Name] = m
+ }
+
+ public := compositesBySchema["public"]
+ require.Len(t, public, 5, "public schema should have exactly the created composite types")
+ require.NotContains(t, public, "plain_table", "table row types must not be synced as composite types")
+
+ collQualified := public["coll_qualified"]
+ require.NotNil(t, collQualified)
+ require.Len(t, collQualified.Attributes, 1)
+ require.Equal(t, "locale.mycoll", collQualified.Attributes[0].Collation,
+ "non-pg_catalog collations must be schema-qualified")
+
+ base := public["zz_base"]
+ require.NotNil(t, base)
+ require.Equal(t, "base address type", base.Comment)
+ require.Len(t, base.Attributes, 2)
+ require.Equal(t, "street", base.Attributes[0].Name)
+ require.Equal(t, "text", base.Attributes[0].Type)
+ require.Equal(t, `"C"`, base.Attributes[0].Collation)
+ require.Equal(t, "street line", base.Attributes[0].Comment)
+ require.Equal(t, "city", base.Attributes[1].Name)
+ require.Equal(t, "character varying(50)", base.Attributes[1].Type)
+ require.Empty(t, base.Attributes[1].Collation)
+
+ nested := public["aa_nested"]
+ require.NotNil(t, nested)
+ require.Len(t, nested.Attributes, 3)
+ require.Equal(t, "public.zz_base", nested.Attributes[0].Type, "user-defined attribute types must be schema-qualified")
+ require.Equal(t, "public.zz_base[]", nested.Attributes[1].Type)
+ require.Equal(t, "text[]", nested.Attributes[2].Type)
+
+ empty := public["empty_type"]
+ require.NotNil(t, empty, "zero-attribute composite types must be synced")
+ require.Empty(t, empty.Attributes)
+
+ wrapper := public["wrapper"]
+ require.NotNil(t, wrapper)
+ require.Len(t, wrapper.Attributes, 1)
+ require.Equal(t, "geo.point2", wrapper.Attributes[0].Type, "cross-schema attribute types must be schema-qualified")
+
+ geo := compositesBySchema["geo"]
+ require.Len(t, geo, 1)
+ require.NotNil(t, geo["point2"])
+}
+
+// legacyListForeignTableQuery is the previous information_schema.foreign_tables-based
+// query, kept as the oracle: for a privileged user the pg_catalog-based query must
+// produce identical rows.
+const legacyListForeignTableQuery = `SELECT
+ foreign_table.foreign_table_schema,
+ foreign_table.foreign_table_name,
+ foreign_table.foreign_server_catalog,
+ foreign_table.foreign_server_name
+FROM information_schema.foreign_tables AS foreign_table
+ORDER BY foreign_table.foreign_table_schema, foreign_table.foreign_table_name;`
+
+const foreignTableSetupSQL = `
+CREATE EXTENSION postgres_fdw;
+CREATE SERVER loopback_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'postgres');
+CREATE SCHEMA fdw_schema;
+CREATE FOREIGN TABLE fdw_schema.remote_orders (
+ id integer NOT NULL,
+ note varchar(30)
+) SERVER loopback_server OPTIONS (schema_name 'public', table_name 'orders');
+CREATE FOREIGN TABLE public.remote_items (
+ item_id bigint
+) SERVER loopback_server OPTIONS (schema_name 'public', table_name 'items');
+`
+
+func queryForeignTableRows(t *testing.T, conn *sql.DB, query string) [][4]string {
+ t.Helper()
+ rows, err := conn.Query(query)
+ require.NoError(t, err)
+ defer rows.Close()
+
+ var result [][4]string
+ for rows.Next() {
+ var r [4]string
+ require.NoError(t, rows.Scan(&r[0], &r[1], &r[2], &r[3]))
+ result = append(result, r)
+ }
+ require.NoError(t, rows.Err())
+ return result
+}
+
+// TestListForeignTableQueryMatchesInformationSchema verifies that the pg_catalog-based
+// foreign table query produces exactly the same rows as the previous
+// information_schema.foreign_tables-based query for a privileged user.
+func TestListForeignTableQueryMatchesInformationSchema(t *testing.T) {
+ t.Parallel()
+
+ _, pgDB := testcontainer.NewPgDatabase(t)
+
+ _, err := pgDB.Exec(foreignTableSetupSQL)
+ require.NoError(t, err)
+
+ legacyRows := queryForeignTableRows(t, pgDB, legacyListForeignTableQuery)
+ newRows := queryForeignTableRows(t, pgDB, getListForeignTableQuery())
+
+ require.Len(t, legacyRows, 2)
+ require.Equal(t, legacyRows, newRows)
+}
+
+// TestSyncForeignTablesWithoutTablePrivilege verifies that foreign table sync no longer
+// depends on the sync user's privileges. information_schema.foreign_tables
+// privilege-filters its rows, so a sync user without privileges on a foreign table used
+// to silently lose the whole table from the synced metadata.
+func TestSyncForeignTablesWithoutTablePrivilege(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ pgContainer := testcontainer.SharedPgContainer(t)
+ dbName, pgDB := testcontainer.NewPgDatabase(t)
+
+ _, err := pgDB.Exec(foreignTableSetupSQL)
+ require.NoError(t, err)
+
+ // pg_read_all_stats lets the sync read pg_table_size/pg_indexes_size; it grants no
+ // table or column privileges, so information_schema.foreign_tables still filters.
+ limitedUser := "limited_" + dbName
+ _, err = pgDB.Exec(fmt.Sprintf("CREATE USER %s WITH LOGIN PASSWORD 'limited-password'; GRANT pg_read_all_stats TO %s;", limitedUser, limitedUser))
+ require.NoError(t, err)
+
+ limitedDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=%s password=limited-password database=%s", pgContainer.GetHost(), pgContainer.GetPort(), limitedUser, dbName))
+ require.NoError(t, err)
+ defer limitedDB.Close()
+ require.NoError(t, limitedDB.Ping())
+
+ // Confirm the privilege filtering this fix works around: the unprivileged user sees
+ // no foreign tables through information_schema.
+ var count int
+ require.NoError(t, limitedDB.QueryRow(`SELECT COUNT(*) FROM information_schema.foreign_tables`).Scan(&count))
+ require.Zero(t, count)
+
+ driver := &Driver{}
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: limitedUser,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
+ Database: dbName,
+ },
+ Password: "limited-password",
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: "16.0",
+ DatabaseName: dbName,
+ },
+ }
+
+ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
+ require.NoError(t, err)
+ defer openedDriver.Close(ctx)
+
+ pgDriver, ok := openedDriver.(*Driver)
+ require.True(t, ok)
+
+ metadata, err := pgDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ var fdwSchema *storepb.SchemaMetadata
+ for _, schema := range metadata.Schemas {
+ if schema.Name == "fdw_schema" {
+ fdwSchema = schema
+ break
+ }
+ }
+ require.NotNil(t, fdwSchema, "fdw_schema not found in synced metadata")
+ require.Len(t, fdwSchema.ExternalTables, 1)
+
+ remoteOrders := fdwSchema.ExternalTables[0]
+ require.Equal(t, "remote_orders", remoteOrders.Name)
+ require.Equal(t, "loopback_server", remoteOrders.ExternalServerName)
+ require.Equal(t, dbName, remoteOrders.ExternalDatabaseName)
+
+ // Columns of foreign tables come from the privilege-independent column sync.
+ require.Len(t, remoteOrders.Columns, 2)
+ require.Equal(t, "id", remoteOrders.Columns[0].Name)
+ require.Equal(t, "integer", remoteOrders.Columns[0].Type)
+ require.False(t, remoteOrders.Columns[0].Nullable)
+ require.Equal(t, "note", remoteOrders.Columns[1].Name)
+ require.Equal(t, "character varying(30)", remoteOrders.Columns[1].Type)
+}
diff --git a/backend/plugin/db/starrocks/antlr_dependency_test.go b/backend/plugin/db/starrocks/antlr_dependency_test.go
deleted file mode 100644
index 55175ef025..0000000000
--- a/backend/plugin/db/starrocks/antlr_dependency_test.go
+++ /dev/null
@@ -1,55 +0,0 @@
-package starrocks
-
-import (
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestStarRocksQueryDoesNotUseANTLROrMySQLParser(t *testing.T) {
- content, err := os.ReadFile("query.go")
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")
- require.NotContains(t, source, "github.com/bytebase/parser/mysql")
- require.NotContains(t, source, "ParseMySQL(")
- require.NotContains(t, source, "TokenStreamRewriter")
- require.NotContains(t, source, "BaseMySQLParserListener")
- require.NotContains(t, source, "omni/mysql/ast")
- require.NotContains(t, source, "omni/mysql/parser")
- require.NotContains(t, source, "plugin/parser/mysql")
-}
-
-func TestStarRocksDriverDoesNotUseANTLR(t *testing.T) {
- content, err := os.ReadFile("starrocks.go")
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")
- require.NotContains(t, source, "github.com/bytebase/parser/mysql")
- require.NotContains(t, source, "plugin/parser/mysql")
-}
-
-func TestContainsDelimiterDirective(t *testing.T) {
- tests := []struct {
- name string
- sql string
- want bool
- }{
- {"basic directive", "DELIMITER ;;\nCREATE PROCEDURE p() BEGIN SELECT 1; END;;\nDELIMITER ;", true},
- {"indented directive", " DELIMITER //\nSELECT 1//\nDELIMITER ;", true},
- {"tab-indented directive", "\tDELIMITER $$\n", true},
- {"string literal not a directive", "INSERT INTO t VALUES ('DELIMITER ');\nSELECT 1;", false},
- {"comment not a directive", "-- DELIMITER //\nSELECT 1;", false},
- {"column name not a directive", "SELECT delimiter FROM t;", false},
- {"no delimiter at all", "SELECT 1;\nSELECT 2;", false},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got := containsDelimiterDirective(tt.sql)
- require.Equal(t, tt.want, got)
- })
- }
-}
diff --git a/backend/plugin/db/starrocks/dispatch_test.go b/backend/plugin/db/starrocks/dispatch_test.go
deleted file mode 100644
index 3d6a4063bc..0000000000
--- a/backend/plugin/db/starrocks/dispatch_test.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package starrocks
-
-import (
- "testing"
-
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-
- // Register the doris + starrocks editor validators.
- _ "github.com/bytebase/bytebase/backend/plugin/parser/doris"
- _ "github.com/bytebase/bytebase/backend/plugin/parser/starrocks"
-)
-
-// TestValidateSQLForEditor_EngineDispatch guards the #20562 P2 fix: QueryConn
-// must split/validate via the connection's engine (d.dbType), not a hardcoded
-// Engine_DORIS. On a StarRocks-only statement the two engines must disagree —
-// doris rejects it (→ allQuery forced true, DDL wrongly routed through
-// QueryContext), starrocks accepts it (→ correct allQuery=false).
-func TestValidateSQLForEditor_EngineDispatch(t *testing.T) {
- // StarRocks generated column AS (expr): doris wants GENERATED ALWAYS AS.
- const ddl = "CREATE TABLE t (a INT, b INT AS (a + 1)) DUPLICATE KEY(a) DISTRIBUTED BY HASH(a)"
-
- _, srAllQuery, srErr := base.ValidateSQLForEditor(storepb.Engine_STARROCKS, ddl)
- require.NoError(t, srErr, "starrocks parser should accept StarRocks DDL")
- require.False(t, srAllQuery, "a CREATE TABLE is DDL, not a read-only query")
-
- _, _, dorisErr := base.ValidateSQLForEditor(storepb.Engine_DORIS, ddl)
- require.Error(t, dorisErr, "doris parser rejects StarRocks AS(expr) DDL — why d.dbType matters")
-}
diff --git a/backend/plugin/db/starrocks/dump_materialized_view_test.go b/backend/plugin/db/starrocks/dump_materialized_view_test.go
deleted file mode 100644
index 34a31ed117..0000000000
--- a/backend/plugin/db/starrocks/dump_materialized_view_test.go
+++ /dev/null
@@ -1,60 +0,0 @@
-package starrocks
-
-import (
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-// BYT-9689: the dump path re-tags materialized-view rows so it emits
-// SHOW CREATE MATERIALIZED VIEW for them. Doris reports MVs as 'BASE TABLE',
-// StarRocks as 'VIEW'; both must become 'MATERIALIZED VIEW' when the name is a known
-// MV, while regular tables and views are left untouched.
-func TestMarkMaterializedViews(t *testing.T) {
- tables := []*TableSchema{
- {Name: "mv_doris", TableType: baseTableType}, // Doris MV reported as BASE TABLE
- {Name: "mv_starrocks", TableType: viewTableType}, // StarRocks MV reported as VIEW
- {Name: "v_regular", TableType: viewTableType}, // regular view (not an MV)
- {Name: "t_plain", TableType: baseTableType}, // plain table
- }
- markMaterializedViews(tables, map[string]bool{"mv_doris": true, "mv_starrocks": true})
-
- require.Equal(t, materializedViewType, tables[0].TableType)
- require.Equal(t, materializedViewType, tables[1].TableType)
- require.Equal(t, viewTableType, tables[2].TableType, "regular view must stay a view")
- require.Equal(t, baseTableType, tables[3].TableType, "plain table must stay a table")
-}
-
-// BYT-9689: the dump emits a temporary regular-view placeholder for every view AND
-// materialized view (getTemporaryMaterializedView creates a CREATE VIEW), then drops it
-// before emitting the real definition. The drop must therefore be DROP VIEW even for an
-// MV — DROP MATERIALIZED VIEW would not match the placeholder, leaving it in the shared
-// namespace so the real CREATE MATERIALIZED VIEW collides with it on replay.
-func TestMaterializedViewPlaceholderIsDroppedAsView(t *testing.T) {
- placeholder := getTemporaryMaterializedView("mv1", []string{"a"})
- require.Contains(t, placeholder, "CREATE VIEW `mv1`")
- require.NotContains(t, placeholder, "CREATE MATERIALIZED VIEW")
-
- require.Equal(t, "DROP VIEW IF EXISTS `mv1`;\n", dropPlaceholderStmt("mv1"))
-}
-
-// BYT-9689: information_schema.tables is unordered, so an MV can be listed before a view
-// it is defined on. A materialized view is materialized from its sources at CREATE time
-// (unlike a lazy regular view), so all regular views must be emitted — and on restore,
-// created real — before any materialized view; otherwise the MV would materialize from
-// the temporary SELECT 1 placeholder. finalEmitOrder partitions views ahead of MVs while
-// preserving each group's relative order, and drops base tables (emitted elsewhere).
-func TestFinalEmitOrder_ViewsBeforeMaterializedViews(t *testing.T) {
- tables := []*TableSchema{
- {Name: "mv1", TableType: materializedViewType},
- {Name: "v1", TableType: viewTableType},
- {Name: "t1", TableType: baseTableType},
- {Name: "mv2", TableType: materializedViewType},
- {Name: "v2", TableType: viewTableType},
- }
- var names []string
- for _, tbl := range finalEmitOrder(tables) {
- names = append(names, tbl.Name)
- }
- require.Equal(t, []string{"v1", "v2", "mv1", "mv2"}, names)
-}
diff --git a/backend/plugin/db/starrocks/dump_test.go b/backend/plugin/db/starrocks/dump_test.go
index 98db9ab3be..b38d5b6c9c 100644
--- a/backend/plugin/db/starrocks/dump_test.go
+++ b/backend/plugin/db/starrocks/dump_test.go
@@ -12,3 +12,56 @@ func TestGetTemporaryView(t *testing.T) {
want := "--\n-- Temporary view structure for `db1`\n--\nCREATE VIEW `db1` AS SELECT\n 1 AS `col1`,\n 1 AS `col2`;\n\n"
a.Equal(want, got)
}
+
+// BYT-9689: the dump path re-tags materialized-view rows so it emits
+// SHOW CREATE MATERIALIZED VIEW for them. Doris reports MVs as 'BASE TABLE',
+// StarRocks as 'VIEW'; both must become 'MATERIALIZED VIEW' when the name is a known
+// MV, while regular tables and views are left untouched.
+func TestMarkMaterializedViews(t *testing.T) {
+ tables := []*TableSchema{
+ {Name: "mv_doris", TableType: baseTableType}, // Doris MV reported as BASE TABLE
+ {Name: "mv_starrocks", TableType: viewTableType}, // StarRocks MV reported as VIEW
+ {Name: "v_regular", TableType: viewTableType}, // regular view (not an MV)
+ {Name: "t_plain", TableType: baseTableType}, // plain table
+ }
+ markMaterializedViews(tables, map[string]bool{"mv_doris": true, "mv_starrocks": true})
+
+ require.Equal(t, materializedViewType, tables[0].TableType)
+ require.Equal(t, materializedViewType, tables[1].TableType)
+ require.Equal(t, viewTableType, tables[2].TableType, "regular view must stay a view")
+ require.Equal(t, baseTableType, tables[3].TableType, "plain table must stay a table")
+}
+
+// BYT-9689: the dump emits a temporary regular-view placeholder for every view AND
+// materialized view (getTemporaryMaterializedView creates a CREATE VIEW), then drops it
+// before emitting the real definition. The drop must therefore be DROP VIEW even for an
+// MV — DROP MATERIALIZED VIEW would not match the placeholder, leaving it in the shared
+// namespace so the real CREATE MATERIALIZED VIEW collides with it on replay.
+func TestMaterializedViewPlaceholderIsDroppedAsView(t *testing.T) {
+ placeholder := getTemporaryMaterializedView("mv1", []string{"a"})
+ require.Contains(t, placeholder, "CREATE VIEW `mv1`")
+ require.NotContains(t, placeholder, "CREATE MATERIALIZED VIEW")
+
+ require.Equal(t, "DROP VIEW IF EXISTS `mv1`;\n", dropPlaceholderStmt("mv1"))
+}
+
+// BYT-9689: information_schema.tables is unordered, so an MV can be listed before a view
+// it is defined on. A materialized view is materialized from its sources at CREATE time
+// (unlike a lazy regular view), so all regular views must be emitted — and on restore,
+// created real — before any materialized view; otherwise the MV would materialize from
+// the temporary SELECT 1 placeholder. finalEmitOrder partitions views ahead of MVs while
+// preserving each group's relative order, and drops base tables (emitted elsewhere).
+func TestFinalEmitOrder_ViewsBeforeMaterializedViews(t *testing.T) {
+ tables := []*TableSchema{
+ {Name: "mv1", TableType: materializedViewType},
+ {Name: "v1", TableType: viewTableType},
+ {Name: "t1", TableType: baseTableType},
+ {Name: "mv2", TableType: materializedViewType},
+ {Name: "v2", TableType: viewTableType},
+ }
+ var names []string
+ for _, tbl := range finalEmitOrder(tables) {
+ names = append(names, tbl.Name)
+ }
+ require.Equal(t, []string{"v1", "v2", "mv1", "mv2"}, names)
+}
diff --git a/backend/plugin/db/starrocks/starrocks_test.go b/backend/plugin/db/starrocks/starrocks_test.go
index 968f892d82..d84a8c1fdc 100644
--- a/backend/plugin/db/starrocks/starrocks_test.go
+++ b/backend/plugin/db/starrocks/starrocks_test.go
@@ -4,6 +4,13 @@ import (
"testing"
"github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+
+ // Register the doris + starrocks editor validators.
+ _ "github.com/bytebase/bytebase/backend/plugin/parser/doris"
+ _ "github.com/bytebase/bytebase/backend/plugin/parser/starrocks"
)
func TestParseVersion(t *testing.T) {
@@ -42,3 +49,42 @@ func TestParseVersion(t *testing.T) {
a.Equal(tc.wantRest, rest)
}
}
+
+func TestContainsDelimiterDirective(t *testing.T) {
+ tests := []struct {
+ name string
+ sql string
+ want bool
+ }{
+ {"basic directive", "DELIMITER ;;\nCREATE PROCEDURE p() BEGIN SELECT 1; END;;\nDELIMITER ;", true},
+ {"indented directive", " DELIMITER //\nSELECT 1//\nDELIMITER ;", true},
+ {"tab-indented directive", "\tDELIMITER $$\n", true},
+ {"string literal not a directive", "INSERT INTO t VALUES ('DELIMITER ');\nSELECT 1;", false},
+ {"comment not a directive", "-- DELIMITER //\nSELECT 1;", false},
+ {"column name not a directive", "SELECT delimiter FROM t;", false},
+ {"no delimiter at all", "SELECT 1;\nSELECT 2;", false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := containsDelimiterDirective(tt.sql)
+ require.Equal(t, tt.want, got)
+ })
+ }
+}
+
+// TestValidateSQLForEditor_EngineDispatch guards the #20562 P2 fix: QueryConn
+// must split/validate via the connection's engine (d.dbType), not a hardcoded
+// Engine_DORIS. On a StarRocks-only statement the two engines must disagree —
+// doris rejects it (→ allQuery forced true, DDL wrongly routed through
+// QueryContext), starrocks accepts it (→ correct allQuery=false).
+func TestValidateSQLForEditor_EngineDispatch(t *testing.T) {
+ // StarRocks generated column AS (expr): doris wants GENERATED ALWAYS AS.
+ const ddl = "CREATE TABLE t (a INT, b INT AS (a + 1)) DUPLICATE KEY(a) DISTRIBUTED BY HASH(a)"
+
+ _, srAllQuery, srErr := base.ValidateSQLForEditor(storepb.Engine_STARROCKS, ddl)
+ require.NoError(t, srErr, "starrocks parser should accept StarRocks DDL")
+ require.False(t, srAllQuery, "a CREATE TABLE is DDL, not a read-only query")
+
+ _, _, dorisErr := base.ValidateSQLForEditor(storepb.Engine_DORIS, ddl)
+ require.Error(t, dorisErr, "doris parser rejects StarRocks AS(expr) DDL — why d.dbType matters")
+}
diff --git a/backend/plugin/db/starrocks/sync_systemdb_test.go b/backend/plugin/db/starrocks/sync_systemdb_test.go
deleted file mode 100644
index c983501999..0000000000
--- a/backend/plugin/db/starrocks/sync_systemdb_test.go
+++ /dev/null
@@ -1,24 +0,0 @@
-package starrocks
-
-import (
- "testing"
-
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-)
-
-// #20566 review (P2): SyncInstance's system-database exclusion must match the
-// parser's per-engine system set — StarRocks's read-only `sys` metadatabase
-// must be excluded so it isn't imported as a user database. Doris has no `sys`,
-// so excluding it there would risk hiding a user db named sys (engine-aware).
-func TestSystemDatabaseExclusion(t *testing.T) {
- sr := (&Driver{dbType: storepb.Engine_STARROCKS}).systemDatabaseExclusion()
- require.Contains(t, sr, "'sys'", "StarRocks sync must exclude the sys metadatabase")
- require.Contains(t, sr, "'information_schema'")
- require.Contains(t, sr, "'_statistics_'")
-
- doris := (&Driver{dbType: storepb.Engine_DORIS}).systemDatabaseExclusion()
- require.NotContains(t, doris, "'sys'", "Doris must not exclude sys (StarRocks-only)")
- require.Contains(t, doris, "'information_schema'")
-}
diff --git a/backend/plugin/db/starrocks/sync_materialized_view_test.go b/backend/plugin/db/starrocks/sync_test.go
similarity index 72%
rename from backend/plugin/db/starrocks/sync_materialized_view_test.go
rename to backend/plugin/db/starrocks/sync_test.go
index c8fa683e9e..0fe6c67f68 100644
--- a/backend/plugin/db/starrocks/sync_materialized_view_test.go
+++ b/backend/plugin/db/starrocks/sync_test.go
@@ -43,3 +43,18 @@ func TestIsSyncRollup(t *testing.T) {
require.False(t, isSyncRollup("MANUAL"))
require.False(t, isSyncRollup("")) // IFNULL-coalesced NULL or a genuinely empty type.
}
+
+// #20566 review (P2): SyncInstance's system-database exclusion must match the
+// parser's per-engine system set — StarRocks's read-only `sys` metadatabase
+// must be excluded so it isn't imported as a user database. Doris has no `sys`,
+// so excluding it there would risk hiding a user db named sys (engine-aware).
+func TestSystemDatabaseExclusion(t *testing.T) {
+ sr := (&Driver{dbType: storepb.Engine_STARROCKS}).systemDatabaseExclusion()
+ require.Contains(t, sr, "'sys'", "StarRocks sync must exclude the sys metadatabase")
+ require.Contains(t, sr, "'information_schema'")
+ require.Contains(t, sr, "'_statistics_'")
+
+ doris := (&Driver{dbType: storepb.Engine_DORIS}).systemDatabaseExclusion()
+ require.NotContains(t, doris, "'sys'", "Doris must not exclude sys (StarRocks-only)")
+ require.Contains(t, doris, "'information_schema'")
+}
diff --git a/backend/plugin/db/tidb/antlr_dependency_test.go b/backend/plugin/db/tidb/antlr_dependency_test.go
deleted file mode 100644
index d21e0d5a83..0000000000
--- a/backend/plugin/db/tidb/antlr_dependency_test.go
+++ /dev/null
@@ -1,20 +0,0 @@
-package tidb
-
-import (
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestTiDBExecuteDoesNotUseMySQLParserPackage(t *testing.T) {
- content, err := os.ReadFile("tidb.go")
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")
- require.NotContains(t, source, "github.com/bytebase/parser/mysql")
- require.NotContains(t, source, "plugin/parser/mysql\"")
- require.NotContains(t, source, "mysqlutil")
- require.NotContains(t, source, "DealWithDelimiter")
-}
diff --git a/backend/plugin/db/tidb/execute_testcontainer_test.go b/backend/plugin/db/tidb/execute_testcontainer_test.go
deleted file mode 100644
index adf97b3324..0000000000
--- a/backend/plugin/db/tidb/execute_testcontainer_test.go
+++ /dev/null
@@ -1,102 +0,0 @@
-package tidb
-
-import (
- "context"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-func openTestDriver(ctx context.Context, t *testing.T, container *testcontainer.Container) *Driver {
- t.Helper()
-
- driver := &Driver{}
- d, err := driver.Open(ctx, storepb.Engine_TIDB, db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: "root",
- Host: container.GetHost(),
- Port: container.GetPort(),
- },
- ConnectionContext: db.ConnectionContext{},
- })
- require.NoError(t, err)
-
- tidbDriver, ok := d.(*Driver)
- require.True(t, ok)
- return tidbDriver
-}
-
-func TestExecuteCreateIndexInTransaction(t *testing.T) {
- ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- defer container.Close(ctx)
-
- tidbDriver := openTestDriver(ctx, t, container)
- defer func() {
- require.NoError(t, tidbDriver.Close(ctx))
- }()
-
- _, err := tidbDriver.Execute(ctx, `
- CREATE DATABASE IF NOT EXISTS test;
- USE test;
- DROP TABLE IF EXISTS test.execute_create_index_in_transaction;
- CREATE TABLE test.execute_create_index_in_transaction (id INT);
- BEGIN;
- CREATE INDEX idx_execute_create_index_in_transaction ON test.execute_create_index_in_transaction(id);
- COMMIT;
- `, db.ExecuteOptions{})
- require.NoError(t, err)
-
- var count int
- query := `
- SELECT COUNT(*)
- FROM information_schema.tidb_indexes
- WHERE table_schema = 'test'
- AND table_name = 'execute_create_index_in_transaction'
- AND key_name = 'idx_execute_create_index_in_transaction'
- `
- err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
- require.NoError(t, err)
- require.Equal(t, 1, count)
-}
-
-func TestExecutePreparedStatementFlowWithCreateIndexString(t *testing.T) {
- ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- defer container.Close(ctx)
-
- tidbDriver := openTestDriver(ctx, t, container)
- defer func() {
- require.NoError(t, tidbDriver.Close(ctx))
- }()
-
- statement := `
- CREATE DATABASE IF NOT EXISTS test;
- USE test;
- DROP TABLE IF EXISTS test.prepare_statement_flow;
- CREATE TABLE test.prepare_statement_flow (id INT);
- SET @sql := 'CREATE INDEX idx_prepare_statement_flow ON test.prepare_statement_flow(id)';
- PREPARE stmt FROM @sql;
- EXECUTE stmt;
- DEALLOCATE PREPARE stmt;
- `
- _, err := tidbDriver.Execute(ctx, statement, db.ExecuteOptions{})
- require.NoError(t, err)
-
- var count int
- query := `
- SELECT COUNT(*)
- FROM information_schema.tidb_indexes
- WHERE table_schema = 'test'
- AND table_name = 'prepare_statement_flow'
- AND key_name = 'idx_prepare_statement_flow'
- `
- err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
- require.NoError(t, err)
- require.Equal(t, 1, count)
-}
diff --git a/backend/plugin/db/tidb/tidb_test.go b/backend/plugin/db/tidb/tidb_test.go
index d52434d2eb..b1d907aa92 100644
--- a/backend/plugin/db/tidb/tidb_test.go
+++ b/backend/plugin/db/tidb/tidb_test.go
@@ -1,12 +1,14 @@
package tidb
import (
+ "context"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -88,3 +90,93 @@ func TestBuildExecuteCommandsDoesNotNormalizeDelimiterForLargeSheet(t *testing.T
require.Len(t, commands, 1)
require.Equal(t, statement, commands[0].Text)
}
+
+func openTestDriver(ctx context.Context, t *testing.T, container *testcontainer.Container) *Driver {
+ t.Helper()
+
+ driver := &Driver{}
+ d, err := driver.Open(ctx, storepb.Engine_TIDB, db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "root",
+ Host: container.GetHost(),
+ Port: container.GetPort(),
+ },
+ ConnectionContext: db.ConnectionContext{},
+ })
+ require.NoError(t, err)
+
+ tidbDriver, ok := d.(*Driver)
+ require.True(t, ok)
+ return tidbDriver
+}
+
+func TestExecuteCreateIndexInTransaction(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.GetTestTiDBContainer(ctx, t)
+ defer container.Close(ctx)
+
+ tidbDriver := openTestDriver(ctx, t, container)
+ defer func() {
+ require.NoError(t, tidbDriver.Close(ctx))
+ }()
+
+ _, err := tidbDriver.Execute(ctx, `
+ CREATE DATABASE IF NOT EXISTS test;
+ USE test;
+ DROP TABLE IF EXISTS test.execute_create_index_in_transaction;
+ CREATE TABLE test.execute_create_index_in_transaction (id INT);
+ BEGIN;
+ CREATE INDEX idx_execute_create_index_in_transaction ON test.execute_create_index_in_transaction(id);
+ COMMIT;
+ `, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ var count int
+ query := `
+ SELECT COUNT(*)
+ FROM information_schema.tidb_indexes
+ WHERE table_schema = 'test'
+ AND table_name = 'execute_create_index_in_transaction'
+ AND key_name = 'idx_execute_create_index_in_transaction'
+ `
+ err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
+ require.NoError(t, err)
+ require.Equal(t, 1, count)
+}
+
+func TestExecutePreparedStatementFlowWithCreateIndexString(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.GetTestTiDBContainer(ctx, t)
+ defer container.Close(ctx)
+
+ tidbDriver := openTestDriver(ctx, t, container)
+ defer func() {
+ require.NoError(t, tidbDriver.Close(ctx))
+ }()
+
+ statement := `
+ CREATE DATABASE IF NOT EXISTS test;
+ USE test;
+ DROP TABLE IF EXISTS test.prepare_statement_flow;
+ CREATE TABLE test.prepare_statement_flow (id INT);
+ SET @sql := 'CREATE INDEX idx_prepare_statement_flow ON test.prepare_statement_flow(id)';
+ PREPARE stmt FROM @sql;
+ EXECUTE stmt;
+ DEALLOCATE PREPARE stmt;
+ `
+ _, err := tidbDriver.Execute(ctx, statement, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ var count int
+ query := `
+ SELECT COUNT(*)
+ FROM information_schema.tidb_indexes
+ WHERE table_schema = 'test'
+ AND table_name = 'prepare_statement_flow'
+ AND key_name = 'idx_prepare_statement_flow'
+ `
+ err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
+ require.NoError(t, err)
+ require.Equal(t, 1, count)
+}
diff --git a/backend/plugin/parser/mysql/antlr_dependency_test.go b/backend/plugin/parser/mysql/antlr_dependency_test.go
deleted file mode 100644
index 2dda560100..0000000000
--- a/backend/plugin/parser/mysql/antlr_dependency_test.go
+++ /dev/null
@@ -1,36 +0,0 @@
-package mysql
-
-import (
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestMySQLOmniASTDoesNotFallbackToANTLR(t *testing.T) {
- content, err := os.ReadFile("omni.go")
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "AsANTLRAST")
- require.NotContains(t, source, "AntlrASTProvider")
- require.NotContains(t, source, "base.ANTLRAST")
- require.NotContains(t, source, "antlrAST")
- require.NotContains(t, source, "parseSingleStatementLenient")
-}
-
-func TestMySQLDelimiterHandlingDoesNotUseLegacyTokenizer(t *testing.T) {
- content, err := os.ReadFile("mysql.go")
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")
- require.NotContains(t, source, "github.com/bytebase/parser/mysql")
- require.NotContains(t, source, "base.ANTLRAST")
- require.NotContains(t, source, "ParseMySQL(")
- require.NotContains(t, source, "parseSingleStatement")
- require.NotContains(t, source, "plugin/parser/tokenizer")
- require.NotContains(t, source, "SplitTiDBMultiSQL")
- require.NotContains(t, source, "mysqlutil")
- require.NotContains(t, source, "DealWithDelimiter")
-}
diff --git a/backend/plugin/parser/plsql/antlr_dependency_test.go b/backend/plugin/parser/plsql/antlr_dependency_test.go
deleted file mode 100644
index 5c2e45641b..0000000000
--- a/backend/plugin/parser/plsql/antlr_dependency_test.go
+++ /dev/null
@@ -1,32 +0,0 @@
-package plsql
-
-import (
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestOracleRuntimeEntrypointsDoNotFallbackToANTLR(t *testing.T) {
- files := []string{
- "diagnose.go",
- "omni.go",
- "plsql.go",
- "split.go",
- "statement_type.go",
- }
- for _, file := range files {
- t.Run(file, func(t *testing.T) {
- content, err := os.ReadFile(file)
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")
- require.NotContains(t, source, "github.com/bytebase/parser/plsql")
- require.NotContains(t, source, "ParsePLSQL(")
- require.NotContains(t, source, "ParsePLSQLForStringsManipulation")
- require.NotContains(t, source, "AsANTLRAST")
- require.NotContains(t, source, "GetANTLRAST")
- })
- }
-}
diff --git a/backend/plugin/parser/tsql/antlr_dependency_test.go b/backend/plugin/parser/tsql/antlr_dependency_test.go
deleted file mode 100644
index 95b6d7b163..0000000000
--- a/backend/plugin/parser/tsql/antlr_dependency_test.go
+++ /dev/null
@@ -1,33 +0,0 @@
-package tsql
-
-import (
- "os"
- "path/filepath"
- "strings"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestTSQLParserDoesNotDependOnANTLR(t *testing.T) {
- forbidden := []string{
- "github.com/antlr4-go/antlr/v4",
- "github.com/bytebase/parser/tsql",
- "ParseTSQL(",
- "AsANTLRAST(",
- }
-
- files, err := filepath.Glob("*.go")
- require.NoError(t, err)
- for _, path := range files {
- if strings.HasSuffix(path, "_test.go") {
- continue
- }
-
- source, err := os.ReadFile(path)
- require.NoError(t, err)
- for _, token := range forbidden {
- require.NotContains(t, string(source), token, path)
- }
- }
-}
diff --git a/backend/plugin/schema/mssql/antlr_dependency_test.go b/backend/plugin/schema/mssql/antlr_dependency_test.go
deleted file mode 100644
index 7bdf55cce7..0000000000
--- a/backend/plugin/schema/mssql/antlr_dependency_test.go
+++ /dev/null
@@ -1,33 +0,0 @@
-package mssql
-
-import (
- "os"
- "path/filepath"
- "strings"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestMSSQLSchemaMetadataDoesNotDependOnANTLR(t *testing.T) {
- forbidden := []string{
- "github.com/antlr4-go/antlr/v4",
- "github.com/bytebase/parser/tsql",
- "getDatabaseMetadataANTLR",
- "BaseTSqlParserListener",
- }
-
- files, err := filepath.Glob("*.go")
- require.NoError(t, err)
- for _, path := range files {
- if strings.HasSuffix(path, "_test.go") {
- continue
- }
-
- source, err := os.ReadFile(path)
- require.NoError(t, err)
- for _, token := range forbidden {
- require.NotContains(t, string(source), token, path)
- }
- }
-}
diff --git a/backend/plugin/schema/mssql/generate_migration_test.go b/backend/plugin/schema/mssql/generate_migration_test.go
index b02b276527..11aef06380 100644
--- a/backend/plugin/schema/mssql/generate_migration_test.go
+++ b/backend/plugin/schema/mssql/generate_migration_test.go
@@ -1,14 +1,25 @@
package mssql
import (
+ "context"
+ "fmt"
"io"
"os"
"path/filepath"
+ "slices"
+ "strings"
"testing"
+ // Import MSSQL driver
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/uuid"
+ _ "github.com/microsoft/go-mssqldb"
+ "github.com/pkg/errors"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/testing/protocmp"
"gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
"github.com/bytebase/bytebase/backend/store/model"
@@ -120,3 +131,2532 @@ func TestGenerateMigration_Views(t *testing.T) {
func TestGenerateMigration_SafeOrder(t *testing.T) {
runMigrationTest(t, "test_migration_safe_order.yaml")
}
+
+// TestGenerateMigrationWithTestcontainer tests the generate migration function
+// by applying migrations and rollback to verify the schema can be restored.
+//
+//nolint:tparallel
+func TestGenerateMigrationWithTestcontainer(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping MSSQL testcontainer test in short mode")
+ }
+
+ ctx := context.Background()
+
+ // Start shared MSSQL container for all subtests
+ container := testcontainer.SharedMSSQLContainer(t)
+
+ // Test cases with various schema changes
+ testCases := []struct {
+ name string
+ initialSchema string
+ migrationDDL string
+ description string
+ }{
+ {
+ name: "basic_table_operations",
+ initialSchema: `
+CREATE SCHEMA [app];
+GO
+
+CREATE TABLE [app].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [created_at] DATETIME2,
+ CONSTRAINT [uk_users_email] UNIQUE ([email])
+);
+
+CREATE INDEX [idx_users_username] ON [app].[users] ([username]);
+
+CREATE TABLE [app].[posts] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NTEXT,
+ [published_at] DATETIME2,
+ CONSTRAINT [fk_posts_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]) ON DELETE CASCADE
+);
+
+CREATE INDEX [idx_posts_user_id] ON [app].[posts] ([user_id]);
+`,
+ migrationDDL: `
+-- Add new column
+ALTER TABLE [app].[users] ADD [status] NVARCHAR(20);
+
+-- Add new table
+CREATE TABLE [app].[comments] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [post_id] INT NOT NULL,
+ [user_id] INT NOT NULL,
+ [content] NTEXT NOT NULL,
+ [created_at] DATETIME2,
+ CONSTRAINT [fk_comments_post] FOREIGN KEY ([post_id]) REFERENCES [app].[posts]([id]) ON DELETE CASCADE,
+ CONSTRAINT [fk_comments_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id])
+);
+
+-- Add index
+CREATE INDEX [idx_comments_post_id] ON [app].[comments] ([post_id]);
+
+-- Add check constraint
+ALTER TABLE [app].[users] ADD CONSTRAINT [ck_users_status] CHECK ([status] IN ('active', 'inactive', 'suspended'));
+`,
+ description: "Basic table operations with foreign keys, indexes, and constraints",
+ },
+ {
+ name: "schema_operations",
+ initialSchema: `
+CREATE SCHEMA [sales];
+GO
+
+CREATE TABLE [sales].[customers] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [email] NVARCHAR(100)
+);
+
+CREATE TABLE [sales].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [customer_id] INT NOT NULL,
+ [order_date] DATE,
+ [total] DECIMAL(10,2),
+ CONSTRAINT [fk_orders_customer] FOREIGN KEY ([customer_id]) REFERENCES [sales].[customers]([id])
+);
+`,
+ migrationDDL: `
+-- Create new schema
+CREATE SCHEMA [inventory];
+GO
+
+-- Create table in new schema
+CREATE TABLE [inventory].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [category_id] INT,
+ CONSTRAINT [ck_products_price] CHECK ([price] > 0)
+);
+
+-- Add reference to new table from existing schema
+ALTER TABLE [sales].[orders] ADD [product_id] INT;
+ALTER TABLE [sales].[orders] ADD CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [inventory].[products]([id]);
+`,
+ description: "Cross-schema operations with new schema creation",
+ },
+ {
+ name: "view_operations",
+ initialSchema: `
+CREATE SCHEMA [reporting];
+GO
+
+CREATE TABLE [reporting].[sales_data] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [product_name] NVARCHAR(100) NOT NULL,
+ [quantity] INT NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [sale_date] DATE
+);
+
+CREATE TABLE [reporting].[customers] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [region] NVARCHAR(50)
+);
+`,
+ migrationDDL: `
+-- Create basic view
+CREATE VIEW [reporting].[product_summary] AS
+SELECT
+ [product_name],
+ COUNT(*) as [sale_count],
+ SUM([quantity]) as [total_quantity],
+ AVG([price]) as [avg_price]
+FROM [reporting].[sales_data]
+GROUP BY [product_name];
+GO
+
+-- Create dependent view
+CREATE VIEW [reporting].[top_products] AS
+SELECT TOP 10
+ [product_name],
+ [total_quantity]
+FROM [reporting].[product_summary]
+WHERE [total_quantity] > 100
+ORDER BY [total_quantity] DESC;
+GO
+`,
+ description: "View creation with dependencies",
+ },
+ {
+ name: "function_and_procedure_operations",
+ initialSchema: `
+CREATE SCHEMA [calc];
+GO
+
+CREATE TABLE [calc].[numbers] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [value] INT NOT NULL,
+ [category] NVARCHAR(20)
+);
+`,
+ migrationDDL: `
+-- Create scalar function
+CREATE FUNCTION [calc].[square](@input INT)
+RETURNS INT
+AS
+BEGIN
+ RETURN @input * @input;
+END;
+GO
+
+-- Create table-valued function
+CREATE FUNCTION [calc].[get_numbers_by_category](@category NVARCHAR(20))
+RETURNS TABLE
+AS
+RETURN (
+ SELECT [id], [value], [category]
+ FROM [calc].[numbers]
+ WHERE [category] = @category
+);
+GO
+
+-- Create stored procedure
+CREATE PROCEDURE [calc].[add_number]
+ @value INT,
+ @category NVARCHAR(20)
+AS
+BEGIN
+ INSERT INTO [calc].[numbers] ([value], [category])
+ VALUES (@value, @category);
+END;
+GO
+`,
+ description: "Functions and procedures creation",
+ },
+ {
+ name: "index_operations",
+ initialSchema: `
+CREATE SCHEMA [perf];
+GO
+
+CREATE TABLE [perf].[events] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [event_name] NVARCHAR(100) NOT NULL,
+ [event_data] NVARCHAR(MAX),
+ [timestamp] DATETIME2,
+ [user_id] INT,
+ [category] NVARCHAR(50)
+);
+`,
+ migrationDDL: `
+-- Add various index types
+CREATE INDEX [idx_events_timestamp] ON [perf].[events] ([timestamp]);
+CREATE INDEX [idx_events_user_category] ON [perf].[events] ([user_id], [category]);
+CREATE UNIQUE INDEX [idx_events_name_timestamp] ON [perf].[events] ([event_name], [timestamp]);
+
+-- Add filtered index (using deterministic date)
+CREATE INDEX [idx_events_recent] ON [perf].[events] ([event_name])
+WHERE [timestamp] >= '2023-01-01';
+
+-- Add computed column first
+ALTER TABLE [perf].[events] ADD [event_month] AS DATEPART(MONTH, [timestamp]);
+
+-- Add index on computed column
+CREATE INDEX [idx_events_month] ON [perf].[events] ([event_month]);
+`,
+ description: "Various index types including filtered and computed column indexes",
+ },
+ {
+ name: "complex_constraints",
+ initialSchema: `
+CREATE SCHEMA [finance];
+GO
+
+CREATE TABLE [finance].[accounts] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [account_number] NVARCHAR(20) NOT NULL,
+ [balance] DECIMAL(15,2),
+ [account_type] NVARCHAR(20) NOT NULL,
+ [created_at] DATETIME2 DEFAULT GETDATE()
+);
+`,
+ migrationDDL: `
+-- Add multiple check constraints
+ALTER TABLE [finance].[accounts] ADD CONSTRAINT [ck_accounts_balance] CHECK ([balance] >= 0);
+ALTER TABLE [finance].[accounts] ADD CONSTRAINT [ck_accounts_type] CHECK ([account_type] IN ('CHECKING', 'SAVINGS', 'CREDIT'));
+
+-- Add unique constraint
+ALTER TABLE [finance].[accounts] ADD CONSTRAINT [uk_accounts_number] UNIQUE ([account_number]);
+
+-- Create related table with foreign key
+CREATE TABLE [finance].[transactions] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [account_id] INT NOT NULL,
+ [amount] DECIMAL(15,2) NOT NULL,
+ [transaction_type] NVARCHAR(20) NOT NULL,
+ [transaction_date] DATETIME2,
+ CONSTRAINT [fk_transactions_account] FOREIGN KEY ([account_id]) REFERENCES [finance].[accounts]([id]),
+ CONSTRAINT [ck_transactions_amount] CHECK ([amount] != 0),
+ CONSTRAINT [ck_transactions_type] CHECK ([transaction_type] IN ('DEBIT', 'CREDIT'))
+);
+`,
+ description: "Complex constraints with multiple check constraints and foreign keys",
+ },
+ {
+ name: "column_modifications",
+ initialSchema: `
+CREATE SCHEMA [hr];
+GO
+
+CREATE TABLE [hr].[employees] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [first_name] NVARCHAR(50),
+ [last_name] NVARCHAR(50),
+ [email] NVARCHAR(100),
+ [phone] VARCHAR(20),
+ [hire_date] DATE,
+ [salary] DECIMAL(10,2)
+);
+`,
+ migrationDDL: `
+-- Add new columns
+ALTER TABLE [hr].[employees] ADD [department_id] INT;
+ALTER TABLE [hr].[employees] ADD [manager_id] INT;
+ALTER TABLE [hr].[employees] ADD [status] NVARCHAR(20);
+
+-- Create department table
+CREATE TABLE [hr].[departments] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [budget] DECIMAL(15,2)
+);
+
+-- Add foreign key constraints
+ALTER TABLE [hr].[employees] ADD CONSTRAINT [fk_employees_department] FOREIGN KEY ([department_id]) REFERENCES [hr].[departments]([id]);
+ALTER TABLE [hr].[employees] ADD CONSTRAINT [fk_employees_manager] FOREIGN KEY ([manager_id]) REFERENCES [hr].[employees]([id]);
+
+-- Add check constraints
+ALTER TABLE [hr].[employees] ADD CONSTRAINT [ck_employees_salary] CHECK ([salary] > 0);
+ALTER TABLE [hr].[employees] ADD CONSTRAINT [ck_employees_status] CHECK ([status] IN ('ACTIVE', 'INACTIVE', 'TERMINATED'));
+`,
+ description: "Column additions and self-referencing foreign keys",
+ },
+ {
+ name: "multiple_schema_dependencies",
+ initialSchema: `
+CREATE SCHEMA [core];
+GO
+CREATE SCHEMA [app];
+GO
+
+CREATE TABLE [core].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL
+);
+
+CREATE TABLE [core].[roles] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(50) NOT NULL
+);
+`,
+ migrationDDL: `
+-- Create user_roles junction table in core schema
+CREATE TABLE [core].[user_roles] (
+ [user_id] INT NOT NULL,
+ [role_id] INT NOT NULL,
+ [assigned_at] DATETIME2,
+ CONSTRAINT [pk_user_roles] PRIMARY KEY ([user_id], [role_id]),
+ CONSTRAINT [fk_user_roles_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE,
+ CONSTRAINT [fk_user_roles_role] FOREIGN KEY ([role_id]) REFERENCES [core].[roles]([id]) ON DELETE CASCADE
+);
+
+-- Create application tables that reference core schema
+CREATE TABLE [app].[sessions] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [token] NVARCHAR(255) NOT NULL,
+ [expires_at] DATETIME2 NOT NULL,
+ CONSTRAINT [fk_sessions_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE
+);
+
+CREATE TABLE [app].[audit_log] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT,
+ [action] NVARCHAR(100) NOT NULL,
+ [timestamp] DATETIME2,
+ [details] NVARCHAR(MAX),
+ CONSTRAINT [fk_audit_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id])
+);
+`,
+ description: "Multiple schema dependencies with cross-schema foreign keys",
+ },
+ {
+ name: "computed_columns_and_triggers",
+ initialSchema: `
+CREATE SCHEMA [ecommerce];
+GO
+
+CREATE TABLE [ecommerce].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [tax_rate] DECIMAL(5,4)
+);
+`,
+ migrationDDL: `
+-- Add computed columns
+ALTER TABLE [ecommerce].[products] ADD [price_with_tax] AS ([price] * (1 + [tax_rate]));
+GO
+
+ALTER TABLE [ecommerce].[products] ADD [price_category] AS (
+ CASE
+ WHEN [price] < 10 THEN 'Budget'
+ WHEN [price] < 100 THEN 'Standard'
+ ELSE 'Premium'
+ END
+);
+GO
+
+-- Create order table
+CREATE TABLE [ecommerce].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [product_id] INT NOT NULL,
+ [quantity] INT NOT NULL,
+ [unit_price] DECIMAL(10,2) NOT NULL,
+ [total] DECIMAL(15,2),
+ [order_date] DATETIME2,
+ CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [ecommerce].[products]([id])
+);
+GO
+
+-- Add computed column for total
+ALTER TABLE [ecommerce].[orders] ADD [computed_total] AS ([quantity] * [unit_price]);
+GO
+
+-- Add indexes on computed columns
+CREATE INDEX [idx_products_price_category] ON [ecommerce].[products] ([price_category]);
+CREATE INDEX [idx_products_price_with_tax] ON [ecommerce].[products] ([price_with_tax]);
+`,
+ description: "Computed columns with indexes",
+ },
+ {
+ name: "create_tables_with_fk",
+ initialSchema: `
+CREATE SCHEMA [test];
+GO
+
+CREATE TABLE [test].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [created_at] DATETIME2,
+ CONSTRAINT [uk_email] UNIQUE ([email])
+);
+
+CREATE INDEX [idx_username] ON [test].[users] ([username]);
+
+CREATE TABLE [test].[posts] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NVARCHAR(MAX),
+ [published_at] DATETIME2,
+ CONSTRAINT [fk_user] FOREIGN KEY ([user_id]) REFERENCES [test].[users]([id]) ON DELETE CASCADE
+);
+
+CREATE INDEX [idx_user_id] ON [test].[posts] ([user_id]);
+`,
+ migrationDDL: `
+DROP TABLE [test].[posts];
+DROP TABLE [test].[users];
+DROP SCHEMA [test];`,
+ description: "Create tables with foreign key constraints",
+ },
+ {
+ name: "multiple_foreign_keys",
+ initialSchema: `
+CREATE SCHEMA [test];
+GO
+
+CREATE TABLE [test].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [created_at] DATETIME2,
+ CONSTRAINT [uk_email] UNIQUE ([email])
+);
+GO
+
+CREATE INDEX [idx_username] ON [test].[users] ([username]);
+GO
+
+CREATE TABLE [test].[posts] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NVARCHAR(MAX),
+ [published_at] DATETIME2,
+ CONSTRAINT [fk_user] FOREIGN KEY ([user_id]) REFERENCES [test].[users]([id]) ON DELETE CASCADE
+);
+GO
+
+CREATE INDEX [idx_user_id] ON [test].[posts] ([user_id]);
+GO
+`,
+ migrationDDL: `
+-- Add new column
+ALTER TABLE [test].[users] ADD [is_active] BIT;
+GO
+
+-- Create new table with multiple foreign keys
+CREATE TABLE [test].[comments] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [post_id] INT NOT NULL,
+ [user_id] INT NOT NULL,
+ [content] NVARCHAR(MAX) NOT NULL,
+ [created_at] DATETIME2,
+ CONSTRAINT [fk_comment_post] FOREIGN KEY ([post_id]) REFERENCES [test].[posts]([id]) ON DELETE CASCADE,
+ CONSTRAINT [fk_comment_user] FOREIGN KEY ([user_id]) REFERENCES [test].[users]([id]) ON DELETE NO ACTION
+);
+GO
+
+CREATE INDEX [idx_post_user] ON [test].[comments] ([post_id], [user_id]);
+
+-- Add new index
+CREATE INDEX [idx_email_active] ON [test].[users] ([email], [is_active]);
+
+-- Add check constraint
+ALTER TABLE [test].[posts] ADD CONSTRAINT [chk_title_length] CHECK (LEN([title]) > 0);
+`,
+ description: "Tables with multiple foreign key constraints",
+ },
+ {
+ name: "drop_and_recreate_fk_constraints",
+ initialSchema: `
+CREATE SCHEMA [library];
+GO
+
+CREATE TABLE [library].[authors] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [email] NVARCHAR(100),
+ CONSTRAINT [uk_email] UNIQUE ([email])
+);
+
+CREATE TABLE [library].[books] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [title] NVARCHAR(200) NOT NULL,
+ [author_id] INT NOT NULL,
+ [isbn] NVARCHAR(20),
+ [published_year] INT,
+ [price] DECIMAL(8, 2),
+ CONSTRAINT [fk_author] FOREIGN KEY ([author_id]) REFERENCES [library].[authors]([id]),
+ CONSTRAINT [chk_year_valid] CHECK ([published_year] >= 1000 AND [published_year] <= 2100),
+ CONSTRAINT [chk_price_positive] CHECK ([price] > 0),
+ CONSTRAINT [uk_isbn] UNIQUE ([isbn])
+);
+
+CREATE INDEX [idx_author] ON [library].[books] ([author_id]);
+CREATE INDEX [idx_year] ON [library].[books] ([published_year]);
+`,
+ migrationDDL: `
+-- Drop and recreate foreign key with different options
+ALTER TABLE [library].[books] DROP CONSTRAINT [fk_author];
+ALTER TABLE [library].[books] ADD CONSTRAINT [fk_author_new] FOREIGN KEY ([author_id]) REFERENCES [library].[authors]([id]) ON DELETE CASCADE ON UPDATE CASCADE;
+
+-- Drop and modify check constraints
+ALTER TABLE [library].[books] DROP CONSTRAINT [chk_year_valid];
+ALTER TABLE [library].[books] ADD CONSTRAINT [chk_year_extended] CHECK ([published_year] >= 1000 AND [published_year] <= 2030);
+
+-- Add new constraints
+ALTER TABLE [library].[books] ADD CONSTRAINT [chk_title_length] CHECK (LEN([title]) >= 3);
+`,
+ description: "Drop and recreate foreign key constraints with different options",
+ },
+ {
+ name: "self_referencing_foreign_keys",
+ initialSchema: `
+CREATE SCHEMA [company];
+GO
+
+CREATE TABLE [company].[departments] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [manager_id] INT
+);
+
+CREATE TABLE [company].[employees] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [department_id] INT,
+ [salary] DECIMAL(10, 2),
+ [hire_date] DATE,
+ CONSTRAINT [fk_dept] FOREIGN KEY ([department_id]) REFERENCES [company].[departments]([id])
+);
+
+CREATE INDEX [idx_dept] ON [company].[employees] ([department_id]);
+
+-- Add self-referencing foreign key
+ALTER TABLE [company].[departments] ADD CONSTRAINT [fk_manager] FOREIGN KEY ([manager_id]) REFERENCES [company].[employees]([id]);
+`,
+ migrationDDL: `
+-- Create base view
+CREATE VIEW [company].[dept_employee_count] AS
+SELECT d.[id] AS dept_id, d.[name] AS dept_name, COUNT(e.[id]) AS emp_count
+FROM [company].[departments] d
+LEFT JOIN [company].[employees] e ON d.[id] = e.[department_id]
+GROUP BY d.[id], d.[name];
+GO
+
+-- Create dependent view
+CREATE VIEW [company].[dept_summary] AS
+SELECT
+ dept_id,
+ dept_name,
+ emp_count,
+ 0 AS avg_salary,
+ 0 AS max_salary,
+ 0 AS min_salary
+FROM [company].[dept_employee_count];
+GO
+
+-- Create highly dependent view
+CREATE VIEW [company].[dept_manager_summary] AS
+SELECT
+ ds.dept_id,
+ ds.dept_name,
+ ds.emp_count,
+ ds.avg_salary,
+ m.[name] AS manager_name
+FROM [company].[dept_summary] ds
+JOIN [company].[departments] d ON ds.dept_id = d.[id]
+LEFT JOIN [company].[employees] m ON d.[manager_id] = m.[id];
+GO
+
+-- Create stored procedure using views
+CREATE PROCEDURE [company].[GetDepartmentReport]
+ @dept_name_pattern NVARCHAR(100)
+AS
+BEGIN
+ SELECT * FROM [company].[dept_manager_summary]
+ WHERE dept_name LIKE '%' + @dept_name_pattern + '%';
+END;
+`,
+ description: "Self-referencing foreign keys and complex view dependencies",
+ },
+ {
+ name: "table_and_column_comments",
+ initialSchema: `
+CREATE SCHEMA [app];
+GO
+
+CREATE TABLE [app].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [created_at] DATETIME2,
+ [status] NVARCHAR(20)
+);
+
+CREATE TABLE [app].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [category] NVARCHAR(50)
+);
+
+CREATE INDEX [idx_users_email] ON [app].[users] ([email]);
+CREATE INDEX [idx_products_category] ON [app].[products] ([category]);
+`,
+ migrationDDL: `
+-- Add table comments using extended properties
+EXEC sp_addextendedproperty 'MS_Description', 'User accounts and profile information', 'SCHEMA', 'app', 'TABLE', 'users';
+EXEC sp_addextendedproperty 'MS_Description', 'Product catalog with pricing information', 'SCHEMA', 'app', 'TABLE', 'products';
+GO
+
+-- Add column comments for users table
+EXEC sp_addextendedproperty 'MS_Description', 'Unique identifier for each user', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Unique username for login authentication', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'username';
+EXEC sp_addextendedproperty 'MS_Description', 'User email address for notifications', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'email';
+EXEC sp_addextendedproperty 'MS_Description', 'Timestamp when the user account was created', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'created_at';
+EXEC sp_addextendedproperty 'MS_Description', 'Current status: active, inactive, or suspended', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'status';
+GO
+
+-- Add column comments for products table
+EXEC sp_addextendedproperty 'MS_Description', 'Unique product identifier', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Product display name', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'name';
+EXEC sp_addextendedproperty 'MS_Description', 'Product price in USD', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'price';
+EXEC sp_addextendedproperty 'MS_Description', 'Product category classification', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'category';
+GO
+
+-- Create new table and add comments immediately
+CREATE TABLE [app].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [product_id] INT NOT NULL,
+ [quantity] INT NOT NULL,
+ [order_date] DATETIME2,
+ CONSTRAINT [fk_orders_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]),
+ CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [app].[products]([id])
+);
+GO
+
+-- Add comments to the new table
+EXEC sp_addextendedproperty 'MS_Description', 'Customer orders tracking system', 'SCHEMA', 'app', 'TABLE', 'orders';
+EXEC sp_addextendedproperty 'MS_Description', 'Unique order identifier', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Reference to the user who placed the order', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'user_id';
+EXEC sp_addextendedproperty 'MS_Description', 'Reference to the ordered product', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'product_id';
+EXEC sp_addextendedproperty 'MS_Description', 'Number of items ordered', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'quantity';
+EXEC sp_addextendedproperty 'MS_Description', 'When the order was placed', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'order_date';
+`,
+ description: "Adding comments to tables and columns using SQL Server extended properties",
+ },
+ {
+ name: "modify_and_drop_comments",
+ initialSchema: `
+CREATE SCHEMA [test];
+GO
+
+CREATE TABLE [test].[customers] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [email] NVARCHAR(100),
+ [phone] NVARCHAR(20),
+ [created_at] DATETIME2 DEFAULT GETDATE()
+);
+GO
+
+-- Add initial comments
+EXEC sp_addextendedproperty 'MS_Description', 'Customer information database', 'SCHEMA', 'test', 'TABLE', 'customers';
+EXEC sp_addextendedproperty 'MS_Description', 'Customer unique ID', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Customer full name', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'name';
+EXEC sp_addextendedproperty 'MS_Description', 'Customer email for marketing', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'email';
+EXEC sp_addextendedproperty 'MS_Description', 'Customer contact number', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'phone';
+GO
+
+CREATE TABLE [test].[invoices] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [customer_id] INT NOT NULL,
+ [amount] DECIMAL(10,2) NOT NULL,
+ [invoice_date] DATE,
+ CONSTRAINT [fk_invoices_customer] FOREIGN KEY ([customer_id]) REFERENCES [test].[customers]([id])
+);
+GO
+
+-- Add comments to invoices table
+EXEC sp_addextendedproperty 'MS_Description', 'Invoice records for billing', 'SCHEMA', 'test', 'TABLE', 'invoices';
+EXEC sp_addextendedproperty 'MS_Description', 'Invoice number', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Link to customer', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'customer_id';
+`,
+ migrationDDL: `
+-- Update existing table comment
+EXEC sp_updateextendedproperty 'MS_Description', 'Complete customer database with contact details and preferences', 'SCHEMA', 'test', 'TABLE', 'customers';
+GO
+
+-- Update existing column comments
+EXEC sp_updateextendedproperty 'MS_Description', 'Primary key - auto-generated customer identifier', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'id';
+EXEC sp_updateextendedproperty 'MS_Description', 'Customer business or personal name', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'name';
+EXEC sp_updateextendedproperty 'MS_Description', 'Primary email address for communications and billing', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'email';
+GO
+
+-- Drop specific column comment
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'phone';
+GO
+
+-- Add new comment to column that didn't have one
+EXEC sp_addextendedproperty 'MS_Description', 'Account creation timestamp for auditing', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'created_at';
+GO
+
+-- Update invoice table comment
+EXEC sp_updateextendedproperty 'MS_Description', 'Financial invoice tracking and billing system', 'SCHEMA', 'test', 'TABLE', 'invoices';
+GO
+
+-- Drop invoice column comments
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'id';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'customer_id';
+GO
+
+-- Add new column with comment
+ALTER TABLE [test].[invoices] ADD [payment_status] NVARCHAR(20);
+GO
+
+EXEC sp_addextendedproperty 'MS_Description', 'Current payment status: pending, paid, overdue, cancelled', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'payment_status';
+`,
+ description: "Modifying existing comments and removing comments using extended properties",
+ },
+ {
+ name: "comments_with_special_characters",
+ initialSchema: `
+CREATE SCHEMA [special];
+GO
+
+CREATE TABLE [special].[documents] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NVARCHAR(MAX),
+ [author] NVARCHAR(100),
+ [created_date] DATETIME2,
+ [metadata] NVARCHAR(500)
+);
+
+CREATE TABLE [special].[translations] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [document_id] INT NOT NULL,
+ [lang_code] NVARCHAR(10) NOT NULL,
+ [translated_title] NVARCHAR(200),
+ [translated_content] NVARCHAR(MAX),
+ CONSTRAINT [fk_translations_doc] FOREIGN KEY ([document_id]) REFERENCES [special].[documents]([id])
+);
+`,
+ migrationDDL: `
+-- Add table comments with special characters and quotes
+EXEC sp_addextendedproperty 'MS_Description', 'Document storage system - handles files, "metadata", & special chars: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'documents';
+GO
+
+-- Add multiline comment with special formatting
+EXEC sp_addextendedproperty 'MS_Description',
+'Translation table for international support:
+- Supports Unicode characters: αβγδε, 中文, العربية, русский
+- Handles quotes: "double quotes" and ''single quotes''
+- Special symbols: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'translations';
+GO
+
+-- Column comments with various special cases
+EXEC sp_addextendedproperty 'MS_Description', 'Primary key (auto-increment) - unique ID for each document', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Document title - may contain "quotes", ''apostrophes'', and symbols: @#$%', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'title';
+GO
+
+-- Multiline comment with technical details
+EXEC sp_addextendedproperty 'MS_Description',
+'Document content field:
+ • Supports rich text formatting
+ • HTML tags like <p>, <div>, <span>
+ • Special characters: © ® ™ § ¶ † ‡ • ‰ ′ ″
+ • Mathematical: ± × ÷ ≠ ≤ ≥ ∞ ∑ ∏ ∫
+ • Currency: $ € £ ¥ ₹ ₽
+ • Arrows: ← → ↑ ↓ ↔ ⇐ ⇒ ⇔', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'content';
+GO
+
+-- Comment with SQL injection attempt (should be safely handled)
+EXEC sp_addextendedproperty 'MS_Description', 'Author name field - prevents SQL injection like ''; DROP TABLE users; --', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'author';
+GO
+
+-- Comment with JSON-like structure
+EXEC sp_addextendedproperty 'MS_Description', 'Metadata JSON: {"version": "1.0", "tags": ["important", "draft"], "settings": {"public": false}}', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'metadata';
+GO
+
+-- Unicode characters in comments
+EXEC sp_addextendedproperty 'MS_Description', 'Language code: en-US, fr-FR, de-DE, ja-JP, zh-CN, ar-SA, ru-RU, hi-IN, 한국어, ไทย', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'lang_code';
+GO
+
+-- Comment with URLs and file paths
+EXEC sp_addextendedproperty 'MS_Description', 'Translated content - may reference URLs like https://example.com/path?param=value&other=123 or file paths C:\Users\Name\Documents\file.txt', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'translated_content';
+GO
+
+-- Create new table with extreme comment case
+CREATE TABLE [special].[test_extreme] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [data] NVARCHAR(MAX)
+);
+GO
+
+-- Extremely long comment to test limits
+EXEC sp_addextendedproperty 'MS_Description',
+'This is an extremely long comment designed to test the limits of extended property storage in SQL Server. It contains multiple lines of text with various formatting, special characters, and technical information. The purpose is to verify that the migration system can properly handle, store, and retrieve complex comment data without truncation or corruption. This comment includes: numbers (123456789), symbols (!@#$%^&*()_+-={}[]|\:";''<>?,./ ), Unicode characters (αβγδε中文العربيةрусский한국어), and structured data like JSON {"key": "value", "array": [1, 2, 3], "nested": {"deep": true}}. Additionally, it tests SQL-like syntax: SELECT * FROM table WHERE column = ''value'' AND other_column IN (1, 2, 3); as well as HTML markup: <html><body><p class="test">Content</p></body></html> and XML: <?xml version="1.0"?><root><item id="1">Test</item></root>. The comment system should preserve all these characters and structures exactly as written, demonstrating robust handling of complex metadata in database schema documentation.', 'SCHEMA', 'special', 'TABLE', 'test_extreme';
+`,
+ description: "Comments with special characters, quotes, multiline text, and Unicode",
+ },
+ {
+ name: "default_constraint_operations",
+ initialSchema: `
+CREATE SCHEMA [defaults];
+GO
+
+CREATE TABLE [defaults].[employees] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [hire_date] DATE NOT NULL DEFAULT GETDATE(),
+ [status] NVARCHAR(20) NOT NULL DEFAULT 'active',
+ [is_active] BIT NOT NULL DEFAULT 1,
+ [salary] DECIMAL(10,2) DEFAULT 50000.00,
+ [department_id] INT DEFAULT 1,
+ [created_at] DATETIME2 DEFAULT SYSDATETIME(),
+ [updated_at] DATETIME2
+);
+
+CREATE TABLE [defaults].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ [stock] INT NOT NULL DEFAULT 0,
+ [is_available] BIT DEFAULT 1
+);
+`,
+ migrationDDL: `
+-- Add column with default
+ALTER TABLE [defaults].[employees] ADD [vacation_days] INT NOT NULL DEFAULT 15;
+
+-- Remove default constraint (by adding new column without default)
+ALTER TABLE [defaults].[products] ADD [cost] DECIMAL(10,2) NOT NULL;
+
+-- Modify existing default value
+-- First drop the existing constraint
+DECLARE @constraint_name NVARCHAR(256);
+SELECT @constraint_name = dc.name
+FROM sys.default_constraints dc
+JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
+WHERE OBJECT_NAME(dc.parent_object_id) = 'employees'
+AND c.name = 'status'
+AND OBJECT_SCHEMA_NAME(dc.parent_object_id) = 'defaults';
+IF @constraint_name IS NOT NULL
+ EXEC('ALTER TABLE [defaults].[employees] DROP CONSTRAINT [' + @constraint_name + ']');
+GO
+
+-- Add new default
+ALTER TABLE [defaults].[employees] ADD CONSTRAINT [DF_employees_status_new] DEFAULT 'pending' FOR [status];
+
+-- Add default with expression
+ALTER TABLE [defaults].[employees] ADD [bonus_percentage] DECIMAL(5,2) DEFAULT (CASE WHEN MONTH(GETDATE()) = 12 THEN 10.0 ELSE 5.0 END);
+
+-- Create new table with various defaults
+CREATE TABLE [defaults].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [order_number] NVARCHAR(50) NOT NULL DEFAULT CONCAT('ORD-', YEAR(GETDATE()), '-', FORMAT(GETDATE(), 'MMdd')),
+ [order_date] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
+ [status] NVARCHAR(20) NOT NULL DEFAULT 'pending',
+ [total] DECIMAL(10,2) NOT NULL DEFAULT 0,
+ [discount] DECIMAL(5,2) DEFAULT NULL,
+ [is_paid] BIT NOT NULL DEFAULT 0,
+ [notes] NVARCHAR(MAX) DEFAULT 'No notes'
+);
+`,
+ description: "Default constraint operations including add, remove, modify with various data types and expressions",
+ },
+ {
+ name: "complex_default_expressions",
+ initialSchema: `
+CREATE SCHEMA [complex];
+GO
+
+CREATE TABLE [complex].[audit_log] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [event_type] NVARCHAR(50) NOT NULL,
+ [user_name] NVARCHAR(100) NOT NULL DEFAULT SYSTEM_USER,
+ [event_time] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
+ [ip_address] NVARCHAR(45),
+ [session_id] UNIQUEIDENTIFIER DEFAULT NEWID()
+);
+
+CREATE TABLE [complex].[documents] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NVARCHAR(MAX),
+ [version] INT NOT NULL DEFAULT 1,
+ [created_date] DATE DEFAULT CAST(GETDATE() AS DATE),
+ [expiry_date] DATE DEFAULT DATEADD(YEAR, 1, CAST(GETDATE() AS DATE))
+);
+`,
+ migrationDDL: `
+-- Add columns with complex default expressions
+ALTER TABLE [complex].[audit_log] ADD [server_name] NVARCHAR(100) DEFAULT @@SERVERNAME;
+ALTER TABLE [complex].[audit_log] ADD [database_name] NVARCHAR(100) DEFAULT DB_NAME();
+ALTER TABLE [complex].[audit_log] ADD [schema_name] NVARCHAR(100) DEFAULT SCHEMA_NAME();
+
+-- Add computed default based on other columns
+ALTER TABLE [complex].[documents] ADD [document_code] NVARCHAR(50) DEFAULT CONCAT('DOC-', FORMAT(GETDATE(), 'yyyyMMdd'), '-', NEWID());
+
+-- Create table with function-based defaults
+CREATE TABLE [complex].[calculations] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [value1] DECIMAL(10,2) NOT NULL,
+ [value2] DECIMAL(10,2) NOT NULL,
+ [pi_value] DECIMAL(10,8) DEFAULT PI(),
+ [random_value] FLOAT DEFAULT RAND(),
+ [checksum_value] INT DEFAULT CHECKSUM(NEWID()),
+ [created_at] DATETIME2 DEFAULT SYSDATETIME(),
+ [created_offset] DATETIMEOFFSET DEFAULT SYSDATETIMEOFFSET()
+);
+
+-- Add default with CASE expression
+ALTER TABLE [complex].[documents] ADD [priority] INT DEFAULT
+ CASE
+ WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 1
+ WHEN DATEPART(HOUR, GETDATE()) < 18 THEN 2
+ ELSE 3
+ END;
+`,
+ description: "Complex default expressions using system functions, variables, and conditional logic",
+ },
+ {
+ name: "default_constraint_edge_cases",
+ initialSchema: `
+CREATE SCHEMA [edge];
+GO
+
+CREATE TABLE [edge].[test_defaults] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [nullable_with_default] INT DEFAULT 42,
+ [string_default] NVARCHAR(50) DEFAULT N'Default''s value',
+ [empty_string] NVARCHAR(50) DEFAULT '',
+ [zero_default] INT DEFAULT 0,
+ [negative_default] INT DEFAULT -1,
+ [decimal_precision] DECIMAL(18,6) DEFAULT 123.456789
+);
+`,
+ migrationDDL: `
+-- Test dropping and re-adding defaults with different names
+DECLARE @constraint_name NVARCHAR(256);
+-- Drop nullable_with_default's constraint
+SELECT @constraint_name = dc.name
+FROM sys.default_constraints dc
+JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
+WHERE OBJECT_NAME(dc.parent_object_id) = 'test_defaults'
+AND c.name = 'nullable_with_default'
+AND OBJECT_SCHEMA_NAME(dc.parent_object_id) = 'edge';
+IF @constraint_name IS NOT NULL
+ EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name + ']');
+GO
+
+-- Add it back with a new value
+ALTER TABLE [edge].[test_defaults] ADD CONSTRAINT [DF_test_defaults_nullable_new] DEFAULT 84 FOR [nullable_with_default];
+
+-- Add column with special characters in default
+ALTER TABLE [edge].[test_defaults] ADD [special_chars] NVARCHAR(100) DEFAULT N'Line1' + CHAR(13) + CHAR(10) + N'Line2';
+
+-- Add column with unicode default
+ALTER TABLE [edge].[test_defaults] ADD [unicode_default] NVARCHAR(100) DEFAULT N'你好世界 🌍';
+
+-- Add column with max value defaults
+ALTER TABLE [edge].[test_defaults] ADD [max_int] BIGINT DEFAULT 9223372036854775807;
+ALTER TABLE [edge].[test_defaults] ADD [min_int] BIGINT DEFAULT -9223372036854775808;
+
+-- Create table with bit field defaults
+CREATE TABLE [edge].[flags] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [is_enabled] BIT NOT NULL DEFAULT 1,
+ [is_deleted] BIT NOT NULL DEFAULT 0,
+ [is_visible] BIT DEFAULT 1,
+ [is_archived] BIT DEFAULT 0
+);
+
+-- Add JSON default (for NVARCHAR storing JSON)
+ALTER TABLE [edge].[test_defaults] ADD [json_config] NVARCHAR(MAX) DEFAULT N'{"enabled": true, "count": 0}';
+`,
+ description: "Edge cases for default constraints including special characters, unicode, extreme values",
+ },
+ {
+ name: "reverse_basic_table_operations",
+ initialSchema: `
+CREATE SCHEMA [app];
+GO
+
+CREATE TABLE [app].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [created_at] DATETIME2,
+ [status] NVARCHAR(20),
+ CONSTRAINT [uk_users_email] UNIQUE ([email]),
+ CONSTRAINT [ck_users_status] CHECK ([status] IN ('active', 'inactive', 'suspended'))
+);
+
+CREATE INDEX [idx_users_username] ON [app].[users] ([username]);
+
+CREATE TABLE [app].[posts] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NTEXT,
+ [published_at] DATETIME2,
+ CONSTRAINT [fk_posts_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]) ON DELETE CASCADE
+);
+
+CREATE INDEX [idx_posts_user_id] ON [app].[posts] ([user_id]);
+
+CREATE TABLE [app].[comments] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [post_id] INT NOT NULL,
+ [user_id] INT NOT NULL,
+ [content] NTEXT NOT NULL,
+ [created_at] DATETIME2,
+ CONSTRAINT [fk_comments_post] FOREIGN KEY ([post_id]) REFERENCES [app].[posts]([id]) ON DELETE CASCADE,
+ CONSTRAINT [fk_comments_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id])
+);
+
+CREATE INDEX [idx_comments_post_id] ON [app].[comments] ([post_id]);
+`,
+ migrationDDL: `
+-- Drop index
+DROP INDEX [idx_comments_post_id] ON [app].[comments];
+
+-- Drop check constraint
+ALTER TABLE [app].[users] DROP CONSTRAINT [ck_users_status];
+
+-- Drop table (must drop in correct order due to foreign keys)
+DROP TABLE [app].[comments];
+
+-- Drop column
+ALTER TABLE [app].[users] DROP COLUMN [status];
+`,
+ description: "Reverse of basic table operations - dropping column, table, index, and constraint",
+ },
+ {
+ name: "reverse_schema_operations",
+ initialSchema: `
+CREATE SCHEMA [sales];
+GO
+
+CREATE SCHEMA [inventory];
+GO
+
+CREATE TABLE [sales].[customers] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [email] NVARCHAR(100)
+);
+
+CREATE TABLE [sales].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [customer_id] INT NOT NULL,
+ [order_date] DATE,
+ [total] DECIMAL(10,2),
+ [product_id] INT,
+ CONSTRAINT [fk_orders_customer] FOREIGN KEY ([customer_id]) REFERENCES [sales].[customers]([id])
+);
+
+CREATE TABLE [inventory].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [category_id] INT,
+ CONSTRAINT [ck_products_price] CHECK ([price] > 0)
+);
+
+ALTER TABLE [sales].[orders] ADD CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [inventory].[products]([id]);
+`,
+ migrationDDL: `
+-- Drop foreign key referencing table in different schema
+ALTER TABLE [sales].[orders] DROP CONSTRAINT [fk_orders_product];
+
+-- Drop column referencing another schema
+ALTER TABLE [sales].[orders] DROP COLUMN [product_id];
+
+-- Drop table in inventory schema
+DROP TABLE [inventory].[products];
+
+-- Drop the inventory schema
+DROP SCHEMA [inventory];
+`,
+ description: "Reverse of schema operations - dropping cross-schema foreign key, table, and schema",
+ },
+ {
+ name: "reverse_view_operations",
+ initialSchema: `
+CREATE SCHEMA [reporting];
+GO
+
+CREATE TABLE [reporting].[sales_data] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [product_name] NVARCHAR(100) NOT NULL,
+ [quantity] INT NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [sale_date] DATE
+);
+
+CREATE TABLE [reporting].[customers] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [region] NVARCHAR(50)
+);
+GO
+
+CREATE VIEW [reporting].[product_summary] AS
+SELECT
+ [product_name],
+ COUNT(*) as [sale_count],
+ SUM([quantity]) as [total_quantity],
+ AVG([price]) as [avg_price]
+FROM [reporting].[sales_data]
+GROUP BY [product_name];
+GO
+
+CREATE VIEW [reporting].[top_products] AS
+SELECT TOP 10
+ [product_name],
+ [total_quantity]
+FROM [reporting].[product_summary]
+WHERE [total_quantity] > 100
+ORDER BY [total_quantity] DESC;
+GO
+`,
+ migrationDDL: `
+-- Drop dependent view first
+DROP VIEW [reporting].[top_products];
+GO
+
+-- Drop base view
+DROP VIEW [reporting].[product_summary];
+GO
+`,
+ description: "Reverse of view operations - dropping views in correct dependency order",
+ },
+ {
+ name: "reverse_function_and_procedure_operations",
+ initialSchema: `
+CREATE SCHEMA [calc];
+GO
+
+CREATE TABLE [calc].[numbers] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [value] INT NOT NULL,
+ [category] NVARCHAR(20)
+);
+GO
+
+CREATE FUNCTION [calc].[square](@input INT)
+RETURNS INT
+AS
+BEGIN
+ RETURN @input * @input;
+END;
+GO
+
+CREATE FUNCTION [calc].[get_numbers_by_category](@category NVARCHAR(20))
+RETURNS TABLE
+AS
+RETURN (
+ SELECT [id], [value], [category]
+ FROM [calc].[numbers]
+ WHERE [category] = @category
+);
+GO
+
+CREATE PROCEDURE [calc].[add_number]
+ @value INT,
+ @category NVARCHAR(20)
+AS
+BEGIN
+ INSERT INTO [calc].[numbers] ([value], [category])
+ VALUES (@value, @category);
+END;
+GO
+`,
+ migrationDDL: `
+-- Drop stored procedure
+DROP PROCEDURE [calc].[add_number];
+GO
+
+-- Drop table-valued function
+DROP FUNCTION [calc].[get_numbers_by_category];
+GO
+
+-- Drop scalar function
+DROP FUNCTION [calc].[square];
+GO
+`,
+ description: "Reverse of function and procedure operations - dropping procedures and functions",
+ },
+ {
+ name: "reverse_index_operations",
+ initialSchema: `
+CREATE SCHEMA [perf];
+GO
+
+CREATE TABLE [perf].[events] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [event_name] NVARCHAR(100) NOT NULL,
+ [event_data] NVARCHAR(MAX),
+ [timestamp] DATETIME2,
+ [user_id] INT,
+ [category] NVARCHAR(50),
+ [event_month] AS DATEPART(MONTH, [timestamp])
+);
+
+CREATE INDEX [idx_events_timestamp] ON [perf].[events] ([timestamp]);
+CREATE INDEX [idx_events_user_category] ON [perf].[events] ([user_id], [category]);
+CREATE UNIQUE INDEX [idx_events_name_timestamp] ON [perf].[events] ([event_name], [timestamp]);
+
+CREATE INDEX [idx_events_recent] ON [perf].[events] ([event_name])
+WHERE [timestamp] >= '2023-01-01';
+
+CREATE INDEX [idx_events_month] ON [perf].[events] ([event_month]);
+`,
+ migrationDDL: `
+-- Drop index on computed column
+DROP INDEX [idx_events_month] ON [perf].[events];
+
+-- Drop computed column
+ALTER TABLE [perf].[events] DROP COLUMN [event_month];
+
+-- Drop filtered index
+DROP INDEX [idx_events_recent] ON [perf].[events];
+
+-- Drop unique index
+DROP INDEX [idx_events_name_timestamp] ON [perf].[events];
+
+-- Drop composite index
+DROP INDEX [idx_events_user_category] ON [perf].[events];
+
+-- Drop simple index
+DROP INDEX [idx_events_timestamp] ON [perf].[events];
+`,
+ description: "Reverse of index operations - dropping various index types including filtered and computed column indexes",
+ },
+ {
+ name: "reverse_complex_constraints",
+ initialSchema: `
+CREATE SCHEMA [finance];
+GO
+
+CREATE TABLE [finance].[accounts] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [account_number] NVARCHAR(20) NOT NULL,
+ [balance] DECIMAL(15,2),
+ [account_type] NVARCHAR(20) NOT NULL,
+ [created_at] DATETIME2 DEFAULT GETDATE(),
+ CONSTRAINT [ck_accounts_balance] CHECK ([balance] >= 0),
+ CONSTRAINT [ck_accounts_type] CHECK ([account_type] IN ('CHECKING', 'SAVINGS', 'CREDIT')),
+ CONSTRAINT [uk_accounts_number] UNIQUE ([account_number])
+);
+
+CREATE TABLE [finance].[transactions] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [account_id] INT NOT NULL,
+ [amount] DECIMAL(15,2) NOT NULL,
+ [transaction_type] NVARCHAR(20) NOT NULL,
+ [transaction_date] DATETIME2,
+ CONSTRAINT [fk_transactions_account] FOREIGN KEY ([account_id]) REFERENCES [finance].[accounts]([id]),
+ CONSTRAINT [ck_transactions_amount] CHECK ([amount] != 0),
+ CONSTRAINT [ck_transactions_type] CHECK ([transaction_type] IN ('DEBIT', 'CREDIT'))
+);
+`,
+ migrationDDL: `
+-- Drop table with foreign key and check constraints
+DROP TABLE [finance].[transactions];
+
+-- Drop unique constraint
+ALTER TABLE [finance].[accounts] DROP CONSTRAINT [uk_accounts_number];
+
+-- Drop check constraints
+ALTER TABLE [finance].[accounts] DROP CONSTRAINT [ck_accounts_type];
+ALTER TABLE [finance].[accounts] DROP CONSTRAINT [ck_accounts_balance];
+`,
+ description: "Reverse of complex constraints - dropping tables with foreign keys and removing constraints",
+ },
+ {
+ name: "reverse_column_modifications",
+ initialSchema: `
+CREATE SCHEMA [hr];
+GO
+
+CREATE TABLE [hr].[departments] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [budget] DECIMAL(15,2)
+);
+
+CREATE TABLE [hr].[employees] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [first_name] NVARCHAR(50),
+ [last_name] NVARCHAR(50),
+ [email] NVARCHAR(100),
+ [phone] VARCHAR(20),
+ [hire_date] DATE,
+ [salary] DECIMAL(10,2),
+ [department_id] INT,
+ [manager_id] INT,
+ [status] NVARCHAR(20),
+ CONSTRAINT [fk_employees_department] FOREIGN KEY ([department_id]) REFERENCES [hr].[departments]([id]),
+ CONSTRAINT [fk_employees_manager] FOREIGN KEY ([manager_id]) REFERENCES [hr].[employees]([id]),
+ CONSTRAINT [ck_employees_salary] CHECK ([salary] > 0),
+ CONSTRAINT [ck_employees_status] CHECK ([status] IN ('ACTIVE', 'INACTIVE', 'TERMINATED'))
+);
+`,
+ migrationDDL: `
+-- Drop check constraints first
+ALTER TABLE [hr].[employees] DROP CONSTRAINT [ck_employees_status];
+ALTER TABLE [hr].[employees] DROP CONSTRAINT [ck_employees_salary];
+
+-- Drop foreign key constraints before dropping columns
+ALTER TABLE [hr].[employees] DROP CONSTRAINT [fk_employees_manager];
+ALTER TABLE [hr].[employees] DROP CONSTRAINT [fk_employees_department];
+
+-- Now safe to drop columns
+ALTER TABLE [hr].[employees] DROP COLUMN [status];
+ALTER TABLE [hr].[employees] DROP COLUMN [manager_id];
+ALTER TABLE [hr].[employees] DROP COLUMN [department_id];
+
+-- Finally drop department table
+DROP TABLE [hr].[departments];
+`,
+ description: "Reverse of column modifications - dropping columns, constraints, and related tables",
+ },
+ {
+ name: "reverse_multiple_schema_dependencies",
+ initialSchema: `
+CREATE SCHEMA [core];
+GO
+CREATE SCHEMA [app];
+GO
+
+CREATE TABLE [core].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL
+);
+
+CREATE TABLE [core].[roles] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(50) NOT NULL
+);
+
+CREATE TABLE [core].[user_roles] (
+ [user_id] INT NOT NULL,
+ [role_id] INT NOT NULL,
+ [assigned_at] DATETIME2,
+ CONSTRAINT [pk_user_roles] PRIMARY KEY ([user_id], [role_id]),
+ CONSTRAINT [fk_user_roles_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE,
+ CONSTRAINT [fk_user_roles_role] FOREIGN KEY ([role_id]) REFERENCES [core].[roles]([id]) ON DELETE CASCADE
+);
+
+CREATE TABLE [app].[sessions] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [token] NVARCHAR(255) NOT NULL,
+ [expires_at] DATETIME2 NOT NULL,
+ CONSTRAINT [fk_sessions_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE
+);
+
+CREATE TABLE [app].[audit_log] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT,
+ [action] NVARCHAR(100) NOT NULL,
+ [timestamp] DATETIME2,
+ [details] NVARCHAR(MAX),
+ CONSTRAINT [fk_audit_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id])
+);
+`,
+ migrationDDL: `
+-- Drop tables that reference core schema
+DROP TABLE [app].[audit_log];
+DROP TABLE [app].[sessions];
+
+-- Drop junction table
+DROP TABLE [core].[user_roles];
+`,
+ description: "Reverse of multiple schema dependencies - dropping cross-schema references",
+ },
+ {
+ name: "reverse_computed_columns_and_triggers",
+ initialSchema: `
+CREATE SCHEMA [ecommerce];
+GO
+
+CREATE TABLE [ecommerce].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [tax_rate] DECIMAL(5,4),
+ [price_with_tax] AS ([price] * (1 + [tax_rate])),
+ [price_category] AS (
+ CASE
+ WHEN [price] < 10 THEN 'Budget'
+ WHEN [price] < 100 THEN 'Standard'
+ ELSE 'Premium'
+ END
+ )
+);
+GO
+
+CREATE INDEX [idx_products_price_category] ON [ecommerce].[products] ([price_category]);
+CREATE INDEX [idx_products_price_with_tax] ON [ecommerce].[products] ([price_with_tax]);
+
+CREATE TABLE [ecommerce].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [product_id] INT NOT NULL,
+ [quantity] INT NOT NULL,
+ [unit_price] DECIMAL(10,2) NOT NULL,
+ [total] DECIMAL(15,2),
+ [order_date] DATETIME2,
+ [computed_total] AS ([quantity] * [unit_price]),
+ CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [ecommerce].[products]([id])
+);
+GO
+`,
+ migrationDDL: `
+-- Drop table with computed column
+DROP TABLE [ecommerce].[orders];
+GO
+
+-- Drop indexes on computed columns
+DROP INDEX [idx_products_price_with_tax] ON [ecommerce].[products];
+DROP INDEX [idx_products_price_category] ON [ecommerce].[products];
+
+-- Drop computed columns
+ALTER TABLE [ecommerce].[products] DROP COLUMN [price_category];
+GO
+
+ALTER TABLE [ecommerce].[products] DROP COLUMN [price_with_tax];
+GO
+`,
+ description: "Reverse of computed columns - dropping indexes and computed columns",
+ },
+ {
+ name: "reverse_temporal_tables",
+ initialSchema: `
+CREATE SCHEMA [tracking];
+GO
+
+CREATE TABLE [tracking].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [last_modified] DATETIME2,
+ [valid_from] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
+ [valid_to] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
+ PERIOD FOR SYSTEM_TIME ([valid_from], [valid_to])
+);
+
+CREATE TABLE [tracking].[products_history] (
+ [id] INT NOT NULL,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [last_modified] DATETIME2,
+ [valid_from] DATETIME2 NOT NULL,
+ [valid_to] DATETIME2 NOT NULL
+);
+
+CREATE CLUSTERED INDEX [idx_products_history_period] ON [tracking].[products_history] ([valid_to], [valid_from]);
+
+ALTER TABLE [tracking].[products] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [tracking].[products_history]));
+`,
+ migrationDDL: `
+-- Disable system versioning first
+ALTER TABLE [tracking].[products] SET (SYSTEM_VERSIONING = OFF);
+
+-- Drop history table (and its index will be dropped automatically)
+DROP TABLE [tracking].[products_history];
+
+-- Drop period definition and system time columns
+ALTER TABLE [tracking].[products] DROP PERIOD FOR SYSTEM_TIME;
+ALTER TABLE [tracking].[products] DROP COLUMN [valid_to];
+ALTER TABLE [tracking].[products] DROP COLUMN [valid_from];
+`,
+ description: "Reverse of temporal tables - disabling system versioning and dropping history",
+ },
+ {
+ name: "reverse_table_and_column_comments",
+ initialSchema: `
+CREATE SCHEMA [app];
+GO
+
+CREATE TABLE [app].[users] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [username] NVARCHAR(50) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [created_at] DATETIME2,
+ [status] NVARCHAR(20)
+);
+
+CREATE TABLE [app].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL,
+ [category] NVARCHAR(50)
+);
+
+CREATE INDEX [idx_users_email] ON [app].[users] ([email]);
+CREATE INDEX [idx_products_category] ON [app].[products] ([category]);
+
+CREATE TABLE [app].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [user_id] INT NOT NULL,
+ [product_id] INT NOT NULL,
+ [quantity] INT NOT NULL,
+ [order_date] DATETIME2,
+ CONSTRAINT [fk_orders_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]),
+ CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [app].[products]([id])
+);
+GO
+
+-- Add table comments using extended properties
+EXEC sp_addextendedproperty 'MS_Description', 'User accounts and profile information', 'SCHEMA', 'app', 'TABLE', 'users';
+EXEC sp_addextendedproperty 'MS_Description', 'Product catalog with pricing information', 'SCHEMA', 'app', 'TABLE', 'products';
+EXEC sp_addextendedproperty 'MS_Description', 'Customer orders tracking system', 'SCHEMA', 'app', 'TABLE', 'orders';
+GO
+
+-- Add column comments for users table
+EXEC sp_addextendedproperty 'MS_Description', 'Unique identifier for each user', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Unique username for login authentication', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'username';
+EXEC sp_addextendedproperty 'MS_Description', 'User email address for notifications', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'email';
+EXEC sp_addextendedproperty 'MS_Description', 'Timestamp when the user account was created', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'created_at';
+EXEC sp_addextendedproperty 'MS_Description', 'Current status: active, inactive, or suspended', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'status';
+GO
+
+-- Add column comments for products table
+EXEC sp_addextendedproperty 'MS_Description', 'Unique product identifier', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Product display name', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'name';
+EXEC sp_addextendedproperty 'MS_Description', 'Product price in USD', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'price';
+EXEC sp_addextendedproperty 'MS_Description', 'Product category classification', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'category';
+GO
+
+-- Add comments to orders table
+EXEC sp_addextendedproperty 'MS_Description', 'Unique order identifier', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Reference to the user who placed the order', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'user_id';
+EXEC sp_addextendedproperty 'MS_Description', 'Reference to the ordered product', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'product_id';
+EXEC sp_addextendedproperty 'MS_Description', 'Number of items ordered', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'quantity';
+EXEC sp_addextendedproperty 'MS_Description', 'When the order was placed', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'order_date';
+`,
+ migrationDDL: `
+-- Drop column comments from orders table
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'order_date';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'quantity';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'product_id';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'user_id';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'id';
+GO
+
+-- Drop table comment from orders
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders';
+GO
+
+-- Drop the orders table
+DROP TABLE [app].[orders];
+GO
+
+-- Drop column comments from products table
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'category';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'price';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'name';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'id';
+GO
+
+-- Drop column comments from users table
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'status';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'created_at';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'email';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'username';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'id';
+GO
+
+-- Drop table comments
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users';
+`,
+ description: "Reverse of table and column comments - dropping extended properties",
+ },
+ {
+ name: "reverse_default_constraint_operations",
+ initialSchema: `
+CREATE SCHEMA [defaults];
+GO
+
+CREATE TABLE [defaults].[employees] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [email] NVARCHAR(100) NOT NULL,
+ [hire_date] DATE NOT NULL DEFAULT GETDATE(),
+ [status] NVARCHAR(20) NOT NULL CONSTRAINT [DF_employees_status_new] DEFAULT 'pending',
+ [is_active] BIT NOT NULL DEFAULT 1,
+ [salary] DECIMAL(10,2) DEFAULT 50000.00,
+ [department_id] INT DEFAULT 1,
+ [created_at] DATETIME2 DEFAULT SYSDATETIME(),
+ [updated_at] DATETIME2,
+ [vacation_days] INT NOT NULL DEFAULT 15,
+ [bonus_percentage] DECIMAL(5,2) DEFAULT (CASE WHEN MONTH(GETDATE()) = 12 THEN 10.0 ELSE 5.0 END)
+);
+
+CREATE TABLE [defaults].[products] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [price] DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ [stock] INT NOT NULL DEFAULT 0,
+ [is_available] BIT DEFAULT 1,
+ [cost] DECIMAL(10,2) NOT NULL
+);
+
+CREATE TABLE [defaults].[orders] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [order_number] NVARCHAR(50) NOT NULL DEFAULT CONCAT('ORD-', YEAR(GETDATE()), '-', FORMAT(GETDATE(), 'MMdd')),
+ [order_date] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
+ [status] NVARCHAR(20) NOT NULL DEFAULT 'pending',
+ [total] DECIMAL(10,2) NOT NULL DEFAULT 0,
+ [discount] DECIMAL(5,2) DEFAULT NULL,
+ [is_paid] BIT NOT NULL DEFAULT 0,
+ [notes] NVARCHAR(MAX) DEFAULT 'No notes'
+);
+`,
+ migrationDDL: `
+-- Drop orders table with all its defaults
+DROP TABLE [defaults].[orders];
+GO
+
+-- Drop default constraint for bonus_percentage column
+DECLARE @constraint_name NVARCHAR(128);
+SELECT @constraint_name = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'defaults' AND t.name = 'employees' AND c.name = 'bonus_percentage';
+IF @constraint_name IS NOT NULL
+ EXEC('ALTER TABLE [defaults].[employees] DROP CONSTRAINT [' + @constraint_name + ']');
+GO
+
+-- Drop default constraint for vacation_days column
+DECLARE @constraint_name2 NVARCHAR(128);
+SELECT @constraint_name2 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'defaults' AND t.name = 'employees' AND c.name = 'vacation_days';
+IF @constraint_name2 IS NOT NULL
+ EXEC('ALTER TABLE [defaults].[employees] DROP CONSTRAINT [' + @constraint_name2 + ']');
+GO
+
+-- Now safe to drop columns
+ALTER TABLE [defaults].[employees] DROP COLUMN [bonus_percentage];
+ALTER TABLE [defaults].[employees] DROP COLUMN [vacation_days];
+
+-- Remove default from status column (named constraint)
+ALTER TABLE [defaults].[employees] DROP CONSTRAINT [DF_employees_status_new];
+
+-- Drop cost column (which has no default)
+ALTER TABLE [defaults].[products] DROP COLUMN [cost];
+`,
+ description: "Reverse of default constraint operations - dropping tables, columns with defaults, and default constraints",
+ },
+ {
+ name: "reverse_complex_default_expressions",
+ initialSchema: `
+CREATE SCHEMA [complex];
+GO
+
+CREATE TABLE [complex].[audit_log] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [event_type] NVARCHAR(50) NOT NULL,
+ [user_name] NVARCHAR(100) NOT NULL DEFAULT SYSTEM_USER,
+ [event_time] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
+ [ip_address] NVARCHAR(45),
+ [session_id] UNIQUEIDENTIFIER DEFAULT NEWID(),
+ [server_name] NVARCHAR(100) DEFAULT @@SERVERNAME,
+ [database_name] NVARCHAR(100) DEFAULT DB_NAME(),
+ [schema_name] NVARCHAR(100) DEFAULT SCHEMA_NAME()
+);
+
+CREATE TABLE [complex].[documents] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NVARCHAR(MAX),
+ [version] INT NOT NULL DEFAULT 1,
+ [created_date] DATE DEFAULT CAST(GETDATE() AS DATE),
+ [expiry_date] DATE DEFAULT DATEADD(YEAR, 1, CAST(GETDATE() AS DATE)),
+ [document_code] NVARCHAR(50) DEFAULT CONCAT('DOC-', FORMAT(GETDATE(), 'yyyyMMdd'), '-', NEWID()),
+ [priority] INT DEFAULT
+ CASE
+ WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 1
+ WHEN DATEPART(HOUR, GETDATE()) < 18 THEN 2
+ ELSE 3
+ END
+);
+
+CREATE TABLE [complex].[calculations] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [value1] DECIMAL(10,2) NOT NULL,
+ [value2] DECIMAL(10,2) NOT NULL,
+ [pi_value] DECIMAL(10,8) DEFAULT PI(),
+ [random_value] FLOAT DEFAULT RAND(),
+ [checksum_value] INT DEFAULT CHECKSUM(NEWID()),
+ [created_at] DATETIME2 DEFAULT SYSDATETIME(),
+ [created_offset] DATETIMEOFFSET DEFAULT SYSDATETIMEOFFSET()
+);
+`,
+ migrationDDL: `
+-- Drop table with function-based defaults
+DROP TABLE [complex].[calculations];
+GO
+
+-- Drop default constraint for priority column
+DECLARE @constraint_name NVARCHAR(128);
+SELECT @constraint_name = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'complex' AND t.name = 'documents' AND c.name = 'priority';
+IF @constraint_name IS NOT NULL
+ EXEC('ALTER TABLE [complex].[documents] DROP CONSTRAINT [' + @constraint_name + ']');
+GO
+
+-- Drop default constraint for document_code column
+DECLARE @constraint_name2 NVARCHAR(128);
+SELECT @constraint_name2 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'complex' AND t.name = 'documents' AND c.name = 'document_code';
+IF @constraint_name2 IS NOT NULL
+ EXEC('ALTER TABLE [complex].[documents] DROP CONSTRAINT [' + @constraint_name2 + ']');
+GO
+
+-- Drop default constraint for schema_name column
+DECLARE @constraint_name3 NVARCHAR(128);
+SELECT @constraint_name3 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'complex' AND t.name = 'audit_log' AND c.name = 'schema_name';
+IF @constraint_name3 IS NOT NULL
+ EXEC('ALTER TABLE [complex].[audit_log] DROP CONSTRAINT [' + @constraint_name3 + ']');
+GO
+
+-- Drop default constraint for database_name column
+DECLARE @constraint_name4 NVARCHAR(128);
+SELECT @constraint_name4 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'complex' AND t.name = 'audit_log' AND c.name = 'database_name';
+IF @constraint_name4 IS NOT NULL
+ EXEC('ALTER TABLE [complex].[audit_log] DROP CONSTRAINT [' + @constraint_name4 + ']');
+GO
+
+-- Drop default constraint for server_name column
+DECLARE @constraint_name5 NVARCHAR(128);
+SELECT @constraint_name5 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'complex' AND t.name = 'audit_log' AND c.name = 'server_name';
+IF @constraint_name5 IS NOT NULL
+ EXEC('ALTER TABLE [complex].[audit_log] DROP CONSTRAINT [' + @constraint_name5 + ']');
+GO
+
+-- Now safe to drop columns
+ALTER TABLE [complex].[documents] DROP COLUMN [priority];
+ALTER TABLE [complex].[documents] DROP COLUMN [document_code];
+ALTER TABLE [complex].[audit_log] DROP COLUMN [schema_name];
+ALTER TABLE [complex].[audit_log] DROP COLUMN [database_name];
+ALTER TABLE [complex].[audit_log] DROP COLUMN [server_name];
+`,
+ description: "Reverse of complex default expressions - dropping columns with system functions and conditional defaults",
+ },
+ {
+ name: "reverse_comments_with_special_characters",
+ initialSchema: `
+CREATE SCHEMA [special];
+GO
+
+CREATE TABLE [special].[documents] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [title] NVARCHAR(200) NOT NULL,
+ [content] NVARCHAR(MAX),
+ [author] NVARCHAR(100),
+ [created_date] DATETIME2,
+ [metadata] NVARCHAR(500)
+);
+
+CREATE TABLE [special].[translations] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [document_id] INT NOT NULL,
+ [lang_code] NVARCHAR(10) NOT NULL,
+ [translated_title] NVARCHAR(200),
+ [translated_content] NVARCHAR(MAX),
+ CONSTRAINT [fk_translations_doc] FOREIGN KEY ([document_id]) REFERENCES [special].[documents]([id])
+);
+
+CREATE TABLE [special].[test_extreme] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [data] NVARCHAR(MAX)
+);
+GO
+
+-- Add table comments with special characters and quotes
+EXEC sp_addextendedproperty 'MS_Description', 'Document storage system - handles files, "metadata", & special chars: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'documents';
+GO
+
+-- Add multiline comment with special formatting
+EXEC sp_addextendedproperty 'MS_Description',
+'Translation table for international support:
+- Supports Unicode characters: αβγδε, 中文, العربية, русский
+- Handles quotes: "double quotes" and ''single quotes''
+- Special symbols: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'translations';
+GO
+
+-- Column comments with various special cases
+EXEC sp_addextendedproperty 'MS_Description', 'Primary key (auto-increment) - unique ID for each document', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'id';
+EXEC sp_addextendedproperty 'MS_Description', 'Document title - may contain "quotes", ''apostrophes'', and symbols: @#$%', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'title';
+GO
+
+-- Multiline comment with technical details
+EXEC sp_addextendedproperty 'MS_Description',
+'Document content field:
+ • Supports rich text formatting
+ • HTML tags like <p>, <div>, <span>
+ • Special characters: © ® ™ § ¶ † ‡ • ‰ ′ ″
+ • Mathematical: ± × ÷ ≠ ≤ ≥ ∞ ∑ ∏ ∫
+ • Currency: $ € £ ¥ ₹ ₽
+ • Arrows: ← → ↑ ↓ ↔ ⇐ ⇒ ⇔', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'content';
+GO
+
+-- Comment with SQL injection attempt (should be safely handled)
+EXEC sp_addextendedproperty 'MS_Description', 'Author name field - prevents SQL injection like ''; DROP TABLE users; --', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'author';
+GO
+
+-- Comment with JSON-like structure
+EXEC sp_addextendedproperty 'MS_Description', 'Metadata JSON: {"version": "1.0", "tags": ["important", "draft"], "settings": {"public": false}}', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'metadata';
+GO
+
+-- Unicode characters in comments
+EXEC sp_addextendedproperty 'MS_Description', 'Language code: en-US, fr-FR, de-DE, ja-JP, zh-CN, ar-SA, ru-RU, hi-IN, 한국어, ไทย', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'lang_code';
+GO
+
+-- Comment with URLs and file paths
+EXEC sp_addextendedproperty 'MS_Description', 'Translated content - may reference URLs like https://example.com/path?param=value&other=123 or file paths C:\Users\Name\Documents\file.txt', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'translated_content';
+GO
+
+-- Extremely long comment to test limits
+EXEC sp_addextendedproperty 'MS_Description',
+'This is an extremely long comment designed to test the limits of extended property storage in SQL Server. It contains multiple lines of text with various formatting, special characters, and technical information. The purpose is to verify that the migration system can properly handle, store, and retrieve complex comment data without truncation or corruption. This comment includes: numbers (123456789), symbols (!@#$%^&*()_+-={}[]|\:";''<>?,./ ), Unicode characters (αβγδε中文العربيةрусский한국어), and structured data like JSON {"key": "value", "array": [1, 2, 3], "nested": {"deep": true}}. Additionally, it tests SQL-like syntax: SELECT * FROM table WHERE column = ''value'' AND other_column IN (1, 2, 3); as well as HTML markup: <html><body><p class="test">Content</p></body></html> and XML: <?xml version="1.0"?><root><item id="1">Test</item></root>. The comment system should preserve all these characters and structures exactly as written, demonstrating robust handling of complex metadata in database schema documentation.', 'SCHEMA', 'special', 'TABLE', 'test_extreme';
+`,
+ migrationDDL: `
+-- Drop extremely long comment
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'test_extreme';
+GO
+
+-- Drop table with extreme comment
+DROP TABLE [special].[test_extreme];
+GO
+
+-- Drop column comments with special characters
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'translated_content';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'lang_code';
+GO
+
+-- Drop column comments from documents
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'metadata';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'author';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'content';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'title';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'id';
+GO
+
+-- Drop table comments with special characters
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'translations';
+EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents';
+`,
+ description: "Reverse of comments with special characters - dropping comments with quotes, unicode, and special formatting",
+ },
+ {
+ name: "reverse_default_constraint_edge_cases",
+ initialSchema: `
+CREATE SCHEMA [edge];
+GO
+
+CREATE TABLE [edge].[test_defaults] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [nullable_with_default] INT CONSTRAINT [DF_test_defaults_nullable_new] DEFAULT 84,
+ [string_default] NVARCHAR(50) DEFAULT N'Default''s value',
+ [empty_string] NVARCHAR(50) DEFAULT '',
+ [zero_default] INT DEFAULT 0,
+ [negative_default] INT DEFAULT -1,
+ [decimal_precision] DECIMAL(18,6) DEFAULT 123.456789,
+ [special_chars] NVARCHAR(100) DEFAULT N'Line1' + CHAR(13) + CHAR(10) + N'Line2',
+ [unicode_default] NVARCHAR(100) DEFAULT N'你好世界 🌍',
+ [max_int] BIGINT DEFAULT 9223372036854775807,
+ [min_int] BIGINT DEFAULT -9223372036854775808,
+ [json_config] NVARCHAR(MAX) DEFAULT N'{"enabled": true, "count": 0}'
+);
+
+CREATE TABLE [edge].[flags] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [is_enabled] BIT NOT NULL DEFAULT 1,
+ [is_deleted] BIT NOT NULL DEFAULT 0,
+ [is_visible] BIT DEFAULT 1,
+ [is_archived] BIT DEFAULT 0
+);
+`,
+ migrationDDL: `
+-- Drop table with bit field defaults
+DROP TABLE [edge].[flags];
+
+-- Drop default constraints before dropping columns
+-- Drop default constraint for json_config column
+DECLARE @constraint_name NVARCHAR(128);
+SELECT @constraint_name = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'json_config';
+IF @constraint_name IS NOT NULL
+ EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name + ']');
+GO
+
+-- Drop default constraint for min_int column
+DECLARE @constraint_name2 NVARCHAR(128);
+SELECT @constraint_name2 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'min_int';
+IF @constraint_name2 IS NOT NULL
+ EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name2 + ']');
+GO
+
+-- Drop default constraint for max_int column
+DECLARE @constraint_name3 NVARCHAR(128);
+SELECT @constraint_name3 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'max_int';
+IF @constraint_name3 IS NOT NULL
+ EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name3 + ']');
+GO
+
+-- Drop default constraint for unicode_default column
+DECLARE @constraint_name4 NVARCHAR(128);
+SELECT @constraint_name4 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'unicode_default';
+IF @constraint_name4 IS NOT NULL
+ EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name4 + ']');
+GO
+
+-- Drop default constraint for special_chars column
+DECLARE @constraint_name5 NVARCHAR(128);
+SELECT @constraint_name5 = dc.name
+FROM sys.default_constraints dc
+INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
+INNER JOIN sys.tables t ON c.object_id = t.object_id
+INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
+WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'special_chars';
+IF @constraint_name5 IS NOT NULL
+ EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name5 + ']');
+GO
+
+-- Now safe to drop columns
+ALTER TABLE [edge].[test_defaults] DROP COLUMN [json_config];
+ALTER TABLE [edge].[test_defaults] DROP COLUMN [min_int];
+ALTER TABLE [edge].[test_defaults] DROP COLUMN [max_int];
+ALTER TABLE [edge].[test_defaults] DROP COLUMN [unicode_default];
+ALTER TABLE [edge].[test_defaults] DROP COLUMN [special_chars];
+
+-- Remove named default constraint
+ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [DF_test_defaults_nullable_new];
+`,
+ description: "Reverse of edge case defaults - dropping special default constraints and columns",
+ },
+ {
+ name: "spatial_index_operations",
+ initialSchema: `
+CREATE SCHEMA [geo];
+GO
+
+-- Enable spatial types
+CREATE TABLE [geo].[locations] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(200) NOT NULL,
+ [geo_point] GEOMETRY NOT NULL,
+ [geo_polygon] GEOMETRY NOT NULL,
+ [geo_location] GEOGRAPHY NOT NULL,
+ [description] NVARCHAR(MAX)
+);
+`,
+ migrationDDL: `
+-- Add spatial indexes with different configurations
+
+-- GEOMETRY_GRID index with full parameters
+CREATE SPATIAL INDEX [idx_geo_point] ON [geo].[locations] ([geo_point])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-180, -90, 180, 90),
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 32,
+ PAD_INDEX = ON,
+ FILLFACTOR = 85,
+ SORT_IN_TEMPDB = ON,
+ ALLOW_ROW_LOCKS = ON,
+ ALLOW_PAGE_LOCKS = ON,
+ MAXDOP = 2,
+ DATA_COMPRESSION = PAGE
+);
+
+-- GEOMETRY_AUTO_GRID index (requires bounding box)
+CREATE SPATIAL INDEX [idx_geo_polygon] ON [geo].[locations] ([geo_polygon])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-1000, -1000, 1000, 1000),
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW)
+);
+
+-- GEOGRAPHY_GRID index with specific grid levels
+CREATE SPATIAL INDEX [idx_geo_location] ON [geo].[locations] ([geo_location])
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 8,
+ FILLFACTOR = 80
+);
+
+-- Add another table with spatial columns
+CREATE TABLE [geo].[boundaries] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [boundary_name] NVARCHAR(100) NOT NULL,
+ [boundary_shape] GEOMETRY NOT NULL,
+ [center_point] GEOMETRY NOT NULL
+);
+
+-- Add spatial index on new table
+CREATE SPATIAL INDEX [idx_boundary_shape] ON [geo].[boundaries] ([boundary_shape])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (0, 0, 1000, 1000),
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = LOW, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 64
+);
+`,
+ description: "Spatial index operations with various tessellation schemes and parameters",
+ },
+ {
+ name: "reverse_spatial_table_operations",
+ initialSchema: `
+CREATE SCHEMA [geo];
+GO
+
+-- Create table with spatial columns and indexes
+CREATE TABLE [geo].[locations] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(200) NOT NULL,
+ [location] GEOGRAPHY NOT NULL,
+ [area] GEOGRAPHY NOT NULL
+);
+GO
+
+-- Create spatial indexes that will be dropped with the table
+-- Using GEOGRAPHY_GRID which doesn't require BOUNDING_BOX
+CREATE SPATIAL INDEX [idx_location] ON [geo].[locations] ([location])
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 16
+);
+GO
+
+CREATE SPATIAL INDEX [idx_area] ON [geo].[locations] ([area])
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 8
+);
+GO
+
+CREATE TABLE [geo].[regions] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [region_name] NVARCHAR(100) NOT NULL,
+ [boundary] GEOMETRY NOT NULL
+);
+GO
+`,
+ migrationDDL: `
+-- Drop entire table (this drops spatial indexes too)
+DROP TABLE [geo].[locations];
+
+-- Add columns to remaining table
+ALTER TABLE [geo].[regions] ADD [population] INT;
+ALTER TABLE [geo].[regions] ADD [area_sqkm] FLOAT;
+`,
+ description: "Reverse spatial table operations - dropping table with spatial indexes",
+ },
+ {
+ name: "reverse_spatial_indexes",
+ initialSchema: `
+CREATE SCHEMA [geo];
+GO
+
+-- Create table with spatial columns and spatial indexes that can be preserved
+CREATE TABLE [geo].[locations] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(200) NOT NULL,
+ [geo_point] GEOMETRY NOT NULL,
+ [geo_polygon] GEOMETRY NOT NULL,
+ [geo_location] GEOGRAPHY NOT NULL,
+ [description] NVARCHAR(MAX)
+);
+GO
+
+-- Create spatial indexes with basic configuration to avoid preservation issues
+CREATE SPATIAL INDEX [idx_geo_point] ON [geo].[locations] ([geo_point])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-180, -90, 180, 90),
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM)
+);
+GO
+
+CREATE SPATIAL INDEX [idx_geo_location] ON [geo].[locations] ([geo_location])
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM)
+);
+GO
+`,
+ migrationDDL: `
+-- Drop and recreate spatial indexes to test preservation
+DROP INDEX [idx_geo_point] ON [geo].[locations];
+DROP INDEX [idx_geo_location] ON [geo].[locations];
+
+-- Recreate with same configuration (should work with DDL preservation)
+CREATE SPATIAL INDEX [idx_geo_point] ON [geo].[locations] ([geo_point])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-180, -90, 180, 90),
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM)
+);
+
+CREATE SPATIAL INDEX [idx_geo_location] ON [geo].[locations] ([geo_location])
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM)
+);
+`,
+ description: "Reverse spatial index operations - dropping and recreating spatial indexes",
+ },
+ {
+ name: "spatial_index_modifications",
+ initialSchema: `
+CREATE SCHEMA [geo];
+GO
+
+CREATE TABLE [geo].[spatial_data] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [name] NVARCHAR(100) NOT NULL,
+ [location] GEOMETRY NOT NULL,
+ [region] GEOMETRY,
+ [area] GEOGRAPHY
+);
+GO
+
+-- Create initial spatial indexes
+CREATE SPATIAL INDEX [idx_location] ON [geo].[spatial_data] ([location])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-100, -100, 100, 100),
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 16
+);
+GO
+
+CREATE SPATIAL INDEX [idx_area] ON [geo].[spatial_data] ([area])
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 32
+);
+GO
+`,
+ migrationDDL: `
+-- Drop and recreate spatial index with different parameters
+DROP INDEX [idx_location] ON [geo].[spatial_data];
+GO
+
+CREATE SPATIAL INDEX [idx_location] ON [geo].[spatial_data] ([location])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-200, -200, 200, 200), -- Expanded bounding box
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW), -- Changed grid densities
+ CELLS_PER_OBJECT = 64, -- Increased cells per object
+ PAD_INDEX = ON,
+ FILLFACTOR = 90,
+ ALLOW_ROW_LOCKS = ON,
+ ALLOW_PAGE_LOCKS = ON
+);
+GO
+
+-- Add new spatial index on previously unindexed column
+CREATE SPATIAL INDEX [idx_region] ON [geo].[spatial_data] ([region])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-500, -500, 500, 500),
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 32
+);
+GO
+
+-- Drop spatial index on geography column
+DROP INDEX [idx_area] ON [geo].[spatial_data];
+GO
+`,
+ description: "Spatial index modifications - recreating with different parameters and adding/dropping indexes",
+ },
+ {
+ name: "spatial_table_schema_changes",
+ initialSchema: `
+CREATE SCHEMA [geo];
+GO
+
+CREATE TABLE [geo].[points] (
+ [id] INT IDENTITY(1,1) PRIMARY KEY,
+ [point_name] NVARCHAR(50) NOT NULL,
+ [location] GEOMETRY NOT NULL
+);
+GO
+
+CREATE SPATIAL INDEX [idx_points_location] ON [geo].[points] ([location])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (0, 0, 100, 100),
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM)
+);
+GO
+`,
+ migrationDDL: `
+-- Add new spatial and non-spatial columns
+ALTER TABLE [geo].[points] ADD [altitude] FLOAT;
+ALTER TABLE [geo].[points] ADD [boundary] GEOMETRY;
+ALTER TABLE [geo].[points] ADD [geo_location] GEOGRAPHY;
+ALTER TABLE [geo].[points] ADD [created_at] DATETIME2 DEFAULT GETDATE();
+
+-- Create spatial indexes on new columns
+CREATE SPATIAL INDEX [idx_points_boundary] ON [geo].[points] ([boundary])
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-50, -50, 150, 150),
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 8
+);
+GO
+
+CREATE SPATIAL INDEX [idx_points_geo_location] ON [geo].[points] ([geo_location])
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),
+ CELLS_PER_OBJECT = 16
+);
+GO
+
+-- Modify existing column (rename)
+EXEC sp_rename '[geo].[points].[point_name]', 'name', 'COLUMN';
+GO
+`,
+ description: "Spatial table schema changes - adding columns and indexes while preserving existing spatial indexes",
+ },
+ }
+
+ // MSSQL Express in CI gets significantly slower when too many schema-heavy
+ // subtests run at once against the same container. Keep bounded parallelism.
+ const maxConcurrentSubtests = 4
+ parallelGate := make(chan struct{}, maxConcurrentSubtests)
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ t.Parallel()
+ parallelGate <- struct{}{}
+ defer func() {
+ <-parallelGate
+ }()
+
+ // Create test database with unique name
+ testDB := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+ _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", testDB))
+ require.NoError(t, err, "failed to create test database")
+
+ // Connect to test database
+ driver, err := createMSSQLDriver(ctx, container.GetHost(), container.GetPort(), testDB)
+ require.NoError(t, err, "failed to connect to test database")
+ defer driver.Close(ctx)
+
+ // Step 1: Initialize database schema and get schema result A
+ err = executeSQL(ctx, driver, testCase.initialSchema)
+ require.NoError(t, err, "failed to execute initial schema")
+
+ schemaA, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err, "failed to sync schema A")
+
+ // Step 2: Apply migration DDL and get schema result B
+ err = executeSQL(ctx, driver, testCase.migrationDDL)
+ require.NoError(t, err, "failed to execute migration DDL")
+
+ schemaB, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err, "failed to sync schema B")
+
+ // Step 3: Generate rollback DDL using generate_migration
+ // Convert to model schemas for diff
+ dbMetadataA := model.NewDatabaseMetadata(schemaA, nil, nil, storepb.Engine_MSSQL, false)
+ dbMetadataB := model.NewDatabaseMetadata(schemaB, nil, nil, storepb.Engine_MSSQL, false)
+
+ // Get diff from B to A (to generate rollback)
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MSSQL, dbMetadataB, dbMetadataA)
+ require.NoError(t, err, "failed to generate diff")
+
+ rollbackDDL, err := schema.GenerateMigration(storepb.Engine_MSSQL, diff)
+ require.NoError(t, err, "failed to generate rollback migration")
+
+ // Only proceed if there's actual rollback DDL to execute
+ if strings.TrimSpace(rollbackDDL) != "" {
+ // Step 4: Execute rollback DDL and get schema result C
+ err = executeSQL(ctx, driver, rollbackDDL)
+ require.NoError(t, err, "failed to execute rollback DDL: %s", rollbackDDL)
+
+ schemaC, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err, "failed to sync schema C")
+
+ // Step 5: Compare schema results A and C
+ err = compareSchemas(schemaA, schemaC)
+ require.NoError(t, err, "schema comparison failed for test case: %s", testCase.description)
+ }
+ })
+ }
+}
+
+// compareSchemas compares two database schemas and returns an error if they differ
+func compareSchemas(schemaA, schemaC *storepb.DatabaseSchemaMetadata) error {
+ // Sort schemas for consistent comparison
+ normalizeSchema(schemaA)
+ normalizeSchema(schemaC)
+
+ // Use protocmp for detailed comparison, ignoring DefaultConstraintName and Position fields
+ // DefaultConstraintName is only populated when syncing from database, not when parsing SQL
+ // Position must be ignored because:
+ // - In SQL Server, column positions are determined by column_id (physical order)
+ // - When a column is dropped and re-added, it gets a new column_id at the end
+ // - Our migration generator uses DROP+ADD for column renames (doesn't use sp_rename)
+ // - This causes legitimate position changes that don't affect functional equivalence
+ // - Example: table with [id(1), name(2), email(3)] -> DROP name -> ADD name -> [id(1), email(3), name(4)]
+ opts := cmp.Options{
+ protocmp.Transform(),
+ protocmp.IgnoreFields(&storepb.ColumnMetadata{}, "default_constraint_name", "position"),
+ }
+ if diff := cmp.Diff(schemaA, schemaC, opts); diff != "" {
+ return errors.Errorf("schemas differ:\n%s", diff)
+ }
+
+ return nil
+}
+
+// normalizeSchema sorts elements within a schema for consistent comparison
+func normalizeSchema(schema *storepb.DatabaseSchemaMetadata) {
+ if schema == nil {
+ return
+ }
+
+ // Sort schemas
+ slices.SortFunc(schema.Schemas, func(i, j *storepb.SchemaMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ for _, s := range schema.Schemas {
+ // Sort tables
+ slices.SortFunc(s.Tables, func(i, j *storepb.TableMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort views
+ slices.SortFunc(s.Views, func(i, j *storepb.ViewMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort functions
+ slices.SortFunc(s.Functions, func(i, j *storepb.FunctionMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort procedures
+ slices.SortFunc(s.Procedures, func(i, j *storepb.ProcedureMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort table elements
+ for _, table := range s.Tables {
+ // Sort columns
+ slices.SortFunc(table.Columns, func(i, j *storepb.ColumnMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort indexes
+ slices.SortFunc(table.Indexes, func(i, j *storepb.IndexMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort foreign keys
+ slices.SortFunc(table.ForeignKeys, func(i, j *storepb.ForeignKeyMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort check constraints
+ slices.SortFunc(table.CheckConstraints, func(i, j *storepb.CheckConstraintMetadata) int {
+ return strings.Compare(i.Name, j.Name)
+ })
+
+ // Sort expressions within indexes
+ for _, index := range table.Indexes {
+ slices.Sort(index.Expressions)
+ }
+ }
+ }
+}
+
+func TestGetViewDependencies(t *testing.T) {
+ cases := []struct {
+ name string
+ viewDef string
+ schema string
+ expected []string
+ }{
+ {
+ name: "simple",
+ viewDef: "CREATE VIEW [dbo].[v] AS SELECT id FROM [dbo].[a]",
+ schema: "dbo",
+ expected: []string{"dbo.a"},
+ },
+ {
+ name: "union two tables",
+ viewDef: "CREATE VIEW [dbo].[v] AS " +
+ "SELECT id FROM [dbo].[a] UNION SELECT id FROM [dbo].[b]",
+ schema: "dbo",
+ expected: []string{"dbo.a", "dbo.b"},
+ },
+ {
+ name: "union all three tables",
+ viewDef: "CREATE VIEW [dbo].[v] AS " +
+ "SELECT id FROM [dbo].[a] UNION ALL " +
+ "SELECT id FROM [dbo].[b] UNION ALL " +
+ "SELECT id FROM [dbo].[c]",
+ schema: "dbo",
+ expected: []string{"dbo.a", "dbo.b", "dbo.c"},
+ },
+ {
+ // GetQuerySpan with empty mock metadata cannot distinguish CTE
+ // references from real table references, so cte names appear in
+ // the dependency set. This is pre-existing behavior carried over
+ // from the ANTLR implementation; the test pins it down.
+ name: "cte",
+ viewDef: "CREATE VIEW [dbo].[v] AS " +
+ "WITH cte AS (SELECT id FROM [dbo].[a]) " +
+ "SELECT id FROM cte",
+ schema: "dbo",
+ expected: []string{"dbo.a", "dbo.cte"},
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := getViewDependencies(tc.viewDef, tc.schema)
+ require.NoError(t, err)
+ require.ElementsMatch(t, tc.expected, got)
+ })
+ }
+}
diff --git a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
deleted file mode 100644
index 58b288d404..0000000000
--- a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
+++ /dev/null
@@ -1,2499 +0,0 @@
-package mssql
-
-import (
- "context"
- "fmt"
- "slices"
- "strings"
- "testing"
-
- // Import MSSQL driver
- "github.com/google/go-cmp/cmp"
- "github.com/google/uuid"
- _ "github.com/microsoft/go-mssqldb"
- "github.com/pkg/errors"
- "github.com/stretchr/testify/require"
- "google.golang.org/protobuf/testing/protocmp"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-// TestGenerateMigrationWithTestcontainer tests the generate migration function
-// by applying migrations and rollback to verify the schema can be restored.
-//
-//nolint:tparallel
-func TestGenerateMigrationWithTestcontainer(t *testing.T) {
- if testing.Short() {
- t.Skip("Skipping MSSQL testcontainer test in short mode")
- }
-
- ctx := context.Background()
-
- // Start shared MSSQL container for all subtests
- container := testcontainer.SharedMSSQLContainer(t)
-
- // Test cases with various schema changes
- testCases := []struct {
- name string
- initialSchema string
- migrationDDL string
- description string
- }{
- {
- name: "basic_table_operations",
- initialSchema: `
-CREATE SCHEMA [app];
-GO
-
-CREATE TABLE [app].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [created_at] DATETIME2,
- CONSTRAINT [uk_users_email] UNIQUE ([email])
-);
-
-CREATE INDEX [idx_users_username] ON [app].[users] ([username]);
-
-CREATE TABLE [app].[posts] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [title] NVARCHAR(200) NOT NULL,
- [content] NTEXT,
- [published_at] DATETIME2,
- CONSTRAINT [fk_posts_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]) ON DELETE CASCADE
-);
-
-CREATE INDEX [idx_posts_user_id] ON [app].[posts] ([user_id]);
-`,
- migrationDDL: `
--- Add new column
-ALTER TABLE [app].[users] ADD [status] NVARCHAR(20);
-
--- Add new table
-CREATE TABLE [app].[comments] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [post_id] INT NOT NULL,
- [user_id] INT NOT NULL,
- [content] NTEXT NOT NULL,
- [created_at] DATETIME2,
- CONSTRAINT [fk_comments_post] FOREIGN KEY ([post_id]) REFERENCES [app].[posts]([id]) ON DELETE CASCADE,
- CONSTRAINT [fk_comments_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id])
-);
-
--- Add index
-CREATE INDEX [idx_comments_post_id] ON [app].[comments] ([post_id]);
-
--- Add check constraint
-ALTER TABLE [app].[users] ADD CONSTRAINT [ck_users_status] CHECK ([status] IN ('active', 'inactive', 'suspended'));
-`,
- description: "Basic table operations with foreign keys, indexes, and constraints",
- },
- {
- name: "schema_operations",
- initialSchema: `
-CREATE SCHEMA [sales];
-GO
-
-CREATE TABLE [sales].[customers] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [email] NVARCHAR(100)
-);
-
-CREATE TABLE [sales].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [customer_id] INT NOT NULL,
- [order_date] DATE,
- [total] DECIMAL(10,2),
- CONSTRAINT [fk_orders_customer] FOREIGN KEY ([customer_id]) REFERENCES [sales].[customers]([id])
-);
-`,
- migrationDDL: `
--- Create new schema
-CREATE SCHEMA [inventory];
-GO
-
--- Create table in new schema
-CREATE TABLE [inventory].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [category_id] INT,
- CONSTRAINT [ck_products_price] CHECK ([price] > 0)
-);
-
--- Add reference to new table from existing schema
-ALTER TABLE [sales].[orders] ADD [product_id] INT;
-ALTER TABLE [sales].[orders] ADD CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [inventory].[products]([id]);
-`,
- description: "Cross-schema operations with new schema creation",
- },
- {
- name: "view_operations",
- initialSchema: `
-CREATE SCHEMA [reporting];
-GO
-
-CREATE TABLE [reporting].[sales_data] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [product_name] NVARCHAR(100) NOT NULL,
- [quantity] INT NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [sale_date] DATE
-);
-
-CREATE TABLE [reporting].[customers] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [region] NVARCHAR(50)
-);
-`,
- migrationDDL: `
--- Create basic view
-CREATE VIEW [reporting].[product_summary] AS
-SELECT
- [product_name],
- COUNT(*) as [sale_count],
- SUM([quantity]) as [total_quantity],
- AVG([price]) as [avg_price]
-FROM [reporting].[sales_data]
-GROUP BY [product_name];
-GO
-
--- Create dependent view
-CREATE VIEW [reporting].[top_products] AS
-SELECT TOP 10
- [product_name],
- [total_quantity]
-FROM [reporting].[product_summary]
-WHERE [total_quantity] > 100
-ORDER BY [total_quantity] DESC;
-GO
-`,
- description: "View creation with dependencies",
- },
- {
- name: "function_and_procedure_operations",
- initialSchema: `
-CREATE SCHEMA [calc];
-GO
-
-CREATE TABLE [calc].[numbers] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [value] INT NOT NULL,
- [category] NVARCHAR(20)
-);
-`,
- migrationDDL: `
--- Create scalar function
-CREATE FUNCTION [calc].[square](@input INT)
-RETURNS INT
-AS
-BEGIN
- RETURN @input * @input;
-END;
-GO
-
--- Create table-valued function
-CREATE FUNCTION [calc].[get_numbers_by_category](@category NVARCHAR(20))
-RETURNS TABLE
-AS
-RETURN (
- SELECT [id], [value], [category]
- FROM [calc].[numbers]
- WHERE [category] = @category
-);
-GO
-
--- Create stored procedure
-CREATE PROCEDURE [calc].[add_number]
- @value INT,
- @category NVARCHAR(20)
-AS
-BEGIN
- INSERT INTO [calc].[numbers] ([value], [category])
- VALUES (@value, @category);
-END;
-GO
-`,
- description: "Functions and procedures creation",
- },
- {
- name: "index_operations",
- initialSchema: `
-CREATE SCHEMA [perf];
-GO
-
-CREATE TABLE [perf].[events] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [event_name] NVARCHAR(100) NOT NULL,
- [event_data] NVARCHAR(MAX),
- [timestamp] DATETIME2,
- [user_id] INT,
- [category] NVARCHAR(50)
-);
-`,
- migrationDDL: `
--- Add various index types
-CREATE INDEX [idx_events_timestamp] ON [perf].[events] ([timestamp]);
-CREATE INDEX [idx_events_user_category] ON [perf].[events] ([user_id], [category]);
-CREATE UNIQUE INDEX [idx_events_name_timestamp] ON [perf].[events] ([event_name], [timestamp]);
-
--- Add filtered index (using deterministic date)
-CREATE INDEX [idx_events_recent] ON [perf].[events] ([event_name])
-WHERE [timestamp] >= '2023-01-01';
-
--- Add computed column first
-ALTER TABLE [perf].[events] ADD [event_month] AS DATEPART(MONTH, [timestamp]);
-
--- Add index on computed column
-CREATE INDEX [idx_events_month] ON [perf].[events] ([event_month]);
-`,
- description: "Various index types including filtered and computed column indexes",
- },
- {
- name: "complex_constraints",
- initialSchema: `
-CREATE SCHEMA [finance];
-GO
-
-CREATE TABLE [finance].[accounts] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [account_number] NVARCHAR(20) NOT NULL,
- [balance] DECIMAL(15,2),
- [account_type] NVARCHAR(20) NOT NULL,
- [created_at] DATETIME2 DEFAULT GETDATE()
-);
-`,
- migrationDDL: `
--- Add multiple check constraints
-ALTER TABLE [finance].[accounts] ADD CONSTRAINT [ck_accounts_balance] CHECK ([balance] >= 0);
-ALTER TABLE [finance].[accounts] ADD CONSTRAINT [ck_accounts_type] CHECK ([account_type] IN ('CHECKING', 'SAVINGS', 'CREDIT'));
-
--- Add unique constraint
-ALTER TABLE [finance].[accounts] ADD CONSTRAINT [uk_accounts_number] UNIQUE ([account_number]);
-
--- Create related table with foreign key
-CREATE TABLE [finance].[transactions] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [account_id] INT NOT NULL,
- [amount] DECIMAL(15,2) NOT NULL,
- [transaction_type] NVARCHAR(20) NOT NULL,
- [transaction_date] DATETIME2,
- CONSTRAINT [fk_transactions_account] FOREIGN KEY ([account_id]) REFERENCES [finance].[accounts]([id]),
- CONSTRAINT [ck_transactions_amount] CHECK ([amount] != 0),
- CONSTRAINT [ck_transactions_type] CHECK ([transaction_type] IN ('DEBIT', 'CREDIT'))
-);
-`,
- description: "Complex constraints with multiple check constraints and foreign keys",
- },
- {
- name: "column_modifications",
- initialSchema: `
-CREATE SCHEMA [hr];
-GO
-
-CREATE TABLE [hr].[employees] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [first_name] NVARCHAR(50),
- [last_name] NVARCHAR(50),
- [email] NVARCHAR(100),
- [phone] VARCHAR(20),
- [hire_date] DATE,
- [salary] DECIMAL(10,2)
-);
-`,
- migrationDDL: `
--- Add new columns
-ALTER TABLE [hr].[employees] ADD [department_id] INT;
-ALTER TABLE [hr].[employees] ADD [manager_id] INT;
-ALTER TABLE [hr].[employees] ADD [status] NVARCHAR(20);
-
--- Create department table
-CREATE TABLE [hr].[departments] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [budget] DECIMAL(15,2)
-);
-
--- Add foreign key constraints
-ALTER TABLE [hr].[employees] ADD CONSTRAINT [fk_employees_department] FOREIGN KEY ([department_id]) REFERENCES [hr].[departments]([id]);
-ALTER TABLE [hr].[employees] ADD CONSTRAINT [fk_employees_manager] FOREIGN KEY ([manager_id]) REFERENCES [hr].[employees]([id]);
-
--- Add check constraints
-ALTER TABLE [hr].[employees] ADD CONSTRAINT [ck_employees_salary] CHECK ([salary] > 0);
-ALTER TABLE [hr].[employees] ADD CONSTRAINT [ck_employees_status] CHECK ([status] IN ('ACTIVE', 'INACTIVE', 'TERMINATED'));
-`,
- description: "Column additions and self-referencing foreign keys",
- },
- {
- name: "multiple_schema_dependencies",
- initialSchema: `
-CREATE SCHEMA [core];
-GO
-CREATE SCHEMA [app];
-GO
-
-CREATE TABLE [core].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL
-);
-
-CREATE TABLE [core].[roles] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(50) NOT NULL
-);
-`,
- migrationDDL: `
--- Create user_roles junction table in core schema
-CREATE TABLE [core].[user_roles] (
- [user_id] INT NOT NULL,
- [role_id] INT NOT NULL,
- [assigned_at] DATETIME2,
- CONSTRAINT [pk_user_roles] PRIMARY KEY ([user_id], [role_id]),
- CONSTRAINT [fk_user_roles_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE,
- CONSTRAINT [fk_user_roles_role] FOREIGN KEY ([role_id]) REFERENCES [core].[roles]([id]) ON DELETE CASCADE
-);
-
--- Create application tables that reference core schema
-CREATE TABLE [app].[sessions] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [token] NVARCHAR(255) NOT NULL,
- [expires_at] DATETIME2 NOT NULL,
- CONSTRAINT [fk_sessions_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE
-);
-
-CREATE TABLE [app].[audit_log] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT,
- [action] NVARCHAR(100) NOT NULL,
- [timestamp] DATETIME2,
- [details] NVARCHAR(MAX),
- CONSTRAINT [fk_audit_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id])
-);
-`,
- description: "Multiple schema dependencies with cross-schema foreign keys",
- },
- {
- name: "computed_columns_and_triggers",
- initialSchema: `
-CREATE SCHEMA [ecommerce];
-GO
-
-CREATE TABLE [ecommerce].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [tax_rate] DECIMAL(5,4)
-);
-`,
- migrationDDL: `
--- Add computed columns
-ALTER TABLE [ecommerce].[products] ADD [price_with_tax] AS ([price] * (1 + [tax_rate]));
-GO
-
-ALTER TABLE [ecommerce].[products] ADD [price_category] AS (
- CASE
- WHEN [price] < 10 THEN 'Budget'
- WHEN [price] < 100 THEN 'Standard'
- ELSE 'Premium'
- END
-);
-GO
-
--- Create order table
-CREATE TABLE [ecommerce].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [product_id] INT NOT NULL,
- [quantity] INT NOT NULL,
- [unit_price] DECIMAL(10,2) NOT NULL,
- [total] DECIMAL(15,2),
- [order_date] DATETIME2,
- CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [ecommerce].[products]([id])
-);
-GO
-
--- Add computed column for total
-ALTER TABLE [ecommerce].[orders] ADD [computed_total] AS ([quantity] * [unit_price]);
-GO
-
--- Add indexes on computed columns
-CREATE INDEX [idx_products_price_category] ON [ecommerce].[products] ([price_category]);
-CREATE INDEX [idx_products_price_with_tax] ON [ecommerce].[products] ([price_with_tax]);
-`,
- description: "Computed columns with indexes",
- },
- {
- name: "create_tables_with_fk",
- initialSchema: `
-CREATE SCHEMA [test];
-GO
-
-CREATE TABLE [test].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [created_at] DATETIME2,
- CONSTRAINT [uk_email] UNIQUE ([email])
-);
-
-CREATE INDEX [idx_username] ON [test].[users] ([username]);
-
-CREATE TABLE [test].[posts] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [title] NVARCHAR(200) NOT NULL,
- [content] NVARCHAR(MAX),
- [published_at] DATETIME2,
- CONSTRAINT [fk_user] FOREIGN KEY ([user_id]) REFERENCES [test].[users]([id]) ON DELETE CASCADE
-);
-
-CREATE INDEX [idx_user_id] ON [test].[posts] ([user_id]);
-`,
- migrationDDL: `
-DROP TABLE [test].[posts];
-DROP TABLE [test].[users];
-DROP SCHEMA [test];`,
- description: "Create tables with foreign key constraints",
- },
- {
- name: "multiple_foreign_keys",
- initialSchema: `
-CREATE SCHEMA [test];
-GO
-
-CREATE TABLE [test].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [created_at] DATETIME2,
- CONSTRAINT [uk_email] UNIQUE ([email])
-);
-GO
-
-CREATE INDEX [idx_username] ON [test].[users] ([username]);
-GO
-
-CREATE TABLE [test].[posts] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [title] NVARCHAR(200) NOT NULL,
- [content] NVARCHAR(MAX),
- [published_at] DATETIME2,
- CONSTRAINT [fk_user] FOREIGN KEY ([user_id]) REFERENCES [test].[users]([id]) ON DELETE CASCADE
-);
-GO
-
-CREATE INDEX [idx_user_id] ON [test].[posts] ([user_id]);
-GO
-`,
- migrationDDL: `
--- Add new column
-ALTER TABLE [test].[users] ADD [is_active] BIT;
-GO
-
--- Create new table with multiple foreign keys
-CREATE TABLE [test].[comments] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [post_id] INT NOT NULL,
- [user_id] INT NOT NULL,
- [content] NVARCHAR(MAX) NOT NULL,
- [created_at] DATETIME2,
- CONSTRAINT [fk_comment_post] FOREIGN KEY ([post_id]) REFERENCES [test].[posts]([id]) ON DELETE CASCADE,
- CONSTRAINT [fk_comment_user] FOREIGN KEY ([user_id]) REFERENCES [test].[users]([id]) ON DELETE NO ACTION
-);
-GO
-
-CREATE INDEX [idx_post_user] ON [test].[comments] ([post_id], [user_id]);
-
--- Add new index
-CREATE INDEX [idx_email_active] ON [test].[users] ([email], [is_active]);
-
--- Add check constraint
-ALTER TABLE [test].[posts] ADD CONSTRAINT [chk_title_length] CHECK (LEN([title]) > 0);
-`,
- description: "Tables with multiple foreign key constraints",
- },
- {
- name: "drop_and_recreate_fk_constraints",
- initialSchema: `
-CREATE SCHEMA [library];
-GO
-
-CREATE TABLE [library].[authors] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [email] NVARCHAR(100),
- CONSTRAINT [uk_email] UNIQUE ([email])
-);
-
-CREATE TABLE [library].[books] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [title] NVARCHAR(200) NOT NULL,
- [author_id] INT NOT NULL,
- [isbn] NVARCHAR(20),
- [published_year] INT,
- [price] DECIMAL(8, 2),
- CONSTRAINT [fk_author] FOREIGN KEY ([author_id]) REFERENCES [library].[authors]([id]),
- CONSTRAINT [chk_year_valid] CHECK ([published_year] >= 1000 AND [published_year] <= 2100),
- CONSTRAINT [chk_price_positive] CHECK ([price] > 0),
- CONSTRAINT [uk_isbn] UNIQUE ([isbn])
-);
-
-CREATE INDEX [idx_author] ON [library].[books] ([author_id]);
-CREATE INDEX [idx_year] ON [library].[books] ([published_year]);
-`,
- migrationDDL: `
--- Drop and recreate foreign key with different options
-ALTER TABLE [library].[books] DROP CONSTRAINT [fk_author];
-ALTER TABLE [library].[books] ADD CONSTRAINT [fk_author_new] FOREIGN KEY ([author_id]) REFERENCES [library].[authors]([id]) ON DELETE CASCADE ON UPDATE CASCADE;
-
--- Drop and modify check constraints
-ALTER TABLE [library].[books] DROP CONSTRAINT [chk_year_valid];
-ALTER TABLE [library].[books] ADD CONSTRAINT [chk_year_extended] CHECK ([published_year] >= 1000 AND [published_year] <= 2030);
-
--- Add new constraints
-ALTER TABLE [library].[books] ADD CONSTRAINT [chk_title_length] CHECK (LEN([title]) >= 3);
-`,
- description: "Drop and recreate foreign key constraints with different options",
- },
- {
- name: "self_referencing_foreign_keys",
- initialSchema: `
-CREATE SCHEMA [company];
-GO
-
-CREATE TABLE [company].[departments] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [manager_id] INT
-);
-
-CREATE TABLE [company].[employees] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [department_id] INT,
- [salary] DECIMAL(10, 2),
- [hire_date] DATE,
- CONSTRAINT [fk_dept] FOREIGN KEY ([department_id]) REFERENCES [company].[departments]([id])
-);
-
-CREATE INDEX [idx_dept] ON [company].[employees] ([department_id]);
-
--- Add self-referencing foreign key
-ALTER TABLE [company].[departments] ADD CONSTRAINT [fk_manager] FOREIGN KEY ([manager_id]) REFERENCES [company].[employees]([id]);
-`,
- migrationDDL: `
--- Create base view
-CREATE VIEW [company].[dept_employee_count] AS
-SELECT d.[id] AS dept_id, d.[name] AS dept_name, COUNT(e.[id]) AS emp_count
-FROM [company].[departments] d
-LEFT JOIN [company].[employees] e ON d.[id] = e.[department_id]
-GROUP BY d.[id], d.[name];
-GO
-
--- Create dependent view
-CREATE VIEW [company].[dept_summary] AS
-SELECT
- dept_id,
- dept_name,
- emp_count,
- 0 AS avg_salary,
- 0 AS max_salary,
- 0 AS min_salary
-FROM [company].[dept_employee_count];
-GO
-
--- Create highly dependent view
-CREATE VIEW [company].[dept_manager_summary] AS
-SELECT
- ds.dept_id,
- ds.dept_name,
- ds.emp_count,
- ds.avg_salary,
- m.[name] AS manager_name
-FROM [company].[dept_summary] ds
-JOIN [company].[departments] d ON ds.dept_id = d.[id]
-LEFT JOIN [company].[employees] m ON d.[manager_id] = m.[id];
-GO
-
--- Create stored procedure using views
-CREATE PROCEDURE [company].[GetDepartmentReport]
- @dept_name_pattern NVARCHAR(100)
-AS
-BEGIN
- SELECT * FROM [company].[dept_manager_summary]
- WHERE dept_name LIKE '%' + @dept_name_pattern + '%';
-END;
-`,
- description: "Self-referencing foreign keys and complex view dependencies",
- },
- {
- name: "table_and_column_comments",
- initialSchema: `
-CREATE SCHEMA [app];
-GO
-
-CREATE TABLE [app].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [created_at] DATETIME2,
- [status] NVARCHAR(20)
-);
-
-CREATE TABLE [app].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [category] NVARCHAR(50)
-);
-
-CREATE INDEX [idx_users_email] ON [app].[users] ([email]);
-CREATE INDEX [idx_products_category] ON [app].[products] ([category]);
-`,
- migrationDDL: `
--- Add table comments using extended properties
-EXEC sp_addextendedproperty 'MS_Description', 'User accounts and profile information', 'SCHEMA', 'app', 'TABLE', 'users';
-EXEC sp_addextendedproperty 'MS_Description', 'Product catalog with pricing information', 'SCHEMA', 'app', 'TABLE', 'products';
-GO
-
--- Add column comments for users table
-EXEC sp_addextendedproperty 'MS_Description', 'Unique identifier for each user', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Unique username for login authentication', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'username';
-EXEC sp_addextendedproperty 'MS_Description', 'User email address for notifications', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'email';
-EXEC sp_addextendedproperty 'MS_Description', 'Timestamp when the user account was created', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'created_at';
-EXEC sp_addextendedproperty 'MS_Description', 'Current status: active, inactive, or suspended', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'status';
-GO
-
--- Add column comments for products table
-EXEC sp_addextendedproperty 'MS_Description', 'Unique product identifier', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Product display name', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'name';
-EXEC sp_addextendedproperty 'MS_Description', 'Product price in USD', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'price';
-EXEC sp_addextendedproperty 'MS_Description', 'Product category classification', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'category';
-GO
-
--- Create new table and add comments immediately
-CREATE TABLE [app].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [product_id] INT NOT NULL,
- [quantity] INT NOT NULL,
- [order_date] DATETIME2,
- CONSTRAINT [fk_orders_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]),
- CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [app].[products]([id])
-);
-GO
-
--- Add comments to the new table
-EXEC sp_addextendedproperty 'MS_Description', 'Customer orders tracking system', 'SCHEMA', 'app', 'TABLE', 'orders';
-EXEC sp_addextendedproperty 'MS_Description', 'Unique order identifier', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Reference to the user who placed the order', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'user_id';
-EXEC sp_addextendedproperty 'MS_Description', 'Reference to the ordered product', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'product_id';
-EXEC sp_addextendedproperty 'MS_Description', 'Number of items ordered', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'quantity';
-EXEC sp_addextendedproperty 'MS_Description', 'When the order was placed', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'order_date';
-`,
- description: "Adding comments to tables and columns using SQL Server extended properties",
- },
- {
- name: "modify_and_drop_comments",
- initialSchema: `
-CREATE SCHEMA [test];
-GO
-
-CREATE TABLE [test].[customers] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [email] NVARCHAR(100),
- [phone] NVARCHAR(20),
- [created_at] DATETIME2 DEFAULT GETDATE()
-);
-GO
-
--- Add initial comments
-EXEC sp_addextendedproperty 'MS_Description', 'Customer information database', 'SCHEMA', 'test', 'TABLE', 'customers';
-EXEC sp_addextendedproperty 'MS_Description', 'Customer unique ID', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Customer full name', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'name';
-EXEC sp_addextendedproperty 'MS_Description', 'Customer email for marketing', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'email';
-EXEC sp_addextendedproperty 'MS_Description', 'Customer contact number', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'phone';
-GO
-
-CREATE TABLE [test].[invoices] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [customer_id] INT NOT NULL,
- [amount] DECIMAL(10,2) NOT NULL,
- [invoice_date] DATE,
- CONSTRAINT [fk_invoices_customer] FOREIGN KEY ([customer_id]) REFERENCES [test].[customers]([id])
-);
-GO
-
--- Add comments to invoices table
-EXEC sp_addextendedproperty 'MS_Description', 'Invoice records for billing', 'SCHEMA', 'test', 'TABLE', 'invoices';
-EXEC sp_addextendedproperty 'MS_Description', 'Invoice number', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Link to customer', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'customer_id';
-`,
- migrationDDL: `
--- Update existing table comment
-EXEC sp_updateextendedproperty 'MS_Description', 'Complete customer database with contact details and preferences', 'SCHEMA', 'test', 'TABLE', 'customers';
-GO
-
--- Update existing column comments
-EXEC sp_updateextendedproperty 'MS_Description', 'Primary key - auto-generated customer identifier', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'id';
-EXEC sp_updateextendedproperty 'MS_Description', 'Customer business or personal name', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'name';
-EXEC sp_updateextendedproperty 'MS_Description', 'Primary email address for communications and billing', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'email';
-GO
-
--- Drop specific column comment
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'phone';
-GO
-
--- Add new comment to column that didn't have one
-EXEC sp_addextendedproperty 'MS_Description', 'Account creation timestamp for auditing', 'SCHEMA', 'test', 'TABLE', 'customers', 'COLUMN', 'created_at';
-GO
-
--- Update invoice table comment
-EXEC sp_updateextendedproperty 'MS_Description', 'Financial invoice tracking and billing system', 'SCHEMA', 'test', 'TABLE', 'invoices';
-GO
-
--- Drop invoice column comments
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'id';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'customer_id';
-GO
-
--- Add new column with comment
-ALTER TABLE [test].[invoices] ADD [payment_status] NVARCHAR(20);
-GO
-
-EXEC sp_addextendedproperty 'MS_Description', 'Current payment status: pending, paid, overdue, cancelled', 'SCHEMA', 'test', 'TABLE', 'invoices', 'COLUMN', 'payment_status';
-`,
- description: "Modifying existing comments and removing comments using extended properties",
- },
- {
- name: "comments_with_special_characters",
- initialSchema: `
-CREATE SCHEMA [special];
-GO
-
-CREATE TABLE [special].[documents] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [title] NVARCHAR(200) NOT NULL,
- [content] NVARCHAR(MAX),
- [author] NVARCHAR(100),
- [created_date] DATETIME2,
- [metadata] NVARCHAR(500)
-);
-
-CREATE TABLE [special].[translations] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [document_id] INT NOT NULL,
- [lang_code] NVARCHAR(10) NOT NULL,
- [translated_title] NVARCHAR(200),
- [translated_content] NVARCHAR(MAX),
- CONSTRAINT [fk_translations_doc] FOREIGN KEY ([document_id]) REFERENCES [special].[documents]([id])
-);
-`,
- migrationDDL: `
--- Add table comments with special characters and quotes
-EXEC sp_addextendedproperty 'MS_Description', 'Document storage system - handles files, "metadata", & special chars: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'documents';
-GO
-
--- Add multiline comment with special formatting
-EXEC sp_addextendedproperty 'MS_Description',
-'Translation table for international support:
-- Supports Unicode characters: αβγδε, 中文, العربية, русский
-- Handles quotes: "double quotes" and ''single quotes''
-- Special symbols: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'translations';
-GO
-
--- Column comments with various special cases
-EXEC sp_addextendedproperty 'MS_Description', 'Primary key (auto-increment) - unique ID for each document', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Document title - may contain "quotes", ''apostrophes'', and symbols: @#$%', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'title';
-GO
-
--- Multiline comment with technical details
-EXEC sp_addextendedproperty 'MS_Description',
-'Document content field:
- • Supports rich text formatting
- • HTML tags like <p>, <div>, <span>
- • Special characters: © ® ™ § ¶ † ‡ • ‰ ′ ″
- • Mathematical: ± × ÷ ≠ ≤ ≥ ∞ ∑ ∏ ∫
- • Currency: $ € £ ¥ ₹ ₽
- • Arrows: ← → ↑ ↓ ↔ ⇐ ⇒ ⇔', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'content';
-GO
-
--- Comment with SQL injection attempt (should be safely handled)
-EXEC sp_addextendedproperty 'MS_Description', 'Author name field - prevents SQL injection like ''; DROP TABLE users; --', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'author';
-GO
-
--- Comment with JSON-like structure
-EXEC sp_addextendedproperty 'MS_Description', 'Metadata JSON: {"version": "1.0", "tags": ["important", "draft"], "settings": {"public": false}}', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'metadata';
-GO
-
--- Unicode characters in comments
-EXEC sp_addextendedproperty 'MS_Description', 'Language code: en-US, fr-FR, de-DE, ja-JP, zh-CN, ar-SA, ru-RU, hi-IN, 한국어, ไทย', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'lang_code';
-GO
-
--- Comment with URLs and file paths
-EXEC sp_addextendedproperty 'MS_Description', 'Translated content - may reference URLs like https://example.com/path?param=value&other=123 or file paths C:\Users\Name\Documents\file.txt', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'translated_content';
-GO
-
--- Create new table with extreme comment case
-CREATE TABLE [special].[test_extreme] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [data] NVARCHAR(MAX)
-);
-GO
-
--- Extremely long comment to test limits
-EXEC sp_addextendedproperty 'MS_Description',
-'This is an extremely long comment designed to test the limits of extended property storage in SQL Server. It contains multiple lines of text with various formatting, special characters, and technical information. The purpose is to verify that the migration system can properly handle, store, and retrieve complex comment data without truncation or corruption. This comment includes: numbers (123456789), symbols (!@#$%^&*()_+-={}[]|\:";''<>?,./ ), Unicode characters (αβγδε中文العربيةрусский한국어), and structured data like JSON {"key": "value", "array": [1, 2, 3], "nested": {"deep": true}}. Additionally, it tests SQL-like syntax: SELECT * FROM table WHERE column = ''value'' AND other_column IN (1, 2, 3); as well as HTML markup: <html><body><p class="test">Content</p></body></html> and XML: <?xml version="1.0"?><root><item id="1">Test</item></root>. The comment system should preserve all these characters and structures exactly as written, demonstrating robust handling of complex metadata in database schema documentation.', 'SCHEMA', 'special', 'TABLE', 'test_extreme';
-`,
- description: "Comments with special characters, quotes, multiline text, and Unicode",
- },
- {
- name: "default_constraint_operations",
- initialSchema: `
-CREATE SCHEMA [defaults];
-GO
-
-CREATE TABLE [defaults].[employees] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [hire_date] DATE NOT NULL DEFAULT GETDATE(),
- [status] NVARCHAR(20) NOT NULL DEFAULT 'active',
- [is_active] BIT NOT NULL DEFAULT 1,
- [salary] DECIMAL(10,2) DEFAULT 50000.00,
- [department_id] INT DEFAULT 1,
- [created_at] DATETIME2 DEFAULT SYSDATETIME(),
- [updated_at] DATETIME2
-);
-
-CREATE TABLE [defaults].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- [stock] INT NOT NULL DEFAULT 0,
- [is_available] BIT DEFAULT 1
-);
-`,
- migrationDDL: `
--- Add column with default
-ALTER TABLE [defaults].[employees] ADD [vacation_days] INT NOT NULL DEFAULT 15;
-
--- Remove default constraint (by adding new column without default)
-ALTER TABLE [defaults].[products] ADD [cost] DECIMAL(10,2) NOT NULL;
-
--- Modify existing default value
--- First drop the existing constraint
-DECLARE @constraint_name NVARCHAR(256);
-SELECT @constraint_name = dc.name
-FROM sys.default_constraints dc
-JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
-WHERE OBJECT_NAME(dc.parent_object_id) = 'employees'
-AND c.name = 'status'
-AND OBJECT_SCHEMA_NAME(dc.parent_object_id) = 'defaults';
-IF @constraint_name IS NOT NULL
- EXEC('ALTER TABLE [defaults].[employees] DROP CONSTRAINT [' + @constraint_name + ']');
-GO
-
--- Add new default
-ALTER TABLE [defaults].[employees] ADD CONSTRAINT [DF_employees_status_new] DEFAULT 'pending' FOR [status];
-
--- Add default with expression
-ALTER TABLE [defaults].[employees] ADD [bonus_percentage] DECIMAL(5,2) DEFAULT (CASE WHEN MONTH(GETDATE()) = 12 THEN 10.0 ELSE 5.0 END);
-
--- Create new table with various defaults
-CREATE TABLE [defaults].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [order_number] NVARCHAR(50) NOT NULL DEFAULT CONCAT('ORD-', YEAR(GETDATE()), '-', FORMAT(GETDATE(), 'MMdd')),
- [order_date] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
- [status] NVARCHAR(20) NOT NULL DEFAULT 'pending',
- [total] DECIMAL(10,2) NOT NULL DEFAULT 0,
- [discount] DECIMAL(5,2) DEFAULT NULL,
- [is_paid] BIT NOT NULL DEFAULT 0,
- [notes] NVARCHAR(MAX) DEFAULT 'No notes'
-);
-`,
- description: "Default constraint operations including add, remove, modify with various data types and expressions",
- },
- {
- name: "complex_default_expressions",
- initialSchema: `
-CREATE SCHEMA [complex];
-GO
-
-CREATE TABLE [complex].[audit_log] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [event_type] NVARCHAR(50) NOT NULL,
- [user_name] NVARCHAR(100) NOT NULL DEFAULT SYSTEM_USER,
- [event_time] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
- [ip_address] NVARCHAR(45),
- [session_id] UNIQUEIDENTIFIER DEFAULT NEWID()
-);
-
-CREATE TABLE [complex].[documents] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [title] NVARCHAR(200) NOT NULL,
- [content] NVARCHAR(MAX),
- [version] INT NOT NULL DEFAULT 1,
- [created_date] DATE DEFAULT CAST(GETDATE() AS DATE),
- [expiry_date] DATE DEFAULT DATEADD(YEAR, 1, CAST(GETDATE() AS DATE))
-);
-`,
- migrationDDL: `
--- Add columns with complex default expressions
-ALTER TABLE [complex].[audit_log] ADD [server_name] NVARCHAR(100) DEFAULT @@SERVERNAME;
-ALTER TABLE [complex].[audit_log] ADD [database_name] NVARCHAR(100) DEFAULT DB_NAME();
-ALTER TABLE [complex].[audit_log] ADD [schema_name] NVARCHAR(100) DEFAULT SCHEMA_NAME();
-
--- Add computed default based on other columns
-ALTER TABLE [complex].[documents] ADD [document_code] NVARCHAR(50) DEFAULT CONCAT('DOC-', FORMAT(GETDATE(), 'yyyyMMdd'), '-', NEWID());
-
--- Create table with function-based defaults
-CREATE TABLE [complex].[calculations] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [value1] DECIMAL(10,2) NOT NULL,
- [value2] DECIMAL(10,2) NOT NULL,
- [pi_value] DECIMAL(10,8) DEFAULT PI(),
- [random_value] FLOAT DEFAULT RAND(),
- [checksum_value] INT DEFAULT CHECKSUM(NEWID()),
- [created_at] DATETIME2 DEFAULT SYSDATETIME(),
- [created_offset] DATETIMEOFFSET DEFAULT SYSDATETIMEOFFSET()
-);
-
--- Add default with CASE expression
-ALTER TABLE [complex].[documents] ADD [priority] INT DEFAULT
- CASE
- WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 1
- WHEN DATEPART(HOUR, GETDATE()) < 18 THEN 2
- ELSE 3
- END;
-`,
- description: "Complex default expressions using system functions, variables, and conditional logic",
- },
- {
- name: "default_constraint_edge_cases",
- initialSchema: `
-CREATE SCHEMA [edge];
-GO
-
-CREATE TABLE [edge].[test_defaults] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [nullable_with_default] INT DEFAULT 42,
- [string_default] NVARCHAR(50) DEFAULT N'Default''s value',
- [empty_string] NVARCHAR(50) DEFAULT '',
- [zero_default] INT DEFAULT 0,
- [negative_default] INT DEFAULT -1,
- [decimal_precision] DECIMAL(18,6) DEFAULT 123.456789
-);
-`,
- migrationDDL: `
--- Test dropping and re-adding defaults with different names
-DECLARE @constraint_name NVARCHAR(256);
--- Drop nullable_with_default's constraint
-SELECT @constraint_name = dc.name
-FROM sys.default_constraints dc
-JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
-WHERE OBJECT_NAME(dc.parent_object_id) = 'test_defaults'
-AND c.name = 'nullable_with_default'
-AND OBJECT_SCHEMA_NAME(dc.parent_object_id) = 'edge';
-IF @constraint_name IS NOT NULL
- EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name + ']');
-GO
-
--- Add it back with a new value
-ALTER TABLE [edge].[test_defaults] ADD CONSTRAINT [DF_test_defaults_nullable_new] DEFAULT 84 FOR [nullable_with_default];
-
--- Add column with special characters in default
-ALTER TABLE [edge].[test_defaults] ADD [special_chars] NVARCHAR(100) DEFAULT N'Line1' + CHAR(13) + CHAR(10) + N'Line2';
-
--- Add column with unicode default
-ALTER TABLE [edge].[test_defaults] ADD [unicode_default] NVARCHAR(100) DEFAULT N'你好世界 🌍';
-
--- Add column with max value defaults
-ALTER TABLE [edge].[test_defaults] ADD [max_int] BIGINT DEFAULT 9223372036854775807;
-ALTER TABLE [edge].[test_defaults] ADD [min_int] BIGINT DEFAULT -9223372036854775808;
-
--- Create table with bit field defaults
-CREATE TABLE [edge].[flags] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [is_enabled] BIT NOT NULL DEFAULT 1,
- [is_deleted] BIT NOT NULL DEFAULT 0,
- [is_visible] BIT DEFAULT 1,
- [is_archived] BIT DEFAULT 0
-);
-
--- Add JSON default (for NVARCHAR storing JSON)
-ALTER TABLE [edge].[test_defaults] ADD [json_config] NVARCHAR(MAX) DEFAULT N'{"enabled": true, "count": 0}';
-`,
- description: "Edge cases for default constraints including special characters, unicode, extreme values",
- },
- {
- name: "reverse_basic_table_operations",
- initialSchema: `
-CREATE SCHEMA [app];
-GO
-
-CREATE TABLE [app].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [created_at] DATETIME2,
- [status] NVARCHAR(20),
- CONSTRAINT [uk_users_email] UNIQUE ([email]),
- CONSTRAINT [ck_users_status] CHECK ([status] IN ('active', 'inactive', 'suspended'))
-);
-
-CREATE INDEX [idx_users_username] ON [app].[users] ([username]);
-
-CREATE TABLE [app].[posts] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [title] NVARCHAR(200) NOT NULL,
- [content] NTEXT,
- [published_at] DATETIME2,
- CONSTRAINT [fk_posts_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]) ON DELETE CASCADE
-);
-
-CREATE INDEX [idx_posts_user_id] ON [app].[posts] ([user_id]);
-
-CREATE TABLE [app].[comments] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [post_id] INT NOT NULL,
- [user_id] INT NOT NULL,
- [content] NTEXT NOT NULL,
- [created_at] DATETIME2,
- CONSTRAINT [fk_comments_post] FOREIGN KEY ([post_id]) REFERENCES [app].[posts]([id]) ON DELETE CASCADE,
- CONSTRAINT [fk_comments_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id])
-);
-
-CREATE INDEX [idx_comments_post_id] ON [app].[comments] ([post_id]);
-`,
- migrationDDL: `
--- Drop index
-DROP INDEX [idx_comments_post_id] ON [app].[comments];
-
--- Drop check constraint
-ALTER TABLE [app].[users] DROP CONSTRAINT [ck_users_status];
-
--- Drop table (must drop in correct order due to foreign keys)
-DROP TABLE [app].[comments];
-
--- Drop column
-ALTER TABLE [app].[users] DROP COLUMN [status];
-`,
- description: "Reverse of basic table operations - dropping column, table, index, and constraint",
- },
- {
- name: "reverse_schema_operations",
- initialSchema: `
-CREATE SCHEMA [sales];
-GO
-
-CREATE SCHEMA [inventory];
-GO
-
-CREATE TABLE [sales].[customers] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [email] NVARCHAR(100)
-);
-
-CREATE TABLE [sales].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [customer_id] INT NOT NULL,
- [order_date] DATE,
- [total] DECIMAL(10,2),
- [product_id] INT,
- CONSTRAINT [fk_orders_customer] FOREIGN KEY ([customer_id]) REFERENCES [sales].[customers]([id])
-);
-
-CREATE TABLE [inventory].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [category_id] INT,
- CONSTRAINT [ck_products_price] CHECK ([price] > 0)
-);
-
-ALTER TABLE [sales].[orders] ADD CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [inventory].[products]([id]);
-`,
- migrationDDL: `
--- Drop foreign key referencing table in different schema
-ALTER TABLE [sales].[orders] DROP CONSTRAINT [fk_orders_product];
-
--- Drop column referencing another schema
-ALTER TABLE [sales].[orders] DROP COLUMN [product_id];
-
--- Drop table in inventory schema
-DROP TABLE [inventory].[products];
-
--- Drop the inventory schema
-DROP SCHEMA [inventory];
-`,
- description: "Reverse of schema operations - dropping cross-schema foreign key, table, and schema",
- },
- {
- name: "reverse_view_operations",
- initialSchema: `
-CREATE SCHEMA [reporting];
-GO
-
-CREATE TABLE [reporting].[sales_data] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [product_name] NVARCHAR(100) NOT NULL,
- [quantity] INT NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [sale_date] DATE
-);
-
-CREATE TABLE [reporting].[customers] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [region] NVARCHAR(50)
-);
-GO
-
-CREATE VIEW [reporting].[product_summary] AS
-SELECT
- [product_name],
- COUNT(*) as [sale_count],
- SUM([quantity]) as [total_quantity],
- AVG([price]) as [avg_price]
-FROM [reporting].[sales_data]
-GROUP BY [product_name];
-GO
-
-CREATE VIEW [reporting].[top_products] AS
-SELECT TOP 10
- [product_name],
- [total_quantity]
-FROM [reporting].[product_summary]
-WHERE [total_quantity] > 100
-ORDER BY [total_quantity] DESC;
-GO
-`,
- migrationDDL: `
--- Drop dependent view first
-DROP VIEW [reporting].[top_products];
-GO
-
--- Drop base view
-DROP VIEW [reporting].[product_summary];
-GO
-`,
- description: "Reverse of view operations - dropping views in correct dependency order",
- },
- {
- name: "reverse_function_and_procedure_operations",
- initialSchema: `
-CREATE SCHEMA [calc];
-GO
-
-CREATE TABLE [calc].[numbers] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [value] INT NOT NULL,
- [category] NVARCHAR(20)
-);
-GO
-
-CREATE FUNCTION [calc].[square](@input INT)
-RETURNS INT
-AS
-BEGIN
- RETURN @input * @input;
-END;
-GO
-
-CREATE FUNCTION [calc].[get_numbers_by_category](@category NVARCHAR(20))
-RETURNS TABLE
-AS
-RETURN (
- SELECT [id], [value], [category]
- FROM [calc].[numbers]
- WHERE [category] = @category
-);
-GO
-
-CREATE PROCEDURE [calc].[add_number]
- @value INT,
- @category NVARCHAR(20)
-AS
-BEGIN
- INSERT INTO [calc].[numbers] ([value], [category])
- VALUES (@value, @category);
-END;
-GO
-`,
- migrationDDL: `
--- Drop stored procedure
-DROP PROCEDURE [calc].[add_number];
-GO
-
--- Drop table-valued function
-DROP FUNCTION [calc].[get_numbers_by_category];
-GO
-
--- Drop scalar function
-DROP FUNCTION [calc].[square];
-GO
-`,
- description: "Reverse of function and procedure operations - dropping procedures and functions",
- },
- {
- name: "reverse_index_operations",
- initialSchema: `
-CREATE SCHEMA [perf];
-GO
-
-CREATE TABLE [perf].[events] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [event_name] NVARCHAR(100) NOT NULL,
- [event_data] NVARCHAR(MAX),
- [timestamp] DATETIME2,
- [user_id] INT,
- [category] NVARCHAR(50),
- [event_month] AS DATEPART(MONTH, [timestamp])
-);
-
-CREATE INDEX [idx_events_timestamp] ON [perf].[events] ([timestamp]);
-CREATE INDEX [idx_events_user_category] ON [perf].[events] ([user_id], [category]);
-CREATE UNIQUE INDEX [idx_events_name_timestamp] ON [perf].[events] ([event_name], [timestamp]);
-
-CREATE INDEX [idx_events_recent] ON [perf].[events] ([event_name])
-WHERE [timestamp] >= '2023-01-01';
-
-CREATE INDEX [idx_events_month] ON [perf].[events] ([event_month]);
-`,
- migrationDDL: `
--- Drop index on computed column
-DROP INDEX [idx_events_month] ON [perf].[events];
-
--- Drop computed column
-ALTER TABLE [perf].[events] DROP COLUMN [event_month];
-
--- Drop filtered index
-DROP INDEX [idx_events_recent] ON [perf].[events];
-
--- Drop unique index
-DROP INDEX [idx_events_name_timestamp] ON [perf].[events];
-
--- Drop composite index
-DROP INDEX [idx_events_user_category] ON [perf].[events];
-
--- Drop simple index
-DROP INDEX [idx_events_timestamp] ON [perf].[events];
-`,
- description: "Reverse of index operations - dropping various index types including filtered and computed column indexes",
- },
- {
- name: "reverse_complex_constraints",
- initialSchema: `
-CREATE SCHEMA [finance];
-GO
-
-CREATE TABLE [finance].[accounts] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [account_number] NVARCHAR(20) NOT NULL,
- [balance] DECIMAL(15,2),
- [account_type] NVARCHAR(20) NOT NULL,
- [created_at] DATETIME2 DEFAULT GETDATE(),
- CONSTRAINT [ck_accounts_balance] CHECK ([balance] >= 0),
- CONSTRAINT [ck_accounts_type] CHECK ([account_type] IN ('CHECKING', 'SAVINGS', 'CREDIT')),
- CONSTRAINT [uk_accounts_number] UNIQUE ([account_number])
-);
-
-CREATE TABLE [finance].[transactions] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [account_id] INT NOT NULL,
- [amount] DECIMAL(15,2) NOT NULL,
- [transaction_type] NVARCHAR(20) NOT NULL,
- [transaction_date] DATETIME2,
- CONSTRAINT [fk_transactions_account] FOREIGN KEY ([account_id]) REFERENCES [finance].[accounts]([id]),
- CONSTRAINT [ck_transactions_amount] CHECK ([amount] != 0),
- CONSTRAINT [ck_transactions_type] CHECK ([transaction_type] IN ('DEBIT', 'CREDIT'))
-);
-`,
- migrationDDL: `
--- Drop table with foreign key and check constraints
-DROP TABLE [finance].[transactions];
-
--- Drop unique constraint
-ALTER TABLE [finance].[accounts] DROP CONSTRAINT [uk_accounts_number];
-
--- Drop check constraints
-ALTER TABLE [finance].[accounts] DROP CONSTRAINT [ck_accounts_type];
-ALTER TABLE [finance].[accounts] DROP CONSTRAINT [ck_accounts_balance];
-`,
- description: "Reverse of complex constraints - dropping tables with foreign keys and removing constraints",
- },
- {
- name: "reverse_column_modifications",
- initialSchema: `
-CREATE SCHEMA [hr];
-GO
-
-CREATE TABLE [hr].[departments] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [budget] DECIMAL(15,2)
-);
-
-CREATE TABLE [hr].[employees] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [first_name] NVARCHAR(50),
- [last_name] NVARCHAR(50),
- [email] NVARCHAR(100),
- [phone] VARCHAR(20),
- [hire_date] DATE,
- [salary] DECIMAL(10,2),
- [department_id] INT,
- [manager_id] INT,
- [status] NVARCHAR(20),
- CONSTRAINT [fk_employees_department] FOREIGN KEY ([department_id]) REFERENCES [hr].[departments]([id]),
- CONSTRAINT [fk_employees_manager] FOREIGN KEY ([manager_id]) REFERENCES [hr].[employees]([id]),
- CONSTRAINT [ck_employees_salary] CHECK ([salary] > 0),
- CONSTRAINT [ck_employees_status] CHECK ([status] IN ('ACTIVE', 'INACTIVE', 'TERMINATED'))
-);
-`,
- migrationDDL: `
--- Drop check constraints first
-ALTER TABLE [hr].[employees] DROP CONSTRAINT [ck_employees_status];
-ALTER TABLE [hr].[employees] DROP CONSTRAINT [ck_employees_salary];
-
--- Drop foreign key constraints before dropping columns
-ALTER TABLE [hr].[employees] DROP CONSTRAINT [fk_employees_manager];
-ALTER TABLE [hr].[employees] DROP CONSTRAINT [fk_employees_department];
-
--- Now safe to drop columns
-ALTER TABLE [hr].[employees] DROP COLUMN [status];
-ALTER TABLE [hr].[employees] DROP COLUMN [manager_id];
-ALTER TABLE [hr].[employees] DROP COLUMN [department_id];
-
--- Finally drop department table
-DROP TABLE [hr].[departments];
-`,
- description: "Reverse of column modifications - dropping columns, constraints, and related tables",
- },
- {
- name: "reverse_multiple_schema_dependencies",
- initialSchema: `
-CREATE SCHEMA [core];
-GO
-CREATE SCHEMA [app];
-GO
-
-CREATE TABLE [core].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL
-);
-
-CREATE TABLE [core].[roles] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(50) NOT NULL
-);
-
-CREATE TABLE [core].[user_roles] (
- [user_id] INT NOT NULL,
- [role_id] INT NOT NULL,
- [assigned_at] DATETIME2,
- CONSTRAINT [pk_user_roles] PRIMARY KEY ([user_id], [role_id]),
- CONSTRAINT [fk_user_roles_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE,
- CONSTRAINT [fk_user_roles_role] FOREIGN KEY ([role_id]) REFERENCES [core].[roles]([id]) ON DELETE CASCADE
-);
-
-CREATE TABLE [app].[sessions] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [token] NVARCHAR(255) NOT NULL,
- [expires_at] DATETIME2 NOT NULL,
- CONSTRAINT [fk_sessions_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id]) ON DELETE CASCADE
-);
-
-CREATE TABLE [app].[audit_log] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT,
- [action] NVARCHAR(100) NOT NULL,
- [timestamp] DATETIME2,
- [details] NVARCHAR(MAX),
- CONSTRAINT [fk_audit_user] FOREIGN KEY ([user_id]) REFERENCES [core].[users]([id])
-);
-`,
- migrationDDL: `
--- Drop tables that reference core schema
-DROP TABLE [app].[audit_log];
-DROP TABLE [app].[sessions];
-
--- Drop junction table
-DROP TABLE [core].[user_roles];
-`,
- description: "Reverse of multiple schema dependencies - dropping cross-schema references",
- },
- {
- name: "reverse_computed_columns_and_triggers",
- initialSchema: `
-CREATE SCHEMA [ecommerce];
-GO
-
-CREATE TABLE [ecommerce].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [tax_rate] DECIMAL(5,4),
- [price_with_tax] AS ([price] * (1 + [tax_rate])),
- [price_category] AS (
- CASE
- WHEN [price] < 10 THEN 'Budget'
- WHEN [price] < 100 THEN 'Standard'
- ELSE 'Premium'
- END
- )
-);
-GO
-
-CREATE INDEX [idx_products_price_category] ON [ecommerce].[products] ([price_category]);
-CREATE INDEX [idx_products_price_with_tax] ON [ecommerce].[products] ([price_with_tax]);
-
-CREATE TABLE [ecommerce].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [product_id] INT NOT NULL,
- [quantity] INT NOT NULL,
- [unit_price] DECIMAL(10,2) NOT NULL,
- [total] DECIMAL(15,2),
- [order_date] DATETIME2,
- [computed_total] AS ([quantity] * [unit_price]),
- CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [ecommerce].[products]([id])
-);
-GO
-`,
- migrationDDL: `
--- Drop table with computed column
-DROP TABLE [ecommerce].[orders];
-GO
-
--- Drop indexes on computed columns
-DROP INDEX [idx_products_price_with_tax] ON [ecommerce].[products];
-DROP INDEX [idx_products_price_category] ON [ecommerce].[products];
-
--- Drop computed columns
-ALTER TABLE [ecommerce].[products] DROP COLUMN [price_category];
-GO
-
-ALTER TABLE [ecommerce].[products] DROP COLUMN [price_with_tax];
-GO
-`,
- description: "Reverse of computed columns - dropping indexes and computed columns",
- },
- {
- name: "reverse_temporal_tables",
- initialSchema: `
-CREATE SCHEMA [tracking];
-GO
-
-CREATE TABLE [tracking].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [last_modified] DATETIME2,
- [valid_from] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
- [valid_to] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
- PERIOD FOR SYSTEM_TIME ([valid_from], [valid_to])
-);
-
-CREATE TABLE [tracking].[products_history] (
- [id] INT NOT NULL,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [last_modified] DATETIME2,
- [valid_from] DATETIME2 NOT NULL,
- [valid_to] DATETIME2 NOT NULL
-);
-
-CREATE CLUSTERED INDEX [idx_products_history_period] ON [tracking].[products_history] ([valid_to], [valid_from]);
-
-ALTER TABLE [tracking].[products] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [tracking].[products_history]));
-`,
- migrationDDL: `
--- Disable system versioning first
-ALTER TABLE [tracking].[products] SET (SYSTEM_VERSIONING = OFF);
-
--- Drop history table (and its index will be dropped automatically)
-DROP TABLE [tracking].[products_history];
-
--- Drop period definition and system time columns
-ALTER TABLE [tracking].[products] DROP PERIOD FOR SYSTEM_TIME;
-ALTER TABLE [tracking].[products] DROP COLUMN [valid_to];
-ALTER TABLE [tracking].[products] DROP COLUMN [valid_from];
-`,
- description: "Reverse of temporal tables - disabling system versioning and dropping history",
- },
- {
- name: "reverse_table_and_column_comments",
- initialSchema: `
-CREATE SCHEMA [app];
-GO
-
-CREATE TABLE [app].[users] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [username] NVARCHAR(50) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [created_at] DATETIME2,
- [status] NVARCHAR(20)
-);
-
-CREATE TABLE [app].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL,
- [category] NVARCHAR(50)
-);
-
-CREATE INDEX [idx_users_email] ON [app].[users] ([email]);
-CREATE INDEX [idx_products_category] ON [app].[products] ([category]);
-
-CREATE TABLE [app].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [user_id] INT NOT NULL,
- [product_id] INT NOT NULL,
- [quantity] INT NOT NULL,
- [order_date] DATETIME2,
- CONSTRAINT [fk_orders_user] FOREIGN KEY ([user_id]) REFERENCES [app].[users]([id]),
- CONSTRAINT [fk_orders_product] FOREIGN KEY ([product_id]) REFERENCES [app].[products]([id])
-);
-GO
-
--- Add table comments using extended properties
-EXEC sp_addextendedproperty 'MS_Description', 'User accounts and profile information', 'SCHEMA', 'app', 'TABLE', 'users';
-EXEC sp_addextendedproperty 'MS_Description', 'Product catalog with pricing information', 'SCHEMA', 'app', 'TABLE', 'products';
-EXEC sp_addextendedproperty 'MS_Description', 'Customer orders tracking system', 'SCHEMA', 'app', 'TABLE', 'orders';
-GO
-
--- Add column comments for users table
-EXEC sp_addextendedproperty 'MS_Description', 'Unique identifier for each user', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Unique username for login authentication', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'username';
-EXEC sp_addextendedproperty 'MS_Description', 'User email address for notifications', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'email';
-EXEC sp_addextendedproperty 'MS_Description', 'Timestamp when the user account was created', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'created_at';
-EXEC sp_addextendedproperty 'MS_Description', 'Current status: active, inactive, or suspended', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'status';
-GO
-
--- Add column comments for products table
-EXEC sp_addextendedproperty 'MS_Description', 'Unique product identifier', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Product display name', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'name';
-EXEC sp_addextendedproperty 'MS_Description', 'Product price in USD', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'price';
-EXEC sp_addextendedproperty 'MS_Description', 'Product category classification', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'category';
-GO
-
--- Add comments to orders table
-EXEC sp_addextendedproperty 'MS_Description', 'Unique order identifier', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Reference to the user who placed the order', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'user_id';
-EXEC sp_addextendedproperty 'MS_Description', 'Reference to the ordered product', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'product_id';
-EXEC sp_addextendedproperty 'MS_Description', 'Number of items ordered', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'quantity';
-EXEC sp_addextendedproperty 'MS_Description', 'When the order was placed', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'order_date';
-`,
- migrationDDL: `
--- Drop column comments from orders table
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'order_date';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'quantity';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'product_id';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'user_id';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders', 'COLUMN', 'id';
-GO
-
--- Drop table comment from orders
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'orders';
-GO
-
--- Drop the orders table
-DROP TABLE [app].[orders];
-GO
-
--- Drop column comments from products table
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'category';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'price';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'name';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products', 'COLUMN', 'id';
-GO
-
--- Drop column comments from users table
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'status';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'created_at';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'email';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'username';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users', 'COLUMN', 'id';
-GO
-
--- Drop table comments
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'products';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'app', 'TABLE', 'users';
-`,
- description: "Reverse of table and column comments - dropping extended properties",
- },
- {
- name: "reverse_default_constraint_operations",
- initialSchema: `
-CREATE SCHEMA [defaults];
-GO
-
-CREATE TABLE [defaults].[employees] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [email] NVARCHAR(100) NOT NULL,
- [hire_date] DATE NOT NULL DEFAULT GETDATE(),
- [status] NVARCHAR(20) NOT NULL CONSTRAINT [DF_employees_status_new] DEFAULT 'pending',
- [is_active] BIT NOT NULL DEFAULT 1,
- [salary] DECIMAL(10,2) DEFAULT 50000.00,
- [department_id] INT DEFAULT 1,
- [created_at] DATETIME2 DEFAULT SYSDATETIME(),
- [updated_at] DATETIME2,
- [vacation_days] INT NOT NULL DEFAULT 15,
- [bonus_percentage] DECIMAL(5,2) DEFAULT (CASE WHEN MONTH(GETDATE()) = 12 THEN 10.0 ELSE 5.0 END)
-);
-
-CREATE TABLE [defaults].[products] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [price] DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- [stock] INT NOT NULL DEFAULT 0,
- [is_available] BIT DEFAULT 1,
- [cost] DECIMAL(10,2) NOT NULL
-);
-
-CREATE TABLE [defaults].[orders] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [order_number] NVARCHAR(50) NOT NULL DEFAULT CONCAT('ORD-', YEAR(GETDATE()), '-', FORMAT(GETDATE(), 'MMdd')),
- [order_date] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
- [status] NVARCHAR(20) NOT NULL DEFAULT 'pending',
- [total] DECIMAL(10,2) NOT NULL DEFAULT 0,
- [discount] DECIMAL(5,2) DEFAULT NULL,
- [is_paid] BIT NOT NULL DEFAULT 0,
- [notes] NVARCHAR(MAX) DEFAULT 'No notes'
-);
-`,
- migrationDDL: `
--- Drop orders table with all its defaults
-DROP TABLE [defaults].[orders];
-GO
-
--- Drop default constraint for bonus_percentage column
-DECLARE @constraint_name NVARCHAR(128);
-SELECT @constraint_name = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'defaults' AND t.name = 'employees' AND c.name = 'bonus_percentage';
-IF @constraint_name IS NOT NULL
- EXEC('ALTER TABLE [defaults].[employees] DROP CONSTRAINT [' + @constraint_name + ']');
-GO
-
--- Drop default constraint for vacation_days column
-DECLARE @constraint_name2 NVARCHAR(128);
-SELECT @constraint_name2 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'defaults' AND t.name = 'employees' AND c.name = 'vacation_days';
-IF @constraint_name2 IS NOT NULL
- EXEC('ALTER TABLE [defaults].[employees] DROP CONSTRAINT [' + @constraint_name2 + ']');
-GO
-
--- Now safe to drop columns
-ALTER TABLE [defaults].[employees] DROP COLUMN [bonus_percentage];
-ALTER TABLE [defaults].[employees] DROP COLUMN [vacation_days];
-
--- Remove default from status column (named constraint)
-ALTER TABLE [defaults].[employees] DROP CONSTRAINT [DF_employees_status_new];
-
--- Drop cost column (which has no default)
-ALTER TABLE [defaults].[products] DROP COLUMN [cost];
-`,
- description: "Reverse of default constraint operations - dropping tables, columns with defaults, and default constraints",
- },
- {
- name: "reverse_complex_default_expressions",
- initialSchema: `
-CREATE SCHEMA [complex];
-GO
-
-CREATE TABLE [complex].[audit_log] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [event_type] NVARCHAR(50) NOT NULL,
- [user_name] NVARCHAR(100) NOT NULL DEFAULT SYSTEM_USER,
- [event_time] DATETIME2 NOT NULL DEFAULT SYSDATETIME(),
- [ip_address] NVARCHAR(45),
- [session_id] UNIQUEIDENTIFIER DEFAULT NEWID(),
- [server_name] NVARCHAR(100) DEFAULT @@SERVERNAME,
- [database_name] NVARCHAR(100) DEFAULT DB_NAME(),
- [schema_name] NVARCHAR(100) DEFAULT SCHEMA_NAME()
-);
-
-CREATE TABLE [complex].[documents] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [title] NVARCHAR(200) NOT NULL,
- [content] NVARCHAR(MAX),
- [version] INT NOT NULL DEFAULT 1,
- [created_date] DATE DEFAULT CAST(GETDATE() AS DATE),
- [expiry_date] DATE DEFAULT DATEADD(YEAR, 1, CAST(GETDATE() AS DATE)),
- [document_code] NVARCHAR(50) DEFAULT CONCAT('DOC-', FORMAT(GETDATE(), 'yyyyMMdd'), '-', NEWID()),
- [priority] INT DEFAULT
- CASE
- WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 1
- WHEN DATEPART(HOUR, GETDATE()) < 18 THEN 2
- ELSE 3
- END
-);
-
-CREATE TABLE [complex].[calculations] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [value1] DECIMAL(10,2) NOT NULL,
- [value2] DECIMAL(10,2) NOT NULL,
- [pi_value] DECIMAL(10,8) DEFAULT PI(),
- [random_value] FLOAT DEFAULT RAND(),
- [checksum_value] INT DEFAULT CHECKSUM(NEWID()),
- [created_at] DATETIME2 DEFAULT SYSDATETIME(),
- [created_offset] DATETIMEOFFSET DEFAULT SYSDATETIMEOFFSET()
-);
-`,
- migrationDDL: `
--- Drop table with function-based defaults
-DROP TABLE [complex].[calculations];
-GO
-
--- Drop default constraint for priority column
-DECLARE @constraint_name NVARCHAR(128);
-SELECT @constraint_name = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'complex' AND t.name = 'documents' AND c.name = 'priority';
-IF @constraint_name IS NOT NULL
- EXEC('ALTER TABLE [complex].[documents] DROP CONSTRAINT [' + @constraint_name + ']');
-GO
-
--- Drop default constraint for document_code column
-DECLARE @constraint_name2 NVARCHAR(128);
-SELECT @constraint_name2 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'complex' AND t.name = 'documents' AND c.name = 'document_code';
-IF @constraint_name2 IS NOT NULL
- EXEC('ALTER TABLE [complex].[documents] DROP CONSTRAINT [' + @constraint_name2 + ']');
-GO
-
--- Drop default constraint for schema_name column
-DECLARE @constraint_name3 NVARCHAR(128);
-SELECT @constraint_name3 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'complex' AND t.name = 'audit_log' AND c.name = 'schema_name';
-IF @constraint_name3 IS NOT NULL
- EXEC('ALTER TABLE [complex].[audit_log] DROP CONSTRAINT [' + @constraint_name3 + ']');
-GO
-
--- Drop default constraint for database_name column
-DECLARE @constraint_name4 NVARCHAR(128);
-SELECT @constraint_name4 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'complex' AND t.name = 'audit_log' AND c.name = 'database_name';
-IF @constraint_name4 IS NOT NULL
- EXEC('ALTER TABLE [complex].[audit_log] DROP CONSTRAINT [' + @constraint_name4 + ']');
-GO
-
--- Drop default constraint for server_name column
-DECLARE @constraint_name5 NVARCHAR(128);
-SELECT @constraint_name5 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'complex' AND t.name = 'audit_log' AND c.name = 'server_name';
-IF @constraint_name5 IS NOT NULL
- EXEC('ALTER TABLE [complex].[audit_log] DROP CONSTRAINT [' + @constraint_name5 + ']');
-GO
-
--- Now safe to drop columns
-ALTER TABLE [complex].[documents] DROP COLUMN [priority];
-ALTER TABLE [complex].[documents] DROP COLUMN [document_code];
-ALTER TABLE [complex].[audit_log] DROP COLUMN [schema_name];
-ALTER TABLE [complex].[audit_log] DROP COLUMN [database_name];
-ALTER TABLE [complex].[audit_log] DROP COLUMN [server_name];
-`,
- description: "Reverse of complex default expressions - dropping columns with system functions and conditional defaults",
- },
- {
- name: "reverse_comments_with_special_characters",
- initialSchema: `
-CREATE SCHEMA [special];
-GO
-
-CREATE TABLE [special].[documents] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [title] NVARCHAR(200) NOT NULL,
- [content] NVARCHAR(MAX),
- [author] NVARCHAR(100),
- [created_date] DATETIME2,
- [metadata] NVARCHAR(500)
-);
-
-CREATE TABLE [special].[translations] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [document_id] INT NOT NULL,
- [lang_code] NVARCHAR(10) NOT NULL,
- [translated_title] NVARCHAR(200),
- [translated_content] NVARCHAR(MAX),
- CONSTRAINT [fk_translations_doc] FOREIGN KEY ([document_id]) REFERENCES [special].[documents]([id])
-);
-
-CREATE TABLE [special].[test_extreme] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [data] NVARCHAR(MAX)
-);
-GO
-
--- Add table comments with special characters and quotes
-EXEC sp_addextendedproperty 'MS_Description', 'Document storage system - handles files, "metadata", & special chars: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'documents';
-GO
-
--- Add multiline comment with special formatting
-EXEC sp_addextendedproperty 'MS_Description',
-'Translation table for international support:
-- Supports Unicode characters: αβγδε, 中文, العربية, русский
-- Handles quotes: "double quotes" and ''single quotes''
-- Special symbols: @#$%^&*()_+-={}[]|\:";''<>?,./', 'SCHEMA', 'special', 'TABLE', 'translations';
-GO
-
--- Column comments with various special cases
-EXEC sp_addextendedproperty 'MS_Description', 'Primary key (auto-increment) - unique ID for each document', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'id';
-EXEC sp_addextendedproperty 'MS_Description', 'Document title - may contain "quotes", ''apostrophes'', and symbols: @#$%', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'title';
-GO
-
--- Multiline comment with technical details
-EXEC sp_addextendedproperty 'MS_Description',
-'Document content field:
- • Supports rich text formatting
- • HTML tags like <p>, <div>, <span>
- • Special characters: © ® ™ § ¶ † ‡ • ‰ ′ ″
- • Mathematical: ± × ÷ ≠ ≤ ≥ ∞ ∑ ∏ ∫
- • Currency: $ € £ ¥ ₹ ₽
- • Arrows: ← → ↑ ↓ ↔ ⇐ ⇒ ⇔', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'content';
-GO
-
--- Comment with SQL injection attempt (should be safely handled)
-EXEC sp_addextendedproperty 'MS_Description', 'Author name field - prevents SQL injection like ''; DROP TABLE users; --', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'author';
-GO
-
--- Comment with JSON-like structure
-EXEC sp_addextendedproperty 'MS_Description', 'Metadata JSON: {"version": "1.0", "tags": ["important", "draft"], "settings": {"public": false}}', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'metadata';
-GO
-
--- Unicode characters in comments
-EXEC sp_addextendedproperty 'MS_Description', 'Language code: en-US, fr-FR, de-DE, ja-JP, zh-CN, ar-SA, ru-RU, hi-IN, 한국어, ไทย', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'lang_code';
-GO
-
--- Comment with URLs and file paths
-EXEC sp_addextendedproperty 'MS_Description', 'Translated content - may reference URLs like https://example.com/path?param=value&other=123 or file paths C:\Users\Name\Documents\file.txt', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'translated_content';
-GO
-
--- Extremely long comment to test limits
-EXEC sp_addextendedproperty 'MS_Description',
-'This is an extremely long comment designed to test the limits of extended property storage in SQL Server. It contains multiple lines of text with various formatting, special characters, and technical information. The purpose is to verify that the migration system can properly handle, store, and retrieve complex comment data without truncation or corruption. This comment includes: numbers (123456789), symbols (!@#$%^&*()_+-={}[]|\:";''<>?,./ ), Unicode characters (αβγδε中文العربيةрусский한국어), and structured data like JSON {"key": "value", "array": [1, 2, 3], "nested": {"deep": true}}. Additionally, it tests SQL-like syntax: SELECT * FROM table WHERE column = ''value'' AND other_column IN (1, 2, 3); as well as HTML markup: <html><body><p class="test">Content</p></body></html> and XML: <?xml version="1.0"?><root><item id="1">Test</item></root>. The comment system should preserve all these characters and structures exactly as written, demonstrating robust handling of complex metadata in database schema documentation.', 'SCHEMA', 'special', 'TABLE', 'test_extreme';
-`,
- migrationDDL: `
--- Drop extremely long comment
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'test_extreme';
-GO
-
--- Drop table with extreme comment
-DROP TABLE [special].[test_extreme];
-GO
-
--- Drop column comments with special characters
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'translated_content';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'translations', 'COLUMN', 'lang_code';
-GO
-
--- Drop column comments from documents
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'metadata';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'author';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'content';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'title';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents', 'COLUMN', 'id';
-GO
-
--- Drop table comments with special characters
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'translations';
-EXEC sp_dropextendedproperty 'MS_Description', 'SCHEMA', 'special', 'TABLE', 'documents';
-`,
- description: "Reverse of comments with special characters - dropping comments with quotes, unicode, and special formatting",
- },
- {
- name: "reverse_default_constraint_edge_cases",
- initialSchema: `
-CREATE SCHEMA [edge];
-GO
-
-CREATE TABLE [edge].[test_defaults] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [nullable_with_default] INT CONSTRAINT [DF_test_defaults_nullable_new] DEFAULT 84,
- [string_default] NVARCHAR(50) DEFAULT N'Default''s value',
- [empty_string] NVARCHAR(50) DEFAULT '',
- [zero_default] INT DEFAULT 0,
- [negative_default] INT DEFAULT -1,
- [decimal_precision] DECIMAL(18,6) DEFAULT 123.456789,
- [special_chars] NVARCHAR(100) DEFAULT N'Line1' + CHAR(13) + CHAR(10) + N'Line2',
- [unicode_default] NVARCHAR(100) DEFAULT N'你好世界 🌍',
- [max_int] BIGINT DEFAULT 9223372036854775807,
- [min_int] BIGINT DEFAULT -9223372036854775808,
- [json_config] NVARCHAR(MAX) DEFAULT N'{"enabled": true, "count": 0}'
-);
-
-CREATE TABLE [edge].[flags] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [is_enabled] BIT NOT NULL DEFAULT 1,
- [is_deleted] BIT NOT NULL DEFAULT 0,
- [is_visible] BIT DEFAULT 1,
- [is_archived] BIT DEFAULT 0
-);
-`,
- migrationDDL: `
--- Drop table with bit field defaults
-DROP TABLE [edge].[flags];
-
--- Drop default constraints before dropping columns
--- Drop default constraint for json_config column
-DECLARE @constraint_name NVARCHAR(128);
-SELECT @constraint_name = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'json_config';
-IF @constraint_name IS NOT NULL
- EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name + ']');
-GO
-
--- Drop default constraint for min_int column
-DECLARE @constraint_name2 NVARCHAR(128);
-SELECT @constraint_name2 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'min_int';
-IF @constraint_name2 IS NOT NULL
- EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name2 + ']');
-GO
-
--- Drop default constraint for max_int column
-DECLARE @constraint_name3 NVARCHAR(128);
-SELECT @constraint_name3 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'max_int';
-IF @constraint_name3 IS NOT NULL
- EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name3 + ']');
-GO
-
--- Drop default constraint for unicode_default column
-DECLARE @constraint_name4 NVARCHAR(128);
-SELECT @constraint_name4 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'unicode_default';
-IF @constraint_name4 IS NOT NULL
- EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name4 + ']');
-GO
-
--- Drop default constraint for special_chars column
-DECLARE @constraint_name5 NVARCHAR(128);
-SELECT @constraint_name5 = dc.name
-FROM sys.default_constraints dc
-INNER JOIN sys.columns c ON dc.parent_column_id = c.column_id AND dc.parent_object_id = c.object_id
-INNER JOIN sys.tables t ON c.object_id = t.object_id
-INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
-WHERE s.name = 'edge' AND t.name = 'test_defaults' AND c.name = 'special_chars';
-IF @constraint_name5 IS NOT NULL
- EXEC('ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [' + @constraint_name5 + ']');
-GO
-
--- Now safe to drop columns
-ALTER TABLE [edge].[test_defaults] DROP COLUMN [json_config];
-ALTER TABLE [edge].[test_defaults] DROP COLUMN [min_int];
-ALTER TABLE [edge].[test_defaults] DROP COLUMN [max_int];
-ALTER TABLE [edge].[test_defaults] DROP COLUMN [unicode_default];
-ALTER TABLE [edge].[test_defaults] DROP COLUMN [special_chars];
-
--- Remove named default constraint
-ALTER TABLE [edge].[test_defaults] DROP CONSTRAINT [DF_test_defaults_nullable_new];
-`,
- description: "Reverse of edge case defaults - dropping special default constraints and columns",
- },
- {
- name: "spatial_index_operations",
- initialSchema: `
-CREATE SCHEMA [geo];
-GO
-
--- Enable spatial types
-CREATE TABLE [geo].[locations] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(200) NOT NULL,
- [geo_point] GEOMETRY NOT NULL,
- [geo_polygon] GEOMETRY NOT NULL,
- [geo_location] GEOGRAPHY NOT NULL,
- [description] NVARCHAR(MAX)
-);
-`,
- migrationDDL: `
--- Add spatial indexes with different configurations
-
--- GEOMETRY_GRID index with full parameters
-CREATE SPATIAL INDEX [idx_geo_point] ON [geo].[locations] ([geo_point])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-180, -90, 180, 90),
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 32,
- PAD_INDEX = ON,
- FILLFACTOR = 85,
- SORT_IN_TEMPDB = ON,
- ALLOW_ROW_LOCKS = ON,
- ALLOW_PAGE_LOCKS = ON,
- MAXDOP = 2,
- DATA_COMPRESSION = PAGE
-);
-
--- GEOMETRY_AUTO_GRID index (requires bounding box)
-CREATE SPATIAL INDEX [idx_geo_polygon] ON [geo].[locations] ([geo_polygon])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-1000, -1000, 1000, 1000),
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW)
-);
-
--- GEOGRAPHY_GRID index with specific grid levels
-CREATE SPATIAL INDEX [idx_geo_location] ON [geo].[locations] ([geo_location])
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 8,
- FILLFACTOR = 80
-);
-
--- Add another table with spatial columns
-CREATE TABLE [geo].[boundaries] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [boundary_name] NVARCHAR(100) NOT NULL,
- [boundary_shape] GEOMETRY NOT NULL,
- [center_point] GEOMETRY NOT NULL
-);
-
--- Add spatial index on new table
-CREATE SPATIAL INDEX [idx_boundary_shape] ON [geo].[boundaries] ([boundary_shape])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (0, 0, 1000, 1000),
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = LOW, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 64
-);
-`,
- description: "Spatial index operations with various tessellation schemes and parameters",
- },
- {
- name: "reverse_spatial_table_operations",
- initialSchema: `
-CREATE SCHEMA [geo];
-GO
-
--- Create table with spatial columns and indexes
-CREATE TABLE [geo].[locations] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(200) NOT NULL,
- [location] GEOGRAPHY NOT NULL,
- [area] GEOGRAPHY NOT NULL
-);
-GO
-
--- Create spatial indexes that will be dropped with the table
--- Using GEOGRAPHY_GRID which doesn't require BOUNDING_BOX
-CREATE SPATIAL INDEX [idx_location] ON [geo].[locations] ([location])
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 16
-);
-GO
-
-CREATE SPATIAL INDEX [idx_area] ON [geo].[locations] ([area])
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 8
-);
-GO
-
-CREATE TABLE [geo].[regions] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [region_name] NVARCHAR(100) NOT NULL,
- [boundary] GEOMETRY NOT NULL
-);
-GO
-`,
- migrationDDL: `
--- Drop entire table (this drops spatial indexes too)
-DROP TABLE [geo].[locations];
-
--- Add columns to remaining table
-ALTER TABLE [geo].[regions] ADD [population] INT;
-ALTER TABLE [geo].[regions] ADD [area_sqkm] FLOAT;
-`,
- description: "Reverse spatial table operations - dropping table with spatial indexes",
- },
- {
- name: "reverse_spatial_indexes",
- initialSchema: `
-CREATE SCHEMA [geo];
-GO
-
--- Create table with spatial columns and spatial indexes that can be preserved
-CREATE TABLE [geo].[locations] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(200) NOT NULL,
- [geo_point] GEOMETRY NOT NULL,
- [geo_polygon] GEOMETRY NOT NULL,
- [geo_location] GEOGRAPHY NOT NULL,
- [description] NVARCHAR(MAX)
-);
-GO
-
--- Create spatial indexes with basic configuration to avoid preservation issues
-CREATE SPATIAL INDEX [idx_geo_point] ON [geo].[locations] ([geo_point])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-180, -90, 180, 90),
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM)
-);
-GO
-
-CREATE SPATIAL INDEX [idx_geo_location] ON [geo].[locations] ([geo_location])
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM)
-);
-GO
-`,
- migrationDDL: `
--- Drop and recreate spatial indexes to test preservation
-DROP INDEX [idx_geo_point] ON [geo].[locations];
-DROP INDEX [idx_geo_location] ON [geo].[locations];
-
--- Recreate with same configuration (should work with DDL preservation)
-CREATE SPATIAL INDEX [idx_geo_point] ON [geo].[locations] ([geo_point])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-180, -90, 180, 90),
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM)
-);
-
-CREATE SPATIAL INDEX [idx_geo_location] ON [geo].[locations] ([geo_location])
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM)
-);
-`,
- description: "Reverse spatial index operations - dropping and recreating spatial indexes",
- },
- {
- name: "spatial_index_modifications",
- initialSchema: `
-CREATE SCHEMA [geo];
-GO
-
-CREATE TABLE [geo].[spatial_data] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [name] NVARCHAR(100) NOT NULL,
- [location] GEOMETRY NOT NULL,
- [region] GEOMETRY,
- [area] GEOGRAPHY
-);
-GO
-
--- Create initial spatial indexes
-CREATE SPATIAL INDEX [idx_location] ON [geo].[spatial_data] ([location])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-100, -100, 100, 100),
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 16
-);
-GO
-
-CREATE SPATIAL INDEX [idx_area] ON [geo].[spatial_data] ([area])
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 32
-);
-GO
-`,
- migrationDDL: `
--- Drop and recreate spatial index with different parameters
-DROP INDEX [idx_location] ON [geo].[spatial_data];
-GO
-
-CREATE SPATIAL INDEX [idx_location] ON [geo].[spatial_data] ([location])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-200, -200, 200, 200), -- Expanded bounding box
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW), -- Changed grid densities
- CELLS_PER_OBJECT = 64, -- Increased cells per object
- PAD_INDEX = ON,
- FILLFACTOR = 90,
- ALLOW_ROW_LOCKS = ON,
- ALLOW_PAGE_LOCKS = ON
-);
-GO
-
--- Add new spatial index on previously unindexed column
-CREATE SPATIAL INDEX [idx_region] ON [geo].[spatial_data] ([region])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-500, -500, 500, 500),
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 32
-);
-GO
-
--- Drop spatial index on geography column
-DROP INDEX [idx_area] ON [geo].[spatial_data];
-GO
-`,
- description: "Spatial index modifications - recreating with different parameters and adding/dropping indexes",
- },
- {
- name: "spatial_table_schema_changes",
- initialSchema: `
-CREATE SCHEMA [geo];
-GO
-
-CREATE TABLE [geo].[points] (
- [id] INT IDENTITY(1,1) PRIMARY KEY,
- [point_name] NVARCHAR(50) NOT NULL,
- [location] GEOMETRY NOT NULL
-);
-GO
-
-CREATE SPATIAL INDEX [idx_points_location] ON [geo].[points] ([location])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (0, 0, 100, 100),
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM)
-);
-GO
-`,
- migrationDDL: `
--- Add new spatial and non-spatial columns
-ALTER TABLE [geo].[points] ADD [altitude] FLOAT;
-ALTER TABLE [geo].[points] ADD [boundary] GEOMETRY;
-ALTER TABLE [geo].[points] ADD [geo_location] GEOGRAPHY;
-ALTER TABLE [geo].[points] ADD [created_at] DATETIME2 DEFAULT GETDATE();
-
--- Create spatial indexes on new columns
-CREATE SPATIAL INDEX [idx_points_boundary] ON [geo].[points] ([boundary])
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-50, -50, 150, 150),
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 8
-);
-GO
-
-CREATE SPATIAL INDEX [idx_points_geo_location] ON [geo].[points] ([geo_location])
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),
- CELLS_PER_OBJECT = 16
-);
-GO
-
--- Modify existing column (rename)
-EXEC sp_rename '[geo].[points].[point_name]', 'name', 'COLUMN';
-GO
-`,
- description: "Spatial table schema changes - adding columns and indexes while preserving existing spatial indexes",
- },
- }
-
- // MSSQL Express in CI gets significantly slower when too many schema-heavy
- // subtests run at once against the same container. Keep bounded parallelism.
- const maxConcurrentSubtests = 4
- parallelGate := make(chan struct{}, maxConcurrentSubtests)
-
- for _, testCase := range testCases {
- t.Run(testCase.name, func(t *testing.T) {
- t.Parallel()
- parallelGate <- struct{}{}
- defer func() {
- <-parallelGate
- }()
-
- // Create test database with unique name
- testDB := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", testDB))
- require.NoError(t, err, "failed to create test database")
-
- // Connect to test database
- driver, err := createMSSQLDriver(ctx, container.GetHost(), container.GetPort(), testDB)
- require.NoError(t, err, "failed to connect to test database")
- defer driver.Close(ctx)
-
- // Step 1: Initialize database schema and get schema result A
- err = executeSQL(ctx, driver, testCase.initialSchema)
- require.NoError(t, err, "failed to execute initial schema")
-
- schemaA, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err, "failed to sync schema A")
-
- // Step 2: Apply migration DDL and get schema result B
- err = executeSQL(ctx, driver, testCase.migrationDDL)
- require.NoError(t, err, "failed to execute migration DDL")
-
- schemaB, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err, "failed to sync schema B")
-
- // Step 3: Generate rollback DDL using generate_migration
- // Convert to model schemas for diff
- dbMetadataA := model.NewDatabaseMetadata(schemaA, nil, nil, storepb.Engine_MSSQL, false)
- dbMetadataB := model.NewDatabaseMetadata(schemaB, nil, nil, storepb.Engine_MSSQL, false)
-
- // Get diff from B to A (to generate rollback)
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MSSQL, dbMetadataB, dbMetadataA)
- require.NoError(t, err, "failed to generate diff")
-
- rollbackDDL, err := schema.GenerateMigration(storepb.Engine_MSSQL, diff)
- require.NoError(t, err, "failed to generate rollback migration")
-
- // Only proceed if there's actual rollback DDL to execute
- if strings.TrimSpace(rollbackDDL) != "" {
- // Step 4: Execute rollback DDL and get schema result C
- err = executeSQL(ctx, driver, rollbackDDL)
- require.NoError(t, err, "failed to execute rollback DDL: %s", rollbackDDL)
-
- schemaC, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err, "failed to sync schema C")
-
- // Step 5: Compare schema results A and C
- err = compareSchemas(schemaA, schemaC)
- require.NoError(t, err, "schema comparison failed for test case: %s", testCase.description)
- }
- })
- }
-}
-
-// compareSchemas compares two database schemas and returns an error if they differ
-func compareSchemas(schemaA, schemaC *storepb.DatabaseSchemaMetadata) error {
- // Sort schemas for consistent comparison
- normalizeSchema(schemaA)
- normalizeSchema(schemaC)
-
- // Use protocmp for detailed comparison, ignoring DefaultConstraintName and Position fields
- // DefaultConstraintName is only populated when syncing from database, not when parsing SQL
- // Position must be ignored because:
- // - In SQL Server, column positions are determined by column_id (physical order)
- // - When a column is dropped and re-added, it gets a new column_id at the end
- // - Our migration generator uses DROP+ADD for column renames (doesn't use sp_rename)
- // - This causes legitimate position changes that don't affect functional equivalence
- // - Example: table with [id(1), name(2), email(3)] -> DROP name -> ADD name -> [id(1), email(3), name(4)]
- opts := cmp.Options{
- protocmp.Transform(),
- protocmp.IgnoreFields(&storepb.ColumnMetadata{}, "default_constraint_name", "position"),
- }
- if diff := cmp.Diff(schemaA, schemaC, opts); diff != "" {
- return errors.Errorf("schemas differ:\n%s", diff)
- }
-
- return nil
-}
-
-// normalizeSchema sorts elements within a schema for consistent comparison
-func normalizeSchema(schema *storepb.DatabaseSchemaMetadata) {
- if schema == nil {
- return
- }
-
- // Sort schemas
- slices.SortFunc(schema.Schemas, func(i, j *storepb.SchemaMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- for _, s := range schema.Schemas {
- // Sort tables
- slices.SortFunc(s.Tables, func(i, j *storepb.TableMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort views
- slices.SortFunc(s.Views, func(i, j *storepb.ViewMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort functions
- slices.SortFunc(s.Functions, func(i, j *storepb.FunctionMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort procedures
- slices.SortFunc(s.Procedures, func(i, j *storepb.ProcedureMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort table elements
- for _, table := range s.Tables {
- // Sort columns
- slices.SortFunc(table.Columns, func(i, j *storepb.ColumnMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort indexes
- slices.SortFunc(table.Indexes, func(i, j *storepb.IndexMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort foreign keys
- slices.SortFunc(table.ForeignKeys, func(i, j *storepb.ForeignKeyMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort check constraints
- slices.SortFunc(table.CheckConstraints, func(i, j *storepb.CheckConstraintMetadata) int {
- return strings.Compare(i.Name, j.Name)
- })
-
- // Sort expressions within indexes
- for _, index := range table.Indexes {
- slices.Sort(index.Expressions)
- }
- }
- }
-}
diff --git a/backend/plugin/schema/mssql/get_database_definition_test.go b/backend/plugin/schema/mssql/get_database_definition_test.go
index 525ec07e4f..3cf46f2338 100644
--- a/backend/plugin/schema/mssql/get_database_definition_test.go
+++ b/backend/plugin/schema/mssql/get_database_definition_test.go
@@ -1,14 +1,20 @@
package mssql
import (
+ "context"
+ "fmt"
"io"
"os"
+ "strings"
"testing"
+ "github.com/google/uuid"
+ _ "github.com/microsoft/go-mssqldb"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
"github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
@@ -55,3 +61,535 @@ func TestGetDatabaseDefinition(t *testing.T) {
yamltest.Record(t, filepath, tests)
}
}
+
+//nolint:tparallel
+func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.SharedMSSQLContainer(t)
+
+ testCases := []struct {
+ name string
+ setupSQL string
+ wantMatch bool
+ }{
+ {
+ name: "basic_tables_with_constraints",
+ setupSQL: `
+CREATE SCHEMA test_schema;
+GO
+
+CREATE TABLE test_schema.users (
+ id INT IDENTITY(1,1) PRIMARY KEY,
+ email VARCHAR(255) NOT NULL UNIQUE,
+ created_at DATETIME2 DEFAULT GETDATE(),
+ is_active BIT DEFAULT 1,
+ age INT CHECK (age >= 18),
+ computed_column AS (id * 2) PERSISTED
+);
+GO
+
+CREATE TABLE test_schema.posts (
+ id INT IDENTITY(1,1) PRIMARY KEY,
+ user_id INT NOT NULL,
+ title NVARCHAR(200) NOT NULL,
+ content NVARCHAR(MAX),
+ CONSTRAINT FK_posts_users FOREIGN KEY (user_id) REFERENCES test_schema.users(id) ON DELETE CASCADE
+);
+GO
+
+CREATE NONCLUSTERED INDEX idx_posts_user_id ON test_schema.posts(user_id);
+CREATE UNIQUE INDEX idx_posts_title ON test_schema.posts(title) WHERE title IS NOT NULL;
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "views_with_dependencies",
+ setupSQL: `
+CREATE TABLE dbo.employees (
+ emp_id INT PRIMARY KEY,
+ emp_name VARCHAR(100),
+ department VARCHAR(50),
+ salary DECIMAL(10,2)
+);
+GO
+
+CREATE VIEW dbo.v_employee_summary AS
+SELECT department, COUNT(*) as emp_count, AVG(salary) as avg_salary
+FROM dbo.employees
+GROUP BY department;
+GO
+
+CREATE VIEW dbo.v_high_earners AS
+SELECT * FROM dbo.v_employee_summary
+WHERE avg_salary > 50000;
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "functions_and_procedures",
+ setupSQL: `
+CREATE FUNCTION dbo.GetFullName(@FirstName NVARCHAR(50), @LastName NVARCHAR(50))
+RETURNS NVARCHAR(100)
+AS
+BEGIN
+ RETURN @FirstName + ' ' + @LastName;
+END
+GO
+
+CREATE PROCEDURE dbo.UpdateSalary
+ @EmployeeId INT,
+ @NewSalary DECIMAL(10,2)
+AS
+BEGIN
+ UPDATE employees SET salary = @NewSalary WHERE emp_id = @EmployeeId;
+END
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "complex_indexes",
+ setupSQL: `
+CREATE TABLE dbo.sales (
+ sale_id INT,
+ product_id INT,
+ sale_date DATE,
+ amount DECIMAL(10,2),
+ region VARCHAR(50)
+);
+GO
+
+CREATE CLUSTERED COLUMNSTORE INDEX cci_sales ON dbo.sales;
+GO
+
+CREATE TABLE dbo.products (
+ product_id INT PRIMARY KEY,
+ product_name VARCHAR(100),
+ category VARCHAR(50)
+);
+GO
+
+CREATE NONCLUSTERED COLUMNSTORE INDEX ncci_products ON dbo.products(product_name, category);
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "temporal_tables",
+ setupSQL: `
+CREATE TABLE dbo.temporal_test (
+ id INT PRIMARY KEY,
+ name VARCHAR(100),
+ SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
+ SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
+ PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
+) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.temporal_test_history));
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "spatial_indexes_geography",
+ setupSQL: `
+CREATE SCHEMA spatial_schema;
+GO
+
+CREATE TABLE spatial_schema.locations (
+ id INT IDENTITY(1,1) PRIMARY KEY,
+ name NVARCHAR(200) NOT NULL,
+ location_point GEOGRAPHY NOT NULL,
+ boundary_polygon GEOGRAPHY NOT NULL,
+ route_line GEOGRAPHY,
+ description NVARCHAR(MAX),
+ created_at DATETIME2 DEFAULT GETDATE()
+);
+GO
+
+-- Create spatial indexes with various configurations
+CREATE SPATIAL INDEX idx_location_point ON spatial_schema.locations(location_point)
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 32,
+ PAD_INDEX = ON,
+ FILLFACTOR = 85,
+ SORT_IN_TEMPDB = ON,
+ ALLOW_ROW_LOCKS = ON,
+ ALLOW_PAGE_LOCKS = ON,
+ MAXDOP = 2,
+ DATA_COMPRESSION = PAGE
+);
+GO
+
+CREATE SPATIAL INDEX idx_boundary_polygon ON spatial_schema.locations(boundary_polygon)
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 16,
+ FILLFACTOR = 80
+);
+GO
+
+CREATE SPATIAL INDEX idx_route_line ON spatial_schema.locations(route_line)
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 64
+);
+GO
+
+-- Create regular indexes on the same table
+CREATE INDEX idx_name ON spatial_schema.locations(name);
+CREATE INDEX idx_created_at ON spatial_schema.locations(created_at);
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "spatial_indexes_geometry",
+ setupSQL: `
+CREATE SCHEMA geom_schema;
+GO
+
+CREATE TABLE geom_schema.shapes (
+ shape_id INT IDENTITY(1,1) PRIMARY KEY,
+ shape_name NVARCHAR(100) NOT NULL,
+ shape_point GEOMETRY NOT NULL,
+ shape_polygon GEOMETRY NOT NULL,
+ shape_line GEOMETRY,
+ area_size AS shape_polygon.STArea() PERSISTED,
+ center_x AS shape_point.STX PERSISTED,
+ center_y AS shape_point.STY PERSISTED
+);
+GO
+
+-- Create GEOMETRY spatial indexes with bounding boxes
+CREATE SPATIAL INDEX idx_shape_point ON geom_schema.shapes(shape_point)
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (0, 0, 100, 100),
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = LOW, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 16,
+ PAD_INDEX = ON,
+ FILLFACTOR = 90
+);
+GO
+
+CREATE SPATIAL INDEX idx_shape_polygon ON geom_schema.shapes(shape_polygon)
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-180, -90, 180, 90),
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 64
+);
+GO
+
+CREATE SPATIAL INDEX idx_shape_line ON geom_schema.shapes(shape_line)
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-1000, -1000, 1000, 1000),
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = MEDIUM, LEVEL_4 = HIGH),
+ CELLS_PER_OBJECT = 8
+);
+GO
+
+-- Create unique index on shape_name
+CREATE UNIQUE INDEX idx_shape_name ON geom_schema.shapes(shape_name);
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "spatial_indexes_mixed_with_constraints",
+ setupSQL: `
+CREATE TABLE dbo.spatial_mixed (
+ id INT IDENTITY(1,1) PRIMARY KEY,
+ code VARCHAR(50) UNIQUE NOT NULL,
+ geo_location GEOGRAPHY NOT NULL,
+ geom_shape GEOMETRY NOT NULL,
+ status NVARCHAR(20) CHECK (status IN ('active', 'inactive', 'pending')),
+ parent_id INT,
+ CONSTRAINT FK_spatial_mixed_parent FOREIGN KEY (parent_id) REFERENCES dbo.spatial_mixed(id)
+);
+GO
+
+-- Create both GEOGRAPHY and GEOMETRY spatial indexes
+CREATE SPATIAL INDEX idx_geo_location_mixed ON dbo.spatial_mixed(geo_location)
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 16
+);
+GO
+
+CREATE SPATIAL INDEX idx_geom_shape_mixed ON dbo.spatial_mixed(geom_shape)
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-100, -100, 100, 100),
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 32
+);
+GO
+
+-- Create regular indexes
+CREATE INDEX idx_status ON dbo.spatial_mixed(status);
+CREATE INDEX idx_parent_id ON dbo.spatial_mixed(parent_id);
+GO
+
+-- Create a view using spatial functions
+CREATE VIEW dbo.v_spatial_summary AS
+SELECT
+ id,
+ code,
+ geo_location.STAsText() AS geo_location_wkt,
+ geom_shape.STAsText() AS geom_shape_wkt,
+ geom_shape.STArea() AS shape_area,
+ status
+FROM dbo.spatial_mixed
+WHERE status = 'active';
+GO
+`,
+ wantMatch: true,
+ },
+ {
+ name: "spatial_indexes_advanced_configurations",
+ setupSQL: `
+CREATE SCHEMA advanced_spatial;
+GO
+
+-- Table with multiple spatial columns
+CREATE TABLE advanced_spatial.geo_data (
+ id BIGINT IDENTITY(1,1) PRIMARY KEY,
+ name NVARCHAR(255) NOT NULL,
+ -- Geography columns
+ current_location GEOGRAPHY NOT NULL,
+ delivery_route GEOGRAPHY,
+ service_area GEOGRAPHY NOT NULL,
+ -- Geometry columns
+ building_footprint GEOMETRY,
+ floor_plan GEOMETRY,
+ -- Regular columns
+ category VARCHAR(100),
+ is_active BIT DEFAULT 1,
+ last_updated DATETIME2 DEFAULT GETDATE()
+);
+GO
+
+-- Create spatial indexes with different tessellation schemes
+CREATE SPATIAL INDEX idx_current_location ON advanced_spatial.geo_data(current_location)
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),
+ CELLS_PER_OBJECT = 1,
+ PAD_INDEX = OFF,
+ FILLFACTOR = 100,
+ ALLOW_ROW_LOCKS = OFF,
+ ALLOW_PAGE_LOCKS = OFF
+);
+GO
+
+CREATE SPATIAL INDEX idx_delivery_route ON advanced_spatial.geo_data(delivery_route)
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),
+ CELLS_PER_OBJECT = 8192,
+ PAD_INDEX = ON,
+ FILLFACTOR = 50
+);
+GO
+
+CREATE SPATIAL INDEX idx_service_area ON advanced_spatial.geo_data(service_area)
+USING GEOGRAPHY_GRID
+WITH (
+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 256
+);
+GO
+
+CREATE SPATIAL INDEX idx_building_footprint ON advanced_spatial.geo_data(building_footprint)
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (-10000, -10000, 10000, 10000),
+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = HIGH),
+ CELLS_PER_OBJECT = 128
+);
+GO
+
+CREATE SPATIAL INDEX idx_floor_plan ON advanced_spatial.geo_data(floor_plan)
+USING GEOMETRY_GRID
+WITH (
+ BOUNDING_BOX = (0, 0, 1000, 1000),
+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = MEDIUM, LEVEL_3 = LOW, LEVEL_4 = MEDIUM),
+ CELLS_PER_OBJECT = 512
+);
+GO
+
+-- Create filtered indexes
+CREATE INDEX idx_active_category ON advanced_spatial.geo_data(category)
+WHERE is_active = 1;
+GO
+
+-- Create columnstore index
+CREATE NONCLUSTERED COLUMNSTORE INDEX ncci_geo_data
+ON advanced_spatial.geo_data(id, name, category, is_active, last_updated);
+GO
+`,
+ wantMatch: true,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel() // Safe to parallelize - shared container, unique databases per test
+ // Use test name for database name - each test case has a unique name
+ databaseName := fmt.Sprintf("test_%s_%s", strings.ReplaceAll(tc.name, " ", "_"), strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
+ // Create test database using container's master connection
+ _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", databaseName))
+ require.NoError(t, err)
+
+ // Connect to test database
+ testDriver, err := createMSSQLDriver(ctx, container.GetHost(), container.GetPort(), databaseName)
+ require.NoError(t, err)
+ defer testDriver.Close(ctx)
+
+ // Step 1: Initialize database schema and get metadata A
+ err = executeSQL(ctx, testDriver, tc.setupSQL)
+ require.NoError(t, err)
+
+ metadataA, err := testDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Step 2: Call GetDatabaseDefinition to generate database definition X
+ definitionX, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, metadataA)
+ require.NoError(t, err)
+ require.NotEmpty(t, definitionX)
+
+ // Step 3: Create a new database and run the definition X
+ newDatabaseName := fmt.Sprintf("copy_%s", databaseName)
+
+ // Create new database using container's master connection
+ _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", newDatabaseName))
+ require.NoError(t, err)
+
+ // Connect to the new database
+ newDriver, err := createMSSQLDriver(ctx, container.GetHost(), container.GetPort(), newDatabaseName)
+ require.NoError(t, err)
+ defer newDriver.Close(ctx)
+
+ // Execute the generated definition
+ err = executeSQL(ctx, newDriver, definitionX)
+ require.NoError(t, err)
+
+ // Get metadata B
+ metadataB, err := newDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Step 4: Compare metadata A and B
+ if tc.wantMatch {
+ // Note: Direct comparison might fail due to:
+ // 1. System-generated names for constraints
+ // 2. Minor differences in index/constraint definitions
+
+ require.Equal(t, len(metadataA.Schemas), len(metadataB.Schemas), "Number of schemas should match")
+
+ for i, schemaA := range metadataA.Schemas {
+ schemaB := metadataB.Schemas[i]
+ require.Equal(t, schemaA.Name, schemaB.Name, "Schema names should match")
+ require.Equal(t, len(schemaA.Tables), len(schemaB.Tables), "Number of tables in schema %s should match", schemaA.Name)
+ require.Equal(t, len(schemaA.Views), len(schemaB.Views), "Number of views in schema %s should match", schemaA.Name)
+ require.Equal(t, len(schemaA.Functions), len(schemaB.Functions), "Number of functions in schema %s should match", schemaA.Name)
+ require.Equal(t, len(schemaA.Procedures), len(schemaB.Procedures), "Number of procedures in schema %s should match", schemaA.Name)
+
+ // Compare tables in detail
+ for j, tableA := range schemaA.Tables {
+ tableB := schemaB.Tables[j]
+ require.Equal(t, tableA.Name, tableB.Name, "Table names should match")
+ require.Equal(t, len(tableA.Columns), len(tableB.Columns), "Number of columns in table %s.%s should match", schemaA.Name, tableA.Name)
+ require.Equal(t, len(tableA.Indexes), len(tableB.Indexes), "Number of indexes in table %s.%s should match", schemaA.Name, tableA.Name)
+
+ // Compare spatial indexes specifically
+ spatialIndexesA := make(map[string]*storepb.IndexMetadata)
+ spatialIndexesB := make(map[string]*storepb.IndexMetadata)
+
+ for _, idx := range tableA.Indexes {
+ if idx.Type == "SPATIAL" {
+ spatialIndexesA[idx.Name] = idx
+ }
+ }
+
+ for _, idx := range tableB.Indexes {
+ if idx.Type == "SPATIAL" {
+ spatialIndexesB[idx.Name] = idx
+ }
+ }
+
+ require.Equal(t, len(spatialIndexesA), len(spatialIndexesB), "Number of spatial indexes in table %s.%s should match", schemaA.Name, tableA.Name)
+
+ // Compare each spatial index in detail
+ for name, idxA := range spatialIndexesA {
+ idxB, exists := spatialIndexesB[name]
+ require.True(t, exists, "Spatial index %s should exist in both schemas", name)
+
+ // Compare spatial index properties
+ require.Equal(t, idxA.Type, idxB.Type, "Index type should match for %s", name)
+ require.Equal(t, idxA.Unique, idxB.Unique, "Unique property should match for %s", name)
+ require.Equal(t, idxA.Primary, idxB.Primary, "Primary property should match for %s", name)
+ require.Equal(t, idxA.Expressions, idxB.Expressions, "Expressions should match for %s", name)
+
+ // Compare spatial configuration
+ if idxA.SpatialConfig != nil {
+ require.NotNil(t, idxB.SpatialConfig, "Spatial config should exist for %s in schema B", name)
+
+ // Compare method
+ require.Equal(t, idxA.SpatialConfig.Method, idxB.SpatialConfig.Method, "Spatial method should match for %s", name)
+
+ // Compare tessellation
+ if idxA.SpatialConfig.Tessellation != nil {
+ require.NotNil(t, idxB.SpatialConfig.Tessellation, "Tessellation config should exist for %s in schema B", name)
+ require.Equal(t, idxA.SpatialConfig.Tessellation.Scheme, idxB.SpatialConfig.Tessellation.Scheme, "Tessellation scheme should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Tessellation.CellsPerObject, idxB.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should match for %s", name)
+
+ // Compare grid levels
+ require.Equal(t, len(idxA.SpatialConfig.Tessellation.GridLevels), len(idxB.SpatialConfig.Tessellation.GridLevels), "Number of grid levels should match for %s", name)
+ for k, levelA := range idxA.SpatialConfig.Tessellation.GridLevels {
+ levelB := idxB.SpatialConfig.Tessellation.GridLevels[k]
+ require.Equal(t, levelA.Level, levelB.Level, "Grid level should match for %s", name)
+ require.Equal(t, levelA.Density, levelB.Density, "Grid density should match for %s at level %d", name, levelA.Level)
+ }
+
+ // Compare bounding box for GEOMETRY indexes
+ if idxA.SpatialConfig.Tessellation.BoundingBox != nil {
+ require.NotNil(t, idxB.SpatialConfig.Tessellation.BoundingBox, "Bounding box should exist for %s in schema B", name)
+ require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Xmin, idxB.SpatialConfig.Tessellation.BoundingBox.Xmin, "Xmin should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Ymin, idxB.SpatialConfig.Tessellation.BoundingBox.Ymin, "Ymin should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Xmax, idxB.SpatialConfig.Tessellation.BoundingBox.Xmax, "Xmax should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Ymax, idxB.SpatialConfig.Tessellation.BoundingBox.Ymax, "Ymax should match for %s", name)
+ }
+ }
+
+ // Compare storage configuration
+ if idxA.SpatialConfig.Storage != nil {
+ require.NotNil(t, idxB.SpatialConfig.Storage, "Storage config should exist for %s in schema B", name)
+ require.Equal(t, idxA.SpatialConfig.Storage.Fillfactor, idxB.SpatialConfig.Storage.Fillfactor, "Fillfactor should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Storage.PadIndex, idxB.SpatialConfig.Storage.PadIndex, "PadIndex should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Storage.AllowRowLocks, idxB.SpatialConfig.Storage.AllowRowLocks, "AllowRowLocks should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Storage.AllowPageLocks, idxB.SpatialConfig.Storage.AllowPageLocks, "AllowPageLocks should match for %s", name)
+ }
+
+ // Compare dimensional configuration
+ if idxA.SpatialConfig.Dimensional != nil {
+ require.NotNil(t, idxB.SpatialConfig.Dimensional, "Dimensional config should exist for %s in schema B", name)
+ require.Equal(t, idxA.SpatialConfig.Dimensional.DataType, idxB.SpatialConfig.Dimensional.DataType, "Spatial data type should match for %s", name)
+ require.Equal(t, idxA.SpatialConfig.Dimensional.Dimensions, idxB.SpatialConfig.Dimensional.Dimensions, "Dimensions should match for %s", name)
+ }
+ }
+ }
+ }
+ }
+ }
+ })
+ }
+}
diff --git a/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go b/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
deleted file mode 100644
index 6dc57d8d8b..0000000000
--- a/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
+++ /dev/null
@@ -1,548 +0,0 @@
-package mssql
-
-import (
- "context"
- "fmt"
- "strings"
- "testing"
-
- "github.com/google/uuid"
- _ "github.com/microsoft/go-mssqldb"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-//nolint:tparallel
-func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
- ctx := context.Background()
- container := testcontainer.SharedMSSQLContainer(t)
-
- testCases := []struct {
- name string
- setupSQL string
- wantMatch bool
- }{
- {
- name: "basic_tables_with_constraints",
- setupSQL: `
-CREATE SCHEMA test_schema;
-GO
-
-CREATE TABLE test_schema.users (
- id INT IDENTITY(1,1) PRIMARY KEY,
- email VARCHAR(255) NOT NULL UNIQUE,
- created_at DATETIME2 DEFAULT GETDATE(),
- is_active BIT DEFAULT 1,
- age INT CHECK (age >= 18),
- computed_column AS (id * 2) PERSISTED
-);
-GO
-
-CREATE TABLE test_schema.posts (
- id INT IDENTITY(1,1) PRIMARY KEY,
- user_id INT NOT NULL,
- title NVARCHAR(200) NOT NULL,
- content NVARCHAR(MAX),
- CONSTRAINT FK_posts_users FOREIGN KEY (user_id) REFERENCES test_schema.users(id) ON DELETE CASCADE
-);
-GO
-
-CREATE NONCLUSTERED INDEX idx_posts_user_id ON test_schema.posts(user_id);
-CREATE UNIQUE INDEX idx_posts_title ON test_schema.posts(title) WHERE title IS NOT NULL;
-GO
-`,
- wantMatch: true,
- },
- {
- name: "views_with_dependencies",
- setupSQL: `
-CREATE TABLE dbo.employees (
- emp_id INT PRIMARY KEY,
- emp_name VARCHAR(100),
- department VARCHAR(50),
- salary DECIMAL(10,2)
-);
-GO
-
-CREATE VIEW dbo.v_employee_summary AS
-SELECT department, COUNT(*) as emp_count, AVG(salary) as avg_salary
-FROM dbo.employees
-GROUP BY department;
-GO
-
-CREATE VIEW dbo.v_high_earners AS
-SELECT * FROM dbo.v_employee_summary
-WHERE avg_salary > 50000;
-GO
-`,
- wantMatch: true,
- },
- {
- name: "functions_and_procedures",
- setupSQL: `
-CREATE FUNCTION dbo.GetFullName(@FirstName NVARCHAR(50), @LastName NVARCHAR(50))
-RETURNS NVARCHAR(100)
-AS
-BEGIN
- RETURN @FirstName + ' ' + @LastName;
-END
-GO
-
-CREATE PROCEDURE dbo.UpdateSalary
- @EmployeeId INT,
- @NewSalary DECIMAL(10,2)
-AS
-BEGIN
- UPDATE employees SET salary = @NewSalary WHERE emp_id = @EmployeeId;
-END
-GO
-`,
- wantMatch: true,
- },
- {
- name: "complex_indexes",
- setupSQL: `
-CREATE TABLE dbo.sales (
- sale_id INT,
- product_id INT,
- sale_date DATE,
- amount DECIMAL(10,2),
- region VARCHAR(50)
-);
-GO
-
-CREATE CLUSTERED COLUMNSTORE INDEX cci_sales ON dbo.sales;
-GO
-
-CREATE TABLE dbo.products (
- product_id INT PRIMARY KEY,
- product_name VARCHAR(100),
- category VARCHAR(50)
-);
-GO
-
-CREATE NONCLUSTERED COLUMNSTORE INDEX ncci_products ON dbo.products(product_name, category);
-GO
-`,
- wantMatch: true,
- },
- {
- name: "temporal_tables",
- setupSQL: `
-CREATE TABLE dbo.temporal_test (
- id INT PRIMARY KEY,
- name VARCHAR(100),
- SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
- SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
- PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
-) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.temporal_test_history));
-GO
-`,
- wantMatch: true,
- },
- {
- name: "spatial_indexes_geography",
- setupSQL: `
-CREATE SCHEMA spatial_schema;
-GO
-
-CREATE TABLE spatial_schema.locations (
- id INT IDENTITY(1,1) PRIMARY KEY,
- name NVARCHAR(200) NOT NULL,
- location_point GEOGRAPHY NOT NULL,
- boundary_polygon GEOGRAPHY NOT NULL,
- route_line GEOGRAPHY,
- description NVARCHAR(MAX),
- created_at DATETIME2 DEFAULT GETDATE()
-);
-GO
-
--- Create spatial indexes with various configurations
-CREATE SPATIAL INDEX idx_location_point ON spatial_schema.locations(location_point)
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 32,
- PAD_INDEX = ON,
- FILLFACTOR = 85,
- SORT_IN_TEMPDB = ON,
- ALLOW_ROW_LOCKS = ON,
- ALLOW_PAGE_LOCKS = ON,
- MAXDOP = 2,
- DATA_COMPRESSION = PAGE
-);
-GO
-
-CREATE SPATIAL INDEX idx_boundary_polygon ON spatial_schema.locations(boundary_polygon)
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 16,
- FILLFACTOR = 80
-);
-GO
-
-CREATE SPATIAL INDEX idx_route_line ON spatial_schema.locations(route_line)
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 64
-);
-GO
-
--- Create regular indexes on the same table
-CREATE INDEX idx_name ON spatial_schema.locations(name);
-CREATE INDEX idx_created_at ON spatial_schema.locations(created_at);
-GO
-`,
- wantMatch: true,
- },
- {
- name: "spatial_indexes_geometry",
- setupSQL: `
-CREATE SCHEMA geom_schema;
-GO
-
-CREATE TABLE geom_schema.shapes (
- shape_id INT IDENTITY(1,1) PRIMARY KEY,
- shape_name NVARCHAR(100) NOT NULL,
- shape_point GEOMETRY NOT NULL,
- shape_polygon GEOMETRY NOT NULL,
- shape_line GEOMETRY,
- area_size AS shape_polygon.STArea() PERSISTED,
- center_x AS shape_point.STX PERSISTED,
- center_y AS shape_point.STY PERSISTED
-);
-GO
-
--- Create GEOMETRY spatial indexes with bounding boxes
-CREATE SPATIAL INDEX idx_shape_point ON geom_schema.shapes(shape_point)
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (0, 0, 100, 100),
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = LOW, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 16,
- PAD_INDEX = ON,
- FILLFACTOR = 90
-);
-GO
-
-CREATE SPATIAL INDEX idx_shape_polygon ON geom_schema.shapes(shape_polygon)
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-180, -90, 180, 90),
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 64
-);
-GO
-
-CREATE SPATIAL INDEX idx_shape_line ON geom_schema.shapes(shape_line)
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-1000, -1000, 1000, 1000),
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = MEDIUM, LEVEL_4 = HIGH),
- CELLS_PER_OBJECT = 8
-);
-GO
-
--- Create unique index on shape_name
-CREATE UNIQUE INDEX idx_shape_name ON geom_schema.shapes(shape_name);
-GO
-`,
- wantMatch: true,
- },
- {
- name: "spatial_indexes_mixed_with_constraints",
- setupSQL: `
-CREATE TABLE dbo.spatial_mixed (
- id INT IDENTITY(1,1) PRIMARY KEY,
- code VARCHAR(50) UNIQUE NOT NULL,
- geo_location GEOGRAPHY NOT NULL,
- geom_shape GEOMETRY NOT NULL,
- status NVARCHAR(20) CHECK (status IN ('active', 'inactive', 'pending')),
- parent_id INT,
- CONSTRAINT FK_spatial_mixed_parent FOREIGN KEY (parent_id) REFERENCES dbo.spatial_mixed(id)
-);
-GO
-
--- Create both GEOGRAPHY and GEOMETRY spatial indexes
-CREATE SPATIAL INDEX idx_geo_location_mixed ON dbo.spatial_mixed(geo_location)
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 16
-);
-GO
-
-CREATE SPATIAL INDEX idx_geom_shape_mixed ON dbo.spatial_mixed(geom_shape)
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-100, -100, 100, 100),
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 32
-);
-GO
-
--- Create regular indexes
-CREATE INDEX idx_status ON dbo.spatial_mixed(status);
-CREATE INDEX idx_parent_id ON dbo.spatial_mixed(parent_id);
-GO
-
--- Create a view using spatial functions
-CREATE VIEW dbo.v_spatial_summary AS
-SELECT
- id,
- code,
- geo_location.STAsText() AS geo_location_wkt,
- geom_shape.STAsText() AS geom_shape_wkt,
- geom_shape.STArea() AS shape_area,
- status
-FROM dbo.spatial_mixed
-WHERE status = 'active';
-GO
-`,
- wantMatch: true,
- },
- {
- name: "spatial_indexes_advanced_configurations",
- setupSQL: `
-CREATE SCHEMA advanced_spatial;
-GO
-
--- Table with multiple spatial columns
-CREATE TABLE advanced_spatial.geo_data (
- id BIGINT IDENTITY(1,1) PRIMARY KEY,
- name NVARCHAR(255) NOT NULL,
- -- Geography columns
- current_location GEOGRAPHY NOT NULL,
- delivery_route GEOGRAPHY,
- service_area GEOGRAPHY NOT NULL,
- -- Geometry columns
- building_footprint GEOMETRY,
- floor_plan GEOMETRY,
- -- Regular columns
- category VARCHAR(100),
- is_active BIT DEFAULT 1,
- last_updated DATETIME2 DEFAULT GETDATE()
-);
-GO
-
--- Create spatial indexes with different tessellation schemes
-CREATE SPATIAL INDEX idx_current_location ON advanced_spatial.geo_data(current_location)
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),
- CELLS_PER_OBJECT = 1,
- PAD_INDEX = OFF,
- FILLFACTOR = 100,
- ALLOW_ROW_LOCKS = OFF,
- ALLOW_PAGE_LOCKS = OFF
-);
-GO
-
-CREATE SPATIAL INDEX idx_delivery_route ON advanced_spatial.geo_data(delivery_route)
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),
- CELLS_PER_OBJECT = 8192,
- PAD_INDEX = ON,
- FILLFACTOR = 50
-);
-GO
-
-CREATE SPATIAL INDEX idx_service_area ON advanced_spatial.geo_data(service_area)
-USING GEOGRAPHY_GRID
-WITH (
- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 256
-);
-GO
-
-CREATE SPATIAL INDEX idx_building_footprint ON advanced_spatial.geo_data(building_footprint)
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (-10000, -10000, 10000, 10000),
- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = HIGH),
- CELLS_PER_OBJECT = 128
-);
-GO
-
-CREATE SPATIAL INDEX idx_floor_plan ON advanced_spatial.geo_data(floor_plan)
-USING GEOMETRY_GRID
-WITH (
- BOUNDING_BOX = (0, 0, 1000, 1000),
- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = MEDIUM, LEVEL_3 = LOW, LEVEL_4 = MEDIUM),
- CELLS_PER_OBJECT = 512
-);
-GO
-
--- Create filtered indexes
-CREATE INDEX idx_active_category ON advanced_spatial.geo_data(category)
-WHERE is_active = 1;
-GO
-
--- Create columnstore index
-CREATE NONCLUSTERED COLUMNSTORE INDEX ncci_geo_data
-ON advanced_spatial.geo_data(id, name, category, is_active, last_updated);
-GO
-`,
- wantMatch: true,
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel() // Safe to parallelize - shared container, unique databases per test
- // Use test name for database name - each test case has a unique name
- databaseName := fmt.Sprintf("test_%s_%s", strings.ReplaceAll(tc.name, " ", "_"), strings.ReplaceAll(uuid.New().String(), "-", "_"))
-
- // Create test database using container's master connection
- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", databaseName))
- require.NoError(t, err)
-
- // Connect to test database
- testDriver, err := createMSSQLDriver(ctx, container.GetHost(), container.GetPort(), databaseName)
- require.NoError(t, err)
- defer testDriver.Close(ctx)
-
- // Step 1: Initialize database schema and get metadata A
- err = executeSQL(ctx, testDriver, tc.setupSQL)
- require.NoError(t, err)
-
- metadataA, err := testDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 2: Call GetDatabaseDefinition to generate database definition X
- definitionX, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, metadataA)
- require.NoError(t, err)
- require.NotEmpty(t, definitionX)
-
- // Step 3: Create a new database and run the definition X
- newDatabaseName := fmt.Sprintf("copy_%s", databaseName)
-
- // Create new database using container's master connection
- _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", newDatabaseName))
- require.NoError(t, err)
-
- // Connect to the new database
- newDriver, err := createMSSQLDriver(ctx, container.GetHost(), container.GetPort(), newDatabaseName)
- require.NoError(t, err)
- defer newDriver.Close(ctx)
-
- // Execute the generated definition
- err = executeSQL(ctx, newDriver, definitionX)
- require.NoError(t, err)
-
- // Get metadata B
- metadataB, err := newDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 4: Compare metadata A and B
- if tc.wantMatch {
- // Note: Direct comparison might fail due to:
- // 1. System-generated names for constraints
- // 2. Minor differences in index/constraint definitions
-
- require.Equal(t, len(metadataA.Schemas), len(metadataB.Schemas), "Number of schemas should match")
-
- for i, schemaA := range metadataA.Schemas {
- schemaB := metadataB.Schemas[i]
- require.Equal(t, schemaA.Name, schemaB.Name, "Schema names should match")
- require.Equal(t, len(schemaA.Tables), len(schemaB.Tables), "Number of tables in schema %s should match", schemaA.Name)
- require.Equal(t, len(schemaA.Views), len(schemaB.Views), "Number of views in schema %s should match", schemaA.Name)
- require.Equal(t, len(schemaA.Functions), len(schemaB.Functions), "Number of functions in schema %s should match", schemaA.Name)
- require.Equal(t, len(schemaA.Procedures), len(schemaB.Procedures), "Number of procedures in schema %s should match", schemaA.Name)
-
- // Compare tables in detail
- for j, tableA := range schemaA.Tables {
- tableB := schemaB.Tables[j]
- require.Equal(t, tableA.Name, tableB.Name, "Table names should match")
- require.Equal(t, len(tableA.Columns), len(tableB.Columns), "Number of columns in table %s.%s should match", schemaA.Name, tableA.Name)
- require.Equal(t, len(tableA.Indexes), len(tableB.Indexes), "Number of indexes in table %s.%s should match", schemaA.Name, tableA.Name)
-
- // Compare spatial indexes specifically
- spatialIndexesA := make(map[string]*storepb.IndexMetadata)
- spatialIndexesB := make(map[string]*storepb.IndexMetadata)
-
- for _, idx := range tableA.Indexes {
- if idx.Type == "SPATIAL" {
- spatialIndexesA[idx.Name] = idx
- }
- }
-
- for _, idx := range tableB.Indexes {
- if idx.Type == "SPATIAL" {
- spatialIndexesB[idx.Name] = idx
- }
- }
-
- require.Equal(t, len(spatialIndexesA), len(spatialIndexesB), "Number of spatial indexes in table %s.%s should match", schemaA.Name, tableA.Name)
-
- // Compare each spatial index in detail
- for name, idxA := range spatialIndexesA {
- idxB, exists := spatialIndexesB[name]
- require.True(t, exists, "Spatial index %s should exist in both schemas", name)
-
- // Compare spatial index properties
- require.Equal(t, idxA.Type, idxB.Type, "Index type should match for %s", name)
- require.Equal(t, idxA.Unique, idxB.Unique, "Unique property should match for %s", name)
- require.Equal(t, idxA.Primary, idxB.Primary, "Primary property should match for %s", name)
- require.Equal(t, idxA.Expressions, idxB.Expressions, "Expressions should match for %s", name)
-
- // Compare spatial configuration
- if idxA.SpatialConfig != nil {
- require.NotNil(t, idxB.SpatialConfig, "Spatial config should exist for %s in schema B", name)
-
- // Compare method
- require.Equal(t, idxA.SpatialConfig.Method, idxB.SpatialConfig.Method, "Spatial method should match for %s", name)
-
- // Compare tessellation
- if idxA.SpatialConfig.Tessellation != nil {
- require.NotNil(t, idxB.SpatialConfig.Tessellation, "Tessellation config should exist for %s in schema B", name)
- require.Equal(t, idxA.SpatialConfig.Tessellation.Scheme, idxB.SpatialConfig.Tessellation.Scheme, "Tessellation scheme should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Tessellation.CellsPerObject, idxB.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should match for %s", name)
-
- // Compare grid levels
- require.Equal(t, len(idxA.SpatialConfig.Tessellation.GridLevels), len(idxB.SpatialConfig.Tessellation.GridLevels), "Number of grid levels should match for %s", name)
- for k, levelA := range idxA.SpatialConfig.Tessellation.GridLevels {
- levelB := idxB.SpatialConfig.Tessellation.GridLevels[k]
- require.Equal(t, levelA.Level, levelB.Level, "Grid level should match for %s", name)
- require.Equal(t, levelA.Density, levelB.Density, "Grid density should match for %s at level %d", name, levelA.Level)
- }
-
- // Compare bounding box for GEOMETRY indexes
- if idxA.SpatialConfig.Tessellation.BoundingBox != nil {
- require.NotNil(t, idxB.SpatialConfig.Tessellation.BoundingBox, "Bounding box should exist for %s in schema B", name)
- require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Xmin, idxB.SpatialConfig.Tessellation.BoundingBox.Xmin, "Xmin should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Ymin, idxB.SpatialConfig.Tessellation.BoundingBox.Ymin, "Ymin should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Xmax, idxB.SpatialConfig.Tessellation.BoundingBox.Xmax, "Xmax should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Tessellation.BoundingBox.Ymax, idxB.SpatialConfig.Tessellation.BoundingBox.Ymax, "Ymax should match for %s", name)
- }
- }
-
- // Compare storage configuration
- if idxA.SpatialConfig.Storage != nil {
- require.NotNil(t, idxB.SpatialConfig.Storage, "Storage config should exist for %s in schema B", name)
- require.Equal(t, idxA.SpatialConfig.Storage.Fillfactor, idxB.SpatialConfig.Storage.Fillfactor, "Fillfactor should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Storage.PadIndex, idxB.SpatialConfig.Storage.PadIndex, "PadIndex should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Storage.AllowRowLocks, idxB.SpatialConfig.Storage.AllowRowLocks, "AllowRowLocks should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Storage.AllowPageLocks, idxB.SpatialConfig.Storage.AllowPageLocks, "AllowPageLocks should match for %s", name)
- }
-
- // Compare dimensional configuration
- if idxA.SpatialConfig.Dimensional != nil {
- require.NotNil(t, idxB.SpatialConfig.Dimensional, "Dimensional config should exist for %s in schema B", name)
- require.Equal(t, idxA.SpatialConfig.Dimensional.DataType, idxB.SpatialConfig.Dimensional.DataType, "Spatial data type should match for %s", name)
- require.Equal(t, idxA.SpatialConfig.Dimensional.Dimensions, idxB.SpatialConfig.Dimensional.Dimensions, "Dimensions should match for %s", name)
- }
- }
- }
- }
- }
- }
- })
- }
-}
diff --git a/backend/plugin/schema/mssql/get_database_metadata_omni_test.go b/backend/plugin/schema/mssql/get_database_metadata_omni_test.go
index f084a59b0c..1bce398346 100644
--- a/backend/plugin/schema/mssql/get_database_metadata_omni_test.go
+++ b/backend/plugin/schema/mssql/get_database_metadata_omni_test.go
@@ -1,10 +1,15 @@
package mssql
import (
+ "encoding/json"
+ "io"
+ "os"
"testing"
"github.com/stretchr/testify/require"
+ "gopkg.in/yaml.v3"
+ "github.com/bytebase/bytebase/backend/common/yamltest"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
@@ -56,3 +61,45 @@ CREATE TABLE sales.orders (
require.Equal(t, []string{"customer_id"}, orders.ForeignKeys[0].Columns)
require.Equal(t, []string{"id"}, orders.ForeignKeys[0].ReferencedColumns)
}
+
+type getDatabaseMetadataCase struct {
+ Input string
+ Result string
+}
+
+func TestGetDatabaseMetadata(t *testing.T) {
+ tests := []getDatabaseMetadataCase{}
+ const (
+ record = false
+ )
+ var (
+ filepath = "test-data/test_get_database_metadata.yaml"
+ )
+
+ a := require.New(t)
+ yamlFile, err := os.Open(filepath)
+ a.NoError(err)
+ defer yamlFile.Close()
+
+ byteValue, err := io.ReadAll(yamlFile)
+ a.NoError(err)
+ a.NoError(yaml.Unmarshal(byteValue, &tests))
+
+ for i, t := range tests {
+ meta, err := GetDatabaseMetadata(t.Input)
+ a.NoError(err)
+
+ jsonBytes, err := json.MarshalIndent(meta, "", " ")
+ a.NoError(err)
+ result := string(jsonBytes)
+
+ if record {
+ tests[i].Result = result
+ } else {
+ a.Equal(t.Result, result, t.Input)
+ }
+ }
+ if record {
+ yamltest.Record(t, filepath, tests)
+ }
+}
diff --git a/backend/plugin/schema/mssql/get_database_metadata_test.go b/backend/plugin/schema/mssql/get_database_metadata_test.go
deleted file mode 100644
index b9a12bc9c0..0000000000
--- a/backend/plugin/schema/mssql/get_database_metadata_test.go
+++ /dev/null
@@ -1,55 +0,0 @@
-package mssql
-
-import (
- "encoding/json"
- "io"
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
- "gopkg.in/yaml.v3"
-
- "github.com/bytebase/bytebase/backend/common/yamltest"
-)
-
-type getDatabaseMetadataCase struct {
- Input string
- Result string
-}
-
-func TestGetDatabaseMetadata(t *testing.T) {
- tests := []getDatabaseMetadataCase{}
- const (
- record = false
- )
- var (
- filepath = "test-data/test_get_database_metadata.yaml"
- )
-
- a := require.New(t)
- yamlFile, err := os.Open(filepath)
- a.NoError(err)
- defer yamlFile.Close()
-
- byteValue, err := io.ReadAll(yamlFile)
- a.NoError(err)
- a.NoError(yaml.Unmarshal(byteValue, &tests))
-
- for i, t := range tests {
- meta, err := GetDatabaseMetadata(t.Input)
- a.NoError(err)
-
- jsonBytes, err := json.MarshalIndent(meta, "", " ")
- a.NoError(err)
- result := string(jsonBytes)
-
- if record {
- tests[i].Result = result
- } else {
- a.Equal(t.Result, result, t.Input)
- }
- }
- if record {
- yamltest.Record(t, filepath, tests)
- }
-}
diff --git a/backend/plugin/schema/mssql/get_view_dependencies_test.go b/backend/plugin/schema/mssql/get_view_dependencies_test.go
deleted file mode 100644
index 7081ce2391..0000000000
--- a/backend/plugin/schema/mssql/get_view_dependencies_test.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package mssql
-
-import (
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestGetViewDependencies(t *testing.T) {
- cases := []struct {
- name string
- viewDef string
- schema string
- expected []string
- }{
- {
- name: "simple",
- viewDef: "CREATE VIEW [dbo].[v] AS SELECT id FROM [dbo].[a]",
- schema: "dbo",
- expected: []string{"dbo.a"},
- },
- {
- name: "union two tables",
- viewDef: "CREATE VIEW [dbo].[v] AS " +
- "SELECT id FROM [dbo].[a] UNION SELECT id FROM [dbo].[b]",
- schema: "dbo",
- expected: []string{"dbo.a", "dbo.b"},
- },
- {
- name: "union all three tables",
- viewDef: "CREATE VIEW [dbo].[v] AS " +
- "SELECT id FROM [dbo].[a] UNION ALL " +
- "SELECT id FROM [dbo].[b] UNION ALL " +
- "SELECT id FROM [dbo].[c]",
- schema: "dbo",
- expected: []string{"dbo.a", "dbo.b", "dbo.c"},
- },
- {
- // GetQuerySpan with empty mock metadata cannot distinguish CTE
- // references from real table references, so cte names appear in
- // the dependency set. This is pre-existing behavior carried over
- // from the ANTLR implementation; the test pins it down.
- name: "cte",
- viewDef: "CREATE VIEW [dbo].[v] AS " +
- "WITH cte AS (SELECT id FROM [dbo].[a]) " +
- "SELECT id FROM cte",
- schema: "dbo",
- expected: []string{"dbo.a", "dbo.cte"},
- },
- }
-
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- got, err := getViewDependencies(tc.viewDef, tc.schema)
- require.NoError(t, err)
- require.ElementsMatch(t, tc.expected, got)
- })
- }
-}
diff --git a/backend/plugin/schema/mysql/antlr_dependency_test.go b/backend/plugin/schema/mysql/antlr_dependency_test.go
deleted file mode 100644
index a864a4536a..0000000000
--- a/backend/plugin/schema/mysql/antlr_dependency_test.go
+++ /dev/null
@@ -1,38 +0,0 @@
-package mysql
-
-import (
- "os"
- "path/filepath"
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestMySQLSchemaDoesNotKeepLegacyANTLRFallbacks(t *testing.T) {
- for _, name := range []string{
- "get_database_metadata.go",
- "walk_through.go",
- } {
- _, err := os.Stat(name)
- require.ErrorIs(t, err, os.ErrNotExist)
- }
-
- files, err := filepath.Glob("*.go")
- require.NoError(t, err)
- for _, file := range files {
- if filepath.Base(file) == "antlr_dependency_test.go" {
- continue
- }
- content, err := os.ReadFile(file)
- require.NoError(t, err)
- source := string(content)
-
- require.NotContains(t, source, "github.com/antlr4-go/antlr/v4", file)
- require.NotContains(t, source, "github.com/bytebase/parser/mysql", file)
- require.NotContains(t, source, "base.ANTLRAST", file)
- require.NotContains(t, source, "GetANTLRAST", file)
- require.NotContains(t, source, "ParseMySQL(", file)
- require.NotContains(t, source, "NormalizeMySQL", file)
- require.NotContains(t, source, "IsTopMySQLRule", file)
- }
-}
diff --git a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mysql/generate_migration_test.go
similarity index 88%
rename from backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
rename to backend/plugin/schema/mysql/generate_migration_test.go
index 127a1a99e5..a01b2d689c 100644
--- a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/generate_migration_test.go
@@ -1895,3 +1895,169 @@ func sortTriggersByName(triggers []*storepb.TriggerMetadata) {
return strings.Compare(a.Name, b.Name)
})
}
+
+// partitionedTable is the shape reported in BYT-10057: a daily BI table with a composite
+// primary key, a secondary index over the same ordered columns, and one range partition
+// per day. Each partition used to add another copy of the table to the diff, so the
+// generated script repeated the whole DDL block once per partition and died on the second
+// copy with "Duplicate key name" (1061) or "Multiple primary key defined" (1068).
+func partitionedTable(partitionCount int) *storepb.TableMetadata {
+ table := &storepb.TableMetadata{
+ Name: "ads_member_asset_by_account_channel_d",
+ Engine: "InnoDB",
+ Charset: "utf8mb4",
+ Collation: "utf8mb4_general_ci",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "dt", Type: "date", Nullable: false},
+ {Name: "member_id", Type: "bigint", Nullable: false},
+ {Name: "account_type", Type: "varchar(50)", Nullable: false},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {Name: "PRIMARY", Expressions: []string{"dt", "member_id", "account_type"}, Primary: true, Unique: true, Visible: true, Type: "BTREE"},
+ {Name: "sindex", Expressions: []string{"dt", "member_id", "account_type"}, Visible: true, Type: "BTREE"},
+ },
+ }
+ for i := range partitionCount {
+ table.Partitions = append(table.Partitions, &storepb.TablePartitionMetadata{
+ Name: fmt.Sprintf("p%d", i),
+ Type: storepb.TablePartitionMetadata_RANGE,
+ Expression: "to_days(`dt`)",
+ Value: fmt.Sprintf("%d", 739000+i),
+ })
+ }
+ return table
+}
+
+func databaseOf(tables ...*storepb.TableMetadata) *model.DatabaseMetadata {
+ return model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Name: "db",
+ Schemas: []*storepb.SchemaMetadata{{Name: "", Tables: tables}},
+ }, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)
+}
+
+func TestDiffMigrationPartitionedTableEmittedOnce(t *testing.T) {
+ for _, partitionCount := range []int{0, 1, 3, 180} {
+ t.Run(fmt.Sprintf("partitions=%d", partitionCount), func(t *testing.T) {
+ a := require.New(t)
+ source := partitionedTable(partitionCount)
+
+ // Target does not have the table yet: the 1061 shape.
+ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
+ a.NoError(err)
+ a.Equal(1, strings.Count(migration, "CREATE TABLE"), "CREATE TABLE must be emitted once:\n%s", migration)
+ a.Equal(1, strings.Count(migration, "CREATE INDEX `sindex`"), "sindex must be emitted once:\n%s", migration)
+
+ // Target has the table but neither the primary key nor the index: the 1068 shape.
+ bare := &storepb.TableMetadata{
+ Name: source.Name,
+ Engine: source.Engine,
+ Charset: source.Charset,
+ Collation: source.Collation,
+ Columns: source.Columns,
+ }
+ migration, err = schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(bare), databaseOf(source))
+ a.NoError(err)
+ a.Equal(1, strings.Count(migration, "ADD PRIMARY KEY"), "primary key must be added once:\n%s", migration)
+ a.Equal(1, strings.Count(migration, "CREATE INDEX `sindex`"), "sindex must be emitted once:\n%s", migration)
+
+ // Diffing the table against itself stays convergent.
+ migration, err = schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(source), databaseOf(partitionedTable(partitionCount)))
+ a.NoError(err)
+ a.Empty(migration)
+ })
+ }
+}
+
+func TestDiffMigrationPreservesPartitioning(t *testing.T) {
+ a := require.New(t)
+ source := partitionedTable(3)
+
+ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
+ a.NoError(err)
+ a.Contains(migration, "PARTITION BY RANGE (to_days(`dt`))", "created table must keep its partitioning:\n%s", migration)
+ for _, partition := range source.Partitions {
+ a.Contains(migration, "PARTITION `"+partition.Name+"`", "partition %s must be created", partition.Name)
+ }
+}
+
+// MySQL rejects a partitioned table whose partitions declare a different storage engine
+// than the table itself: "ERROR 1497 (HY000): The mix of handlers in the partitions is
+// not allowed in this version of MySQL". Partitioning a non-InnoDB table is still legal
+// on MySQL 5.7, so the partition clause has to carry the table's own engine.
+func TestDiffMigrationPartitionEngineMatchesTable(t *testing.T) {
+ for _, engine := range []string{"InnoDB", "MyISAM", "ARCHIVE"} {
+ t.Run(engine, func(t *testing.T) {
+ a := require.New(t)
+ source := partitionedTable(2)
+ source.Engine = engine
+
+ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
+ a.NoError(err)
+ a.Equal(len(source.Partitions), strings.Count(migration, "PARTITION `p"),
+ "every partition must be emitted:\n%s", migration)
+ a.Equal(len(source.Partitions)+1, strings.Count(migration, "ENGINE="+engine),
+ "the table and every partition must declare the same engine:\n%s", migration)
+ })
+ }
+}
+
+// Partition names are identifiers, so a reserved word is legal when quoted at creation and
+// comes back from information_schema unquoted. MySQL's own SHOW CREATE TABLE quotes them,
+// and the regenerated DDL has to as well or it is a syntax error.
+func TestDiffMigrationQuotesPartitionNames(t *testing.T) {
+ a := require.New(t)
+ source := partitionedTable(0)
+ source.Partitions = []*storepb.TablePartitionMetadata{
+ {Name: "select", Type: storepb.TablePartitionMetadata_RANGE, Expression: "to_days(`dt`)", Value: "739000"},
+ {Name: "order", Type: storepb.TablePartitionMetadata_RANGE, Expression: "to_days(`dt`)", Value: "MAXVALUE"},
+ }
+
+ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
+ a.NoError(err)
+ a.Contains(migration, "PARTITION `select`", "reserved-word partition name must be quoted:\n%s", migration)
+ a.Contains(migration, "PARTITION `order`", "reserved-word partition name must be quoted:\n%s", migration)
+}
+
+// tableAction returns the action the diff recorded for one table, or "" if absent.
+func tableAction(diff *schema.MetadataDiff, name string) schema.MetadataDiffAction {
+ for _, tableDiff := range diff.TableChanges {
+ if tableDiff.TableName == name {
+ return tableDiff.Action
+ }
+ }
+ return ""
+}
+
+// The differ decides whether a table exists in the other snapshot by name. A partition of
+// some other table may carry that name, and resolving it as if it were the table makes the
+// differ miss a drop and mistake a create for a modification of the partition's owner.
+func TestDiffMigrationPartitionAliasDoesNotMaskTableExistence(t *testing.T) {
+ archive := &storepb.TableMetadata{
+ Name: "archive", Engine: "InnoDB",
+ Columns: []*storepb.ColumnMetadata{{Name: "archive_col", Type: "int"}},
+ }
+ events := func(partitions ...*storepb.TablePartitionMetadata) *storepb.TableMetadata {
+ return &storepb.TableMetadata{
+ Name: "events", Engine: "InnoDB",
+ Columns: []*storepb.ColumnMetadata{{Name: "events_col", Type: "int"}},
+ Partitions: partitions,
+ }
+ }
+ alias := &storepb.TablePartitionMetadata{Name: "archive", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"}
+
+ t.Run("drop", func(t *testing.T) {
+ a := require.New(t)
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MYSQL,
+ databaseOf(archive, events()), databaseOf(events(alias)))
+ a.NoError(err)
+ a.Equal(schema.MetadataDiffActionDrop, tableAction(diff, "archive"))
+ })
+
+ t.Run("create", func(t *testing.T) {
+ a := require.New(t)
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MYSQL,
+ databaseOf(events(alias)), databaseOf(archive, events()))
+ a.NoError(err)
+ a.Equal(schema.MetadataDiffActionCreate, tableAction(diff, "archive"))
+ })
+}
diff --git a/backend/plugin/schema/mysql/get_database_definition_test.go b/backend/plugin/schema/mysql/get_database_definition_test.go
index 22d7b0c63a..396ce4f0bd 100644
--- a/backend/plugin/schema/mysql/get_database_definition_test.go
+++ b/backend/plugin/schema/mysql/get_database_definition_test.go
@@ -1,12 +1,22 @@
package mysql
import (
+ "context"
+ "fmt"
"strings"
"testing"
+ // Import MySQL driver
+ _ "github.com/go-sql-driver/mysql"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/uuid"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/testing/protocmp"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
)
func TestGetTableDefinitionWithCheckConstraint(t *testing.T) {
@@ -468,3 +478,509 @@ func TestWriteColumnDefinitionBodyGeneratedOrder(t *testing.T) {
"point NOT NULL /*!80003 SRID 4326 */",
render(&storepb.ColumnMetadata{Name: "loc", Type: "point", Nullable: false, Srid: srid(4326)}))
}
+
+//nolint:tparallel
+func TestGetDatabaseDefinition(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping MySQL testcontainer test in short mode")
+ }
+
+ ctx := context.Background()
+
+ // Start shared MySQL container for all subtests
+ container := testcontainer.SharedMySQLContainer(t)
+
+ type testCase struct {
+ description string
+ originalDDL string
+ }
+
+ testCases := []testCase{
+ {
+ description: "Basic tables with various column types",
+ originalDDL: `
+CREATE TABLE users (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ username VARCHAR(50) NOT NULL UNIQUE,
+ email VARCHAR(100) NOT NULL,
+ age INT CHECK (age >= 18),
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ profile JSON,
+ is_active BOOLEAN DEFAULT TRUE,
+ INDEX idx_email (email),
+ INDEX idx_created_at (created_at)
+);
+
+CREATE TABLE posts (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ user_id INT NOT NULL,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ published_at DATETIME,
+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
+ INDEX idx_user_published (user_id, published_at)
+);
+`,
+ },
+ {
+ description: "Generated columns and complex indexes",
+ originalDDL: `
+CREATE TABLE products (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ price DECIMAL(10, 2) NOT NULL,
+ tax_rate DECIMAL(5, 2) NOT NULL DEFAULT 0.08,
+ price_with_tax DECIMAL(10, 2) AS (price * (1 + tax_rate)) STORED,
+ description TEXT,
+ tags JSON,
+ FULLTEXT idx_fulltext (name, description)
+);
+
+CREATE TABLE inventory (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ product_id INT NOT NULL,
+ warehouse VARCHAR(50) NOT NULL,
+ quantity INT NOT NULL DEFAULT 0,
+ last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ UNIQUE KEY uk_product_warehouse (product_id, warehouse),
+ FOREIGN KEY (product_id) REFERENCES products(id)
+);
+`,
+ },
+ {
+ description: "Views and triggers",
+ originalDDL: `
+CREATE TABLE orders (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ order_number VARCHAR(20) NOT NULL UNIQUE,
+ customer_name VARCHAR(100) NOT NULL,
+ total_amount DECIMAL(10, 2) NOT NULL,
+ status VARCHAR(20) DEFAULT 'pending',
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE order_history (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ order_id INT NOT NULL,
+ old_status VARCHAR(20),
+ new_status VARCHAR(20),
+ changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ FOREIGN KEY (order_id) REFERENCES orders(id)
+);
+
+CREATE VIEW pending_orders AS
+SELECT id, order_number, customer_name, total_amount, created_at
+FROM orders
+WHERE status = 'pending'
+ORDER BY created_at DESC;
+
+CREATE TRIGGER order_status_change
+AFTER UPDATE ON orders
+FOR EACH ROW
+BEGIN
+ IF OLD.status != NEW.status THEN
+ INSERT INTO order_history (order_id, old_status, new_status)
+ VALUES (NEW.id, OLD.status, NEW.status);
+ END IF;
+END;
+`,
+ },
+ {
+ description: "Stored procedures and functions",
+ originalDDL: `
+CREATE TABLE accounts (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ account_number VARCHAR(20) NOT NULL UNIQUE,
+ balance DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+DELIMITER $$
+
+CREATE FUNCTION calculate_interest(principal DECIMAL(15, 2), rate DECIMAL(5, 4), years INT)
+RETURNS DECIMAL(15, 2)
+DETERMINISTIC
+READS SQL DATA
+BEGIN
+ RETURN principal * POW(1 + rate, years);
+END$$
+
+CREATE PROCEDURE transfer_funds(
+ IN from_account VARCHAR(20),
+ IN to_account VARCHAR(20),
+ IN amount DECIMAL(15, 2)
+)
+BEGIN
+ DECLARE from_balance DECIMAL(15, 2);
+
+ START TRANSACTION;
+
+ SELECT balance INTO from_balance
+ FROM accounts
+ WHERE account_number = from_account
+ FOR UPDATE;
+
+ IF from_balance >= amount THEN
+ UPDATE accounts
+ SET balance = balance - amount
+ WHERE account_number = from_account;
+
+ UPDATE accounts
+ SET balance = balance + amount
+ WHERE account_number = to_account;
+
+ COMMIT;
+ ELSE
+ ROLLBACK;
+ SIGNAL SQLSTATE '45000'
+ SET MESSAGE_TEXT = 'Insufficient funds';
+ END IF;
+END$$
+
+DELIMITER ;
+`,
+ },
+ {
+ description: "Partitioned tables",
+ originalDDL: `
+-- RANGE partition
+CREATE TABLE sales (
+ id INT NOT NULL AUTO_INCREMENT,
+ sale_date DATE NOT NULL,
+ product_id INT NOT NULL,
+ quantity INT NOT NULL,
+ amount DECIMAL(10, 2) NOT NULL,
+ PRIMARY KEY (id, sale_date)
+) PARTITION BY RANGE (YEAR(sale_date)) (
+ PARTITION p2022 VALUES LESS THAN (2023),
+ PARTITION p2023 VALUES LESS THAN (2024),
+ PARTITION p2024 VALUES LESS THAN (2025),
+ PARTITION p_future VALUES LESS THAN MAXVALUE
+);
+
+-- HASH partition
+CREATE TABLE employees (
+ id INT NOT NULL,
+ name VARCHAR(100) NOT NULL,
+ department_id INT NOT NULL,
+ hired_date DATE,
+ PRIMARY KEY (id)
+) PARTITION BY HASH(id) PARTITIONS 4;
+
+-- LIST partition
+CREATE TABLE customer_regions (
+ id INT NOT NULL AUTO_INCREMENT,
+ customer_name VARCHAR(100) NOT NULL,
+ region VARCHAR(20) NOT NULL,
+ sales_amount DECIMAL(10, 2),
+ PRIMARY KEY (id, region)
+) PARTITION BY LIST COLUMNS(region) (
+ PARTITION p_north VALUES IN ('north', 'northeast', 'northwest'),
+ PARTITION p_south VALUES IN ('south', 'southeast', 'southwest'),
+ PARTITION p_east VALUES IN ('east'),
+ PARTITION p_west VALUES IN ('west'),
+ PARTITION p_central VALUES IN ('central')
+);
+
+-- KEY partition
+CREATE TABLE user_sessions (
+ session_id VARCHAR(64) NOT NULL,
+ user_id INT NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ PRIMARY KEY (session_id)
+) PARTITION BY KEY() PARTITIONS 8;
+
+-- RANGE COLUMNS partition
+CREATE TABLE order_archive (
+ order_id INT NOT NULL,
+ order_date DATE NOT NULL,
+ customer_id INT NOT NULL,
+ status VARCHAR(20) NOT NULL,
+ total_amount DECIMAL(10, 2),
+ PRIMARY KEY (order_id, order_date)
+) PARTITION BY RANGE COLUMNS(order_date) (
+ PARTITION p_2022_q1 VALUES LESS THAN ('2022-04-01'),
+ PARTITION p_2022_q2 VALUES LESS THAN ('2022-07-01'),
+ PARTITION p_2022_q3 VALUES LESS THAN ('2022-10-01'),
+ PARTITION p_2022_q4 VALUES LESS THAN ('2023-01-01'),
+ PARTITION p_2023_and_later VALUES LESS THAN (MAXVALUE)
+);
+`,
+ },
+ {
+ description: "Events",
+ originalDDL: `
+CREATE TABLE daily_stats (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ stat_date DATE NOT NULL UNIQUE,
+ total_orders INT DEFAULT 0,
+ total_revenue DECIMAL(15, 2) DEFAULT 0.00,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE EVENT IF NOT EXISTS update_daily_stats
+ON SCHEDULE EVERY 1 DAY
+STARTS CURRENT_TIMESTAMP
+DO
+ INSERT INTO daily_stats (stat_date, total_orders, total_revenue)
+ VALUES (CURDATE() - INTERVAL 1 DAY, 0, 0.00)
+ ON DUPLICATE KEY UPDATE
+ total_orders = VALUES(total_orders),
+ total_revenue = VALUES(total_revenue);
+`,
+ },
+ {
+ description: "Character sets and collations",
+ originalDDL: `
+CREATE TABLE translations (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ language_code VARCHAR(5) CHARACTER SET ascii NOT NULL,
+ content_key VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
+ translation TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
+ UNIQUE KEY uk_lang_key (language_code, content_key)
+) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
+`,
+ },
+ {
+ description: "Check constraints with string literals and function with multiline parameters",
+ originalDDL: `
+CREATE TABLE transactions (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ type VARCHAR(10),
+ amount DECIMAL(10, 2),
+ CONSTRAINT c1 CHECK (type = 'A' AND amount IS NOT NULL)
+);
+
+DELIMITER $$
+
+CREATE FUNCTION lookup_token(
+ p_a BIGINT,
+ p_b VARCHAR(36)
+) RETURNS CHAR(36) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
+ NO SQL
+ DETERMINISTIC
+BEGIN
+ RETURN p_b;
+END$$
+
+DELIMITER ;
+`,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.description, func(t *testing.T) {
+ t.Parallel()
+
+ // Create unique test databases for parallel execution
+ testDBName := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+ newDBName := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
+ // Create initial test database
+ _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", testDBName))
+ require.NoError(t, err)
+
+ // Step 1: Initialize the database schema and get metadata A
+ driverA, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), testDBName)
+ require.NoError(t, err)
+ defer driverA.Close(ctx)
+
+ _, err = driverA.Execute(ctx, tc.originalDDL, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ metadataA, err := driverA.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Step 2: Call GetDatabaseDefinition to generate the database definition X
+ defCtx := schema.GetDefinitionContext{
+ SkipBackupSchema: false,
+ PrintHeader: true,
+ }
+ definitionX, err := schema.GetDatabaseDefinition(storepb.Engine_MYSQL, defCtx, metadataA)
+ require.NoError(t, err)
+ require.NotEmpty(t, definitionX)
+
+ // Step 3: Create a new database and apply the generated DDL
+ _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", newDBName))
+ require.NoError(t, err)
+
+ driverB, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), newDBName)
+ require.NoError(t, err)
+ defer driverB.Close(ctx)
+
+ _, err = driverB.Execute(ctx, definitionX, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ metadataB, err := driverB.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Step 4: Compare the database metadata A and B, should be the same
+ normalizeMetadata(metadataA)
+ normalizeMetadata(metadataB)
+
+ opts := []cmp.Option{
+ protocmp.Transform(),
+ protocmp.IgnoreFields(&storepb.TableMetadata{}, "row_count", "data_size", "index_size", "data_free"),
+ }
+
+ // Add custom ignored fields for specific test cases (for events test)
+ if tc.description == "Events" {
+ // Ignore time-specific fields that vary between runs
+ opts = append(opts, protocmp.IgnoreFields(&storepb.EventMetadata{}, "time_zone", "sql_mode", "character_set_client"))
+ }
+
+ if diff := cmp.Diff(metadataA, metadataB, opts...); diff != "" {
+ t.Errorf("Metadata mismatch (-want +got):\n%s", diff)
+ }
+ })
+ }
+}
+
+// normalizeMetadata normalizes the metadata to ignore differences that don't affect schema equivalence
+func normalizeMetadata(metadata *storepb.DatabaseSchemaMetadata) {
+ // Clear database name as it will differ between original and recreated
+ metadata.Name = ""
+
+ // Normalize AUTO_INCREMENT values to 0 as they can differ
+ for _, schema := range metadata.Schemas {
+ for _, table := range schema.Tables {
+ // Clear runtime-specific values
+ table.RowCount = 0
+ table.DataSize = 0
+ table.IndexSize = 0
+ table.DataFree = 0
+
+ // Normalize column defaults
+ for _, column := range table.Columns {
+ // MySQL might represent defaults differently
+ if def := column.GetDefault(); def != "" {
+ // Normalize CURRENT_TIMESTAMP variations
+ if def == "CURRENT_TIMESTAMP" ||
+ def == "current_timestamp()" ||
+ def == "now()" {
+ column.Default = "CURRENT_TIMESTAMP"
+ }
+ }
+ }
+
+ // Remove duplicate check constraints (sometimes appear as both message and string)
+ seen := make(map[string]bool)
+ var uniqueChecks []*storepb.CheckConstraintMetadata
+ for _, check := range table.CheckConstraints {
+ key := fmt.Sprintf("%s:%s", check.Name, check.Expression)
+ if !seen[key] {
+ seen[key] = true
+ uniqueChecks = append(uniqueChecks, check)
+ }
+ }
+ table.CheckConstraints = uniqueChecks
+ }
+ }
+}
+
+// TestGetDatabaseDefinitionWithConnectedDeps tests the ability to handle complex foreign key dependencies
+func TestGetDatabaseDefinitionWithConnectedDeps(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping MySQL testcontainer test in short mode")
+ }
+
+ // Unique per run: TestMain keeps one container for the whole package, so a
+ // fixed name collides with itself under go test -count=2.
+ databaseName := fmt.Sprintf("test_complex_deps_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
+ const (
+ originalDDL = `
+CREATE TABLE department (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ parent_id INT,
+ FOREIGN KEY (parent_id) REFERENCES department(id) ON DELETE SET NULL
+);
+
+CREATE TABLE employee (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ department_id INT,
+ manager_id INT,
+ FOREIGN KEY (department_id) REFERENCES department(id) ON DELETE SET NULL,
+ FOREIGN KEY (manager_id) REFERENCES employee(id) ON DELETE SET NULL
+);
+
+CREATE TABLE project (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ lead_id INT NOT NULL,
+ department_id INT NOT NULL,
+ FOREIGN KEY (lead_id) REFERENCES employee(id),
+ FOREIGN KEY (department_id) REFERENCES department(id)
+);
+
+CREATE TABLE project_member (
+ project_id INT NOT NULL,
+ employee_id INT NOT NULL,
+ role VARCHAR(50),
+ PRIMARY KEY (project_id, employee_id),
+ FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE,
+ FOREIGN KEY (employee_id) REFERENCES employee(id) ON DELETE CASCADE
+);
+`
+ )
+
+ ctx := context.Background()
+
+ // Start MySQL container
+ container := testcontainer.SharedMySQLContainer(t)
+
+ // Create test database
+ _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", databaseName))
+ require.NoError(t, err)
+
+ // Step 1: Initialize the database schema
+ driverA, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), databaseName)
+ require.NoError(t, err)
+ defer driverA.Close(ctx)
+
+ _, err = driverA.Execute(ctx, originalDDL, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ metadataA, err := driverA.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Step 2: Generate definition
+ defCtx := schema.GetDefinitionContext{
+ SkipBackupSchema: false,
+ PrintHeader: true,
+ }
+ definitionX, err := schema.GetDatabaseDefinition(storepb.Engine_MYSQL, defCtx, metadataA)
+ require.NoError(t, err)
+
+ // Step 3: Create new database and apply definition
+ newDBName := fmt.Sprintf("%s_recreated", databaseName)
+ _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", newDBName))
+ require.NoError(t, err)
+
+ driverB, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), newDBName)
+ require.NoError(t, err)
+ defer driverB.Close(ctx)
+
+ _, err = driverB.Execute(ctx, definitionX, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ metadataB, err := driverB.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Compare
+ normalizeMetadata(metadataA)
+ normalizeMetadata(metadataB)
+
+ opts := []cmp.Option{
+ protocmp.Transform(),
+ protocmp.IgnoreFields(&storepb.TableMetadata{}, "row_count", "data_size", "index_size", "data_free"),
+ }
+
+ if diff := cmp.Diff(metadataA, metadataB, opts...); diff != "" {
+ t.Errorf("Metadata mismatch (-want +got):\n%s", diff)
+ }
+}
diff --git a/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go b/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go
deleted file mode 100644
index aeb828e5c1..0000000000
--- a/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go
+++ /dev/null
@@ -1,526 +0,0 @@
-package mysql
-
-import (
- "context"
- "fmt"
- "strings"
- "testing"
-
- // Import MySQL driver
- _ "github.com/go-sql-driver/mysql"
- "github.com/google/go-cmp/cmp"
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
- "google.golang.org/protobuf/testing/protocmp"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-//nolint:tparallel
-func TestGetDatabaseDefinition(t *testing.T) {
- if testing.Short() {
- t.Skip("Skipping MySQL testcontainer test in short mode")
- }
-
- ctx := context.Background()
-
- // Start shared MySQL container for all subtests
- container := testcontainer.SharedMySQLContainer(t)
-
- type testCase struct {
- description string
- originalDDL string
- }
-
- testCases := []testCase{
- {
- description: "Basic tables with various column types",
- originalDDL: `
-CREATE TABLE users (
- id INT PRIMARY KEY AUTO_INCREMENT,
- username VARCHAR(50) NOT NULL UNIQUE,
- email VARCHAR(100) NOT NULL,
- age INT CHECK (age >= 18),
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- profile JSON,
- is_active BOOLEAN DEFAULT TRUE,
- INDEX idx_email (email),
- INDEX idx_created_at (created_at)
-);
-
-CREATE TABLE posts (
- id INT PRIMARY KEY AUTO_INCREMENT,
- user_id INT NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- published_at DATETIME,
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
- INDEX idx_user_published (user_id, published_at)
-);
-`,
- },
- {
- description: "Generated columns and complex indexes",
- originalDDL: `
-CREATE TABLE products (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- price DECIMAL(10, 2) NOT NULL,
- tax_rate DECIMAL(5, 2) NOT NULL DEFAULT 0.08,
- price_with_tax DECIMAL(10, 2) AS (price * (1 + tax_rate)) STORED,
- description TEXT,
- tags JSON,
- FULLTEXT idx_fulltext (name, description)
-);
-
-CREATE TABLE inventory (
- id INT PRIMARY KEY AUTO_INCREMENT,
- product_id INT NOT NULL,
- warehouse VARCHAR(50) NOT NULL,
- quantity INT NOT NULL DEFAULT 0,
- last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- UNIQUE KEY uk_product_warehouse (product_id, warehouse),
- FOREIGN KEY (product_id) REFERENCES products(id)
-);
-`,
- },
- {
- description: "Views and triggers",
- originalDDL: `
-CREATE TABLE orders (
- id INT PRIMARY KEY AUTO_INCREMENT,
- order_number VARCHAR(20) NOT NULL UNIQUE,
- customer_name VARCHAR(100) NOT NULL,
- total_amount DECIMAL(10, 2) NOT NULL,
- status VARCHAR(20) DEFAULT 'pending',
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE order_history (
- id INT PRIMARY KEY AUTO_INCREMENT,
- order_id INT NOT NULL,
- old_status VARCHAR(20),
- new_status VARCHAR(20),
- changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- FOREIGN KEY (order_id) REFERENCES orders(id)
-);
-
-CREATE VIEW pending_orders AS
-SELECT id, order_number, customer_name, total_amount, created_at
-FROM orders
-WHERE status = 'pending'
-ORDER BY created_at DESC;
-
-CREATE TRIGGER order_status_change
-AFTER UPDATE ON orders
-FOR EACH ROW
-BEGIN
- IF OLD.status != NEW.status THEN
- INSERT INTO order_history (order_id, old_status, new_status)
- VALUES (NEW.id, OLD.status, NEW.status);
- END IF;
-END;
-`,
- },
- {
- description: "Stored procedures and functions",
- originalDDL: `
-CREATE TABLE accounts (
- id INT PRIMARY KEY AUTO_INCREMENT,
- account_number VARCHAR(20) NOT NULL UNIQUE,
- balance DECIMAL(15, 2) NOT NULL DEFAULT 0.00,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-DELIMITER $$
-
-CREATE FUNCTION calculate_interest(principal DECIMAL(15, 2), rate DECIMAL(5, 4), years INT)
-RETURNS DECIMAL(15, 2)
-DETERMINISTIC
-READS SQL DATA
-BEGIN
- RETURN principal * POW(1 + rate, years);
-END$$
-
-CREATE PROCEDURE transfer_funds(
- IN from_account VARCHAR(20),
- IN to_account VARCHAR(20),
- IN amount DECIMAL(15, 2)
-)
-BEGIN
- DECLARE from_balance DECIMAL(15, 2);
-
- START TRANSACTION;
-
- SELECT balance INTO from_balance
- FROM accounts
- WHERE account_number = from_account
- FOR UPDATE;
-
- IF from_balance >= amount THEN
- UPDATE accounts
- SET balance = balance - amount
- WHERE account_number = from_account;
-
- UPDATE accounts
- SET balance = balance + amount
- WHERE account_number = to_account;
-
- COMMIT;
- ELSE
- ROLLBACK;
- SIGNAL SQLSTATE '45000'
- SET MESSAGE_TEXT = 'Insufficient funds';
- END IF;
-END$$
-
-DELIMITER ;
-`,
- },
- {
- description: "Partitioned tables",
- originalDDL: `
--- RANGE partition
-CREATE TABLE sales (
- id INT NOT NULL AUTO_INCREMENT,
- sale_date DATE NOT NULL,
- product_id INT NOT NULL,
- quantity INT NOT NULL,
- amount DECIMAL(10, 2) NOT NULL,
- PRIMARY KEY (id, sale_date)
-) PARTITION BY RANGE (YEAR(sale_date)) (
- PARTITION p2022 VALUES LESS THAN (2023),
- PARTITION p2023 VALUES LESS THAN (2024),
- PARTITION p2024 VALUES LESS THAN (2025),
- PARTITION p_future VALUES LESS THAN MAXVALUE
-);
-
--- HASH partition
-CREATE TABLE employees (
- id INT NOT NULL,
- name VARCHAR(100) NOT NULL,
- department_id INT NOT NULL,
- hired_date DATE,
- PRIMARY KEY (id)
-) PARTITION BY HASH(id) PARTITIONS 4;
-
--- LIST partition
-CREATE TABLE customer_regions (
- id INT NOT NULL AUTO_INCREMENT,
- customer_name VARCHAR(100) NOT NULL,
- region VARCHAR(20) NOT NULL,
- sales_amount DECIMAL(10, 2),
- PRIMARY KEY (id, region)
-) PARTITION BY LIST COLUMNS(region) (
- PARTITION p_north VALUES IN ('north', 'northeast', 'northwest'),
- PARTITION p_south VALUES IN ('south', 'southeast', 'southwest'),
- PARTITION p_east VALUES IN ('east'),
- PARTITION p_west VALUES IN ('west'),
- PARTITION p_central VALUES IN ('central')
-);
-
--- KEY partition
-CREATE TABLE user_sessions (
- session_id VARCHAR(64) NOT NULL,
- user_id INT NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- PRIMARY KEY (session_id)
-) PARTITION BY KEY() PARTITIONS 8;
-
--- RANGE COLUMNS partition
-CREATE TABLE order_archive (
- order_id INT NOT NULL,
- order_date DATE NOT NULL,
- customer_id INT NOT NULL,
- status VARCHAR(20) NOT NULL,
- total_amount DECIMAL(10, 2),
- PRIMARY KEY (order_id, order_date)
-) PARTITION BY RANGE COLUMNS(order_date) (
- PARTITION p_2022_q1 VALUES LESS THAN ('2022-04-01'),
- PARTITION p_2022_q2 VALUES LESS THAN ('2022-07-01'),
- PARTITION p_2022_q3 VALUES LESS THAN ('2022-10-01'),
- PARTITION p_2022_q4 VALUES LESS THAN ('2023-01-01'),
- PARTITION p_2023_and_later VALUES LESS THAN (MAXVALUE)
-);
-`,
- },
- {
- description: "Events",
- originalDDL: `
-CREATE TABLE daily_stats (
- id INT PRIMARY KEY AUTO_INCREMENT,
- stat_date DATE NOT NULL UNIQUE,
- total_orders INT DEFAULT 0,
- total_revenue DECIMAL(15, 2) DEFAULT 0.00,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE EVENT IF NOT EXISTS update_daily_stats
-ON SCHEDULE EVERY 1 DAY
-STARTS CURRENT_TIMESTAMP
-DO
- INSERT INTO daily_stats (stat_date, total_orders, total_revenue)
- VALUES (CURDATE() - INTERVAL 1 DAY, 0, 0.00)
- ON DUPLICATE KEY UPDATE
- total_orders = VALUES(total_orders),
- total_revenue = VALUES(total_revenue);
-`,
- },
- {
- description: "Character sets and collations",
- originalDDL: `
-CREATE TABLE translations (
- id INT PRIMARY KEY AUTO_INCREMENT,
- language_code VARCHAR(5) CHARACTER SET ascii NOT NULL,
- content_key VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
- translation TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
- UNIQUE KEY uk_lang_key (language_code, content_key)
-) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-`,
- },
- {
- description: "Check constraints with string literals and function with multiline parameters",
- originalDDL: `
-CREATE TABLE transactions (
- id INT PRIMARY KEY AUTO_INCREMENT,
- type VARCHAR(10),
- amount DECIMAL(10, 2),
- CONSTRAINT c1 CHECK (type = 'A' AND amount IS NOT NULL)
-);
-
-DELIMITER $$
-
-CREATE FUNCTION lookup_token(
- p_a BIGINT,
- p_b VARCHAR(36)
-) RETURNS CHAR(36) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
- NO SQL
- DETERMINISTIC
-BEGIN
- RETURN p_b;
-END$$
-
-DELIMITER ;
-`,
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- t.Parallel()
-
- // Create unique test databases for parallel execution
- testDBName := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
- newDBName := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
-
- // Create initial test database
- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", testDBName))
- require.NoError(t, err)
-
- // Step 1: Initialize the database schema and get metadata A
- driverA, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), testDBName)
- require.NoError(t, err)
- defer driverA.Close(ctx)
-
- _, err = driverA.Execute(ctx, tc.originalDDL, db.ExecuteOptions{})
- require.NoError(t, err)
-
- metadataA, err := driverA.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 2: Call GetDatabaseDefinition to generate the database definition X
- defCtx := schema.GetDefinitionContext{
- SkipBackupSchema: false,
- PrintHeader: true,
- }
- definitionX, err := schema.GetDatabaseDefinition(storepb.Engine_MYSQL, defCtx, metadataA)
- require.NoError(t, err)
- require.NotEmpty(t, definitionX)
-
- // Step 3: Create a new database and apply the generated DDL
- _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", newDBName))
- require.NoError(t, err)
-
- driverB, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), newDBName)
- require.NoError(t, err)
- defer driverB.Close(ctx)
-
- _, err = driverB.Execute(ctx, definitionX, db.ExecuteOptions{})
- require.NoError(t, err)
-
- metadataB, err := driverB.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 4: Compare the database metadata A and B, should be the same
- normalizeMetadata(metadataA)
- normalizeMetadata(metadataB)
-
- opts := []cmp.Option{
- protocmp.Transform(),
- protocmp.IgnoreFields(&storepb.TableMetadata{}, "row_count", "data_size", "index_size", "data_free"),
- }
-
- // Add custom ignored fields for specific test cases (for events test)
- if tc.description == "Events" {
- // Ignore time-specific fields that vary between runs
- opts = append(opts, protocmp.IgnoreFields(&storepb.EventMetadata{}, "time_zone", "sql_mode", "character_set_client"))
- }
-
- if diff := cmp.Diff(metadataA, metadataB, opts...); diff != "" {
- t.Errorf("Metadata mismatch (-want +got):\n%s", diff)
- }
- })
- }
-}
-
-// normalizeMetadata normalizes the metadata to ignore differences that don't affect schema equivalence
-func normalizeMetadata(metadata *storepb.DatabaseSchemaMetadata) {
- // Clear database name as it will differ between original and recreated
- metadata.Name = ""
-
- // Normalize AUTO_INCREMENT values to 0 as they can differ
- for _, schema := range metadata.Schemas {
- for _, table := range schema.Tables {
- // Clear runtime-specific values
- table.RowCount = 0
- table.DataSize = 0
- table.IndexSize = 0
- table.DataFree = 0
-
- // Normalize column defaults
- for _, column := range table.Columns {
- // MySQL might represent defaults differently
- if def := column.GetDefault(); def != "" {
- // Normalize CURRENT_TIMESTAMP variations
- if def == "CURRENT_TIMESTAMP" ||
- def == "current_timestamp()" ||
- def == "now()" {
- column.Default = "CURRENT_TIMESTAMP"
- }
- }
- }
-
- // Remove duplicate check constraints (sometimes appear as both message and string)
- seen := make(map[string]bool)
- var uniqueChecks []*storepb.CheckConstraintMetadata
- for _, check := range table.CheckConstraints {
- key := fmt.Sprintf("%s:%s", check.Name, check.Expression)
- if !seen[key] {
- seen[key] = true
- uniqueChecks = append(uniqueChecks, check)
- }
- }
- table.CheckConstraints = uniqueChecks
- }
- }
-}
-
-// TestGetDatabaseDefinitionWithConnectedDeps tests the ability to handle complex foreign key dependencies
-func TestGetDatabaseDefinitionWithConnectedDeps(t *testing.T) {
- if testing.Short() {
- t.Skip("Skipping MySQL testcontainer test in short mode")
- }
-
- // Unique per run: TestMain keeps one container for the whole package, so a
- // fixed name collides with itself under go test -count=2.
- databaseName := fmt.Sprintf("test_complex_deps_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
-
- const (
- originalDDL = `
-CREATE TABLE department (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- parent_id INT,
- FOREIGN KEY (parent_id) REFERENCES department(id) ON DELETE SET NULL
-);
-
-CREATE TABLE employee (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- department_id INT,
- manager_id INT,
- FOREIGN KEY (department_id) REFERENCES department(id) ON DELETE SET NULL,
- FOREIGN KEY (manager_id) REFERENCES employee(id) ON DELETE SET NULL
-);
-
-CREATE TABLE project (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- lead_id INT NOT NULL,
- department_id INT NOT NULL,
- FOREIGN KEY (lead_id) REFERENCES employee(id),
- FOREIGN KEY (department_id) REFERENCES department(id)
-);
-
-CREATE TABLE project_member (
- project_id INT NOT NULL,
- employee_id INT NOT NULL,
- role VARCHAR(50),
- PRIMARY KEY (project_id, employee_id),
- FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE,
- FOREIGN KEY (employee_id) REFERENCES employee(id) ON DELETE CASCADE
-);
-`
- )
-
- ctx := context.Background()
-
- // Start MySQL container
- container := testcontainer.SharedMySQLContainer(t)
-
- // Create test database
- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", databaseName))
- require.NoError(t, err)
-
- // Step 1: Initialize the database schema
- driverA, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), databaseName)
- require.NoError(t, err)
- defer driverA.Close(ctx)
-
- _, err = driverA.Execute(ctx, originalDDL, db.ExecuteOptions{})
- require.NoError(t, err)
-
- metadataA, err := driverA.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 2: Generate definition
- defCtx := schema.GetDefinitionContext{
- SkipBackupSchema: false,
- PrintHeader: true,
- }
- definitionX, err := schema.GetDatabaseDefinition(storepb.Engine_MYSQL, defCtx, metadataA)
- require.NoError(t, err)
-
- // Step 3: Create new database and apply definition
- newDBName := fmt.Sprintf("%s_recreated", databaseName)
- _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", newDBName))
- require.NoError(t, err)
-
- driverB, err := createMySQLDriver(ctx, container.GetHost(), container.GetPort(), newDBName)
- require.NoError(t, err)
- defer driverB.Close(ctx)
-
- _, err = driverB.Execute(ctx, definitionX, db.ExecuteOptions{})
- require.NoError(t, err)
-
- metadataB, err := driverB.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Compare
- normalizeMetadata(metadataA)
- normalizeMetadata(metadataB)
-
- opts := []cmp.Option{
- protocmp.Transform(),
- protocmp.IgnoreFields(&storepb.TableMetadata{}, "row_count", "data_size", "index_size", "data_free"),
- }
-
- if diff := cmp.Diff(metadataA, metadataB, opts...); diff != "" {
- t.Errorf("Metadata mismatch (-want +got):\n%s", diff)
- }
-}
diff --git a/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/mysql/get_database_metadata_omni_test.go
similarity index 100%
rename from backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
rename to backend/plugin/schema/mysql/get_database_metadata_omni_test.go
diff --git a/backend/plugin/schema/mysql/get_multi_file_definition_live_test.go b/backend/plugin/schema/mysql/get_multi_file_definition_live_test.go
deleted file mode 100644
index 403f7ee635..0000000000
--- a/backend/plugin/schema/mysql/get_multi_file_definition_live_test.go
+++ /dev/null
@@ -1,339 +0,0 @@
-package mysql
-
-// Live round-trip proof for the MySQL multi-file SDL export (GetMultiFileDatabaseDefinition).
-//
-// The correctness bar is the declarative round-trip: the exported multi-file set must be
-// re-importable via the declarative path, which CONCATENATES the files
-// (action/command/file.go: lexical sort of paths, joined with "\n"). So the proof loads a
-// real, rich schema (sakila: 16 tables, plain views, 3 functions, 3 procedures, 3 triggers)
-// into live MySQL, syncs it, generates the multi-file set, then asserts:
-//
-// concat(multi-file) ≡ single-file dump (mysqlDiffSDLMigration(concat, single) empty)
-// concat(multi-file) is idempotent (mysqlDiffSDLMigration(concat, concat) empty)
-//
-// proving the multi-file export is a faithful, re-importable representation identical to the
-// single-file dump. It also asserts the expected per-object files exist (correct dirs, valid
-// non-empty SDL) and that the API ZIP construction (mirroring database_service.getMultiFileSDL)
-// yields one entry per file.
-//
-// Shared helpers (liveServers, loadRealWorld, dumpSDL, statementCount, realWorldSchemas,
-// objectCounts) come from the sibling _test.go files in this package.
-
-import (
- "archive/zip"
- "bytes"
- "cmp"
- "context"
- "io"
- "slices"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "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"
-)
-
-// concatMultiFile reproduces the declarative rollout concatenation from
-// action/command/file.go: sort files by path lexically, then join contents with a single
-// "\n" separator between files. This is the exact byte stream the declarative re-import
-// path feeds back to the SDL loader.
-func concatMultiFile(result *schema.MultiFileSchemaResult) string {
- files := make([]schema.File, len(result.Files))
- copy(files, result.Files)
- slices.SortFunc(files, func(a, b schema.File) int { return cmp.Compare(a.Name, b.Name) })
- var b strings.Builder
- for i, f := range files {
- if i > 0 {
- b.WriteString("\n")
- }
- b.WriteString(f.Content)
- }
- return b.String()
-}
-
-// TestMultiFileSDLExportRoundTrip is the headline proof across the real-world corpus and
-// both live versions. sakila is the priority (rich views/functions/procedures/triggers) on
-// 8.0; roundcube / employees / employees_part / mediawiki carry the 5.7 leg (sakila's
-// actor_info INVOKER view does not CREATE on 5.7). For each loadable fixture × version:
-// generate the multi-file export, assert one valid non-empty file per synced object in the
-// correct flat dir, then prove the declarative round-trip — concat(files) ≡ single-file
-// dump AND concat(files) is idempotent.
-//
-//nolint:tparallel
-func TestMultiFileSDLExportRoundTrip(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, rw := range realWorldSchemas() {
- rw := rw
- t.Run(rw.name, func(t *testing.T) {
- if srv.version == "5.7" && rw.only57Skip != "" {
- t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, rw.name, rw.only57Skip)
- }
- if rw.viewParsePending != "" {
- t.Skipf("[%s/%s] SDL re-import pending: %s", srv.name, rw.name, rw.viewParsePending)
- }
-
- meta, dbName := loadRealWorld(ctx, t, srv, rw)
- tbl, vw, fn, pr, tg := objectCounts(meta)
- t.Logf("[%s/%s] loaded: tables=%d views=%d functions=%d procedures=%d triggers=%d (db=%s)",
- srv.name, rw.name, tbl, vw, fn, pr, tg, dbName)
-
- metaProto := meta.GetProto()
- require.NotNil(t, metaProto, "synced metadata proto must be non-nil")
-
- // --- Generate the multi-file export. ---
- result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
- SkipBackupSchema: true,
- }, metaProto)
- require.NoError(t, err)
- require.NotEmpty(t, result.Files, "[%s/%s] multi-file export produced no files", srv.name, rw.name)
-
- // --- Assert the expected per-object files: correct dirs, one per object,
- // valid non-empty SDL, unique names. ---
- byDir := map[string][]schema.File{}
- seen := map[string]bool{}
- for _, f := range result.Files {
- require.False(t, seen[f.Name], "[%s/%s] duplicate file name %q", srv.name, rw.name, f.Name)
- seen[f.Name] = true
- require.True(t, strings.HasSuffix(f.Name, ".sql"), "[%s/%s] file %q must end .sql", srv.name, rw.name, f.Name)
- require.NotEmpty(t, strings.TrimSpace(f.Content), "[%s/%s] file %q has empty content", srv.name, rw.name, f.Name)
- dir, _, ok := strings.Cut(f.Name, "/")
- require.True(t, ok, "[%s/%s] file %q must be under a directory", srv.name, rw.name, f.Name)
- byDir[dir] = append(byDir[dir], f)
-
- // Spot-check the leading CREATE keyword matches the directory.
- upper := strings.ToUpper(strings.TrimSpace(f.Content))
- switch dir {
- case "tables":
- require.True(t, strings.HasPrefix(upper, "CREATE TABLE "), "[%s/%s] %q: %.40s", srv.name, rw.name, f.Name, upper)
- case "views":
- require.Contains(t, upper, " VIEW `", "[%s/%s] %q must be a CREATE VIEW", srv.name, rw.name, f.Name)
- case "functions":
- require.Contains(t, upper, "FUNCTION", "[%s/%s] %q must be a CREATE FUNCTION", srv.name, rw.name, f.Name)
- case "procedures":
- require.Contains(t, upper, "PROCEDURE", "[%s/%s] %q must be a CREATE PROCEDURE", srv.name, rw.name, f.Name)
- case "triggers":
- require.True(t, strings.HasPrefix(upper, "CREATE TRIGGER "), "[%s/%s] %q", srv.name, rw.name, f.Name)
- case "events":
- require.Contains(t, upper, "EVENT", "[%s/%s] %q must be a CREATE EVENT", srv.name, rw.name, f.Name)
- default:
- t.Fatalf("[%s/%s] unexpected directory %q from file %q", srv.name, rw.name, dir, f.Name)
- }
- }
-
- // One file per synced object, per type.
- require.Len(t, byDir["tables"], tbl, "[%s/%s] table file count", srv.name, rw.name)
- require.Len(t, byDir["views"], vw, "[%s/%s] view file count", srv.name, rw.name)
- require.Len(t, byDir["functions"], fn, "[%s/%s] function file count", srv.name, rw.name)
- require.Len(t, byDir["procedures"], pr, "[%s/%s] procedure file count", srv.name, rw.name)
- require.Len(t, byDir["triggers"], tg, "[%s/%s] trigger file count", srv.name, rw.name)
-
- t.Logf("[%s/%s] multi-file layout: %d files (tables=%d views=%d functions=%d procedures=%d triggers=%d events=%d)",
- srv.name, rw.name, len(result.Files), len(byDir["tables"]), len(byDir["views"]),
- len(byDir["functions"]), len(byDir["procedures"]), len(byDir["triggers"]), len(byDir["events"]))
-
- // --- The round-trip proof. ---
- single := dumpSDL(ctx, t, srv, dbName)
- require.NotEmpty(t, single, "[%s/%s] single-file dump empty", srv.name, rw.name)
- concat := concatMultiFile(result)
- require.NotEmpty(t, concat, "[%s/%s] concatenated multi-file empty", srv.name, rw.name)
-
- // (a) concat ≡ single-file: diffing the concatenated multi-file export against
- // the single-file dump must be a no-op (both describe the identical schema).
- diffVsSingle, err := mysqlDiffSDLMigration(concat, single, srv.version)
- require.NoError(t, err)
- if diffVsSingle != "" {
- t.Logf("[%s/%s] NON-EMPTY concat-vs-single (%d stmts):\n%s",
- srv.name, rw.name, statementCount(diffVsSingle), diffVsSingle)
- }
- require.Empty(t, diffVsSingle, "[%s/%s] concat(multi-file) must equal single-file dump", srv.name, rw.name)
-
- // (b) concat is idempotent: re-importing the concatenated export against itself
- // is a no-op, proving it is a self-consistent, re-importable SDL document.
- selfDiff, err := mysqlDiffSDLMigration(concat, concat, srv.version)
- require.NoError(t, err)
- require.Empty(t, selfDiff, "[%s/%s] concat(multi-file) must be idempotent, got:\n%s", srv.name, rw.name, selfDiff)
- })
- }
- })
- }
-}
-
-// TestMultiFileSDLExportZip proves the API ZIP path: the MultiFileSchemaResult produces a
-// valid ZIP with exactly one entry per file whose bytes match the file content — mirroring
-// backend/api/v1/database_service.go getMultiFileSDL. Exercised on 8.0 (sakila's load target).
-//
-//nolint:tparallel
-func TestMultiFileSDLExportZip(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- var sakila realWorldSchema
- for _, rw := range realWorldSchemas() {
- if rw.name == "sakila" {
- sakila = rw
- }
- }
-
- srv := liveServers[0] // mysql80
- require.Equal(t, "8.0", srv.version)
-
- meta, _ := loadRealWorld(ctx, t, srv, sakila)
- metaProto := meta.GetProto()
- require.NotNil(t, metaProto)
-
- result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
- SkipBackupSchema: true,
- }, metaProto)
- require.NoError(t, err)
- require.NotEmpty(t, result.Files)
-
- // Build the ZIP exactly as getMultiFileSDL does.
- var buf bytes.Buffer
- zw := zip.NewWriter(&buf)
- for _, f := range result.Files {
- w, err := zw.Create(f.Name)
- require.NoError(t, err)
- _, err = w.Write([]byte(f.Content))
- require.NoError(t, err)
- }
- require.NoError(t, zw.Close())
-
- // Read it back and assert entry-for-entry fidelity.
- zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
- require.NoError(t, err)
- require.Len(t, zr.File, len(result.Files), "ZIP entry count must equal file count")
-
- want := map[string]string{}
- for _, f := range result.Files {
- want[f.Name] = f.Content
- }
- for _, entry := range zr.File {
- content, ok := want[entry.Name]
- require.True(t, ok, "unexpected ZIP entry %q", entry.Name)
- rc, err := entry.Open()
- require.NoError(t, err)
- got, err := io.ReadAll(rc)
- rc.Close()
- require.NoError(t, err)
- require.Equal(t, content, string(got), "ZIP entry %q content mismatch", entry.Name)
- }
- t.Logf("[%s/sakila] ZIP has %d entries, all matching", srv.name, len(zr.File))
-}
-
-// TestMultiFileSDLEventOrderingLive is the direct answer to the review claim that the
-// multi-file layout — which writes events/ (sorting alphabetically BEFORE tables/) — can
-// feed the SDL loader an event-before-table order that fails, because an event body may
-// reference a table. It builds a schema with a table AND an event whose body DELETEs from
-// that table, exports it multi-file, concatenates the files in the EXACT alphabetical path
-// order the declarative rollout uses (action/command/file.go: events/*.sql then
-// tables/*.sql), and proves the loader handles it: LoadSDL is dependency-aware and layers
-// CREATE EVENT last regardless of text order (omni sdl.go sdlPriority: event=6, after
-// table=1), and event bodies are parsed opaquely (no dependency edge, no forward-reference
-// error). So:
-//
-// (1) the concatenation really is event-before-table (the layout the claim describes);
-// (2) concat(multi-file) is idempotent and equals the single-file dump; and
-// (3) the realistic path — source=empty, target=that alphabetical concat -> generate plan
-// -> APPLY to a fresh live 8.0 database -> converges — succeeds.
-//
-//nolint:tparallel
-func TestMultiFileSDLEventOrderingLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
- srv := liveServers[0] // 8.0 — events are the relevant surface and the scheduler is on.
- require.Equal(t, "8.0", srv.version)
-
- // A table plus an event whose body references it. In the flat multi-file layout the
- // event lands in events/purge_audit.sql and the table in tables/audit_log.sql, so the
- // action's lexical path sort places the event's CREATE ahead of the table's.
- const ddl = `
-CREATE TABLE audit_log (
- id INT PRIMARY KEY AUTO_INCREMENT,
- msg VARCHAR(200) NOT NULL,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE EVENT purge_audit
- ON SCHEDULE EVERY 1 DAY
- DO DELETE FROM audit_log WHERE created_at < (NOW() - INTERVAL 30 DAY);
-`
-
- dbSrc := newLiveDatabase(ctx, t, srv, "sdl_evt_src")
- require.NoError(t, applyDDL(ctx, t, srv, dbSrc, ddl))
-
- driver, err := createLiveMySQLDriver(ctx, srv, dbSrc)
- require.NoError(t, err)
- defer driver.Close(ctx)
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- metaProto := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true).GetProto()
- require.NotNil(t, metaProto)
-
- result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
- SkipBackupSchema: true,
- }, metaProto)
- require.NoError(t, err)
- require.NotEmpty(t, result.Files)
-
- // (1) The event and table files exist, and — sorted lexically as the action does — the
- // event file really does sort before the table file. This is the ordering the claim is
- // about; the rest of the test proves the loader tolerates it.
- var eventPath, tablePath string
- for _, f := range result.Files {
- switch {
- case strings.HasPrefix(f.Name, "events/"):
- eventPath = f.Name
- case strings.HasPrefix(f.Name, "tables/"):
- tablePath = f.Name
- default:
- }
- }
- require.NotEmpty(t, eventPath, "expected an events/ file")
- require.NotEmpty(t, tablePath, "expected a tables/ file")
- require.Less(t, eventPath, tablePath, "the action sorts %q BEFORE %q (event before table)", eventPath, tablePath)
-
- // (2) concat in alphabetical path order (event body precedes its table in the text) is
- // both idempotent and equal to the single-file dump — the loader topo-sorts the event
- // after the table regardless of this text order.
- concat := concatMultiFile(result)
- require.NotEmpty(t, concat)
- require.Less(t, strings.Index(concat, "CREATE EVENT"), strings.Index(concat, "CREATE TABLE"),
- "concat must present the event body before the table (the order under test)")
-
- selfDiff, err := mysqlDiffSDLMigration(concat, concat, srv.version)
- require.NoError(t, err, "LoadSDL must accept the event-before-table concat without a forward-reference error")
- require.Empty(t, selfDiff, "concat(multi-file) with event-before-table must be idempotent")
-
- single := dumpSDL(ctx, t, srv, dbSrc)
- require.NotEmpty(t, single)
- diffVsSingle, err := mysqlDiffSDLMigration(concat, single, srv.version)
- require.NoError(t, err)
- require.Empty(t, diffVsSingle, "concat(multi-file) must equal the single-file dump")
-
- // (3) The realistic apply: source=empty, target=the alphabetical concat. Generate the
- // plan and APPLY it to a fresh live 8.0 database, then prove convergence (re-dump ==
- // single). If event-before-table ordering were a real problem this apply would fail.
- plan, err := mysqlDiffSDLMigration("", concat, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, plan, "empty -> target must produce a non-empty plan")
- require.Contains(t, plan, "CREATE TABLE", "plan must create the table")
- require.Contains(t, plan, "EVENT", "plan must create the event")
-
- dbDst := newLiveDatabase(ctx, t, srv, "sdl_evt_dst")
- require.NoError(t, applyDDL(ctx, t, srv, dbDst, plan), "applying the generated plan to live 8.0 must succeed")
-
- redump := dumpSDL(ctx, t, srv, dbDst)
- converge, err := mysqlDiffSDLMigration(redump, single, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "applied schema must converge with the source dump")
- t.Logf("[%s] event-before-table concat applied and converged; plan:\n%s", srv.name, plan)
-}
diff --git a/backend/plugin/schema/mysql/get_multi_file_definition_test.go b/backend/plugin/schema/mysql/get_multi_file_definition_test.go
index 3cd1d44ce4..52c966888f 100644
--- a/backend/plugin/schema/mysql/get_multi_file_definition_test.go
+++ b/backend/plugin/schema/mysql/get_multi_file_definition_test.go
@@ -1,15 +1,27 @@
package mysql
-// Unit tests for the multi-file SDL file-name sanitization + de-duplication. These need no
-// live database — they pin the safe-file-name behavior for MySQL identifiers that contain
-// path-unsafe or ambiguous characters.
-
import (
+ "archive/zip"
+ "bytes"
+ "cmp"
+ "context"
+ "io"
+ "slices"
+ "strings"
"testing"
+ _ "github.com/go-sql-driver/mysql"
"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"
)
+// Unit tests for the multi-file SDL file-name sanitization + de-duplication. These need no
+// live database — they pin the safe-file-name behavior for MySQL identifiers that contain
+// path-unsafe or ambiguous characters.
+
func TestSanitizeFileStem(t *testing.T) {
cases := []struct {
name string
@@ -85,3 +97,323 @@ func TestFileNameAllocatorReservesGeneratedSuffix(t *testing.T) {
require.Equal(t, "tables/t_1.sql", b.alloc("tables", "t/")) // sanitizes to "t", bumped to t_1
require.Equal(t, "tables/t_1_1.sql", b.alloc("tables", "t_1")) // real "t_1" cannot reuse the generated t_1
}
+
+// Live round-trip proof for the MySQL multi-file SDL export (GetMultiFileDatabaseDefinition).
+//
+// The correctness bar is the declarative round-trip: the exported multi-file set must be
+// re-importable via the declarative path, which CONCATENATES the files
+// (action/command/file.go: lexical sort of paths, joined with "\n"). So the proof loads a
+// real, rich schema (sakila: 16 tables, plain views, 3 functions, 3 procedures, 3 triggers)
+// into live MySQL, syncs it, generates the multi-file set, then asserts:
+//
+// concat(multi-file) ≡ single-file dump (mysqlDiffSDLMigration(concat, single) empty)
+// concat(multi-file) is idempotent (mysqlDiffSDLMigration(concat, concat) empty)
+//
+// proving the multi-file export is a faithful, re-importable representation identical to the
+// single-file dump. It also asserts the expected per-object files exist (correct dirs, valid
+// non-empty SDL) and that the API ZIP construction (mirroring database_service.getMultiFileSDL)
+// yields one entry per file.
+//
+// Shared helpers (liveServers, loadRealWorld, dumpSDL, statementCount, realWorldSchemas,
+// objectCounts) are declared in sdl_migration_omni_test.go.
+
+// concatMultiFile reproduces the declarative rollout concatenation from
+// action/command/file.go: sort files by path lexically, then join contents with a single
+// "\n" separator between files. This is the exact byte stream the declarative re-import
+// path feeds back to the SDL loader.
+func concatMultiFile(result *schema.MultiFileSchemaResult) string {
+ files := make([]schema.File, len(result.Files))
+ copy(files, result.Files)
+ slices.SortFunc(files, func(a, b schema.File) int { return cmp.Compare(a.Name, b.Name) })
+ var b strings.Builder
+ for i, f := range files {
+ if i > 0 {
+ b.WriteString("\n")
+ }
+ b.WriteString(f.Content)
+ }
+ return b.String()
+}
+
+// TestMultiFileSDLExportRoundTrip is the headline proof across the real-world corpus and
+// both live versions. sakila is the priority (rich views/functions/procedures/triggers) on
+// 8.0; roundcube / employees / employees_part / mediawiki carry the 5.7 leg (sakila's
+// actor_info INVOKER view does not CREATE on 5.7). For each loadable fixture × version:
+// generate the multi-file export, assert one valid non-empty file per synced object in the
+// correct flat dir, then prove the declarative round-trip — concat(files) ≡ single-file
+// dump AND concat(files) is idempotent.
+//
+//nolint:tparallel
+func TestMultiFileSDLExportRoundTrip(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, rw := range realWorldSchemas() {
+ rw := rw
+ t.Run(rw.name, func(t *testing.T) {
+ if srv.version == "5.7" && rw.only57Skip != "" {
+ t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, rw.name, rw.only57Skip)
+ }
+ if rw.viewParsePending != "" {
+ t.Skipf("[%s/%s] SDL re-import pending: %s", srv.name, rw.name, rw.viewParsePending)
+ }
+
+ meta, dbName := loadRealWorld(ctx, t, srv, rw)
+ tbl, vw, fn, pr, tg := objectCounts(meta)
+ t.Logf("[%s/%s] loaded: tables=%d views=%d functions=%d procedures=%d triggers=%d (db=%s)",
+ srv.name, rw.name, tbl, vw, fn, pr, tg, dbName)
+
+ metaProto := meta.GetProto()
+ require.NotNil(t, metaProto, "synced metadata proto must be non-nil")
+
+ // --- Generate the multi-file export. ---
+ result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
+ SkipBackupSchema: true,
+ }, metaProto)
+ require.NoError(t, err)
+ require.NotEmpty(t, result.Files, "[%s/%s] multi-file export produced no files", srv.name, rw.name)
+
+ // --- Assert the expected per-object files: correct dirs, one per object,
+ // valid non-empty SDL, unique names. ---
+ byDir := map[string][]schema.File{}
+ seen := map[string]bool{}
+ for _, f := range result.Files {
+ require.False(t, seen[f.Name], "[%s/%s] duplicate file name %q", srv.name, rw.name, f.Name)
+ seen[f.Name] = true
+ require.True(t, strings.HasSuffix(f.Name, ".sql"), "[%s/%s] file %q must end .sql", srv.name, rw.name, f.Name)
+ require.NotEmpty(t, strings.TrimSpace(f.Content), "[%s/%s] file %q has empty content", srv.name, rw.name, f.Name)
+ dir, _, ok := strings.Cut(f.Name, "/")
+ require.True(t, ok, "[%s/%s] file %q must be under a directory", srv.name, rw.name, f.Name)
+ byDir[dir] = append(byDir[dir], f)
+
+ // Spot-check the leading CREATE keyword matches the directory.
+ upper := strings.ToUpper(strings.TrimSpace(f.Content))
+ switch dir {
+ case "tables":
+ require.True(t, strings.HasPrefix(upper, "CREATE TABLE "), "[%s/%s] %q: %.40s", srv.name, rw.name, f.Name, upper)
+ case "views":
+ require.Contains(t, upper, " VIEW `", "[%s/%s] %q must be a CREATE VIEW", srv.name, rw.name, f.Name)
+ case "functions":
+ require.Contains(t, upper, "FUNCTION", "[%s/%s] %q must be a CREATE FUNCTION", srv.name, rw.name, f.Name)
+ case "procedures":
+ require.Contains(t, upper, "PROCEDURE", "[%s/%s] %q must be a CREATE PROCEDURE", srv.name, rw.name, f.Name)
+ case "triggers":
+ require.True(t, strings.HasPrefix(upper, "CREATE TRIGGER "), "[%s/%s] %q", srv.name, rw.name, f.Name)
+ case "events":
+ require.Contains(t, upper, "EVENT", "[%s/%s] %q must be a CREATE EVENT", srv.name, rw.name, f.Name)
+ default:
+ t.Fatalf("[%s/%s] unexpected directory %q from file %q", srv.name, rw.name, dir, f.Name)
+ }
+ }
+
+ // One file per synced object, per type.
+ require.Len(t, byDir["tables"], tbl, "[%s/%s] table file count", srv.name, rw.name)
+ require.Len(t, byDir["views"], vw, "[%s/%s] view file count", srv.name, rw.name)
+ require.Len(t, byDir["functions"], fn, "[%s/%s] function file count", srv.name, rw.name)
+ require.Len(t, byDir["procedures"], pr, "[%s/%s] procedure file count", srv.name, rw.name)
+ require.Len(t, byDir["triggers"], tg, "[%s/%s] trigger file count", srv.name, rw.name)
+
+ t.Logf("[%s/%s] multi-file layout: %d files (tables=%d views=%d functions=%d procedures=%d triggers=%d events=%d)",
+ srv.name, rw.name, len(result.Files), len(byDir["tables"]), len(byDir["views"]),
+ len(byDir["functions"]), len(byDir["procedures"]), len(byDir["triggers"]), len(byDir["events"]))
+
+ // --- The round-trip proof. ---
+ single := dumpSDL(ctx, t, srv, dbName)
+ require.NotEmpty(t, single, "[%s/%s] single-file dump empty", srv.name, rw.name)
+ concat := concatMultiFile(result)
+ require.NotEmpty(t, concat, "[%s/%s] concatenated multi-file empty", srv.name, rw.name)
+
+ // (a) concat ≡ single-file: diffing the concatenated multi-file export against
+ // the single-file dump must be a no-op (both describe the identical schema).
+ diffVsSingle, err := mysqlDiffSDLMigration(concat, single, srv.version)
+ require.NoError(t, err)
+ if diffVsSingle != "" {
+ t.Logf("[%s/%s] NON-EMPTY concat-vs-single (%d stmts):\n%s",
+ srv.name, rw.name, statementCount(diffVsSingle), diffVsSingle)
+ }
+ require.Empty(t, diffVsSingle, "[%s/%s] concat(multi-file) must equal single-file dump", srv.name, rw.name)
+
+ // (b) concat is idempotent: re-importing the concatenated export against itself
+ // is a no-op, proving it is a self-consistent, re-importable SDL document.
+ selfDiff, err := mysqlDiffSDLMigration(concat, concat, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, selfDiff, "[%s/%s] concat(multi-file) must be idempotent, got:\n%s", srv.name, rw.name, selfDiff)
+ })
+ }
+ })
+ }
+}
+
+// TestMultiFileSDLExportZip proves the API ZIP path: the MultiFileSchemaResult produces a
+// valid ZIP with exactly one entry per file whose bytes match the file content — mirroring
+// backend/api/v1/database_service.go getMultiFileSDL. Exercised on 8.0 (sakila's load target).
+//
+//nolint:tparallel
+func TestMultiFileSDLExportZip(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ var sakila realWorldSchema
+ for _, rw := range realWorldSchemas() {
+ if rw.name == "sakila" {
+ sakila = rw
+ }
+ }
+
+ srv := liveServers[0] // mysql80
+ require.Equal(t, "8.0", srv.version)
+
+ meta, _ := loadRealWorld(ctx, t, srv, sakila)
+ metaProto := meta.GetProto()
+ require.NotNil(t, metaProto)
+
+ result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
+ SkipBackupSchema: true,
+ }, metaProto)
+ require.NoError(t, err)
+ require.NotEmpty(t, result.Files)
+
+ // Build the ZIP exactly as getMultiFileSDL does.
+ var buf bytes.Buffer
+ zw := zip.NewWriter(&buf)
+ for _, f := range result.Files {
+ w, err := zw.Create(f.Name)
+ require.NoError(t, err)
+ _, err = w.Write([]byte(f.Content))
+ require.NoError(t, err)
+ }
+ require.NoError(t, zw.Close())
+
+ // Read it back and assert entry-for-entry fidelity.
+ zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
+ require.NoError(t, err)
+ require.Len(t, zr.File, len(result.Files), "ZIP entry count must equal file count")
+
+ want := map[string]string{}
+ for _, f := range result.Files {
+ want[f.Name] = f.Content
+ }
+ for _, entry := range zr.File {
+ content, ok := want[entry.Name]
+ require.True(t, ok, "unexpected ZIP entry %q", entry.Name)
+ rc, err := entry.Open()
+ require.NoError(t, err)
+ got, err := io.ReadAll(rc)
+ rc.Close()
+ require.NoError(t, err)
+ require.Equal(t, content, string(got), "ZIP entry %q content mismatch", entry.Name)
+ }
+ t.Logf("[%s/sakila] ZIP has %d entries, all matching", srv.name, len(zr.File))
+}
+
+// TestMultiFileSDLEventOrderingLive is the direct answer to the review claim that the
+// multi-file layout — which writes events/ (sorting alphabetically BEFORE tables/) — can
+// feed the SDL loader an event-before-table order that fails, because an event body may
+// reference a table. It builds a schema with a table AND an event whose body DELETEs from
+// that table, exports it multi-file, concatenates the files in the EXACT alphabetical path
+// order the declarative rollout uses (action/command/file.go: events/*.sql then
+// tables/*.sql), and proves the loader handles it: LoadSDL is dependency-aware and layers
+// CREATE EVENT last regardless of text order (omni sdl.go sdlPriority: event=6, after
+// table=1), and event bodies are parsed opaquely (no dependency edge, no forward-reference
+// error). So:
+//
+// (1) the concatenation really is event-before-table (the layout the claim describes);
+// (2) concat(multi-file) is idempotent and equals the single-file dump; and
+// (3) the realistic path — source=empty, target=that alphabetical concat -> generate plan
+// -> APPLY to a fresh live 8.0 database -> converges — succeeds.
+//
+//nolint:tparallel
+func TestMultiFileSDLEventOrderingLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+ srv := liveServers[0] // 8.0 — events are the relevant surface and the scheduler is on.
+ require.Equal(t, "8.0", srv.version)
+
+ // A table plus an event whose body references it. In the flat multi-file layout the
+ // event lands in events/purge_audit.sql and the table in tables/audit_log.sql, so the
+ // action's lexical path sort places the event's CREATE ahead of the table's.
+ const ddl = `
+CREATE TABLE audit_log (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ msg VARCHAR(200) NOT NULL,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE EVENT purge_audit
+ ON SCHEDULE EVERY 1 DAY
+ DO DELETE FROM audit_log WHERE created_at < (NOW() - INTERVAL 30 DAY);
+`
+
+ dbSrc := newLiveDatabase(ctx, t, srv, "sdl_evt_src")
+ require.NoError(t, applyDDL(ctx, t, srv, dbSrc, ddl))
+
+ driver, err := createLiveMySQLDriver(ctx, srv, dbSrc)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ metaProto := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true).GetProto()
+ require.NotNil(t, metaProto)
+
+ result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
+ SkipBackupSchema: true,
+ }, metaProto)
+ require.NoError(t, err)
+ require.NotEmpty(t, result.Files)
+
+ // (1) The event and table files exist, and — sorted lexically as the action does — the
+ // event file really does sort before the table file. This is the ordering the claim is
+ // about; the rest of the test proves the loader tolerates it.
+ var eventPath, tablePath string
+ for _, f := range result.Files {
+ switch {
+ case strings.HasPrefix(f.Name, "events/"):
+ eventPath = f.Name
+ case strings.HasPrefix(f.Name, "tables/"):
+ tablePath = f.Name
+ default:
+ }
+ }
+ require.NotEmpty(t, eventPath, "expected an events/ file")
+ require.NotEmpty(t, tablePath, "expected a tables/ file")
+ require.Less(t, eventPath, tablePath, "the action sorts %q BEFORE %q (event before table)", eventPath, tablePath)
+
+ // (2) concat in alphabetical path order (event body precedes its table in the text) is
+ // both idempotent and equal to the single-file dump — the loader topo-sorts the event
+ // after the table regardless of this text order.
+ concat := concatMultiFile(result)
+ require.NotEmpty(t, concat)
+ require.Less(t, strings.Index(concat, "CREATE EVENT"), strings.Index(concat, "CREATE TABLE"),
+ "concat must present the event body before the table (the order under test)")
+
+ selfDiff, err := mysqlDiffSDLMigration(concat, concat, srv.version)
+ require.NoError(t, err, "LoadSDL must accept the event-before-table concat without a forward-reference error")
+ require.Empty(t, selfDiff, "concat(multi-file) with event-before-table must be idempotent")
+
+ single := dumpSDL(ctx, t, srv, dbSrc)
+ require.NotEmpty(t, single)
+ diffVsSingle, err := mysqlDiffSDLMigration(concat, single, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, diffVsSingle, "concat(multi-file) must equal the single-file dump")
+
+ // (3) The realistic apply: source=empty, target=the alphabetical concat. Generate the
+ // plan and APPLY it to a fresh live 8.0 database, then prove convergence (re-dump ==
+ // single). If event-before-table ordering were a real problem this apply would fail.
+ plan, err := mysqlDiffSDLMigration("", concat, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, plan, "empty -> target must produce a non-empty plan")
+ require.Contains(t, plan, "CREATE TABLE", "plan must create the table")
+ require.Contains(t, plan, "EVENT", "plan must create the event")
+
+ dbDst := newLiveDatabase(ctx, t, srv, "sdl_evt_dst")
+ require.NoError(t, applyDDL(ctx, t, srv, dbDst, plan), "applying the generated plan to live 8.0 must succeed")
+
+ redump := dumpSDL(ctx, t, srv, dbDst)
+ converge, err := mysqlDiffSDLMigration(redump, single, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "applied schema must converge with the source dump")
+ t.Logf("[%s] event-before-table concat applied and converged; plan:\n%s", srv.name, plan)
+}
diff --git a/backend/plugin/schema/mysql/partition_diff_test.go b/backend/plugin/schema/mysql/partition_diff_test.go
deleted file mode 100644
index 81de3ee104..0000000000
--- a/backend/plugin/schema/mysql/partition_diff_test.go
+++ /dev/null
@@ -1,179 +0,0 @@
-package mysql
-
-import (
- "fmt"
- "strings"
- "testing"
-
- "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"
-)
-
-// partitionedTable is the shape reported in BYT-10057: a daily BI table with a composite
-// primary key, a secondary index over the same ordered columns, and one range partition
-// per day. Each partition used to add another copy of the table to the diff, so the
-// generated script repeated the whole DDL block once per partition and died on the second
-// copy with "Duplicate key name" (1061) or "Multiple primary key defined" (1068).
-func partitionedTable(partitionCount int) *storepb.TableMetadata {
- table := &storepb.TableMetadata{
- Name: "ads_member_asset_by_account_channel_d",
- Engine: "InnoDB",
- Charset: "utf8mb4",
- Collation: "utf8mb4_general_ci",
- Columns: []*storepb.ColumnMetadata{
- {Name: "dt", Type: "date", Nullable: false},
- {Name: "member_id", Type: "bigint", Nullable: false},
- {Name: "account_type", Type: "varchar(50)", Nullable: false},
- },
- Indexes: []*storepb.IndexMetadata{
- {Name: "PRIMARY", Expressions: []string{"dt", "member_id", "account_type"}, Primary: true, Unique: true, Visible: true, Type: "BTREE"},
- {Name: "sindex", Expressions: []string{"dt", "member_id", "account_type"}, Visible: true, Type: "BTREE"},
- },
- }
- for i := range partitionCount {
- table.Partitions = append(table.Partitions, &storepb.TablePartitionMetadata{
- Name: fmt.Sprintf("p%d", i),
- Type: storepb.TablePartitionMetadata_RANGE,
- Expression: "to_days(`dt`)",
- Value: fmt.Sprintf("%d", 739000+i),
- })
- }
- return table
-}
-
-func databaseOf(tables ...*storepb.TableMetadata) *model.DatabaseMetadata {
- return model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Name: "db",
- Schemas: []*storepb.SchemaMetadata{{Name: "", Tables: tables}},
- }, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)
-}
-
-func TestDiffMigrationPartitionedTableEmittedOnce(t *testing.T) {
- for _, partitionCount := range []int{0, 1, 3, 180} {
- t.Run(fmt.Sprintf("partitions=%d", partitionCount), func(t *testing.T) {
- a := require.New(t)
- source := partitionedTable(partitionCount)
-
- // Target does not have the table yet: the 1061 shape.
- migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
- a.NoError(err)
- a.Equal(1, strings.Count(migration, "CREATE TABLE"), "CREATE TABLE must be emitted once:\n%s", migration)
- a.Equal(1, strings.Count(migration, "CREATE INDEX `sindex`"), "sindex must be emitted once:\n%s", migration)
-
- // Target has the table but neither the primary key nor the index: the 1068 shape.
- bare := &storepb.TableMetadata{
- Name: source.Name,
- Engine: source.Engine,
- Charset: source.Charset,
- Collation: source.Collation,
- Columns: source.Columns,
- }
- migration, err = schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(bare), databaseOf(source))
- a.NoError(err)
- a.Equal(1, strings.Count(migration, "ADD PRIMARY KEY"), "primary key must be added once:\n%s", migration)
- a.Equal(1, strings.Count(migration, "CREATE INDEX `sindex`"), "sindex must be emitted once:\n%s", migration)
-
- // Diffing the table against itself stays convergent.
- migration, err = schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(source), databaseOf(partitionedTable(partitionCount)))
- a.NoError(err)
- a.Empty(migration)
- })
- }
-}
-
-func TestDiffMigrationPreservesPartitioning(t *testing.T) {
- a := require.New(t)
- source := partitionedTable(3)
-
- migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
- a.NoError(err)
- a.Contains(migration, "PARTITION BY RANGE (to_days(`dt`))", "created table must keep its partitioning:\n%s", migration)
- for _, partition := range source.Partitions {
- a.Contains(migration, "PARTITION `"+partition.Name+"`", "partition %s must be created", partition.Name)
- }
-}
-
-// MySQL rejects a partitioned table whose partitions declare a different storage engine
-// than the table itself: "ERROR 1497 (HY000): The mix of handlers in the partitions is
-// not allowed in this version of MySQL". Partitioning a non-InnoDB table is still legal
-// on MySQL 5.7, so the partition clause has to carry the table's own engine.
-func TestDiffMigrationPartitionEngineMatchesTable(t *testing.T) {
- for _, engine := range []string{"InnoDB", "MyISAM", "ARCHIVE"} {
- t.Run(engine, func(t *testing.T) {
- a := require.New(t)
- source := partitionedTable(2)
- source.Engine = engine
-
- migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
- a.NoError(err)
- a.Equal(len(source.Partitions), strings.Count(migration, "PARTITION `p"),
- "every partition must be emitted:\n%s", migration)
- a.Equal(len(source.Partitions)+1, strings.Count(migration, "ENGINE="+engine),
- "the table and every partition must declare the same engine:\n%s", migration)
- })
- }
-}
-
-// Partition names are identifiers, so a reserved word is legal when quoted at creation and
-// comes back from information_schema unquoted. MySQL's own SHOW CREATE TABLE quotes them,
-// and the regenerated DDL has to as well or it is a syntax error.
-func TestDiffMigrationQuotesPartitionNames(t *testing.T) {
- a := require.New(t)
- source := partitionedTable(0)
- source.Partitions = []*storepb.TablePartitionMetadata{
- {Name: "select", Type: storepb.TablePartitionMetadata_RANGE, Expression: "to_days(`dt`)", Value: "739000"},
- {Name: "order", Type: storepb.TablePartitionMetadata_RANGE, Expression: "to_days(`dt`)", Value: "MAXVALUE"},
- }
-
- migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))
- a.NoError(err)
- a.Contains(migration, "PARTITION `select`", "reserved-word partition name must be quoted:\n%s", migration)
- a.Contains(migration, "PARTITION `order`", "reserved-word partition name must be quoted:\n%s", migration)
-}
-
-// tableAction returns the action the diff recorded for one table, or "" if absent.
-func tableAction(diff *schema.MetadataDiff, name string) schema.MetadataDiffAction {
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.TableName == name {
- return tableDiff.Action
- }
- }
- return ""
-}
-
-// The differ decides whether a table exists in the other snapshot by name. A partition of
-// some other table may carry that name, and resolving it as if it were the table makes the
-// differ miss a drop and mistake a create for a modification of the partition's owner.
-func TestDiffMigrationPartitionAliasDoesNotMaskTableExistence(t *testing.T) {
- archive := &storepb.TableMetadata{
- Name: "archive", Engine: "InnoDB",
- Columns: []*storepb.ColumnMetadata{{Name: "archive_col", Type: "int"}},
- }
- events := func(partitions ...*storepb.TablePartitionMetadata) *storepb.TableMetadata {
- return &storepb.TableMetadata{
- Name: "events", Engine: "InnoDB",
- Columns: []*storepb.ColumnMetadata{{Name: "events_col", Type: "int"}},
- Partitions: partitions,
- }
- }
- alias := &storepb.TablePartitionMetadata{Name: "archive", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"}
-
- t.Run("drop", func(t *testing.T) {
- a := require.New(t)
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MYSQL,
- databaseOf(archive, events()), databaseOf(events(alias)))
- a.NoError(err)
- a.Equal(schema.MetadataDiffActionDrop, tableAction(diff, "archive"))
- })
-
- t.Run("create", func(t *testing.T) {
- a := require.New(t)
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MYSQL,
- databaseOf(events(alias)), databaseOf(archive, events()))
- a.NoError(err)
- a.Equal(schema.MetadataDiffActionCreate, tableAction(diff, "archive"))
- })
-}
diff --git a/backend/plugin/schema/mysql/sdl_deep_stress_test.go b/backend/plugin/schema/mysql/sdl_deep_stress_test.go
deleted file mode 100644
index a795891c64..0000000000
--- a/backend/plugin/schema/mysql/sdl_deep_stress_test.go
+++ /dev/null
@@ -1,1255 +0,0 @@
-package mysql
-
-// Deep (round-2) stress test for the MySQL declarative (SDL) migration path. Round 1
-// (sdl_stress_live_test.go) covered breadth/scale/normalization. This file pushes four
-// DIFFERENT axes, all adversarial — the goal is to FIND bugs, not to confirm green:
-//
-// - Axis 1: malformed / semantic-error TARGET SDL fed to mysqlDiffSDLMigration and
-// schema.SDLMigration. Must fail GRACEFULLY (clean error or defined no-op) — never panic,
-// never silently emit a wrong/destructive plan.
-// - Axis 2: exotic types + extreme values. sync -> MetadataToSDL -> diff vs user form must
-// be empty (idempotent) on both versions; every non-empty no-op is a normalization gap.
-// - Axis 3: sequential release chains S0->S1->...->S4 on a real DB; each step minimal,
-// applied, re-synced, converged before the next.
-// - Axis 4: ~80-100 table scale; no-op must be empty + fast; one small change must be one
-// minimal ALTER, not a re-emit of all tables.
-//
-// Drives the PRODUCTION entry points only:
-// - schema.SDLMigration(MYSQL, userSDL, syncedMetadata, version),
-// - mysqlDiffSDLMigration(source, target, version),
-// - schema.SDLDropAdvices(MYSQL, userSDL, syncedMetadata, version),
-// - schema.MetadataToSDL.
-//
-// Shared helpers (createLiveMySQLDriver, newLiveDatabase, applyAndDump, statementCount,
-// liveServers, liveServer, syncMetadata, dumpSDL, applyDDL) come from the other two live
-// test files in this package.
-
-import (
- "context"
- "fmt"
- "strings"
- "testing"
- "time"
-
- _ "github.com/go-sql-driver/mysql"
- "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"
-)
-
-// ============================================================================
-// Axis 1: adversarial / malformed / semantic-error TARGET SDL.
-// ============================================================================
-//
-// Robustness contract: feeding bad target SDL into the diff must produce a CLEAN Go error
-// (or a defined no-op) — NEVER a panic, NEVER a malformed/destructive plan. The diff path
-// returns errors as Go values; a panic is therefore a (B) bug regardless of message.
-
-// diffOutcome captures how a single adversarial input was handled.
-type diffOutcome struct {
- panicked bool
- panicVal any
- err error
- plan string
- planEmpty bool
-}
-
-// safeDiff runs mysqlDiffSDLMigration(source, target, version) under a panic
-// recover so a panic is reported as data instead of crashing the test binary. A baseline
-// non-empty `source` is supplied so that if the target parses as EMPTY, the resulting plan
-// would be a (destructive) full-drop — letting us detect "silently dropped everything".
-func safeDiff(source, target, version string) (out diffOutcome) {
- defer func() {
- if r := recover(); r != nil {
- out.panicked = true
- out.panicVal = r
- }
- }()
- plan, err := mysqlDiffSDLMigration(source, target, version)
- out.err = err
- out.plan = plan
- out.planEmpty = strings.TrimSpace(plan) == ""
- return out
-}
-
-// safeSDLMigration runs the production schema.SDLMigration under a panic recover.
-func safeSDLMigration(target string, meta *model.DatabaseMetadata, version string) (out diffOutcome) {
- defer func() {
- if r := recover(); r != nil {
- out.panicked = true
- out.panicVal = r
- }
- }()
- plan, err := schema.SDLMigration(storepb.Engine_MYSQL, target, meta, version)
- out.err = err
- out.plan = plan
- out.planEmpty = strings.TrimSpace(plan) == ""
- return out
-}
-
-// adversarialCase is one bad-input probe.
-//
-// The UNIVERSAL bar for every case is: no panic, and no error-free plan that would
-// DESTROY a valid pre-existing object that the (correctly-interpreted) target still
-// contains. Beyond that, cases differ in how strict we can be about rejection:
-//
-// - wantReject=true: the input is malformed enough that a robust loader SHOULD return a
-// non-nil error. We assert err != nil. (If a future omni accepts it, this flips to a
-// soft finding rather than a crash — see the test body.)
-// - wantReject=false: the input is degenerate-but-arguably-valid, or its rejection is the
-// server's job at apply time (e.g. over-length identifier, dangling FK). We only assert
-// no-panic + no-unexpected-destruction and LOG the outcome.
-type adversarialCase struct {
- name string
- target string
- wantReject bool // require a non-nil error from the loader
- note string
-}
-
-// adversarialCases enumerates malformed and semantically-broken target SDL. The `source`
-// is a fixed valid 2-table schema so any plan the engine produces against a broken target
-// is observable (and a full-table-drop plan is detectable).
-func adversarialCases() []adversarialCase {
- return []adversarialCase{
- // ---- Syntax errors (must be rejected) ----
- {name: "truncated_create", target: "CREATE TABLE t (id INT", wantReject: true, note: "unterminated CREATE"},
- {name: "unmatched_paren", target: "CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50) ENGINE=InnoDB;", wantReject: true, note: "missing close paren"},
- {name: "garbage_tokens", target: "@@@ this is not sql ;;; %%%", wantReject: true, note: "pure garbage"},
- {name: "keyword_salad", target: "CREATE CREATE TABLE TABLE t t (id id INT INT);", wantReject: true, note: "doubled keywords"},
- {name: "missing_type", target: "CREATE TABLE t (id, name VARCHAR(50));", wantReject: true, note: "column with no type"},
-
- // ---- Semantic errors ----
- {
- name: "fk_nonexistent_table",
- target: "CREATE TABLE child (id INT PRIMARY KEY, pid INT, CONSTRAINT fk FOREIGN KEY (pid) REFERENCES no_such_parent (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false, // omni accepts a dangling FK at load; the server rejects on apply. No-panic + non-destructive is the bar.
- note: "FK to a table that does not exist",
- },
- {
- name: "fk_nonexistent_column",
- target: "CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\nCREATE TABLE child (id INT PRIMARY KEY, pid INT, CONSTRAINT fk FOREIGN KEY (pid) REFERENCES parent (no_such_col)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false,
- note: "FK references a column that does not exist on parent",
- },
- {
- name: "duplicate_table",
- target: "CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\nCREATE TABLE t (id INT PRIMARY KEY, x INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: true,
- note: "two CREATE TABLE with the same name",
- },
- {
- name: "duplicate_column",
- target: "CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50), name VARCHAR(60)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: true,
- note: "duplicate column name within a table",
- },
- {
- name: "duplicate_index_name",
- target: "CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT, KEY idx_dup (a), KEY idx_dup (b)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: true,
- note: "two indexes with the same name",
- },
- {
- name: "pk_declared_twice",
- target: "CREATE TABLE t (id INT, x INT, PRIMARY KEY (id), PRIMARY KEY (x)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: true,
- note: "two PRIMARY KEY clauses",
- },
- {
- name: "index_nonexistent_column",
- target: "CREATE TABLE t (id INT PRIMARY KEY, a INT, KEY idx_ghost (no_such_col)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false, // omni does NOT validate index-column existence at load; server rejects on apply (errno 1072). Soft finding.
- note: "index on a column that does not exist",
- },
- {
- name: "check_references_other_table",
- target: "CREATE TABLE t (id INT PRIMARY KEY, a INT, CONSTRAINT ck CHECK (a < (SELECT COUNT(*) FROM other))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: true, // 8.0 rejects a subquery in CHECK at load
- note: "CHECK with a subquery referencing another table",
- },
- {
- name: "generated_col_forward_ref",
- target: "CREATE TABLE t (id INT PRIMARY KEY, g INT AS (later + 1) STORED, later INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false, // forward reference; MySQL allows it. No-panic + sane plan is the bar.
- note: "generated column referencing a column declared later",
- },
- {
- name: "circular_fk_notnull",
- target: "CREATE TABLE a (id INT PRIMARY KEY, b_id INT NOT NULL, CONSTRAINT fk_a FOREIGN KEY (b_id) REFERENCES b (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\nCREATE TABLE b (id INT PRIMARY KEY, a_id INT NOT NULL, CONSTRAINT fk_b FOREIGN KEY (a_id) REFERENCES a (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false, // SDL loader disables FK checks; a clean plan or error both acceptable, no panic is the bar
- note: "mutually-referential NOT NULL FKs",
- },
-
- // ---- Degenerate ----
- {name: "empty_string", target: "", wantReject: false, note: "empty SDL -> full drop of source; must not panic"},
- {name: "whitespace_only", target: " \n\t \n", wantReject: false, note: "whitespace only"},
- {name: "comments_only", target: "-- just a comment\n/* block comment */\n", wantReject: false, note: "comments only"},
- {name: "single_semicolon", target: ";", wantReject: false, note: "a lone statement terminator"},
- {name: "create_sequence", target: "CREATE SEQUENCE s START WITH 1 INCREMENT BY 1;", wantReject: true, note: "different-engine DDL (no SEQUENCE in MySQL)"},
- {name: "dml_insert", target: "INSERT INTO t (id) VALUES (1);", wantReject: false, note: "DML in SDL target (LoadSQL fallback may accept and yield a plan)"},
- {name: "dml_update", target: "UPDATE t SET id = 2;", wantReject: false, note: "UPDATE in SDL target"},
- {name: "drop_table_in_target", target: "DROP TABLE t;", wantReject: true, note: "DROP in an SDL target (SDL is CREATE-only -> rejected)"},
- {name: "alter_in_target", target: "ALTER TABLE t ADD COLUMN x INT;", wantReject: true, note: "ALTER in an SDL target (rejected)"},
-
- // ---- Identifier edges ----
- {
- name: "ident_64_chars",
- target: "CREATE TABLE `" + strings.Repeat("a", 64) + "` (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false, // 64 is the MAX legal length -> accepted, no panic
- note: "64-char identifier (legal max)",
- },
- {
- name: "ident_65_chars",
- target: "CREATE TABLE `" + strings.Repeat("a", 65) + "` (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false, // omni does NOT enforce the 64-char limit at load; the server rejects on apply (errno 1059). Soft finding.
- note: "65-char identifier (over the limit)",
- },
- {
- name: "reserved_word_quoted",
- target: "CREATE TABLE `select` (`from` INT PRIMARY KEY, `where` VARCHAR(50)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false,
- note: "reserved words as backtick-quoted identifiers",
- },
- {
- name: "unicode_emoji_ident",
- target: "CREATE TABLE `ta*b📊le` (`col😀` INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false,
- note: "unicode/emoji identifiers",
- },
- {
- name: "ident_with_backtick_dot",
- target: "CREATE TABLE `we``ird` (`a.b` INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: false,
- note: "identifiers needing escaping (embedded backtick, dot)",
- },
- {
- name: "case_only_differ",
- target: "CREATE TABLE t (id INT PRIMARY KEY, Name VARCHAR(50), name VARCHAR(60)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- wantReject: true, // MySQL column names are case-insensitive -> Name/name collide
- note: "columns differing only by case",
- },
- }
-}
-
-// TestSDLDeepAdversarial drives malformed/semantic-error target SDL through the production
-// diff. Bar: NEVER panic, NEVER silently emit a destructive plan. We feed a fixed valid
-// 2-table `source` so a wrong plan that drops everything is observable.
-//
-//nolint:tparallel
-func TestSDLDeepAdversarial(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- // A fixed valid source schema (two real tables) so the diff has something to operate on.
- const sourceSDL = `CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE other (id INT PRIMARY KEY, t_id INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- // Materialize the source schema once so the production-path probe has a real synced
- // metadata to diff against (schema.SDLMigration path).
- meta, _ := syncMetadata(ctx, t, srv, "sdl_deep_adv", sourceSDL)
-
- for _, ac := range adversarialCases() {
- ac := ac
- t.Run(ac.name, func(t *testing.T) {
- // (1) Lower-level version-aware diff: source(valid) -> target(bad).
- out := safeDiff(sourceSDL, ac.target, srv.version)
-
- // UNIVERSAL bar 1: never panic. A panic on ANY input is a (B) bug.
- require.False(t, out.panicked,
- "[%s/%s] (B) PANIC on adversarial input %q: %v\ninput:\n%s",
- srv.name, ac.name, ac.note, out.panicVal, ac.target)
-
- // (2) Production entry: schema.SDLMigration(meta, badTarget). Same no-panic bar
- // on the real production call (covers the MetadataToSDL->diff composition).
- // Run for EVERY case, including the rejected ones.
- pout := safeSDLMigration(ac.target, meta, srv.version)
- require.False(t, pout.panicked,
- "[%s/%s] (B) PANIC in schema.SDLMigration on %q: %v\ninput:\n%s",
- srv.name, ac.name, ac.note, pout.panicVal, ac.target)
-
- if ac.wantReject {
- // The loader SHOULD reject this. If it does (err != nil), great. If it
- // does NOT, that is a robustness gap, but it is only a (B) bug if the
- // resulting error-free plan is also DESTRUCTIVE/wrong; a malformed input
- // that yields a clean error OR a harmless no-op is acceptable. We assert
- // the error and, on the rare accept, demand the plan at least not be
- // silently emitted as a non-empty migration.
- if out.err == nil {
- require.True(t, out.planEmpty,
- "[%s/%s] (B) malformed input %q was ACCEPTED (no error) and produced a non-empty plan — a wrong/garbage migration:\n%s",
- srv.name, ac.name, ac.note, out.plan)
- t.Logf("[%s/%s] SOFT: malformed input %q accepted as empty no-op rather than erroring", srv.name, ac.name, ac.note)
- } else {
- t.Logf("[%s/%s] reject OK: %v", srv.name, ac.name, out.err)
- }
- return
- }
-
- // wantReject==false: the input is degenerate-but-tolerable or its rejection
- // is the server's job at apply time. No-panic already asserted. A clean error
- // is fine; an error-free plan is fine (the declarative semantics of the target
- // are well-defined). Just record the outcome for the report.
- if out.err != nil {
- t.Logf("[%s/%s] tolerated-with-error: %v", srv.name, ac.name, out.err)
- } else {
- t.Logf("[%s/%s] tolerated input %q -> plan(empty=%v):\n%s",
- srv.name, ac.name, ac.note, out.planEmpty, out.plan)
- }
- })
- }
- })
- }
-}
-
-// ============================================================================
-// Axis 2: exotic types + extreme values (idempotence; flag normalization gaps).
-// ============================================================================
-//
-// Each case authors ONE less-common type-surface table. It is applied to a live DB, synced,
-// dumped via MetadataToSDL, and the PRODUCTION version-aware diff of (dumped vs the user
-// form) MUST be empty on each version it runs on. A non-empty no-op is a normalization gap
-// (a (B) bug) and the test logs input + stored/dumped form so the owning omni Canonical* /
-// dumper rule can be located. 8.0-only constructs are gated off 5.7 via only80.
-
-// exoticCase authors one exotic-type table. only80 gates 8.0-only surface off 5.7.
-//
-// bGap marks a case that this round CONFIRMED is NOT idempotent — a (B) normalization /
-// dumper round-trip bug. Those cases are SKIPPED by the idempotence test (so the suite stays
-// green) but kept here as the executable repro, and are reproduced/explained by
-// TestSDLDeepExoticKnownGaps below. bGapReason is the one-line root cause.
-type exoticCase struct {
- name string
- ddl string
- only80 bool
- bGap bool
- bGapReason string
-}
-
-// exoticCases enumerates the less-common type surface across families. Each names exactly
-// one construct cluster so a single non-empty no-op (or load error) pinpoints the offending
-// type. Cases marked bGap are confirmed (B) bugs this round found — see bGapReason and the
-// detailed analysis on TestSDLDeepExoticKnownGaps.
-func exoticCases() []exoticCase {
- return []exoticCase{
- // ---- JSON ----
- {
- name: "json_basic",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- doc JSON,
- doc_nn JSON NOT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "json_default_80",
- only80: true, // JSON column DEFAULT requires 8.0.13+
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- doc JSON DEFAULT (JSON_OBJECT()),
- arr JSON DEFAULT (JSON_ARRAY())
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "json_multivalue_index_80",
- only80: true, // multi-valued index (CAST ... AS ... ARRAY) is 8.0.17+
- bGap: false, // FIXED (dumper strips the functional-index charset introducer + unescapes quotes; omni #353 accepts AS … ARRAY, re-pinned a362f7a7)
- // FIXED (Bug B): the synced functional-index expression
- // `(cast(json_extract(`tags`,_utf8mb4\'$.ids\') as unsigned array))` carried two things the
- // omni loader rejects — the `_utf8mb4'…'` charset introducer before the JSON-path literal
- // (`unexpected token`) and backslash-escaped single quotes (`syntax error at or near "\"`).
- // normalizeFunctionalIndexExpr in get_database_definition.go now unescapes the quotes and
- // strips the introducer, so the dumped expr is `((cast(json_extract(`tags`,'$.ids') as
- // unsigned array)))`; omni's functional-index normalizer canonicalizes the dumped and user
- // forms identically and the no-op is empty. (The `AS UNSIGNED ARRAY` cast itself is accepted
- // by omni #353 — the introducer/escaping was the LoadSDL blocker, not the ARRAY keyword.)
- bGapReason: "",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- tags JSON NOT NULL,
- KEY idx_tags ((CAST(tags->'$.ids' AS UNSIGNED ARRAY)))
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
-
- // ---- Spatial ----
- {
- name: "spatial_types",
- only80: true, // 8.0-only GAP (FIXED): on 5.7 the dumper emits `geometrycollection` and IS idempotent; 8.0 used to break.
- // FIXED (bug 8): the dumper now normalizes the 8.0 `geomcollection` type synonym to
- // the canonical `geometrycollection` spelling omni parses (normalizeColumnType in
- // get_database_definition.go), so the dumped source reloads and the no-op is empty.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- g GEOMETRY,
- pt POINT,
- ls LINESTRING,
- poly POLYGON,
- gc GEOMETRYCOLLECTION
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "spatial_notnull_index",
- // FIXED (bug 7): the dumper no longer emits a key-part prefix length for SPATIAL (and
- // FULLTEXT) index parts (printIndexKeyPart suppressPrefix), so the spatial key dumps as
- // `(`pt`)` matching SHOW CREATE and the no-op is empty on both 5.7 and 8.0.
- ddl: `CREATE TABLE t (
- id INT NOT NULL AUTO_INCREMENT,
- pt POINT NOT NULL,
- PRIMARY KEY (id),
- SPATIAL KEY idx_pt (pt)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "spatial_srid_80",
- only80: true, // SRID column attribute is 8.0
- // FIXED (bug 6 + bug 7): sync now captures the column SRID (information_schema.COLUMNS.SRS_ID
- // into ColumnMetadata.srid) and the dumper emits `/*!80003 SRID 4326 */` after NOT NULL,
- // while the spatial-key prefix is suppressed (bug 7). Both halves of the former no-op are
- // gone, so the dump round-trips empty.
- ddl: `CREATE TABLE t (
- id INT NOT NULL AUTO_INCREMENT,
- pt POINT NOT NULL SRID 4326,
- PRIMARY KEY (id),
- SPATIAL KEY idx_pt (pt)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
-
- // ---- Fractional seconds ----
- {
- name: "fractional_seconds",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- dt6 DATETIME(6) NOT NULL,
- tm3 TIME(3) NOT NULL,
- ts6 TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
- dt0 DATETIME(0)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
-
- // ---- BLOB / TEXT family ----
- {
- name: "blob_text_family",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- tb TINYBLOB,
- b BLOB,
- mb MEDIUMBLOB,
- lb LONGBLOB,
- tt TINYTEXT,
- txt TEXT,
- mt MEDIUMTEXT,
- lt LONGTEXT
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "text_charset_collate",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
- b LONGTEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci,
- c BLOB
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "text_blob_prefix_index",
- ddl: `CREATE TABLE t (
- id INT NOT NULL AUTO_INCREMENT,
- body TEXT,
- data BLOB,
- PRIMARY KEY (id),
- KEY idx_body (body(50)),
- KEY idx_data (data(30))
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
-
- // ---- Charsets beyond utf8mb4 ----
- {
- name: "charset_latin1_ascii",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci,
- b VARCHAR(50) CHARACTER SET ascii,
- c CHAR(10) CHARACTER SET latin1
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "charset_binary_varbinary",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a BINARY(16),
- b VARBINARY(255),
- c VARCHAR(50) CHARACTER SET binary
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "charset_gbk_big5_utf16",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a VARCHAR(50) CHARACTER SET gbk,
- b VARCHAR(50) CHARACTER SET big5,
- c VARCHAR(50) CHARACTER SET utf16
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "table_charset_differs_from_column",
- // Table default latin1; one column overrides to utf8mb4 (column charset != table charset).
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a VARCHAR(50),
- b VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
-) ENGINE=InnoDB DEFAULT CHARSET=latin1;`,
- },
-
- // ---- Numeric / bit / extreme ----
- {
- name: "bit_type",
- // FIXED (Bug A): the dumper rendered the BIT default as a quoted string (`DEFAULT 'b\\'0\\''`)
- // because the sync stores the bit literal QUOTE()-escaped in ColumnMetadata.Default; omni then
- // loaded a string default that never matched the user's `b'0'`, re-emitting `MODIFY ... DEFAULT
- // b'0'` every no-op. renderColumnDefault in get_database_definition.go now recovers the bit
- // literal and emits it unquoted (`DEFAULT b'0'`, matching SHOW CREATE / mysqldump); omni #352
- // canonicalizes the bit literal so the no-op is empty on both versions. b64 (no default) is fine.
- bGap: false,
- bGapReason: "",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- b1 BIT(1) NOT NULL DEFAULT b'0',
- b8 BIT(8) NOT NULL DEFAULT b'101',
- b64 BIT(64)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "decimal_max_precision",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- d DECIMAL(65,30) NOT NULL DEFAULT 0
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "bigint_unsigned_maxdefault",
- bGap: false, // FIXED (re-pinned omni a362f7a7)
- bGapReason: "DANGEROUS (silently-wrong value, not just a phantom): a BIGINT UNSIGNED default of 18446744073709551615 (max uint64) is compared/re-emitted as 9223372036854775807 (max INT64) — the default is parsed into a signed int64 and clamped/overflows. The diff emits `MODIFY ... DEFAULT '9223372036854775807'`, which would CHANGE the default to the wrong number on apply. sm/ti (in-range) are fine.",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- big BIGINT UNSIGNED NOT NULL DEFAULT 18446744073709551615,
- sm SMALLINT UNSIGNED NOT NULL DEFAULT 65535,
- ti TINYINT UNSIGNED NOT NULL DEFAULT 255
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "year_and_set",
- bGap: false, // FIXED (re-pinned omni a362f7a7)
- bGapReason: "YEAR default canonicalization: user `DEFAULT 2000` (numeric) vs stored `'2000'` (string) not recognized equal; the diff re-emits `MODIFY ... y year ... DEFAULT '2000'` every no-op (phantom; both versions). The SET column is idempotent.",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- y YEAR NOT NULL DEFAULT 2000,
- s SET('a','b','c','d','e','f','g') NOT NULL DEFAULT 'a,c,e'
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "large_enum_set",
- // Many ENUM/SET members.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- e ENUM('m00','m01','m02','m03','m04','m05','m06','m07','m08','m09','m10','m11','m12','m13','m14','m15') NOT NULL DEFAULT 'm00',
- s SET('s0','s1','s2','s3','s4','s5','s6','s7','s8','s9') NOT NULL DEFAULT 's0,s9'
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "varchar_near_row_limit",
- // A wide VARCHAR in latin1 (1 byte/char) approaching the 65535-byte row limit.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- wide VARCHAR(16000) CHARACTER SET latin1 NOT NULL DEFAULT ''
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
-
- // ---- Generated columns chained ----
- {
- name: "generated_chained",
- // total references price/qty; with_tax references total (a generated col referencing
- // another generated col); both a VIRTUAL and a STORED, plus an index on a generated col.
- // NB: `code` is a plain (non-auto-increment) column — MySQL forbids a generated column
- // referencing an AUTO_INCREMENT column (errno 3109), so `label` references `code`.
- ddl: `CREATE TABLE t (
- id INT NOT NULL AUTO_INCREMENT,
- code INT NOT NULL DEFAULT 0,
- price DECIMAL(10,2) NOT NULL DEFAULT 0,
- qty INT NOT NULL DEFAULT 0,
- total DECIMAL(20,2) AS (price * qty) STORED,
- with_tax DECIMAL(20,2) AS (total * 1.1) VIRTUAL,
- label VARCHAR(40) AS (CONCAT('#', code)) STORED,
- PRIMARY KEY (id),
- KEY idx_total (total)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
-
- // ---- AUTO_INCREMENT seed ----
- {
- name: "auto_increment_seed_500",
- ddl: `CREATE TABLE t (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(50) NOT NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB AUTO_INCREMENT=500 DEFAULT CHARSET=utf8mb4;`,
- },
-
- // ---- Invisible columns / indexes (8.0) ----
- {
- name: "invisible_column_80",
- only80: true,
- // FIXED (bug 4): sync now captures column invisibility (information_schema.COLUMNS.EXTRA
- // INVISIBLE token into ColumnMetadata.is_invisible) and the dumper emits
- // `/*!80023 INVISIBLE */`, so the no-op is empty.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a INT NOT NULL DEFAULT 0,
- secret INT INVISIBLE NOT NULL DEFAULT 0
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "invisible_index_80",
- only80: true,
- // FIXED (bug 5): index visibility was already synced (IndexMetadata.visible from
- // information_schema.STATISTICS.IS_VISIBLE); the dumper now emits `/*!80000 INVISIBLE */`
- // in the index clause, so the no-op is empty instead of a DROP+ADD.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a INT NOT NULL,
- KEY idx_a (a) INVISIBLE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- }
-}
-
-// TestSDLDeepExoticIdempotence is Axis 2: every NON-bGap exotic-type case is applied live,
-// synced, dumped, and the production version-aware diff (dumped vs user form) MUST be empty
-// on both versions (8.0-only types skip 5.7). The bGap cases (confirmed normalization/dumper
-// (B) bugs found this round) are skipped here and reproduced/explained by
-// TestSDLDeepExoticKnownGaps so this assertion stays green. Each unexpected non-empty no-op
-// is logged with input + stored form so the gap can be pinned to a Canonical* rule or the
-// dumper.
-//
-//nolint:tparallel
-func TestSDLDeepExoticIdempotence(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, ec := range exoticCases() {
- ec := ec
- if srv.version == "5.7" && ec.only80 {
- continue
- }
- if ec.bGap {
- continue // confirmed (B) gap — pinned by TestSDLDeepExoticKnownGaps
- }
- t.Run(ec.name, func(t *testing.T) {
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_deep_exo", ec.ddl)
-
- // Production entry: schema.SDLMigration (meta -> SDL internally).
- noop := safeSDLMigration(ec.ddl, meta, srv.version)
- require.False(t, noop.panicked,
- "[%s/%s] (B) PANIC in schema.SDLMigration: %v\ninput:\n%s",
- srv.name, ec.name, noop.panicVal, ec.ddl)
- require.NoError(t, noop.err, "[%s/%s] SDLMigration error:\n%s", srv.name, ec.name, ec.ddl)
-
- source := dumpSDL(ctx, t, srv, dbName)
- if !noop.planEmpty {
- t.Logf("[%s/%s] (B) NON-EMPTY no-op normalization gap:\nUSER FORM:\n%s\nSTORED/DUMPED:\n%s\nDIFF:\n%s",
- srv.name, ec.name, ec.ddl, source, noop.plan)
- }
- require.True(t, noop.planEmpty,
- "[%s/%s] (B) exotic-type no-op (SDLMigration) must be empty, got:\n%s", srv.name, ec.name, noop.plan)
-
- // Version-aware lower-level entry, same property.
- noop2, err := mysqlDiffSDLMigration(source, ec.ddl, srv.version)
- require.NoError(t, err)
- require.Empty(t, noop2,
- "[%s/%s] (B) exotic-type no-op (mysqlDiffSDLMigration) must be empty, got:\n%s", srv.name, ec.name, noop2)
- })
- }
- })
- }
-}
-
-// TestSDLDeepExoticKnownGaps PINS the (B) normalization / dumper round-trip bugs this deep
-// round found in the exotic-type surface. Each bGap case is applied live and its no-op is
-// computed; the test asserts the no-op is currently NON-empty OR the dumped source fails to
-// reload (the bug signature), and logs the full user->stored->diff so a fix subagent has the
-// exact repro. When a bug is FIXED the corresponding case will start producing an empty,
-// reloadable no-op — flip its bGap flag to false (moving it back under the green idempotence
-// test) and this pin will report the now-idempotent case so the regression coverage moves
-// with the fix.
-//
-// Confirmed gaps (see each case's bGapReason for the precise root cause):
-// - DANGEROUS, silently-wrong value: bigint_unsigned_maxdefault (uint64 max default clamped
-// to int64 max in the diff — would change the default on apply).
-// - Default-literal canonicalization phantoms (re-emit every no-op): bit_type (BIT b'..'),
-// year_and_set (YEAR numeric-vs-quoted).
-// - Dumper drops a real attribute (re-emit every no-op): invisible_column_80,
-// invisible_index_80, spatial_srid_80 (SRID), spatial_notnull_index / spatial_srid_80
-// (phantom spatial-key prefix length).
-// - Dumper emits a spelling the omni parser can't reload (HARD error): spatial_types
-// (`geomcollection`), json_multivalue_index_80 (charset introducer in the functional key).
-//
-// SKIPPED so the suite stays green; remove the Skip to reproduce all gaps at once.
-//
-//nolint:tparallel
-func TestSDLDeepExoticKnownGaps(t *testing.T) {
- t.Skip("Pins confirmed (B) exotic-type normalization/dumper gaps; remove Skip to reproduce")
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, ec := range exoticCases() {
- ec := ec
- if !ec.bGap {
- continue
- }
- if srv.version == "5.7" && ec.only80 {
- continue
- }
- t.Run(ec.name, func(t *testing.T) {
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_deep_exo_gap", ec.ddl)
-
- noop := safeSDLMigration(ec.ddl, meta, srv.version)
- require.False(t, noop.panicked,
- "[%s/%s] (B) PANIC: %v", srv.name, ec.name, noop.panicVal)
-
- source := dumpSDL(ctx, t, srv, dbName)
- t.Logf("[%s/%s] (B) %s\nUSER:\n%s\nSTORED:\n%s\nNO-OP err=%v plan:\n%s",
- srv.name, ec.name, ec.bGapReason, ec.ddl, source, noop.err, noop.plan)
-
- // The bug signature is EITHER a hard error from reloading the dumped source
- // OR a non-empty no-op. Asserting it documents the current broken behavior.
- broke := noop.err != nil || !noop.planEmpty
- require.True(t, broke,
- "[%s/%s] expected the known (B) gap to reproduce (load error or non-empty no-op) but it was idempotent — the bug may be FIXED; clear bGap on this case.\nreason was: %s",
- srv.name, ec.name, ec.bGapReason)
- })
- }
- })
- }
-}
-
-// ============================================================================
-// Axis 3: sequential release chains (the real iterative usage).
-// ============================================================================
-//
-// Start from S0 on a real DB. Apply a SEQUENCE of declarative releases S0->S1->...->S4,
-// each a realistic incremental change set. At EACH step: source = MetadataToSDL(sync(DB)),
-// diff against the step target, assert the plan is minimal+correctly-ordered, APPLY it to
-// the real DB, re-sync, and assert the next no-op is empty before moving on. This proves
-// correctness across a migration HISTORY, not a single diff.
-
-// chainStep is one release in the sequence. target is the FULL desired SDL at that step
-// (declarative — the complete schema, not a delta). wantContains are uppercase substrings
-// the generated plan must contain; wantOrder pins a required relative ordering (first must
-// appear before second). only80Extra is appended to target only on 8.0 (CHECK etc.).
-type chainStep struct {
- name string
- target80 string
- target57 string
- wantContain []string // uppercase substrings the plan must contain
- wantAbsent []string // uppercase substrings the plan must NOT contain
- wantOrder [2]string // [before, after] uppercase substrings; before must precede after (skip if either empty)
-}
-
-func (s chainStep) target(version string) string {
- if version == "5.7" {
- return s.target57
- }
- return s.target80
-}
-
-// --- The sequence. Each Sn target is the FULL schema at that point. ---
-
-// chainS0: a small starting schema — customer + order with an FK, one view.
-const chainS0 = `
-CREATE TABLE customer (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(255) NOT NULL,
- PRIMARY KEY (id),
- UNIQUE KEY uk_cust_email (email)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE ord (
- id INT NOT NULL AUTO_INCREMENT,
- customer_id INT NOT NULL,
- total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- KEY idx_ord_cust (customer_id),
- CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_cust_orders AS
-SELECT c.name, o.id AS order_id, o.total
-FROM customer c JOIN ord o ON o.customer_id = c.id;
-`
-
-// chainS1: add table `order_item` + FK to ord (and to a new `product` table).
-const chainS1Common = `
-CREATE TABLE customer (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(255) NOT NULL,
- PRIMARY KEY (id),
- UNIQUE KEY uk_cust_email (email)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE ord (
- id INT NOT NULL AUTO_INCREMENT,
- customer_id INT NOT NULL,
- total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- KEY idx_ord_cust (customer_id),
- CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE product (
- id INT NOT NULL AUTO_INCREMENT,
- sku VARCHAR(40) NOT NULL,
- name VARCHAR(120) NOT NULL,
- price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- PRIMARY KEY (id),
- UNIQUE KEY uk_prod_sku (sku)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE order_item (
- id INT NOT NULL AUTO_INCREMENT,
- order_id INT NOT NULL,
- product_id INT NOT NULL,
- qty INT NOT NULL DEFAULT 1,
- PRIMARY KEY (id),
- KEY idx_oi_order (order_id),
- KEY idx_oi_product (product_id),
- CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE,
- CONSTRAINT fk_oi_product FOREIGN KEY (product_id) REFERENCES product (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_cust_orders AS
-SELECT c.name, o.id AS order_id, o.total
-FROM customer c JOIN ord o ON o.customer_id = c.id;
-`
-
-// chainS2: add a column (customer.loyalty_points) + an index (ord.idx_ord_created) + a
-// generated column (order_item.line_total references qty — but needs price; keep it simple:
-// generated col on product: price_with_tax).
-const chainS2Common = `
-CREATE TABLE customer (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(255) NOT NULL,
- loyalty_points INT NOT NULL DEFAULT 0,
- PRIMARY KEY (id),
- UNIQUE KEY uk_cust_email (email)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE ord (
- id INT NOT NULL AUTO_INCREMENT,
- customer_id INT NOT NULL,
- total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- KEY idx_ord_cust (customer_id),
- KEY idx_ord_created (created_at),
- CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE product (
- id INT NOT NULL AUTO_INCREMENT,
- sku VARCHAR(40) NOT NULL,
- name VARCHAR(120) NOT NULL,
- price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- price_with_tax DECIMAL(12,4) AS (price * 1.1) STORED,
- PRIMARY KEY (id),
- UNIQUE KEY uk_prod_sku (sku)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE order_item (
- id INT NOT NULL AUTO_INCREMENT,
- order_id INT NOT NULL,
- product_id INT NOT NULL,
- qty INT NOT NULL DEFAULT 1,
- PRIMARY KEY (id),
- KEY idx_oi_order (order_id),
- KEY idx_oi_product (product_id),
- CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE,
- CONSTRAINT fk_oi_product FOREIGN KEY (product_id) REFERENCES product (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_cust_orders AS
-SELECT c.name, o.id AS order_id, o.total
-FROM customer c JOIN ord o ON o.customer_id = c.id;
-`
-
-// chainS3: modify a column type (ord.total DECIMAL(10,2)->DECIMAL(14,4)) + widen a VARCHAR
-// (customer.name VARCHAR(100)->VARCHAR(200)) + change a default (customer.loyalty_points
-// DEFAULT 0 -> DEFAULT 100).
-const chainS3Common = `
-CREATE TABLE customer (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(200) NOT NULL,
- email VARCHAR(255) NOT NULL,
- loyalty_points INT NOT NULL DEFAULT 100,
- PRIMARY KEY (id),
- UNIQUE KEY uk_cust_email (email)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE ord (
- id INT NOT NULL AUTO_INCREMENT,
- customer_id INT NOT NULL,
- total DECIMAL(14,4) NOT NULL DEFAULT 0.0000,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- KEY idx_ord_cust (customer_id),
- KEY idx_ord_created (created_at),
- CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE product (
- id INT NOT NULL AUTO_INCREMENT,
- sku VARCHAR(40) NOT NULL,
- name VARCHAR(120) NOT NULL,
- price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- price_with_tax DECIMAL(12,4) AS (price * 1.1) STORED,
- PRIMARY KEY (id),
- UNIQUE KEY uk_prod_sku (sku)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE order_item (
- id INT NOT NULL AUTO_INCREMENT,
- order_id INT NOT NULL,
- product_id INT NOT NULL,
- qty INT NOT NULL DEFAULT 1,
- PRIMARY KEY (id),
- KEY idx_oi_order (order_id),
- KEY idx_oi_product (product_id),
- CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE,
- CONSTRAINT fk_oi_product FOREIGN KEY (product_id) REFERENCES product (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_cust_orders AS
-SELECT c.name, o.id AS order_id, o.total
-FROM customer c JOIN ord o ON o.customer_id = c.id;
-`
-
-// chainS4: drop a column WITH its index (drop product.price_with_tax generated col), drop a
-// table that is an FK PARENT (drop product — order_item.fk_oi_product child FK must drop
-// first; also drop order_item to keep it consistent? No — keep order_item but remove its FK
-// to product by dropping the product reference). To exercise "drop FK parent, child FK drops
-// first", we drop `product` AND order_item's fk_oi_product + product_id column. Replace the
-// view, and (8.0) add a CHECK on ord.
-//
-// S4 also changes order_item to remove product linkage, replaces v_cust_orders, adds a
-// trigger on ord, and (8.0) adds a CHECK on ord.total.
-const chainS4Base = `
-CREATE TABLE customer (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(200) NOT NULL,
- email VARCHAR(255) NOT NULL,
- loyalty_points INT NOT NULL DEFAULT 100,
- PRIMARY KEY (id),
- UNIQUE KEY uk_cust_email (email)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE ord (
- id INT NOT NULL AUTO_INCREMENT,
- customer_id INT NOT NULL,
- total DECIMAL(14,4) NOT NULL DEFAULT 0.0000,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- KEY idx_ord_cust (customer_id),
- KEY idx_ord_created (created_at){{ORD_CHECK}}
- ,CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE order_item (
- id INT NOT NULL AUTO_INCREMENT,
- order_id INT NOT NULL,
- qty INT NOT NULL DEFAULT 1,
- PRIMARY KEY (id),
- KEY idx_oi_order (order_id),
- CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_cust_orders AS
-SELECT c.name, c.email, o.id AS order_id, o.total
-FROM customer c JOIN ord o ON o.customer_id = c.id;
-
-CREATE TRIGGER trg_ord_ins BEFORE INSERT ON ord FOR EACH ROW
-SET NEW.created_at = NOW();
-`
-
-func chainSteps() []chainStep {
- s4_80 := strings.Replace(chainS4Base, "{{ORD_CHECK}}", ",\n\tCONSTRAINT chk_total CHECK (total >= 0)", 1)
- s4_57 := strings.Replace(chainS4Base, "{{ORD_CHECK}}", "", 1)
- return []chainStep{
- {
- name: "S1_add_table_fk",
- target80: chainS1Common,
- target57: chainS1Common,
- wantContain: []string{"CREATE TABLE `PRODUCT`", "CREATE TABLE `ORDER_ITEM`"},
- wantAbsent: []string{"DROP TABLE", "CREATE TABLE `CUSTOMER`", "CREATE TABLE `ORD`"},
- // The order_item FK to ord/product is deferred to PhasePost: table create precedes the FK add.
- wantOrder: [2]string{"CREATE TABLE `ORDER_ITEM`", "ADD CONSTRAINT `FK_OI_ORDER`"},
- },
- {
- name: "S2_add_column_index_generated",
- target80: chainS2Common,
- target57: chainS2Common,
- wantContain: []string{"ADD COLUMN `LOYALTY_POINTS`", "ADD KEY `IDX_ORD_CREATED`", "PRICE_WITH_TAX"},
- wantAbsent: []string{"DROP TABLE", "CREATE TABLE"},
- },
- {
- name: "S3_modify_type_widen_default",
- target80: chainS3Common,
- target57: chainS3Common,
- // total type change, name widen, loyalty default change.
- wantContain: []string{"DECIMAL(14,4)", "VARCHAR(200)"},
- wantAbsent: []string{"DROP TABLE", "CREATE TABLE `CUSTOMER`", "DROP COLUMN"},
- },
- {
- name: "S4_drop_col_drop_parent_replace_view_trigger",
- target80: s4_80,
- target57: s4_57,
- // product table dropped; order_item.product_id column + its FK dropped; view replaced; trigger added.
- wantContain: []string{"DROP TABLE `PRODUCT`", "DROP COLUMN `PRODUCT_ID`", "DROP FOREIGN KEY `FK_OI_PRODUCT`", "TRG_ORD_INS", "V_CUST_ORDERS"},
- wantAbsent: []string{"DROP TABLE `CUSTOMER`", "DROP TABLE `ORD`"},
- // The child FK on order_item must drop before the parent product table is dropped.
- wantOrder: [2]string{"DROP FOREIGN KEY `FK_OI_PRODUCT`", "DROP TABLE `PRODUCT`"},
- },
- }
-}
-
-// TestSDLDeepSequentialChain is Axis 3: drive S0 -> S1 -> S2 -> S3 -> S4 on ONE live DB.
-// Each step computes source=MetadataToSDL(sync), diffs the step target, asserts the plan is
-// minimal + correctly ordered, APPLIES it, re-syncs, and asserts the next no-op is empty
-// before advancing. Proves correctness across a migration history. Both versions.
-//
-// The steps run inline (NOT as separate subtests) BECAUSE they share one evolving DB: each
-// step's diff is from its predecessor's applied state. Running a single step in isolation
-// (e.g. `-run .../S4`) would compute a diff from S0 and assert wrong things — so the chain is
-// kept atomic per server.
-//
-//nolint:tparallel
-func TestSDLDeepSequentialChain(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- // Materialize S0 on a single DB that persists across all steps.
- dbName := newLiveDatabase(ctx, t, srv, "sdl_deep_chain")
- require.NoError(t, applyDDL(ctx, t, srv, dbName, chainS0), "[%s] apply S0", srv.name)
-
- // Confirm S0 itself is idempotent before starting the chain.
- s0src := dumpSDL(ctx, t, srv, dbName)
- s0noop, err := mysqlDiffSDLMigration(s0src, chainS0, srv.version)
- require.NoError(t, err)
- require.Empty(t, s0noop, "[%s] S0 must be idempotent before the chain, got:\n%s", srv.name, s0noop)
-
- // Run every step inline, in order, against the same evolving DB.
- for _, step := range chainSteps() {
- runChainStep(ctx, t, srv, dbName, step)
- if t.Failed() {
- return // a broken step poisons the chain state; stop so the failure is unambiguous
- }
- }
- })
- }
-}
-
-// runChainStep executes one release in the sequential chain against the live dbName.
-func runChainStep(ctx context.Context, t *testing.T, srv liveServer, dbName string, step chainStep) {
- t.Helper()
- target := step.target(srv.version)
-
- // 1) Compute the plan from the CURRENT live DB state to the step target.
- source := dumpSDL(ctx, t, srv, dbName)
- plan, err := mysqlDiffSDLMigration(source, target, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, plan, "[%s/%s] step must produce a non-empty plan", srv.name, step.name)
- t.Logf("[%s/%s] plan:\n%s", srv.name, step.name, plan)
- upper := strings.ToUpper(plan)
-
- // 2) Minimality + correctness: required substrings present, forbidden absent.
- for _, want := range step.wantContain {
- require.Contains(t, upper, want, "[%s/%s] plan missing %q:\n%s", srv.name, step.name, want, plan)
- }
- for _, absent := range step.wantAbsent {
- require.NotContains(t, upper, absent, "[%s/%s] plan must not contain %q:\n%s", srv.name, step.name, absent, plan)
- }
-
- // 3) Ordering.
- if step.wantOrder[0] != "" && step.wantOrder[1] != "" {
- bi := indexOf(upper, step.wantOrder[0])
- ai := indexOf(upper, step.wantOrder[1])
- require.GreaterOrEqual(t, bi, 0, "[%s/%s] ordering: %q not found:\n%s", srv.name, step.name, step.wantOrder[0], plan)
- require.GreaterOrEqual(t, ai, 0, "[%s/%s] ordering: %q not found:\n%s", srv.name, step.name, step.wantOrder[1], plan)
- require.Less(t, bi, ai, "[%s/%s] %q must precede %q:\n%s", srv.name, step.name, step.wantOrder[0], step.wantOrder[1], plan)
- }
-
- // 4) Apply to the real DB.
- require.NoError(t, applyDDL(ctx, t, srv, dbName, plan),
- "[%s/%s] step plan failed to apply:\n%s", srv.name, step.name, plan)
-
- // 5) Re-sync and assert convergence (next no-op empty) BEFORE advancing.
- newSource := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(newSource, target, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "[%s/%s] step did not converge, residual:\n%s", srv.name, step.name, converge)
-
- // 6) Production-path cross-check: schema.SDLMigration on a freshly-synced meta must also be
- // empty (the path the release executor takes).
- meta := syncMetaForDB(ctx, t, srv, dbName)
- prodNoop, err := schema.SDLMigration(storepb.Engine_MYSQL, target, meta, srv.version)
- require.NoError(t, err)
- require.Empty(t, prodNoop, "[%s/%s] production-path no-op after apply must be empty, got:\n%s", srv.name, step.name, prodNoop)
-}
-
-// syncMetaForDB syncs an existing dbName and returns its model.DatabaseMetadata.
-func syncMetaForDB(ctx context.Context, t *testing.T, srv liveServer, dbName string) *model.DatabaseMetadata {
- t.Helper()
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true)
-}
-
-// ============================================================================
-// Axis 4: scale / performance sanity.
-// ============================================================================
-//
-// Generate ~90 tables (each ~8-15 columns, a PK, 2-3 secondary indexes, FKs forming a
-// realistic chain). Sync -> MetadataToSDL -> no-op diff must be EMPTY and complete in
-// reasonable wall-time. Then add ONE column to ONE table -> assert exactly one minimal
-// ALTER, not a re-emit of all tables. Record timings.
-
-// genScaleSchema builds n tables t000..t{n-1}. Each table i (i>=1) carries an FK to table
-// i-1 (a chain), plus 2 secondary indexes and ~10 columns of mixed types. Returns the DDL.
-func genScaleSchema(n int) string {
- var b strings.Builder
- for i := 0; i < n; i++ {
- fmt.Fprintf(&b, "CREATE TABLE `t%03d` (\n", i)
- b.WriteString("\tid INT NOT NULL AUTO_INCREMENT,\n")
- b.WriteString("\tcode VARCHAR(40) NOT NULL,\n")
- b.WriteString("\tname VARCHAR(120) NOT NULL,\n")
- b.WriteString("\tdescription TEXT,\n")
- b.WriteString("\tamount DECIMAL(14,2) NOT NULL DEFAULT 0.00,\n")
- b.WriteString("\tqty INT NOT NULL DEFAULT 0,\n")
- b.WriteString("\tactive BOOLEAN NOT NULL DEFAULT TRUE,\n")
- b.WriteString("\tcreated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n")
- b.WriteString("\tupdated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n")
- b.WriteString("\tstatus ENUM('new','open','closed') NOT NULL DEFAULT 'new',\n")
- if i > 0 {
- b.WriteString("\tprev_id INT NULL,\n")
- }
- b.WriteString("\tPRIMARY KEY (id),\n")
- fmt.Fprintf(&b, "\tUNIQUE KEY `uk_t%03d_code` (code),\n", i)
- fmt.Fprintf(&b, "\tKEY `idx_t%03d_name` (name),\n", i)
- if i > 0 {
- fmt.Fprintf(&b, "\tKEY `idx_t%03d_prev` (prev_id),\n", i)
- fmt.Fprintf(&b, "\tCONSTRAINT `fk_t%03d_prev` FOREIGN KEY (prev_id) REFERENCES `t%03d` (id) ON DELETE SET NULL\n", i, i-1)
- } else {
- // trim the trailing comma from the last index line for table 0
- fmt.Fprintf(&b, "\tKEY `idx_t%03d_amount` (amount)\n", i)
- }
- b.WriteString(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n")
- }
- return b.String()
-}
-
-// TestSDLDeepScale is Axis 4: a ~90-table schema. The no-op diff must be empty and fast;
-// adding one column to one table must yield exactly one minimal ALTER, not a re-emit of all
-// tables. Timings are recorded and a pathological (super-linear / multi-second) no-op is
-// flagged.
-//
-//nolint:tparallel
-func TestSDLDeepScale(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- const tableCount = 90
- schemaDDL := genScaleSchema(tableCount)
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_deep_scale", schemaDDL)
-
- // (1) No-op diff: must be empty + record wall-time.
- source := dumpSDL(ctx, t, srv, dbName)
- start := time.Now()
- noop, err := mysqlDiffSDLMigration(source, schemaDDL, srv.version)
- elapsed := time.Since(start)
- require.NoError(t, err)
- if noop != "" {
- t.Logf("[%s] NON-EMPTY %d-table no-op (%d statements):\n%s", srv.name, tableCount, statementCount(noop), noop)
- }
- require.Empty(t, noop, "[%s] %d-table no-op must be empty, got %d statements", srv.name, tableCount, statementCount(noop))
- t.Logf("[%s] SCALE no-op diff over %d tables: %s", srv.name, tableCount, elapsed)
- // Soft perf flag: a no-op over ~90 tables should be well under a few seconds.
- if elapsed > 10*time.Second {
- t.Errorf("[%s] (PERF) %d-table no-op took %s (>10s) — pathological", srv.name, tableCount, elapsed)
- }
-
- // (2) Production-path no-op timing too.
- pstart := time.Now()
- prodNoop, err := schema.SDLMigration(storepb.Engine_MYSQL, schemaDDL, meta, srv.version)
- pelapsed := time.Since(pstart)
- require.NoError(t, err)
- require.Empty(t, prodNoop, "[%s] %d-table production no-op must be empty", srv.name, tableCount)
- t.Logf("[%s] SCALE production-path no-op (incl. MetadataToSDL): %s", srv.name, pelapsed)
-
- // (3) One small change: add a column to ONE table (t045). Must be exactly one ALTER
- // touching only that table — not a re-emit of the other 89.
- changed := strings.Replace(schemaDDL,
- "CREATE TABLE `t045` (\n\tid INT NOT NULL AUTO_INCREMENT,\n\tcode VARCHAR(40) NOT NULL,",
- "CREATE TABLE `t045` (\n\tid INT NOT NULL AUTO_INCREMENT,\n\tcode VARCHAR(40) NOT NULL,\n\tnew_flag INT NOT NULL DEFAULT 0,",
- 1)
- require.NotEqual(t, schemaDDL, changed, "[%s] scale change setup must differ", srv.name)
-
- cstart := time.Now()
- change, err := mysqlDiffSDLMigration(source, changed, srv.version)
- celapsed := time.Since(cstart)
- require.NoError(t, err)
- t.Logf("[%s] SCALE single-change diff: %s\nplan:\n%s", srv.name, celapsed, change)
- require.NotEmpty(t, change, "[%s] single-change must produce DDL", srv.name)
-
- upper := strings.ToUpper(change)
- require.Contains(t, upper, "ADD COLUMN `NEW_FLAG`", "[%s] expected the new column:\n%s", srv.name, change)
- require.Contains(t, upper, "`T045`", "[%s] expected the changed table named:\n%s", srv.name, change)
- require.NotContains(t, upper, "CREATE TABLE", "[%s] single-change must not recreate tables:\n%s", srv.name, change)
- require.NotContains(t, upper, "DROP TABLE", "[%s] single-change must not drop tables:\n%s", srv.name, change)
- // Strict minimality: exactly ONE statement (the single ADD COLUMN).
- require.Equal(t, 1, statementCount(change),
- "[%s] single-change over %d tables must be exactly 1 statement, got %d:\n%s",
- srv.name, tableCount, statementCount(change), change)
-
- // No other table may be named in the plan.
- for i := 0; i < tableCount; i++ {
- if i == 45 {
- continue
- }
- token := fmt.Sprintf("`T%03d`", i)
- require.NotContains(t, upper, token,
- "[%s] single-change must not touch table t%03d:\n%s", srv.name, i, change)
- }
- })
- }
-}
diff --git a/backend/plugin/schema/mysql/sdl_enterprise_crossfuzz_test.go b/backend/plugin/schema/mysql/sdl_enterprise_crossfuzz_test.go
deleted file mode 100644
index ea40e80176..0000000000
--- a/backend/plugin/schema/mysql/sdl_enterprise_crossfuzz_test.go
+++ /dev/null
@@ -1,323 +0,0 @@
-package mysql
-
-// A8 of the ENTERPRISE smoke axes (see sdl_enterprise_test.go): CROSS-VERSION STATEFUL
-// FUZZ — the composition of A6 (same-version multi-round stateful fuzz) and A7
-// (single-shot cross-version upgrade). Real teams run MIXED fleets: a schema authored
-// and dumped on 5.7 drives targets applied to 8.0 (and vice versa during migrations),
-// repeatedly, while the schema keeps evolving. A6 and A7 both pass in isolation; this
-// axis hunts what only their composition shows — version-normalization drift
-// COMPOUNDING across rounds (a residual one round becomes the authored text of the
-// next, so a per-round phantom that a single-shot test shrugs off snowballs here).
-//
-// Protocol per seed, on the 5.7-legal A6 base (30-table Zabbix slice + fuzz aux +
-// stateful aux; the 8.0-only aux tables are excluded — the SAME logical schema must
-// load on BOTH versions), both databases collation-aligned (utf8mb4/utf8mb4_general_ci,
-// the A7 aligned-probe posture) so the fleets start logically identical:
-//
-// round 0: the two fresh dumps must already cross-diff EMPTY (all four
-// direction x version-normalization combos).
-// for round r in 1..4:
-// T_r = mutate(authored side's dump_{r-1}, K ∈ [2,4] from the 5.7-LEGAL menu)
-// each side s ∈ {5.7, 8.0}: plan_s = Diff(dump_s, T_r, full server version) must
-// be non-empty, apply cleanly, converge (re-dump diffs empty against T_r
-// under s's version), self-diff empty, reload through STRICT LoadSDL, and
-// match the mutation ledger's object counts (per-side ledgers: baselines may
-// differ, deltas are shared);
-// plan_80 must carry no 5.7 integer display width in any ALTER (entUpgAssert-
-// NoWidthInAlters — a leaked width is a phantom column MODIFY);
-// CROSS-CHECK: the two fresh dumps must cross-diff EMPTY in all four combos —
-// the fleets stay logically identical despite version-different stored forms;
-// ACCRETION (A6's guards, on BOTH sides): each side's dump stays under its own
-// dump_0 length + the cumulative added-object budget, and every statement not
-// named by this round's mutations is byte-identical to its round-start form.
-//
-// forward_57_authored seeds mutate the 5.7 dump; reverse_80_authored seeds mutate the
-// 8.0 dump (the authored text then carries NO display widths while the 5.7 side's
-// stored form does — the opposite normalization direction). Both directions use the
-// 5.7-legal menu: every target applies to both fleets, so 5.7-illegal mutations (CHECK,
-// SRID spatial, INVISIBLE) are structurally excluded. Additionally, child-side FK
-// member columns are protected from the generated-column kinds — both servers refuse
-// a STORED generated column based on a cascading-FK member (see
-// entA8ProtectFKMemberColumns for the empirical matrix this axis established).
-//
-// Determinism: the mutation stream is a pure function of the seed (in the subtest
-// name). Failures ddmin-minimize over a cross-version trial (both round-start dumps
-// reloaded into fresh aligned scratch databases) and report seed + round + the failing
-// check (apply/converge/idempotence/reload/counts per side, or the cross combo) + the
-// minimized ledger. Accretion-guard failures are terminal requires with the same
-// seed/round/check labeling. Scratch databases are entsdl_-prefixed and dropped.
-//
-// ≥8 forward seeds + ≥4 reverse seeds.
-//
-// Shared machinery: entSfBaseDDL, entSfParse, entSfMenu, entSfMutationsFromMenu,
-// entSfRoundToTarget, entSfMinimizeWith, entSfScratchDB, entSfCountObjects,
-// entSfAssertUntouchedStable, entSfDescs, entSfLedger, entSfRoundSlack (A6);
-// entUpgServers, entUpgLoadAligned, entUpgAssertNoWidthInAlters, entUpgTrim,
-// entUpg57Version, entUpg80Version (A7); liveServers, dumpSDL, syncMetaForDB,
-// newLiveDatabase, createLiveMySQLDriver, entNormalizeDelimiters (siblings).
-
-import (
- "context"
- "fmt"
- "math/rand"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/pkg/errors"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
-)
-
-const (
- entA8Rounds = 4
- entA8ForwardSeeds = 8
- entA8ReverseSeeds = 4
-)
-
-// entA8Fleet is one side of the mixed-version fleet.
-type entA8Fleet struct {
- srv liveServer
- // version is the FULL server version threaded into every diff for this side (the
- // release path passes the synced version string, not a bare major.minor).
- version string
- dbName string
- prev string // canonical dump at the current round boundary
- dump0 int // round-0 dump length (text-accretion baseline)
- budget int // cumulative added-object text budget
- expect entSfCounts
-}
-
-// entA8CrossCheck proves the two fleets are logically identical: all four
-// (direction x version-normalization) cross-diffs of the two canonical dumps must be
-// empty. The two "apply" pairings mirror A7's aligned probe — the plan a fleet would
-// receive to be driven to the OTHER fleet's schema; the remaining two probe the
-// canonicalizer's version symmetry (the same pair re-judged under the other version's
-// stored form).
-func entA8CrossCheck(d57, d80 string) error {
- for _, c := range []struct{ label, source, target, version string }{
- {"80db<=57dump(as 8.0.32)", d80, d57, entUpg80Version},
- {"57db<=80dump(as 5.7.25)", d57, d80, entUpg57Version},
- {"57src->80tgt(as 8.0.32)", d57, d80, entUpg80Version},
- {"80src->57tgt(as 5.7.25)", d80, d57, entUpg57Version},
- } {
- plan, err := mysqlDiffSDLMigration(c.source, c.target, c.version)
- if err != nil {
- return errors.Wrapf(err, "check=cross %s: diff failed", c.label)
- }
- if strings.TrimSpace(plan) != "" {
- return errors.Errorf("check=cross %s: fleets drifted apart; residual plan:\n%s", c.label, entUpgTrim(plan))
- }
- }
- return nil
-}
-
-// entA8ProtectFKMemberColumns marks child-side foreign-key member columns as protected
-// in the mutation-planning model, keeping the A8 menu off a server-refused shape this
-// axis found empirically: BOTH oracle servers reject a STORED generated column whose
-// base column participates in a foreign key with a cascading referential action —
-// 8.0.32 with a clean 1215 "Cannot add foreign key constraint" at DDL time, 5.7.25
-// with an opaque errno-150 failure at ALGORITHM=COPY rename (the shape seed 18 of a
-// widened sweep minimized to: ADD COLUMN ... GENERATED ALWAYS AS ((groupid + 7)) STORED
-// on hosts_groups, whose groupid carries zabbix's usual ON DELETE CASCADE FK).
-// Parent-side referenced columns, VIRTUAL generated columns, and RESTRICT/NO ACTION
-// FKs are all accepted by both servers and stay eligible. The guard is
-// action-agnostic (membership, not action): zabbix FKs are almost all
-// ON DELETE CASCADE and the add_fk mutation plants ON DELETE SET NULL, so an
-// action-aware refinement would buy nothing here.
-func entA8ProtectFKMemberColumns(m *entSfSchema) {
- for _, tbl := range m.base.tables {
- for _, fk := range tbl.fks {
- for _, c := range fk.cols {
- m.protected[tbl.name+"."+c] = true
- }
- }
- }
-}
-
-// entA8Round drives one shared target through both fleets and cross-checks the
-// results: each side runs the full A6 round oracle (diff -> apply -> converge ->
-// idempotence -> strict LoadSDL reload -> ledger counts) under its own version, then
-// the two fresh dumps must cross-diff empty. Protocol violations return stage-tagged
-// errors (for ddmin); infrastructure failures fail t hard. On success both fleets'
-// prev/expect/budget are advanced and the 8.0-side plan is returned for the width
-// guard (the 5.7-side plan may carry display widths legitimately — they ARE its
-// stored form — so nothing judges it beyond the round oracle).
-func entA8Round(ctx context.Context, t *testing.T, f57, f80 *entA8Fleet, target string, muts []entSfMutation) (string, error) {
- t.Helper()
- want57, want80 := f57.expect, f80.expect
- for _, m := range muts {
- want57 = want57.plus(m.delta)
- want80 = want80.plus(m.delta)
- }
- after57, _, err := entSfRoundToTarget(ctx, t, f57.srv, f57.dbName, f57.prev, target, f57.version, want57)
- if err != nil {
- return "", errors.Wrapf(err, "check=side %s", f57.srv.name)
- }
- after80, plan80, err := entSfRoundToTarget(ctx, t, f80.srv, f80.dbName, f80.prev, target, f80.version, want80)
- if err != nil {
- return "", errors.Wrapf(err, "check=side %s", f80.srv.name)
- }
- if err := entA8CrossCheck(after57, after80); err != nil {
- return "", err
- }
- for _, m := range muts {
- f57.budget += m.budget
- f80.budget += m.budget
- }
- f57.prev, f57.expect = after57, want57
- f80.prev, f80.expect = after80, want80
- return plan80, nil
-}
-
-// entA8ScratchFromDump reloads a canonical dump into a fresh, eagerly-droppable,
-// collation-aligned scratch database. The dump text executes with FOREIGN_KEY_CHECKS
-// off for the session (canonical dumps order tables alphabetically, so inline foreign
-// keys may forward-reference; the driver runs the whole batch on one connection, so
-// the session setting holds).
-func entA8ScratchFromDump(ctx context.Context, t *testing.T, srv liveServer, dump string) (string, func()) {
- t.Helper()
- dbName, drop := entSfScratchDB(ctx, t, srv, "entsdl_a8t")
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, "ALTER DATABASE `"+dbName+"` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", db.ExecuteOptions{})
- require.NoError(t, err, "[%s] align trial database collation", srv.name)
- _, err = driver.Execute(ctx, "SET FOREIGN_KEY_CHECKS=0;\n"+entNormalizeDelimiters(dump), db.ExecuteOptions{})
- require.NoError(t, err, "[%s] reload round-start dump into trial database", srv.name)
- return dbName, drop
-}
-
-// entA8Trial reproduces one cross-version round from scratch for ddmin: both
-// round-start dumps reload into fresh aligned scratch databases, the mutation subset
-// re-authors the target from the authored side's dump, and the full round runs.
-func entA8Trial(ctx context.Context, t *testing.T, srv57, srv80 liveServer, prev57, prev80 string, authored80 bool, muts []entSfMutation) error {
- t.Helper()
- db57, drop57 := entA8ScratchFromDump(ctx, t, srv57, prev57)
- defer drop57()
- db80, drop80 := entA8ScratchFromDump(ctx, t, srv80, prev80)
- defer drop80()
-
- f57 := &entA8Fleet{srv: srv57, version: entUpg57Version, dbName: db57}
- f80 := &entA8Fleet{srv: srv80, version: entUpg80Version, dbName: db80}
- for _, f := range []*entA8Fleet{f57, f80} {
- f.prev = dumpSDL(ctx, t, f.srv, f.dbName)
- f.expect = entSfCountObjects(syncMetaForDB(ctx, t, f.srv, f.dbName))
- }
- authored := f57.prev
- if authored80 {
- authored = f80.prev
- }
- target := authored
- for _, m := range muts {
- target = m.apply(t, target)
- }
- _, err := entA8Round(ctx, t, f57, f80, target, muts)
- return err
-}
-
-// entA8RunSeed runs one full cross-version stateful seed: 4 rounds of
-// mutate-on-the-authored-side -> apply-to-both-fleets -> cross-check -> accretion
-// guards on both sides.
-func entA8RunSeed(ctx context.Context, t *testing.T, srv57, srv80 liveServer, baseDDL string, seed int64, authored80 bool) {
- t.Helper()
- rng := rand.New(rand.NewSource(seed)) //nolint:gosec // deterministic fuzz, not crypto
-
- f57 := &entA8Fleet{srv: srv57, version: entUpg57Version}
- f80 := &entA8Fleet{srv: srv80, version: entUpg80Version}
- f57.dbName = entUpgLoadAligned(ctx, t, srv57, "entsdl_a8m57", baseDDL)
- f80.dbName = entUpgLoadAligned(ctx, t, srv80, "entsdl_a8m80", baseDDL)
- for _, f := range []*entA8Fleet{f57, f80} {
- f.prev = dumpSDL(ctx, t, f.srv, f.dbName)
- f.dump0 = len(f.prev)
- f.expect = entSfCountObjects(syncMetaForDB(ctx, t, f.srv, f.dbName))
- }
- dir, author := "fwd57", f57
- if authored80 {
- dir, author = "rev80", f80
- }
-
- // Round 0: freshly loaded from the same DDL under aligned defaults, the fleets must
- // already be logically identical — a failure here is a base cross-version phantom,
- // not mutation drift.
- require.NoError(t, entA8CrossCheck(f57.prev, f80.prev),
- "[A8 %s seed=%d round=0] fleets diverge before any mutation (base cross-check)", dir, seed)
-
- ledger := &entSfLedger{viewModified: map[string]bool{}}
- var history []string
- for r := 1; r <= entA8Rounds; r++ {
- m := entSfParse(t, author.prev)
- entA8ProtectFKMemberColumns(m)
- k := 2 + rng.Intn(3) // K ∈ [2,4]
- muts := entSfMutationsFromMenu(t, m, rng, entSfMenu("5.7"), ledger, r, k)
- for _, mut := range muts {
- history = append(history, fmt.Sprintf("r%d: %s", r, mut.desc))
- }
- t.Logf("[A8 %s seed=%d round=%d] K=%d mutations:\n %s",
- dir, seed, r, k, strings.Join(entSfDescs(muts), "\n "))
-
- target := author.prev
- for _, mut := range muts {
- target = mut.apply(t, target)
- }
- prev57, prev80 := f57.prev, f80.prev
- plan80, err := entA8Round(ctx, t, f57, f80, target, muts)
- if err != nil {
- minimized, minErr := entSfMinimizeWith(muts, err, func(trial []entSfMutation) error {
- return entA8Trial(ctx, t, srv57, srv80, prev57, prev80, authored80, trial)
- })
- t.Fatalf("[A8 %s seed=%d round=%d] FAILED\nminimized mutations (%d of %d):\n %s\nerror:\n%v\nseed history:\n %s",
- dir, seed, r, len(minimized), len(muts), strings.Join(entSfDescs(minimized), "\n "),
- minErr, strings.Join(history, "\n "))
- }
-
- // A leaked 5.7 display width inside an 8.0-side ALTER is a phantom column MODIFY
- // (the 5.7-side plan may carry widths legitimately — they ARE its stored form).
- entUpgAssertNoWidthInAlters(t, fmt.Sprintf("A8 %s seed=%d round=%d plan80", dir, seed, r), plan80)
-
- // Accretion guards on BOTH sides' dumps (each against its own round-0 baseline).
- for _, s := range []struct {
- f *entA8Fleet
- prev string
- }{{f57, prev57}, {f80, prev80}} {
- require.Less(t, len(s.f.prev), s.f.dump0+s.f.budget+entSfRoundSlack*r,
- "[A8 %s seed=%d round=%d side=%s] check=accretion: dump grew past the added-object budget: len(dump_%d)=%d, len(dump_0)=%d, budget=%d — text accretion suspected.\nseed history:\n %s",
- dir, seed, r, s.f.srv.name, r, len(s.f.prev), s.f.dump0, s.f.budget+entSfRoundSlack*r, strings.Join(history, "\n "))
- entSfAssertUntouchedStable(t, s.f.srv, seed, r, s.prev, s.f.prev, muts)
- }
-
- t.Logf("[A8 %s seed=%d round=%d] ok: 57 %d -> %d bytes, 80 %d -> %d bytes (budget 57=%d 80=%d), counts 57=%+v 80=%+v",
- dir, seed, r, len(prev57), len(f57.prev), len(prev80), len(f80.prev),
- f57.dump0+f57.budget+entSfRoundSlack*r, f80.dump0+f80.budget+entSfRoundSlack*r, f57.expect, f80.expect)
- }
-}
-
-//nolint:tparallel
-func TestSDLEnterpriseCrossVersionStatefulFuzz(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
- srv57, srv80 := entUpgServers(t)
- // The fleet schema must load on BOTH versions, so the base is the 5.7 shape of the
- // A6 stateful-fuzz base (entSfAux80's SRID/INVISIBLE/CHECK tables are 8.0-only).
- baseDDL := entSfBaseDDL(t, srv57)
-
- t.Run("forward_57_authored", func(t *testing.T) {
- for seed := int64(1); seed <= entA8ForwardSeeds; seed++ {
- seed := seed
- t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
- entA8RunSeed(ctx, t, srv57, srv80, baseDDL, seed, false)
- })
- }
- })
- // Reverse-authored seeds use a disjoint seed range: the two dumps' candidate models
- // are logically identical, so reusing 1..N would largely replay the forward streams.
- t.Run("reverse_80_authored", func(t *testing.T) {
- for seed := int64(101); seed < 101+entA8ReverseSeeds; seed++ {
- seed := seed
- t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
- entA8RunSeed(ctx, t, srv57, srv80, baseDDL, seed, true)
- })
- }
- })
-}
diff --git a/backend/plugin/schema/mysql/sdl_enterprise_fuzz_test.go b/backend/plugin/schema/mysql/sdl_enterprise_fuzz_test.go
deleted file mode 100644
index 7a0c567d34..0000000000
--- a/backend/plugin/schema/mysql/sdl_enterprise_fuzz_test.go
+++ /dev/null
@@ -1,927 +0,0 @@
-package mysql
-
-// A4 of the ENTERPRISE smoke axes (see sdl_enterprise_test.go): a SEEDED, deterministic
-// random-migration fuzzer over a 30-table Zabbix slice. Each seed derives K ∈ [3,8]
-// independent mutations from a menu (add/drop/modify column, add/drop index, add/drop FK
-// with valid targets only, add/drop/modify view, add/drop trigger, add/drop routine,
-// partition/departition), applies them TEXTUALLY to the canonical dump to build the
-// target SDL, and runs the oracle protocol (diff → apply → converge → idempotence).
-//
-// Determinism: the mutation stream is a pure function of the seed (math/rand with a
-// fixed source; the schema model is iterated in sorted order), so a failing seed replays
-// exactly. Every failure prints the seed and a ddmin-minimized mutation list, each
-// minimization trial re-running the full oracle on a fresh scratch database.
-//
-// ≥20 seeds run on 8.0 and ≥10 on 5.7 (the menu is 5.7-legal, so both use the same
-// generator). Scratch databases are entsdl_-prefixed and dropped eagerly per trial.
-
-import (
- "context"
- "fmt"
- "math/rand"
- "regexp"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/pkg/errors"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
-)
-
-// entFuzzSliceTables is the ~30-table fuzz base: the core slice plus the httptest family
-// (6 more triggered tables, FK-chained through httptest/httpstep into hosts and items)
-// and token (a 2-FK fan-in on users).
-var entFuzzSliceTables = append(append([]string{}, entSliceCoreTables...),
- "httptest", "httpstep", "httptestitem", "httpstepitem", "httptest_field", "httpstep_field",
- "token",
-)
-
-// entFuzzAux seeds the droppable/modifiable object pool the menu needs: two views, one
-// function, one procedure, and a partitioned table (the departition target).
-const entFuzzAux = `
-CREATE TABLE ent_fz_part (
- id bigint unsigned NOT NULL,
- bucket integer NOT NULL,
- note varchar(40) DEFAULT '' NOT NULL,
- PRIMARY KEY (id,bucket)
-) ENGINE=InnoDB
-PARTITION BY HASH (bucket) PARTITIONS 4;
-CREATE VIEW ent_fz_v1 AS SELECT userid, username FROM users;
-CREATE VIEW ent_fz_v2 AS SELECT hostid, host FROM hosts;
-CREATE FUNCTION ent_fz_fn1() RETURNS INT DETERMINISTIC RETURN 1;
-CREATE PROCEDURE ent_fz_pr1() BEGIN SELECT 1; END;
-`
-
-// entFzProtectedTables are never touched by column-level mutations: the changelog
-// trigger bodies name changelog's columns outright (not via NEW/OLD), and ent_fz_part
-// belongs to the partition mutations.
-var entFzProtectedTables = map[string]bool{"changelog": true, "ent_fz_part": true}
-
-// entFzProtectedColumns are referenced by the seeded views (or their modify-view
-// extension columns) and must survive column drops.
-var entFzProtectedColumns = map[string]bool{
- "users.username": true, "users.name": true,
- "hosts.host": true, "hosts.status": true,
-}
-
-// ----------------------------------------------------------------------------
-// Canonical-dump schema model (guides valid mutation choices; the mutations themselves
-// are textual).
-// ----------------------------------------------------------------------------
-
-type entFzIndex struct {
- name string
- cols []string
- unique bool
-}
-
-type entFzFK struct {
- name string
- cols []string
-}
-
-type entFzTable struct {
- name string
- cols []string // ordered
- colLine map[string]string // name -> trimmed definition line
- pk map[string]bool
- indexes []entFzIndex
- fks []entFzFK
- partitioned bool
-}
-
-type entFzModel struct {
- tables []*entFzTable // sorted by name
- byName map[string]*entFzTable
- referenced map[string]bool // tables referenced by any FK
- triggerCols map[string]map[string]bool // table -> NEW./OLD.-referenced columns
- triggers []string // sorted trigger names
- views []string // seeded, droppable views
- functions []string // seeded, droppable functions
- procedures []string // seeded, droppable procedures
-}
-
-var (
- entFzReColLine = regexp.MustCompile("^`(\\w+)` (.+?),?$")
- entFzRePK = regexp.MustCompile(`^PRIMARY KEY \((.+)\),?$`)
- entFzReIndex = regexp.MustCompile("^(UNIQUE )?KEY `(\\w+)` \\((.+)\\),?$")
- entFzReFK = regexp.MustCompile("^CONSTRAINT `(\\w+)` FOREIGN KEY \\((.+?)\\) REFERENCES `(\\w+)`")
- entFzReTrigger = regexp.MustCompile("(?i)^CREATE TRIGGER `(\\w+)`.* ON `(\\w+)`")
- entFzReNewOld = regexp.MustCompile(`(?i)\b(?:new|old)\.(\w+)`)
- entFzReCol = regexp.MustCompile("`(\\w+)`")
- entFzReVarLen = regexp.MustCompile(`varchar\((\d+)\)`)
-)
-
-// entFuzzModel parses the canonical dump into the mutation-planning model.
-func entFuzzModel(t *testing.T, source string) *entFzModel {
- t.Helper()
- stmts, err := mysqlparser.SplitSQL(source)
- require.NoError(t, err, "split canonical dump for fuzz model")
-
- m := &entFzModel{
- byName: map[string]*entFzTable{},
- referenced: map[string]bool{},
- triggerCols: map[string]map[string]bool{},
- }
- for _, s := range stmts {
- text := strings.TrimSpace(s.Text)
- upper := strings.ToUpper(text)
- switch {
- case strings.HasPrefix(upper, "CREATE TABLE `"):
- tbl := entFzParseTable(text)
- if tbl != nil {
- m.tables = append(m.tables, tbl)
- m.byName[tbl.name] = tbl
- }
- case strings.HasPrefix(upper, "CREATE TRIGGER `") || (strings.HasPrefix(upper, "CREATE ") && strings.Contains(upper, " TRIGGER `")):
- if mm := entFzReTrigger.FindStringSubmatch(text); mm != nil {
- m.triggers = append(m.triggers, mm[1])
- onTable := mm[2]
- if m.triggerCols[onTable] == nil {
- m.triggerCols[onTable] = map[string]bool{}
- }
- for _, ref := range entFzReNewOld.FindAllStringSubmatch(text, -1) {
- m.triggerCols[onTable][ref[1]] = true
- }
- }
- default:
- }
- }
- // FK-referenced tables (for partition candidacy).
- for _, s := range stmts {
- for _, mm := range entFzReFKRefs.FindAllStringSubmatch(s.Text, -1) {
- m.referenced[mm[1]] = true
- }
- }
- // Seeded droppable objects (fixed names — the fuzz base owns them).
- m.views = []string{"ent_fz_v1", "ent_fz_v2"}
- m.functions = []string{"ent_fz_fn1"}
- m.procedures = []string{"ent_fz_pr1"}
- return m
-}
-
-var entFzReFKRefs = regexp.MustCompile("FOREIGN KEY \\(.+?\\) REFERENCES `(\\w+)`")
-
-// entFzParseTable parses one canonical CREATE TABLE statement.
-func entFzParseTable(stmt string) *entFzTable {
- header := regexp.MustCompile("^CREATE TABLE `(\\w+)`").FindStringSubmatch(stmt)
- if header == nil {
- return nil
- }
- tbl := &entFzTable{
- name: header[1],
- colLine: map[string]string{},
- pk: map[string]bool{},
- partitioned: strings.Contains(strings.ToUpper(stmt), "PARTITION BY"),
- }
- for _, raw := range strings.Split(stmt, "\n")[1:] {
- line := strings.TrimSpace(raw)
- if line == "" || strings.HasPrefix(line, ")") {
- break
- }
- if mm := entFzRePK.FindStringSubmatch(line); mm != nil {
- for _, c := range entFzReCol.FindAllStringSubmatch(mm[1], -1) {
- tbl.pk[c[1]] = true
- }
- continue
- }
- if mm := entFzReIndex.FindStringSubmatch(line); mm != nil {
- idx := entFzIndex{name: mm[2], unique: mm[1] != ""}
- for _, c := range entFzReCol.FindAllStringSubmatch(mm[3], -1) {
- idx.cols = append(idx.cols, c[1])
- }
- tbl.indexes = append(tbl.indexes, idx)
- continue
- }
- if mm := entFzReFK.FindStringSubmatch(line); mm != nil {
- fk := entFzFK{name: mm[1]}
- for _, c := range entFzReCol.FindAllStringSubmatch(mm[2], -1) {
- fk.cols = append(fk.cols, c[1])
- }
- tbl.fks = append(tbl.fks, fk)
- continue
- }
- if strings.HasPrefix(line, "CONSTRAINT `") || strings.HasPrefix(line, "FULLTEXT KEY") {
- continue // CHECK constraints / fulltext — not mutation targets
- }
- if mm := entFzReColLine.FindStringSubmatch(line); mm != nil {
- tbl.cols = append(tbl.cols, mm[1])
- tbl.colLine[mm[1]] = strings.TrimSuffix(line, ",")
- }
- }
- return tbl
-}
-
-// entFzIndexedCols returns every column participating in any index or FK of tbl.
-func entFzIndexedCols(tbl *entFzTable) map[string]bool {
- used := map[string]bool{}
- for c := range tbl.pk {
- used[c] = true
- }
- for _, idx := range tbl.indexes {
- for _, c := range idx.cols {
- used[c] = true
- }
- }
- for _, fk := range tbl.fks {
- for _, c := range fk.cols {
- used[c] = true
- }
- }
- return used
-}
-
-// ----------------------------------------------------------------------------
-// Mutation generation.
-// ----------------------------------------------------------------------------
-
-// entFzMutation is one textual schema mutation with a replayable description.
-type entFzMutation struct {
- desc string
- apply func(t *testing.T, source string) string
-}
-
-// entFzState tracks per-round conflicts so the K mutations stay independent.
-type entFzState struct {
- usedCols map[string]bool // "table.col" touched by drop/modify
- usedIndexes map[string]bool // "table.index" dropped
- usedFKs map[string]bool
- usedViews map[string]bool
- usedTriggers map[string]bool
- usedRoutines map[string]bool
- lockedTables map[string]bool // partition/departition/fk-structure locks
-}
-
-func entFzNewState() *entFzState {
- return &entFzState{
- usedCols: map[string]bool{},
- usedIndexes: map[string]bool{},
- usedFKs: map[string]bool{},
- usedViews: map[string]bool{},
- usedTriggers: map[string]bool{},
- usedRoutines: map[string]bool{},
- lockedTables: map[string]bool{},
- }
-}
-
-var entFzMenu = []string{
- "add_column", "drop_column", "modify_column",
- "add_index", "drop_index",
- "add_fk", "drop_fk",
- "add_view", "drop_view", "modify_view",
- "add_trigger", "drop_trigger",
- "add_routine", "drop_routine",
- "partition", "departition",
-}
-
-// entFuzzMutations derives k independent mutations from the model, deterministically in
-// (model order, rng stream).
-func entFuzzMutations(t *testing.T, model *entFzModel, rng *rand.Rand, k int) []entFzMutation {
- t.Helper()
- state := entFzNewState()
- var muts []entFzMutation
- for attempt := 0; attempt < 400 && len(muts) < k; attempt++ {
- kind := entFzMenu[rng.Intn(len(entFzMenu))]
- seq := len(muts)
- if m, ok := entFzGenerate(kind, model, rng, state, seq); ok {
- muts = append(muts, m)
- }
- }
- require.Len(t, muts, k, "fuzz generator starved (only %d of %d mutations)", len(muts), k)
- return muts
-}
-
-// entFzPickTable picks a random mutable (non-protected, non-locked) table.
-func entFzPickTable(model *entFzModel, rng *rand.Rand, state *entFzState) *entFzTable {
- var candidates []*entFzTable
- for _, tbl := range model.tables {
- if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
- continue
- }
- candidates = append(candidates, tbl)
- }
- if len(candidates) == 0 {
- return nil
- }
- return candidates[rng.Intn(len(candidates))]
-}
-
-// entFzGenerate builds one mutation of the given kind, or reports it infeasible.
-func entFzGenerate(kind string, model *entFzModel, rng *rand.Rand, state *entFzState, seq int) (entFzMutation, bool) {
- switch kind {
- case "add_column":
- tbl := entFzPickTable(model, rng, state)
- if tbl == nil {
- return entFzMutation{}, false
- }
- defs := []string{"varchar(40) DEFAULT NULL", "int NOT NULL DEFAULT '0'", "decimal(8,2) DEFAULT NULL", "bigint unsigned DEFAULT NULL"}
- def := defs[rng.Intn(len(defs))]
- col := fmt.Sprintf("ent_fz_c%d", seq)
- table := tbl.name
- return entFzMutation{
- desc: fmt.Sprintf("add_column %s.%s (%s)", table, col, def),
- apply: func(t *testing.T, source string) string {
- return addColumnToTable(t, source, table, "`"+col+"` "+def)
- },
- }, true
-
- case "drop_column":
- type cand struct{ table, col string }
- var cands []cand
- for _, tbl := range model.tables {
- if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
- continue
- }
- indexed := entFzIndexedCols(tbl)
- trigRef := model.triggerCols[tbl.name]
- for _, c := range tbl.cols {
- if indexed[c] || trigRef[c] || entFzProtectedColumns[tbl.name+"."+c] {
- continue
- }
- if state.usedCols[tbl.name+"."+c] {
- continue
- }
- if strings.Contains(tbl.colLine[c], "GENERATED") {
- continue
- }
- cands = append(cands, cand{tbl.name, c})
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.col] = true
- return entFzMutation{
- desc: fmt.Sprintf("drop_column %s.%s", pick.table, pick.col),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "`"+pick.col+"`")
- },
- }, true
-
- case "modify_column":
- type cand struct {
- table, col string
- from, to int
- }
- var cands []cand
- for _, tbl := range model.tables {
- if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
- continue
- }
- for _, c := range tbl.cols {
- if state.usedCols[tbl.name+"."+c] {
- continue
- }
- mm := entFzReVarLen.FindStringSubmatch(tbl.colLine[c])
- if mm == nil {
- continue
- }
- n := entFzAtoi(mm[1])
- if n < 8 || n > 150 {
- continue
- }
- cands = append(cands, cand{tbl.name, c, n, n + 37})
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.col] = true
- return entFzMutation{
- desc: fmt.Sprintf("modify_column widen %s.%s varchar(%d)->varchar(%d)", pick.table, pick.col, pick.from, pick.to),
- apply: func(t *testing.T, source string) string {
- return entReplaceInTable(t, source, pick.table,
- fmt.Sprintf("`%s` varchar(%d)", pick.col, pick.from),
- fmt.Sprintf("`%s` varchar(%d)", pick.col, pick.to))
- },
- }, true
-
- case "add_index":
- type cand struct{ table, col string }
- var cands []cand
- for _, tbl := range model.tables {
- if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
- continue
- }
- for _, c := range tbl.cols {
- line := tbl.colLine[c]
- rest := strings.TrimPrefix(line, "`"+c+"` ")
- ok := strings.HasPrefix(rest, "bigint") || strings.HasPrefix(rest, "int") || strings.HasPrefix(rest, "integer")
- if mm := entFzReVarLen.FindStringSubmatch(rest); mm != nil && strings.HasPrefix(rest, "varchar") {
- ok = entFzAtoi(mm[1]) <= 150
- }
- if ok {
- cands = append(cands, cand{tbl.name, c})
- }
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- idx := fmt.Sprintf("ent_fz_i%d", seq)
- return entFzMutation{
- desc: fmt.Sprintf("add_index %s on %s(%s)", idx, pick.table, pick.col),
- apply: func(t *testing.T, source string) string {
- return addIndexToTable(t, source, pick.table, "KEY `"+idx+"` (`"+pick.col+"`)")
- },
- }, true
-
- case "drop_index":
- type cand struct{ table, index string }
- var cands []cand
- for _, tbl := range model.tables {
- if state.lockedTables[tbl.name] {
- continue
- }
- fkCols := map[string]bool{}
- for _, fk := range tbl.fks {
- for _, c := range fk.cols {
- fkCols[c] = true
- }
- }
- for _, idx := range tbl.indexes {
- if state.usedIndexes[tbl.name+"."+idx.name] {
- continue
- }
- overlap := false
- for _, c := range idx.cols {
- if fkCols[c] {
- overlap = true
- break
- }
- }
- if !overlap {
- cands = append(cands, cand{tbl.name, idx.name})
- }
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedIndexes[pick.table+"."+pick.index] = true
- return entFzMutation{
- desc: fmt.Sprintf("drop_index %s.%s", pick.table, pick.index),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "`"+pick.index+"`")
- },
- }, true
-
- case "add_fk":
- src := entFzPickTable(model, rng, state)
- if src == nil || src.partitioned {
- return entFzMutation{}, false
- }
- var refs []*entFzTable
- for _, tbl := range model.tables {
- if tbl.partitioned || state.lockedTables[tbl.name] || len(tbl.pk) != 1 {
- continue
- }
- pkCol := ""
- for c := range tbl.pk {
- pkCol = c
- }
- if strings.HasPrefix(strings.TrimPrefix(tbl.colLine[pkCol], "`"+pkCol+"` "), "bigint") &&
- strings.Contains(tbl.colLine[pkCol], "unsigned") {
- refs = append(refs, tbl)
- }
- }
- if len(refs) == 0 {
- return entFzMutation{}, false
- }
- ref := refs[rng.Intn(len(refs))]
- refPK := ""
- for c := range ref.pk {
- refPK = c
- }
- srcName, refName := src.name, ref.name
- state.lockedTables[srcName] = true
- state.lockedTables[refName] = true
- col := fmt.Sprintf("ent_fz_r%d", seq)
- fk := fmt.Sprintf("ent_fz_fk%d", seq)
- return entFzMutation{
- desc: fmt.Sprintf("add_fk %s.%s -> %s.%s (%s)", srcName, col, refName, refPK, fk),
- apply: func(t *testing.T, source string) string {
- s := addColumnToTable(t, source, srcName, "`"+col+"` bigint unsigned DEFAULT NULL")
- s = addIndexToTable(t, s, srcName, "KEY `"+fk+"` (`"+col+"`)")
- return addIndexToTable(t, s, srcName,
- "CONSTRAINT `"+fk+"` FOREIGN KEY (`"+col+"`) REFERENCES `"+refName+"` (`"+refPK+"`) ON DELETE SET NULL")
- },
- }, true
-
- case "drop_fk":
- type cand struct{ table, fk string }
- var cands []cand
- for _, tbl := range model.tables {
- if state.lockedTables[tbl.name] {
- continue
- }
- for _, fk := range tbl.fks {
- if !state.usedFKs[tbl.name+"."+fk.name] {
- cands = append(cands, cand{tbl.name, fk.name})
- }
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedFKs[pick.table+"."+pick.fk] = true
- return entFzMutation{
- desc: fmt.Sprintf("drop_fk %s.%s", pick.table, pick.fk),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "CONSTRAINT `"+pick.fk+"`")
- },
- }, true
-
- case "add_view":
- tbl := entFzPickTable(model, rng, state)
- if tbl == nil || len(tbl.pk) == 0 {
- return entFzMutation{}, false
- }
- pkCol := ""
- for _, c := range tbl.cols {
- if tbl.pk[c] {
- pkCol = c
- break
- }
- }
- if pkCol == "" {
- return entFzMutation{}, false
- }
- name := fmt.Sprintf("ent_fz_v%d", 10+seq)
- table := tbl.name
- return entFzMutation{
- desc: fmt.Sprintf("add_view %s over %s(%s)", name, table, pkCol),
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE VIEW %s AS SELECT %s FROM %s;\n", name, pkCol, table)
- },
- }, true
-
- case "drop_view":
- var cands []string
- for _, v := range model.views {
- if !state.usedViews[v] {
- cands = append(cands, v)
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedViews[pick] = true
- return entFzMutation{
- desc: "drop_view " + pick,
- apply: func(t *testing.T, source string) string {
- return dropObjectBlock(t, source, "VIEW", pick)
- },
- }, true
-
- case "modify_view":
- extension := map[string]string{
- "ent_fz_v1": "`users`.`name` AS `name`",
- "ent_fz_v2": "`hosts`.`status` AS `status`",
- }
- var cands []string
- for _, v := range model.views {
- if !state.usedViews[v] && extension[v] != "" {
- cands = append(cands, v)
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedViews[pick] = true
- ext := extension[pick]
- return entFzMutation{
- desc: "modify_view " + pick,
- apply: func(t *testing.T, source string) string {
- return addColumnToView(t, source, pick, ext)
- },
- }, true
-
- case "add_trigger":
- tbl := entFzPickTable(model, rng, state)
- if tbl == nil || len(tbl.pk) == 0 {
- return entFzMutation{}, false
- }
- pkCol := ""
- for _, c := range tbl.cols {
- if tbl.pk[c] {
- pkCol = c
- break
- }
- }
- if pkCol == "" {
- return entFzMutation{}, false
- }
- name := fmt.Sprintf("ent_fz_t%d", seq)
- table := tbl.name
- return entFzMutation{
- desc: fmt.Sprintf("add_trigger %s on %s", name, table),
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE TRIGGER %s BEFORE UPDATE ON %s FOR EACH ROW SET NEW.%s = NEW.%s;\n",
- name, table, pkCol, pkCol)
- },
- }, true
-
- case "drop_trigger":
- var cands []string
- for _, trg := range model.triggers {
- if !state.usedTriggers[trg] {
- cands = append(cands, trg)
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedTriggers[pick] = true
- return entFzMutation{
- desc: "drop_trigger " + pick,
- apply: func(t *testing.T, source string) string {
- return dropTrigger(t, source, pick)
- },
- }, true
-
- case "add_routine":
- if rng.Intn(2) == 0 {
- name := fmt.Sprintf("ent_fz_fn%d", 10+seq)
- ret := seq + 100
- return entFzMutation{
- desc: "add_routine function " + name,
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE FUNCTION %s() RETURNS INT DETERMINISTIC RETURN %d;\n", name, ret)
- },
- }, true
- }
- name := fmt.Sprintf("ent_fz_pr%d", 10+seq)
- sel := seq + 200
- return entFzMutation{
- desc: "add_routine procedure " + name,
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE PROCEDURE %s() BEGIN SELECT %d; END;\n", name, sel)
- },
- }, true
-
- case "drop_routine":
- type cand struct{ kind, name string }
- var cands []cand
- for _, fn := range model.functions {
- if !state.usedRoutines[fn] {
- cands = append(cands, cand{"FUNCTION", fn})
- }
- }
- for _, pr := range model.procedures {
- if !state.usedRoutines[pr] {
- cands = append(cands, cand{"PROCEDURE", pr})
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedRoutines[pick.name] = true
- return entFzMutation{
- desc: "drop_routine " + strings.ToLower(pick.kind) + " " + pick.name,
- apply: func(t *testing.T, source string) string {
- return dropObjectBlock(t, source, pick.kind, pick.name)
- },
- }, true
-
- case "partition":
- var cands []*entFzTable
- for _, tbl := range model.tables {
- // changelog is column-protected but IS the partition candidate (the only
- // FK-free, unreferenced, single-PK table in the slice); ent_fz_part is
- // already partitioned.
- if tbl.partitioned || state.lockedTables[tbl.name] {
- continue
- }
- if len(tbl.fks) > 0 || model.referenced[tbl.name] || len(tbl.pk) != 1 {
- continue
- }
- // The partition key must be part of every unique key: single-col-PK tables
- // with no other UNIQUE index qualify.
- hasUnique := false
- for _, idx := range tbl.indexes {
- if idx.unique {
- hasUnique = true
- break
- }
- }
- if hasUnique {
- continue
- }
- cands = append(cands, tbl)
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- pkCol := ""
- for c := range pick.pk {
- pkCol = c
- }
- state.lockedTables[pick.name] = true
- table := pick.name
- return entFzMutation{
- desc: fmt.Sprintf("partition %s by hash(%s)", table, pkCol),
- apply: func(t *testing.T, source string) string {
- return entAppendTableClause(t, source, table, "PARTITION BY HASH (`"+pkCol+"`)\nPARTITIONS 4")
- },
- }, true
-
- case "departition":
- var cands []*entFzTable
- for _, tbl := range model.tables {
- if tbl.partitioned && !state.lockedTables[tbl.name] {
- cands = append(cands, tbl)
- }
- }
- if len(cands) == 0 {
- return entFzMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.lockedTables[pick.name] = true
- table := pick.name
- return entFzMutation{
- desc: "departition " + table,
- apply: func(t *testing.T, source string) string {
- return entSetPartitionClause(t, source, table, "")
- },
- }, true
-
- default:
- return entFzMutation{}, false
- }
-}
-
-// entFzAtoi is a no-error atoi for regex-captured digits.
-func entFzAtoi(s string) int {
- n := 0
- for _, r := range s {
- n = n*10 + int(r-'0')
- }
- return n
-}
-
-// ----------------------------------------------------------------------------
-// Trial runner + ddmin minimization.
-// ----------------------------------------------------------------------------
-
-// entFuzzScratchDB creates an eagerly-droppable scratch database (fuzz + minimization
-// churn through many databases inside a single test, so cleanup cannot wait for test
-// end).
-func entFuzzScratchDB(ctx context.Context, t *testing.T, srv liveServer) (string, func()) {
- t.Helper()
- dbName := newLiveDatabase(ctx, t, srv, "entsdl_fz")
- drop := func() {
- c, err := createLiveMySQLDriver(ctx, srv, "")
- if err != nil {
- return
- }
- defer c.Close(ctx)
- _, _ = c.Execute(ctx, "DROP DATABASE IF EXISTS `"+dbName+"`", db.ExecuteOptions{})
- }
- return dbName, drop
-}
-
-// entFuzzTrial runs the whole oracle protocol for one mutation set on a fresh scratch
-// database. Infrastructure failures (server down, base DDL broken, mutation anchors
-// missing) fail the test hard; ORACLE violations (diff error, apply error, residual
-// after apply, non-idempotent dump) are returned as errors so the caller can minimize.
-func entFuzzTrial(ctx context.Context, t *testing.T, srv liveServer, baseDDL string, muts []entFzMutation) error {
- t.Helper()
- dbName, drop := entFuzzScratchDB(ctx, t, srv)
- defer drop()
-
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- _, err = driver.Execute(ctx, entNormalizeDelimiters(baseDDL), db.ExecuteOptions{})
- driver.Close(ctx)
- require.NoError(t, err, "[%s] fuzz base failed to load", srv.name)
-
- source := dumpSDL(ctx, t, srv, dbName)
- target := source
- for _, m := range muts {
- target = m.apply(t, target)
- }
-
- plan, err := mysqlDiffSDLMigration(source, target, srv.version)
- if err != nil {
- return errors.Wrap(err, "diff failed")
- }
- if strings.TrimSpace(plan) == "" {
- return errors.New("empty plan for a non-empty mutation set")
- }
- if err := applyDDL(ctx, t, srv, dbName, plan); err != nil {
- return errors.Wrapf(err, "plan failed to apply; plan was:\n%s", plan)
- }
- after := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(after, target, srv.version)
- if err != nil {
- return errors.Wrap(err, "converge diff failed")
- }
- if converge != "" {
- return errors.Errorf("did not converge; residual:\n%s\nplan was:\n%s", converge, plan)
- }
- self, err := mysqlDiffSDLMigration(after, after, srv.version)
- if err != nil {
- return errors.Wrap(err, "idempotence diff failed")
- }
- if self != "" {
- return errors.Errorf("post-apply dump not idempotent:\n%s", self)
- }
- return nil
-}
-
-// entFuzzMinimize greedily shrinks a failing mutation set (ddmin-lite: drop one mutation
-// at a time, keep the removal whenever the rest still fails). Returns the minimized set
-// and its error.
-func entFuzzMinimize(ctx context.Context, t *testing.T, srv liveServer, baseDDL string, muts []entFzMutation, firstErr error) ([]entFzMutation, error) {
- t.Helper()
- minimized := muts
- lastErr := firstErr
- for i := 0; i < len(minimized) && len(minimized) > 1; {
- trial := make([]entFzMutation, 0, len(minimized)-1)
- trial = append(trial, minimized[:i]...)
- trial = append(trial, minimized[i+1:]...)
- if err := entFuzzTrial(ctx, t, srv, baseDDL, trial); err != nil {
- minimized = trial
- lastErr = err
- continue // same index now names the next mutation
- }
- i++
- }
- return minimized, lastErr
-}
-
-func entFzDescs(muts []entFzMutation) []string {
- out := make([]string, len(muts))
- for i, m := range muts {
- out[i] = m.desc
- }
- return out
-}
-
-// ----------------------------------------------------------------------------
-// The fuzz test: ≥20 seeds on 8.0, ≥10 on 5.7 (deterministic; the seed is in the
-// subtest name).
-// ----------------------------------------------------------------------------
-
-//nolint:tparallel
-func TestSDLEnterpriseFuzz(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- seedCount := 20
- if srv.version == "5.7" {
- seedCount = 10
- }
- t.Run(srv.name, func(t *testing.T) {
- baseDDL := entZabbixSlice(t, entFuzzSliceTables...) + entFuzzAux
-
- // The model is built once per server from a reference load (the canonical dump
- // is deterministic, so it matches every trial's source).
- refDB, refDrop := entFuzzScratchDB(ctx, t, srv)
- driver, err := createLiveMySQLDriver(ctx, srv, refDB)
- require.NoError(t, err)
- _, err = driver.Execute(ctx, entNormalizeDelimiters(baseDDL), db.ExecuteOptions{})
- driver.Close(ctx)
- require.NoError(t, err, "[%s] fuzz base failed to load", srv.name)
- refSource := dumpSDL(ctx, t, srv, refDB)
- refDrop()
- model := entFuzzModel(t, refSource)
- require.NotEmpty(t, model.tables, "fuzz model parsed no tables")
- require.NotEmpty(t, model.triggers, "fuzz model parsed no triggers")
-
- for seed := int64(1); seed <= int64(seedCount); seed++ {
- seed := seed
- t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
- rng := rand.New(rand.NewSource(seed)) //nolint:gosec // deterministic fuzz, not crypto
- k := 3 + rng.Intn(6) // K ∈ [3,8]
- muts := entFuzzMutations(t, model, rng, k)
- t.Logf("[FUZZ %s seed=%d] K=%d mutations:\n %s", srv.name, seed, k, strings.Join(entFzDescs(muts), "\n "))
-
- err := entFuzzTrial(ctx, t, srv, baseDDL, muts)
- if err == nil {
- return
- }
- minimized, minErr := entFuzzMinimize(ctx, t, srv, baseDDL, muts, err)
- t.Errorf("[FUZZ %s seed=%d] FAILED\nminimized mutations (%d of %d):\n %s\nerror:\n%v",
- srv.name, seed, len(minimized), len(muts), strings.Join(entFzDescs(minimized), "\n "), minErr)
- })
- }
- })
- }
-}
diff --git a/backend/plugin/schema/mysql/sdl_enterprise_statefuzz_test.go b/backend/plugin/schema/mysql/sdl_enterprise_statefuzz_test.go
deleted file mode 100644
index 259f7ec118..0000000000
--- a/backend/plugin/schema/mysql/sdl_enterprise_statefuzz_test.go
+++ /dev/null
@@ -1,1584 +0,0 @@
-package mysql
-
-// A6 of the ENTERPRISE smoke axes (see sdl_enterprise_test.go): a SEEDED, deterministic
-// MULTI-ROUND STATEFUL fuzzer. The single-round fuzzer (A4, sdl_enterprise_fuzz_test.go)
-// mutates a pristine dump once; real projects mutate the LAST dump repeatedly, so drift
-// and text-accretion bugs that only appear across dump->mutate->apply->dump cycles are
-// invisible to it (real example this campaign: omni used to accrete one paren layer per
-// dump cycle around subquery wrappers until the fold fixed it — a per-round differ never
-// sees the growth).
-//
-// Protocol per seed, on a 30-table Zabbix slice base:
-//
-// dump_0 = canonical dump of the freshly loaded base
-// for round r in 1..5:
-// target_r = mutate(dump_{r-1}, K ∈ [2,5] mutations from the menu)
-// oracle = diff(dump_{r-1} -> target_r) → apply → converge → idempotence
-// dump_r = fresh canonical dump, and ADDITIONALLY:
-// (a) dump_r reloads through the STRICT LoadSDL path (no LoadSQL fallback mask),
-// (b) len(dump_r) stays under len(dump_0) + the cumulative added-object budget
-// (linear ledger — catches unbounded text accretion), and every statement NOT
-// touched by this round's mutations is BYTE-IDENTICAL to its dump_{r-1} form
-// (catches per-cycle accretion and collateral churn on untouched objects),
-// (c) object counts (tables/views/routines/triggers/columns/indexes/FKs/checks)
-// match the mutation ledger exactly.
-//
-// The menu is the A4 menu PLUS (8.0-only where marked): add/drop CHECK constraint (8.0),
-// add/drop FULLTEXT index, add/drop SPATIAL column+index with SRID (8.0 — the SRID
-// attribute is 8.0-only), add/modify/drop STORED and VIRTUAL generated columns, toggle
-// column/index INVISIBLE (8.0), and ENUM member append. 5.7-illegal mutations are gated
-// off the 5.7 menu.
-//
-// Determinism: the mutation stream is a pure function of the seed (the seed is in the
-// subtest name); the schema model is parsed from the previous round's dump in statement
-// order, so candidate lists are stable. Oracle violations are ddmin-minimized (one
-// mutation at a time, each trial reloading dump_{r-1} into a fresh scratch database) and
-// reported with seed + round + minimized ledger. Scratch databases are entsdl_-prefixed
-// and dropped eagerly.
-//
-// ≥12 seeds run on 8.0 and ≥6 on 5.7.
-//
-// Shared helpers (liveServers, createLiveMySQLDriver, newLiveDatabase, dumpSDL, applyDDL,
-// syncMetaForDB, objectCounts, addColumnToTable, addIndexToTable, addColumnToView,
-// dropObjectBlock, dropTrigger, entZabbixSlice, entFuzzSliceTables, entFuzzAux,
-// entNormalizeDelimiters, entDropLineInTable, entReplaceLineInTable, entAppendTableClause,
-// entSetPartitionClause, entFzProtectedTables, entFzProtectedColumns, entFuzzModel,
-// entFzIndexedCols, entFzState, entFzMutation, withDatabaseContext, mysqlVersionFor) come
-// from the sibling files in this package.
-
-import (
- "context"
- "fmt"
- "math/rand"
- "regexp"
- "slices"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/pkg/errors"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/omni/mysql/catalog"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-const (
- entSfRounds = 5
- // entSfRoundSlack is the per-round allowance on top of the per-mutation text budgets
- // (absorbs benign re-rendering, e.g. a DEFAULT NULL a fresh dump makes explicit).
- // Deliberately small so systematic accretion across 30+ objects trips the guard.
- entSfRoundSlack = 400
-)
-
-// ----------------------------------------------------------------------------
-// Stateful-fuzz aux schema: seeds the droppable/modifiable object pool for the NEW menu
-// kinds. entSfAuxCommon is 5.7-legal; entSfAux80 seeds the 8.0-only kinds (SRID spatial,
-// INVISIBLE, CHECK) and is appended on 8.0 only.
-// ----------------------------------------------------------------------------
-
-const entSfAuxCommon = `
-CREATE TABLE ent_sf_enum (
- id bigint unsigned NOT NULL,
- mood enum('sunny','cloudy') NOT NULL DEFAULT 'sunny',
- grade enum('a','b','c') NOT NULL DEFAULT 'a',
- PRIMARY KEY (id)
-) ENGINE=InnoDB;
-CREATE TABLE ent_sf_text (
- id bigint unsigned NOT NULL,
- title varchar(120) NOT NULL DEFAULT '',
- body text,
- summary text,
- PRIMARY KEY (id),
- FULLTEXT KEY ent_sf_ft_seed (body)
-) ENGINE=InnoDB;
-CREATE TABLE ent_sf_gen (
- id bigint unsigned NOT NULL,
- price decimal(8,2) NOT NULL DEFAULT '0.00',
- qty int NOT NULL DEFAULT '0',
- total decimal(10,2) GENERATED ALWAYS AS ((price * 2)) STORED,
- total2 bigint GENERATED ALWAYS AS ((qty + 7)) VIRTUAL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB;
-`
-
-const entSfAux80 = `
-CREATE TABLE ent_sf_geo (
- id bigint unsigned NOT NULL,
- pt point NOT NULL SRID 4326,
- PRIMARY KEY (id),
- SPATIAL KEY ent_sf_sp_seed (pt)
-) ENGINE=InnoDB;
-CREATE TABLE ent_sf_vis (
- id bigint unsigned NOT NULL,
- visa int NOT NULL DEFAULT '0',
- visb int INVISIBLE NOT NULL DEFAULT '0',
- visc int NOT NULL DEFAULT '0',
- PRIMARY KEY (id),
- KEY ent_sf_ki_a (visa) INVISIBLE,
- KEY ent_sf_ki_b (visc)
-) ENGINE=InnoDB;
-CREATE TABLE ent_sf_chk (
- id bigint unsigned NOT NULL,
- score int NOT NULL DEFAULT '0',
- bound int NOT NULL DEFAULT '10',
- PRIMARY KEY (id),
- CONSTRAINT ent_sf_chk_seed CHECK ((score >= 0))
-) ENGINE=InnoDB;
-`
-
-func entSfBaseDDL(t *testing.T, srv liveServer) string {
- t.Helper()
- base := entZabbixSlice(t, entFuzzSliceTables...) + entFuzzAux + entSfAuxCommon
- if srv.version != "5.7" {
- base += entSfAux80
- }
- return base
-}
-
-// ----------------------------------------------------------------------------
-// Object-count ledger.
-// ----------------------------------------------------------------------------
-
-// entSfCounts is the schema-shape summary compared against the mutation ledger after
-// every round. Comparable (all ints) so `!=` works.
-type entSfCounts struct {
- tables, views, functions, procedures, triggers int
- columns, indexes, fks, checks int
-}
-
-func (c entSfCounts) plus(d entSfCounts) entSfCounts {
- return entSfCounts{
- tables: c.tables + d.tables, views: c.views + d.views,
- functions: c.functions + d.functions, procedures: c.procedures + d.procedures,
- triggers: c.triggers + d.triggers, columns: c.columns + d.columns,
- indexes: c.indexes + d.indexes, fks: c.fks + d.fks, checks: c.checks + d.checks,
- }
-}
-
-func entSfCountObjects(meta *model.DatabaseMetadata) entSfCounts {
- c := entSfCounts{}
- c.tables, c.views, c.functions, c.procedures, c.triggers = objectCounts(meta)
- proto := meta.GetProto()
- if proto == nil {
- return c
- }
- for _, sm := range proto.GetSchemas() {
- for _, tbl := range sm.GetTables() {
- c.columns += len(tbl.GetColumns())
- c.indexes += len(tbl.GetIndexes())
- c.fks += len(tbl.GetForeignKeys())
- c.checks += len(tbl.GetCheckConstraints())
- }
- }
- return c
-}
-
-// entSfMutation wraps the replayable A4 mutation with the ledger metadata the stateful
-// protocol needs: the expected object-count delta, the dump-text growth budget, and the
-// statement keys the mutation may legitimately rewrite (everything else must stay
-// byte-identical across the round).
-type entSfMutation struct {
- entFzMutation
- delta entSfCounts
- budget int
- touched []string
-}
-
-// entSfLedger is the cross-round state: expected counts, and the once-only guards for
-// mutations that cannot repeat on the same object across rounds.
-type entSfLedger struct {
- expect entSfCounts
- viewModified map[string]bool
-}
-
-// ----------------------------------------------------------------------------
-// Extended schema model: everything the A4 model parses, plus the constructs the new
-// menu mutates (FULLTEXT/SPATIAL keys, CHECK constraints, generated columns, INVISIBLE
-// columns/indexes, ENUM columns) and the referential protections they imply.
-// ----------------------------------------------------------------------------
-
-type entSfIdxRec struct {
- table, name, line string
- cols []string
- invisible bool
- fkOverlap bool
-}
-
-type entSfNamedRec struct{ table, name string }
-
-type entSfColRec struct{ table, col string }
-
-type entSfGenRec struct{ table, name, line string }
-
-type entSfSchema struct {
- base *entFzModel
- views []string
- functions []string
- procedures []string
- idx []entSfIdxRec // non-PK B-tree indexes, visible AND invisible
- fulltext []entSfNamedRec
- spatial []entSfIdxRec // cols[0] is the single spatial column
- checks []entSfNamedRec
- genCols []entSfGenRec
- enumCols []entSfColRec
- invisCols []entSfColRec
- // visibleCols counts non-INVISIBLE columns per table (a toggle must leave >= 1).
- visibleCols map[string]int
- // protected are "table.col" keys drop/modify must not touch beyond the base model's
- // own exclusions: fulltext/spatial/invisible-index parts, generated-expression and
- // CHECK-expression references.
- protected map[string]bool
- // noPartition marks tables that cannot gain a partition clause (FULLTEXT/SPATIAL
- // indexes are unsupported on partitioned tables).
- noPartition map[string]bool
-}
-
-var (
- entSfReIdxLine = regexp.MustCompile("^(UNIQUE )?KEY `(\\w+)` \\((.+?)\\)( /\\*!80000 INVISIBLE \\*/)?,?$")
- entSfReFtLine = regexp.MustCompile("^FULLTEXT KEY `(\\w+)` \\((.+?)\\),?$")
- entSfReSpLine = regexp.MustCompile("^SPATIAL KEY `(\\w+)` \\(`(\\w+)`\\),?$")
- entSfReChkLine = regexp.MustCompile("^CONSTRAINT `(\\w+)` CHECK \\((.+)\\),?$")
- entSfReColLine = regexp.MustCompile("^`(\\w+)` (.+?),?$")
- entSfReViewHdr = regexp.MustCompile("(?i)^CREATE .*?VIEW `(\\w+)`")
- entSfReFnHdr = regexp.MustCompile("(?i)^CREATE .*?FUNCTION `(\\w+)`")
- entSfRePrHdr = regexp.MustCompile("(?i)^CREATE .*?PROCEDURE `(\\w+)`")
- entSfReGenConst = regexp.MustCompile(`\+ (\d+)\)`)
- entSfReEnumSpec = regexp.MustCompile(`enum\(([^)]*)\)`)
-)
-
-const (
- entSfInvisibleColToken = " /*!80023 INVISIBLE */"
- entSfInvisibleIdxToken = " /*!80000 INVISIBLE */"
-)
-
-// entSfParse builds the extended model from a canonical dump.
-func entSfParse(t *testing.T, source string) *entSfSchema {
- t.Helper()
- m := &entSfSchema{
- base: entFuzzModel(t, source),
- visibleCols: map[string]int{},
- protected: map[string]bool{},
- noPartition: map[string]bool{},
- }
- stmts, err := mysqlparser.SplitSQL(source)
- require.NoError(t, err, "split canonical dump for stateful-fuzz model")
- for _, s := range stmts {
- text := strings.TrimSpace(s.Text)
- upper := strings.ToUpper(text)
- switch {
- case strings.HasPrefix(upper, "CREATE TABLE `"):
- m.parseTableExtras(text)
- case entSfReViewHdr.MatchString(text):
- m.views = append(m.views, entSfReViewHdr.FindStringSubmatch(text)[1])
- case entSfReFnHdr.MatchString(text):
- m.functions = append(m.functions, entSfReFnHdr.FindStringSubmatch(text)[1])
- case entSfRePrHdr.MatchString(text):
- m.procedures = append(m.procedures, entSfRePrHdr.FindStringSubmatch(text)[1])
- default:
- }
- }
- return m
-}
-
-func (m *entSfSchema) parseTableExtras(stmt string) {
- header := entReTable.FindStringSubmatch(stmt)
- if header == nil {
- return
- }
- table := header[1]
- fkCols := map[string]bool{}
- if tbl := m.base.byName[table]; tbl != nil {
- for _, fk := range tbl.fks {
- for _, c := range fk.cols {
- fkCols[c] = true
- }
- }
- }
- for _, raw := range strings.Split(stmt, "\n")[1:] {
- line := strings.TrimSpace(raw)
- if line == "" || strings.HasPrefix(line, ")") {
- break
- }
- if mm := entSfReFtLine.FindStringSubmatch(line); mm != nil {
- m.fulltext = append(m.fulltext, entSfNamedRec{table: table, name: mm[1]})
- m.noPartition[table] = true
- for _, c := range entFzReCol.FindAllStringSubmatch(mm[2], -1) {
- m.protected[table+"."+c[1]] = true
- }
- continue
- }
- if mm := entSfReSpLine.FindStringSubmatch(line); mm != nil {
- m.spatial = append(m.spatial, entSfIdxRec{table: table, name: mm[1], cols: []string{mm[2]}})
- m.noPartition[table] = true
- m.protected[table+"."+mm[2]] = true
- continue
- }
- if mm := entSfReChkLine.FindStringSubmatch(line); mm != nil {
- m.checks = append(m.checks, entSfNamedRec{table: table, name: mm[1]})
- for _, c := range entFzReCol.FindAllStringSubmatch(mm[2], -1) {
- m.protected[table+"."+c[1]] = true
- }
- continue
- }
- if mm := entSfReIdxLine.FindStringSubmatch(line); mm != nil {
- rec := entSfIdxRec{table: table, name: mm[2], line: strings.TrimSuffix(line, ","), invisible: mm[4] != ""}
- for _, c := range entFzReCol.FindAllStringSubmatch(mm[3], -1) {
- rec.cols = append(rec.cols, c[1])
- if rec.invisible {
- m.protected[table+"."+c[1]] = true
- }
- if fkCols[c[1]] {
- rec.fkOverlap = true
- }
- }
- m.idx = append(m.idx, rec)
- continue
- }
- if mm := entSfReColLine.FindStringSubmatch(line); mm != nil {
- col, def := mm[1], strings.TrimSuffix(line, ",")
- if genIdx := strings.Index(def, "GENERATED ALWAYS AS"); genIdx >= 0 {
- m.genCols = append(m.genCols, entSfGenRec{table: table, name: col, line: def})
- for _, c := range entFzReCol.FindAllStringSubmatch(def[genIdx:], -1) {
- m.protected[table+"."+c[1]] = true
- }
- }
- if strings.HasPrefix(strings.TrimPrefix(def, "`"+col+"` "), "enum(") {
- m.enumCols = append(m.enumCols, entSfColRec{table: table, col: col})
- }
- if strings.HasSuffix(def, strings.TrimSpace(entSfInvisibleColToken)) {
- m.invisCols = append(m.invisCols, entSfColRec{table: table, col: col})
- } else {
- m.visibleCols[table]++
- }
- }
- }
-}
-
-// colDef returns the trimmed definition line of table.col from the base model.
-func (m *entSfSchema) colDef(table, col string) string {
- if tbl := m.base.byName[table]; tbl != nil {
- return tbl.colLine[col]
- }
- return ""
-}
-
-// ----------------------------------------------------------------------------
-// Mutation generation.
-// ----------------------------------------------------------------------------
-
-func entSfMenu(version string) []string {
- menu := []string{
- "add_column", "drop_column", "modify_column",
- "add_index", "drop_index",
- "add_fk", "drop_fk",
- "add_view", "drop_view", "modify_view",
- "add_trigger", "drop_trigger",
- "add_routine", "drop_routine",
- "partition", "departition",
- "add_fulltext", "drop_fulltext",
- "add_gen_stored", "add_gen_virtual", "modify_gen", "drop_gen",
- "enum_append",
- }
- if version != "5.7" {
- // 5.7-illegal kinds: CHECK is parsed-and-ignored on 5.7 (nothing syncs back, so
- // convergence is impossible); the SRID column attribute and column/index
- // INVISIBLE are 8.0-only syntax.
- menu = append(menu,
- "add_check", "drop_check",
- "add_spatial", "drop_spatial",
- "toggle_col_invisible", "toggle_idx_invisible",
- )
- }
- return menu
-}
-
-// entSfMutations derives k independent mutations for round r, deterministically in
-// (model order, rng stream).
-func entSfMutations(t *testing.T, m *entSfSchema, rng *rand.Rand, srv liveServer, ledger *entSfLedger, r, k int) []entSfMutation {
- t.Helper()
- return entSfMutationsFromMenu(t, m, rng, entSfMenu(srv.version), ledger, r, k)
-}
-
-// entSfMutationsFromMenu is entSfMutations with an explicit menu. The cross-version
-// stateful fuzzer (A8, sdl_enterprise_crossfuzz_test.go) passes the 5.7-legal menu
-// regardless of the authoring side — its targets apply to BOTH versions.
-func entSfMutationsFromMenu(t *testing.T, m *entSfSchema, rng *rand.Rand, menu []string, ledger *entSfLedger, r, k int) []entSfMutation {
- t.Helper()
- state := entFzNewState()
- var muts []entSfMutation
- for attempt := 0; attempt < 500 && len(muts) < k; attempt++ {
- kind := menu[rng.Intn(len(menu))]
- if mut, ok := entSfGenerate(t, kind, m, rng, state, ledger, r, len(muts)); ok {
- muts = append(muts, mut)
- }
- }
- require.Len(t, muts, k, "stateful-fuzz generator starved (only %d of %d mutations, round %d)", len(muts), k, r)
- return muts
-}
-
-// entSfMutableTables returns the non-protected, non-locked tables.
-func entSfMutableTables(m *entSfSchema, state *entFzState) []*entFzTable {
- var out []*entFzTable
- for _, tbl := range m.base.tables {
- if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
- continue
- }
- out = append(out, tbl)
- }
- return out
-}
-
-// entSfColCandidates lists table.col pairs whose (dump-form) definition rest satisfies
-// accept, excluding protected/used/locked/generated columns.
-// entSfIsFKMemberCol reports whether table.col participates in any child-side
-// foreign key of the base model. Used to keep STORED generated columns off FK
-// member bases (A8 finding: both servers refuse that shape; 5.7 opaquely).
-func entSfIsFKMemberCol(m *entSfSchema, table, col string) bool {
- tbl := m.base.byName[table]
- if tbl == nil {
- return false
- }
- for _, fk := range tbl.fks {
- for _, c := range fk.cols {
- if c == col {
- return true
- }
- }
- }
- return false
-}
-
-func entSfColCandidates(m *entSfSchema, state *entFzState, accept func(rest string) bool) []entSfColRec {
- var out []entSfColRec
- for _, tbl := range entSfMutableTables(m, state) {
- for _, c := range tbl.cols {
- key := tbl.name + "." + c
- if state.usedCols[key] || m.protected[key] || entFzProtectedColumns[key] {
- continue
- }
- def := tbl.colLine[c]
- if strings.Contains(def, "GENERATED ALWAYS AS") {
- continue
- }
- rest := strings.TrimPrefix(def, "`"+c+"` ")
- if accept(rest) {
- out = append(out, entSfColRec{table: tbl.name, col: c})
- }
- }
- }
- return out
-}
-
-func entSfIsIntRest(rest string) bool {
- return strings.HasPrefix(rest, "bigint") || strings.HasPrefix(rest, "int") || strings.HasPrefix(rest, "integer")
-}
-
-// entSfGenerate builds one mutation of the given kind, or reports it infeasible. seq
-// namespaces generated object names within the round; r namespaces across rounds.
-//
-//nolint:gocyclo
-func entSfGenerate(t *testing.T, kind string, m *entSfSchema, rng *rand.Rand, state *entFzState, ledger *entSfLedger, r, seq int) (entSfMutation, bool) {
- t.Helper()
- name := func(tag string) string { return fmt.Sprintf("ent_sf_r%d_%s%d", r, tag, seq) }
- tableKey := func(tbl string) []string { return []string{"table:" + tbl} }
-
- switch kind {
- case "add_column":
- tables := entSfMutableTables(m, state)
- if len(tables) == 0 {
- return entSfMutation{}, false
- }
- tbl := tables[rng.Intn(len(tables))].name
- defs := []string{"varchar(40) DEFAULT NULL", "int NOT NULL DEFAULT '0'", "decimal(8,2) DEFAULT NULL", "bigint unsigned DEFAULT NULL"}
- def := defs[rng.Intn(len(defs))]
- col := name("c")
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_column %s.%s (%s)", tbl, col, def),
- apply: func(t *testing.T, source string) string {
- return addColumnToTable(t, source, tbl, "`"+col+"` "+def)
- },
- },
- delta: entSfCounts{columns: 1}, budget: 150, touched: tableKey(tbl),
- }, true
-
- case "drop_column":
- var cands []entSfColRec
- for _, tbl := range entSfMutableTables(m, state) {
- indexed := entFzIndexedCols(tbl)
- trigRef := m.base.triggerCols[tbl.name]
- for _, c := range tbl.cols {
- key := tbl.name + "." + c
- if indexed[c] || trigRef[c] || entFzProtectedColumns[key] || m.protected[key] || state.usedCols[key] {
- continue
- }
- if strings.Contains(tbl.colLine[c], "GENERATED") {
- continue
- }
- cands = append(cands, entSfColRec{table: tbl.name, col: c})
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.col] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("drop_column %s.%s", pick.table, pick.col),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "`"+pick.col+"`")
- },
- },
- delta: entSfCounts{columns: -1}, touched: tableKey(pick.table),
- }, true
-
- case "modify_column":
- type cand struct {
- rec entSfColRec
- from, to int
- }
- var cands []cand
- for _, tbl := range entSfMutableTables(m, state) {
- for _, c := range tbl.cols {
- key := tbl.name + "." + c
- if state.usedCols[key] || m.protected[key] {
- continue
- }
- mm := entFzReVarLen.FindStringSubmatch(tbl.colLine[c])
- if mm == nil || strings.Contains(tbl.colLine[c], "GENERATED") {
- continue
- }
- n := entFzAtoi(mm[1])
- if n < 8 || n > 150 {
- continue
- }
- cands = append(cands, cand{rec: entSfColRec{table: tbl.name, col: c}, from: n, to: n + 23})
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.rec.table+"."+pick.rec.col] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("modify_column widen %s.%s varchar(%d)->varchar(%d)", pick.rec.table, pick.rec.col, pick.from, pick.to),
- apply: func(t *testing.T, source string) string {
- return entReplaceInTable(t, source, pick.rec.table,
- fmt.Sprintf("`%s` varchar(%d)", pick.rec.col, pick.from),
- fmt.Sprintf("`%s` varchar(%d)", pick.rec.col, pick.to))
- },
- },
- budget: 80, touched: tableKey(pick.rec.table),
- }, true
-
- case "add_index":
- cands := entSfColCandidates(m, state, func(rest string) bool {
- if entSfIsIntRest(rest) {
- return true
- }
- mm := entFzReVarLen.FindStringSubmatch(rest)
- return mm != nil && strings.HasPrefix(rest, "varchar") && entFzAtoi(mm[1]) <= 150
- })
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- idx := name("i")
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_index %s on %s(%s)", idx, pick.table, pick.col),
- apply: func(t *testing.T, source string) string {
- return addIndexToTable(t, source, pick.table, "KEY `"+idx+"` (`"+pick.col+"`)")
- },
- },
- delta: entSfCounts{indexes: 1}, budget: 150, touched: tableKey(pick.table),
- }, true
-
- case "drop_index":
- var cands []entSfIdxRec
- for _, rec := range m.idx {
- if rec.invisible || rec.fkOverlap || state.lockedTables[rec.table] || state.usedIndexes[rec.table+"."+rec.name] {
- continue
- }
- cands = append(cands, rec)
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedIndexes[pick.table+"."+pick.name] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("drop_index %s.%s", pick.table, pick.name),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "`"+pick.name+"`")
- },
- },
- delta: entSfCounts{indexes: -1}, touched: tableKey(pick.table),
- }, true
-
- case "add_fk":
- var srcs []*entFzTable
- for _, tbl := range entSfMutableTables(m, state) {
- if !tbl.partitioned {
- srcs = append(srcs, tbl)
- }
- }
- if len(srcs) == 0 {
- return entSfMutation{}, false
- }
- src := srcs[rng.Intn(len(srcs))]
- var refs []*entFzTable
- for _, tbl := range m.base.tables {
- if tbl.partitioned || state.lockedTables[tbl.name] || len(tbl.pk) != 1 {
- continue
- }
- pkCol := ""
- for c := range tbl.pk {
- pkCol = c
- }
- if strings.HasPrefix(strings.TrimPrefix(tbl.colLine[pkCol], "`"+pkCol+"` "), "bigint") &&
- strings.Contains(tbl.colLine[pkCol], "unsigned") {
- refs = append(refs, tbl)
- }
- }
- if len(refs) == 0 {
- return entSfMutation{}, false
- }
- ref := refs[rng.Intn(len(refs))]
- refPK := ""
- for c := range ref.pk {
- refPK = c
- }
- srcName, refName := src.name, ref.name
- state.lockedTables[srcName] = true
- state.lockedTables[refName] = true
- col, fk := name("r"), name("fk")
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_fk %s.%s -> %s.%s (%s)", srcName, col, refName, refPK, fk),
- apply: func(t *testing.T, source string) string {
- s := addColumnToTable(t, source, srcName, "`"+col+"` bigint unsigned DEFAULT NULL")
- s = addIndexToTable(t, s, srcName, "KEY `"+fk+"` (`"+col+"`)")
- return addIndexToTable(t, s, srcName,
- "CONSTRAINT `"+fk+"` FOREIGN KEY (`"+col+"`) REFERENCES `"+refName+"` (`"+refPK+"`) ON DELETE SET NULL")
- },
- },
- delta: entSfCounts{columns: 1, indexes: 1, fks: 1}, budget: 450, touched: tableKey(srcName),
- }, true
-
- case "drop_fk":
- var cands []entSfNamedRec
- for _, tbl := range m.base.tables {
- if state.lockedTables[tbl.name] {
- continue
- }
- for _, fk := range tbl.fks {
- if !state.usedFKs[tbl.name+"."+fk.name] {
- cands = append(cands, entSfNamedRec{table: tbl.name, name: fk.name})
- }
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedFKs[pick.table+"."+pick.name] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("drop_fk %s.%s", pick.table, pick.name),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "CONSTRAINT `"+pick.name+"`")
- },
- },
- delta: entSfCounts{fks: -1}, touched: tableKey(pick.table),
- }, true
-
- case "add_view":
- tables := entSfMutableTables(m, state)
- if len(tables) == 0 {
- return entSfMutation{}, false
- }
- tbl := tables[rng.Intn(len(tables))]
- pkCol := ""
- for _, c := range tbl.cols {
- if tbl.pk[c] {
- pkCol = c
- break
- }
- }
- if pkCol == "" {
- return entSfMutation{}, false
- }
- vw, table := name("v"), tbl.name
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_view %s over %s(%s)", vw, table, pkCol),
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE VIEW %s AS SELECT %s FROM %s;\n", vw, pkCol, table)
- },
- },
- delta: entSfCounts{views: 1}, budget: 600, touched: nil,
- }, true
-
- case "drop_view":
- var cands []string
- for _, v := range m.views {
- if !state.usedViews[v] {
- cands = append(cands, v)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedViews[pick] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: "drop_view " + pick,
- apply: func(t *testing.T, source string) string {
- return dropObjectBlock(t, source, "VIEW", pick)
- },
- },
- delta: entSfCounts{views: -1}, touched: []string{"view:" + pick},
- }, true
-
- case "modify_view":
- // The extension is only appendable once per view over the whole seed (a second
- // append would duplicate the column), so the guard lives in the LEDGER, not the
- // per-round state.
- extension := map[string]string{
- "ent_fz_v1": "`users`.`name` AS `name`",
- "ent_fz_v2": "`hosts`.`status` AS `status`",
- }
- var cands []string
- for _, v := range m.views {
- if !state.usedViews[v] && !ledger.viewModified[v] && extension[v] != "" {
- cands = append(cands, v)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedViews[pick] = true
- ledger.viewModified[pick] = true
- ext := extension[pick]
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: "modify_view " + pick,
- apply: func(t *testing.T, source string) string {
- return addColumnToView(t, source, pick, ext)
- },
- },
- budget: 80, touched: []string{"view:" + pick},
- }, true
-
- case "add_trigger":
- tables := entSfMutableTables(m, state)
- if len(tables) == 0 {
- return entSfMutation{}, false
- }
- tbl := tables[rng.Intn(len(tables))]
- pkCol := ""
- for _, c := range tbl.cols {
- if tbl.pk[c] {
- pkCol = c
- break
- }
- }
- if pkCol == "" {
- return entSfMutation{}, false
- }
- trg, table := name("t"), tbl.name
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_trigger %s on %s", trg, table),
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE TRIGGER %s BEFORE UPDATE ON %s FOR EACH ROW SET NEW.%s = NEW.%s;\n",
- trg, table, pkCol, pkCol)
- },
- },
- delta: entSfCounts{triggers: 1}, budget: 500, touched: nil,
- }, true
-
- case "drop_trigger":
- var cands []string
- for _, trg := range m.base.triggers {
- if !state.usedTriggers[trg] {
- cands = append(cands, trg)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedTriggers[pick] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: "drop_trigger " + pick,
- apply: func(t *testing.T, source string) string {
- return dropTrigger(t, source, pick)
- },
- },
- delta: entSfCounts{triggers: -1}, touched: []string{"trigger:" + pick},
- }, true
-
- case "add_routine":
- if rng.Intn(2) == 0 {
- fn := name("fn")
- ret := 100*r + seq
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: "add_routine function " + fn,
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE FUNCTION %s() RETURNS INT DETERMINISTIC RETURN %d;\n", fn, ret)
- },
- },
- delta: entSfCounts{functions: 1}, budget: 500, touched: nil,
- }, true
- }
- pr := name("pr")
- sel := 200*r + seq
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: "add_routine procedure " + pr,
- apply: func(_ *testing.T, source string) string {
- return source + fmt.Sprintf("\nCREATE PROCEDURE %s() BEGIN SELECT %d; END;\n", pr, sel)
- },
- },
- delta: entSfCounts{procedures: 1}, budget: 500, touched: nil,
- }, true
-
- case "drop_routine":
- type cand struct{ kind, name string }
- var cands []cand
- for _, fn := range m.functions {
- if !state.usedRoutines[fn] {
- cands = append(cands, cand{"FUNCTION", fn})
- }
- }
- for _, pr := range m.procedures {
- if !state.usedRoutines[pr] {
- cands = append(cands, cand{"PROCEDURE", pr})
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedRoutines[pick.name] = true
- delta := entSfCounts{functions: -1}
- key := "function:" + pick.name
- if pick.kind == "PROCEDURE" {
- delta = entSfCounts{procedures: -1}
- key = "procedure:" + pick.name
- }
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: "drop_routine " + strings.ToLower(pick.kind) + " " + pick.name,
- apply: func(t *testing.T, source string) string {
- return dropObjectBlock(t, source, pick.kind, pick.name)
- },
- },
- delta: delta, touched: []string{key},
- }, true
-
- case "partition":
- var cands []*entFzTable
- for _, tbl := range m.base.tables {
- if tbl.partitioned || state.lockedTables[tbl.name] || m.noPartition[tbl.name] {
- continue
- }
- if len(tbl.fks) > 0 || m.base.referenced[tbl.name] || len(tbl.pk) != 1 {
- continue
- }
- hasUnique := false
- for _, idx := range tbl.indexes {
- if idx.unique {
- hasUnique = true
- break
- }
- }
- if hasUnique {
- continue
- }
- cands = append(cands, tbl)
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- pkCol := ""
- for c := range pick.pk {
- pkCol = c
- }
- state.lockedTables[pick.name] = true
- table := pick.name
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("partition %s by hash(%s)", table, pkCol),
- apply: func(t *testing.T, source string) string {
- return entAppendTableClause(t, source, table, "PARTITION BY HASH (`"+pkCol+"`)\nPARTITIONS 4")
- },
- },
- budget: 300, touched: tableKey(table),
- }, true
-
- case "departition":
- var cands []*entFzTable
- for _, tbl := range m.base.tables {
- if tbl.partitioned && !state.lockedTables[tbl.name] {
- cands = append(cands, tbl)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.lockedTables[pick.name] = true
- table := pick.name
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: "departition " + table,
- apply: func(t *testing.T, source string) string {
- return entSetPartitionClause(t, source, table, "")
- },
- },
- touched: tableKey(table),
- }, true
-
- case "add_fulltext":
- // One FULLTEXT creation per table per round (InnoDB builds them one at a time),
- // and never on a partitioned table — the whole table is locked for the round.
- var cands []entSfColRec
- for _, rec := range entSfColCandidates(m, state, func(rest string) bool {
- return strings.HasPrefix(rest, "varchar") || strings.HasPrefix(rest, "text") ||
- strings.HasPrefix(rest, "mediumtext") || strings.HasPrefix(rest, "longtext")
- }) {
- if tbl := m.base.byName[rec.table]; tbl != nil && tbl.partitioned {
- continue
- }
- cands = append(cands, rec)
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.lockedTables[pick.table] = true
- idx := name("ft")
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_fulltext %s on %s(%s)", idx, pick.table, pick.col),
- apply: func(t *testing.T, source string) string {
- return addIndexToTable(t, source, pick.table, "FULLTEXT KEY `"+idx+"` (`"+pick.col+"`)")
- },
- },
- delta: entSfCounts{indexes: 1}, budget: 150, touched: tableKey(pick.table),
- }, true
-
- case "drop_fulltext":
- var cands []entSfNamedRec
- for _, rec := range m.fulltext {
- if !state.lockedTables[rec.table] && !state.usedIndexes[rec.table+"."+rec.name] {
- cands = append(cands, rec)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedIndexes[pick.table+"."+pick.name] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("drop_fulltext %s.%s", pick.table, pick.name),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "FULLTEXT KEY `"+pick.name+"`")
- },
- },
- delta: entSfCounts{indexes: -1}, touched: tableKey(pick.table),
- }, true
-
- case "add_spatial":
- // A NOT NULL POINT column with an SRID plus its SPATIAL index, in one mutation.
- // SPATIAL indexes are unsupported on partitioned tables; the table is locked so
- // no same-round mutation interleaves with the two-clause add.
- var cands []*entFzTable
- for _, tbl := range entSfMutableTables(m, state) {
- if !tbl.partitioned {
- cands = append(cands, tbl)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.lockedTables[pick.name] = true
- table, col, idx := pick.name, name("sp"), name("spi")
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_spatial %s.%s (SRID 4326) + %s", table, col, idx),
- apply: func(t *testing.T, source string) string {
- s := addColumnToTable(t, source, table, "`"+col+"` point NOT NULL /*!80003 SRID 4326 */")
- return addIndexToTable(t, s, table, "SPATIAL KEY `"+idx+"` (`"+col+"`)")
- },
- },
- delta: entSfCounts{columns: 1, indexes: 1}, budget: 400, touched: tableKey(table),
- }, true
-
- case "drop_spatial":
- // Index and column leave together (a dangling SPATIAL KEY over a dropped column
- // would be an invalid target).
- var cands []entSfIdxRec
- for _, rec := range m.spatial {
- if !state.lockedTables[rec.table] {
- cands = append(cands, rec)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.lockedTables[pick.table] = true
- col := pick.cols[0]
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("drop_spatial %s.%s (+column %s)", pick.table, pick.name, col),
- apply: func(t *testing.T, source string) string {
- s := entDropLineInTable(t, source, pick.table, "SPATIAL KEY `"+pick.name+"`")
- return entDropLineInTable(t, s, pick.table, "`"+col+"` point")
- },
- },
- delta: entSfCounts{columns: -1, indexes: -1}, touched: tableKey(pick.table),
- }, true
-
- case "add_check":
- cands := entSfColCandidates(m, state, entSfIsIntRest)
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.col] = true
- chk := name("ck")
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("add_check %s on %s(%s)", chk, pick.table, pick.col),
- apply: func(t *testing.T, source string) string {
- return addIndexToTable(t, source, pick.table, "CONSTRAINT `"+chk+"` CHECK ((`"+pick.col+"` >= 0))")
- },
- },
- delta: entSfCounts{checks: 1}, budget: 250, touched: tableKey(pick.table),
- }, true
-
- case "drop_check":
- var cands []entSfNamedRec
- for _, rec := range m.checks {
- if !state.lockedTables[rec.table] && !state.usedIndexes[rec.table+"."+rec.name] {
- cands = append(cands, rec)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedIndexes[pick.table+"."+pick.name] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("drop_check %s.%s", pick.table, pick.name),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "CONSTRAINT `"+pick.name+"`")
- },
- },
- delta: entSfCounts{checks: -1}, touched: tableKey(pick.table),
- }, true
-
- case "add_gen_stored", "add_gen_virtual":
- mode, tag := "STORED", "gs"
- if kind == "add_gen_virtual" {
- mode, tag = "VIRTUAL", "gv"
- }
- cands := entSfColCandidates(m, state, entSfIsIntRest)
- if mode == "STORED" {
- // A8 finding: BOTH servers refuse a STORED generated column whose base
- // column is a child-side FK member (8.0 clean 1215; 5.7 opaque errno
- // 150 at ALGORITHM=COPY rename). VIRTUAL stays eligible.
- kept := cands[:0]
- for _, c := range cands {
- if !entSfIsFKMemberCol(m, c.table, c.col) {
- kept = append(kept, c)
- }
- }
- cands = kept
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.col] = true
- col := name(tag)
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("%s %s.%s as (%s + 7)", kind, pick.table, col, pick.col),
- apply: func(t *testing.T, source string) string {
- return addColumnToTable(t, source, pick.table,
- "`"+col+"` bigint GENERATED ALWAYS AS ((`"+pick.col+"` + 7)) "+mode)
- },
- },
- delta: entSfCounts{columns: 1}, budget: 300, touched: tableKey(pick.table),
- }, true
-
- case "modify_gen":
- // Bump the additive constant of a fuzz-shaped generated expression (`col` + N).
- var cands []entSfGenRec
- for _, rec := range m.genCols {
- if state.lockedTables[rec.table] || state.usedCols[rec.table+"."+rec.name] {
- continue
- }
- if entSfReGenConst.MatchString(rec.line) {
- cands = append(cands, rec)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.name] = true
- old := entSfReGenConst.FindStringSubmatch(pick.line)
- newLine := entSfReGenConst.ReplaceAllString(pick.line, fmt.Sprintf("+ %d)", entFzAtoi(old[1])+1))
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("modify_gen %s.%s (+%s -> +%d)", pick.table, pick.name, old[1], entFzAtoi(old[1])+1),
- apply: func(t *testing.T, source string) string {
- return entReplaceLineInTable(t, source, pick.table, "`"+pick.name+"`", newLine)
- },
- },
- budget: 80, touched: tableKey(pick.table),
- }, true
-
- case "drop_gen":
- var cands []entSfGenRec
- for _, rec := range m.genCols {
- if state.lockedTables[rec.table] || state.usedCols[rec.table+"."+rec.name] || entFzProtectedTables[rec.table] {
- continue
- }
- cands = append(cands, rec)
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.name] = true
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("drop_gen %s.%s", pick.table, pick.name),
- apply: func(t *testing.T, source string) string {
- return entDropLineInTable(t, source, pick.table, "`"+pick.name+"`")
- },
- },
- delta: entSfCounts{columns: -1}, touched: tableKey(pick.table),
- }, true
-
- case "toggle_col_invisible":
- // Visible -> invisible needs >= 2 visible columns left; invisible -> visible is
- // always legal. Both directions render/strip the /*!80023 INVISIBLE */ token the
- // dumper and omni loader agree on.
- type cand struct {
- rec entSfColRec
- on bool
- }
- var cands []cand
- for _, rec := range m.invisCols {
- key := rec.table + "." + rec.col
- if !state.usedCols[key] && !state.lockedTables[rec.table] && !entFzProtectedTables[rec.table] {
- cands = append(cands, cand{rec: rec, on: false})
- }
- }
- for _, rec := range entSfColCandidates(m, state, func(rest string) bool {
- return entSfIsIntRest(rest) || strings.HasPrefix(rest, "varchar")
- }) {
- if m.visibleCols[rec.table] < 2 {
- continue
- }
- if strings.Contains(m.colDef(rec.table, rec.col), "INVISIBLE") {
- continue
- }
- cands = append(cands, cand{rec: rec, on: true})
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.rec.table+"."+pick.rec.col] = true
- line := m.colDef(pick.rec.table, pick.rec.col)
- newLine := line + entSfInvisibleColToken
- dir := "->invisible"
- if !pick.on {
- newLine = strings.TrimSuffix(line, entSfInvisibleColToken)
- dir = "->visible"
- }
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("toggle_col_invisible %s.%s %s", pick.rec.table, pick.rec.col, dir),
- apply: func(t *testing.T, source string) string {
- return entReplaceLineInTable(t, source, pick.rec.table, "`"+pick.rec.col+"`", newLine)
- },
- },
- budget: 60, touched: tableKey(pick.rec.table),
- }, true
-
- case "toggle_idx_invisible":
- var cands []entSfIdxRec
- for _, rec := range m.idx {
- if rec.fkOverlap || state.lockedTables[rec.table] || state.usedIndexes[rec.table+"."+rec.name] {
- continue
- }
- cands = append(cands, rec)
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedIndexes[pick.table+"."+pick.name] = true
- newLine := pick.line + entSfInvisibleIdxToken
- dir := "->invisible"
- if pick.invisible {
- newLine = strings.TrimSuffix(pick.line, entSfInvisibleIdxToken)
- dir = "->visible"
- }
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("toggle_idx_invisible %s.%s %s", pick.table, pick.name, dir),
- apply: func(t *testing.T, source string) string {
- return entReplaceLineInTable(t, source, pick.table, "`"+pick.name+"`", newLine)
- },
- },
- budget: 60, touched: tableKey(pick.table),
- }, true
-
- case "enum_append":
- var cands []entSfColRec
- for _, rec := range m.enumCols {
- key := rec.table + "." + rec.col
- if !state.usedCols[key] && !state.lockedTables[rec.table] && !entFzProtectedTables[rec.table] && !m.protected[key] {
- cands = append(cands, rec)
- }
- }
- if len(cands) == 0 {
- return entSfMutation{}, false
- }
- pick := cands[rng.Intn(len(cands))]
- state.usedCols[pick.table+"."+pick.col] = true
- line := m.colDef(pick.table, pick.col)
- member := fmt.Sprintf("m%d_%d", r, seq)
- newLine := entSfReEnumSpec.ReplaceAllString(line, "enum($1,'"+member+"')")
- if newLine == line {
- return entSfMutation{}, false
- }
- return entSfMutation{
- entFzMutation: entFzMutation{
- desc: fmt.Sprintf("enum_append %s.%s += '%s'", pick.table, pick.col, member),
- apply: func(t *testing.T, source string) string {
- return entReplaceLineInTable(t, source, pick.table, "`"+pick.col+"`", newLine)
- },
- },
- budget: 60, touched: tableKey(pick.table),
- }, true
-
- default:
- return entSfMutation{}, false
- }
-}
-
-// ----------------------------------------------------------------------------
-// Round runner, trial (for ddmin), and the untouched-statement stability guard.
-// ----------------------------------------------------------------------------
-
-// entSfScratchDB creates an eagerly-droppable scratch database (minimization churns
-// through many databases inside a single test).
-func entSfScratchDB(ctx context.Context, t *testing.T, srv liveServer, prefix string) (string, func()) {
- t.Helper()
- dbName := newLiveDatabase(ctx, t, srv, prefix)
- drop := func() {
- c, err := createLiveMySQLDriver(ctx, srv, "")
- if err != nil {
- return
- }
- defer c.Close(ctx)
- _, _ = c.Execute(ctx, "DROP DATABASE IF EXISTS `"+dbName+"`", db.ExecuteOptions{})
- }
- return dbName, drop
-}
-
-// entSfRound runs one full stateful round against an already-loaded database: mutate
-// source textually, oracle (diff → apply → converge → idempotence), then the round-local
-// guards — (a) the fresh dump reloads through the STRICT LoadSDL path, (c) object counts
-// match want. Infrastructure failures fail t hard; protocol violations return an error
-// so the caller can ddmin. Returns the fresh canonical dump.
-func entSfRound(ctx context.Context, t *testing.T, srv liveServer, dbName, source string, muts []entSfMutation, want entSfCounts) (string, error) {
- t.Helper()
- target := source
- for _, m := range muts {
- target = m.apply(t, target)
- }
- after, _, err := entSfRoundToTarget(ctx, t, srv, dbName, source, target, srv.version, want)
- return after, err
-}
-
-// entSfRoundToTarget is the target-explicit core of entSfRound: the caller supplies the
-// mutated target text and the version threaded into every diff. The cross-version
-// stateful fuzzer (A8) authors ONE target from one side's dump and drives BOTH sides'
-// databases through it, so the target cannot be derived from this side's source. Returns
-// the fresh canonical dump and the generated plan.
-func entSfRoundToTarget(ctx context.Context, t *testing.T, srv liveServer, dbName, source, target, version string, want entSfCounts) (string, string, error) {
- t.Helper()
- plan, err := mysqlDiffSDLMigration(source, target, version)
- if err != nil {
- return "", "", errors.Wrap(err, "diff failed")
- }
- if strings.TrimSpace(plan) == "" {
- return "", "", errors.New("empty plan for a non-empty mutation set")
- }
- if err := applyDDL(ctx, t, srv, dbName, plan); err != nil {
- return "", "", errors.Wrapf(err, "plan failed to apply; plan was:\n%s", plan)
- }
- after := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(after, target, version)
- if err != nil {
- return "", "", errors.Wrap(err, "converge diff failed")
- }
- if converge != "" {
- return "", "", errors.Errorf("did not converge; residual:\n%s\nplan was:\n%s", converge, plan)
- }
- self, err := mysqlDiffSDLMigration(after, after, version)
- if err != nil {
- return "", "", errors.Wrap(err, "idempotence diff failed")
- }
- if self != "" {
- return "", "", errors.Errorf("post-apply dump not idempotent:\n%s", self)
- }
- // (a) LoadSDL must accept its own output — checked directly (DiffSDLMigration would
- // mask an SDL rejection behind the LoadSQL fallback).
- if _, err := catalog.LoadSDLWithVersion(withDatabaseContext(after), mysqlVersionFor(version)); err != nil {
- return "", "", errors.Wrap(err, "canonical dump does not reload through LoadSDL")
- }
- // (c) object counts must match the mutation ledger.
- got := entSfCountObjects(syncMetaForDB(ctx, t, srv, dbName))
- if got != want {
- return "", "", errors.Errorf("object counts diverge from the mutation ledger:\n want %+v\n got %+v\nplan was:\n%s", want, got, plan)
- }
- return after, plan, nil
-}
-
-// entSfTrial reproduces one round from scratch: load baseSDL (a canonical dump) into a
-// fresh database and run entSfRound over it. Used by ddmin.
-func entSfTrial(ctx context.Context, t *testing.T, srv liveServer, baseSDL string, muts []entSfMutation) error {
- t.Helper()
- dbName, drop := entSfScratchDB(ctx, t, srv, "entsdl_sf")
- defer drop()
-
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- // FOREIGN_KEY_CHECKS off for the session: baseSDL here is a CANONICAL dump
- // (alphabetical table order), so inline foreign keys may forward-reference —
- // same handling as entA8ScratchFromDump.
- _, err = driver.Execute(ctx, "SET FOREIGN_KEY_CHECKS=0;\n"+entNormalizeDelimiters(baseSDL), db.ExecuteOptions{})
- driver.Close(ctx)
- require.NoError(t, err, "[%s] stateful-fuzz trial base failed to load", srv.name)
-
- want := entSfCountObjects(syncMetaForDB(ctx, t, srv, dbName))
- for _, m := range muts {
- want = want.plus(m.delta)
- }
- source := dumpSDL(ctx, t, srv, dbName)
- _, err = entSfRound(ctx, t, srv, dbName, source, muts, want)
- return err
-}
-
-// entSfMinimize greedily shrinks a failing round's mutation set (ddmin-lite, mirroring
-// entFuzzMinimize but over the stateful trial).
-func entSfMinimize(ctx context.Context, t *testing.T, srv liveServer, baseSDL string, muts []entSfMutation, firstErr error) ([]entSfMutation, error) {
- t.Helper()
- return entSfMinimizeWith(muts, firstErr, func(trial []entSfMutation) error {
- return entSfTrial(ctx, t, srv, baseSDL, trial)
- })
-}
-
-// entSfMinimizeWith is the trial-generic ddmin-lite core, shared by the same-version
-// (A6) and cross-version (A8) stateful fuzzers.
-func entSfMinimizeWith(muts []entSfMutation, firstErr error, trial func([]entSfMutation) error) ([]entSfMutation, error) {
- minimized := muts
- lastErr := firstErr
- for i := 0; i < len(minimized) && len(minimized) > 1; {
- candidate := make([]entSfMutation, 0, len(minimized)-1)
- candidate = append(candidate, minimized[:i]...)
- candidate = append(candidate, minimized[i+1:]...)
- if err := trial(candidate); err != nil {
- minimized = candidate
- lastErr = err
- continue
- }
- i++
- }
- return minimized, lastErr
-}
-
-func entSfDescs(muts []entSfMutation) []string {
- out := make([]string, len(muts))
- for i, m := range muts {
- out[i] = m.desc
- }
- return out
-}
-
-// entSfStmtKeys splits a canonical dump and keys every statement by kind:name so the
-// stability guard can pair statements across rounds.
-func entSfStmtKeys(t *testing.T, dump string) map[string]string {
- t.Helper()
- stmts, err := mysqlparser.SplitSQL(dump)
- require.NoError(t, err, "split dump for stability keys")
- out := map[string]string{}
- for i, s := range stmts {
- text := strings.TrimSpace(s.Text)
- if text == "" || text == ";" {
- continue
- }
- key := ""
- switch {
- case entReTable.MatchString(text):
- key = "table:" + entReTable.FindStringSubmatch(text)[1]
- case entSfReViewHdr.MatchString(text):
- key = "view:" + entSfReViewHdr.FindStringSubmatch(text)[1]
- case entSfReFnHdr.MatchString(text):
- key = "function:" + entSfReFnHdr.FindStringSubmatch(text)[1]
- case entSfRePrHdr.MatchString(text):
- key = "procedure:" + entSfRePrHdr.FindStringSubmatch(text)[1]
- case entFzReTrigger.MatchString(text):
- key = "trigger:" + entFzReTrigger.FindStringSubmatch(text)[1]
- default:
- key = fmt.Sprintf("other:%d", i)
- }
- out[key] = text
- }
- return out
-}
-
-// entSfAssertUntouchedStable proves every statement NOT named by this round's mutations
-// is byte-identical across the round — the sharp form of the accretion guard (the
-// historical paren-accretion bug regenerated untouched objects with one extra wrapper
-// per cycle while converging and self-diffing empty).
-func entSfAssertUntouchedStable(t *testing.T, srv liveServer, seed int64, r int, prev, after string, muts []entSfMutation) {
- t.Helper()
- touched := map[string]bool{}
- for _, m := range muts {
- for _, k := range m.touched {
- touched[k] = true
- }
- }
- prevKeys := entSfStmtKeys(t, prev)
- afterKeys := entSfStmtKeys(t, after)
- keys := make([]string, 0, len(prevKeys))
- for k := range prevKeys {
- if _, ok := afterKeys[k]; ok && !touched[k] {
- keys = append(keys, k)
- }
- }
- slices.Sort(keys)
- for _, k := range keys {
- require.Equal(t, prevKeys[k], afterKeys[k],
- "[STATEFUZZ %s seed=%d round=%d] untouched object %s changed text across the round (collateral churn / accretion)",
- srv.name, seed, r, k)
- }
-}
-
-// ----------------------------------------------------------------------------
-// The stateful fuzz test: >=12 seeds on 8.0, >=6 on 5.7, 5 rounds each.
-// ----------------------------------------------------------------------------
-
-func entSfRunSeed(ctx context.Context, t *testing.T, srv liveServer, baseDDL string, seed int64) {
- t.Helper()
- rng := rand.New(rand.NewSource(seed)) //nolint:gosec // deterministic fuzz, not crypto
-
- dbName, drop := entSfScratchDB(ctx, t, srv, "entsdl_sf")
- defer drop()
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- _, err = driver.Execute(ctx, entNormalizeDelimiters(baseDDL), db.ExecuteOptions{})
- driver.Close(ctx)
- require.NoError(t, err, "[%s] stateful-fuzz base failed to load", srv.name)
-
- dump0 := dumpSDL(ctx, t, srv, dbName)
- ledger := &entSfLedger{
- expect: entSfCountObjects(syncMetaForDB(ctx, t, srv, dbName)),
- viewModified: map[string]bool{},
- }
- prev := dump0
- budget := 0
- var history []string
-
- for r := 1; r <= entSfRounds; r++ {
- m := entSfParse(t, prev)
- k := 2 + rng.Intn(4) // K ∈ [2,5]
- muts := entSfMutations(t, m, rng, srv, ledger, r, k)
- for _, mut := range muts {
- history = append(history, fmt.Sprintf("r%d: %s", r, mut.desc))
- }
- t.Logf("[STATEFUZZ %s seed=%d round=%d] K=%d mutations:\n %s",
- srv.name, seed, r, k, strings.Join(entSfDescs(muts), "\n "))
-
- want := ledger.expect
- for _, mut := range muts {
- want = want.plus(mut.delta)
- }
- after, err := entSfRound(ctx, t, srv, dbName, prev, muts, want)
- if err != nil {
- minimized, minErr := entSfMinimize(ctx, t, srv, prev, muts, err)
- t.Fatalf("[STATEFUZZ %s seed=%d round=%d] FAILED\nminimized mutations (%d of %d):\n %s\nerror:\n%v\nseed history:\n %s",
- srv.name, seed, r, len(minimized), len(muts), strings.Join(entSfDescs(minimized), "\n "),
- minErr, strings.Join(history, "\n "))
- }
- ledger.expect = want
-
- // (b) accretion guards: the linear text budget and the untouched-statement
- // stability check.
- for _, mut := range muts {
- budget += mut.budget
- }
- require.Less(t, len(after), len(dump0)+budget+entSfRoundSlack*r,
- "[STATEFUZZ %s seed=%d round=%d] dump grew past the added-object budget: len(dump_%d)=%d, len(dump_0)=%d, budget=%d — text accretion suspected.\nseed history:\n %s",
- srv.name, seed, r, r, len(after), len(dump0), budget+entSfRoundSlack*r, strings.Join(history, "\n "))
- entSfAssertUntouchedStable(t, srv, seed, r, prev, after, muts)
-
- t.Logf("[STATEFUZZ %s seed=%d round=%d] ok: dump %d -> %d bytes (budget %d), counts %+v",
- srv.name, seed, r, len(prev), len(after), len(dump0)+budget+entSfRoundSlack*r, ledger.expect)
- prev = after
- }
-}
-
-//nolint:tparallel
-func TestSDLEnterpriseStatefulFuzz(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- seedCount := 12
- if srv.version == "5.7" {
- seedCount = 6
- }
- t.Run(srv.name, func(t *testing.T) {
- baseDDL := entSfBaseDDL(t, srv)
- for seed := int64(1); seed <= int64(seedCount); seed++ {
- seed := seed
- t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
- entSfRunSeed(ctx, t, srv, baseDDL, seed)
- })
- }
- })
- }
-}
diff --git a/backend/plugin/schema/mysql/sdl_enterprise_test.go b/backend/plugin/schema/mysql/sdl_enterprise_test.go
deleted file mode 100644
index d35888e9cf..0000000000
--- a/backend/plugin/schema/mysql/sdl_enterprise_test.go
+++ /dev/null
@@ -1,1828 +0,0 @@
-package mysql
-
-// ENTERPRISE smoke axes for the MySQL declarative (SDL) migration path. The real-world
-// suite (sdl_realworld_test.go) proved the path on mid-size production schemas; this file
-// scales verification to ENTERPRISE corpora and adds the axes an enterprise rollout
-// exercises hardest:
-//
-// corpus — Zabbix 7.0.0 (203 tables, 272 FKs, 65 changelog triggers), PrestaShop
-// (243 tables), OpenEMR (283 tables, INSERTs stripped), and the stock
-// MySQL `sys` schema (scaffolded, gated on an in-flight omni fix).
-// baseline — every corpus loads live, and its canonical dump self-diffs EMPTY
-// (determinism) and no-ops through the production schema.SDLMigration.
-// A1 — single-object CRUD (create / modify / drop) for every object kind, on a
-// 20-table Zabbix slice.
-// A2 — dependent objects: FK chains, trigger→table deps (the Zabbix changelog
-// pattern), 3-deep view stacks, function-used-by-view, circular FK pairs.
-// A3 — special types: AUTO_INCREMENT, generated columns, ENUM/SET members,
-// charset/collation, temporal precision, DECIMAL, TEXT/BLOB families,
-// signedness, prefix-indexed TEXT, partition ops.
-// A5 — sequential chain (with one-shot endpoint equality), apply-back,
-// combined release, scale timing guard, multi-file export round-trip.
-// A4 (fuzz) — lives in sdl_enterprise_fuzz_test.go.
-//
-// The per-case oracle protocol (entOracle) is the realworld suite's, made reusable:
-//
-// (1) load base B into an entsdl_-prefixed scratch DB, sync → canonical current SDL C;
-// (2) DDL = mysqlDiffSDLMigration(C, target T, version);
-// (3) apply DDL to the scratch DB — clean execution (dependency ordering) is under test;
-// (4) re-sync → C'; assert Diff(C', T) == "" (convergence) and Diff(C', C') == ""
-// (idempotence);
-// (5) minimality asserts (exact statement counts) where cheap.
-//
-// Shared helpers (liveServers, createLiveMySQLDriver, newLiveDatabase, dumpSDL, applyDDL,
-// statementCount, normalizeDelimiters, syncMetaForDB, objectCounts, addColumnToTable,
-// addIndexToTable, addColumnToView, dropObjectBlock, dropTrigger, findObjectSegment,
-// removeSegment, mustReplace, concatMultiFile) come from the sibling _test.go files.
-
-import (
- "context"
- _ "embed"
- "regexp"
- "strings"
- "sync"
- "testing"
- "time"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-// ----------------------------------------------------------------------------
-// Embedded enterprise corpora.
-//
-// Preprocessing applied when the corpus was lifted into testdata:
-// - zabbix: Zabbix 7.0.0 create/mysql/schema.sql verbatim, minus the single
-// INSERT INTO dbversion row (DDL-only testdata). Keeps the DELIMITER $$
-// changelog-trigger block (normalizeDelimiters handles it), the 301
-// standalone CREATE INDEX statements, and the 272 trailing
-// ALTER TABLE ... ADD CONSTRAINT foreign keys.
-// - prestashop: db_structure.sql with the installer placeholders substituted
-// (PREFIX_ -> ps_, ENGINE_TYPE -> InnoDB, ...). Leads with
-// SET SESSION sql_mode='' (two DEFAULT '0000-00-00 00:00:00' columns
-// need non-strict mode at load; the driver executes the corpus on a
-// single connection so the session setting holds).
-// - openemr: database.sql with all 5,821 INSERT statements stripped by a
-// quote-aware scanner (283 tables, DDL-only).
-// - sys: the stock MySQL 8.0 sys schema, single-file form. Formerly gated
-// on two omni parser gaps it exposed (adjacent string literals #360,
-// paren-subquery operand continuation #366) — both fixed and pinned.
-// ----------------------------------------------------------------------------
-
-//go:embed testdata/enterprise/zabbix.sql
-var entZabbixSQL string
-
-//go:embed testdata/enterprise/prestashop.sql
-var entPrestashopSQL string
-
-//go:embed testdata/enterprise/openemr.sql
-var entOpenemrSQL string
-
-//go:embed testdata/enterprise/sys.sql
-var entSysSQL string
-
-// entCorpus is one embedded enterprise schema.
-type entCorpus struct {
- name string
- ddl string
- // gate, when non-empty, skips every leg for this corpus with the given reason.
- gate string
- // gate57, when non-empty, skips only the 5.7 legs with the given reason.
- gate57 string
- // canonical marks ddl as a canonical SDL dump (alphabetical object order) rather
- // than upstream creation-ordered DDL. Canonical dumps are loaded by applying the
- // engine's own empty→schema plan, whose statements are dependency-ordered —
- // sequential client apply of the raw dump would break on view-on-view forward
- // references (sys: `host_summary` reads `x$...` views that sort later).
- canonical bool
-}
-
-func entCorpora() []entCorpus {
- return []entCorpus{
- {name: "zabbix", ddl: entZabbixSQL},
- {name: "prestashop", ddl: entPrestashopSQL},
- {name: "openemr", ddl: entOpenemrSQL},
- {name: "sys", ddl: entSysSQL, canonical: true,
- gate57: "sys corpus is the stock MySQL 8.0 sys schema; its views read performance_schema tables that 5.7.25 does not have"},
- }
-}
-
-// entLoadCorpus loads one corpus into a fresh scratch database, honoring per-version
-// gates. Canonical dumps go through the engine's own dependency-ordered create plan
-// (dogfooding: every corpus load exercises the empty→schema ordering guarantees).
-func entLoadCorpus(ctx context.Context, t *testing.T, srv liveServer, prefix string, corpus entCorpus) string {
- t.Helper()
- if corpus.gate != "" {
- t.Skipf("[%s/%s] %s", srv.name, corpus.name, corpus.gate)
- }
- if corpus.gate57 != "" && srv.name == "mysql57" {
- t.Skipf("[%s/%s] %s", srv.name, corpus.name, corpus.gate57)
- }
- if !corpus.canonical {
- return entLoadDDL(ctx, t, srv, prefix+corpus.name, corpus.ddl)
- }
- dbName := newLiveDatabase(ctx, t, srv, prefix+corpus.name)
- plan, err := mysqlDiffSDLMigration("", corpus.ddl, srv.version)
- require.NoError(t, err, "[%s/%s] empty→corpus create plan", srv.name, corpus.name)
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, plan, db.ExecuteOptions{})
- require.NoError(t, err, "[%s/%s] apply engine create plan", srv.name, corpus.name)
- return dbName
-}
-
-// entNormalizeDelimiters rewrites a multi-DELIMITER script into the no-DELIMITER form
-// the production split path handles. It extends the realworld suite's normalizeDelimiters
-// for the Zabbix style, where the custom delimiter sits on its OWN line and most trigger
-// bodies are ALREADY ';'-terminated — naively rewriting every delimiter line to ";" left
-// bare ";" statements that MySQL rejects (Error 1064 near ';'). Here a delimiter (inline
-// or standalone) only yields a ";" when the statement is not already terminated.
-func entNormalizeDelimiters(ddl string) string {
- if !strings.Contains(ddl, "DELIMITER") {
- return ddl
- }
- lines := strings.Split(ddl, "\n")
- out := make([]string, 0, len(lines))
- delim := ";"
- terminated := func() bool {
- for i := len(out) - 1; i >= 0; i-- {
- trimmed := strings.TrimSpace(out[i])
- if trimmed == "" {
- continue
- }
- return strings.HasSuffix(trimmed, ";")
- }
- return true
- }
- for _, ln := range lines {
- trimmed := strings.TrimSpace(ln)
- if strings.HasPrefix(strings.ToUpper(trimmed), "DELIMITER ") {
- delim = strings.TrimSpace(trimmed[len("DELIMITER "):])
- if delim == "" {
- delim = ";"
- }
- continue
- }
- if delim == ";" {
- out = append(out, ln)
- continue
- }
- if trimmed == delim {
- if !terminated() {
- out = append(out, ";")
- }
- continue
- }
- if strings.HasSuffix(trimmed, delim) {
- idx := strings.LastIndex(ln, delim)
- body := strings.TrimRight(ln[:idx], " \t")
- if !strings.HasSuffix(strings.TrimSpace(body), ";") {
- body += ";"
- }
- out = append(out, body)
- continue
- }
- out = append(out, ln)
- }
- return strings.Join(out, "\n")
-}
-
-// entLoadDDL creates a fresh entsdl_-prefixed scratch database on srv (dropped in
-// cleanup by newLiveDatabase) and applies ddl through the production driver path.
-func entLoadDDL(ctx context.Context, t *testing.T, srv liveServer, prefix, ddl string) string {
- t.Helper()
- dbName := newLiveDatabase(ctx, t, srv, prefix)
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, entNormalizeDelimiters(ddl), db.ExecuteOptions{})
- require.NoError(t, err, "[%s] apply base DDL", srv.name)
- return dbName
-}
-
-// entPlanStatementCount counts plan statements compound-aware: routine/trigger bodies
-// carry internal ';' that the naive statementCount (string split) over-counts, so the
-// minimality asserts split with the production splitter instead.
-func entPlanStatementCount(t *testing.T, plan string) int {
- t.Helper()
- stmts, err := mysqlparser.SplitSQL(plan)
- require.NoError(t, err, "split plan for statement count:\n%s", plan)
- n := 0
- for _, s := range stmts {
- if text := strings.TrimSpace(s.Text); text != "" && text != ";" {
- n++
- }
- }
- return n
-}
-
-// entOracle runs steps (2)-(4) of the oracle protocol against an already-loaded scratch
-// database: dump the canonical current SDL, diff to target, apply the generated DDL, and
-// prove convergence + idempotence. Returns the generated plan for minimality asserts.
-func entOracle(ctx context.Context, t *testing.T, srv liveServer, dbName, target, label string) string {
- t.Helper()
- source := dumpSDL(ctx, t, srv, dbName)
- require.NotEqual(t, source, target, "[%s] target must differ from source", label)
-
- plan, err := mysqlDiffSDLMigration(source, target, srv.version)
- require.NoError(t, err, "[%s] diff source->target", label)
- require.NotEmpty(t, plan, "[%s] expected a non-empty migration plan", label)
- t.Logf("[%s] plan (%d stmts):\n%s", label, statementCount(plan), plan)
-
- applyErr := applyDDL(ctx, t, srv, dbName, plan)
- require.NoError(t, applyErr, "[%s] generated plan failed to apply:\n%s", label, plan)
-
- after := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(after, target, srv.version)
- require.NoError(t, err, "[%s] converge diff", label)
- require.Empty(t, converge, "[%s] did not converge; residual:\n%s\nplan was:\n%s", label, converge, plan)
-
- self, err := mysqlDiffSDLMigration(after, after, srv.version)
- require.NoError(t, err, "[%s] idempotence diff", label)
- require.Empty(t, self, "[%s] post-apply dump not idempotent:\n%s", label, self)
- return plan
-}
-
-// ----------------------------------------------------------------------------
-// Zabbix slice extraction.
-//
-// The corpus declares tables, then standalone CREATE [UNIQUE] INDEX statements, then a
-// DELIMITER block of changelog triggers, then trailing ALTER TABLE ... ADD CONSTRAINT
-// foreign keys. A slice keeps the CREATE TABLE + CREATE INDEX statements of the included
-// tables, the triggers ON included tables (their bodies write `changelog`, so changelog
-// must be included), and only the FKs whose BOTH endpoints are included — yielding a
-// self-consistent sub-schema in the corpus's own statement order.
-// ----------------------------------------------------------------------------
-
-type entZbxKind int
-
-const (
- entZbxTable entZbxKind = iota
- entZbxIndex
- entZbxFK
- entZbxTrigger
- entZbxOther
-)
-
-type entZbxStmt struct {
- text string
- kind entZbxKind
- table string
- refTable string
-}
-
-var (
- entReTable = regexp.MustCompile("(?i)^CREATE TABLE `(\\w+)`")
- entReIndex = regexp.MustCompile("(?i)^CREATE (?:UNIQUE )?INDEX `\\w+` ON `(\\w+)`")
- entReFK = regexp.MustCompile("(?is)^ALTER TABLE `(\\w+)` ADD CONSTRAINT `\\w+` FOREIGN KEY.*?REFERENCES `(\\w+)`")
- entReTrigger = regexp.MustCompile(`(?is)^create\s+trigger\s+\w+\s+(?:before|after)\s+(?:insert|update|delete)\s+on\s+` + "`?(\\w+)`?")
-
- entZbxOnce sync.Once
- entZbxStmts []entZbxStmt
- entZbxErr error
-)
-
-// entZabbixStatements splits the normalized zabbix corpus once and classifies every
-// statement for slicing.
-func entZabbixStatements() ([]entZbxStmt, error) {
- entZbxOnce.Do(func() {
- stmts, err := mysqlparser.SplitSQL(entNormalizeDelimiters(entZabbixSQL))
- if err != nil {
- entZbxErr = err
- return
- }
- for _, s := range stmts {
- text := strings.TrimSpace(s.Text)
- if text == "" || text == ";" {
- continue
- }
- classified := entZbxStmt{text: text, kind: entZbxOther}
- if m := entReTable.FindStringSubmatch(text); m != nil {
- classified.kind, classified.table = entZbxTable, m[1]
- } else if m := entReIndex.FindStringSubmatch(text); m != nil {
- classified.kind, classified.table = entZbxIndex, m[1]
- } else if m := entReFK.FindStringSubmatch(text); m != nil {
- classified.kind, classified.table, classified.refTable = entZbxFK, m[1], m[2]
- } else if m := entReTrigger.FindStringSubmatch(text); m != nil {
- classified.kind, classified.table = entZbxTrigger, m[1]
- }
- entZbxStmts = append(entZbxStmts, classified)
- }
- })
- return entZbxStmts, entZbxErr
-}
-
-// entZabbixSlice extracts a self-consistent slice of the zabbix corpus containing the
-// given tables (which must include changelog — the trigger bodies write to it).
-func entZabbixSlice(t *testing.T, tables ...string) string {
- t.Helper()
- include := make(map[string]bool, len(tables))
- for _, tbl := range tables {
- include[tbl] = true
- }
- require.True(t, include["changelog"], "zabbix slices must include changelog (trigger bodies write to it)")
-
- stmts, err := entZabbixStatements()
- require.NoError(t, err, "split zabbix corpus")
-
- var b strings.Builder
- found := map[string]bool{}
- for _, s := range stmts {
- keep := false
- switch s.kind {
- case entZbxTable, entZbxIndex, entZbxTrigger:
- keep = include[s.table]
- if s.kind == entZbxTable && keep {
- found[s.table] = true
- }
- case entZbxFK:
- keep = include[s.table] && include[s.refTable]
- case entZbxOther:
- keep = false
- default:
- keep = false
- }
- if keep {
- b.WriteString(s.text)
- if !strings.HasSuffix(strings.TrimSpace(s.text), ";") {
- b.WriteString(";")
- }
- b.WriteString("\n")
- }
- }
- for _, tbl := range tables {
- require.True(t, found[tbl], "zabbix slice: table %q not found in corpus", tbl)
- }
- return b.String()
-}
-
-// entSliceCoreTables is the ~20-table Zabbix slice used by A1/A2/A5: a real 4-deep FK
-// chain (item_tag -> items -> hosts -> proxy -> proxy_group), the changelog trigger
-// pattern on 8 of the tables (hosts alone carries 5 triggers, including a BEGIN/END
-// body), self-referencing FKs (hosts.templateid, items.templateid/master_itemid), and
-// composite unique indexes (hosts_groups).
-var entSliceCoreTables = []string{
- "changelog",
- "role", "users", "media_type", "media",
- "proxy_group", "proxy", "proxy_rtdata",
- "maintenances", "hosts", "host_rtdata",
- "hstgrp", "hosts_groups",
- "interface", "valuemap", "items", "item_tag", "item_preproc",
- "drules", "dchecks",
- "connector", "connector_tag",
-}
-
-// ----------------------------------------------------------------------------
-// Targeted string-surgery helpers on the canonical dump (ent-prefixed; the shared
-// realworld helpers cover the untargeted forms).
-// ----------------------------------------------------------------------------
-
-// entTableSegment returns the [start,end) range of the CREATE TABLE statement for table
-// in source (located via the SDL splitter, so partition comments stay in-segment).
-func entTableSegment(t *testing.T, source, table string) (int, int) {
- t.Helper()
- header := "CREATE TABLE `" + table + "`"
- start, end := findObjectSegment(t, source, func(stmt string) bool {
- return strings.HasPrefix(stmt, header)
- })
- require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
- return start, end
-}
-
-// entReplaceInTable replaces old with new exactly once, scoped to table's CREATE block.
-func entReplaceInTable(t *testing.T, source, table, old, replacement string) string {
- t.Helper()
- start, end := entTableSegment(t, source, table)
- seg := source[start:end]
- require.Contains(t, seg, old, "table %q block must contain %q", table, old)
- return source[:start] + strings.Replace(seg, old, replacement, 1) + source[end:]
-}
-
-// entReplaceAllInTable replaces every occurrence of old within table's CREATE block.
-func entReplaceAllInTable(t *testing.T, source, table, old, replacement string) string {
- t.Helper()
- start, end := entTableSegment(t, source, table)
- seg := source[start:end]
- require.Contains(t, seg, old, "table %q block must contain %q", table, old)
- return source[:start] + strings.ReplaceAll(seg, old, replacement) + source[end:]
-}
-
-// entDropTableBlock removes the whole CREATE TABLE statement for table.
-func entDropTableBlock(t *testing.T, source, table string) string {
- t.Helper()
- start, end := entTableSegment(t, source, table)
- return removeSegment(source, start, end)
-}
-
-// entDropObjectNamed removes the CREATE statement whose text contains `<KEYWORD>
-// `+"`name`"+“ (EVENT and other kinds the shared dropObjectBlock does not cover).
-func entDropObjectNamed(t *testing.T, source, keyword, name string) string {
- t.Helper()
- needle := keyword + " `" + strings.ToUpper(name) + "`"
- start, end := findObjectSegment(t, source, func(stmt string) bool {
- u := strings.ToUpper(stmt)
- return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, needle)
- })
- require.GreaterOrEqual(t, start, 0, "%s %q not found in source", keyword, name)
- return removeSegment(source, start, end)
-}
-
-// entDropLineInTable removes the single body line containing marker from table's CREATE
-// block, fixing the dangling comma when the removed line was the last body element.
-func entDropLineInTable(t *testing.T, source, table, marker string) string {
- t.Helper()
- return entEditLineInTable(t, source, table, marker, "")
-}
-
-// entReplaceLineInTable swaps the single body line containing marker for newLine
-// (indentation and trailing comma are managed here).
-func entReplaceLineInTable(t *testing.T, source, table, marker, newLine string) string {
- t.Helper()
- return entEditLineInTable(t, source, table, marker, newLine)
-}
-
-// entEditLineInTable is the shared core of drop/replace-line: newLine == "" drops the
-// marker line, otherwise the line is replaced by newLine (re-indented, comma preserved).
-func entEditLineInTable(t *testing.T, source, table, marker, newLine string) string {
- t.Helper()
- start, end := entTableSegment(t, source, table)
- seg := source[start:end]
- lines := strings.Split(seg, "\n")
- idx := -1
- for i, ln := range lines {
- if i == 0 {
- continue // never the CREATE TABLE header
- }
- if strings.Contains(ln, marker) {
- idx = i
- break
- }
- }
- require.GreaterOrEqual(t, idx, 0, "table %q has no body line containing %q:\n%s", table, marker, seg)
- hadComma := strings.HasSuffix(strings.TrimSpace(lines[idx]), ",")
- if newLine == "" {
- lines = append(lines[:idx], lines[idx+1:]...)
- if !hadComma {
- // Removed the last body element: strip the now-dangling comma above it.
- for j := idx - 1; j > 0; j-- {
- trimmed := strings.TrimRight(lines[j], " \t")
- if strings.HasSuffix(trimmed, ",") {
- lines[j] = strings.TrimSuffix(trimmed, ",")
- break
- }
- if strings.TrimSpace(trimmed) != "" {
- break
- }
- }
- }
- } else {
- replaced := " " + strings.TrimSpace(newLine)
- if hadComma {
- replaced += ","
- }
- lines[idx] = replaced
- }
- return source[:start] + strings.Join(lines, "\n") + source[end:]
-}
-
-// entSetPartitionClause replaces table's whole partition clause (the dumper's
-// /*!NNNNN PARTITION BY ... */ executable comment, or a plain clause) with newClause
-// (plain form; pass "" to departition). The clause runs from the comment opener (or
-// PARTITION BY) to the end of the statement, so it is rebuilt rather than patched.
-func entSetPartitionClause(t *testing.T, source, table, newClause string) string {
- t.Helper()
- start, end := entTableSegment(t, source, table)
- seg := source[start:end]
- pIdx := strings.Index(strings.ToUpper(seg), "PARTITION BY")
- require.GreaterOrEqual(t, pIdx, 0, "table %q has no PARTITION BY clause:\n%s", table, seg)
- clauseStart := pIdx
- if c := strings.LastIndex(seg[:pIdx], "/*!"); c >= 0 {
- clauseStart = c
- }
- // The clause (with its optional comment close) runs to the statement end; the
- // trailing ";" (when in-segment) is preserved.
- tail := ""
- rest := strings.TrimRight(seg[clauseStart:], " \t\n")
- if strings.HasSuffix(rest, ";") {
- tail = ";"
- }
- prefix := strings.TrimRight(seg[:clauseStart], " \t\n")
- if newClause == "" {
- return source[:start] + prefix + tail + source[end:]
- }
- return source[:start] + prefix + "\n" + newClause + tail + source[end:]
-}
-
-// entAppendTableClause appends clause (e.g. a plain PARTITION BY) to the end of table's
-// CREATE statement, before the trailing semicolon.
-func entAppendTableClause(t *testing.T, source, table, clause string) string {
- t.Helper()
- start, end := entTableSegment(t, source, table)
- seg := source[start:end]
- trimmed := strings.TrimRight(seg, " \t\n")
- require.True(t, strings.HasSuffix(trimmed, ";"), "table %q statement must end with ';':\n%s", table, seg)
- body := strings.TrimSuffix(trimmed, ";")
- return source[:start] + body + "\n" + clause + ";" + source[end:]
-}
-
-// entUintType returns the canonical dump spelling of INT UNSIGNED on srv. The sync
-// driver's columnTypeCanonicalSynonyms map folds the SIGNED default display widths
-// (int(11) -> int) but not the unsigned ones, so a 5.7 dump renders `int(10) unsigned`
-// verbatim while a plain int renders `int` on both versions (an asymmetry the omni
-// canonicalizer absorbs — recorded as an observation in the campaign findings).
-func entUintType(srv liveServer) string {
- if srv.version == "5.7" {
- return "int(10) unsigned"
- }
- return "int unsigned"
-}
-
-// ----------------------------------------------------------------------------
-// Baseline: every corpus loads on every version, and its canonical dump is a fixed
-// point — self-diff empty AND the production schema.SDLMigration no-op empty.
-// ----------------------------------------------------------------------------
-
-//nolint:tparallel
-func TestSDLEnterpriseBaseline(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, corpus := range entCorpora() {
- corpus := corpus
- t.Run(corpus.name, func(t *testing.T) {
- dbName := entLoadCorpus(ctx, t, srv, "entsdl_bl_", corpus)
- meta := syncMetaForDB(ctx, t, srv, dbName)
- tbl, vw, fn, pr, tg := objectCounts(meta)
- t.Logf("[BASELINE %s/%s] loaded: tables=%d views=%d functions=%d procedures=%d triggers=%d (db=%s)",
- srv.name, corpus.name, tbl, vw, fn, pr, tg, dbName)
-
- source := dumpSDL(ctx, t, srv, dbName)
- require.NotEmpty(t, source, "[%s/%s] MetadataToSDL produced empty SDL", srv.name, corpus.name)
-
- selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
- require.NoError(t, err)
- require.Empty(t, selfDiff, "[%s/%s] canonical dump must self-diff empty, got:\n%s",
- srv.name, corpus.name, selfDiff)
-
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, source, meta, srv.version)
- require.NoError(t, err)
- require.Empty(t, noop, "[%s/%s] production-path no-op must be empty, got:\n%s",
- srv.name, corpus.name, noop)
- })
- }
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A1: single-object CRUD — for each object kind, one create, one semantic modify, and
-// one drop, chained on a freshly loaded Zabbix core slice (each phase runs the full
-// oracle protocol from a fresh canonical dump).
-// ----------------------------------------------------------------------------
-
-// entA1Aux seeds the slice with one object of each kind that needs a pre-existing
-// instance to modify/drop, plus a RANGE-partitioned table for the partition kind.
-const entA1Aux = `
-CREATE TABLE ent_ranked (
- id bigint unsigned NOT NULL,
- score integer DEFAULT '0' NOT NULL,
- ratio decimal(5,2) DEFAULT '0.00' NOT NULL,
- owner_userid bigint unsigned NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB;
-CREATE TABLE ent_part_log (
- id bigint unsigned NOT NULL,
- bucket integer NOT NULL,
- note varchar(64) DEFAULT '' NOT NULL,
- PRIMARY KEY (id,bucket)
-) ENGINE=InnoDB
-PARTITION BY RANGE (bucket) (
- PARTITION p0 VALUES LESS THAN (100),
- PARTITION p1 VALUES LESS THAN (200)
-);
-CREATE VIEW ent_v_users AS SELECT userid, username, name FROM users;
-CREATE FUNCTION ent_f_host_count() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM hosts);
-CREATE PROCEDURE ent_p_touch_user(IN uid BIGINT UNSIGNED) BEGIN UPDATE users SET name = name WHERE userid = uid; END;
-CREATE TRIGGER ent_trg_role_bi BEFORE INSERT ON role FOR EACH ROW SET NEW.name = TRIM(NEW.name);
-CREATE EVENT ent_ev_hk ON SCHEDULE EVERY 1 DAY DO DELETE FROM changelog WHERE clock < 0;
-`
-
-// entCRUDPhase is one oracle round (create / modify / drop) within a kind.
-type entCRUDPhase struct {
- name string
- mutate func(t *testing.T, srv liveServer, source string) string
- // want are uppercased substrings the plan must contain.
- want []string
- // exactStmts, when > 0, asserts the plan is exactly this many statements
- // (single-object minimality).
- exactStmts int
-}
-
-// entCRUDKind is the create+modify+drop triple for one object kind.
-type entCRUDKind struct {
- name string
- skip57 string
- phases []entCRUDPhase
-}
-
-func entCRUDKinds() []entCRUDKind {
- return []entCRUDKind{
- {
- name: "table",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + "\nCREATE TABLE ent_crud_t (\n id bigint unsigned NOT NULL,\n label varchar(40) DEFAULT '' NOT NULL,\n PRIMARY KEY (id)\n) ENGINE=InnoDB;\n"
- },
- want: []string{"CREATE TABLE", "ENT_CRUD_T"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "ent_crud_t", ") ENGINE=InnoDB", ") ENGINE=InnoDB COMMENT='ent crud table'")
- },
- want: []string{"ENT_CRUD_T", "COMMENT"},
- exactStmts: 1,
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropTableBlock(t, source, "ent_crud_t")
- },
- want: []string{"DROP TABLE", "ENT_CRUD_T"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "column",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addColumnToTable(t, source, "users", "`ent_crud_col` varchar(50) DEFAULT NULL")
- },
- want: []string{"ALTER TABLE", "ENT_CRUD_COL"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "users", "`ent_crud_col` varchar(50)", "`ent_crud_col` varchar(120)")
- },
- want: []string{"ENT_CRUD_COL", "VARCHAR(120)"},
- exactStmts: 1,
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropLineInTable(t, source, "users", "`ent_crud_col`")
- },
- want: []string{"DROP COLUMN", "ENT_CRUD_COL"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "index_plain",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addIndexToTable(t, source, "users", "KEY `ent_idx_theme` (`theme`)")
- },
- want: []string{"ENT_IDX_THEME"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceLineInTable(t, source, "users", "`ent_idx_theme`", "KEY `ent_idx_theme` (`theme`,`lang`)")
- },
- want: []string{"ENT_IDX_THEME"},
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropLineInTable(t, source, "users", "`ent_idx_theme`")
- },
- want: []string{"ENT_IDX_THEME"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "index_unique",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addIndexToTable(t, source, "users", "UNIQUE KEY `ent_uk_passwd` (`passwd`)")
- },
- want: []string{"ENT_UK_PASSWD", "UNIQUE"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceLineInTable(t, source, "users", "`ent_uk_passwd`", "UNIQUE KEY `ent_uk_passwd` (`passwd`,`lang`)")
- },
- want: []string{"ENT_UK_PASSWD"},
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropLineInTable(t, source, "users", "`ent_uk_passwd`")
- },
- want: []string{"ENT_UK_PASSWD"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "index_prefix",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addIndexToTable(t, source, "hosts", "KEY `ent_idx_desc` (`description`(32))")
- },
- want: []string{"ENT_IDX_DESC", "(32)"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceLineInTable(t, source, "hosts", "`ent_idx_desc`", "KEY `ent_idx_desc` (`description`(64))")
- },
- want: []string{"ENT_IDX_DESC", "(64)"},
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropLineInTable(t, source, "hosts", "`ent_idx_desc`")
- },
- want: []string{"ENT_IDX_DESC"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "index_composite",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addIndexToTable(t, source, "maintenances", "KEY `ent_idx_window` (`active_till`,`maintenance_type`)")
- },
- want: []string{"ENT_IDX_WINDOW"},
- exactStmts: 1,
- },
- {
- name: "modify", // reorder the key parts — a semantic index change
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceLineInTable(t, source, "maintenances", "`ent_idx_window`", "KEY `ent_idx_window` (`maintenance_type`,`active_till`)")
- },
- want: []string{"ENT_IDX_WINDOW"},
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropLineInTable(t, source, "maintenances", "`ent_idx_window`")
- },
- want: []string{"ENT_IDX_WINDOW"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "foreign_key",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addIndexToTable(t, source, "ent_ranked", "CONSTRAINT `ent_fk_owner` FOREIGN KEY (`owner_userid`) REFERENCES `users` (`userid`) ON DELETE SET NULL")
- },
- want: []string{"ENT_FK_OWNER", "FOREIGN KEY"},
- },
- {
- name: "modify", // referential-action change
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "ent_ranked", "ON DELETE SET NULL", "ON DELETE CASCADE")
- },
- want: []string{"ENT_FK_OWNER"},
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropLineInTable(t, source, "ent_ranked", "CONSTRAINT `ent_fk_owner`")
- },
- want: []string{"ENT_FK_OWNER"},
- },
- },
- },
- {
- name: "check",
- skip57: "5.7 parses-and-ignores CHECK constraints (nothing syncs back)",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addIndexToTable(t, source, "ent_ranked", "CONSTRAINT `ent_chk_score` CHECK ((`score` >= 0))")
- },
- want: []string{"ENT_CHK_SCORE", "CHECK"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceLineInTable(t, source, "ent_ranked", "`ent_chk_score`", "CONSTRAINT `ent_chk_score` CHECK ((`score` <= 1000000))")
- },
- want: []string{"ENT_CHK_SCORE"},
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropLineInTable(t, source, "ent_ranked", "`ent_chk_score`")
- },
- want: []string{"ENT_CHK_SCORE"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "view",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + "\nCREATE VIEW ent_crud_v AS SELECT roleid, name FROM role;\n"
- },
- want: []string{"ENT_CRUD_V"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addColumnToView(t, source, "ent_crud_v", "`role`.`type` AS `type`")
- },
- want: []string{"ENT_CRUD_V"},
- exactStmts: 1,
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropObjectBlock(t, source, "VIEW", "ent_crud_v")
- },
- want: []string{"DROP VIEW", "ENT_CRUD_V"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "function",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + "\nCREATE FUNCTION ent_crud_f() RETURNS INT DETERMINISTIC RETURN 41;\n"
- },
- want: []string{"ENT_CRUD_F"},
- exactStmts: 1,
- },
- {
- name: "modify", // body change — this omni build renders it as DROP + CREATE
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return mustReplace(t, source, "RETURN 41", "RETURN 42")
- },
- want: []string{"ENT_CRUD_F"},
- exactStmts: 2,
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropObjectBlock(t, source, "FUNCTION", "ent_crud_f")
- },
- want: []string{"DROP FUNCTION", "ENT_CRUD_F"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "procedure",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + "\nCREATE PROCEDURE ent_crud_p(IN rid BIGINT UNSIGNED) BEGIN UPDATE role SET readonly = readonly WHERE roleid = rid; END;\n"
- },
- want: []string{"ENT_CRUD_P"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return mustReplace(t, source, "SET readonly = readonly", "SET readonly = 0")
- },
- want: []string{"ENT_CRUD_P"},
- exactStmts: 2,
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropObjectBlock(t, source, "PROCEDURE", "ent_crud_p")
- },
- want: []string{"DROP PROCEDURE", "ENT_CRUD_P"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "trigger",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + "\nCREATE TRIGGER ent_crud_trg BEFORE UPDATE ON media_type FOR EACH ROW SET NEW.name = NEW.name;\n"
- },
- want: []string{"ENT_CRUD_TRG"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return mustReplace(t, source, "SET NEW.name = NEW.name", "SET NEW.name = TRIM(NEW.name)")
- },
- want: []string{"ENT_CRUD_TRG"},
- exactStmts: 2,
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropTrigger(t, source, "ent_crud_trg")
- },
- want: []string{"DROP TRIGGER", "ENT_CRUD_TRG"},
- exactStmts: 1,
- },
- },
- },
- {
- name: "event",
- phases: []entCRUDPhase{
- {
- name: "create",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + "\nCREATE EVENT ent_crud_ev ON SCHEDULE EVERY 12 HOUR DO DELETE FROM changelog WHERE clock < 100;\n"
- },
- want: []string{"ENT_CRUD_EV"},
- exactStmts: 1,
- },
- {
- name: "modify",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return mustReplace(t, source, "EVERY 12 HOUR", "EVERY 6 HOUR")
- },
- want: []string{"ENT_CRUD_EV"},
- },
- {
- name: "drop",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entDropObjectNamed(t, source, "EVENT", "ent_crud_ev")
- },
- want: []string{"DROP EVENT", "ENT_CRUD_EV"},
- exactStmts: 1,
- },
- },
- },
- {
- // NOTE (observation, not a failure): for every partition change the differ
- // emits a full `ALTER TABLE ... PARTITION BY` REPARTITION statement rather
- // than the targeted ADD/DROP/REORGANIZE PARTITION — semantically correct and
- // converging, but a whole-table rebuild on large enterprise tables. See the
- // campaign findings.
- name: "partition",
- phases: []entCRUDPhase{
- {
- name: "create", // add a partition
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entSetPartitionClause(t, source, "ent_part_log",
- "PARTITION BY RANGE (`bucket`)\n(PARTITION p0 VALUES LESS THAN (100),\n PARTITION p1 VALUES LESS THAN (200),\n PARTITION p2 VALUES LESS THAN (300))")
- },
- want: []string{"PARTITION BY", "P2"},
- },
- {
- name: "modify", // reorganize: merge p0+p1 into p01
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entSetPartitionClause(t, source, "ent_part_log",
- "PARTITION BY RANGE (`bucket`)\n(PARTITION p01 VALUES LESS THAN (200),\n PARTITION p2 VALUES LESS THAN (300))")
- },
- want: []string{"PARTITION BY", "P01"},
- },
- {
- name: "drop", // drop the p2 partition (the repartition plan re-lists survivors)
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entSetPartitionClause(t, source, "ent_part_log",
- "PARTITION BY RANGE (`bucket`)\n(PARTITION p01 VALUES LESS THAN (200))")
- },
- want: []string{"PARTITION BY", "P01"},
- },
- },
- },
- }
-}
-
-//nolint:tparallel
-func TestSDLEnterpriseSingleObjectCRUD(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, kind := range entCRUDKinds() {
- kind := kind
- t.Run(kind.name, func(t *testing.T) {
- if srv.version == "5.7" && kind.skip57 != "" {
- t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, kind.name, kind.skip57)
- }
- base := entZabbixSlice(t, entSliceCoreTables...) + entA1Aux
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a1", base)
-
- for _, phase := range kind.phases {
- label := srv.name + "/A1/" + kind.name + "/" + phase.name
- source := dumpSDL(ctx, t, srv, dbName)
- target := phase.mutate(t, srv, source)
- plan := entOracle(ctx, t, srv, dbName, target, label)
-
- upper := strings.ToUpper(plan)
- for _, want := range phase.want {
- require.Contains(t, upper, want, "[%s] plan missing %q:\n%s", label, want, plan)
- }
- if phase.exactStmts > 0 {
- require.Equal(t, phase.exactStmts, entPlanStatementCount(t, plan),
- "[%s] plan not minimal (%d stmts expected):\n%s", label, phase.exactStmts, plan)
- }
- }
- })
- }
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A2: dependent objects — the enterprise differentiator. FK chains, trigger→table
-// dependencies (Zabbix changelog pattern), view-on-view stacks, function-used-by-view,
-// and a circular FK pair, each as one or more oracle rounds on the core slice.
-// ----------------------------------------------------------------------------
-
-//nolint:tparallel
-func TestSDLEnterpriseDependentObjects(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- t.Run("fk_chain_extend", func(t *testing.T) {
- // Extend the real item_tag -> items -> hosts -> proxy -> proxy_group chain
- // with two new tables in ONE release: ent_item_note -> items and
- // ent_note_tag -> ent_item_note. Creation must be topological.
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
- source := dumpSDL(ctx, t, srv, dbName)
- target := source + `
-CREATE TABLE ent_item_note (
- noteid bigint unsigned NOT NULL,
- itemid bigint unsigned NOT NULL,
- note varchar(255) DEFAULT '' NOT NULL,
- PRIMARY KEY (noteid),
- KEY ent_item_note_1 (itemid),
- CONSTRAINT ent_c_item_note_1 FOREIGN KEY (itemid) REFERENCES items (itemid) ON DELETE CASCADE
-) ENGINE=InnoDB;
-CREATE TABLE ent_note_tag (
- notetagid bigint unsigned NOT NULL,
- noteid bigint unsigned NOT NULL,
- tag varchar(64) DEFAULT '' NOT NULL,
- PRIMARY KEY (notetagid),
- KEY ent_note_tag_1 (noteid),
- CONSTRAINT ent_c_note_tag_1 FOREIGN KEY (noteid) REFERENCES ent_item_note (noteid) ON DELETE CASCADE
-) ENGINE=InnoDB;
-`
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/fk_chain_extend")
- upper := strings.ToUpper(plan)
- noteIdx := strings.Index(upper, "CREATE TABLE `ENT_ITEM_NOTE`")
- tagIdx := strings.Index(upper, "CREATE TABLE `ENT_NOTE_TAG`")
- require.GreaterOrEqual(t, noteIdx, 0, "plan must create ent_item_note:\n%s", plan)
- require.GreaterOrEqual(t, tagIdx, 0, "plan must create ent_note_tag:\n%s", plan)
- })
-
- t.Run("fk_chain_drop_mid", func(t *testing.T) {
- // Drop the middle of the chain — items — together with its dependents
- // item_tag and item_preproc (each carrying 3 changelog triggers) in ONE
- // release. The plan must drop FKs/tables in dependency order and must not
- // drop a trigger after its table is already gone.
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
- source := dumpSDL(ctx, t, srv, dbName)
- target := source
- for _, trg := range []string{
- "items_insert", "items_update", "items_delete",
- "item_tag_insert", "item_tag_update", "item_tag_delete",
- "item_preproc_insert", "item_preproc_update", "item_preproc_delete",
- } {
- target = dropTrigger(t, target, trg)
- }
- for _, tbl := range []string{"item_tag", "item_preproc", "items"} {
- target = entDropTableBlock(t, target, tbl)
- }
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/fk_chain_drop_mid")
- upper := strings.ToUpper(plan)
- for _, want := range []string{"`ITEMS`", "`ITEM_TAG`", "`ITEM_PREPROC`"} {
- require.Contains(t, upper, want, "plan must drop %s:\n%s", want, plan)
- }
- })
-
- t.Run("trigger_table_widen", func(t *testing.T) {
- // The Zabbix changelog pattern: hosts carries 5 triggers, two of which
- // (hosts_name_upper_*) read/write hosts.name and hosts.name_upper. Widen
- // BOTH columns in one release; the triggers must survive the ALTERs.
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
- source := dumpSDL(ctx, t, srv, dbName)
- target := entReplaceInTable(t, source, "hosts", "`name` varchar(128)", "`name` varchar(190)")
- target = entReplaceInTable(t, target, "hosts", "`name_upper` varchar(128)", "`name_upper` varchar(190)")
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/trigger_table_widen")
- upper := strings.ToUpper(plan)
- require.Contains(t, upper, "VARCHAR(190)", "plan must widen the columns:\n%s", plan)
- require.NotContains(t, upper, "DROP TABLE", "widening must not recreate hosts:\n%s", plan)
- })
-
- t.Run("trigger_modify_one_of_five", func(t *testing.T) {
- // Redefine ONE of hosts' five triggers (hosts_update) and prove the other
- // four are untouched by the plan.
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
- source := dumpSDL(ctx, t, srv, dbName)
- target := mustReplace(t, source, "values (1,old.hostid,2,", "values (1,new.hostid,2,")
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/trigger_modify_one_of_five")
- upper := strings.ToUpper(plan)
- require.Contains(t, upper, "HOSTS_UPDATE", "plan must redefine hosts_update:\n%s", plan)
- for _, untouched := range []string{"HOSTS_INSERT", "HOSTS_DELETE", "HOSTS_NAME_UPPER_INSERT", "HOSTS_NAME_UPPER_UPDATE"} {
- require.NotContains(t, upper, untouched, "plan must not touch %s:\n%s", untouched, plan)
- }
- require.Equal(t, 2, entPlanStatementCount(t, plan), "trigger redefine should be DROP+CREATE:\n%s", plan)
- })
-
- t.Run("view_stack", func(t *testing.T) {
- // 3-deep view stack v3 -> v2 -> v1 -> users: create the whole stack in one
- // release, modify the MIDDLE view, then drop the whole stack.
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
-
- source := dumpSDL(ctx, t, srv, dbName)
- target := source + `
-CREATE VIEW ent_vs1 AS SELECT userid, username, surname FROM users;
-CREATE VIEW ent_vs2 AS SELECT userid, username FROM ent_vs1;
-CREATE VIEW ent_vs3 AS SELECT userid FROM ent_vs2;
-`
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/view_stack/create")
- upper := strings.ToUpper(plan)
- v1 := strings.Index(upper, "`ENT_VS1`")
- v2 := strings.Index(upper, "`ENT_VS2`")
- v3 := strings.Index(upper, "`ENT_VS3`")
- require.True(t, v1 >= 0 && v2 >= 0 && v3 >= 0, "plan must create all three views:\n%s", plan)
- require.True(t, v1 < v2 && v2 < v3, "views must be created base-first (v1<v2<v3):\n%s", plan)
-
- source = dumpSDL(ctx, t, srv, dbName)
- target = addColumnToView(t, source, "ent_vs2", "`ent_vs1`.`surname` AS `surname`")
- plan = entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/view_stack/modify_middle")
- upper = strings.ToUpper(plan)
- require.Contains(t, upper, "VIEW `ENT_VS2`")
- require.NotContains(t, upper, "ENT_VS3", "modifying v2 must not churn v3:\n%s", plan)
- // v2's body legitimately references ent_vs1 in FROM; minimality is proven
- // by the single-statement plan (v1 and v3 are untouched).
- require.Equal(t, 1, entPlanStatementCount(t, plan), "modifying v2 must be a single statement:\n%s", plan)
-
- source = dumpSDL(ctx, t, srv, dbName)
- target = dropObjectBlock(t, source, "VIEW", "ent_vs3")
- target = dropObjectBlock(t, target, "VIEW", "ent_vs2")
- target = dropObjectBlock(t, target, "VIEW", "ent_vs1")
- plan = entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/view_stack/drop")
- require.Equal(t, 3, entPlanStatementCount(t, plan), "stack drop should be exactly 3 DROP VIEW:\n%s", plan)
- })
-
- t.Run("function_used_by_view", func(t *testing.T) {
- // Create a function and a view CALLING it in one release (MySQL rejects a
- // view over a missing function, so creation order is enforced by apply);
- // then drop both in one release.
- //
- // Was PINNED-BUG (view created before the function it calls → Error 1305):
- // fixed by omni #361 (routine creates precede view creates; drops inverse).
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
-
- source := dumpSDL(ctx, t, srv, dbName)
- target := source + `
-CREATE FUNCTION ent_ucount() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM users);
-CREATE VIEW ent_v_fn AS SELECT ent_ucount() AS n;
-`
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/function_used_by_view/create")
- upper := strings.ToUpper(plan)
- require.Contains(t, upper, "ENT_UCOUNT")
- require.Contains(t, upper, "ENT_V_FN")
-
- source = dumpSDL(ctx, t, srv, dbName)
- target = dropObjectBlock(t, source, "VIEW", "ent_v_fn")
- target = dropObjectBlock(t, target, "FUNCTION", "ent_ucount")
- plan = entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/function_used_by_view/drop")
- require.Equal(t, 2, entPlanStatementCount(t, plan), "should be exactly DROP VIEW + DROP FUNCTION:\n%s", plan)
- })
-
- t.Run("circular_fk_pair", func(t *testing.T) {
- // Create two mutually-referencing tables in ONE release. The generated DDL
- // must break the cycle itself (deferred ALTER or FK-checks toggling) — the
- // apply connection runs with default settings.
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
- source := dumpSDL(ctx, t, srv, dbName)
- target := source + `
-CREATE TABLE ent_circ_a (
- aid bigint unsigned NOT NULL,
- bid bigint unsigned NULL,
- PRIMARY KEY (aid),
- KEY ent_circ_a_1 (bid),
- CONSTRAINT ent_c_circ_a FOREIGN KEY (bid) REFERENCES ent_circ_b (bid) ON DELETE SET NULL
-) ENGINE=InnoDB;
-CREATE TABLE ent_circ_b (
- bid bigint unsigned NOT NULL,
- aid bigint unsigned NULL,
- PRIMARY KEY (bid),
- KEY ent_circ_b_1 (aid),
- CONSTRAINT ent_c_circ_b FOREIGN KEY (aid) REFERENCES ent_circ_a (aid) ON DELETE SET NULL
-) ENGINE=InnoDB;
-`
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/circular_fk_pair")
- upper := strings.ToUpper(plan)
- require.Contains(t, upper, "ENT_CIRC_A")
- require.Contains(t, upper, "ENT_CIRC_B")
- })
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A3: special types — AUTO_INCREMENT, generated columns, ENUM/SET members,
-// charset/collation, temporal precision, DECIMAL, TEXT/BLOB families, signedness,
-// prefix-indexed TEXT, and partition operations. Small synthetic bases, one oracle
-// round each.
-// ----------------------------------------------------------------------------
-
-type entTypeCase struct {
- name string
- base string
- skip57 string
- // pinned, when non-empty, skips the case with a PINNED-BUG classification (a
- // confirmed engine bug being fixed upstream; the case re-arms when the skip drops).
- pinned string
- mutate func(t *testing.T, srv liveServer, source string) string
- want []string
- // exactStmts, when > 0, asserts the plan statement count.
- exactStmts int
-}
-
-func entTypeCases() []entTypeCase {
- return []entTypeCase{
- {
- // REPRO: add `seq bigint unsigned NOT NULL AUTO_INCREMENT` + UNIQUE KEY uk_seq
- // (seq) in one release. The plan emits TWO statements — ADD COLUMN, then ADD
- // UNIQUE KEY — and MySQL rejects the first with Error 1075 (an auto column
- // must be defined as a key). The clauses must be grouped into one ALTER.
- name: "ai_add_column",
- base: "CREATE TABLE t3 (id int NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- // Was PINNED-BUG (AI column and its key split into separate ALTERs →
- // Error 1075): fixed by omni #364 (mergeAutoIncrementKeyOps grouping).
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := addColumnToTable(t, source, "t3", "`seq` bigint unsigned NOT NULL AUTO_INCREMENT")
- return addIndexToTable(t, s, "t3", "UNIQUE KEY `uk_seq` (`seq`)")
- },
- want: []string{"SEQ", "AUTO_INCREMENT"},
- },
- {
- name: "ai_drop_attribute",
- base: "CREATE TABLE t3 (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3",
- "`id` int NOT NULL AUTO_INCREMENT", "`id` int NOT NULL")
- },
- want: []string{"`ID`"},
- exactStmts: 1,
- },
- {
- name: "ai_composite_pk_member",
- base: "CREATE TABLE t3 (a int NOT NULL, b int NOT NULL AUTO_INCREMENT, PRIMARY KEY (a,b), KEY idx_b (b)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3",
- "`b` int NOT NULL AUTO_INCREMENT", "`b` bigint NOT NULL AUTO_INCREMENT")
- },
- want: []string{"BIGINT", "AUTO_INCREMENT"},
- exactStmts: 1,
- },
- {
- name: "generated_stored_add",
- base: "CREATE TABLE t3 (id int NOT NULL, price decimal(8,2) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addColumnToTable(t, source, "t3", "`total` decimal(10,2) GENERATED ALWAYS AS ((`price` * 1.1)) STORED")
- },
- want: []string{"TOTAL", "STORED"},
- exactStmts: 1,
- },
- {
- name: "generated_virtual_add",
- base: "CREATE TABLE t3 (id int NOT NULL, price decimal(8,2) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addColumnToTable(t, source, "t3", "`total_v` decimal(10,2) GENERATED ALWAYS AS ((`price` * 2)) VIRTUAL")
- },
- want: []string{"TOTAL_V"},
- exactStmts: 1,
- },
- {
- name: "generated_dependent_alter",
- // Widen the BASE column and change the generated column depending on it in the
- // SAME release — the plan must sequence the two ALTERs so MySQL accepts them.
- base: "CREATE TABLE t3 (id int NOT NULL, price decimal(8,2) NOT NULL, total decimal(10,2) GENERATED ALWAYS AS (price * 2) STORED, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := entReplaceInTable(t, source, "t3", "`price` decimal(8,2)", "`price` decimal(10,2)")
- return entReplaceInTable(t, s, "t3", "* 2", "* 3")
- },
- want: []string{"PRICE"},
- },
- {
- name: "enum_member_add",
- base: "CREATE TABLE t3 (id int NOT NULL, kind enum('a','b') NOT NULL DEFAULT 'a', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3", "enum('a','b')", "enum('a','b','c')")
- },
- want: []string{"ENUM"},
- exactStmts: 1,
- },
- {
- name: "enum_member_remove",
- base: "CREATE TABLE t3 (id int NOT NULL, kind enum('a','b','c') NOT NULL DEFAULT 'a', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3", "enum('a','b','c')", "enum('a','b')")
- },
- want: []string{"ENUM"},
- exactStmts: 1,
- },
- {
- name: "enum_member_reorder",
- base: "CREATE TABLE t3 (id int NOT NULL, kind enum('a','b','c') NOT NULL DEFAULT 'a', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3", "enum('a','b','c')", "enum('c','a','b')")
- },
- want: []string{"ENUM"},
- exactStmts: 1,
- },
- {
- name: "set_member_add_remove",
- base: "CREATE TABLE t3 (id int NOT NULL, tags set('x','y') NOT NULL DEFAULT '', flags set('p','q','r') NOT NULL DEFAULT '', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := entReplaceInTable(t, source, "t3", "set('x','y')", "set('x','y','z')")
- return entReplaceInTable(t, s, "t3", "set('p','q','r')", "set('p','q')")
- },
- want: []string{"SET("},
- },
- {
- // utf8mb4_unicode_ci is non-default on BOTH versions (8.0 default 0900_ai_ci,
- // 5.7 default general_ci), so the dump renders the explicit column COLLATE on
- // both — a stable anchor. When the collation matches the table default the
- // dump omits CHARACTER SET/COLLATE entirely.
- name: "charset_column_collation",
- base: "CREATE TABLE t3 (id int NOT NULL, label varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3",
- "`label` varchar(80) COLLATE utf8mb4_unicode_ci",
- "`label` varchar(80) COLLATE utf8mb4_bin")
- },
- want: []string{"UTF8MB4_BIN"},
- exactStmts: 1,
- },
- {
- name: "charset_table_level",
- base: "CREATE TABLE t3 (id int NOT NULL, title varchar(60) NOT NULL, body varchar(200) DEFAULT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=latin1;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- // Replace the whole table-options line so the (version-dependent) COLLATE
- // suffix can never survive as an invalid latin1/utf8mb4 hybrid. The options
- // line is the segment's last line, so everything from ') ENGINE=InnoDB' to
- // the segment end is rewritten.
- start, end := entTableSegment(t, source, "t3")
- seg := source[start:end]
- optIdx := strings.Index(seg, ") ENGINE=InnoDB")
- require.GreaterOrEqual(t, optIdx, 0, "t3 options line not found:\n%s", seg)
- tail := ""
- if nl := strings.Index(seg[optIdx:], "\n"); nl >= 0 {
- tail = seg[optIdx+nl:]
- }
- newSeg := seg[:optIdx] + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" + tail
- return source[:start] + newSeg + source[end:]
- },
- want: []string{"UTF8MB4"},
- },
- {
- name: "temporal_precision",
- base: "CREATE TABLE t3 (id int NOT NULL, ts timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), dt datetime NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := entReplaceAllInTable(t, source, "t3", "timestamp(3)", "timestamp(6)")
- s = entReplaceAllInTable(t, s, "t3", "CURRENT_TIMESTAMP(3)", "CURRENT_TIMESTAMP(6)")
- return entReplaceInTable(t, s, "t3", "`dt` datetime ", "`dt` datetime(3) ")
- },
- want: []string{"TIMESTAMP(6)", "DATETIME(3)"},
- },
- {
- name: "decimal_widen",
- base: "CREATE TABLE t3 (id int NOT NULL, amount decimal(10,2) NOT NULL DEFAULT '0.00', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3", "decimal(10,2)", "decimal(12,4)")
- },
- want: []string{"DECIMAL(12,4)"},
- exactStmts: 1,
- },
- {
- name: "decimal_narrow",
- base: "CREATE TABLE t3 (id int NOT NULL, amount decimal(12,4) NOT NULL DEFAULT '0.0000', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := entReplaceInTable(t, source, "t3", "decimal(12,4)", "decimal(10,2)")
- return entReplaceInTable(t, s, "t3", "'0.0000'", "'0.00'")
- },
- want: []string{"DECIMAL(10,2)"},
- exactStmts: 1,
- },
- {
- name: "text_blob_family",
- base: "CREATE TABLE t3 (id int NOT NULL, body text, payload blob, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := entReplaceInTable(t, source, "t3", "`body` text", "`body` mediumtext")
- return entReplaceInTable(t, s, "t3", "`payload` blob", "`payload` longblob")
- },
- want: []string{"MEDIUMTEXT", "LONGBLOB"},
- },
- {
- name: "unsigned_signed_flip",
- base: "CREATE TABLE t3 (id int NOT NULL, cnt int unsigned NOT NULL DEFAULT '0', pos int NOT NULL DEFAULT '0', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, srv liveServer, source string) string {
- s := entReplaceInTable(t, source, "t3", "`cnt` "+entUintType(srv), "`cnt` int")
- return entReplaceInTable(t, s, "t3", "`pos` int", "`pos` "+entUintType(srv))
- },
- want: []string{"`CNT`", "`POS`"},
- },
- {
- name: "prefix_indexed_text_type_change",
- base: "CREATE TABLE t3 (id int NOT NULL, body text NOT NULL, PRIMARY KEY (id), KEY idx_body (body(24))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entReplaceInTable(t, source, "t3", "`body` text", "`body` mediumtext")
- },
- want: []string{"MEDIUMTEXT"},
- },
- {
- name: "partition_add",
- base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY RANGE (bucket) (PARTITION p0 VALUES LESS THAN (100), PARTITION p1 VALUES LESS THAN (200));",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entSetPartitionClause(t, source, "t3",
- "PARTITION BY RANGE (`bucket`)\n(PARTITION p0 VALUES LESS THAN (100),\n PARTITION p1 VALUES LESS THAN (200),\n PARTITION p2 VALUES LESS THAN (300))")
- },
- want: []string{"P2"},
- },
- {
- name: "partition_drop",
- base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY RANGE (bucket) (PARTITION p0 VALUES LESS THAN (100), PARTITION p1 VALUES LESS THAN (200), PARTITION p2 VALUES LESS THAN (300));",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entSetPartitionClause(t, source, "t3",
- "PARTITION BY RANGE (`bucket`)\n(PARTITION p0 VALUES LESS THAN (100),\n PARTITION p1 VALUES LESS THAN (200))")
- },
- // The differ emits a full repartition listing only the survivors (see the
- // partition-minimality observation in the campaign findings).
- want: []string{"PARTITION BY", "P0"},
- },
- {
- name: "partition_reorganize",
- base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY RANGE (bucket) (PARTITION p0 VALUES LESS THAN (100), PARTITION p1 VALUES LESS THAN (200), PARTITION pmax VALUES LESS THAN MAXVALUE);",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entSetPartitionClause(t, source, "t3",
- "PARTITION BY RANGE (`bucket`)\n(PARTITION p01 VALUES LESS THAN (200),\n PARTITION pmax VALUES LESS THAN MAXVALUE)")
- },
- want: []string{"P01"},
- },
- {
- name: "partition_existing_table",
- base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entAppendTableClause(t, source, "t3", "PARTITION BY HASH (`bucket`)\nPARTITIONS 4")
- },
- want: []string{"PARTITION BY", "HASH"},
- },
- {
- name: "departition",
- base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY HASH (bucket) PARTITIONS 4;",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return entSetPartitionClause(t, source, "t3", "")
- },
- want: []string{"T3"},
- },
- }
-}
-
-//nolint:tparallel
-func TestSDLEnterpriseSpecialTypes(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, tc := range entTypeCases() {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- if srv.version == "5.7" && tc.skip57 != "" {
- t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, tc.name, tc.skip57)
- }
- if tc.pinned != "" {
- t.Skip(tc.pinned)
- }
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a3", tc.base)
- source := dumpSDL(ctx, t, srv, dbName)
- target := tc.mutate(t, srv, source)
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A3/"+tc.name)
-
- upper := strings.ToUpper(plan)
- for _, want := range tc.want {
- require.Contains(t, upper, want, "[%s/A3/%s] plan missing %q:\n%s", srv.name, tc.name, want, plan)
- }
- if tc.exactStmts > 0 {
- require.Equal(t, tc.exactStmts, entPlanStatementCount(t, plan),
- "[%s/A3/%s] plan not minimal:\n%s", srv.name, tc.name, plan)
- }
- })
- }
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A5(a): sequential chain S0 -> S1 -> S2 -> S3 -> S4 on the core slice. Every step must
-// converge, and the endpoint must be REACHABLE FROM S0 IN ONE SHOT on a second database
-// — both databases must dump to the same canonical schema.
-// ----------------------------------------------------------------------------
-
-//nolint:tparallel
-func TestSDLEnterpriseSequentialChain(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- base := entZabbixSlice(t, entSliceCoreTables...)
- chainDB := entLoadDDL(ctx, t, srv, "entsdl_a5chain", base)
-
- // The chain targets are built by cumulative TEXTUAL mutation of the canonical
- // S0 dump, so the final text is identical for the chained and one-shot legs.
- s0 := dumpSDL(ctx, t, srv, chainDB)
-
- // The appended blocks use backticked names so the later chain steps can locate
- // and mutate them with the same segment helpers that work on canonical dumps.
- s1 := addColumnToTable(t, s0, "users", "`ent_ch_flag` int NOT NULL DEFAULT '0'")
- s1 += `
-CREATE TABLE ` + "`ent_ch_audit`" + ` (
- auditid bigint unsigned NOT NULL,
- userid bigint unsigned NULL,
- note varchar(60) DEFAULT '' NOT NULL,
- PRIMARY KEY (auditid)
-) ENGINE=InnoDB;
-`
- s2 := addIndexToTable(t, s1, "users", "KEY `ent_ch_idx` (`ent_ch_flag`)")
- s2 += `
-CREATE VIEW ` + "`ent_ch_v`" + ` AS SELECT auditid, note FROM ent_ch_audit;
-`
- s3 := addIndexToTable(t, s2, "ent_ch_audit", "KEY `ent_ch_fk` (`userid`)")
- s3 = addIndexToTable(t, s3, "ent_ch_audit", "CONSTRAINT `ent_ch_fk` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON DELETE SET NULL")
- s3 = entReplaceInTable(t, s3, "maintenances", "`name` varchar(128)", "`name` varchar(190)")
- s3 += `
-CREATE TRIGGER ent_ch_trg BEFORE INSERT ON ent_ch_audit FOR EACH ROW SET NEW.note = TRIM(NEW.note);
-`
- s4 := entDropLineInTable(t, s3, "users", "`ent_ch_idx`")
- s4 = addColumnToView(t, s4, "ent_ch_v", "`ent_ch_audit`.`userid` AS `userid`")
- s4 += `
-CREATE PROCEDURE ent_ch_p(IN aid BIGINT UNSIGNED) BEGIN DELETE FROM ent_ch_audit WHERE auditid = aid; END;
-`
- for i, step := range []string{s1, s2, s3, s4} {
- entOracle(ctx, t, srv, chainDB, step, srv.name+"/A5/chain/S"+string(rune('1'+i)))
- }
-
- // One-shot leg: a second fresh database goes S0 -> S4 in a single release.
- oneshotDB := entLoadDDL(ctx, t, srv, "entsdl_a5one", base)
- entOracle(ctx, t, srv, oneshotDB, s4, srv.name+"/A5/chain/oneshot")
-
- // Same endpoint: the two databases' canonical dumps must be equivalent.
- chainDump := dumpSDL(ctx, t, srv, chainDB)
- oneshotDump := dumpSDL(ctx, t, srv, oneshotDB)
- diffAB, err := mysqlDiffSDLMigration(chainDump, oneshotDump, srv.version)
- require.NoError(t, err)
- require.Empty(t, diffAB, "chained and one-shot endpoints diverge (chain->oneshot):\n%s", diffAB)
- diffBA, err := mysqlDiffSDLMigration(oneshotDump, chainDump, srv.version)
- require.NoError(t, err)
- require.Empty(t, diffBA, "chained and one-shot endpoints diverge (oneshot->chain):\n%s", diffBA)
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A5(b): apply-back — migrate B -> T (destructive + additive mix), then T -> B using the
-// ORIGINAL canonical dump as the target; the database must land exactly back on B.
-// ----------------------------------------------------------------------------
-
-//nolint:tparallel
-func TestSDLEnterpriseApplyBack(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a5back", entZabbixSlice(t, entSliceCoreTables...))
- c0 := dumpSDL(ctx, t, srv, dbName)
-
- // Forward: drop a plain index, drop an unindexed column, widen a column, and
- // add a table — a realistic mixed release.
- target := entDropLineInTable(t, c0, "users", "`users_2`")
- target = entDropLineInTable(t, target, "users", "`attempt_ip`")
- target = entReplaceInTable(t, target, "media_type", "`smtp_helo` varchar(255)", "`smtp_helo` varchar(300)")
- target += `
-CREATE TABLE ent_ab_t (
- id bigint unsigned NOT NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB;
-`
- entOracle(ctx, t, srv, dbName, target, srv.name+"/A5/apply_back/forward")
-
- // Backward: the original canonical dump is the target again.
- entOracle(ctx, t, srv, dbName, c0, srv.name+"/A5/apply_back/backward")
-
- // The database must be exactly B again (canonical-dump equivalence).
- final := dumpSDL(ctx, t, srv, dbName)
- backDiff, err := mysqlDiffSDLMigration(final, c0, srv.version)
- require.NoError(t, err)
- require.Empty(t, backDiff, "apply-back did not restore B:\n%s", backDiff)
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A5(c): combined release — table + column + index + FK + view + trigger + routine all
-// changed in ONE diff.
-// ----------------------------------------------------------------------------
-
-// entA5cAux seeds the view/function the combined release modifies.
-const entA5cAux = `
-CREATE VIEW ent_cmb_v AS SELECT roleid, name FROM role;
-CREATE FUNCTION ent_cmb_f() RETURNS INT DETERMINISTIC RETURN 7;
-`
-
-//nolint:tparallel
-func TestSDLEnterpriseCombinedRelease(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- base := entZabbixSlice(t, entSliceCoreTables...) + entA5cAux
- dbName := entLoadDDL(ctx, t, srv, "entsdl_a5cmb", base)
- source := dumpSDL(ctx, t, srv, dbName)
-
- // One release, seven object kinds:
- target := addColumnToTable(t, source, "media_type", "`ent_cmb_col` varchar(32) DEFAULT NULL") // column
- target = addIndexToTable(t, target, "users", "KEY `ent_cmb_idx` (`theme`)") // index
- target = addColumnToTable(t, target, "dchecks", "`ent_cmb_ref` bigint unsigned NULL") // FK column +
- target = addIndexToTable(t, target, "dchecks", "KEY `ent_cmb_fk` (`ent_cmb_ref`)")
- target = addIndexToTable(t, target, "dchecks", "CONSTRAINT `ent_cmb_fk` FOREIGN KEY (`ent_cmb_ref`) REFERENCES `connector` (`connectorid`) ON DELETE SET NULL") // FK
- target = addColumnToView(t, target, "ent_cmb_v", "`role`.`type` AS `type`") // view
- target = mustReplace(t, target, "RETURN 7", "RETURN 8") // routine
- target += `
-CREATE TABLE ent_cmb_t (
- id bigint unsigned NOT NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB;
-CREATE TRIGGER ent_cmb_trg BEFORE UPDATE ON maintenances FOR EACH ROW SET NEW.name = TRIM(NEW.name);
-`
- plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A5/combined_release")
- upper := strings.ToUpper(plan)
- for _, want := range []string{
- "ENT_CMB_COL", "ENT_CMB_IDX", "ENT_CMB_REF", "ENT_CMB_FK",
- "ENT_CMB_V", "ENT_CMB_F", "ENT_CMB_T", "ENT_CMB_TRG",
- } {
- require.Contains(t, upper, want, "combined release plan missing %q:\n%s", want, plan)
- }
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A5(e): scale timing guard on the largest corpus (OpenEMR, 283 tables) — the no-op
-// diff must stay under 30s, and a single-column change must be exactly one ALTER,
-// also computed under 30s.
-// ----------------------------------------------------------------------------
-
-//nolint:tparallel
-func TestSDLEnterpriseScaleGuard(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
- const budget = 30 * time.Second
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- dbName := entLoadDDL(ctx, t, srv, "entsdl_scale", entOpenemrSQL)
- source := dumpSDL(ctx, t, srv, dbName)
-
- startNoop := time.Now()
- selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
- noopDur := time.Since(startNoop)
- require.NoError(t, err)
- require.Empty(t, selfDiff, "openemr no-op diff must be empty:\n%s", selfDiff)
- t.Logf("[SCALE %s] openemr no-op diff: %v (budget %v)", srv.name, noopDur, budget)
- require.Less(t, noopDur, budget, "openemr no-op diff exceeded the %v budget", budget)
-
- // Single-column change: exactly one ALTER, computed within budget.
- target := addColumnToTable(t, source, "patient_data", "`ent_scale_col` varchar(40) DEFAULT NULL")
- startOne := time.Now()
- plan, err := mysqlDiffSDLMigration(source, target, srv.version)
- oneDur := time.Since(startOne)
- require.NoError(t, err)
- t.Logf("[SCALE %s] openemr single-column diff: %v, plan:\n%s", srv.name, oneDur, plan)
- require.Less(t, oneDur, budget, "openemr single-column diff exceeded the %v budget", budget)
- require.Equal(t, 1, entPlanStatementCount(t, plan), "single-column change must be exactly 1 statement:\n%s", plan)
- upper := strings.ToUpper(plan)
- require.Contains(t, upper, "ALTER TABLE")
- require.Contains(t, upper, "ENT_SCALE_COL")
-
- // Close the loop: apply + converge (the full oracle contract at scale).
- require.NoError(t, applyDDL(ctx, t, srv, dbName, plan), "scale plan failed to apply:\n%s", plan)
- after := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(after, target, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "openemr single-column change did not converge:\n%s", converge)
- })
- }
-}
-
-// ----------------------------------------------------------------------------
-// A5(f): multi-file export round-trip at enterprise scale — for every corpus, the
-// concatenated GetMultiFileDatabaseDefinition output must describe the identical schema
-// as the single-file dump (diff empty both ways) and must be self-idempotent.
-// ----------------------------------------------------------------------------
-
-//nolint:tparallel
-func TestSDLEnterpriseMultiFileRoundTrip(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, corpus := range entCorpora() {
- corpus := corpus
- t.Run(corpus.name, func(t *testing.T) {
- dbName := entLoadCorpus(ctx, t, srv, "entsdl_mf_", corpus)
- meta := syncMetaForDB(ctx, t, srv, dbName)
-
- result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
- SkipBackupSchema: true,
- }, meta.GetProto())
- require.NoError(t, err)
- require.NotEmpty(t, result.Files, "[%s/%s] multi-file export produced no files", srv.name, corpus.name)
-
- single := dumpSDL(ctx, t, srv, dbName)
- concat := concatMultiFile(result)
- t.Logf("[MULTIFILE %s/%s] %d files, concat %d bytes, single %d bytes",
- srv.name, corpus.name, len(result.Files), len(concat), len(single))
-
- forward, err := mysqlDiffSDLMigration(concat, single, srv.version)
- require.NoError(t, err)
- require.Empty(t, forward, "[%s/%s] concat(multi-file) != single dump (concat->single):\n%s",
- srv.name, corpus.name, forward)
- backward, err := mysqlDiffSDLMigration(single, concat, srv.version)
- require.NoError(t, err)
- require.Empty(t, backward, "[%s/%s] concat(multi-file) != single dump (single->concat):\n%s",
- srv.name, corpus.name, backward)
- self, err := mysqlDiffSDLMigration(concat, concat, srv.version)
- require.NoError(t, err)
- require.Empty(t, self, "[%s/%s] concat(multi-file) not idempotent:\n%s", srv.name, corpus.name, self)
- })
- }
- })
- }
-}
diff --git a/backend/plugin/schema/mysql/sdl_enterprise_upgrade_test.go b/backend/plugin/schema/mysql/sdl_enterprise_upgrade_test.go
deleted file mode 100644
index f5f486ba6d..0000000000
--- a/backend/plugin/schema/mysql/sdl_enterprise_upgrade_test.go
+++ /dev/null
@@ -1,457 +0,0 @@
-package mysql
-
-// A7 of the ENTERPRISE smoke axes (see sdl_enterprise_test.go): CROSS-VERSION upgrade —
-// the migration-during-upgrade scenario where a schema is authored/dumped on one MySQL
-// version and the target is applied on the other.
-//
-// Legs, per corpus (zabbix / prestashop / openemr; sys stays behind its entCorpora gates):
-//
-// upgrade_57_dump_targets_80_db — load the corpus on 5.7, take its canonical dump D57,
-// load the SAME original DDL on 8.0, then use D57 as the TARGET against the 8.0
-// database (version-aware diff as 8.0.32). The plan must apply cleanly and
-// converge; 5.7-isms (integer display widths) must normalize away — asserted by the
-// no-width-in-ALTER guard — and nothing may phantom-drop. Genuine semantic
-// differences (the server-default charset a version applied at load time) are
-// LEGITIMATE plan content: a 5.7-authored dump of a charset-less corpus says
-// latin1, and declarative semantics honor it.
-// minimal_mutation_after_upgrade — (zabbix) one semantic edit (add column + index) on
-// top of D57, targeted at the upgraded 8.0 database → exactly the minimal DDL, no
-// cross-version noise re-emerging.
-// fresh_80_from_57_dump — D57 against an EMPTY 8.0 database (fresh create from
-// a 5.7-authored dump) → apply → re-dump → converges to D57 and self-diffs empty.
-// downgrade_80_dump_targets_57_db — the reverse: D80 as target on the 5.7 database.
-// Probed: where an 8.0-ism blocks the 5.7 apply (utf8mb4_0900_ai_ci — 8.0's default
-// utf8mb4 collation does not exist on 5.7), the leg skips with the named reason.
-// aligned_zabbix_utf8mb4_general_ci — the distilled asymmetry probe: both databases
-// created with the SAME 5.7-legal default (utf8mb4/utf8mb4_general_ci) before
-// loading zabbix, so the only cross-dump differences left are pure version
-// renderings (display widths). Both cross-diffs must then be EMPTY — any residual
-// is a phantom diff by definition.
-//
-// prestashop note: the corpus itself leads with SET SESSION sql_mode='' (two
-// '0000-00-00 00:00:00' defaults need non-strict mode). Generated plans cannot carry
-// session state, so plan APPLICATION for that corpus is prefixed with the corpus's own
-// preamble — the exact concession the corpus already requires at load time.
-//
-// Shared helpers (liveServers, entLoadDDL, dumpSDL, applyDDL, entPlanStatementCount,
-// entNormalizeDelimiters, newLiveDatabase, createLiveMySQLDriver, addColumnToTable,
-// addIndexToTable, entCorpora + embedded corpora) come from the sibling files.
-
-import (
- "context"
- "fmt"
- "regexp"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/plugin/db"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
-)
-
-// Full server versions are threaded into the diff for this axis (the release path passes
-// the synced server version string, e.g. "8.0.32", not a bare major.minor).
-const (
- entUpg57Version = "5.7.25"
- entUpg80Version = "8.0.32"
-)
-
-type entUpgCorpus struct {
- name string
- ddl string
- // preamble is prepended to every generated plan before application (see the
- // prestashop note above). It mirrors the corpus's own leading session setup.
- preamble string
- // gate, when non-empty, skips the corpus with the given reason.
- gate string
-}
-
-// entUpgCorpora derives the cross-version corpus list from the shared entCorpora().
-// The axis authors the SAME original DDL on BOTH versions, so a corpus gated on either
-// version (sys: gate + gate57) is gated here too.
-//
-// openemr note: the corpus's uuid columns (`uuid` binary(16) NOT NULL DEFAULT ”) once
-// gated it out of this axis — the 8.0 sync stored the information_schema hex NOTATION
-// ('0x…') verbatim and every cross-version leg looped on a phantom
-// `MODIFY COLUMN ... DEFAULT '0x…'` residual. The sync now canonicalizes binary-family
-// defaults (see TestSDLEnterpriseUpgradeBinaryDefaultCanonicalForm), so the corpus runs
-// un-gated.
-func entUpgCorpora() []entUpgCorpus {
- var out []entUpgCorpus
- for _, c := range entCorpora() {
- u := entUpgCorpus{name: c.name, ddl: c.ddl, gate: c.gate}
- if u.gate == "" && c.gate57 != "" {
- u.gate = c.gate57
- }
- if u.gate == "" && c.canonical {
- u.gate = "corpus ships as a canonical dump requiring the engine create-plan loader; A7 authors from raw upstream DDL"
- }
- if c.name == "prestashop" {
- u.preamble = "SET SESSION sql_mode='';\n"
- }
- out = append(out, u)
- }
- return out
-}
-
-func entUpgServers(t *testing.T) (srv57, srv80 liveServer) {
- t.Helper()
- found57, found80 := false, false
- for _, srv := range liveServers {
- switch srv.version {
- case "5.7":
- srv57, found57 = srv, true
- case "8.0":
- srv80, found80 = srv, true
- default:
- }
- }
- require.True(t, found57 && found80, "cross-version axis needs both a 5.7 and an 8.0 live server")
- return srv57, srv80
-}
-
-// entUpgTrim caps logged plan/residual text so a 250-table plan stays readable.
-func entUpgTrim(s string) string {
- const limit = 6000
- if len(s) <= limit {
- return s
- }
- return s[:limit] + fmt.Sprintf("\n... (%d more bytes)", len(s)-limit)
-}
-
-// entUpgReDrop matches destructive operations that can NEVER be legitimate when both
-// databases were loaded from the same original DDL — any hit is a phantom diff.
-var entUpgReDrop = regexp.MustCompile(`(?i)\bDROP\s+(TABLE|COLUMN|TRIGGER|VIEW|FUNCTION|PROCEDURE|INDEX|KEY|CHECK|CONSTRAINT|FOREIGN)`)
-
-func entUpgAssertNoDrops(t *testing.T, label, plan string) {
- t.Helper()
- if m := entUpgReDrop.FindString(plan); m != "" {
- require.Failf(t, "phantom destructive operation in cross-version plan",
- "[%s] plan contains %q — with identical source DDL on both versions nothing may be dropped:\n%s",
- label, m, entUpgTrim(plan))
- }
-}
-
-// entUpgReIntWidth matches integer display widths — the 5.7 stored form 8.0 must
-// normalize away. tinyint(1) is exempt (the BOOLEAN spelling, canonical on BOTH
-// versions).
-var entUpgReIntWidth = regexp.MustCompile(`(?i)\b(?:tiny|small|medium|big)?int\(\d+\)`)
-
-// entUpgAssertNoWidthInAlters proves no ALTER statement carries a 5.7 display width —
-// a width inside an ALTER means the 5.7 rendering leaked through normalization and
-// phantom-modified a column. CREATE TABLE statements are not judged: a pass-through
-// width in a fresh CREATE is cosmetic, converges, and is not a phantom diff.
-func entUpgAssertNoWidthInAlters(t *testing.T, label, plan string) {
- t.Helper()
- if strings.TrimSpace(plan) == "" {
- return
- }
- stmts, err := mysqlparser.SplitSQL(plan)
- require.NoError(t, err, "[%s] split plan for width guard", label)
- for _, s := range stmts {
- text := strings.TrimSpace(s.Text)
- if !strings.HasPrefix(strings.ToUpper(text), "ALTER TABLE") {
- continue
- }
- for _, m := range entUpgReIntWidth.FindAllString(text, -1) {
- if strings.EqualFold(m, "tinyint(1)") {
- continue
- }
- require.Failf(t, "integer display width leaked into a cross-version ALTER",
- "[%s] ALTER carries %q (5.7 display width not normalized away):\n%s", label, m, text)
- }
- }
-}
-
-// entUpgConverge diffs the live database against target (as version), applies the plan
-// (with the corpus preamble), and proves convergence + idempotence. Returns the plan
-// ("" when the schemas were already equivalent).
-func entUpgConverge(ctx context.Context, t *testing.T, srv liveServer, dbName, target, version, preamble, label string) string {
- t.Helper()
- source := dumpSDL(ctx, t, srv, dbName)
- plan, err := mysqlDiffSDLMigration(source, target, version)
- require.NoError(t, err, "[%s] diff", label)
- if strings.TrimSpace(plan) == "" {
- t.Logf("[%s] empty plan — schemas already equivalent", label)
- } else {
- t.Logf("[%s] plan: %d statements, %d bytes:\n%s", label, entPlanStatementCount(t, plan), len(plan), entUpgTrim(plan))
- require.NoError(t, applyDDL(ctx, t, srv, dbName, preamble+plan),
- "[%s] generated plan failed to apply:\n%s", label, entUpgTrim(plan))
- }
- after := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(after, target, version)
- require.NoError(t, err, "[%s] converge diff", label)
- require.Empty(t, converge, "[%s] did not converge; residual:\n%s", label, entUpgTrim(converge))
- self, err := mysqlDiffSDLMigration(after, after, version)
- require.NoError(t, err, "[%s] idempotence diff", label)
- require.Empty(t, self, "[%s] post-apply dump not idempotent:\n%s", label, entUpgTrim(self))
- return plan
-}
-
-// entUpgLoadAligned creates a database whose DEFAULT charset/collation is pinned to the
-// 5.7-legal utf8mb4/utf8mb4_general_ci on either server, then loads ddl — removing the
-// server-default charset divergence so cross-dumps differ only by version renderings.
-func entUpgLoadAligned(ctx context.Context, t *testing.T, srv liveServer, prefix, ddl string) string {
- t.Helper()
- dbName := newLiveDatabase(ctx, t, srv, prefix)
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, "ALTER DATABASE `"+dbName+"` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", db.ExecuteOptions{})
- require.NoError(t, err, "[%s] align database default collation", srv.name)
- _, err = driver.Execute(ctx, entNormalizeDelimiters(ddl), db.ExecuteOptions{})
- require.NoError(t, err, "[%s] apply aligned base DDL", srv.name)
- return dbName
-}
-
-//nolint:tparallel
-func TestSDLEnterpriseCrossVersionUpgrade(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
- srv57, srv80 := entUpgServers(t)
-
- for _, corpus := range entUpgCorpora() {
- corpus := corpus
- t.Run(corpus.name, func(t *testing.T) {
- if corpus.gate != "" {
- t.Skipf("[A7/%s] %s", corpus.name, corpus.gate)
- }
-
- // Author on 5.7 and on 8.0 from the SAME original DDL; capture both
- // canonical dumps up front so later legs judge fixed texts.
- db57 := entLoadDDL(ctx, t, srv57, "entsdl_up57_"+corpus.name, corpus.ddl)
- d57 := dumpSDL(ctx, t, srv57, db57)
- db80 := entLoadDDL(ctx, t, srv80, "entsdl_up80_"+corpus.name, corpus.ddl)
- d80 := dumpSDL(ctx, t, srv80, db80)
- t.Logf("[A7/%s] D57 %d bytes, D80 %d bytes", corpus.name, len(d57), len(d80))
-
- t.Run("upgrade_57_dump_targets_80_db", func(t *testing.T) {
- label := "A7/" + corpus.name + "/upgrade_57_to_80"
- plan := entUpgConverge(ctx, t, srv80, db80, d57, entUpg80Version, corpus.preamble, label)
- entUpgAssertNoDrops(t, label, plan)
- entUpgAssertNoWidthInAlters(t, label, plan)
- })
-
- if corpus.name == "zabbix" {
- t.Run("minimal_mutation_after_upgrade", func(t *testing.T) {
- label := "A7/" + corpus.name + "/minimal_mutation_after_upgrade"
- // Self-sufficient: land the database on D57 first (a no-op when the
- // upgrade leg just ran).
- entUpgConverge(ctx, t, srv80, db80, d57, entUpg80Version, corpus.preamble, label+"/pre")
-
- // One semantic edit on top of the 5.7-authored dump.
- target := addColumnToTable(t, d57, "users", "`ent_up_col` varchar(50) DEFAULT NULL")
- target = addIndexToTable(t, target, "users", "KEY `ent_up_idx` (`ent_up_col`)")
-
- source := dumpSDL(ctx, t, srv80, db80)
- plan, err := mysqlDiffSDLMigration(source, target, entUpg80Version)
- require.NoError(t, err, "[%s] diff", label)
- require.NotEmpty(t, plan, "[%s] the semantic edit must produce a plan", label)
- t.Logf("[%s] plan:\n%s", label, plan)
-
- n := entPlanStatementCount(t, plan)
- require.LessOrEqual(t, n, 2, "[%s] plan must be the minimal DDL (add column + add index), got %d statements:\n%s", label, n, plan)
- stmts, err := mysqlparser.SplitSQL(plan)
- require.NoError(t, err)
- for _, s := range stmts {
- text := strings.TrimSpace(s.Text)
- if text == "" || text == ";" {
- continue
- }
- require.True(t, strings.HasPrefix(strings.ToUpper(text), "ALTER TABLE `USERS`"),
- "[%s] plan statement strays off the edited table (cross-version noise re-emerged):\n%s", label, text)
- }
- upper := strings.ToUpper(plan)
- require.Contains(t, upper, "ENT_UP_COL", "[%s] plan missing the added column:\n%s", label, plan)
- require.Contains(t, upper, "ENT_UP_IDX", "[%s] plan missing the added index:\n%s", label, plan)
-
- require.NoError(t, applyDDL(ctx, t, srv80, db80, plan), "[%s] minimal plan failed to apply:\n%s", label, plan)
- after := dumpSDL(ctx, t, srv80, db80)
- converge, err := mysqlDiffSDLMigration(after, target, entUpg80Version)
- require.NoError(t, err)
- require.Empty(t, converge, "[%s] minimal mutation did not converge:\n%s", label, converge)
- })
- }
-
- t.Run("fresh_80_from_57_dump", func(t *testing.T) {
- label := "A7/" + corpus.name + "/fresh_80_from_57_dump"
- dbEmpty := newLiveDatabase(ctx, t, srv80, "entsdl_upe80_"+corpus.name)
- plan := entUpgConverge(ctx, t, srv80, dbEmpty, d57, entUpg80Version, corpus.preamble, label)
- require.NotEmpty(t, plan, "[%s] creating from a 5.7-authored dump on an empty database must emit DDL", label)
- entUpgAssertNoWidthInAlters(t, label, plan)
- })
-
- t.Run("downgrade_80_dump_targets_57_db", func(t *testing.T) {
- label := "A7/" + corpus.name + "/downgrade_80_to_57"
- source := dumpSDL(ctx, t, srv57, db57)
- plan, err := mysqlDiffSDLMigration(source, d80, entUpg57Version)
- require.NoError(t, err, "[%s] diff", label)
- if strings.TrimSpace(plan) != "" {
- t.Logf("[%s] plan: %d statements, %d bytes:\n%s", label, entPlanStatementCount(t, plan), len(plan), entUpgTrim(plan))
- if applyErr := applyDDL(ctx, t, srv57, db57, corpus.preamble+plan); applyErr != nil {
- // Probe outcome: an 8.0-authored dump can carry 8.0-isms no 5.7
- // server accepts. The only one these corpora produce is 8.0's
- // utf8mb4 default collation.
- if strings.Contains(plan, "utf8mb4_0900_ai_ci") {
- t.Skipf("[%s] 8.0-authored dump is not 5.7-legal: plan carries utf8mb4_0900_ai_ci "+
- "(8.0's utf8mb4 default collation, unknown to 5.7); apply failed as expected: %v", label, applyErr)
- }
- require.NoErrorf(t, applyErr, "[%s] downgrade plan failed to apply and no known 8.0-ism explains it:\n%s",
- label, entUpgTrim(plan))
- }
- }
- after := dumpSDL(ctx, t, srv57, db57)
- converge, err := mysqlDiffSDLMigration(after, d80, entUpg57Version)
- require.NoError(t, err, "[%s] converge diff", label)
- require.Empty(t, converge, "[%s] did not converge; residual:\n%s", label, entUpgTrim(converge))
- self, err := mysqlDiffSDLMigration(after, after, entUpg57Version)
- require.NoError(t, err)
- require.Empty(t, self, "[%s] post-apply dump not idempotent:\n%s", label, entUpgTrim(self))
- })
- })
- }
-
- // The distilled asymmetry probe: with the server-default charset divergence removed,
- // BOTH cross-diffs must be empty — the remaining differences are exactly the
- // version renderings (5.7 display widths) the normalizer must absorb.
- t.Run("aligned_zabbix_utf8mb4_general_ci", func(t *testing.T) {
- db57 := entUpgLoadAligned(ctx, t, srv57, "entsdl_upal57", entZabbixSQL)
- db80 := entUpgLoadAligned(ctx, t, srv80, "entsdl_upal80", entZabbixSQL)
- d57 := dumpSDL(ctx, t, srv57, db57)
- d80 := dumpSDL(ctx, t, srv80, db80)
-
- t.Run("57_dump_targets_80_db", func(t *testing.T) {
- cur := dumpSDL(ctx, t, srv80, db80)
- plan, err := mysqlDiffSDLMigration(cur, d57, entUpg80Version)
- require.NoError(t, err, "aligned 57->80 diff")
- require.Empty(t, plan,
- "collation-aligned 5.7 dump phantom-diffs an equivalent 8.0 database (widths/collation defaults did not normalize away):\n%s",
- entUpgTrim(plan))
- })
- t.Run("80_dump_targets_57_db", func(t *testing.T) {
- cur := dumpSDL(ctx, t, srv57, db57)
- plan, err := mysqlDiffSDLMigration(cur, d80, entUpg57Version)
- require.NoError(t, err, "aligned 80->57 diff")
- require.Empty(t, plan,
- "collation-aligned 8.0 dump phantom-diffs an equivalent 5.7 database (widths/collation defaults did not normalize away):\n%s",
- entUpgTrim(plan))
- })
- })
-}
-
-// ----------------------------------------------------------------------------
-// REGRESSION (formerly the pinned bug this axis found, openemr corpus): binary-family
-// column defaults.
-//
-// MySQL reports binary/varbinary literal defaults in information_schema.COLUMNS in
-// version-specific encodings: 8.0 as hex NOTATION text built from the value truncated
-// at its first NUL byte ("0x" for DEFAULT '', "0x6162" for DEFAULT 'ab'), 5.7 as the
-// RAW BYTES, NUL-padded to the declared width for binary(N). The sync used to store
-// the 8.0 notation verbatim and the dumper re-quoted it as a STRING literal
-// (DEFAULT '0x6162') — value-unfaithful dumps, cross-version phantom MODIFY loops (the
-// openemr uuid columns that once gated that corpus out of the A7 legs), and round-trip
-// double-encoding ('0x6162' -> '0x307836313632').
-//
-// The sync now decodes both encodings into one canonical form — '' when the value is
-// empty (binary(N) padding stripped), a plain quoted string for clean text, an
-// unquoted hex literal otherwise; see canonicalBinaryDefault in
-// backend/plugin/db/mysql/sync.go. This test pins the fixed behavior: value-faithful
-// dumps on both versions, EMPTY cross-version diffs (the DDL pins the table charset so
-// the dumps may differ by nothing at all), and a clean round-trip through a fresh 8.0
-// database. The per-version legs then cover the hex-literal fallback for values no
-// quoted SDL string can carry — probed on the version whose information_schema reports
-// them faithfully (0xFF61 on 8.0; a significant trailing NUL, 0x6100, on 5.7).
-// ----------------------------------------------------------------------------
-
-const entUpgBinaryDefaultDDL = `
-CREATE TABLE ent_up_bin (
- id int NOT NULL,
- u binary(16) NOT NULL DEFAULT '',
- v varbinary(24) NOT NULL DEFAULT 'ab',
- PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-`
-
-//nolint:tparallel
-func TestSDLEnterpriseUpgradeBinaryDefaultCanonicalForm(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
- srv57, srv80 := entUpgServers(t)
-
- // Both versions dump the same value-faithful canonical form.
- db80 := entLoadDDL(ctx, t, srv80, "entsdl_upbin80", entUpgBinaryDefaultDDL)
- d80 := dumpSDL(ctx, t, srv80, db80)
- db57 := entLoadDDL(ctx, t, srv57, "entsdl_upbin57", entUpgBinaryDefaultDDL)
- d57 := dumpSDL(ctx, t, srv57, db57)
- for _, d := range []struct{ version, dump string }{{"8.0", d80}, {"5.7", d57}} {
- require.Contains(t, d.dump, "`u` binary(16) NOT NULL DEFAULT ''",
- "%s dump must render the empty binary(16) default as '' (not the I_S encoding):\n%s", d.version, d.dump)
- require.Contains(t, d.dump, "`v` varbinary(24) NOT NULL DEFAULT 'ab'",
- "%s dump must render the varbinary default value-faithfully:\n%s", d.version, d.dump)
- require.NotContains(t, d.dump, "0x",
- "%s dump leaked an information_schema hex encoding for a clean-text default:\n%s", d.version, d.dump)
- }
-
- // Cross-version: with the table charset pinned, the dumps describe the same schema
- // and BOTH cross-diffs must be empty — the shape that once looped on
- // MODIFY COLUMN ... DEFAULT '0x…'.
- cross80, err := mysqlDiffSDLMigration(d80, d57, entUpg80Version)
- require.NoError(t, err)
- require.Empty(t, cross80, "5.7 dump phantom-diffs the equivalent 8.0 database:\n%s", cross80)
- cross57, err := mysqlDiffSDLMigration(d57, d80, entUpg57Version)
- require.NoError(t, err)
- require.Empty(t, cross57, "8.0 dump phantom-diffs the equivalent 5.7 database:\n%s", cross57)
-
- // Round-trip the 8.0 dump through a FRESH database: the create plan must apply, the
- // re-dump must converge to the dump (no double-encoding), and self-diff empty.
- dbFresh := newLiveDatabase(ctx, t, srv80, "entsdl_upbinfresh")
- empty := dumpSDL(ctx, t, srv80, dbFresh)
- plan, err := mysqlDiffSDLMigration(empty, d80, entUpg80Version)
- require.NoError(t, err)
- require.NotEmpty(t, plan)
- require.NoError(t, applyDDL(ctx, t, srv80, dbFresh, plan), "create plan from the 8.0 dump failed to apply:\n%s", plan)
- redump := dumpSDL(ctx, t, srv80, dbFresh)
- require.NotContains(t, redump, "0x3078",
- "the 8.0 dump round-trip double-encodes the default again ('0x6162' -> '0x307836313632'):\n%s", redump)
- converge, err := mysqlDiffSDLMigration(redump, d80, entUpg80Version)
- require.NoError(t, err)
- require.Empty(t, converge, "8.0 dump did not converge through a fresh database; residual:\n%s", converge)
-
- // Values no quoted SDL string can carry fall back to an unquoted hex literal, each
- // probed on the version whose information_schema reports the value faithfully (8.0
- // truncates at NUL bytes; 5.7 truncates at non-UTF8 bytes).
- t.Run("hex_literal_fallback_nonutf8_80", func(t *testing.T) {
- ddl := "CREATE TABLE ent_up_binhex (id int NOT NULL, w varbinary(8) NOT NULL DEFAULT 0xFF61, PRIMARY KEY (id)) ENGINE=InnoDB;"
- dbName := entLoadDDL(ctx, t, srv80, "entsdl_upbinhex80", ddl)
- dump := dumpSDL(ctx, t, srv80, dbName)
- require.Contains(t, dump, "`w` varbinary(8) NOT NULL DEFAULT 0xff61",
- "8.0 dump must keep the non-UTF8 default as an unquoted hex literal:\n%s", dump)
- self, err := mysqlDiffSDLMigration(dump, dump, entUpg80Version)
- require.NoError(t, err)
- require.Empty(t, self, "hex-literal dump not idempotent:\n%s", self)
-
- fresh := newLiveDatabase(ctx, t, srv80, "entsdl_upbinhexf80")
- plan, err := mysqlDiffSDLMigration(dumpSDL(ctx, t, srv80, fresh), dump, entUpg80Version)
- require.NoError(t, err)
- require.NoError(t, applyDDL(ctx, t, srv80, fresh, plan), "hex-literal create plan failed to apply:\n%s", plan)
- require.Equal(t, dump, dumpSDL(ctx, t, srv80, fresh), "hex-literal default did not round-trip byte-identically")
- })
- t.Run("hex_literal_fallback_trailnul_57", func(t *testing.T) {
- ddl := "CREATE TABLE ent_up_binnul (id int NOT NULL, w varbinary(8) NOT NULL DEFAULT 0x6100, PRIMARY KEY (id)) ENGINE=InnoDB;"
- dbName := entLoadDDL(ctx, t, srv57, "entsdl_upbinnul57", ddl)
- dump := dumpSDL(ctx, t, srv57, dbName)
- require.Contains(t, dump, "`w` varbinary(8) NOT NULL DEFAULT 0x6100",
- "5.7 dump must keep the trailing-NUL default as an unquoted hex literal (the NUL is significant on varbinary):\n%s", dump)
- self, err := mysqlDiffSDLMigration(dump, dump, entUpg57Version)
- require.NoError(t, err)
- require.Empty(t, self, "hex-literal dump not idempotent:\n%s", self)
-
- fresh := newLiveDatabase(ctx, t, srv57, "entsdl_upbinnulf57")
- plan, err := mysqlDiffSDLMigration(dumpSDL(ctx, t, srv57, fresh), dump, entUpg57Version)
- require.NoError(t, err)
- require.NoError(t, applyDDL(ctx, t, srv57, fresh, plan), "hex-literal create plan failed to apply:\n%s", plan)
- require.Equal(t, dump, dumpSDL(ctx, t, srv57, fresh), "hex-literal default did not round-trip byte-identically")
- })
-}
diff --git a/backend/plugin/schema/mysql/sdl_migration_omni_live_test.go b/backend/plugin/schema/mysql/sdl_migration_omni_live_test.go
deleted file mode 100644
index c0fbd35994..0000000000
--- a/backend/plugin/schema/mysql/sdl_migration_omni_live_test.go
+++ /dev/null
@@ -1,795 +0,0 @@
-package mysql
-
-import (
- "context"
- "fmt"
- "os"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/db"
- mysqldb "github.com/bytebase/bytebase/backend/plugin/db/mysql"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-// skipUnlessLiveOracle gates every live-oracle SDL suite in this package. These
-// suites need the developer's local MySQL oracles (5.7 at 127.0.0.1:13307, 8.0 at
-// 127.0.0.1:13306 — see liveServers) and are opt-in via MYSQL_SDL_LIVE_ORACLE=1.
-// CI runs `go test ./backend/...` without -short and has no such servers, so an
-// explicit environment gate (mirroring the cosmosdb integration tests) keeps the
-// suites out of CI while leaving them one env var away locally.
-func skipUnlessLiveOracle(t *testing.T) {
- t.Helper()
- if testing.Short() {
- t.Skip("skipping live MySQL SDL oracle test in short mode")
- }
- if os.Getenv("MYSQL_SDL_LIVE_ORACLE") == "" {
- t.Skip("skipping live MySQL SDL oracle test: set MYSQL_SDL_LIVE_ORACLE=1 (needs local MySQL 5.7 at :13307 and 8.0 at :13306)")
- }
-}
-
-// liveServer describes a live oracle MySQL instance the SDL smoke test exercises.
-type liveServer struct {
- name string
- host string
- port string
- version string
-}
-
-// liveServers are the two oracle MySQL instances. Idempotence diverges by version
-// (5.7 injects integer display widths and uses the utf8mb4_general_ci default; 8.0
-// drops widths and uses utf8mb4_0900_ai_ci), so both are exercised.
-var liveServers = []liveServer{
- {name: "mysql80", host: "127.0.0.1", port: "13306", version: "8.0"},
- {name: "mysql57", host: "127.0.0.1", port: "13307", version: "5.7"},
-}
-
-const liveOraclePassword = "010424"
-
-// representativeDDL is the user-authored target schema D. It intentionally spans the
-// constructs whose stored form MySQL rewrites: integer display widths, BOOLEAN
-// (stored tinyint(1)), DECIMAL, implicit vs explicit charset/collation, defaults
-// (literal, CURRENT_TIMESTAMP, ON UPDATE), a secondary index, a foreign key, a
-// STORED generated column, and a view.
-const representativeDDL = `
-CREATE TABLE author (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- active BOOLEAN NOT NULL DEFAULT TRUE,
- rating DECIMAL(5, 2) NOT NULL DEFAULT 0.00,
- bio TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- INDEX idx_author_name (name)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE book (
- id INT PRIMARY KEY AUTO_INCREMENT,
- author_id INT NOT NULL,
- title VARCHAR(200) NOT NULL,
- price DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
- discount DECIMAL(4, 2) NOT NULL DEFAULT 0.00,
- final_price DECIMAL(12, 4) AS (price * (1 - discount)) STORED,
- page_count INT NOT NULL DEFAULT 0,
- CONSTRAINT fk_book_author FOREIGN KEY (author_id) REFERENCES author (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE OR REPLACE VIEW active_books AS
-SELECT b.id, b.title, b.final_price
-FROM book b JOIN author a ON b.author_id = a.id
-WHERE a.active = TRUE;
-`
-
-// statementCount counts the non-empty ";"-separated statements in generated DDL.
-// MigrationPlan.SQL joins ops with ";\n", so this equals the number of operations.
-func statementCount(sql string) int {
- n := 0
- for _, part := range strings.Split(sql, ";") {
- if strings.TrimSpace(part) != "" {
- n++
- }
- }
- return n
-}
-
-// createLiveMySQLDriver opens a driver against a live oracle MySQL instance.
-func createLiveMySQLDriver(ctx context.Context, srv liveServer, database string) (db.Driver, error) {
- driver := &mysqldb.Driver{}
- config := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: "root",
- Host: srv.host,
- Port: srv.port,
- Database: database,
- },
- Password: liveOraclePassword,
- ConnectionContext: db.ConnectionContext{
- EngineVersion: srv.version,
- DatabaseName: database,
- },
- }
- return driver.Open(ctx, storepb.Engine_MYSQL, config)
-}
-
-// syncToSDL applies representativeDDL to a fresh database on srv, syncs it back to
-// metadata, and returns the canonical SDL dump (MetadataToSDL). The database is
-// dropped on cleanup.
-func syncToSDL(ctx context.Context, t *testing.T, srv liveServer) string {
- t.Helper()
-
- dbName := fmt.Sprintf("sdl_smoke_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
-
- admin, err := createLiveMySQLDriver(ctx, srv, "")
- require.NoError(t, err)
- _, err = admin.Execute(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName), db.ExecuteOptions{})
- require.NoError(t, err)
- admin.Close(ctx)
-
- t.Cleanup(func() {
- cleanup, err := createLiveMySQLDriver(ctx, srv, "")
- if err != nil {
- return
- }
- defer cleanup.Close(ctx)
- _, _ = cleanup.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
- })
-
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- _, err = driver.Execute(ctx, representativeDDL, db.ExecuteOptions{})
- require.NoError(t, err)
-
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
- require.NoError(t, err)
- require.NotEmpty(t, source, "MetadataToSDL produced empty SDL")
-
- return source
-}
-
-// syncToMetadata applies representativeDDL to a fresh database on srv, syncs it back, and
-// returns the model.DatabaseMetadata — the exact value the production release path feeds to
-// schema.SDLMigration — together with the database name so the caller can apply DDL back.
-// The database is dropped on cleanup.
-func syncToMetadata(ctx context.Context, t *testing.T, srv liveServer) (*model.DatabaseMetadata, string) {
- t.Helper()
-
- dbName := newLiveDatabase(ctx, t, srv, "sdl_prod")
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- _, err = driver.Execute(ctx, representativeDDL, db.ExecuteOptions{})
- require.NoError(t, err)
-
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true), dbName
-}
-
-// TestSDLProductionPathLive exercises the REAL production entry point, schema.SDLMigration
-// (the function database_migrate_executor.go's diff() calls), end to end against live 5.7
-// and 8.0. It proves the version threading wired through schema.SDLMigration ->
-// mysqlDiffSDLMigration -> the MySQL version-aware registry reaches the omni
-// normalizer:
-//
-// (1) the 5.7 no-op (synced metadata diffed against its own authoring DDL) is empty —
-// a non-empty result is the version-dispatch regression (utf8mb4 default-collation
-// phantom) this wiring fixes; and
-// (2) a 5.7 schema change produces DDL valid on 5.7 — never names utf8mb4_0900_ai_ci
-// (errno 1273) — and applies cleanly to the live 5.7 server.
-//
-//nolint:tparallel
-func TestSDLProductionPathLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- addColumnDDL := strings.Replace(
- representativeDDL,
- "\tpage_count INT NOT NULL DEFAULT 0,",
- "\tpage_count INT NOT NULL DEFAULT 0,\n\tisbn VARCHAR(20) NULL,",
- 1,
- )
- require.NotEqual(t, representativeDDL, addColumnDDL, "test setup: addColumnDDL must differ from D")
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- meta, dbName := syncToMetadata(ctx, t, srv)
-
- // (1) Production-path no-op idempotence: schema.SDLMigration converts meta to
- // SDL internally and diffs against the user DDL, threading srv.version.
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, representativeDDL, meta, srv.version)
- require.NoError(t, err)
- require.Empty(t, noop, "[%s] production-path no-op must be empty, got:\n%s", srv.name, noop)
-
- // (2) Production-path change: minimal, version-valid DDL.
- change, err := schema.SDLMigration(storepb.Engine_MYSQL, addColumnDDL, meta, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, change, "[%s] production-path change must be non-empty", srv.name)
- upper := strings.ToUpper(change)
- require.Contains(t, upper, "ADD COLUMN", "[%s] expected ADD COLUMN, got:\n%s", srv.name, change)
- require.Contains(t, upper, "ISBN", "[%s] expected the new column, got:\n%s", srv.name, change)
- require.NotContains(t, upper, "CREATE TABLE", "[%s] change must not rebuild the table:\n%s", srv.name, change)
- if srv.version == "5.7" {
- require.NotContains(t, change, "utf8mb4_0900_ai_ci",
- "[%s] production-path 5.7 change names an 8.0-only collation (errno 1273):\n%s", srv.name, change)
- }
-
- // Apply the production-path DDL back to the real server — errno 1273 would
- // surface here on 5.7 if the version were not threaded through schema.SDLMigration.
- applyDriver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer applyDriver.Close(ctx)
- _, applyErr := applyDriver.Execute(ctx, change, db.ExecuteOptions{})
- require.NoError(t, applyErr, "[%s] production-path DDL failed to apply:\n%s", srv.name, change)
- })
- }
-}
-
-// TestSDLDeclarativePathLive exercises the full wired declarative path against live
-// MySQL 5.7 and 8.0: sync -> MetadataToSDL -> DiffSDLMigration. It asserts the
-// no-op idempotence property, minimal-DDL change generation, and apply-back.
-//
-//nolint:tparallel
-func TestSDLDeclarativePathLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- // ---- Assertion 1: no-op idempotence ----
- source := syncToSDL(ctx, t, srv)
-
- t.Logf("[%s] canonical SDL source:\n%s", srv.name, source)
-
- // source vs source must ALWAYS be empty on both versions (identical inputs;
- // version-independent). A non-empty result here would be a pure wiring/dump
- // determinism bug.
- selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
- require.NoError(t, err)
- require.Empty(t, selfDiff, "[%s] source-vs-source diff must be empty, got:\n%s", srv.name, selfDiff)
-
- // source vs D is the meaningful case: the stored/dumped canonical form
- // diffed against the user's original DDL must canonicalize equal. The version
- // is threaded so a 5.7 schema is canonicalized as 5.7 (a bare CHARSET=utf8mb4
- // resolves to utf8mb4_general_ci, not the 8.0 default) — the fix for the
- // version-dispatch gap (omni catalog SessionState.Version + LoadSDLWithVersion).
- noopDiff, err := mysqlDiffSDLMigration(source, representativeDDL, srv.version)
- require.NoError(t, err)
- require.Empty(t, noopDiff, "[%s] no-op idempotence FAILED: source-vs-D diff must be empty, got:\n%s", srv.name, noopDiff)
- })
- }
-}
-
-// TestSDLMinimalChangeLive asserts that a real change to D produces minimal DDL and
-// that applying it back converges (re-diff empty). Split out so a no-op failure
-// (TestSDLDeclarativePathLive) is reported independently of a change-generation
-// failure.
-//
-//nolint:tparallel
-func TestSDLMinimalChangeLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- // D2 = D with one added column on `book`.
- addColumnDDL := strings.Replace(
- representativeDDL,
- "\tpage_count INT NOT NULL DEFAULT 0,",
- "\tpage_count INT NOT NULL DEFAULT 0,\n\tisbn VARCHAR(20) NULL,",
- 1,
- )
- require.NotEqual(t, representativeDDL, addColumnDDL, "test setup: addColumnDDL must differ from D")
-
- // D2' = D with a changed column type (page_count INT -> BIGINT).
- changeTypeDDL := strings.Replace(
- representativeDDL,
- "\tpage_count INT NOT NULL DEFAULT 0,",
- "\tpage_count BIGINT NOT NULL DEFAULT 0,",
- 1,
- )
- require.NotEqual(t, representativeDDL, changeTypeDDL, "test setup: changeTypeDDL must differ from D")
-
- // D2'' = D with an added secondary index on book(title).
- addIndexDDL := strings.Replace(
- representativeDDL,
- "\tpage_count INT NOT NULL DEFAULT 0,",
- "\tpage_count INT NOT NULL DEFAULT 0,\n\tINDEX idx_book_title (title),",
- 1,
- )
- require.NotEqual(t, representativeDDL, addIndexDDL, "test setup: addIndexDDL must differ from D")
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- t.Run("add_column", func(t *testing.T) {
- source := syncToSDL(ctx, t, srv)
-
- diff, err := mysqlDiffSDLMigration(source, addColumnDDL, srv.version)
- require.NoError(t, err)
- t.Logf("[%s] add-column diff:\n%s", srv.name, diff)
- require.NotEmpty(t, diff, "[%s] add-column must produce DDL", srv.name)
- upper := strings.ToUpper(diff)
- require.Contains(t, upper, "ADD COLUMN", "[%s] expected ADD COLUMN, got:\n%s", srv.name, diff)
- require.Contains(t, upper, "ISBN", "[%s] expected the new column, got:\n%s", srv.name, diff)
- require.NotContains(t, upper, "CREATE TABLE", "[%s] add-column must not rebuild the table:\n%s", srv.name, diff)
- require.NotContains(t, upper, "DROP TABLE", "[%s] add-column must not drop the table:\n%s", srv.name, diff)
- // Strict minimality: exactly the ADD COLUMN, nothing else — on BOTH versions,
- // now that the version is threaded and the 5.7 utf8mb4-default-collation
- // phantom no longer contaminates the diff.
- require.Equal(t, 1, statementCount(diff),
- "[%s] add-column must be a single minimal statement, got:\n%s", srv.name, diff)
- })
-
- t.Run("change_type", func(t *testing.T) {
- source := syncToSDL(ctx, t, srv)
-
- diff, err := mysqlDiffSDLMigration(source, changeTypeDDL, srv.version)
- require.NoError(t, err)
- t.Logf("[%s] change-type diff:\n%s", srv.name, diff)
- require.NotEmpty(t, diff, "[%s] change-type must produce DDL", srv.name)
- upper := strings.ToUpper(diff)
- require.True(t,
- strings.Contains(upper, "MODIFY") || strings.Contains(upper, "CHANGE"),
- "[%s] expected MODIFY/CHANGE COLUMN, got:\n%s", srv.name, diff)
- require.Contains(t, upper, "BIGINT", "[%s] expected the new type, got:\n%s", srv.name, diff)
- require.NotContains(t, upper, "CREATE TABLE", "[%s] change-type must not rebuild the table:\n%s", srv.name, diff)
- require.Equal(t, 1, statementCount(diff),
- "[%s] change-type must be a single minimal statement, got:\n%s", srv.name, diff)
- })
-
- // add_index now exercises REAL index-diff behavior: the merged omni
- // breadth engine populates SchemaDiff.Indexes and generates a minimal
- // ADD KEY. MySQL renders a secondary index as `ADD KEY` (the canonical
- // synonym of ADD INDEX), so the assertion accepts either spelling.
- t.Run("add_index", func(t *testing.T) {
- source := syncToSDL(ctx, t, srv)
-
- diff, err := mysqlDiffSDLMigration(source, addIndexDDL, srv.version)
- require.NoError(t, err)
- t.Logf("[%s] add-index diff:\n%s", srv.name, diff)
- require.NotEmpty(t, diff, "[%s] add-index must produce DDL", srv.name)
- upper := strings.ToUpper(diff)
- require.True(t,
- strings.Contains(upper, "ADD KEY") || strings.Contains(upper, "ADD INDEX"),
- "[%s] expected ADD KEY/INDEX, got:\n%s", srv.name, diff)
- require.Contains(t, upper, "IDX_BOOK_TITLE", "[%s] expected the new index, got:\n%s", srv.name, diff)
- require.NotContains(t, upper, "CREATE TABLE", "[%s] add-index must not rebuild the table:\n%s", srv.name, diff)
- require.NotContains(t, upper, "DROP TABLE", "[%s] add-index must not drop the table:\n%s", srv.name, diff)
- require.Equal(t, 1, statementCount(diff),
- "[%s] add-index must be a single minimal statement, got:\n%s", srv.name, diff)
- })
- })
- }
-}
-
-// TestSDLApplyBackLive exercises assertion 3: take D, sync to source, compute the
-// minimal change DDL against D2, apply that DDL to the real DB, re-sync, and confirm
-// the schema now matches D2 (re-running the diff yields empty).
-//
-//nolint:tparallel
-func TestSDLApplyBackLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- addColumnDDL := strings.Replace(
- representativeDDL,
- "\tpage_count INT NOT NULL DEFAULT 0,",
- "\tpage_count INT NOT NULL DEFAULT 0,\n\tisbn VARCHAR(20) NULL,",
- 1,
- )
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- // Build the database, capture its name, sync to source.
- dbName := fmt.Sprintf("sdl_apply_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
-
- admin, err := createLiveMySQLDriver(ctx, srv, "")
- require.NoError(t, err)
- _, err = admin.Execute(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName), db.ExecuteOptions{})
- require.NoError(t, err)
- admin.Close(ctx)
- t.Cleanup(func() {
- cleanup, err := createLiveMySQLDriver(ctx, srv, "")
- if err != nil {
- return
- }
- defer cleanup.Close(ctx)
- _, _ = cleanup.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
- })
-
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- _, err = driver.Execute(ctx, representativeDDL, db.ExecuteOptions{})
- require.NoError(t, err)
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
- require.NoError(t, err)
-
- // Compute minimal change DDL for D2 (add column), version-aware so the change
- // DDL is valid on the target server (no utf8mb4_0900_ai_ci on 5.7).
- changeDDL, err := mysqlDiffSDLMigration(source, addColumnDDL, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, changeDDL, "[%s] expected non-empty change DDL", srv.name)
- t.Logf("[%s] apply-back change DDL:\n%s", srv.name, changeDDL)
-
- // The change DDL must never name a collation the target server lacks.
- if srv.version == "5.7" {
- require.NotContains(t, changeDDL, "utf8mb4_0900_ai_ci",
- "[%s] change DDL names a collation that does not exist on 5.7:\n%s", srv.name, changeDDL)
- }
-
- // Apply it back to the real DB — this is where Error 1273 would surface on 5.7
- // if the version were not threaded.
- applyDriver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer applyDriver.Close(ctx)
- _, applyErr := applyDriver.Execute(ctx, changeDDL, db.ExecuteOptions{})
- require.NoError(t, applyErr, "[%s] apply-back DDL failed to apply", srv.name)
-
- newMetadata, err := applyDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
- newSource, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_MYSQL, true))
- require.NoError(t, err)
-
- // The schema must now match D2: re-diff yields empty.
- converge, err := mysqlDiffSDLMigration(newSource, addColumnDDL, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "[%s] apply-back did not converge to D2, residual diff:\n%s", srv.name, converge)
- })
- }
-}
-
-// breadthCase exercises one MySQL breadth object type end to end through the wired
-// declarative path. baseDDL builds the starting schema; targetDDL is the desired SDL.
-// noop holds true when baseDDL already equals the target's object (idempotence is then
-// asserted against targetDDL directly). When noop is false, baseDDL omits/differs the
-// object so the diff produces a real change whose generated DDL must contain changeWant
-// (case-insensitive) and, when applied back, must converge.
-type breadthCase struct {
- name string
- baseDDL string
- targetDDL string
- changeWant string // uppercase substring the minimal change DDL must contain
- skip57 bool // CHECK constraints are 8.0-only
-}
-
-// newLiveDatabase creates a fresh database on srv and registers cleanup. It returns the
-// database name so the caller can apply DDL and sync it repeatedly within one test.
-func newLiveDatabase(ctx context.Context, t *testing.T, srv liveServer, prefix string) string {
- t.Helper()
- dbName := fmt.Sprintf("%s_%s", prefix, strings.ReplaceAll(uuid.New().String(), "-", "_"))
- admin, err := createLiveMySQLDriver(ctx, srv, "")
- require.NoError(t, err)
- _, err = admin.Execute(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName), db.ExecuteOptions{})
- require.NoError(t, err)
- admin.Close(ctx)
- t.Cleanup(func() {
- c, err := createLiveMySQLDriver(ctx, srv, "")
- if err != nil {
- return
- }
- defer c.Close(ctx)
- _, _ = c.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
- })
- return dbName
-}
-
-// applyAndDump applies ddl to dbName on srv, syncs the schema, and returns the canonical
-// SDL dump (MetadataToSDL of the synced metadata) — i.e. the declarative-path source.
-func applyAndDump(ctx context.Context, t *testing.T, srv liveServer, dbName, ddl string) string {
- t.Helper()
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, ddl, db.ExecuteOptions{})
- require.NoError(t, err, "apply DDL")
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
- require.NoError(t, err)
- return source
-}
-
-// breadthCases covers the eight MySQL breadth object types the merged omni engine
-// supports: secondary/unique index, foreign key, CHECK (8.0), partition, view, stored
-// routine (function + procedure), trigger, and event. Each carries a base→target delta
-// that names exactly one object so the generated change is minimal.
-func breadthCases() []breadthCase {
- return []breadthCase{
- {
- name: "secondary_index",
- baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL,
- INDEX idx_name (name)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- changeWant: "ADD KEY",
- },
- {
- name: "unique_index",
- baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, email VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, email VARCHAR(100) NOT NULL,
- UNIQUE KEY uk_email (email)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- changeWant: "UNIQUE",
- },
- {
- name: "foreign_key",
- baseDDL: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE child (id INT PRIMARY KEY, pid INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- targetDDL: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE child (id INT PRIMARY KEY, pid INT NOT NULL,
- CONSTRAINT fk_child_parent FOREIGN KEY (pid) REFERENCES parent (id) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- changeWant: "FOREIGN KEY",
- },
- {
- name: "check_constraint",
- baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, age INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, age INT NOT NULL,
- CONSTRAINT chk_age CHECK (age >= 0)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- changeWant: "CHECK",
- skip57: true,
- },
- {
- name: "partition",
- baseDDL: `CREATE TABLE sales (id INT NOT NULL AUTO_INCREMENT, sale_date DATE NOT NULL, PRIMARY KEY (id, sale_date)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- targetDDL: `CREATE TABLE sales (id INT NOT NULL AUTO_INCREMENT, sale_date DATE NOT NULL, PRIMARY KEY (id, sale_date)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY RANGE (YEAR(sale_date)) (PARTITION p2023 VALUES LESS THAN (2024), PARTITION p_future VALUES LESS THAN MAXVALUE);`,
- changeWant: "PARTITION BY",
- },
- {
- name: "view",
- baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, active BOOLEAN NOT NULL DEFAULT TRUE, title VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE VIEW v AS SELECT id FROM t WHERE active = TRUE;`,
- targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, active BOOLEAN NOT NULL DEFAULT TRUE, title VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE VIEW v AS SELECT id, title FROM t WHERE active = TRUE;`,
- changeWant: "VIEW",
- },
- {
- name: "function",
- baseDDL: `CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE FUNCTION add_n(a INT) RETURNS INT DETERMINISTIC RETURN a + 1;`,
- targetDDL: `CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE FUNCTION add_n(a INT) RETURNS INT DETERMINISTIC RETURN a + 2;`,
- changeWant: "FUNCTION",
- },
- {
- name: "procedure",
- baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, cnt INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, cnt INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE PROCEDURE bump(IN tid INT) BEGIN UPDATE t SET cnt = cnt + 1 WHERE id = tid; END;`,
- changeWant: "PROCEDURE",
- },
- {
- name: "trigger",
- baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val); END;`,
- targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val + 1); END;`,
- changeWant: "TRIGGER",
- },
- {
- name: "event",
- baseDDL: `CREATE TABLE log (id INT PRIMARY KEY AUTO_INCREMENT, d DATE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- targetDDL: `CREATE TABLE log (id INT PRIMARY KEY AUTO_INCREMENT, d DATE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE EVENT IF NOT EXISTS daily_ev ON SCHEDULE EVERY 1 DAY DO INSERT INTO log (d) VALUES (CURDATE());`,
- changeWant: "EVENT",
- },
- }
-}
-
-// TestSDLBreadthLive is the breadth deliverable: every MySQL breadth object type is
-// round-tripped end to end through the wired declarative path (sync → MetadataToSDL →
-// mysqlDiffSDLMigration) on live MySQL 5.7 and 8.0, asserting three properties per
-// type:
-//
-// (1) no-op idempotence — the target schema synced, dumped, and diffed against its own
-// user DDL produces an empty migration (and source-vs-source is empty);
-// (2) minimal change — building the base schema and diffing the target yields exactly
-// the minimal ALTER/CREATE/DROP for that object (no table rebuild);
-// (3) apply-back convergence — applying that generated DDL to the real database and
-// re-diffing yields empty.
-//
-//nolint:tparallel
-func TestSDLBreadthLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, bc := range breadthCases() {
- bc := bc
- if srv.version == "5.7" && bc.skip57 {
- continue
- }
- t.Run(bc.name, func(t *testing.T) {
- // (1) No-op idempotence: build the TARGET, dump, diff against the
- // target user DDL — must be empty on both versions.
- noopDB := newLiveDatabase(ctx, t, srv, "sdl_breadth_noop")
- targetSource := applyAndDump(ctx, t, srv, noopDB, bc.targetDDL)
- t.Logf("[%s/%s] target dump:\n%s", srv.name, bc.name, targetSource)
-
- selfDiff, err := mysqlDiffSDLMigration(targetSource, targetSource, srv.version)
- require.NoError(t, err)
- require.Empty(t, selfDiff, "[%s/%s] source-vs-source must be empty:\n%s", srv.name, bc.name, selfDiff)
-
- noop, err := mysqlDiffSDLMigration(targetSource, bc.targetDDL, srv.version)
- require.NoError(t, err)
- require.Empty(t, noop, "[%s/%s] no-op idempotence FAILED, residual:\n%s", srv.name, bc.name, noop)
-
- // (2) Minimal change: build the BASE, diff the target.
- changeDB := newLiveDatabase(ctx, t, srv, "sdl_breadth_chg")
- baseSource := applyAndDump(ctx, t, srv, changeDB, bc.baseDDL)
-
- change, err := mysqlDiffSDLMigration(baseSource, bc.targetDDL, srv.version)
- require.NoError(t, err)
- t.Logf("[%s/%s] change DDL:\n%s", srv.name, bc.name, change)
- require.NotEmpty(t, change, "[%s/%s] change must be non-empty", srv.name, bc.name)
- require.Contains(t, strings.ToUpper(change), bc.changeWant,
- "[%s/%s] change missing %q:\n%s", srv.name, bc.name, bc.changeWant, change)
- require.NotContains(t, strings.ToUpper(change), "DROP TABLE",
- "[%s/%s] change must not drop the table:\n%s", srv.name, bc.name, change)
- require.NotContains(t, strings.ToUpper(change), "CREATE TABLE",
- "[%s/%s] change must not rebuild the table:\n%s", srv.name, bc.name, change)
-
- // (3) Apply-back convergence: apply the generated DDL to the real DB,
- // re-sync, re-diff must be empty. The omni plan is op-by-op; the joined
- // blob applies cleanly because the MySQL driver's SplitSQL is BEGIN…END
- // aware, so trigger/routine bodies with internal ";" stay intact.
- applyDriver, err := createLiveMySQLDriver(ctx, srv, changeDB)
- require.NoError(t, err)
- defer applyDriver.Close(ctx)
- _, applyErr := applyDriver.Execute(ctx, change, db.ExecuteOptions{})
- require.NoError(t, applyErr, "[%s/%s] apply-back failed to apply:\n%s", srv.name, bc.name, change)
-
- newMetadata, err := applyDriver.SyncDBSchema(ctx)
- require.NoError(t, err)
- newSource, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_MYSQL, true))
- require.NoError(t, err)
-
- converge, err := mysqlDiffSDLMigration(newSource, bc.targetDDL, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "[%s/%s] apply-back did not converge, residual:\n%s", srv.name, bc.name, converge)
- })
- }
- })
- }
-}
-
-// countAdviceCode returns how many advices carry the given code.
-func countAdviceCode(advices []*storepb.Advice, c int32) int {
- n := 0
- for _, a := range advices {
- if a.Code == c {
- n++
- }
- }
- return n
-}
-
-// TestSDLDropAdvicesLive exercises schema.SDLDropAdvices (the registered MySQL drop-advice
-// analyzer, reached the same way the release-check gating reaches it) end to end against
-// live 5.7 and 8.0: sync the representative schema, then point the user SDL at a target that
-// drops a whole table and a column, and assert WARNING advices are produced with the
-// SDLDropOperation code. A no-op target must yield zero advices.
-//
-//nolint:tparallel
-func TestSDLDropAdvicesLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- // Target SDL drops the `book` table entirely and drops author.bio (a column).
- dropTargetDDL := `
-CREATE TABLE author (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- active BOOLEAN NOT NULL DEFAULT TRUE,
- rating DECIMAL(5, 2) NOT NULL DEFAULT 0.00,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- INDEX idx_author_name (name)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-`
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- meta, _ := syncToMetadata(ctx, t, srv)
-
- // No-op target: representativeDDL itself must produce zero drop advices.
- noopAdvices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, representativeDDL, meta, srv.version)
- require.NoError(t, err)
- require.Empty(t, noopAdvices, "[%s] no-op target must yield no drop advices, got: %+v", srv.name, noopAdvices)
-
- // Destructive target: book table dropped (active_books view depends on it, so the
- // view is dropped too) and author.bio column dropped.
- advices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, dropTargetDDL, meta, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, advices, "[%s] destructive target must yield drop advices", srv.name)
-
- for _, a := range advices {
- require.Equal(t, storepb.Advice_WARNING, a.Status, "[%s] drop advice must be WARNING: %+v", srv.name, a)
- }
-
- dropCount := countAdviceCode(advices, code.SDLDropOperation.Int32())
- require.GreaterOrEqual(t, dropCount, 2,
- "[%s] expected at least DROP TABLE book + DROP COLUMN bio warnings, got: %+v", srv.name, advices)
-
- // The dropped table and column must be named somewhere in the advice content.
- joined := ""
- for _, a := range advices {
- joined += a.Content + "\n"
- }
- require.Contains(t, joined, "book", "[%s] expected the dropped table named, got:\n%s", srv.name, joined)
- require.Contains(t, joined, "bio", "[%s] expected the dropped column named, got:\n%s", srv.name, joined)
- })
- }
-}
-
-// TestSDLSpatialSRIDPresenceLive verifies the SRID presence semantics (X5) against the
-// live 8.0 oracle: a geometry column with EXPLICIT SRID 0 (information_schema reports
-// SRS_ID=0 — a valid SRS, distinct from "no SRID" which reports NULL) and one with SRID
-// 4326 must both round-trip — dump -> SDL -> apply to a fresh database -> re-dump —
-// with byte-identical dumps and an empty self-diff. Before the fix the 0-sentinel
-// conflated SRID 0 with "no SRID" and dropped the attribute at dump time.
-func TestSDLSpatialSRIDPresenceLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
- srv := liveServers[0] // 8.0 — the SRID column attribute is an 8.0-only surface.
-
- const ddl = `
-CREATE TABLE geo (
- id INT PRIMARY KEY,
- pt0 GEOMETRY NOT NULL /*!80003 SRID 0 */,
- pt4326 POINT NOT NULL /*!80003 SRID 4326 */,
- ptnone GEOMETRY NOT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-`
- dbA := newLiveDatabase(ctx, t, srv, "sdl_srid_a")
- require.NoError(t, applyDDL(ctx, t, srv, dbA, ddl))
- dumpA := dumpSDL(ctx, t, srv, dbA)
-
- // The dump must carry the explicit SRID 0 (presence, not a zero sentinel), the
- // SRID 4326, and no SRID attribute on the undeclared column.
- require.Contains(t, dumpA, "`pt0` geometry NOT NULL /*!80003 SRID 0 */")
- require.Contains(t, dumpA, "`pt4326` point NOT NULL /*!80003 SRID 4326 */")
- require.Contains(t, dumpA, "`ptnone` geometry NOT NULL,")
- require.NotContains(t, dumpA, "`ptnone` geometry NOT NULL /*!80003")
-
- // Apply the dump to a fresh database and re-dump: the round-trip must be lossless.
- dbB := newLiveDatabase(ctx, t, srv, "sdl_srid_b")
- require.NoError(t, applyDDL(ctx, t, srv, dbB, dumpA))
- dumpB := dumpSDL(ctx, t, srv, dbB)
- require.Equal(t, dumpA, dumpB, "dump -> apply -> re-dump must be byte-stable")
-
- selfDiff, err := mysqlDiffSDLMigration(dumpB, dumpA, srv.version)
- require.NoError(t, err)
- require.Empty(t, selfDiff, "SRID round-trip self-diff must be empty")
-
- // Removing a real SRID restriction must diff (4326 -> unset is a column change).
- // Note the 0 <-> unset transition is folded to a no-op by omni's canonicalizer
- // (out of scope here); the dump above still preserves the explicit SRID 0
- // faithfully, so the declared schema round-trips byte-exact.
- noSridTarget := strings.Replace(dumpA, "`pt4326` point NOT NULL /*!80003 SRID 4326 */", "`pt4326` point NOT NULL", 1)
- change, err := mysqlDiffSDLMigration(dumpA, noSridTarget, srv.version)
- require.NoError(t, err)
- require.Contains(t, change, "MODIFY", "dropping an explicit SRID 4326 must produce a column modification")
-}
diff --git a/backend/plugin/schema/mysql/sdl_migration_omni_test.go b/backend/plugin/schema/mysql/sdl_migration_omni_test.go
index 915833a618..5c1dbd4ec4 100644
--- a/backend/plugin/schema/mysql/sdl_migration_omni_test.go
+++ b/backend/plugin/schema/mysql/sdl_migration_omni_test.go
@@ -1,16 +1,30 @@
package mysql
import (
+ "context"
+ "database/sql"
+ _ "embed"
+ "fmt"
+ "math/rand"
+ "os"
+ "regexp"
+ "slices"
"strings"
+ "sync"
"testing"
+ "time"
+ "github.com/bytebase/omni/mysql/catalog"
+ mysqldriver "github.com/go-sql-driver/mysql"
+ "github.com/google/uuid"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
- "github.com/bytebase/omni/mysql/catalog"
-
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
+ "github.com/bytebase/bytebase/backend/plugin/db"
+ mysqldb "github.com/bytebase/bytebase/backend/plugin/db/mysql"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
"github.com/bytebase/bytebase/backend/plugin/schema"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -370,3 +384,8797 @@ func TestLoadCatalogFallbackSeedsExplicitDefaultsForTimestamp(t *testing.T) {
require.NoError(t, err)
require.Empty(t, sql80, "8.0 source/target with identical bare TIMESTAMP must not phantom-diff; got %q", sql80)
}
+
+// skipUnlessLiveOracle gates every live-oracle SDL suite in this package. These
+// suites need the developer's local MySQL oracles (5.7 at 127.0.0.1:13307, 8.0 at
+// 127.0.0.1:13306 — see liveServers) and are opt-in via MYSQL_SDL_LIVE_ORACLE=1.
+// CI runs `go test ./backend/...` without -short and has no such servers, so an
+// explicit environment gate (mirroring the cosmosdb integration tests) keeps the
+// suites out of CI while leaving them one env var away locally.
+func skipUnlessLiveOracle(t *testing.T) {
+ t.Helper()
+ if testing.Short() {
+ t.Skip("skipping live MySQL SDL oracle test in short mode")
+ }
+ if os.Getenv("MYSQL_SDL_LIVE_ORACLE") == "" {
+ t.Skip("skipping live MySQL SDL oracle test: set MYSQL_SDL_LIVE_ORACLE=1 (needs local MySQL 5.7 at :13307 and 8.0 at :13306)")
+ }
+}
+
+// liveServer describes a live oracle MySQL instance the SDL smoke test exercises.
+type liveServer struct {
+ name string
+ host string
+ port string
+ version string
+}
+
+// liveServers are the two oracle MySQL instances. Idempotence diverges by version
+// (5.7 injects integer display widths and uses the utf8mb4_general_ci default; 8.0
+// drops widths and uses utf8mb4_0900_ai_ci), so both are exercised.
+var liveServers = []liveServer{
+ {name: "mysql80", host: "127.0.0.1", port: "13306", version: "8.0"},
+ {name: "mysql57", host: "127.0.0.1", port: "13307", version: "5.7"},
+}
+
+const liveOraclePassword = "010424"
+
+// representativeDDL is the user-authored target schema D. It intentionally spans the
+// constructs whose stored form MySQL rewrites: integer display widths, BOOLEAN
+// (stored tinyint(1)), DECIMAL, implicit vs explicit charset/collation, defaults
+// (literal, CURRENT_TIMESTAMP, ON UPDATE), a secondary index, a foreign key, a
+// STORED generated column, and a view.
+//
+//go:embed testdata/sdl/representative_ddl.sql
+var representativeDDL string
+
+// statementCount counts the non-empty ";"-separated statements in generated DDL.
+// MigrationPlan.SQL joins ops with ";\n", so this equals the number of operations.
+func statementCount(sql string) int {
+ n := 0
+ for _, part := range strings.Split(sql, ";") {
+ if strings.TrimSpace(part) != "" {
+ n++
+ }
+ }
+ return n
+}
+
+// createLiveMySQLDriver opens a driver against a live oracle MySQL instance.
+func createLiveMySQLDriver(ctx context.Context, srv liveServer, database string) (db.Driver, error) {
+ driver := &mysqldb.Driver{}
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: "root",
+ Host: srv.host,
+ Port: srv.port,
+ Database: database,
+ },
+ Password: liveOraclePassword,
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: srv.version,
+ DatabaseName: database,
+ },
+ }
+ return driver.Open(ctx, storepb.Engine_MYSQL, config)
+}
+
+// syncToSDL applies representativeDDL to a fresh database on srv, syncs it back to
+// metadata, and returns the canonical SDL dump (MetadataToSDL). The database is
+// dropped on cleanup.
+func syncToSDL(ctx context.Context, t *testing.T, srv liveServer) string {
+ t.Helper()
+
+ dbName := fmt.Sprintf("sdl_smoke_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
+ admin, err := createLiveMySQLDriver(ctx, srv, "")
+ require.NoError(t, err)
+ _, err = admin.Execute(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName), db.ExecuteOptions{})
+ require.NoError(t, err)
+ admin.Close(ctx)
+
+ t.Cleanup(func() {
+ cleanup, err := createLiveMySQLDriver(ctx, srv, "")
+ if err != nil {
+ return
+ }
+ defer cleanup.Close(ctx)
+ _, _ = cleanup.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
+ })
+
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ _, err = driver.Execute(ctx, representativeDDL, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
+ require.NoError(t, err)
+ require.NotEmpty(t, source, "MetadataToSDL produced empty SDL")
+
+ return source
+}
+
+// syncToMetadata applies representativeDDL to a fresh database on srv, syncs it back, and
+// returns the model.DatabaseMetadata — the exact value the production release path feeds to
+// schema.SDLMigration — together with the database name so the caller can apply DDL back.
+// The database is dropped on cleanup.
+func syncToMetadata(ctx context.Context, t *testing.T, srv liveServer) (*model.DatabaseMetadata, string) {
+ t.Helper()
+
+ dbName := newLiveDatabase(ctx, t, srv, "sdl_prod")
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ _, err = driver.Execute(ctx, representativeDDL, db.ExecuteOptions{})
+ require.NoError(t, err)
+
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true), dbName
+}
+
+// TestSDLProductionPathLive exercises the REAL production entry point, schema.SDLMigration
+// (the function database_migrate_executor.go's diff() calls), end to end against live 5.7
+// and 8.0. It proves the version threading wired through schema.SDLMigration ->
+// mysqlDiffSDLMigration -> the MySQL version-aware registry reaches the omni
+// normalizer:
+//
+// (1) the 5.7 no-op (synced metadata diffed against its own authoring DDL) is empty —
+// a non-empty result is the version-dispatch regression (utf8mb4 default-collation
+// phantom) this wiring fixes; and
+// (2) a 5.7 schema change produces DDL valid on 5.7 — never names utf8mb4_0900_ai_ci
+// (errno 1273) — and applies cleanly to the live 5.7 server.
+//
+//nolint:tparallel
+func TestSDLProductionPathLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ addColumnDDL := strings.Replace(
+ representativeDDL,
+ "\tpage_count INT NOT NULL DEFAULT 0,",
+ "\tpage_count INT NOT NULL DEFAULT 0,\n\tisbn VARCHAR(20) NULL,",
+ 1,
+ )
+ require.NotEqual(t, representativeDDL, addColumnDDL, "test setup: addColumnDDL must differ from D")
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ meta, dbName := syncToMetadata(ctx, t, srv)
+
+ // (1) Production-path no-op idempotence: schema.SDLMigration converts meta to
+ // SDL internally and diffs against the user DDL, threading srv.version.
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, representativeDDL, meta, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noop, "[%s] production-path no-op must be empty, got:\n%s", srv.name, noop)
+
+ // (2) Production-path change: minimal, version-valid DDL.
+ change, err := schema.SDLMigration(storepb.Engine_MYSQL, addColumnDDL, meta, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, change, "[%s] production-path change must be non-empty", srv.name)
+ upper := strings.ToUpper(change)
+ require.Contains(t, upper, "ADD COLUMN", "[%s] expected ADD COLUMN, got:\n%s", srv.name, change)
+ require.Contains(t, upper, "ISBN", "[%s] expected the new column, got:\n%s", srv.name, change)
+ require.NotContains(t, upper, "CREATE TABLE", "[%s] change must not rebuild the table:\n%s", srv.name, change)
+ if srv.version == "5.7" {
+ require.NotContains(t, change, "utf8mb4_0900_ai_ci",
+ "[%s] production-path 5.7 change names an 8.0-only collation (errno 1273):\n%s", srv.name, change)
+ }
+
+ // Apply the production-path DDL back to the real server — errno 1273 would
+ // surface here on 5.7 if the version were not threaded through schema.SDLMigration.
+ applyDriver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer applyDriver.Close(ctx)
+ _, applyErr := applyDriver.Execute(ctx, change, db.ExecuteOptions{})
+ require.NoError(t, applyErr, "[%s] production-path DDL failed to apply:\n%s", srv.name, change)
+ })
+ }
+}
+
+// TestSDLDeclarativePathLive exercises the full wired declarative path against live
+// MySQL 5.7 and 8.0: sync -> MetadataToSDL -> DiffSDLMigration. It asserts the
+// no-op idempotence property, minimal-DDL change generation, and apply-back.
+//
+//nolint:tparallel
+func TestSDLDeclarativePathLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ // ---- Assertion 1: no-op idempotence ----
+ source := syncToSDL(ctx, t, srv)
+
+ t.Logf("[%s] canonical SDL source:\n%s", srv.name, source)
+
+ // source vs source must ALWAYS be empty on both versions (identical inputs;
+ // version-independent). A non-empty result here would be a pure wiring/dump
+ // determinism bug.
+ selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, selfDiff, "[%s] source-vs-source diff must be empty, got:\n%s", srv.name, selfDiff)
+
+ // source vs D is the meaningful case: the stored/dumped canonical form
+ // diffed against the user's original DDL must canonicalize equal. The version
+ // is threaded so a 5.7 schema is canonicalized as 5.7 (a bare CHARSET=utf8mb4
+ // resolves to utf8mb4_general_ci, not the 8.0 default) — the fix for the
+ // version-dispatch gap (omni catalog SessionState.Version + LoadSDLWithVersion).
+ noopDiff, err := mysqlDiffSDLMigration(source, representativeDDL, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noopDiff, "[%s] no-op idempotence FAILED: source-vs-D diff must be empty, got:\n%s", srv.name, noopDiff)
+ })
+ }
+}
+
+// TestSDLMinimalChangeLive asserts that a real change to D produces minimal DDL and
+// that applying it back converges (re-diff empty). Split out so a no-op failure
+// (TestSDLDeclarativePathLive) is reported independently of a change-generation
+// failure.
+//
+//nolint:tparallel
+func TestSDLMinimalChangeLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ // D2 = D with one added column on `book`.
+ addColumnDDL := strings.Replace(
+ representativeDDL,
+ "\tpage_count INT NOT NULL DEFAULT 0,",
+ "\tpage_count INT NOT NULL DEFAULT 0,\n\tisbn VARCHAR(20) NULL,",
+ 1,
+ )
+ require.NotEqual(t, representativeDDL, addColumnDDL, "test setup: addColumnDDL must differ from D")
+
+ // D2' = D with a changed column type (page_count INT -> BIGINT).
+ changeTypeDDL := strings.Replace(
+ representativeDDL,
+ "\tpage_count INT NOT NULL DEFAULT 0,",
+ "\tpage_count BIGINT NOT NULL DEFAULT 0,",
+ 1,
+ )
+ require.NotEqual(t, representativeDDL, changeTypeDDL, "test setup: changeTypeDDL must differ from D")
+
+ // D2'' = D with an added secondary index on book(title).
+ addIndexDDL := strings.Replace(
+ representativeDDL,
+ "\tpage_count INT NOT NULL DEFAULT 0,",
+ "\tpage_count INT NOT NULL DEFAULT 0,\n\tINDEX idx_book_title (title),",
+ 1,
+ )
+ require.NotEqual(t, representativeDDL, addIndexDDL, "test setup: addIndexDDL must differ from D")
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ t.Run("add_column", func(t *testing.T) {
+ source := syncToSDL(ctx, t, srv)
+
+ diff, err := mysqlDiffSDLMigration(source, addColumnDDL, srv.version)
+ require.NoError(t, err)
+ t.Logf("[%s] add-column diff:\n%s", srv.name, diff)
+ require.NotEmpty(t, diff, "[%s] add-column must produce DDL", srv.name)
+ upper := strings.ToUpper(diff)
+ require.Contains(t, upper, "ADD COLUMN", "[%s] expected ADD COLUMN, got:\n%s", srv.name, diff)
+ require.Contains(t, upper, "ISBN", "[%s] expected the new column, got:\n%s", srv.name, diff)
+ require.NotContains(t, upper, "CREATE TABLE", "[%s] add-column must not rebuild the table:\n%s", srv.name, diff)
+ require.NotContains(t, upper, "DROP TABLE", "[%s] add-column must not drop the table:\n%s", srv.name, diff)
+ // Strict minimality: exactly the ADD COLUMN, nothing else — on BOTH versions,
+ // now that the version is threaded and the 5.7 utf8mb4-default-collation
+ // phantom no longer contaminates the diff.
+ require.Equal(t, 1, statementCount(diff),
+ "[%s] add-column must be a single minimal statement, got:\n%s", srv.name, diff)
+ })
+
+ t.Run("change_type", func(t *testing.T) {
+ source := syncToSDL(ctx, t, srv)
+
+ diff, err := mysqlDiffSDLMigration(source, changeTypeDDL, srv.version)
+ require.NoError(t, err)
+ t.Logf("[%s] change-type diff:\n%s", srv.name, diff)
+ require.NotEmpty(t, diff, "[%s] change-type must produce DDL", srv.name)
+ upper := strings.ToUpper(diff)
+ require.True(t,
+ strings.Contains(upper, "MODIFY") || strings.Contains(upper, "CHANGE"),
+ "[%s] expected MODIFY/CHANGE COLUMN, got:\n%s", srv.name, diff)
+ require.Contains(t, upper, "BIGINT", "[%s] expected the new type, got:\n%s", srv.name, diff)
+ require.NotContains(t, upper, "CREATE TABLE", "[%s] change-type must not rebuild the table:\n%s", srv.name, diff)
+ require.Equal(t, 1, statementCount(diff),
+ "[%s] change-type must be a single minimal statement, got:\n%s", srv.name, diff)
+ })
+
+ // add_index now exercises REAL index-diff behavior: the merged omni
+ // breadth engine populates SchemaDiff.Indexes and generates a minimal
+ // ADD KEY. MySQL renders a secondary index as `ADD KEY` (the canonical
+ // synonym of ADD INDEX), so the assertion accepts either spelling.
+ t.Run("add_index", func(t *testing.T) {
+ source := syncToSDL(ctx, t, srv)
+
+ diff, err := mysqlDiffSDLMigration(source, addIndexDDL, srv.version)
+ require.NoError(t, err)
+ t.Logf("[%s] add-index diff:\n%s", srv.name, diff)
+ require.NotEmpty(t, diff, "[%s] add-index must produce DDL", srv.name)
+ upper := strings.ToUpper(diff)
+ require.True(t,
+ strings.Contains(upper, "ADD KEY") || strings.Contains(upper, "ADD INDEX"),
+ "[%s] expected ADD KEY/INDEX, got:\n%s", srv.name, diff)
+ require.Contains(t, upper, "IDX_BOOK_TITLE", "[%s] expected the new index, got:\n%s", srv.name, diff)
+ require.NotContains(t, upper, "CREATE TABLE", "[%s] add-index must not rebuild the table:\n%s", srv.name, diff)
+ require.NotContains(t, upper, "DROP TABLE", "[%s] add-index must not drop the table:\n%s", srv.name, diff)
+ require.Equal(t, 1, statementCount(diff),
+ "[%s] add-index must be a single minimal statement, got:\n%s", srv.name, diff)
+ })
+ })
+ }
+}
+
+// TestSDLApplyBackLive exercises assertion 3: take D, sync to source, compute the
+// minimal change DDL against D2, apply that DDL to the real DB, re-sync, and confirm
+// the schema now matches D2 (re-running the diff yields empty).
+//
+//nolint:tparallel
+func TestSDLApplyBackLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ addColumnDDL := strings.Replace(
+ representativeDDL,
+ "\tpage_count INT NOT NULL DEFAULT 0,",
+ "\tpage_count INT NOT NULL DEFAULT 0,\n\tisbn VARCHAR(20) NULL,",
+ 1,
+ )
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ // Build the database, capture its name, sync to source.
+ dbName := fmt.Sprintf("sdl_apply_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
+ admin, err := createLiveMySQLDriver(ctx, srv, "")
+ require.NoError(t, err)
+ _, err = admin.Execute(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName), db.ExecuteOptions{})
+ require.NoError(t, err)
+ admin.Close(ctx)
+ t.Cleanup(func() {
+ cleanup, err := createLiveMySQLDriver(ctx, srv, "")
+ if err != nil {
+ return
+ }
+ defer cleanup.Close(ctx)
+ _, _ = cleanup.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
+ })
+
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ _, err = driver.Execute(ctx, representativeDDL, db.ExecuteOptions{})
+ require.NoError(t, err)
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
+ require.NoError(t, err)
+
+ // Compute minimal change DDL for D2 (add column), version-aware so the change
+ // DDL is valid on the target server (no utf8mb4_0900_ai_ci on 5.7).
+ changeDDL, err := mysqlDiffSDLMigration(source, addColumnDDL, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, changeDDL, "[%s] expected non-empty change DDL", srv.name)
+ t.Logf("[%s] apply-back change DDL:\n%s", srv.name, changeDDL)
+
+ // The change DDL must never name a collation the target server lacks.
+ if srv.version == "5.7" {
+ require.NotContains(t, changeDDL, "utf8mb4_0900_ai_ci",
+ "[%s] change DDL names a collation that does not exist on 5.7:\n%s", srv.name, changeDDL)
+ }
+
+ // Apply it back to the real DB — this is where Error 1273 would surface on 5.7
+ // if the version were not threaded.
+ applyDriver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer applyDriver.Close(ctx)
+ _, applyErr := applyDriver.Execute(ctx, changeDDL, db.ExecuteOptions{})
+ require.NoError(t, applyErr, "[%s] apply-back DDL failed to apply", srv.name)
+
+ newMetadata, err := applyDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ newSource, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_MYSQL, true))
+ require.NoError(t, err)
+
+ // The schema must now match D2: re-diff yields empty.
+ converge, err := mysqlDiffSDLMigration(newSource, addColumnDDL, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "[%s] apply-back did not converge to D2, residual diff:\n%s", srv.name, converge)
+ })
+ }
+}
+
+// breadthCase exercises one MySQL breadth object type end to end through the wired
+// declarative path. baseDDL builds the starting schema; targetDDL is the desired SDL.
+// noop holds true when baseDDL already equals the target's object (idempotence is then
+// asserted against targetDDL directly). When noop is false, baseDDL omits/differs the
+// object so the diff produces a real change whose generated DDL must contain changeWant
+// (case-insensitive) and, when applied back, must converge.
+type breadthCase struct {
+ name string
+ baseDDL string
+ targetDDL string
+ changeWant string // uppercase substring the minimal change DDL must contain
+ skip57 bool // CHECK constraints are 8.0-only
+}
+
+// newLiveDatabase creates a fresh database on srv and registers cleanup. It returns the
+// database name so the caller can apply DDL and sync it repeatedly within one test.
+func newLiveDatabase(ctx context.Context, t *testing.T, srv liveServer, prefix string) string {
+ t.Helper()
+ dbName := fmt.Sprintf("%s_%s", prefix, strings.ReplaceAll(uuid.New().String(), "-", "_"))
+ admin, err := createLiveMySQLDriver(ctx, srv, "")
+ require.NoError(t, err)
+ _, err = admin.Execute(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName), db.ExecuteOptions{})
+ require.NoError(t, err)
+ admin.Close(ctx)
+ t.Cleanup(func() {
+ c, err := createLiveMySQLDriver(ctx, srv, "")
+ if err != nil {
+ return
+ }
+ defer c.Close(ctx)
+ _, _ = c.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
+ })
+ return dbName
+}
+
+// applyAndDump applies ddl to dbName on srv, syncs the schema, and returns the canonical
+// SDL dump (MetadataToSDL of the synced metadata) — i.e. the declarative-path source.
+func applyAndDump(ctx context.Context, t *testing.T, srv liveServer, dbName, ddl string) string {
+ t.Helper()
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, ddl, db.ExecuteOptions{})
+ require.NoError(t, err, "apply DDL")
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
+ require.NoError(t, err)
+ return source
+}
+
+// breadthCases covers the eight MySQL breadth object types the merged omni engine
+// supports: secondary/unique index, foreign key, CHECK (8.0), partition, view, stored
+// routine (function + procedure), trigger, and event. Each carries a base→target delta
+// that names exactly one object so the generated change is minimal.
+func breadthCases() []breadthCase {
+ return []breadthCase{
+ {
+ name: "secondary_index",
+ baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL,
+ INDEX idx_name (name)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ changeWant: "ADD KEY",
+ },
+ {
+ name: "unique_index",
+ baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, email VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, email VARCHAR(100) NOT NULL,
+ UNIQUE KEY uk_email (email)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ changeWant: "UNIQUE",
+ },
+ {
+ name: "foreign_key",
+ baseDDL: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE child (id INT PRIMARY KEY, pid INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ targetDDL: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE child (id INT PRIMARY KEY, pid INT NOT NULL,
+ CONSTRAINT fk_child_parent FOREIGN KEY (pid) REFERENCES parent (id) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ changeWant: "FOREIGN KEY",
+ },
+ {
+ name: "check_constraint",
+ baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, age INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, age INT NOT NULL,
+ CONSTRAINT chk_age CHECK (age >= 0)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ changeWant: "CHECK",
+ skip57: true,
+ },
+ {
+ name: "partition",
+ baseDDL: `CREATE TABLE sales (id INT NOT NULL AUTO_INCREMENT, sale_date DATE NOT NULL, PRIMARY KEY (id, sale_date)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ targetDDL: `CREATE TABLE sales (id INT NOT NULL AUTO_INCREMENT, sale_date DATE NOT NULL, PRIMARY KEY (id, sale_date)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY RANGE (YEAR(sale_date)) (PARTITION p2023 VALUES LESS THAN (2024), PARTITION p_future VALUES LESS THAN MAXVALUE);`,
+ changeWant: "PARTITION BY",
+ },
+ {
+ name: "view",
+ baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, active BOOLEAN NOT NULL DEFAULT TRUE, title VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE VIEW v AS SELECT id FROM t WHERE active = TRUE;`,
+ targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, active BOOLEAN NOT NULL DEFAULT TRUE, title VARCHAR(100) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE VIEW v AS SELECT id, title FROM t WHERE active = TRUE;`,
+ changeWant: "VIEW",
+ },
+ {
+ name: "function",
+ baseDDL: `CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE FUNCTION add_n(a INT) RETURNS INT DETERMINISTIC RETURN a + 1;`,
+ targetDDL: `CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE FUNCTION add_n(a INT) RETURNS INT DETERMINISTIC RETURN a + 2;`,
+ changeWant: "FUNCTION",
+ },
+ {
+ name: "procedure",
+ baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, cnt INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, cnt INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE PROCEDURE bump(IN tid INT) BEGIN UPDATE t SET cnt = cnt + 1 WHERE id = tid; END;`,
+ changeWant: "PROCEDURE",
+ },
+ {
+ name: "trigger",
+ baseDDL: `CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val); END;`,
+ targetDDL: `CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val + 1); END;`,
+ changeWant: "TRIGGER",
+ },
+ {
+ name: "event",
+ baseDDL: `CREATE TABLE log (id INT PRIMARY KEY AUTO_INCREMENT, d DATE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ targetDDL: `CREATE TABLE log (id INT PRIMARY KEY AUTO_INCREMENT, d DATE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE EVENT IF NOT EXISTS daily_ev ON SCHEDULE EVERY 1 DAY DO INSERT INTO log (d) VALUES (CURDATE());`,
+ changeWant: "EVENT",
+ },
+ }
+}
+
+// TestSDLBreadthLive is the breadth deliverable: every MySQL breadth object type is
+// round-tripped end to end through the wired declarative path (sync → MetadataToSDL →
+// mysqlDiffSDLMigration) on live MySQL 5.7 and 8.0, asserting three properties per
+// type:
+//
+// (1) no-op idempotence — the target schema synced, dumped, and diffed against its own
+// user DDL produces an empty migration (and source-vs-source is empty);
+// (2) minimal change — building the base schema and diffing the target yields exactly
+// the minimal ALTER/CREATE/DROP for that object (no table rebuild);
+// (3) apply-back convergence — applying that generated DDL to the real database and
+// re-diffing yields empty.
+//
+//nolint:tparallel
+func TestSDLBreadthLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, bc := range breadthCases() {
+ bc := bc
+ if srv.version == "5.7" && bc.skip57 {
+ continue
+ }
+ t.Run(bc.name, func(t *testing.T) {
+ // (1) No-op idempotence: build the TARGET, dump, diff against the
+ // target user DDL — must be empty on both versions.
+ noopDB := newLiveDatabase(ctx, t, srv, "sdl_breadth_noop")
+ targetSource := applyAndDump(ctx, t, srv, noopDB, bc.targetDDL)
+ t.Logf("[%s/%s] target dump:\n%s", srv.name, bc.name, targetSource)
+
+ selfDiff, err := mysqlDiffSDLMigration(targetSource, targetSource, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, selfDiff, "[%s/%s] source-vs-source must be empty:\n%s", srv.name, bc.name, selfDiff)
+
+ noop, err := mysqlDiffSDLMigration(targetSource, bc.targetDDL, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noop, "[%s/%s] no-op idempotence FAILED, residual:\n%s", srv.name, bc.name, noop)
+
+ // (2) Minimal change: build the BASE, diff the target.
+ changeDB := newLiveDatabase(ctx, t, srv, "sdl_breadth_chg")
+ baseSource := applyAndDump(ctx, t, srv, changeDB, bc.baseDDL)
+
+ change, err := mysqlDiffSDLMigration(baseSource, bc.targetDDL, srv.version)
+ require.NoError(t, err)
+ t.Logf("[%s/%s] change DDL:\n%s", srv.name, bc.name, change)
+ require.NotEmpty(t, change, "[%s/%s] change must be non-empty", srv.name, bc.name)
+ require.Contains(t, strings.ToUpper(change), bc.changeWant,
+ "[%s/%s] change missing %q:\n%s", srv.name, bc.name, bc.changeWant, change)
+ require.NotContains(t, strings.ToUpper(change), "DROP TABLE",
+ "[%s/%s] change must not drop the table:\n%s", srv.name, bc.name, change)
+ require.NotContains(t, strings.ToUpper(change), "CREATE TABLE",
+ "[%s/%s] change must not rebuild the table:\n%s", srv.name, bc.name, change)
+
+ // (3) Apply-back convergence: apply the generated DDL to the real DB,
+ // re-sync, re-diff must be empty. The omni plan is op-by-op; the joined
+ // blob applies cleanly because the MySQL driver's SplitSQL is BEGIN…END
+ // aware, so trigger/routine bodies with internal ";" stay intact.
+ applyDriver, err := createLiveMySQLDriver(ctx, srv, changeDB)
+ require.NoError(t, err)
+ defer applyDriver.Close(ctx)
+ _, applyErr := applyDriver.Execute(ctx, change, db.ExecuteOptions{})
+ require.NoError(t, applyErr, "[%s/%s] apply-back failed to apply:\n%s", srv.name, bc.name, change)
+
+ newMetadata, err := applyDriver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ newSource, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_MYSQL, true))
+ require.NoError(t, err)
+
+ converge, err := mysqlDiffSDLMigration(newSource, bc.targetDDL, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "[%s/%s] apply-back did not converge, residual:\n%s", srv.name, bc.name, converge)
+ })
+ }
+ })
+ }
+}
+
+// countAdviceCode returns how many advices carry the given code.
+func countAdviceCode(advices []*storepb.Advice, c int32) int {
+ n := 0
+ for _, a := range advices {
+ if a.Code == c {
+ n++
+ }
+ }
+ return n
+}
+
+// TestSDLDropAdvicesLive exercises schema.SDLDropAdvices (the registered MySQL drop-advice
+// analyzer, reached the same way the release-check gating reaches it) end to end against
+// live 5.7 and 8.0: sync the representative schema, then point the user SDL at a target that
+// drops a whole table and a column, and assert WARNING advices are produced with the
+// SDLDropOperation code. A no-op target must yield zero advices.
+//
+//nolint:tparallel
+func TestSDLDropAdvicesLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ // Target SDL drops the `book` table entirely and drops author.bio (a column).
+ dropTargetDDL := `
+CREATE TABLE author (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ active BOOLEAN NOT NULL DEFAULT TRUE,
+ rating DECIMAL(5, 2) NOT NULL DEFAULT 0.00,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ INDEX idx_author_name (name)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+`
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ meta, _ := syncToMetadata(ctx, t, srv)
+
+ // No-op target: representativeDDL itself must produce zero drop advices.
+ noopAdvices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, representativeDDL, meta, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noopAdvices, "[%s] no-op target must yield no drop advices, got: %+v", srv.name, noopAdvices)
+
+ // Destructive target: book table dropped (active_books view depends on it, so the
+ // view is dropped too) and author.bio column dropped.
+ advices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, dropTargetDDL, meta, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, advices, "[%s] destructive target must yield drop advices", srv.name)
+
+ for _, a := range advices {
+ require.Equal(t, storepb.Advice_WARNING, a.Status, "[%s] drop advice must be WARNING: %+v", srv.name, a)
+ }
+
+ dropCount := countAdviceCode(advices, code.SDLDropOperation.Int32())
+ require.GreaterOrEqual(t, dropCount, 2,
+ "[%s] expected at least DROP TABLE book + DROP COLUMN bio warnings, got: %+v", srv.name, advices)
+
+ // The dropped table and column must be named somewhere in the advice content.
+ joined := ""
+ for _, a := range advices {
+ joined += a.Content + "\n"
+ }
+ require.Contains(t, joined, "book", "[%s] expected the dropped table named, got:\n%s", srv.name, joined)
+ require.Contains(t, joined, "bio", "[%s] expected the dropped column named, got:\n%s", srv.name, joined)
+ })
+ }
+}
+
+// TestSDLSpatialSRIDPresenceLive verifies the SRID presence semantics (X5) against the
+// live 8.0 oracle: a geometry column with EXPLICIT SRID 0 (information_schema reports
+// SRS_ID=0 — a valid SRS, distinct from "no SRID" which reports NULL) and one with SRID
+// 4326 must both round-trip — dump -> SDL -> apply to a fresh database -> re-dump —
+// with byte-identical dumps and an empty self-diff. Before the fix the 0-sentinel
+// conflated SRID 0 with "no SRID" and dropped the attribute at dump time.
+func TestSDLSpatialSRIDPresenceLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+ srv := liveServers[0] // 8.0 — the SRID column attribute is an 8.0-only surface.
+
+ const ddl = `
+CREATE TABLE geo (
+ id INT PRIMARY KEY,
+ pt0 GEOMETRY NOT NULL /*!80003 SRID 0 */,
+ pt4326 POINT NOT NULL /*!80003 SRID 4326 */,
+ ptnone GEOMETRY NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+`
+ dbA := newLiveDatabase(ctx, t, srv, "sdl_srid_a")
+ require.NoError(t, applyDDL(ctx, t, srv, dbA, ddl))
+ dumpA := dumpSDL(ctx, t, srv, dbA)
+
+ // The dump must carry the explicit SRID 0 (presence, not a zero sentinel), the
+ // SRID 4326, and no SRID attribute on the undeclared column.
+ require.Contains(t, dumpA, "`pt0` geometry NOT NULL /*!80003 SRID 0 */")
+ require.Contains(t, dumpA, "`pt4326` point NOT NULL /*!80003 SRID 4326 */")
+ require.Contains(t, dumpA, "`ptnone` geometry NOT NULL,")
+ require.NotContains(t, dumpA, "`ptnone` geometry NOT NULL /*!80003")
+
+ // Apply the dump to a fresh database and re-dump: the round-trip must be lossless.
+ dbB := newLiveDatabase(ctx, t, srv, "sdl_srid_b")
+ require.NoError(t, applyDDL(ctx, t, srv, dbB, dumpA))
+ dumpB := dumpSDL(ctx, t, srv, dbB)
+ require.Equal(t, dumpA, dumpB, "dump -> apply -> re-dump must be byte-stable")
+
+ selfDiff, err := mysqlDiffSDLMigration(dumpB, dumpA, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, selfDiff, "SRID round-trip self-diff must be empty")
+
+ // Removing a real SRID restriction must diff (4326 -> unset is a column change).
+ // Note the 0 <-> unset transition is folded to a no-op by omni's canonicalizer
+ // (out of scope here); the dump above still preserves the explicit SRID 0
+ // faithfully, so the declared schema round-trips byte-exact.
+ noSridTarget := strings.Replace(dumpA, "`pt4326` point NOT NULL /*!80003 SRID 4326 */", "`pt4326` point NOT NULL", 1)
+ change, err := mysqlDiffSDLMigration(dumpA, noSridTarget, srv.version)
+ require.NoError(t, err)
+ require.Contains(t, change, "MODIFY", "dropping an explicit SRID 4326 must produce a column modification")
+}
+
+// End-to-end proof for BYT-9832 STAGE 2 (bytebase wiring) against the LIVE 5.7 (:13307)
+// and 8.0 (:13306) oracles. It drives the REAL production entry point,
+// schema.SDLMigration (the function database_migrate_executor.go's diff() calls), so the
+// whole path exercised is: seed object under a distinctive session context → sync to
+// metadata (which captures sql_mode/charset/collation, and event time_zone) →
+// schema.SDLMigration(userSDL, syncedMetadata, version) → the generated migration is
+// APPLIED on a deploy session whose DEFAULT sql_mode differs → read back
+// information_schema to prove the ORIGINAL context survived the recreate/ALTER.
+//
+// Without the STAGE 2 wiring (buildSDLSessionContextMap + ApplySessionContext on the
+// source catalog) the object comes back stamped with the deploy session's mode — which is
+// exactly the regression these assertions catch.
+
+// origRoutineMode is a non-default sql_mode whose semantics visibly differ from the deploy
+// default: PIPES_AS_CONCAT makes `||` string concat (not OR), NO_BACKSLASH_ESCAPES changes
+// escaping. A bare recreate under the deploy default would silently lose both.
+const origRoutineMode = "PIPES_AS_CONCAT,NO_BACKSLASH_ESCAPES"
+
+// deploySessionMode is the (different) sql_mode the migration is applied under, standing in
+// for a deploy connection whose session default is not the object's authoring mode.
+const deploySessionMode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
+
+// openLiveDB opens a raw *sql.DB against a live oracle with multi-statement support so a
+// framed migration (SET …; CREATE …; SET …) runs as one script on one session, and returns
+// it with a cleanup. It connects plaintext (no tls param), the equivalent of the mysql
+// CLI's --ssl-mode=DISABLED that 5.7 needs.
+func openLiveDB(t *testing.T, srv liveServer, database string) *sql.DB {
+ t.Helper()
+ cfg := mysqldriver.NewConfig()
+ cfg.User = "root"
+ cfg.Passwd = liveOraclePassword
+ cfg.Net = "tcp"
+ cfg.Addr = fmt.Sprintf("%s:%s", srv.host, srv.port)
+ cfg.DBName = database
+ cfg.MultiStatements = true
+ cfg.AllowNativePasswords = true
+ sqlDB, err := sql.Open("mysql", cfg.FormatDSN())
+ require.NoError(t, err, "[%s] sql.Open", srv.name)
+ t.Cleanup(func() { _ = sqlDB.Close() })
+ return sqlDB
+}
+
+// scConn grabs a dedicated single connection (so session-variable state is stable across
+// statements) and registers its cleanup.
+func scConn(ctx context.Context, t *testing.T, sqlDB *sql.DB, srv liveServer) *sql.Conn {
+ t.Helper()
+ conn, err := sqlDB.Conn(ctx)
+ require.NoError(t, err, "[%s] grab conn", srv.name)
+ t.Cleanup(func() { _ = conn.Close() })
+ return conn
+}
+
+// newSCDatabase creates a fresh database on srv and registers cleanup, returning its name.
+func newSCDatabase(ctx context.Context, t *testing.T, srv liveServer, prefix string) string {
+ t.Helper()
+ dbName := fmt.Sprintf("%s_%s", prefix, strings.ReplaceAll(uuid.New().String(), "-", "_"))
+ admin := openLiveDB(t, srv, "")
+ _, err := admin.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName))
+ require.NoError(t, err, "[%s] create db", srv.name)
+ t.Cleanup(func() {
+ c := openLiveDB(t, srv, "")
+ _, _ = c.ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName))
+ })
+ return dbName
+}
+
+// syncDBToMetadata syncs dbName on srv to the model.DatabaseMetadata the production release
+// path feeds schema.SDLMigration — the value whose per-object sql_mode/charset/collation
+// (and event time_zone) STAGE 2 threads into the diff.
+func syncDBToMetadata(ctx context.Context, t *testing.T, srv liveServer, dbName string) *model.DatabaseMetadata {
+ t.Helper()
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err, "[%s] open driver", srv.name)
+ defer driver.Close(ctx)
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err, "[%s] sync", srv.name)
+ return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true)
+}
+
+// applyMigrationUnderDeployMode runs the generated migration on a session whose sql_mode is
+// first set to deploySessionMode, proving the framing (not the ambient session) governs the
+// stored context.
+func applyMigrationUnderDeployMode(ctx context.Context, t *testing.T, conn *sql.Conn, srv, migrationSQL string) {
+ t.Helper()
+ _, err := conn.ExecContext(ctx, "SET SESSION sql_mode = "+quoteLiteral(deploySessionMode))
+ require.NoError(t, err, "[%s] set deploy mode", srv)
+ _, err = conn.ExecContext(ctx, migrationSQL)
+ require.NoError(t, err, "[%s] APPLY FAILED:\n%s", srv, migrationSQL)
+}
+
+// quoteLiteral single-quotes a MySQL string literal for the test's own SET statements
+// (the omni generator does its own quoting inside the migration).
+func quoteLiteral(s string) string {
+ return "'" + strings.ReplaceAll(s, "'", "''") + "'"
+}
+
+func execUnderMode(ctx context.Context, t *testing.T, conn *sql.Conn, srv liveServer, sqlMode, timeZone string, stmts ...string) {
+ t.Helper()
+ _, err := conn.ExecContext(ctx, "SET SESSION sql_mode = "+quoteLiteral(sqlMode))
+ require.NoError(t, err, "[%s] set authoring sql_mode", srv.name)
+ if timeZone != "" {
+ _, err = conn.ExecContext(ctx, "SET SESSION time_zone = "+quoteLiteral(timeZone))
+ require.NoError(t, err, "[%s] set authoring time_zone", srv.name)
+ }
+ for _, s := range stmts {
+ _, err = conn.ExecContext(ctx, s)
+ require.NoError(t, err, "[%s] seed stmt %q", srv.name, s)
+ }
+}
+
+func readRoutineSQLMode(ctx context.Context, t *testing.T, conn *sql.Conn, dbName, name string) string {
+ t.Helper()
+ var mode sql.NullString
+ err := conn.QueryRowContext(ctx,
+ "SELECT SQL_MODE FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA=? AND ROUTINE_NAME=?",
+ dbName, name).Scan(&mode)
+ require.NoError(t, err, "read routine sql_mode")
+ return mode.String
+}
+
+func readTriggerSQLMode(ctx context.Context, t *testing.T, conn *sql.Conn, dbName, name string) string {
+ t.Helper()
+ var mode sql.NullString
+ err := conn.QueryRowContext(ctx,
+ "SELECT SQL_MODE FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA=? AND TRIGGER_NAME=?",
+ dbName, name).Scan(&mode)
+ require.NoError(t, err, "read trigger sql_mode")
+ return mode.String
+}
+
+func readEventModeTZ(ctx context.Context, t *testing.T, conn *sql.Conn, dbName, name string) (string, string) {
+ t.Helper()
+ var mode, tz sql.NullString
+ err := conn.QueryRowContext(ctx,
+ "SELECT SQL_MODE, TIME_ZONE FROM information_schema.EVENTS WHERE EVENT_SCHEMA=? AND EVENT_NAME=?",
+ dbName, name).Scan(&mode, &tz)
+ require.NoError(t, err, "read event sql_mode/time_zone")
+ return mode.String, tz.String
+}
+
+// TestSDLSessionContextRoutineLive proves a routine whose BODY changes is re-emitted under
+// its ORIGINAL sql_mode across the whole production wiring (schema.SDLMigration).
+//
+//nolint:tparallel
+func TestSDLSessionContextRoutineLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ dbName := newSCDatabase(ctx, t, srv, "sc_rtn")
+ conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
+
+ // Seed the function under the ORIGINAL mode.
+ execUnderMode(ctx, t, conn, srv, origRoutineMode, "",
+ "CREATE FUNCTION f(a INT) RETURNS INT DETERMINISTIC RETURN a + 1")
+
+ // This is the exact value the release path hands schema.SDLMigration.
+ metadata := syncDBToMetadata(ctx, t, srv, dbName)
+
+ // Desired SDL: same routine, body edited (a + 2). Bare — carries no session framing.
+ userSDL := "CREATE FUNCTION f(a INT) RETURNS INT DETERMINISTIC RETURN a + 2;"
+ migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, migrationSQL, "[%s] expected a recreate migration", srv.name)
+ t.Logf("[%s] routine migration:\n%s", srv.name, migrationSQL)
+ // The framing must be present (proof STAGE 2 wired the context through).
+ require.Contains(t, migrationSQL, "SET sql_mode",
+ "[%s] migration lacks sql_mode framing:\n%s", srv.name, migrationSQL)
+
+ applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
+
+ got := readRoutineSQLMode(ctx, t, conn, dbName, "f")
+ require.Equal(t, origRoutineMode, got,
+ "[%s] routine sql_mode not preserved across recreate\nmigration:\n%s", srv.name, migrationSQL)
+ })
+ }
+}
+
+// TestSDLSessionContextTriggerLive proves a trigger whose BODY changes is re-emitted under
+// its ORIGINAL sql_mode across schema.SDLMigration.
+//
+//nolint:tparallel
+func TestSDLSessionContextTriggerLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ dbName := newSCDatabase(ctx, t, srv, "sc_trg")
+ conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
+
+ base := []string{
+ "CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
+ "CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
+ }
+ // Tables under the deploy mode; the TRIGGER under the ORIGINAL mode.
+ execUnderMode(ctx, t, conn, srv, deploySessionMode, "", base...)
+ execUnderMode(ctx, t, conn, srv, origRoutineMode, "",
+ "CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val); END")
+
+ metadata := syncDBToMetadata(ctx, t, srv, dbName)
+
+ userSDL := strings.Join([]string{
+ "CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ "CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ "CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val + 1); END;",
+ }, "\n")
+ migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, migrationSQL, "[%s] expected a trigger recreate migration", srv.name)
+ t.Logf("[%s] trigger migration:\n%s", srv.name, migrationSQL)
+ require.Contains(t, migrationSQL, "SET sql_mode",
+ "[%s] trigger migration lacks sql_mode framing:\n%s", srv.name, migrationSQL)
+
+ applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
+
+ got := readTriggerSQLMode(ctx, t, conn, dbName, "t_audit")
+ require.Equal(t, origRoutineMode, got,
+ "[%s] trigger sql_mode not preserved across recreate\nmigration:\n%s", srv.name, migrationSQL)
+ })
+ }
+}
+
+// TestSDLSessionContextEventLive proves an event whose BODY changes preserves both sql_mode
+// and the ORIGINAL time_zone across schema.SDLMigration. The event modify path is an ALTER
+// EVENT … DO (which empirically re-stamps sql_mode from the session), so a bare apply would
+// silently lose the original mode — the case the framing exists for.
+//
+//nolint:tparallel
+func TestSDLSessionContextEventLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ const origEventMode = "PIPES_AS_CONCAT"
+ const origTZ = "+08:00"
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ dbName := newSCDatabase(ctx, t, srv, "sc_evt")
+ conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
+
+ // event_scheduler toggle may lack privilege; ignore its error only.
+ if _, err := conn.ExecContext(ctx, "SET GLOBAL event_scheduler = OFF"); err != nil {
+ t.Logf("[%s] event_scheduler off (ignored): %v", srv.name, err)
+ }
+ execUnderMode(ctx, t, conn, srv, origEventMode, origTZ,
+ "CREATE EVENT e ON SCHEDULE EVERY 1 HOUR DISABLE DO SET @x = 1")
+
+ metadata := syncDBToMetadata(ctx, t, srv, dbName)
+
+ userSDL := "CREATE EVENT e ON SCHEDULE EVERY 1 HOUR DISABLE DO SET @x = 2;"
+ migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, migrationSQL, "[%s] expected an event modify migration", srv.name)
+ t.Logf("[%s] event migration:\n%s", srv.name, migrationSQL)
+ require.Contains(t, migrationSQL, "SET sql_mode",
+ "[%s] event migration lacks sql_mode framing:\n%s", srv.name, migrationSQL)
+
+ applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
+
+ gotMode, gotTZ := readEventModeTZ(ctx, t, conn, dbName, "e")
+ require.Equal(t, origEventMode, gotMode,
+ "[%s] event sql_mode not preserved\nmigration:\n%s", srv.name, migrationSQL)
+ require.Equal(t, origTZ, gotTZ,
+ "[%s] event time_zone not preserved\nmigration:\n%s", srv.name, migrationSQL)
+ })
+ }
+}
+
+// TestSDLSessionContextNewObjectDefaultLive proves a routine ABSENT from the source (a
+// first-time create) is emitted BARE — no session framing — so it adopts the server default
+// mode. STAGE 2 must only preserve context for objects that already exist in the source.
+//
+//nolint:tparallel
+func TestSDLSessionContextNewObjectDefaultLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ dbName := newSCDatabase(ctx, t, srv, "sc_new")
+ conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
+
+ // Source has only a table; the routine is brand new in the desired SDL.
+ execUnderMode(ctx, t, conn, srv, deploySessionMode, "",
+ "CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4")
+
+ metadata := syncDBToMetadata(ctx, t, srv, dbName)
+
+ userSDL := strings.Join([]string{
+ "CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ "CREATE FUNCTION g(a INT) RETURNS INT DETERMINISTIC RETURN a + 5;",
+ }, "\n")
+ migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, migrationSQL, "[%s] expected a create migration", srv.name)
+ t.Logf("[%s] new-object migration:\n%s", srv.name, migrationSQL)
+ // A first-time create must NOT be wrapped in session framing.
+ require.NotContains(t, migrationSQL, "SET sql_mode",
+ "[%s] first-time create should be bare (no session framing):\n%s", srv.name, migrationSQL)
+
+ // applyMigrationUnderDeployMode runs it on a session whose sql_mode is
+ // deploySessionMode; a bare CREATE therefore captures that mode. This proves a
+ // brand-new object adopts the deploy session default (no preserved context).
+ applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
+
+ got := readRoutineSQLMode(ctx, t, conn, dbName, "g")
+ require.Equal(t, deploySessionMode, got,
+ "[%s] new routine should adopt the deploy session mode, got %q", srv.name, got)
+ })
+ }
+}
+
+// TestSDLSessionContextNoChurnLive proves that an identical source and desired routine —
+// same body, with the source's session context carried in from synced metadata — produces
+// NO migration. The session context is deliberately excluded from declarative identity, so
+// a mode-only carry must never manufacture a phantom recreate.
+//
+//nolint:tparallel
+func TestSDLSessionContextNoChurnLive(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ dbName := newSCDatabase(ctx, t, srv, "sc_noop")
+ conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
+
+ execUnderMode(ctx, t, conn, srv, origRoutineMode, "",
+ "CREATE FUNCTION f(a INT) RETURNS INT DETERMINISTIC RETURN a + 1")
+
+ metadata := syncDBToMetadata(ctx, t, srv, dbName)
+
+ // Desired SDL == the dumped current object (same body). No change intended.
+ userSDL, err := schema.MetadataToSDL(storepb.Engine_MYSQL, metadata)
+ require.NoError(t, err)
+
+ migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, migrationSQL,
+ "[%s] identical schema (context carried) must produce no migration, got:\n%s", srv.name, migrationSQL)
+ })
+ }
+}
+
+// Real-world-schema smoke test for the MySQL declarative (SDL) migration path. Unlike the
+// synthetic stress suites (the stress and deep-stress axes above) these cases drive
+// ACTUAL production schemas — Sakila, employees, employees(partitioned), Roundcube, and
+// MediaWiki — through the exact production entry points end to end against live MySQL 5.7 +
+// 8.0:
+//
+// - schema.SDLMigration(MYSQL, userSDL, syncedMetadata, engineVersion),
+// - mysqlDiffSDLMigration(source, target, engineVersion),
+// - schema.SDLDropAdvices(MYSQL, userSDL, syncedMetadata, engineVersion),
+// - the real db/mysql sync + schema.MetadataToSDL dumper.
+//
+// The schemas are embedded preprocessed fixtures (testdata/realworld/*.sql) so the test is
+// self-contained. Sakila is THE priority: it carries real view / function / procedure /
+// trigger bodies (incl. a SQL SECURITY INVOKER view with fully schema-qualified references),
+// which is where the at-scale view/routine round-trip is won or lost.
+//
+// Shared helpers (createLiveMySQLDriver, newLiveDatabase, liveServers, liveOraclePassword,
+// statementCount, syncMetadata, dumpSDL, applyDDL) are declared with the live round-trip
+// axes above.
+
+// ----------------------------------------------------------------------------
+// Embedded preprocessed real-world schema fixtures.
+//
+// Preprocessing applied when the corpus was lifted into testdata (so the committed test runs
+// without the external corpus):
+// - sakila: DROP/CREATE SCHEMA + USE + session SET @OLD_* lines stripped; a single
+// leading SET FOREIGN_KEY_CHECKS=0 prepended (the dump's forward FK refs
+// require it). DELIMITER routine/trigger blocks kept verbatim. The
+// actor_info view keeps its hardcoded `sakila.`-qualified body, so sakila
+// MUST be loaded into a database literally named `sakila`.
+// - employees: DB-context + data-load tail (flush logs / SELECT 'LOADING' / source ...)
+// stripped; CREATE TABLE/VIEW kept. Wrapped in a scratch DB.
+// - employees_part: same as employees, keeping the /*!50510 ALTER ... PARTITION BY RANGE
+// COLUMNS */ version-gated blocks for `titles` and `salaries`.
+// - roundcube: no CREATE DATABASE — wrapped in a scratch DB. Every table is
+// ROW_FORMAT=DYNAMIC (a known table-option round-trip stressor) with real
+// charset variety (utf8mb4_unicode_ci, binary). Trailing INSERT stripped.
+// - mediawiki: MediaWiki placeholders preprocessed: /*_*/ and /*i*/ deleted,
+// /*$wgDBTableOptions*/ -> "ENGINE=InnoDB DEFAULT CHARSET=binary",
+// /*$wgDBprefix*/ -> empty. 58 tables incl. a MyISAM FULLTEXT searchindex.
+
+//go:embed testdata/realworld/sakila.sql
+var sakilaSchema string
+
+//go:embed testdata/realworld/employees.sql
+var employeesSchema string
+
+//go:embed testdata/realworld/employees_part.sql
+var employeesPartSchema string
+
+//go:embed testdata/realworld/roundcube.sql
+var roundcubeSchema string
+
+//go:embed testdata/realworld/mediawiki.sql
+var mediawikiSchema string
+
+// omniViewParseBug is the precise classification for the (B) omni SDL-parser limitation that
+// blocks every multi-table view in the real-world corpus.
+//
+// REPRO: schema.MetadataToSDL dumps a multi-table view's FROM clause in MySQL's canonical
+// parenthesized left-deep join form, e.g. (from sakila.film_list):
+//
+// ... from ((((`category` left join `film_category` on(...)) left join `film` on(...))
+// join `film_actor` on(...)) join `actor` on(...)) group by ...
+//
+// Re-loading that dump through the SDL path (catalog.LoadSDLWithVersion / LoadSQL, called by
+// mysqlDiffSDLMigration and schema.SDLMigration) fails with
+//
+// expected SELECT, TABLE, VALUES, or '('
+//
+// LOCUS: omni mysql/parser/select.go:390 — the FROM-clause table_reference parser does not
+// accept a parenthesized join group `( t1 JOIN t2 ON ... )`; the leading '(' falls through to
+// the query-primary parser, which only expects a parenthesized sub-SELECT. omni's top-level
+// Parse() accepts the same construct, so the gap is specifically in the SDL catalog loader's
+// view-body SELECT sub-parser. This is DISTINCT from the known varchar(N) BINARY -> _bin
+// collation phantom-MODIFY bug. Until omni fixes the parenthesized-join table_reference and the
+// pin updates, any fixture whose synced+dumped form carries a multi-table view (sakila and the
+// employees family, via current_dept_emp) cannot round-trip — these legs are pending, not red.
+const omniViewParseBug = "omni SDL view-body parser rejects MySQL's canonical parenthesized JOIN in a view FROM clause (omni mysql/parser/select.go:390); blocks every multi-table view"
+
+// omniVarcharBinaryBug is the OTHER known, being-fixed-in-parallel (B) omni bug: the omni SDL
+// loader drops the `BINARY` modifier on a `varchar(N) BINARY` column (which MySQL stores as the
+// `_bin` collation), so the original-vs-synced diff emits a phantom
+// `ALTER TABLE ... MODIFY COLUMN ... varchar(N)`. roundcube's cache/users tables use that form.
+// Pending until the omni fix merges and the pin updates.
+const omniVarcharBinaryBug = "omni SDL loader drops varchar(N) BINARY -> _bin collation, producing a phantom MODIFY COLUMN (confirmed (B) bug, fixed in omni separately)"
+
+// realWorldSchema describes one embedded production schema fixture.
+type realWorldSchema struct {
+ name string
+ ddl string
+ // fixedDBName, when non-empty, forces the scratch database to this literal name instead of
+ // a unique-suffixed one. Required for sakila, whose actor_info view body hardcodes the
+ // `sakila.` schema qualifier and therefore only resolves in a database named `sakila`.
+ fixedDBName string
+ // only57Skip marks a fixture that does not load on 5.7 (so the 5.7 leg is skipped with a
+ // classification rather than failing).
+ only57Skip string
+ // viewParsePending, when non-empty, marks a fixture whose canonical dump carries a
+ // multi-table view that the current omni SDL parser cannot re-read (see omniViewParseBug).
+ // The SDL-path legs (idempotence, original-vs-synced, and every migration scenario on this
+ // schema) are skipped with this classification rather than failing on an out-of-scope,
+ // being-fixed-upstream engine bug. The live LOAD + sync + MetadataToSDL dump still run and
+ // are asserted non-empty in Phase 0, so the dumper half of the round-trip stays covered.
+ viewParsePending string
+}
+
+func realWorldSchemas() []realWorldSchema {
+ return []realWorldSchema{
+ {
+ name: "sakila",
+ ddl: sakilaSchema,
+ fixedDBName: "sakila",
+ // On 5.7 the SQL SECURITY INVOKER actor_info view, whose body is fully `sakila.`
+ // qualified, fails to CREATE even into a database named sakila because 5.7 resolves
+ // the invoker view's referenced tables more strictly at create time (errno 1146 on
+ // sakila.actor under FOREIGN_KEY_CHECKS=0). 8.0 creates it fine. So the 5.7 leg is
+ // skipped (harness/engine-version classification — see report). All other sakila
+ // objects (16 tables, 6 plain views, 3 functions, 3 procedures, 3 triggers) load on
+ // both versions.
+ only57Skip: "actor_info SQL SECURITY INVOKER view (hardcoded sakila.* refs) fails to CREATE on 5.7",
+ // Multi-table-join views (film_list, customer_list, ...) now round-trip: the omni SDL
+ // parser's parenthesized-JOIN FROM fix is merged + pinned (#356).
+ },
+ {name: "employees", ddl: employeesSchema},
+ {name: "employees_part", ddl: employeesPartSchema},
+ {name: "roundcube", ddl: roundcubeSchema},
+ {name: "mediawiki", ddl: mediawikiSchema},
+ }
+}
+
+// newNamedLiveDatabase is newLiveDatabase with an explicit database name (for fixtures that
+// must use a fixed name, e.g. sakila). It drops any pre-existing database of that name first
+// and registers cleanup.
+func newNamedLiveDatabase(ctx context.Context, t *testing.T, srv liveServer, dbName string) string {
+ t.Helper()
+ admin, err := createLiveMySQLDriver(ctx, srv, "")
+ require.NoError(t, err)
+ _, err = admin.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`; CREATE DATABASE `%s`", dbName, dbName), db.ExecuteOptions{})
+ require.NoError(t, err)
+ admin.Close(ctx)
+ t.Cleanup(func() {
+ c, err := createLiveMySQLDriver(ctx, srv, "")
+ if err != nil {
+ return
+ }
+ defer c.Close(ctx)
+ _, _ = c.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
+ })
+ return dbName
+}
+
+// loadRealWorld creates a scratch database (fixed-named or unique), applies the fixture DDL
+// through the real db/mysql driver, syncs it, and returns the synced model metadata plus the
+// database name. The fixture DDL may contain DELIMITER blocks and SET FOREIGN_KEY_CHECKS=0;
+// the driver's DealWithDelimiter + single-connection execution handle both.
+func loadRealWorld(ctx context.Context, t *testing.T, srv liveServer, rw realWorldSchema) (*model.DatabaseMetadata, string) {
+ t.Helper()
+ var dbName string
+ if rw.fixedDBName != "" {
+ dbName = newNamedLiveDatabase(ctx, t, srv, rw.fixedDBName)
+ } else {
+ dbName = newLiveDatabase(ctx, t, srv, "sdl_rw_"+rw.name)
+ }
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, normalizeDelimiters(rw.ddl), db.ExecuteOptions{})
+ require.NoError(t, err, "[%s/%s] apply fixture DDL", srv.name, rw.name)
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true), dbName
+}
+
+// normalizeDelimiters rewrites a mysqldump-style multi-DELIMITER fixture into the no-DELIMITER
+// form the production split path (mysqlparser.DealWithDelimiter -> omni Split) handles cleanly.
+//
+// The sakila fixture interleaves regular ";"-delimited DDL with several DELIMITER blocks in
+// different styles (";;", "//", "$$") for its triggers/routines. The production path's
+// DealWithDelimiter only reliably rewrites a single uniform block; on the interleaved mix omni's
+// splitter silently truncates the statement stream (it stopped after the first routine, so only
+// 10 of 16 tables and 0 of 7 views ever loaded). Normalizing here — drop every standalone
+// DELIMITER line and rewrite each body's custom terminator (e.g. "END //") back to a plain
+// "END;" — yields the exact form the passing stress suites use (BEGIN ... END; with internal
+// ";", no DELIMITER), which omni's compound-statement-aware splitter segments correctly. The
+// fixtures without DELIMITER blocks pass through unchanged.
+func normalizeDelimiters(ddl string) string {
+ if !strings.Contains(ddl, "DELIMITER") {
+ return ddl
+ }
+ lines := strings.Split(ddl, "\n")
+ out := make([]string, 0, len(lines))
+ delim := ";"
+ for _, ln := range lines {
+ trimmed := strings.TrimSpace(ln)
+ if strings.HasPrefix(strings.ToUpper(trimmed), "DELIMITER ") {
+ delim = strings.TrimSpace(trimmed[len("DELIMITER "):])
+ if delim == "" {
+ delim = ";"
+ }
+ continue
+ }
+ if delim != ";" && strings.HasSuffix(trimmed, delim) {
+ if idx := strings.LastIndex(ln, delim); idx >= 0 {
+ ln = ln[:idx] + ";"
+ }
+ }
+ out = append(out, ln)
+ }
+ return strings.Join(out, "\n")
+}
+
+// objectCounts summarizes a synced schema for the Phase 0 report. It walks the raw proto so
+// it is agnostic to the (single, unnamed) schema MySQL uses.
+func objectCounts(meta *model.DatabaseMetadata) (tables, views, funcs, procs, triggers int) {
+ proto := meta.GetProto()
+ if proto == nil {
+ return 0, 0, 0, 0, 0
+ }
+ for _, sm := range proto.GetSchemas() {
+ tables += len(sm.GetTables())
+ views += len(sm.GetViews())
+ funcs += len(sm.GetFunctions())
+ procs += len(sm.GetProcedures())
+ for _, tbl := range sm.GetTables() {
+ triggers += len(tbl.GetTriggers())
+ }
+ }
+ return tables, views, funcs, procs, triggers
+}
+
+// ============================================================================
+// Phase 0 + Phase 1: load each real schema, then prove SDL idempotence.
+// ============================================================================
+
+// TestSDLRealWorldIdempotence is the baseline that matters most: every loaded production
+// schema must round-trip through the SDL path with NO phantom diff. For each schema × version:
+//
+// (0) load the fixture live, sync, and log object counts (Phase 0 usability),
+// (1a) source-vs-source determinism: source = MetadataToSDL(synced);
+// mysqlDiffSDLMigration(source, source, version) MUST be empty,
+// (1b) production path: schema.SDLMigration(MYSQL, source, synced, version) MUST be empty
+// (this is the exact call the release executor makes, with the canonical dump as the
+// user target — proving the dumper output re-imports to the identical metadata).
+//
+// Any non-empty no-op is a finding (logged with the spurious DDL for classification).
+//
+//nolint:tparallel
+func TestSDLRealWorldIdempotence(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, rw := range realWorldSchemas() {
+ rw := rw
+ t.Run(rw.name, func(t *testing.T) {
+ if srv.version == "5.7" && rw.only57Skip != "" {
+ t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, rw.name, rw.only57Skip)
+ }
+
+ meta, dbName := loadRealWorld(ctx, t, srv, rw)
+ tbl, vw, fn, pr, tg := objectCounts(meta)
+ t.Logf("[PHASE0 %s/%s] loaded: tables=%d views=%d functions=%d procedures=%d triggers=%d (db=%s)",
+ srv.name, rw.name, tbl, vw, fn, pr, tg, dbName)
+
+ source := dumpSDL(ctx, t, srv, dbName)
+ require.NotEmpty(t, source, "[%s/%s] MetadataToSDL produced empty SDL", srv.name, rw.name)
+
+ if rw.viewParsePending != "" {
+ // Load + sync + dump (Phase 0) covered above; the SDL re-import (Phase 1)
+ // is blocked by an out-of-scope omni parser bug. Skip with classification.
+ t.Skipf("[%s/%s] SDL re-import pending: %s", srv.name, rw.name, rw.viewParsePending)
+ }
+
+ // (1a) determinism.
+ selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
+ require.NoError(t, err)
+ if selfDiff != "" {
+ t.Logf("[PHASE1a %s/%s] NON-EMPTY source-vs-source (%d stmts):\n%s",
+ srv.name, rw.name, statementCount(selfDiff), selfDiff)
+ }
+ require.Empty(t, selfDiff, "[%s/%s] source-vs-source must be empty, got:\n%s", srv.name, rw.name, selfDiff)
+
+ // (1b) production path, canonical dump as user target.
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, source, meta, srv.version)
+ require.NoError(t, err)
+ if noop != "" {
+ t.Logf("[PHASE1b %s/%s] NON-EMPTY production no-op (%d stmts):\n%s",
+ srv.name, rw.name, statementCount(noop), noop)
+ t.Logf("[PHASE1b %s/%s] dumped source:\n%s", srv.name, rw.name, source)
+ }
+ require.Empty(t, noop, "[%s/%s] production-path no-op must be empty, got:\n%s", srv.name, rw.name, noop)
+ })
+ }
+ })
+ }
+}
+
+// TestSDLRealWorldOriginalVsSynced is the adversarial original-vs-synced probe: it diffs the
+// ORIGINAL hand-authored fixture SDL (as a user would commit it) against the engine-synced
+// metadata. This is stricter than the canonical-dump round-trip because the original carries
+// non-canonical forms (utf8 vs utf8mb3, int display widths, charset defaults, hardcoded view
+// qualifiers, ROW_FORMAT options, ...) that the engine rewrites on store. A non-empty result
+// here pins a real normalization gap the production path would surface to a user editing the
+// committed schema.
+//
+// NOTE: this is run only for fixtures whose original text re-imports cleanly under the omni
+// SDL loader (which disables FK checks but parses the raw DDL). Fixtures that carry DELIMITER
+// blocks, version-gated /*! */ partition comments, or MediaWiki-style standalone CREATE INDEX
+// statements are NOT valid single-document SDL inputs to the omni loader, so they are diffed
+// only through the canonical-dump round-trip in TestSDLRealWorldIdempotence. The viable
+// original-vs-synced fixtures are roundcube (pure CREATE TABLE) and employees (CREATE TABLE +
+// CREATE OR REPLACE VIEW). They are the at-scale normalization stressors.
+//
+//nolint:tparallel
+func TestSDLRealWorldOriginalVsSynced(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ // Only fixtures that are valid as a single SDL document (no DELIMITER / no /*! */ gated
+ // ALTERs / no standalone CREATE INDEX) are eligible. employees uses CREATE OR REPLACE VIEW
+ // which the omni loader accepts; roundcube is pure CREATE TABLE.
+ //
+ // Both eligible fixtures currently surface a distinct, being-fixed-upstream (B) omni bug, so
+ // the assertion is pending with a precise classification (the load + sync still run):
+ // - roundcube: varchar(N) BINARY -> _bin phantom MODIFY (omniVarcharBinaryBug),
+ // - employees: current_dept_emp's parenthesized-join dump fails to re-parse (omniViewParseBug).
+ eligible := map[string]string{"roundcube": omniVarcharBinaryBug, "employees": omniViewParseBug}
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, rw := range realWorldSchemas() {
+ rw := rw
+ pending, ok := eligible[rw.name]
+ if !ok {
+ continue
+ }
+ t.Run(rw.name, func(t *testing.T) {
+ meta, _ := loadRealWorld(ctx, t, srv, rw)
+ if pending != "" {
+ t.Skipf("[%s/%s] original-vs-synced pending: %s", srv.name, rw.name, pending)
+ }
+
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, rw.ddl, meta, srv.version)
+ require.NoError(t, err)
+ if noop != "" {
+ t.Logf("[ORIGINAL-VS-SYNCED %s/%s] NON-EMPTY (%d stmts):\n%s",
+ srv.name, rw.name, statementCount(noop), noop)
+ }
+ require.Empty(t, noop, "[%s/%s] original-vs-synced no-op must be empty, got:\n%s", srv.name, rw.name, noop)
+ })
+ }
+ })
+ }
+}
+
+// ============================================================================
+// Phase 2: realistic daily-dev migrations on the live DB, mixing object types.
+//
+// Each scenario: source = MetadataToSDL(sync(current live DB)); build a modified target;
+// plan = mysqlDiffSDLMigration(source, target, version); assert the plan is minimal +
+// correctly ordered (per scenario); apply the plan to the live DB; re-sync; assert the next
+// diff is empty (convergence).
+//
+// Sakila is the primary vehicle (real views/functions/procedures/triggers). employees_part
+// covers a partition change; roundcube/mediawiki cover table/column/index/FK breadth at scale.
+// ============================================================================
+
+// rwScenario is one daily-dev migration applied on top of a freshly-loaded real schema.
+type rwScenario struct {
+ name string
+ // schema selects which fixture to load as the live baseline.
+ schema string
+ // mutate transforms the canonical source SDL into the developer's modified target. It
+ // receives the dumped source so it can do targeted string surgery on the real stored form.
+ mutate func(t *testing.T, srv liveServer, source string) string
+ // wantContains are uppercased substrings the generated plan MUST contain (minimality /
+ // correctness anchors). Empty means "just assert non-empty + converges".
+ wantContains []string
+ // skip57 marks scenarios that only run on 8.0 (e.g. sakila-based ones).
+ skip57 string
+}
+
+// mustReplace asserts the replacement actually changed the text (guards against fixture drift
+// silently turning a scenario into a no-op).
+func mustReplace(t *testing.T, s, old, replacement string) string {
+ t.Helper()
+ require.Contains(t, s, old, "scenario setup: source must contain %q to mutate", old)
+ return strings.Replace(s, old, replacement, 1)
+}
+
+func rwScenarios() []rwScenario {
+ return []rwScenario{
+ // ---- Sakila: VIEW changes ----
+ {
+ name: "sakila_view_alter_select",
+ schema: "sakila",
+ skip57: "sakila view scenarios run on 8.0 (sakila does not fully load on 5.7)",
+ // film_list is a real view joining film/category/film_actor/actor. Append a genuinely
+ // new column (replacement_cost) to its SELECT — a developer broadening a reporting view.
+ // (price/rental_rate is already projected, so we add a column not already present.)
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addColumnToView(t, source, "film_list", "`film`.`replacement_cost` AS `replacement_cost`")
+ },
+ wantContains: []string{"FILM_LIST"},
+ },
+ {
+ name: "sakila_view_add_new_and_view_on_view",
+ schema: "sakila",
+ skip57: "sakila view scenarios run on 8.0",
+ // Add a brand-new view AND a view that depends on it (view-on-view), appended to the
+ // source. The plan must create both, ordered base-before-dependent.
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + `
+CREATE VIEW v_actor_min AS SELECT actor_id, first_name FROM actor;
+CREATE VIEW v_actor_min2 AS SELECT actor_id FROM v_actor_min;
+`
+ },
+ wantContains: []string{"V_ACTOR_MIN", "V_ACTOR_MIN2"},
+ },
+ {
+ name: "sakila_view_drop",
+ schema: "sakila",
+ skip57: "sakila view scenarios run on 8.0",
+ // Drop the sales_by_store view (a real aggregate view) by removing it from the source.
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropObjectBlock(t, source, "VIEW", "sales_by_store")
+ },
+ wantContains: []string{"DROP VIEW", "SALES_BY_STORE"},
+ },
+ // ---- Sakila: FUNCTION changes ----
+ {
+ name: "sakila_function_add",
+ schema: "sakila",
+ skip57: "sakila routine scenarios run on 8.0",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + `
+CREATE FUNCTION f_film_count() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM film);
+`
+ },
+ wantContains: []string{"F_FILM_COUNT"},
+ },
+ {
+ name: "sakila_function_drop",
+ schema: "sakila",
+ skip57: "sakila routine scenarios run on 8.0",
+ // Drop inventory_held_by_customer (a real function with a body).
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropObjectBlock(t, source, "FUNCTION", "inventory_held_by_customer")
+ },
+ wantContains: []string{"DROP FUNCTION", "INVENTORY_HELD_BY_CUSTOMER"},
+ },
+ // ---- Sakila: PROCEDURE changes ----
+ {
+ name: "sakila_procedure_add",
+ schema: "sakila",
+ skip57: "sakila routine scenarios run on 8.0",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + `
+CREATE PROCEDURE p_touch_film(IN fid INT) BEGIN UPDATE film SET last_update = NOW() WHERE film_id = fid; END;
+`
+ },
+ wantContains: []string{"P_TOUCH_FILM"},
+ },
+ {
+ name: "sakila_procedure_drop",
+ schema: "sakila",
+ skip57: "sakila routine scenarios run on 8.0",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropObjectBlock(t, source, "PROCEDURE", "film_not_in_stock")
+ },
+ wantContains: []string{"DROP PROCEDURE", "FILM_NOT_IN_STOCK"},
+ },
+ // ---- Sakila: TRIGGER changes ----
+ {
+ name: "sakila_trigger_add",
+ schema: "sakila",
+ skip57: "sakila trigger scenarios run on 8.0",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + `
+CREATE TRIGGER trg_payment_bi BEFORE INSERT ON payment FOR EACH ROW SET NEW.last_update = NOW();
+`
+ },
+ wantContains: []string{"TRG_PAYMENT_BI"},
+ },
+ {
+ name: "sakila_trigger_drop",
+ schema: "sakila",
+ skip57: "sakila trigger scenarios run on 8.0",
+ // Drop the ins_film trigger (real multi-statement trigger on film).
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropTrigger(t, source, "ins_film")
+ },
+ wantContains: []string{"DROP TRIGGER", "INS_FILM"},
+ },
+ // ---- Sakila: TABLE / COLUMN / INDEX / FK changes ----
+ {
+ name: "sakila_table_add_column_index",
+ schema: "sakila",
+ skip57: "run on 8.0 alongside the other sakila scenarios",
+ // Add a NOT NULL column with a default + a secondary index to the real `category`
+ // table (widening daily dev: a new flag + an index on it).
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := addColumnToTable(t, source, "category", "`is_featured` tinyint(1) NOT NULL DEFAULT '0'")
+ return addIndexToTable(t, s, "category", "KEY `idx_cat_featured` (`is_featured`)")
+ },
+ wantContains: []string{"IS_FEATURED", "IDX_CAT_FEATURED"},
+ },
+ {
+ name: "sakila_widen_varchar",
+ schema: "sakila",
+ skip57: "run on 8.0 alongside the other sakila scenarios",
+ // Widen actor.first_name VARCHAR(45) -> VARCHAR(80): a classic column-type change.
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return widenActorFirstName(t, source)
+ },
+ wantContains: []string{"FIRST_NAME"},
+ },
+ // ---- Sakila: COMBINED feature migration (table + dependent view + function + trigger) ----
+ {
+ name: "sakila_combined_feature_release",
+ schema: "sakila",
+ skip57: "run on 8.0 alongside the other sakila scenarios",
+ // One release that, together: adds a column to `staff`, adds a function, adds a
+ // trigger on `staff`, and adds a new view that reads the new column. Exercises correct
+ // cross-object ordering in a single diff.
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := addColumnToTable(t, source, "staff", "`shift_note` varchar(120) DEFAULT NULL")
+ s += `
+CREATE FUNCTION f_staff_count() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM staff);
+CREATE VIEW v_staff_notes AS SELECT staff_id, shift_note FROM staff;
+CREATE TRIGGER trg_staff_bu BEFORE UPDATE ON staff FOR EACH ROW SET NEW.last_update = NOW();
+`
+ return s
+ },
+ wantContains: []string{"SHIFT_NOTE", "F_STAFF_COUNT", "V_STAFF_NOTES", "TRG_STAFF_BU"},
+ },
+ // ---- employees_part: PARTITION change ----
+ {
+ name: "employees_part_partition_change",
+ schema: "employees_part",
+ // salaries is RANGE COLUMNS(from_date) partitioned. Coalesce its tail by replacing the
+ // whole partition definition with a coarser 3-partition scheme. The plan must emit a
+ // partition reorganization, apply, and converge.
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return repartitionSalaries(t, source)
+ },
+ wantContains: []string{"PARTITION"},
+ },
+ // ---- roundcube: table/column/index/FK at scale ----
+ {
+ name: "roundcube_add_column_index",
+ schema: "roundcube",
+ // Add a column + index to the real `users` table.
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := addColumnToTable(t, source, "users", "`timezone` varchar(64) DEFAULT NULL")
+ return addIndexToTable(t, s, "users", "KEY `idx_users_tz` (`timezone`)")
+ },
+ wantContains: []string{"TIMEZONE", "IDX_USERS_TZ"},
+ },
+ {
+ name: "roundcube_drop_index",
+ schema: "roundcube",
+ // Drop a genuine non-FK secondary index so the plan emits a clean DROP INDEX. `session`
+ // has no foreign keys and exactly one secondary index (expires_at_index), so dropping it
+ // can never hit errno 1553. (contacts' only secondary index, user_contacts_index, backs
+ // its user_id FK and cannot be dropped on its own — that would be a different, FK-ordering
+ // scenario.)
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropFirstSecondaryIndex(t, source, "session")
+ },
+ wantContains: []string{"DROP", "EXPIRES_AT_INDEX"},
+ },
+ // ---- mediawiki: table/column/index at scale (59-table schema) ----
+ {
+ name: "mediawiki_add_column",
+ schema: "mediawiki",
+ // Add a column to the real `page` table in a 58-table schema. The diff must be minimal
+ // (touch only `page`), not churn the other 57 tables.
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addColumnToTable(t, source, "page", "`page_extra_flag` tinyint(1) NOT NULL DEFAULT '0'")
+ },
+ wantContains: []string{"PAGE_EXTRA_FLAG"},
+ },
+ }
+}
+
+// TestSDLRealWorldMigrations is Phase 2: realistic daily-dev migrations, mixing object types,
+// verified end to end (emit minimal+ordered DDL -> apply to the live DB -> converge).
+//
+//nolint:tparallel
+func TestSDLRealWorldMigrations(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ byName := map[string]realWorldSchema{}
+ for _, rw := range realWorldSchemas() {
+ byName[rw.name] = rw
+ }
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, sc := range rwScenarios() {
+ sc := sc
+ t.Run(sc.name, func(t *testing.T) {
+ if srv.version == "5.7" && sc.skip57 != "" {
+ t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, sc.name, sc.skip57)
+ }
+ rw, ok := byName[sc.schema]
+ require.True(t, ok, "unknown schema %q", sc.schema)
+ if srv.version == "5.7" && rw.only57Skip != "" {
+ t.Skipf("[%s/%s] schema %s not loadable on 5.7: %s", srv.name, sc.name, rw.name, rw.only57Skip)
+ }
+ if rw.viewParsePending != "" {
+ // The mysqlDiffSDLMigration plan loads the dumped source, which carries a
+ // multi-table view the omni SDL parser cannot re-read. Every scenario on this
+ // schema is blocked at the source-load step (not by the scenario's own change),
+ // so skip with classification rather than fail. NOTE: this gates the entire
+ // sakila view/function/procedure/trigger migration path — see report headline.
+ t.Skipf("[%s/%s] migration plan pending: %s", srv.name, sc.name, rw.viewParsePending)
+ }
+
+ _, dbName := loadRealWorld(ctx, t, srv, rw)
+ source := dumpSDL(ctx, t, srv, dbName)
+
+ target := sc.mutate(t, srv, source)
+ require.NotEqual(t, source, target, "[%s/%s] scenario produced an identical target (no change)", srv.name, sc.name)
+
+ plan, err := mysqlDiffSDLMigration(source, target, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, plan, "[%s/%s] scenario must produce DDL", srv.name, sc.name)
+ t.Logf("[PHASE2 %s/%s] plan (%d stmts):\n%s", srv.name, sc.name, statementCount(plan), plan)
+
+ upper := strings.ToUpper(plan)
+ for _, want := range sc.wantContains {
+ require.Contains(t, upper, want, "[%s/%s] plan missing %q:\n%s", srv.name, sc.name, want, plan)
+ }
+
+ // Apply the generated DDL to the live DB and confirm convergence.
+ applyErr := applyDDL(ctx, t, srv, dbName, plan)
+ require.NoError(t, applyErr, "[%s/%s] plan failed to apply:\n%s", srv.name, sc.name, plan)
+
+ newSource := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(newSource, target, srv.version)
+ require.NoError(t, err)
+ if converge != "" {
+ t.Logf("[PHASE2 %s/%s] NON-EMPTY residual after apply (%d stmts):\n%s",
+ srv.name, sc.name, statementCount(converge), converge)
+ }
+ require.Empty(t, converge, "[%s/%s] did not converge, residual:\n%s", srv.name, sc.name, converge)
+ })
+ }
+ })
+ }
+}
+
+// ============================================================================
+// String-surgery helpers for Phase 2 target construction. They operate on the canonical
+// MetadataToSDL dump (backtick-quoted, lowercase keywords for column defs, uppercase for
+// table-level CREATE) and assert their anchor exists so a fixture drift fails loudly.
+// ============================================================================
+
+// findObjectSegment locates the whole CREATE statement for an object in the dumped source and
+// returns its [start,end) byte range. It splits the source with the SAME splitter the SDL
+// loader uses (mysqlparser.SplitSQL -> omni Split), so a multi-statement routine body (DECLARE
+// ...; ...; END;) is treated as one segment — naive ";"-scanning truncated such bodies at their
+// first internal semicolon. headerMatch must hold for the (whitespace-trimmed) segment text.
+func findObjectSegment(t *testing.T, source string, headerMatch func(stmt string) bool) (int, int) {
+ t.Helper()
+ stmts, err := mysqlparser.SplitSQL(source)
+ require.NoError(t, err, "split source for object lookup")
+ for _, s := range stmts {
+ if headerMatch(strings.TrimSpace(s.Text)) {
+ return int(s.Range.Start), int(s.Range.End)
+ }
+ }
+ return -1, -1
+}
+
+// dumpedViewHeader reports whether stmt is the CREATE statement for the named view in the
+// canonical dump. The dumper emits "CREATE OR REPLACE ALGORITHM=UNDEFINED SQL SECURITY DEFINER
+// VIEW `name` AS ..." (or, for an INVOKER view, "... SQL SECURITY INVOKER VIEW `name`"), so the
+// modifiers between VIEW and the name vary — match on the "VIEW `name`" token regardless of the
+// ALGORITHM/SQL SECURITY clause.
+func dumpedViewHeader(stmt, view string) bool {
+ u := strings.ToUpper(stmt)
+ return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, " VIEW `"+strings.ToUpper(view)+"` ")
+}
+
+// removeSegment excises [start,end) from source and trims now-orphaned blank lines.
+func removeSegment(source string, start, end int) string {
+ // Swallow a trailing newline so we don't leave a blank gap where the statement was.
+ for end < len(source) && (source[end] == '\n' || source[end] == '\r') {
+ end++
+ }
+ return source[:start] + source[end:]
+}
+
+// addColumnToView appends a column expression to a view's SELECT list by inserting it before the
+// FROM of that view's definition. It operates on the whole dumped view statement (located via
+// the splitter) so it is robust to the real "CREATE OR REPLACE ALGORITHM=... VIEW" header form.
+// The FROM anchor tolerates any surrounding whitespace: the pretty-printed dump puts the
+// top-level from on its own line (…AS `x`\nfrom `t`…).
+func addColumnToView(t *testing.T, source, view, colExpr string) string {
+ t.Helper()
+ start, end := findObjectSegment(t, source, func(stmt string) bool { return dumpedViewHeader(stmt, view) })
+ require.GreaterOrEqual(t, start, 0, "view %q not found in source", view)
+ stmt := source[start:end]
+ loc := reViewFromAnchor.FindStringIndex(stmt)
+ require.NotNil(t, loc, "view %q body has no FROM to anchor on:\n%s", view, stmt)
+ fromIdx := loc[0]
+ newStmt := stmt[:fromIdx] + "," + colExpr + stmt[fromIdx:]
+ return source[:start] + newStmt + source[end:]
+}
+
+// reViewFromAnchor matches the first whitespace-delimited FROM keyword in a dumped view
+// statement (case-insensitive, any whitespace kind on both sides).
+var reViewFromAnchor = regexp.MustCompile(`(?i)[ \t\r\n]from[ \t\r\n]`)
+
+// dropObjectBlock removes the whole CREATE <kind> ... block (VIEW/FUNCTION/PROCEDURE) for the
+// named object so the diff emits a DROP. It locates the statement via the SDL splitter, so a
+// routine with a multi-statement body is removed in full.
+func dropObjectBlock(t *testing.T, source, kind, name string) string {
+ t.Helper()
+ var match func(stmt string) bool
+ switch kind {
+ case "VIEW":
+ match = func(stmt string) bool { return dumpedViewHeader(stmt, name) }
+ case "FUNCTION":
+ match = func(stmt string) bool {
+ u := strings.ToUpper(stmt)
+ return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, "FUNCTION `"+strings.ToUpper(name)+"`")
+ }
+ case "PROCEDURE":
+ match = func(stmt string) bool {
+ u := strings.ToUpper(stmt)
+ return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, "PROCEDURE `"+strings.ToUpper(name)+"`")
+ }
+ default:
+ t.Fatalf("unsupported kind %q", kind)
+ }
+ start, end := findObjectSegment(t, source, match)
+ require.GreaterOrEqual(t, start, 0, "%s %q not found in source", kind, name)
+ return removeSegment(source, start, end)
+}
+
+// dropTrigger removes the whole CREATE TRIGGER `name` ... block (located via the SDL splitter,
+// so a BEGIN ... END; body is removed in full).
+func dropTrigger(t *testing.T, source, name string) string {
+ t.Helper()
+ start, end := findObjectSegment(t, source, func(stmt string) bool {
+ u := strings.ToUpper(stmt)
+ return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, "TRIGGER `"+strings.ToUpper(name)+"`")
+ })
+ require.GreaterOrEqual(t, start, 0, "trigger %q not found in source", name)
+ return removeSegment(source, start, end)
+}
+
+// addColumnToTable inserts a column definition into a table's CREATE block, right after the
+// table's PRIMARY KEY line (or before the closing paren if no PK line is found). It anchors on
+// the canonical "CREATE TABLE `name` (" header.
+func addColumnToTable(t *testing.T, source, table, colDef string) string {
+ t.Helper()
+ header := "CREATE TABLE `" + table + "` ("
+ start := strings.Index(source, header)
+ require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
+ // Find the closing ")" of this CREATE TABLE at the start of a line (the dumper emits the
+ // closing paren + options on its own line beginning with ')').
+ bodyStart := start + len(header)
+ closeIdx := findTableClose(source, bodyStart)
+ require.GreaterOrEqual(t, closeIdx, 0, "table %q close paren not found", table)
+ // Insert the column as a new line just before the close. Strip any trailing comma handling:
+ // the dumper's last body line has no trailing comma, so we add ",\n <colDef>" after the
+ // last body line's content. Simplest robust form: insert " <colDef>,\n" right after the
+ // body start (first column position) — MySQL accepts column order changes via SDL.
+ insertion := " " + colDef + ",\n"
+ return source[:bodyStart] + "\n" + insertion + source[bodyStart:closeIdx] + source[closeIdx:]
+}
+
+// addIndexToTable inserts an index definition into a table's CREATE block before the closing
+// paren.
+func addIndexToTable(t *testing.T, source, table, idxDef string) string {
+ t.Helper()
+ header := "CREATE TABLE `" + table + "` ("
+ start := strings.Index(source, header)
+ require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
+ bodyStart := start + len(header)
+ closeIdx := findTableClose(source, bodyStart)
+ require.GreaterOrEqual(t, closeIdx, 0, "table %q close paren not found", table)
+ // The line immediately before closeIdx ends a body element without a trailing comma. Insert
+ // ",\n <idxDef>" right before the close paren, after trimming the trailing newline.
+ body := source[bodyStart:closeIdx]
+ trimmed := strings.TrimRight(body, "\n")
+ newBody := trimmed + ",\n " + idxDef + "\n"
+ return source[:bodyStart] + newBody + source[closeIdx:]
+}
+
+// findTableClose returns the index of the line-leading ')' that closes the CREATE TABLE body
+// beginning at bodyStart. The dumper emits ")\n" or ") ENGINE=..." with the ')' at column 0.
+func findTableClose(source string, bodyStart int) int {
+ nl := strings.Index(source[bodyStart:], "\n)")
+ if nl < 0 {
+ return -1
+ }
+ return bodyStart + nl + 1
+}
+
+// dropFirstSecondaryIndex removes the first " KEY `...` (...)," line from the named table so
+// the diff emits a DROP INDEX. It skips PRIMARY/UNIQUE to keep the change a plain index drop.
+func dropFirstSecondaryIndex(t *testing.T, source, table string) string {
+ t.Helper()
+ header := "CREATE TABLE `" + table + "` ("
+ start := strings.Index(source, header)
+ require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
+ bodyStart := start + len(header)
+ closeIdx := findTableClose(source, bodyStart)
+ require.GreaterOrEqual(t, closeIdx, 0, "table %q close paren not found", table)
+ body := source[bodyStart:closeIdx]
+ lines := strings.Split(body, "\n")
+ out := make([]string, 0, len(lines))
+ dropped := false
+ for _, ln := range lines {
+ trimmed := strings.TrimSpace(ln)
+ if !dropped && strings.HasPrefix(trimmed, "KEY `") {
+ dropped = true
+ // Remove a dangling comma on the previous line if this was the last body element.
+ continue
+ }
+ out = append(out, ln)
+ }
+ require.True(t, dropped, "table %q had no secondary KEY to drop:\n%s", table, body)
+ newBody := strings.Join(out, "\n")
+ // Fix a possible trailing comma before the close paren.
+ newBody = strings.TrimRight(newBody, "\n")
+ newBody = strings.TrimRight(newBody, ",")
+ newBody += "\n"
+ return source[:bodyStart] + newBody + source[closeIdx:]
+}
+
+// widenActorFirstName changes actor.first_name varchar(45) -> varchar(80) in the source.
+func widenActorFirstName(t *testing.T, source string) string {
+ t.Helper()
+ // The dumper renders the column as: `first_name` varchar(45) ...
+ old := "`first_name` varchar(45)"
+ require.Contains(t, source, old, "actor.first_name varchar(45) not found in source")
+ return strings.Replace(source, old, "`first_name` varchar(80)", 1)
+}
+
+// repartitionSalaries replaces the salaries table partition clause with a coarser 3-partition
+// RANGE COLUMNS scheme. It anchors on the canonical PARTITION BY block the dumper emits.
+func repartitionSalaries(t *testing.T, source string) string {
+ t.Helper()
+ header := "CREATE TABLE `salaries`"
+ start := strings.Index(source, header)
+ require.GreaterOrEqual(t, start, 0, "salaries table not found in source")
+ // Find the PARTITION BY clause within the salaries statement.
+ rest := source[start:]
+ semi := strings.Index(rest, ";")
+ require.GreaterOrEqual(t, semi, 0, "salaries statement has no terminating semicolon")
+ stmt := rest[:semi]
+ pIdx := strings.Index(strings.ToUpper(stmt), "PARTITION BY")
+ require.GreaterOrEqual(t, pIdx, 0, "salaries has no PARTITION BY clause to change:\n%s", stmt)
+ // The dumper wraps partitioning in a /*!NNNNN PARTITION BY ... */ executable comment. Replace
+ // the whole partition portion INCLUDING the wrapper opener, else we leave an unterminated
+ // /*! comment (and produce invalid SQL).
+ replStart := pIdx
+ if c := strings.LastIndex(stmt[:pIdx], "/*!"); c >= 0 {
+ replStart = c
+ }
+ // Build a coarser partition scheme covering the same range column.
+ newPart := "PARTITION BY RANGE COLUMNS(`from_date`)\n" +
+ "(PARTITION p_old VALUES LESS THAN ('1990-01-01'),\n" +
+ " PARTITION p_mid VALUES LESS THAN ('2000-01-01'),\n" +
+ " PARTITION p_max VALUES LESS THAN (MAXVALUE))"
+ newStmt := stmt[:replStart] + newPart
+ return source[:start] + newStmt + source[start+semi:]
+}
+
+// uniqueDBName mirrors newLiveDatabase's naming for ad hoc probes (kept for symmetry/debug).
+//
+//nolint:unused
+func uniqueDBName(prefix string) string {
+ return fmt.Sprintf("%s_%s", prefix, strings.ReplaceAll(uuid.New().String(), "-", "_"))
+}
+
+// Stress smoke test for the MySQL declarative (SDL) migration path, driving the
+// PRODUCTION entry points end to end against live MySQL 5.7 + 8.0:
+//
+// - schema.SDLMigration(MYSQL, userSDL, syncedMetadata, engineVersion) — the exact
+// call database_migrate_executor.diff() makes,
+// - mysqlDiffSDLMigration(source, target, engineVersion) — version-aware diff,
+// - schema.SDLDropAdvices(MYSQL, userSDL, syncedMetadata, engineVersion) — drop advices.
+//
+// The per-type basics live in the live round-trip axes above. These cases push harder:
+// a large combined schema with many interacting objects, deliberately non-canonical user
+// forms (where idempotence is won or lost), a multi-change release that must order DDL
+// correctly, a drop-heavy release with advices, and explicit 5.7-vs-8.0 divergence guards.
+//
+// Shared helpers (createLiveMySQLDriver, newLiveDatabase, applyAndDump, statementCount,
+// liveServers, liveServer, liveOraclePassword) are declared with the live round-trip axes
+// (same package).
+
+// syncMetadata applies ddl to a fresh database on srv, syncs it, and returns the
+// model.DatabaseMetadata (the value the production release path feeds to
+// schema.SDLMigration) together with the database name so the caller can apply DDL back.
+func syncMetadata(ctx context.Context, t *testing.T, srv liveServer, prefix, ddl string) (*model.DatabaseMetadata, string) {
+ t.Helper()
+ dbName := newLiveDatabase(ctx, t, srv, prefix)
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, ddl, db.ExecuteOptions{})
+ require.NoError(t, err, "[%s] apply setup DDL:\n%s", srv.name, ddl)
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true), dbName
+}
+
+// dumpSDL syncs dbName on srv and returns the canonical MetadataToSDL dump.
+func dumpSDL(ctx context.Context, t *testing.T, srv liveServer, dbName string) string {
+ t.Helper()
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
+ require.NoError(t, err)
+ return source
+}
+
+// applyDDL executes ddl against dbName on srv.
+func applyDDL(ctx context.Context, t *testing.T, srv liveServer, dbName, ddl string) error {
+ t.Helper()
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, ddl, db.ExecuteOptions{})
+ return err
+}
+
+// ============================================================================
+// Scenario 1: Large combined-schema idempotence (the headline test).
+// ============================================================================
+
+// bigSchemaCore is the shared body of the large combined schema: a single realistic
+// database with many interacting objects. It spans the normalization-sensitive constructs
+// plus PKs, composite/unique/prefix/fulltext indexes, foreign keys (circular, self-ref,
+// composite), CHECK constraints, RANGE + HASH partitions, views (view-on-view, joined),
+// stored functions + procedures, multiple triggers per table, and an event. The
+// CTE/derived-table view (v_project_load) diverges by version (5.7 has no CTE support) and
+// is appended by bigSchemaProjectLoadView.
+//
+// CHECK note: 8.0 honors CHECK; 5.7 parses-and-ignores it (a no-op there). The same body
+// therefore applies on both, and the synced metadata differs only by the absent checks on
+// 5.7 — which is exactly the per-version idempotence each side must satisfy.
+//
+// FK note: department<->employee is circular, so the FK that closes the cycle
+// (department.manager_id -> employee) is added with a trailing ALTER TABLE after both
+// tables exist (the SDL loader disables foreign_key_checks, but the live setup apply does
+// not).
+//
+//go:embed testdata/sdl/big_schema_core.sql
+var bigSchemaCore string
+
+// circularFKClose closes the department<->employee circular dependency after both
+// tables exist (the live setup apply keeps foreign_key_checks on, unlike the SDL loader).
+const circularFKClose = `
+ALTER TABLE department ADD CONSTRAINT fk_dept_manager FOREIGN KEY (manager_id) REFERENCES employee (id) ON DELETE SET NULL;
+`
+
+// bigSchemaProjectLoadView returns the version-correct v_project_load definition.
+//
+// - 8.0 uses a CTE (WITH ...) — exercising the CTE construct and confirming the dump
+// preserves it verbatim. (8.0 also stores derived-table inner refs unqualified, so the
+// CTE form round-trips cleanly.)
+// - 5.7 has no CTE support AND mis-handles derived tables in the SDL no-op path (see
+// TestSDLStressViewDerivedTable57 — 5.7 db-qualifies derived-table inner refs, which the
+// bbcatalog-wrapped omni diff cannot fold). So 5.7 uses a view-on-view form
+// (v_active_employees-style, with a real aggregate join) that is known-idempotent and
+// keeps the large-schema test focused on finding OTHER at-scale bugs.
+func bigSchemaProjectLoadView(version string) string {
+ if version == "5.7" {
+ return `
+CREATE VIEW v_proj_member_count AS
+SELECT project_id, COUNT(*) AS members, SUM(allocation) AS total_alloc
+FROM assignment GROUP BY project_id;
+
+CREATE VIEW v_project_load AS
+SELECT p.code, p.title, pmc.members, pmc.total_alloc
+FROM project p JOIN v_proj_member_count pmc ON p.id = pmc.project_id;
+`
+ }
+ return `
+CREATE VIEW v_project_load AS
+WITH per_proj AS (
+ SELECT project_id, COUNT(*) AS members, SUM(allocation) AS total_alloc
+ FROM assignment GROUP BY project_id
+)
+SELECT p.code, p.title, pp.members, pp.total_alloc
+FROM project p JOIN per_proj pp ON p.id = pp.project_id;
+`
+}
+
+// bigSchemaSetup is the full apply sequence for the live database: the shared core, the
+// version-correct CTE/derived view, then the closing circular FK (added last so both
+// referenced tables exist under foreign_key_checks=ON). CHECK constraints are valid on
+// 8.0; 5.7 silently parses-and-ignores CHECK, so the same body applies on both — the
+// synced metadata differs only by the absent checks on 5.7, which is exactly the
+// per-version idempotence each side must satisfy.
+func bigSchemaSetup(version string) string {
+ return bigSchemaCore + bigSchemaProjectLoadView(version) + circularFKClose
+}
+
+// bigSchemaUserSDL is what a user would author: the table bodies (circular FK inlined into
+// department) plus the version-correct project-load view. Declarative SDL has no ordering
+// constraint (the omni loader disables FK checks during load). This is the single-document
+// target D the production path diffs against.
+func bigSchemaUserSDL(version string) string {
+ return bigSchemaUserSDLCore + bigSchemaProjectLoadView(version)
+}
+
+// bigSchemaUserSDLCore is the table/routine/trigger body the user authors, sans the
+// version-specific project-load view (appended by bigSchemaUserSDL).
+//
+//go:embed testdata/sdl/big_schema_user_sdl_core.sql
+var bigSchemaUserSDLCore string
+
+// TestSDLStressLargeSchemaIdempotence is the headline at-scale idempotence proof. One
+// realistic ~13-table schema with views, view-on-view, CTE view, functions, procedures,
+// multiple triggers, an event, RANGE + HASH partitions, circular/self-ref/composite FKs,
+// and CHECK constraints (8.0) is applied to a live database, synced, dumped via
+// MetadataToSDL, then diffed against the user-authored SDL through the PRODUCTION
+// version-aware path. The diff MUST be empty on both 5.7 and 8.0.
+//
+//nolint:tparallel
+func TestSDLStressLargeSchemaIdempotence(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_big", bigSchemaSetup(srv.version))
+ userSDL := bigSchemaUserSDL(srv.version)
+
+ // (a) Production-path no-op: schema.SDLMigration converts meta -> SDL and diffs
+ // against the user SDL, threading the version. MUST be empty.
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, meta, srv.version)
+ require.NoError(t, err)
+ if noop != "" {
+ t.Logf("[%s] NON-EMPTY no-op diff (%d statements):\n%s", srv.name, statementCount(noop), noop)
+ }
+ require.Empty(t, noop, "[%s] large-schema production-path no-op must be empty, got:\n%s", srv.name, noop)
+
+ // (b) source-vs-source determinism: dump and diff against itself.
+ source := dumpSDL(ctx, t, srv, dbName)
+ selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, selfDiff, "[%s] large-schema source-vs-source must be empty, got:\n%s", srv.name, selfDiff)
+
+ // (c) source-vs-user-SDL via the version-aware diff (the same property as (a) but
+ // through the lower-level entry the breadth tests use).
+ noop2, err := mysqlDiffSDLMigration(source, userSDL, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noop2, "[%s] large-schema source-vs-D diff must be empty, got:\n%s", srv.name, noop2)
+ })
+ }
+}
+
+// TestSDLStressViewDerivedTable57 PINS a real (B) wiring bug found by the stress test:
+// on MySQL 5.7 a view whose body contains a derived table (a subquery in the FROM clause)
+// is NOT idempotent through the SDL no-op path — it emits a spurious CREATE OR REPLACE
+// VIEW.
+//
+// Root cause: MySQL 5.7's view canonicalizer fully-qualifies the table references INSIDE a
+// FROM-clause derived table with the schema name (`<synced_db>`.`assignment`), whereas 8.0
+// stores them unqualified. MetadataToSDL emits that body verbatim. The omni SDL diff loads
+// both inputs under the synthetic database `bbcatalog`, and its canonicalViewBody only
+// folds away the view's OWN-database prefix — which is now `bbcatalog`, not the real synced
+// DB name embedded in the body. So the `from` body keeps `<synced_db>`.`assignment` while
+// the `to` (user) body resolves to `bbcatalog`.`assignment` and is folded to unqualified;
+// the two bodies differ and the diff emits a no-op CREATE OR REPLACE VIEW.
+//
+// Blast radius (mapped live): 5.7 ONLY, and ONLY FROM-clause derived tables. Plain joins,
+// view-on-view, scalar subqueries (SELECT list), and IN-subqueries (WHERE) are all
+// idempotent on 5.7; 8.0 is fully idempotent including derived tables and CTEs.
+//
+// Likely fix locus: bytebase MetadataToSDL / get_database_definition.go should emit view
+// bodies with the synced own-database qualifier stripped (db-neutral) before the omni
+// loader sees them; or the omni SDL wrapping should rewrite the synced DB name to the
+// bbcatalog context. The omni canonicalViewBody itself is correct — it just can't fold a
+// qualifier naming a database other than the view's loaded own-database.
+//
+// This test is SKIPPED so the suite stays green; remove the Skip to reproduce the failure.
+//
+//nolint:tparallel
+func TestSDLStressViewDerivedTable57(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+ srv := liveServer{name: "mysql57", host: "127.0.0.1", port: "13307", version: "5.7"}
+
+ ddl := `CREATE TABLE project (id INT PRIMARY KEY, code VARCHAR(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE assignment (employee_id INT, project_id INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE VIEW vx AS
+SELECT p.code, d.c
+FROM project p JOIN (SELECT project_id, COUNT(*) c FROM assignment GROUP BY project_id) d
+ON p.id = d.project_id;`
+
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_dtv", ddl)
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, ddl, meta, srv.version)
+ require.NoError(t, err)
+ source := dumpSDL(ctx, t, srv, dbName)
+ t.Logf("dumped source:\n%s", source)
+ require.Empty(t, noop, "5.7 derived-table view no-op must be empty, got:\n%s", noop)
+}
+
+// ============================================================================
+// Scenario 2: Normalization-heavy user forms (the real idempotence challenge).
+// ============================================================================
+
+// normCase authors a table in a deliberately non-canonical form. The schema is applied
+// to a live DB, synced, dumped, and the production version-aware diff of (dumped vs the
+// user form) MUST be empty — the engine must canonicalize the user form to the stored
+// form on each version.
+type normCase struct {
+ name string
+ ddl string
+}
+
+// normCases each name exactly one normalization the engine must absorb. They are NOT
+// merged into one table so a single failure pinpoints the offending construct.
+func normCases() []normCase {
+ return []normCase{
+ {
+ name: "int_widths",
+ // 8.0 drops widths (int/bigint/tinyint); 5.7 keeps int(11)/bigint(20)/tinyint(4).
+ ddl: `CREATE TABLE t (
+ a INT(11) NOT NULL,
+ b BIGINT(20) NOT NULL DEFAULT 0,
+ c SMALLINT(6) NOT NULL DEFAULT 0,
+ d TINYINT(4) NOT NULL DEFAULT 0,
+ e MEDIUMINT(9) NOT NULL DEFAULT 0,
+ PRIMARY KEY (a)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "bool_boolean",
+ // BOOL / BOOLEAN both store tinyint(1); TRUE/FALSE -> 1/0.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ f1 BOOL NOT NULL DEFAULT FALSE,
+ f2 BOOLEAN NOT NULL DEFAULT TRUE,
+ f3 BOOL NOT NULL DEFAULT 0,
+ f4 BOOLEAN NOT NULL DEFAULT 1
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "bare_charset_utf8mb4",
+ // No COLLATE: resolves to the version default (utf8mb4_general_ci on 5.7,
+ // utf8mb4_0900_ai_ci on 8.0) — the headline 5.7-vs-8.0 collation case.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a VARCHAR(50) CHARSET utf8mb4,
+ b VARCHAR(50)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "utf8_to_utf8mb3",
+ // utf8 -> utf8mb3 on 8.0; stays utf8 on 5.7.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a VARCHAR(50) CHARSET utf8,
+ b TEXT CHARACTER SET utf8
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "int_defaults",
+ // DEFAULT 0 on int and DEFAULT '0' both store '0'.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a INT NOT NULL DEFAULT 0,
+ b INT NOT NULL DEFAULT '0',
+ c DECIMAL(10,2) NOT NULL DEFAULT 0,
+ d VARCHAR(10) NOT NULL DEFAULT '0'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "unnamed_index_and_fk",
+ // Unnamed index + unnamed FK: the engine auto-names both. Idempotence requires
+ // the dump's auto-name to canonicalize against the user's unnamed form.
+ ddl: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE t (
+ id INT PRIMARY KEY,
+ pid INT NOT NULL,
+ name VARCHAR(50) NOT NULL,
+ INDEX (name),
+ FOREIGN KEY (pid) REFERENCES parent (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "restrict_fk_actions",
+ // RESTRICT == no clause. On 5.7 the dump renders ON UPDATE RESTRICT explicitly;
+ // on 8.0 it is omitted. Either way the user RESTRICT form must canonicalize equal.
+ ddl: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE t (
+ id INT PRIMARY KEY,
+ pid INT NOT NULL,
+ CONSTRAINT fk_t_parent FOREIGN KEY (pid) REFERENCES parent (id) ON DELETE RESTRICT ON UPDATE RESTRICT
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "using_hash_on_innodb",
+ // USING HASH is dropped on InnoDB (B-tree only).
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a INT NOT NULL,
+ KEY idx_a (a) USING HASH
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "generated_col_spacing",
+ // Odd spacing/casing in the generated expression must canonicalize to the stored form.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ price DECIMAL(10,2) NOT NULL DEFAULT 0,
+ qty INT NOT NULL DEFAULT 0,
+ total DECIMAL(20,2) AS ( price * qty ) STORED,
+ label VARCHAR(20) AS (CONCAT('x', id)) VIRTUAL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ // NOTE: ROW_FORMAT=DYNAMIC (and other table CREATE_OPTIONS) are NOT idempotent — see
+ // TestSDLStressTableCreateOptions, a documented (B) bug. Pulled out of the passing set.
+ {
+ name: "enum_set",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ color ENUM('red','green','blue') NOT NULL DEFAULT 'red',
+ flags SET('a','b','c') NOT NULL DEFAULT 'a,b'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "timestamp_defaults",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "auto_increment_seed",
+ // AUTO_INCREMENT seed in the table option: the dump may carry AUTO_INCREMENT=N;
+ // idempotence requires it not to phantom-diff against the user form without it.
+ ddl: `CREATE TABLE t (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(50) NOT NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8mb4;`,
+ },
+ }
+}
+
+// TestSDLStressNormalization is where idempotence is won or lost. Each non-canonical user
+// form is applied live, synced, dumped, and the PRODUCTION version-aware diff (dumped vs
+// the user form) MUST be empty on both 5.7 and 8.0. Run as the production entry
+// schema.SDLMigration AND the version-aware mysqlDiffSDLMigration.
+//
+//nolint:tparallel
+func TestSDLStressNormalization(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, nc := range normCases() {
+ nc := nc
+ t.Run(nc.name, func(t *testing.T) {
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_norm", nc.ddl)
+
+ // Production entry: schema.SDLMigration (meta -> SDL internally).
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, nc.ddl, meta, srv.version)
+ require.NoError(t, err)
+ if noop != "" {
+ source := dumpSDL(ctx, t, srv, dbName)
+ t.Logf("[%s/%s] stored/dumped SDL:\n%s", srv.name, nc.name, source)
+ t.Logf("[%s/%s] user form:\n%s", srv.name, nc.name, nc.ddl)
+ t.Logf("[%s/%s] NON-EMPTY no-op diff:\n%s", srv.name, nc.name, noop)
+ }
+ require.Empty(t, noop, "[%s/%s] normalization no-op (SDLMigration) must be empty, got:\n%s", srv.name, nc.name, noop)
+
+ // Version-aware lower-level entry, same property.
+ source := dumpSDL(ctx, t, srv, dbName)
+ noop2, err := mysqlDiffSDLMigration(source, nc.ddl, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noop2, "[%s/%s] normalization no-op (mysqlDiffSDLMigration) must be empty, got:\n%s", srv.name, nc.name, noop2)
+ })
+ }
+ })
+ }
+}
+
+// TestSDLStressTableCreateOptions PINS a second real (B) wiring bug found by the stress
+// test: a table authored with a CREATE_OPTION such as ROW_FORMAT=DYNAMIC is NOT idempotent
+// through the SDL no-op path on EITHER 5.7 or 8.0 — it emits a spurious
+// `ALTER TABLE t ROW_FORMAT=DYNAMIC`.
+//
+// Root cause is purely in bytebase's SDL renderer, not omni: MySQL stores ROW_FORMAT in
+// CREATE_OPTIONS, and bytebase's MySQL sync DOES capture it into TableMetadata.CreateOptions
+// (backend/plugin/db/mysql/sync.go:561). But the SDL dumper
+// get_database_definition.go's table-option block (~L587-609) renders only ENGINE,
+// DEFAULT CHARSET, COLLATE, COMMENT, and partitions — it never emits table.CreateOptions.
+// So the dumped `from` SDL drops ROW_FORMAT while the user `to` SDL keeps it; the omni diff
+// (correctly) sees a table-option delta and emits the ALTER. This applies to any
+// create-option (ROW_FORMAT, KEY_BLOCK_SIZE, COMPRESSION, STATS_PERSISTENT, ...).
+//
+// Likely fix locus: bytebase get_database_definition.go — emit table.CreateOptions in the
+// SDL table-option block (filtering the synthetic 'partitioned' token, which is surfaced
+// via Partitions, not as a literal option). The omni differ already round-trips ROW_FORMAT
+// when it is present on both sides.
+//
+// SKIPPED so the suite stays green; remove the Skip to reproduce.
+//
+//nolint:tparallel
+func TestSDLStressTableCreateOptions(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ ddl := `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a VARCHAR(100) NOT NULL
+) ENGINE=InnoDB ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4;`
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_rf", ddl)
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, ddl, meta, srv.version)
+ require.NoError(t, err)
+ source := dumpSDL(ctx, t, srv, dbName)
+ t.Logf("[%s] dumped source (ROW_FORMAT dropped):\n%s", srv.name, source)
+ require.Empty(t, noop, "[%s] ROW_FORMAT no-op must be empty, got:\n%s", srv.name, noop)
+ })
+ }
+}
+
+// ============================================================================
+// Scenario 3: Multi-change release (combined DDL + ordering).
+// ============================================================================
+
+// multiChangeBase is the baseline schema for the multi-change release.
+//
+//go:embed testdata/sdl/multi_change_base.sql
+var multiChangeBase string
+
+// multiChangeTarget applies MANY simultaneous changes in ONE diff:
+// - add table `review` referencing product via FK,
+// - drop the legacy_code column (which has index idx_cat_legacy) from category,
+// - modify product.price type DECIMAL(10,2) -> DECIMAL(12,4),
+// - add an index on product(name),
+// - drop table legacy_audit (an FK CHILD of product — its own FK must drop first),
+// - add a CHECK on product (8.0),
+// - replace view v_catalog,
+// - add a trigger on product,
+// - change daily_stat partitioning (HASH 2 -> HASH 4 partitions).
+//
+// The 8.0 variant includes the CHECK; multiChangeTarget57 omits it.
+//
+//go:embed testdata/sdl/multi_change_target_80.sql
+var multiChangeTarget80 string
+
+// multiChangeTarget57 is multiChangeTarget80 without the CHECK constraint (5.7 ignores CHECK).
+//
+//go:embed testdata/sdl/multi_change_target_57.sql
+var multiChangeTarget57 string
+
+// indexOf returns the byte index of the first occurrence of substr in s, or -1. Used to
+// assert relative ordering between two statements in a generated plan.
+func indexOf(s, substr string) int {
+ return strings.Index(s, substr)
+}
+
+// TestSDLStressMultiChange applies many simultaneous changes in ONE diff and asserts the
+// plan is correct, minimal-ish, correctly ordered, and converges when applied back. Both
+// versions.
+//
+//nolint:tparallel
+func TestSDLStressMultiChange(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ target := multiChangeTarget80
+ if srv.version == "5.7" {
+ target = multiChangeTarget57
+ }
+
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_multi", multiChangeBase)
+
+ // Production path: compute the combined plan.
+ plan, err := schema.SDLMigration(storepb.Engine_MYSQL, target, meta, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, plan, "[%s] multi-change must produce DDL", srv.name)
+ t.Logf("[%s] multi-change plan:\n%s", srv.name, plan)
+ upper := strings.ToUpper(plan)
+
+ // Correctness: every intended change is present.
+ require.Contains(t, upper, "CREATE TABLE `REVIEW`", "[%s] expected new review table:\n%s", srv.name, plan)
+ require.Contains(t, upper, "DROP COLUMN `LEGACY_CODE`", "[%s] expected drop of legacy_code:\n%s", srv.name, plan)
+ require.True(t, strings.Contains(upper, "DECIMAL(12,4)"), "[%s] expected price type change:\n%s", srv.name, plan)
+ require.Contains(t, upper, "IDX_PROD_NAME", "[%s] expected new index on product(name):\n%s", srv.name, plan)
+ require.Contains(t, upper, "DROP TABLE `LEGACY_AUDIT`", "[%s] expected drop of legacy_audit:\n%s", srv.name, plan)
+ require.Contains(t, upper, "TRG_PROD_INS", "[%s] expected new trigger:\n%s", srv.name, plan)
+ require.Contains(t, upper, "V_CATALOG", "[%s] expected view replace:\n%s", srv.name, plan)
+ require.Contains(t, upper, "PARTITION BY HASH", "[%s] expected daily_stat partition change:\n%s", srv.name, plan)
+ if srv.version == "8.0" {
+ require.Contains(t, upper, "CHK_PRICE", "[%s] expected CHECK add:\n%s", srv.name, plan)
+ }
+
+ // --- Ordering correctness ---
+ dropLegacyFK := indexOf(upper, "DROP FOREIGN KEY `FK_LEGACY_AUDIT_PROD`")
+ dropLegacyTable := indexOf(upper, "DROP TABLE `LEGACY_AUDIT`")
+ require.GreaterOrEqual(t, dropLegacyFK, 0, "[%s] expected legacy_audit FK drop:\n%s", srv.name, plan)
+ require.GreaterOrEqual(t, dropLegacyTable, 0, "[%s] expected legacy_audit table drop:\n%s", srv.name, plan)
+ require.Less(t, dropLegacyFK, dropLegacyTable,
+ "[%s] FK drop must precede table drop:\n%s", srv.name, plan)
+
+ // Index drop must precede the column drop on category (dropping legacy_code).
+ dropCatIndex := indexOf(upper, "DROP INDEX `IDX_CAT_LEGACY`")
+ dropCatColumn := indexOf(upper, "DROP COLUMN `LEGACY_CODE`")
+ require.GreaterOrEqual(t, dropCatIndex, 0, "[%s] expected category index drop:\n%s", srv.name, plan)
+ require.Less(t, dropCatIndex, dropCatColumn,
+ "[%s] index drop must precede column drop:\n%s", srv.name, plan)
+
+ // The review FK is deferred to PhasePost: a standalone ADD CONSTRAINT FK_REVIEW_PROD
+ // must appear AFTER the review table is created (and not be inlined in the CREATE).
+ createReview := indexOf(upper, "CREATE TABLE `REVIEW`")
+ require.GreaterOrEqual(t, createReview, 0, "[%s] review table create not found:\n%s", srv.name, plan)
+ addReviewFK := indexOf(upper, "ADD CONSTRAINT `FK_REVIEW_PROD`")
+ require.GreaterOrEqual(t, addReviewFK, 0, "[%s] expected deferred review FK add:\n%s", srv.name, plan)
+ require.Less(t, createReview, addReviewFK,
+ "[%s] review FK add must follow the review table create (PhasePost):\n%s", srv.name, plan)
+
+ // Apply the WHOLE plan back to the real DB and confirm convergence (re-diff empty).
+ applyErr := applyDDL(ctx, t, srv, dbName, plan)
+ require.NoError(t, applyErr, "[%s] multi-change plan failed to apply:\n%s", srv.name, plan)
+
+ newSource := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(newSource, target, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "[%s] multi-change did not converge, residual:\n%s", srv.name, converge)
+ })
+ }
+}
+
+// ============================================================================
+// Scenario 4: Drop-heavy release + advices.
+// ============================================================================
+
+// dropHeavyBase has tables, indexes, views, and routines to drop.
+//
+//go:embed testdata/sdl/drop_heavy_base.sql
+var dropHeavyBase string
+
+// dropHeavyTarget drops scratch_table (whole table), a.scratch column (+ its index),
+// a.idx_a_name index, the v_a view, f_double function, and p_reset procedure. Table b
+// loses its FK target only if a is dropped — here a survives, b survives.
+//
+//go:embed testdata/sdl/drop_heavy_target.sql
+var dropHeavyTarget string
+
+// TestSDLStressDropHeavy asserts SDLDropAdvices emits WARNING advices for each destructive
+// op, and that the generated destructive DDL applies and converges. Both versions.
+//
+//nolint:tparallel
+func TestSDLStressDropHeavy(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_drop", dropHeavyBase)
+
+ // Drop advices: the table/column/index drops ARE warned. (The view/function/
+ // procedure drops are MISSING — a confirmed (B) bug pinned by
+ // TestSDLStressDropAdvicesViewRoutineGap.)
+ advices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, dropHeavyTarget, meta, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, advices, "[%s] drop-heavy target must yield advices", srv.name)
+ for _, a := range advices {
+ require.Equal(t, storepb.Advice_WARNING, a.Status, "[%s] drop advice must be WARNING: %+v", srv.name, a)
+ }
+ joined := ""
+ for _, a := range advices {
+ joined += a.Content + "\n"
+ }
+ t.Logf("[%s] drop advices:\n%s", srv.name, joined)
+
+ // The table/column/index drops are warned (>=3: scratch_table, scratch column,
+ // two indexes). The dropped table must be named.
+ dropCount := countAdviceCode(advices, code.SDLDropOperation.Int32())
+ require.GreaterOrEqual(t, dropCount, 3,
+ "[%s] expected >=3 drop warnings (table, column, index), got %d:\n%s", srv.name, dropCount, joined)
+ require.Contains(t, joined, "scratch_table", "[%s] expected dropped table named:\n%s", srv.name, joined)
+ require.Contains(t, joined, "scratch", "[%s] expected dropped column named:\n%s", srv.name, joined)
+
+ // Generate and apply the destructive DDL; confirm convergence. The PLAN correctly
+ // drops the view/function/procedure even though the advices omit them.
+ plan, err := schema.SDLMigration(storepb.Engine_MYSQL, dropHeavyTarget, meta, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, plan, "[%s] drop-heavy plan must be non-empty", srv.name)
+ t.Logf("[%s] drop-heavy plan:\n%s", srv.name, plan)
+
+ applyErr := applyDDL(ctx, t, srv, dbName, plan)
+ require.NoError(t, applyErr, "[%s] drop-heavy plan failed to apply:\n%s", srv.name, plan)
+
+ newSource := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(newSource, dropHeavyTarget, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "[%s] drop-heavy did not converge, residual:\n%s", srv.name, converge)
+ })
+ }
+}
+
+// TestSDLStressDropAdvicesViewRoutineGap PINS a third real (B) bug found by the stress
+// test: mysqlSDLDropAdvices (backend/plugin/schema/mysql/sdl_migration_omni.go) emits NO
+// advice at all for any view / function / procedure / trigger / event operation — neither a
+// DROP advice for a standalone drop nor a REPLACE advice for a redefinition. So a
+// declarative release that drops or replaces a view/routine/trigger/event gives the user
+// ZERO destructive-operation warning. Affects both 5.7 and 8.0; the generated migration DDL
+// itself is correct (it does drop/replace) — only the advice walker is wrong.
+//
+// Two compounding faults in the replace-pair detection (sdl_migration_omni.go L205-294):
+//
+// 1. The premise is wrong for this omni build. The code assumes a redefinition is rendered
+// as an OpDrop<Obj> followed by an OpCreate<Obj> of the same name, and classifies the
+// CREATE as a replace. But omni emits a redefinition as a SINGLE OpCreate<Obj> op (no
+// paired drop — verified: a view redefine yields exactly `CreateView v`). So isReplace is
+// never set for the create, and the OpCreateView/Function/... replace branch never fires
+// -> no REPLACE advice.
+//
+// 2. markDropped self-poisons the standalone-drop path. For every OpDropView it does
+// markDropped(OpCreateView, name); isReplace(OpCreateView, name) then reads that SAME map
+// and returns true for the drop itself. So the OpDropView branch's `if !isReplace(...)`
+// is false -> the DROP advice is SUPPRESSED even though there is no matching CREATE.
+//
+// Net: every view/routine/trigger/event drop is silently swallowed, and every redefinition
+// is unwarned. The table/column/index/constraint/FK/check advices are unaffected (they don't
+// go through the replace-pair logic).
+//
+// Likely fix: build the dropped/created name sets in a FIRST pass over plan.Ops, classify a
+// name as "replace" only when BOTH a drop AND a create of that name exist, and emit a REPLACE
+// advice from whichever op is present (omni's lone CreateView for a redefine), a DROP advice
+// for a drop with no matching create, and suppress the drop half only of a genuine pair.
+//
+// SKIPPED so the suite stays green; remove the Skip to reproduce.
+//
+//nolint:tparallel
+func TestSDLStressDropAdvicesViewRoutineGap(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ base := `CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE VIEW v AS SELECT id, a FROM t;
+CREATE FUNCTION f(x INT) RETURNS INT DETERMINISTIC RETURN x*2;`
+ meta, _ := syncMetadata(ctx, t, srv, "sdl_stress_advgap", base)
+
+ // Drop the view + function (table t survives).
+ dropTarget := `CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`
+
+ // The migration DDL DOES drop them (proving the diff is correct; only advices fail).
+ plan, err := schema.SDLMigration(storepb.Engine_MYSQL, dropTarget, meta, srv.version)
+ require.NoError(t, err)
+ require.Contains(t, strings.ToUpper(plan), "DROP VIEW", "[%s] plan should drop the view:\n%s", srv.name, plan)
+ require.Contains(t, strings.ToUpper(plan), "DROP FUNCTION", "[%s] plan should drop the function:\n%s", srv.name, plan)
+
+ // But the advices are EMPTY (the bug).
+ advices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, dropTarget, meta, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, advices, "[%s] BUG: dropping a view + function must yield drop advices, got none", srv.name)
+ })
+ }
+}
+
+// ============================================================================
+// Scenario 5: 5.7-vs-8.0 divergence guards.
+// ============================================================================
+
+// divergeSchema authors constructs whose stored form diverges by version: bare utf8mb4
+// (collation), utf8 (mb3 on 8.0), int widths, and a CHECK (8.0 only).
+//
+//go:embed testdata/sdl/diverge_schema.sql
+var divergeSchema string
+
+// divergeSchemaWithCheck adds a CHECK (8.0 honors it; 5.7 parses-and-ignores).
+//
+//go:embed testdata/sdl/diverge_schema_with_check.sql
+var divergeSchemaWithCheck string
+
+// TestSDLStressVersionDivergence confirms each version is idempotent against its OWN
+// stored form with no cross-version contamination: 5.7 never emits utf8mb4_0900_ai_ci;
+// 8.0 handles utf8mb3; CHECK is present on 8.0, absent on 5.7. It dumps the synced schema
+// and asserts version-specific properties on the canonical SDL, then proves no-op
+// idempotence per version.
+//
+//nolint:tparallel
+func TestSDLStressVersionDivergence(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ userSDL := divergeSchema
+ if srv.version == "8.0" {
+ userSDL = divergeSchemaWithCheck
+ }
+
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_diverge", userSDL)
+ source := dumpSDL(ctx, t, srv, dbName)
+ t.Logf("[%s] dumped SDL:\n%s", srv.name, source)
+
+ // Version-specific stored-form guards on the canonical dump. Note MetadataToSDL
+ // renders collations from the synced metadata (server-accurate, hence divergent)
+ // but integer display widths in the 8.0-canonical form (widths dropped) on BOTH
+ // versions — the int-width divergence is enforced by the omni diff normalizer, not
+ // the dumper, so it is asserted via the no-op idempotence below rather than the dump
+ // string. CHECK is honored on 8.0 and parsed-and-ignored on 5.7, so it appears in
+ // the 8.0 dump only.
+ if srv.version == "5.7" {
+ require.NotContains(t, source, "utf8mb4_0900_ai_ci",
+ "[%s] 5.7 dump must not contain the 8.0-only collation:\n%s", srv.name, source)
+ require.NotContains(t, source, "utf8mb3",
+ "[%s] 5.7 dump must not normalize utf8 -> utf8mb3:\n%s", srv.name, source)
+ require.Contains(t, source, "utf8mb4_general_ci",
+ "[%s] 5.7 dump must use the 5.7 default collation:\n%s", srv.name, source)
+ require.NotContains(t, source, "chk_amount",
+ "[%s] 5.7 ignores CHECK, so it must be absent from the dump:\n%s", srv.name, source)
+ } else {
+ require.Contains(t, source, "utf8mb4_0900_ai_ci",
+ "[%s] 8.0 dump must use the 8.0 default collation:\n%s", srv.name, source)
+ require.Contains(t, source, "utf8mb3",
+ "[%s] 8.0 dump must normalize utf8 -> utf8mb3:\n%s", srv.name, source)
+ require.Contains(t, source, "chk_amount",
+ "[%s] 8.0 honors CHECK, so it must be present in the dump:\n%s", srv.name, source)
+ }
+
+ // No-op idempotence per version (production path).
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, meta, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noop, "[%s] divergence no-op must be empty, got:\n%s", srv.name, noop)
+
+ // A 5.7 CHANGE must never name the 8.0-only collation. Add a column to force a
+ // minimal ALTER and apply it back.
+ changeSDL := strings.Replace(userSDL,
+ "\tPRIMARY KEY (id)",
+ "\textra VARCHAR(30) NULL,\n\tPRIMARY KEY (id)", 1)
+ require.NotEqual(t, userSDL, changeSDL, "[%s] test setup: change SDL must differ", srv.name)
+ change, err := schema.SDLMigration(storepb.Engine_MYSQL, changeSDL, meta, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, change, "[%s] divergence change must be non-empty", srv.name)
+ if srv.version == "5.7" {
+ require.NotContains(t, change, "utf8mb4_0900_ai_ci",
+ "[%s] 5.7 change names an 8.0-only collation (errno 1273):\n%s", srv.name, change)
+ }
+ applyErr := applyDDL(ctx, t, srv, dbName, change)
+ require.NoError(t, applyErr, "[%s] divergence change failed to apply:\n%s", srv.name, change)
+
+ // Converge.
+ newSource := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(newSource, changeSDL, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "[%s] divergence change did not converge, residual:\n%s", srv.name, converge)
+ })
+ }
+}
+
+// Deep (round-2) stress test for the MySQL declarative (SDL) migration path. Round 1
+// (the stress axes above) covered breadth/scale/normalization. These cases push four
+// DIFFERENT axes, all adversarial — the goal is to FIND bugs, not to confirm green:
+//
+// - Axis 1: malformed / semantic-error TARGET SDL fed to mysqlDiffSDLMigration and
+// schema.SDLMigration. Must fail GRACEFULLY (clean error or defined no-op) — never panic,
+// never silently emit a wrong/destructive plan.
+// - Axis 2: exotic types + extreme values. sync -> MetadataToSDL -> diff vs user form must
+// be empty (idempotent) on both versions; every non-empty no-op is a normalization gap.
+// - Axis 3: sequential release chains S0->S1->...->S4 on a real DB; each step minimal,
+// applied, re-synced, converged before the next.
+// - Axis 4: ~80-100 table scale; no-op must be empty + fast; one small change must be one
+// minimal ALTER, not a re-emit of all tables.
+//
+// Drives the PRODUCTION entry points only:
+// - schema.SDLMigration(MYSQL, userSDL, syncedMetadata, version),
+// - mysqlDiffSDLMigration(source, target, version),
+// - schema.SDLDropAdvices(MYSQL, userSDL, syncedMetadata, version),
+// - schema.MetadataToSDL.
+//
+// Shared helpers (createLiveMySQLDriver, newLiveDatabase, applyAndDump, statementCount,
+// liveServers, liveServer, syncMetadata, dumpSDL, applyDDL) come from the other two live
+// test files in this package.
+
+// ============================================================================
+// Axis 1: adversarial / malformed / semantic-error TARGET SDL.
+// ============================================================================
+//
+// Robustness contract: feeding bad target SDL into the diff must produce a CLEAN Go error
+// (or a defined no-op) — NEVER a panic, NEVER a malformed/destructive plan. The diff path
+// returns errors as Go values; a panic is therefore a (B) bug regardless of message.
+
+// diffOutcome captures how a single adversarial input was handled.
+type diffOutcome struct {
+ panicked bool
+ panicVal any
+ err error
+ plan string
+ planEmpty bool
+}
+
+// safeDiff runs mysqlDiffSDLMigration(source, target, version) under a panic
+// recover so a panic is reported as data instead of crashing the test binary. A baseline
+// non-empty `source` is supplied so that if the target parses as EMPTY, the resulting plan
+// would be a (destructive) full-drop — letting us detect "silently dropped everything".
+func safeDiff(source, target, version string) (out diffOutcome) {
+ defer func() {
+ if r := recover(); r != nil {
+ out.panicked = true
+ out.panicVal = r
+ }
+ }()
+ plan, err := mysqlDiffSDLMigration(source, target, version)
+ out.err = err
+ out.plan = plan
+ out.planEmpty = strings.TrimSpace(plan) == ""
+ return out
+}
+
+// safeSDLMigration runs the production schema.SDLMigration under a panic recover.
+func safeSDLMigration(target string, meta *model.DatabaseMetadata, version string) (out diffOutcome) {
+ defer func() {
+ if r := recover(); r != nil {
+ out.panicked = true
+ out.panicVal = r
+ }
+ }()
+ plan, err := schema.SDLMigration(storepb.Engine_MYSQL, target, meta, version)
+ out.err = err
+ out.plan = plan
+ out.planEmpty = strings.TrimSpace(plan) == ""
+ return out
+}
+
+// adversarialCase is one bad-input probe.
+//
+// The UNIVERSAL bar for every case is: no panic, and no error-free plan that would
+// DESTROY a valid pre-existing object that the (correctly-interpreted) target still
+// contains. Beyond that, cases differ in how strict we can be about rejection:
+//
+// - wantReject=true: the input is malformed enough that a robust loader SHOULD return a
+// non-nil error. We assert err != nil. (If a future omni accepts it, this flips to a
+// soft finding rather than a crash — see the test body.)
+// - wantReject=false: the input is degenerate-but-arguably-valid, or its rejection is the
+// server's job at apply time (e.g. over-length identifier, dangling FK). We only assert
+// no-panic + no-unexpected-destruction and LOG the outcome.
+type adversarialCase struct {
+ name string
+ target string
+ wantReject bool // require a non-nil error from the loader
+ note string
+}
+
+// adversarialCases enumerates malformed and semantically-broken target SDL. The `source`
+// is a fixed valid 2-table schema so any plan the engine produces against a broken target
+// is observable (and a full-table-drop plan is detectable).
+func adversarialCases() []adversarialCase {
+ return []adversarialCase{
+ // ---- Syntax errors (must be rejected) ----
+ {name: "truncated_create", target: "CREATE TABLE t (id INT", wantReject: true, note: "unterminated CREATE"},
+ {name: "unmatched_paren", target: "CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50) ENGINE=InnoDB;", wantReject: true, note: "missing close paren"},
+ {name: "garbage_tokens", target: "@@@ this is not sql ;;; %%%", wantReject: true, note: "pure garbage"},
+ {name: "keyword_salad", target: "CREATE CREATE TABLE TABLE t t (id id INT INT);", wantReject: true, note: "doubled keywords"},
+ {name: "missing_type", target: "CREATE TABLE t (id, name VARCHAR(50));", wantReject: true, note: "column with no type"},
+
+ // ---- Semantic errors ----
+ {
+ name: "fk_nonexistent_table",
+ target: "CREATE TABLE child (id INT PRIMARY KEY, pid INT, CONSTRAINT fk FOREIGN KEY (pid) REFERENCES no_such_parent (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false, // omni accepts a dangling FK at load; the server rejects on apply. No-panic + non-destructive is the bar.
+ note: "FK to a table that does not exist",
+ },
+ {
+ name: "fk_nonexistent_column",
+ target: "CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\nCREATE TABLE child (id INT PRIMARY KEY, pid INT, CONSTRAINT fk FOREIGN KEY (pid) REFERENCES parent (no_such_col)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false,
+ note: "FK references a column that does not exist on parent",
+ },
+ {
+ name: "duplicate_table",
+ target: "CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\nCREATE TABLE t (id INT PRIMARY KEY, x INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: true,
+ note: "two CREATE TABLE with the same name",
+ },
+ {
+ name: "duplicate_column",
+ target: "CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50), name VARCHAR(60)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: true,
+ note: "duplicate column name within a table",
+ },
+ {
+ name: "duplicate_index_name",
+ target: "CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT, KEY idx_dup (a), KEY idx_dup (b)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: true,
+ note: "two indexes with the same name",
+ },
+ {
+ name: "pk_declared_twice",
+ target: "CREATE TABLE t (id INT, x INT, PRIMARY KEY (id), PRIMARY KEY (x)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: true,
+ note: "two PRIMARY KEY clauses",
+ },
+ {
+ name: "index_nonexistent_column",
+ target: "CREATE TABLE t (id INT PRIMARY KEY, a INT, KEY idx_ghost (no_such_col)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false, // omni does NOT validate index-column existence at load; server rejects on apply (errno 1072). Soft finding.
+ note: "index on a column that does not exist",
+ },
+ {
+ name: "check_references_other_table",
+ target: "CREATE TABLE t (id INT PRIMARY KEY, a INT, CONSTRAINT ck CHECK (a < (SELECT COUNT(*) FROM other))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: true, // 8.0 rejects a subquery in CHECK at load
+ note: "CHECK with a subquery referencing another table",
+ },
+ {
+ name: "generated_col_forward_ref",
+ target: "CREATE TABLE t (id INT PRIMARY KEY, g INT AS (later + 1) STORED, later INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false, // forward reference; MySQL allows it. No-panic + sane plan is the bar.
+ note: "generated column referencing a column declared later",
+ },
+ {
+ name: "circular_fk_notnull",
+ target: "CREATE TABLE a (id INT PRIMARY KEY, b_id INT NOT NULL, CONSTRAINT fk_a FOREIGN KEY (b_id) REFERENCES b (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\nCREATE TABLE b (id INT PRIMARY KEY, a_id INT NOT NULL, CONSTRAINT fk_b FOREIGN KEY (a_id) REFERENCES a (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false, // SDL loader disables FK checks; a clean plan or error both acceptable, no panic is the bar
+ note: "mutually-referential NOT NULL FKs",
+ },
+
+ // ---- Degenerate ----
+ {name: "empty_string", target: "", wantReject: false, note: "empty SDL -> full drop of source; must not panic"},
+ {name: "whitespace_only", target: " \n\t \n", wantReject: false, note: "whitespace only"},
+ {name: "comments_only", target: "-- just a comment\n/* block comment */\n", wantReject: false, note: "comments only"},
+ {name: "single_semicolon", target: ";", wantReject: false, note: "a lone statement terminator"},
+ {name: "create_sequence", target: "CREATE SEQUENCE s START WITH 1 INCREMENT BY 1;", wantReject: true, note: "different-engine DDL (no SEQUENCE in MySQL)"},
+ {name: "dml_insert", target: "INSERT INTO t (id) VALUES (1);", wantReject: false, note: "DML in SDL target (LoadSQL fallback may accept and yield a plan)"},
+ {name: "dml_update", target: "UPDATE t SET id = 2;", wantReject: false, note: "UPDATE in SDL target"},
+ {name: "drop_table_in_target", target: "DROP TABLE t;", wantReject: true, note: "DROP in an SDL target (SDL is CREATE-only -> rejected)"},
+ {name: "alter_in_target", target: "ALTER TABLE t ADD COLUMN x INT;", wantReject: true, note: "ALTER in an SDL target (rejected)"},
+
+ // ---- Identifier edges ----
+ {
+ name: "ident_64_chars",
+ target: "CREATE TABLE `" + strings.Repeat("a", 64) + "` (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false, // 64 is the MAX legal length -> accepted, no panic
+ note: "64-char identifier (legal max)",
+ },
+ {
+ name: "ident_65_chars",
+ target: "CREATE TABLE `" + strings.Repeat("a", 65) + "` (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false, // omni does NOT enforce the 64-char limit at load; the server rejects on apply (errno 1059). Soft finding.
+ note: "65-char identifier (over the limit)",
+ },
+ {
+ name: "reserved_word_quoted",
+ target: "CREATE TABLE `select` (`from` INT PRIMARY KEY, `where` VARCHAR(50)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false,
+ note: "reserved words as backtick-quoted identifiers",
+ },
+ {
+ name: "unicode_emoji_ident",
+ target: "CREATE TABLE `ta*b📊le` (`col😀` INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false,
+ note: "unicode/emoji identifiers",
+ },
+ {
+ name: "ident_with_backtick_dot",
+ target: "CREATE TABLE `we``ird` (`a.b` INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: false,
+ note: "identifiers needing escaping (embedded backtick, dot)",
+ },
+ {
+ name: "case_only_differ",
+ target: "CREATE TABLE t (id INT PRIMARY KEY, Name VARCHAR(50), name VARCHAR(60)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ wantReject: true, // MySQL column names are case-insensitive -> Name/name collide
+ note: "columns differing only by case",
+ },
+ }
+}
+
+// TestSDLDeepAdversarial drives malformed/semantic-error target SDL through the production
+// diff. Bar: NEVER panic, NEVER silently emit a destructive plan. We feed a fixed valid
+// 2-table `source` so a wrong plan that drops everything is observable.
+//
+//nolint:tparallel
+func TestSDLDeepAdversarial(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ // A fixed valid source schema (two real tables) so the diff has something to operate on.
+ const sourceSDL = `CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+CREATE TABLE other (id INT PRIMARY KEY, t_id INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ // Materialize the source schema once so the production-path probe has a real synced
+ // metadata to diff against (schema.SDLMigration path).
+ meta, _ := syncMetadata(ctx, t, srv, "sdl_deep_adv", sourceSDL)
+
+ for _, ac := range adversarialCases() {
+ ac := ac
+ t.Run(ac.name, func(t *testing.T) {
+ // (1) Lower-level version-aware diff: source(valid) -> target(bad).
+ out := safeDiff(sourceSDL, ac.target, srv.version)
+
+ // UNIVERSAL bar 1: never panic. A panic on ANY input is a (B) bug.
+ require.False(t, out.panicked,
+ "[%s/%s] (B) PANIC on adversarial input %q: %v\ninput:\n%s",
+ srv.name, ac.name, ac.note, out.panicVal, ac.target)
+
+ // (2) Production entry: schema.SDLMigration(meta, badTarget). Same no-panic bar
+ // on the real production call (covers the MetadataToSDL->diff composition).
+ // Run for EVERY case, including the rejected ones.
+ pout := safeSDLMigration(ac.target, meta, srv.version)
+ require.False(t, pout.panicked,
+ "[%s/%s] (B) PANIC in schema.SDLMigration on %q: %v\ninput:\n%s",
+ srv.name, ac.name, ac.note, pout.panicVal, ac.target)
+
+ if ac.wantReject {
+ // The loader SHOULD reject this. If it does (err != nil), great. If it
+ // does NOT, that is a robustness gap, but it is only a (B) bug if the
+ // resulting error-free plan is also DESTRUCTIVE/wrong; a malformed input
+ // that yields a clean error OR a harmless no-op is acceptable. We assert
+ // the error and, on the rare accept, demand the plan at least not be
+ // silently emitted as a non-empty migration.
+ if out.err == nil {
+ require.True(t, out.planEmpty,
+ "[%s/%s] (B) malformed input %q was ACCEPTED (no error) and produced a non-empty plan — a wrong/garbage migration:\n%s",
+ srv.name, ac.name, ac.note, out.plan)
+ t.Logf("[%s/%s] SOFT: malformed input %q accepted as empty no-op rather than erroring", srv.name, ac.name, ac.note)
+ } else {
+ t.Logf("[%s/%s] reject OK: %v", srv.name, ac.name, out.err)
+ }
+ return
+ }
+
+ // wantReject==false: the input is degenerate-but-tolerable or its rejection
+ // is the server's job at apply time. No-panic already asserted. A clean error
+ // is fine; an error-free plan is fine (the declarative semantics of the target
+ // are well-defined). Just record the outcome for the report.
+ if out.err != nil {
+ t.Logf("[%s/%s] tolerated-with-error: %v", srv.name, ac.name, out.err)
+ } else {
+ t.Logf("[%s/%s] tolerated input %q -> plan(empty=%v):\n%s",
+ srv.name, ac.name, ac.note, out.planEmpty, out.plan)
+ }
+ })
+ }
+ })
+ }
+}
+
+// ============================================================================
+// Axis 2: exotic types + extreme values (idempotence; flag normalization gaps).
+// ============================================================================
+//
+// Each case authors ONE less-common type-surface table. It is applied to a live DB, synced,
+// dumped via MetadataToSDL, and the PRODUCTION version-aware diff of (dumped vs the user
+// form) MUST be empty on each version it runs on. A non-empty no-op is a normalization gap
+// (a (B) bug) and the test logs input + stored/dumped form so the owning omni Canonical* /
+// dumper rule can be located. 8.0-only constructs are gated off 5.7 via only80.
+
+// exoticCase authors one exotic-type table. only80 gates 8.0-only surface off 5.7.
+//
+// bGap marks a case that this round CONFIRMED is NOT idempotent — a (B) normalization /
+// dumper round-trip bug. Those cases are SKIPPED by the idempotence test (so the suite stays
+// green) but kept here as the executable repro, and are reproduced/explained by
+// TestSDLDeepExoticKnownGaps below. bGapReason is the one-line root cause.
+type exoticCase struct {
+ name string
+ ddl string
+ only80 bool
+ bGap bool
+ bGapReason string
+}
+
+// exoticCases enumerates the less-common type surface across families. Each names exactly
+// one construct cluster so a single non-empty no-op (or load error) pinpoints the offending
+// type. Cases marked bGap are confirmed (B) bugs this round found — see bGapReason and the
+// detailed analysis on TestSDLDeepExoticKnownGaps.
+func exoticCases() []exoticCase {
+ return []exoticCase{
+ // ---- JSON ----
+ {
+ name: "json_basic",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ doc JSON,
+ doc_nn JSON NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "json_default_80",
+ only80: true, // JSON column DEFAULT requires 8.0.13+
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ doc JSON DEFAULT (JSON_OBJECT()),
+ arr JSON DEFAULT (JSON_ARRAY())
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "json_multivalue_index_80",
+ only80: true, // multi-valued index (CAST ... AS ... ARRAY) is 8.0.17+
+ bGap: false, // FIXED (dumper strips the functional-index charset introducer + unescapes quotes; omni #353 accepts AS … ARRAY, re-pinned a362f7a7)
+ // FIXED (Bug B): the synced functional-index expression
+ // `(cast(json_extract(`tags`,_utf8mb4\'$.ids\') as unsigned array))` carried two things the
+ // omni loader rejects — the `_utf8mb4'…'` charset introducer before the JSON-path literal
+ // (`unexpected token`) and backslash-escaped single quotes (`syntax error at or near "\"`).
+ // normalizeFunctionalIndexExpr in get_database_definition.go now unescapes the quotes and
+ // strips the introducer, so the dumped expr is `((cast(json_extract(`tags`,'$.ids') as
+ // unsigned array)))`; omni's functional-index normalizer canonicalizes the dumped and user
+ // forms identically and the no-op is empty. (The `AS UNSIGNED ARRAY` cast itself is accepted
+ // by omni #353 — the introducer/escaping was the LoadSDL blocker, not the ARRAY keyword.)
+ bGapReason: "",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ tags JSON NOT NULL,
+ KEY idx_tags ((CAST(tags->'$.ids' AS UNSIGNED ARRAY)))
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+
+ // ---- Spatial ----
+ {
+ name: "spatial_types",
+ only80: true, // 8.0-only GAP (FIXED): on 5.7 the dumper emits `geometrycollection` and IS idempotent; 8.0 used to break.
+ // FIXED (bug 8): the dumper now normalizes the 8.0 `geomcollection` type synonym to
+ // the canonical `geometrycollection` spelling omni parses (normalizeColumnType in
+ // get_database_definition.go), so the dumped source reloads and the no-op is empty.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ g GEOMETRY,
+ pt POINT,
+ ls LINESTRING,
+ poly POLYGON,
+ gc GEOMETRYCOLLECTION
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "spatial_notnull_index",
+ // FIXED (bug 7): the dumper no longer emits a key-part prefix length for SPATIAL (and
+ // FULLTEXT) index parts (printIndexKeyPart suppressPrefix), so the spatial key dumps as
+ // `(`pt`)` matching SHOW CREATE and the no-op is empty on both 5.7 and 8.0.
+ ddl: `CREATE TABLE t (
+ id INT NOT NULL AUTO_INCREMENT,
+ pt POINT NOT NULL,
+ PRIMARY KEY (id),
+ SPATIAL KEY idx_pt (pt)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "spatial_srid_80",
+ only80: true, // SRID column attribute is 8.0
+ // FIXED (bug 6 + bug 7): sync now captures the column SRID (information_schema.COLUMNS.SRS_ID
+ // into ColumnMetadata.srid) and the dumper emits `/*!80003 SRID 4326 */` after NOT NULL,
+ // while the spatial-key prefix is suppressed (bug 7). Both halves of the former no-op are
+ // gone, so the dump round-trips empty.
+ ddl: `CREATE TABLE t (
+ id INT NOT NULL AUTO_INCREMENT,
+ pt POINT NOT NULL SRID 4326,
+ PRIMARY KEY (id),
+ SPATIAL KEY idx_pt (pt)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+
+ // ---- Fractional seconds ----
+ {
+ name: "fractional_seconds",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ dt6 DATETIME(6) NOT NULL,
+ tm3 TIME(3) NOT NULL,
+ ts6 TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
+ dt0 DATETIME(0)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+
+ // ---- BLOB / TEXT family ----
+ {
+ name: "blob_text_family",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ tb TINYBLOB,
+ b BLOB,
+ mb MEDIUMBLOB,
+ lb LONGBLOB,
+ tt TINYTEXT,
+ txt TEXT,
+ mt MEDIUMTEXT,
+ lt LONGTEXT
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "text_charset_collate",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
+ b LONGTEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci,
+ c BLOB
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "text_blob_prefix_index",
+ ddl: `CREATE TABLE t (
+ id INT NOT NULL AUTO_INCREMENT,
+ body TEXT,
+ data BLOB,
+ PRIMARY KEY (id),
+ KEY idx_body (body(50)),
+ KEY idx_data (data(30))
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+
+ // ---- Charsets beyond utf8mb4 ----
+ {
+ name: "charset_latin1_ascii",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a VARCHAR(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci,
+ b VARCHAR(50) CHARACTER SET ascii,
+ c CHAR(10) CHARACTER SET latin1
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "charset_binary_varbinary",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a BINARY(16),
+ b VARBINARY(255),
+ c VARCHAR(50) CHARACTER SET binary
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "charset_gbk_big5_utf16",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a VARCHAR(50) CHARACTER SET gbk,
+ b VARCHAR(50) CHARACTER SET big5,
+ c VARCHAR(50) CHARACTER SET utf16
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "table_charset_differs_from_column",
+ // Table default latin1; one column overrides to utf8mb4 (column charset != table charset).
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a VARCHAR(50),
+ b VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;`,
+ },
+
+ // ---- Numeric / bit / extreme ----
+ {
+ name: "bit_type",
+ // FIXED (Bug A): the dumper rendered the BIT default as a quoted string (`DEFAULT 'b\\'0\\''`)
+ // because the sync stores the bit literal QUOTE()-escaped in ColumnMetadata.Default; omni then
+ // loaded a string default that never matched the user's `b'0'`, re-emitting `MODIFY ... DEFAULT
+ // b'0'` every no-op. renderColumnDefault in get_database_definition.go now recovers the bit
+ // literal and emits it unquoted (`DEFAULT b'0'`, matching SHOW CREATE / mysqldump); omni #352
+ // canonicalizes the bit literal so the no-op is empty on both versions. b64 (no default) is fine.
+ bGap: false,
+ bGapReason: "",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ b1 BIT(1) NOT NULL DEFAULT b'0',
+ b8 BIT(8) NOT NULL DEFAULT b'101',
+ b64 BIT(64)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "decimal_max_precision",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ d DECIMAL(65,30) NOT NULL DEFAULT 0
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "bigint_unsigned_maxdefault",
+ bGap: false, // FIXED (re-pinned omni a362f7a7)
+ bGapReason: "DANGEROUS (silently-wrong value, not just a phantom): a BIGINT UNSIGNED default of 18446744073709551615 (max uint64) is compared/re-emitted as 9223372036854775807 (max INT64) — the default is parsed into a signed int64 and clamped/overflows. The diff emits `MODIFY ... DEFAULT '9223372036854775807'`, which would CHANGE the default to the wrong number on apply. sm/ti (in-range) are fine.",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ big BIGINT UNSIGNED NOT NULL DEFAULT 18446744073709551615,
+ sm SMALLINT UNSIGNED NOT NULL DEFAULT 65535,
+ ti TINYINT UNSIGNED NOT NULL DEFAULT 255
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "year_and_set",
+ bGap: false, // FIXED (re-pinned omni a362f7a7)
+ bGapReason: "YEAR default canonicalization: user `DEFAULT 2000` (numeric) vs stored `'2000'` (string) not recognized equal; the diff re-emits `MODIFY ... y year ... DEFAULT '2000'` every no-op (phantom; both versions). The SET column is idempotent.",
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ y YEAR NOT NULL DEFAULT 2000,
+ s SET('a','b','c','d','e','f','g') NOT NULL DEFAULT 'a,c,e'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "large_enum_set",
+ // Many ENUM/SET members.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ e ENUM('m00','m01','m02','m03','m04','m05','m06','m07','m08','m09','m10','m11','m12','m13','m14','m15') NOT NULL DEFAULT 'm00',
+ s SET('s0','s1','s2','s3','s4','s5','s6','s7','s8','s9') NOT NULL DEFAULT 's0,s9'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "varchar_near_row_limit",
+ // A wide VARCHAR in latin1 (1 byte/char) approaching the 65535-byte row limit.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ wide VARCHAR(16000) CHARACTER SET latin1 NOT NULL DEFAULT ''
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+
+ // ---- Generated columns chained ----
+ {
+ name: "generated_chained",
+ // total references price/qty; with_tax references total (a generated col referencing
+ // another generated col); both a VIRTUAL and a STORED, plus an index on a generated col.
+ // NB: `code` is a plain (non-auto-increment) column — MySQL forbids a generated column
+ // referencing an AUTO_INCREMENT column (errno 3109), so `label` references `code`.
+ ddl: `CREATE TABLE t (
+ id INT NOT NULL AUTO_INCREMENT,
+ code INT NOT NULL DEFAULT 0,
+ price DECIMAL(10,2) NOT NULL DEFAULT 0,
+ qty INT NOT NULL DEFAULT 0,
+ total DECIMAL(20,2) AS (price * qty) STORED,
+ with_tax DECIMAL(20,2) AS (total * 1.1) VIRTUAL,
+ label VARCHAR(40) AS (CONCAT('#', code)) STORED,
+ PRIMARY KEY (id),
+ KEY idx_total (total)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+
+ // ---- AUTO_INCREMENT seed ----
+ {
+ name: "auto_increment_seed_500",
+ ddl: `CREATE TABLE t (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(50) NOT NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB AUTO_INCREMENT=500 DEFAULT CHARSET=utf8mb4;`,
+ },
+
+ // ---- Invisible columns / indexes (8.0) ----
+ {
+ name: "invisible_column_80",
+ only80: true,
+ // FIXED (bug 4): sync now captures column invisibility (information_schema.COLUMNS.EXTRA
+ // INVISIBLE token into ColumnMetadata.is_invisible) and the dumper emits
+ // `/*!80023 INVISIBLE */`, so the no-op is empty.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a INT NOT NULL DEFAULT 0,
+ secret INT INVISIBLE NOT NULL DEFAULT 0
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ {
+ name: "invisible_index_80",
+ only80: true,
+ // FIXED (bug 5): index visibility was already synced (IndexMetadata.visible from
+ // information_schema.STATISTICS.IS_VISIBLE); the dumper now emits `/*!80000 INVISIBLE */`
+ // in the index clause, so the no-op is empty instead of a DROP+ADD.
+ ddl: `CREATE TABLE t (
+ id INT PRIMARY KEY,
+ a INT NOT NULL,
+ KEY idx_a (a) INVISIBLE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
+ },
+ }
+}
+
+// TestSDLDeepExoticIdempotence is Axis 2: every NON-bGap exotic-type case is applied live,
+// synced, dumped, and the production version-aware diff (dumped vs user form) MUST be empty
+// on both versions (8.0-only types skip 5.7). The bGap cases (confirmed normalization/dumper
+// (B) bugs found this round) are skipped here and reproduced/explained by
+// TestSDLDeepExoticKnownGaps so this assertion stays green. Each unexpected non-empty no-op
+// is logged with input + stored form so the gap can be pinned to a Canonical* rule or the
+// dumper.
+//
+//nolint:tparallel
+func TestSDLDeepExoticIdempotence(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, ec := range exoticCases() {
+ ec := ec
+ if srv.version == "5.7" && ec.only80 {
+ continue
+ }
+ if ec.bGap {
+ continue // confirmed (B) gap — pinned by TestSDLDeepExoticKnownGaps
+ }
+ t.Run(ec.name, func(t *testing.T) {
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_deep_exo", ec.ddl)
+
+ // Production entry: schema.SDLMigration (meta -> SDL internally).
+ noop := safeSDLMigration(ec.ddl, meta, srv.version)
+ require.False(t, noop.panicked,
+ "[%s/%s] (B) PANIC in schema.SDLMigration: %v\ninput:\n%s",
+ srv.name, ec.name, noop.panicVal, ec.ddl)
+ require.NoError(t, noop.err, "[%s/%s] SDLMigration error:\n%s", srv.name, ec.name, ec.ddl)
+
+ source := dumpSDL(ctx, t, srv, dbName)
+ if !noop.planEmpty {
+ t.Logf("[%s/%s] (B) NON-EMPTY no-op normalization gap:\nUSER FORM:\n%s\nSTORED/DUMPED:\n%s\nDIFF:\n%s",
+ srv.name, ec.name, ec.ddl, source, noop.plan)
+ }
+ require.True(t, noop.planEmpty,
+ "[%s/%s] (B) exotic-type no-op (SDLMigration) must be empty, got:\n%s", srv.name, ec.name, noop.plan)
+
+ // Version-aware lower-level entry, same property.
+ noop2, err := mysqlDiffSDLMigration(source, ec.ddl, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noop2,
+ "[%s/%s] (B) exotic-type no-op (mysqlDiffSDLMigration) must be empty, got:\n%s", srv.name, ec.name, noop2)
+ })
+ }
+ })
+ }
+}
+
+// TestSDLDeepExoticKnownGaps PINS the (B) normalization / dumper round-trip bugs this deep
+// round found in the exotic-type surface. Each bGap case is applied live and its no-op is
+// computed; the test asserts the no-op is currently NON-empty OR the dumped source fails to
+// reload (the bug signature), and logs the full user->stored->diff so a fix subagent has the
+// exact repro. When a bug is FIXED the corresponding case will start producing an empty,
+// reloadable no-op — flip its bGap flag to false (moving it back under the green idempotence
+// test) and this pin will report the now-idempotent case so the regression coverage moves
+// with the fix.
+//
+// Confirmed gaps (see each case's bGapReason for the precise root cause):
+// - DANGEROUS, silently-wrong value: bigint_unsigned_maxdefault (uint64 max default clamped
+// to int64 max in the diff — would change the default on apply).
+// - Default-literal canonicalization phantoms (re-emit every no-op): bit_type (BIT b'..'),
+// year_and_set (YEAR numeric-vs-quoted).
+// - Dumper drops a real attribute (re-emit every no-op): invisible_column_80,
+// invisible_index_80, spatial_srid_80 (SRID), spatial_notnull_index / spatial_srid_80
+// (phantom spatial-key prefix length).
+// - Dumper emits a spelling the omni parser can't reload (HARD error): spatial_types
+// (`geomcollection`), json_multivalue_index_80 (charset introducer in the functional key).
+//
+// SKIPPED so the suite stays green; remove the Skip to reproduce all gaps at once.
+//
+//nolint:tparallel
+func TestSDLDeepExoticKnownGaps(t *testing.T) {
+ t.Skip("Pins confirmed (B) exotic-type normalization/dumper gaps; remove Skip to reproduce")
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, ec := range exoticCases() {
+ ec := ec
+ if !ec.bGap {
+ continue
+ }
+ if srv.version == "5.7" && ec.only80 {
+ continue
+ }
+ t.Run(ec.name, func(t *testing.T) {
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_deep_exo_gap", ec.ddl)
+
+ noop := safeSDLMigration(ec.ddl, meta, srv.version)
+ require.False(t, noop.panicked,
+ "[%s/%s] (B) PANIC: %v", srv.name, ec.name, noop.panicVal)
+
+ source := dumpSDL(ctx, t, srv, dbName)
+ t.Logf("[%s/%s] (B) %s\nUSER:\n%s\nSTORED:\n%s\nNO-OP err=%v plan:\n%s",
+ srv.name, ec.name, ec.bGapReason, ec.ddl, source, noop.err, noop.plan)
+
+ // The bug signature is EITHER a hard error from reloading the dumped source
+ // OR a non-empty no-op. Asserting it documents the current broken behavior.
+ broke := noop.err != nil || !noop.planEmpty
+ require.True(t, broke,
+ "[%s/%s] expected the known (B) gap to reproduce (load error or non-empty no-op) but it was idempotent — the bug may be FIXED; clear bGap on this case.\nreason was: %s",
+ srv.name, ec.name, ec.bGapReason)
+ })
+ }
+ })
+ }
+}
+
+// ============================================================================
+// Axis 3: sequential release chains (the real iterative usage).
+// ============================================================================
+//
+// Start from S0 on a real DB. Apply a SEQUENCE of declarative releases S0->S1->...->S4,
+// each a realistic incremental change set. At EACH step: source = MetadataToSDL(sync(DB)),
+// diff against the step target, assert the plan is minimal+correctly-ordered, APPLY it to
+// the real DB, re-sync, and assert the next no-op is empty before moving on. This proves
+// correctness across a migration HISTORY, not a single diff.
+
+// chainStep is one release in the sequence. target is the FULL desired SDL at that step
+// (declarative — the complete schema, not a delta). wantContains are uppercase substrings
+// the generated plan must contain; wantOrder pins a required relative ordering (first must
+// appear before second). only80Extra is appended to target only on 8.0 (CHECK etc.).
+type chainStep struct {
+ name string
+ target80 string
+ target57 string
+ wantContain []string // uppercase substrings the plan must contain
+ wantAbsent []string // uppercase substrings the plan must NOT contain
+ wantOrder [2]string // [before, after] uppercase substrings; before must precede after (skip if either empty)
+}
+
+func (s chainStep) target(version string) string {
+ if version == "5.7" {
+ return s.target57
+ }
+ return s.target80
+}
+
+// --- The sequence. Each Sn target is the FULL schema at that point. ---
+
+// chainS0: a small starting schema — customer + order with an FK, one view.
+//
+//go:embed testdata/sdl/chain_s0.sql
+var chainS0 string
+
+// chainS1: add table `order_item` + FK to ord (and to a new `product` table).
+//
+//go:embed testdata/sdl/chain_s1_common.sql
+var chainS1Common string
+
+// chainS2: add a column (customer.loyalty_points) + an index (ord.idx_ord_created) + a
+// generated column (order_item.line_total references qty — but needs price; keep it simple:
+// generated col on product: price_with_tax).
+//
+//go:embed testdata/sdl/chain_s2_common.sql
+var chainS2Common string
+
+// chainS3: modify a column type (ord.total DECIMAL(10,2)->DECIMAL(14,4)) + widen a VARCHAR
+// (customer.name VARCHAR(100)->VARCHAR(200)) + change a default (customer.loyalty_points
+// DEFAULT 0 -> DEFAULT 100).
+//
+//go:embed testdata/sdl/chain_s3_common.sql
+var chainS3Common string
+
+// chainS4: drop a column WITH its index (drop product.price_with_tax generated col), drop a
+// table that is an FK PARENT (drop product — order_item.fk_oi_product child FK must drop
+// first; also drop order_item to keep it consistent? No — keep order_item but remove its FK
+// to product by dropping the product reference). To exercise "drop FK parent, child FK drops
+// first", we drop `product` AND order_item's fk_oi_product + product_id column. Replace the
+// view, and (8.0) add a CHECK on ord.
+//
+// S4 also changes order_item to remove product linkage, replaces v_cust_orders, adds a
+// trigger on ord, and (8.0) adds a CHECK on ord.total.
+//
+//go:embed testdata/sdl/chain_s4_base.sql
+var chainS4Base string
+
+func chainSteps() []chainStep {
+ s4_80 := strings.Replace(chainS4Base, "{{ORD_CHECK}}", ",\n\tCONSTRAINT chk_total CHECK (total >= 0)", 1)
+ s4_57 := strings.Replace(chainS4Base, "{{ORD_CHECK}}", "", 1)
+ return []chainStep{
+ {
+ name: "S1_add_table_fk",
+ target80: chainS1Common,
+ target57: chainS1Common,
+ wantContain: []string{"CREATE TABLE `PRODUCT`", "CREATE TABLE `ORDER_ITEM`"},
+ wantAbsent: []string{"DROP TABLE", "CREATE TABLE `CUSTOMER`", "CREATE TABLE `ORD`"},
+ // The order_item FK to ord/product is deferred to PhasePost: table create precedes the FK add.
+ wantOrder: [2]string{"CREATE TABLE `ORDER_ITEM`", "ADD CONSTRAINT `FK_OI_ORDER`"},
+ },
+ {
+ name: "S2_add_column_index_generated",
+ target80: chainS2Common,
+ target57: chainS2Common,
+ wantContain: []string{"ADD COLUMN `LOYALTY_POINTS`", "ADD KEY `IDX_ORD_CREATED`", "PRICE_WITH_TAX"},
+ wantAbsent: []string{"DROP TABLE", "CREATE TABLE"},
+ },
+ {
+ name: "S3_modify_type_widen_default",
+ target80: chainS3Common,
+ target57: chainS3Common,
+ // total type change, name widen, loyalty default change.
+ wantContain: []string{"DECIMAL(14,4)", "VARCHAR(200)"},
+ wantAbsent: []string{"DROP TABLE", "CREATE TABLE `CUSTOMER`", "DROP COLUMN"},
+ },
+ {
+ name: "S4_drop_col_drop_parent_replace_view_trigger",
+ target80: s4_80,
+ target57: s4_57,
+ // product table dropped; order_item.product_id column + its FK dropped; view replaced; trigger added.
+ wantContain: []string{"DROP TABLE `PRODUCT`", "DROP COLUMN `PRODUCT_ID`", "DROP FOREIGN KEY `FK_OI_PRODUCT`", "TRG_ORD_INS", "V_CUST_ORDERS"},
+ wantAbsent: []string{"DROP TABLE `CUSTOMER`", "DROP TABLE `ORD`"},
+ // The child FK on order_item must drop before the parent product table is dropped.
+ wantOrder: [2]string{"DROP FOREIGN KEY `FK_OI_PRODUCT`", "DROP TABLE `PRODUCT`"},
+ },
+ }
+}
+
+// TestSDLDeepSequentialChain is Axis 3: drive S0 -> S1 -> S2 -> S3 -> S4 on ONE live DB.
+// Each step computes source=MetadataToSDL(sync), diffs the step target, asserts the plan is
+// minimal + correctly ordered, APPLIES it, re-syncs, and asserts the next no-op is empty
+// before advancing. Proves correctness across a migration history. Both versions.
+//
+// The steps run inline (NOT as separate subtests) BECAUSE they share one evolving DB: each
+// step's diff is from its predecessor's applied state. Running a single step in isolation
+// (e.g. `-run .../S4`) would compute a diff from S0 and assert wrong things — so the chain is
+// kept atomic per server.
+//
+//nolint:tparallel
+func TestSDLDeepSequentialChain(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ // Materialize S0 on a single DB that persists across all steps.
+ dbName := newLiveDatabase(ctx, t, srv, "sdl_deep_chain")
+ require.NoError(t, applyDDL(ctx, t, srv, dbName, chainS0), "[%s] apply S0", srv.name)
+
+ // Confirm S0 itself is idempotent before starting the chain.
+ s0src := dumpSDL(ctx, t, srv, dbName)
+ s0noop, err := mysqlDiffSDLMigration(s0src, chainS0, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, s0noop, "[%s] S0 must be idempotent before the chain, got:\n%s", srv.name, s0noop)
+
+ // Run every step inline, in order, against the same evolving DB.
+ for _, step := range chainSteps() {
+ runChainStep(ctx, t, srv, dbName, step)
+ if t.Failed() {
+ return // a broken step poisons the chain state; stop so the failure is unambiguous
+ }
+ }
+ })
+ }
+}
+
+// runChainStep executes one release in the sequential chain against the live dbName.
+func runChainStep(ctx context.Context, t *testing.T, srv liveServer, dbName string, step chainStep) {
+ t.Helper()
+ target := step.target(srv.version)
+
+ // 1) Compute the plan from the CURRENT live DB state to the step target.
+ source := dumpSDL(ctx, t, srv, dbName)
+ plan, err := mysqlDiffSDLMigration(source, target, srv.version)
+ require.NoError(t, err)
+ require.NotEmpty(t, plan, "[%s/%s] step must produce a non-empty plan", srv.name, step.name)
+ t.Logf("[%s/%s] plan:\n%s", srv.name, step.name, plan)
+ upper := strings.ToUpper(plan)
+
+ // 2) Minimality + correctness: required substrings present, forbidden absent.
+ for _, want := range step.wantContain {
+ require.Contains(t, upper, want, "[%s/%s] plan missing %q:\n%s", srv.name, step.name, want, plan)
+ }
+ for _, absent := range step.wantAbsent {
+ require.NotContains(t, upper, absent, "[%s/%s] plan must not contain %q:\n%s", srv.name, step.name, absent, plan)
+ }
+
+ // 3) Ordering.
+ if step.wantOrder[0] != "" && step.wantOrder[1] != "" {
+ bi := indexOf(upper, step.wantOrder[0])
+ ai := indexOf(upper, step.wantOrder[1])
+ require.GreaterOrEqual(t, bi, 0, "[%s/%s] ordering: %q not found:\n%s", srv.name, step.name, step.wantOrder[0], plan)
+ require.GreaterOrEqual(t, ai, 0, "[%s/%s] ordering: %q not found:\n%s", srv.name, step.name, step.wantOrder[1], plan)
+ require.Less(t, bi, ai, "[%s/%s] %q must precede %q:\n%s", srv.name, step.name, step.wantOrder[0], step.wantOrder[1], plan)
+ }
+
+ // 4) Apply to the real DB.
+ require.NoError(t, applyDDL(ctx, t, srv, dbName, plan),
+ "[%s/%s] step plan failed to apply:\n%s", srv.name, step.name, plan)
+
+ // 5) Re-sync and assert convergence (next no-op empty) BEFORE advancing.
+ newSource := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(newSource, target, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "[%s/%s] step did not converge, residual:\n%s", srv.name, step.name, converge)
+
+ // 6) Production-path cross-check: schema.SDLMigration on a freshly-synced meta must also be
+ // empty (the path the release executor takes).
+ meta := syncMetaForDB(ctx, t, srv, dbName)
+ prodNoop, err := schema.SDLMigration(storepb.Engine_MYSQL, target, meta, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, prodNoop, "[%s/%s] production-path no-op after apply must be empty, got:\n%s", srv.name, step.name, prodNoop)
+}
+
+// syncMetaForDB syncs an existing dbName and returns its model.DatabaseMetadata.
+func syncMetaForDB(ctx context.Context, t *testing.T, srv liveServer, dbName string) *model.DatabaseMetadata {
+ t.Helper()
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ metadata, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+ return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true)
+}
+
+// ============================================================================
+// Axis 4: scale / performance sanity.
+// ============================================================================
+//
+// Generate ~90 tables (each ~8-15 columns, a PK, 2-3 secondary indexes, FKs forming a
+// realistic chain). Sync -> MetadataToSDL -> no-op diff must be EMPTY and complete in
+// reasonable wall-time. Then add ONE column to ONE table -> assert exactly one minimal
+// ALTER, not a re-emit of all tables. Record timings.
+
+// genScaleSchema builds n tables t000..t{n-1}. Each table i (i>=1) carries an FK to table
+// i-1 (a chain), plus 2 secondary indexes and ~10 columns of mixed types. Returns the DDL.
+func genScaleSchema(n int) string {
+ var b strings.Builder
+ for i := 0; i < n; i++ {
+ fmt.Fprintf(&b, "CREATE TABLE `t%03d` (\n", i)
+ b.WriteString("\tid INT NOT NULL AUTO_INCREMENT,\n")
+ b.WriteString("\tcode VARCHAR(40) NOT NULL,\n")
+ b.WriteString("\tname VARCHAR(120) NOT NULL,\n")
+ b.WriteString("\tdescription TEXT,\n")
+ b.WriteString("\tamount DECIMAL(14,2) NOT NULL DEFAULT 0.00,\n")
+ b.WriteString("\tqty INT NOT NULL DEFAULT 0,\n")
+ b.WriteString("\tactive BOOLEAN NOT NULL DEFAULT TRUE,\n")
+ b.WriteString("\tcreated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n")
+ b.WriteString("\tupdated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n")
+ b.WriteString("\tstatus ENUM('new','open','closed') NOT NULL DEFAULT 'new',\n")
+ if i > 0 {
+ b.WriteString("\tprev_id INT NULL,\n")
+ }
+ b.WriteString("\tPRIMARY KEY (id),\n")
+ fmt.Fprintf(&b, "\tUNIQUE KEY `uk_t%03d_code` (code),\n", i)
+ fmt.Fprintf(&b, "\tKEY `idx_t%03d_name` (name),\n", i)
+ if i > 0 {
+ fmt.Fprintf(&b, "\tKEY `idx_t%03d_prev` (prev_id),\n", i)
+ fmt.Fprintf(&b, "\tCONSTRAINT `fk_t%03d_prev` FOREIGN KEY (prev_id) REFERENCES `t%03d` (id) ON DELETE SET NULL\n", i, i-1)
+ } else {
+ // trim the trailing comma from the last index line for table 0
+ fmt.Fprintf(&b, "\tKEY `idx_t%03d_amount` (amount)\n", i)
+ }
+ b.WriteString(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n")
+ }
+ return b.String()
+}
+
+// TestSDLDeepScale is Axis 4: a ~90-table schema. The no-op diff must be empty and fast;
+// adding one column to one table must yield exactly one minimal ALTER, not a re-emit of all
+// tables. Timings are recorded and a pathological (super-linear / multi-second) no-op is
+// flagged.
+//
+//nolint:tparallel
+func TestSDLDeepScale(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ const tableCount = 90
+ schemaDDL := genScaleSchema(tableCount)
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ meta, dbName := syncMetadata(ctx, t, srv, "sdl_deep_scale", schemaDDL)
+
+ // (1) No-op diff: must be empty + record wall-time.
+ source := dumpSDL(ctx, t, srv, dbName)
+ start := time.Now()
+ noop, err := mysqlDiffSDLMigration(source, schemaDDL, srv.version)
+ elapsed := time.Since(start)
+ require.NoError(t, err)
+ if noop != "" {
+ t.Logf("[%s] NON-EMPTY %d-table no-op (%d statements):\n%s", srv.name, tableCount, statementCount(noop), noop)
+ }
+ require.Empty(t, noop, "[%s] %d-table no-op must be empty, got %d statements", srv.name, tableCount, statementCount(noop))
+ t.Logf("[%s] SCALE no-op diff over %d tables: %s", srv.name, tableCount, elapsed)
+ // Soft perf flag: a no-op over ~90 tables should be well under a few seconds.
+ if elapsed > 10*time.Second {
+ t.Errorf("[%s] (PERF) %d-table no-op took %s (>10s) — pathological", srv.name, tableCount, elapsed)
+ }
+
+ // (2) Production-path no-op timing too.
+ pstart := time.Now()
+ prodNoop, err := schema.SDLMigration(storepb.Engine_MYSQL, schemaDDL, meta, srv.version)
+ pelapsed := time.Since(pstart)
+ require.NoError(t, err)
+ require.Empty(t, prodNoop, "[%s] %d-table production no-op must be empty", srv.name, tableCount)
+ t.Logf("[%s] SCALE production-path no-op (incl. MetadataToSDL): %s", srv.name, pelapsed)
+
+ // (3) One small change: add a column to ONE table (t045). Must be exactly one ALTER
+ // touching only that table — not a re-emit of the other 89.
+ changed := strings.Replace(schemaDDL,
+ "CREATE TABLE `t045` (\n\tid INT NOT NULL AUTO_INCREMENT,\n\tcode VARCHAR(40) NOT NULL,",
+ "CREATE TABLE `t045` (\n\tid INT NOT NULL AUTO_INCREMENT,\n\tcode VARCHAR(40) NOT NULL,\n\tnew_flag INT NOT NULL DEFAULT 0,",
+ 1)
+ require.NotEqual(t, schemaDDL, changed, "[%s] scale change setup must differ", srv.name)
+
+ cstart := time.Now()
+ change, err := mysqlDiffSDLMigration(source, changed, srv.version)
+ celapsed := time.Since(cstart)
+ require.NoError(t, err)
+ t.Logf("[%s] SCALE single-change diff: %s\nplan:\n%s", srv.name, celapsed, change)
+ require.NotEmpty(t, change, "[%s] single-change must produce DDL", srv.name)
+
+ upper := strings.ToUpper(change)
+ require.Contains(t, upper, "ADD COLUMN `NEW_FLAG`", "[%s] expected the new column:\n%s", srv.name, change)
+ require.Contains(t, upper, "`T045`", "[%s] expected the changed table named:\n%s", srv.name, change)
+ require.NotContains(t, upper, "CREATE TABLE", "[%s] single-change must not recreate tables:\n%s", srv.name, change)
+ require.NotContains(t, upper, "DROP TABLE", "[%s] single-change must not drop tables:\n%s", srv.name, change)
+ // Strict minimality: exactly ONE statement (the single ADD COLUMN).
+ require.Equal(t, 1, statementCount(change),
+ "[%s] single-change over %d tables must be exactly 1 statement, got %d:\n%s",
+ srv.name, tableCount, statementCount(change), change)
+
+ // No other table may be named in the plan.
+ for i := 0; i < tableCount; i++ {
+ if i == 45 {
+ continue
+ }
+ token := fmt.Sprintf("`T%03d`", i)
+ require.NotContains(t, upper, token,
+ "[%s] single-change must not touch table t%03d:\n%s", srv.name, i, change)
+ }
+ })
+ }
+}
+
+// ENTERPRISE smoke axes for the MySQL declarative (SDL) migration path. The real-world
+// suite (the real-world axes above) proved the path on mid-size production schemas; these
+// scales verification to ENTERPRISE corpora and adds the axes an enterprise rollout
+// exercises hardest:
+//
+// corpus — Zabbix 7.0.0 (203 tables, 272 FKs, 65 changelog triggers), PrestaShop
+// (243 tables), OpenEMR (283 tables, INSERTs stripped), and the stock
+// MySQL `sys` schema (scaffolded, gated on an in-flight omni fix).
+// baseline — every corpus loads live, and its canonical dump self-diffs EMPTY
+// (determinism) and no-ops through the production schema.SDLMigration.
+// A1 — single-object CRUD (create / modify / drop) for every object kind, on a
+// 20-table Zabbix slice.
+// A2 — dependent objects: FK chains, trigger→table deps (the Zabbix changelog
+// pattern), 3-deep view stacks, function-used-by-view, circular FK pairs.
+// A3 — special types: AUTO_INCREMENT, generated columns, ENUM/SET members,
+// charset/collation, temporal precision, DECIMAL, TEXT/BLOB families,
+// signedness, prefix-indexed TEXT, partition ops.
+// A5 — sequential chain (with one-shot endpoint equality), apply-back,
+// combined release, scale timing guard, multi-file export round-trip.
+// A4 (fuzz) — see the A4 section below.
+//
+// The per-case oracle protocol (entOracle) is the realworld suite's, made reusable:
+//
+// (1) load base B into an entsdl_-prefixed scratch DB, sync → canonical current SDL C;
+// (2) DDL = mysqlDiffSDLMigration(C, target T, version);
+// (3) apply DDL to the scratch DB — clean execution (dependency ordering) is under test;
+// (4) re-sync → C'; assert Diff(C', T) == "" (convergence) and Diff(C', C') == ""
+// (idempotence);
+// (5) minimality asserts (exact statement counts) where cheap.
+//
+// Shared helpers (liveServers, createLiveMySQLDriver, newLiveDatabase, dumpSDL, applyDDL,
+// statementCount, normalizeDelimiters, syncMetaForDB, objectCounts, addColumnToTable,
+// addIndexToTable, addColumnToView, dropObjectBlock, dropTrigger, findObjectSegment,
+// removeSegment, mustReplace, concatMultiFile) are declared with the axes above.
+
+// ----------------------------------------------------------------------------
+// Embedded enterprise corpora.
+//
+// Preprocessing applied when the corpus was lifted into testdata:
+// - zabbix: Zabbix 7.0.0 create/mysql/schema.sql verbatim, minus the single
+// INSERT INTO dbversion row (DDL-only testdata). Keeps the DELIMITER $$
+// changelog-trigger block (normalizeDelimiters handles it), the 301
+// standalone CREATE INDEX statements, and the 272 trailing
+// ALTER TABLE ... ADD CONSTRAINT foreign keys.
+// - prestashop: db_structure.sql with the installer placeholders substituted
+// (PREFIX_ -> ps_, ENGINE_TYPE -> InnoDB, ...). Leads with
+// SET SESSION sql_mode='' (two DEFAULT '0000-00-00 00:00:00' columns
+// need non-strict mode at load; the driver executes the corpus on a
+// single connection so the session setting holds).
+// - openemr: database.sql with all 5,821 INSERT statements stripped by a
+// quote-aware scanner (283 tables, DDL-only).
+// - sys: the stock MySQL 8.0 sys schema, single-file form. Formerly gated
+// on two omni parser gaps it exposed (adjacent string literals #360,
+// paren-subquery operand continuation #366) — both fixed and pinned.
+// ----------------------------------------------------------------------------
+
+//go:embed testdata/enterprise/zabbix.sql
+var entZabbixSQL string
+
+//go:embed testdata/enterprise/prestashop.sql
+var entPrestashopSQL string
+
+//go:embed testdata/enterprise/openemr.sql
+var entOpenemrSQL string
+
+//go:embed testdata/enterprise/sys.sql
+var entSysSQL string
+
+// entCorpus is one embedded enterprise schema.
+type entCorpus struct {
+ name string
+ ddl string
+ // gate, when non-empty, skips every leg for this corpus with the given reason.
+ gate string
+ // gate57, when non-empty, skips only the 5.7 legs with the given reason.
+ gate57 string
+ // canonical marks ddl as a canonical SDL dump (alphabetical object order) rather
+ // than upstream creation-ordered DDL. Canonical dumps are loaded by applying the
+ // engine's own empty→schema plan, whose statements are dependency-ordered —
+ // sequential client apply of the raw dump would break on view-on-view forward
+ // references (sys: `host_summary` reads `x$...` views that sort later).
+ canonical bool
+}
+
+func entCorpora() []entCorpus {
+ return []entCorpus{
+ {name: "zabbix", ddl: entZabbixSQL},
+ {name: "prestashop", ddl: entPrestashopSQL},
+ {name: "openemr", ddl: entOpenemrSQL},
+ {name: "sys", ddl: entSysSQL, canonical: true,
+ gate57: "sys corpus is the stock MySQL 8.0 sys schema; its views read performance_schema tables that 5.7.25 does not have"},
+ }
+}
+
+// entLoadCorpus loads one corpus into a fresh scratch database, honoring per-version
+// gates. Canonical dumps go through the engine's own dependency-ordered create plan
+// (dogfooding: every corpus load exercises the empty→schema ordering guarantees).
+func entLoadCorpus(ctx context.Context, t *testing.T, srv liveServer, prefix string, corpus entCorpus) string {
+ t.Helper()
+ if corpus.gate != "" {
+ t.Skipf("[%s/%s] %s", srv.name, corpus.name, corpus.gate)
+ }
+ if corpus.gate57 != "" && srv.name == "mysql57" {
+ t.Skipf("[%s/%s] %s", srv.name, corpus.name, corpus.gate57)
+ }
+ if !corpus.canonical {
+ return entLoadDDL(ctx, t, srv, prefix+corpus.name, corpus.ddl)
+ }
+ dbName := newLiveDatabase(ctx, t, srv, prefix+corpus.name)
+ plan, err := mysqlDiffSDLMigration("", corpus.ddl, srv.version)
+ require.NoError(t, err, "[%s/%s] empty→corpus create plan", srv.name, corpus.name)
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, plan, db.ExecuteOptions{})
+ require.NoError(t, err, "[%s/%s] apply engine create plan", srv.name, corpus.name)
+ return dbName
+}
+
+// entNormalizeDelimiters rewrites a multi-DELIMITER script into the no-DELIMITER form
+// the production split path handles. It extends the realworld suite's normalizeDelimiters
+// for the Zabbix style, where the custom delimiter sits on its OWN line and most trigger
+// bodies are ALREADY ';'-terminated — naively rewriting every delimiter line to ";" left
+// bare ";" statements that MySQL rejects (Error 1064 near ';'). Here a delimiter (inline
+// or standalone) only yields a ";" when the statement is not already terminated.
+func entNormalizeDelimiters(ddl string) string {
+ if !strings.Contains(ddl, "DELIMITER") {
+ return ddl
+ }
+ lines := strings.Split(ddl, "\n")
+ out := make([]string, 0, len(lines))
+ delim := ";"
+ terminated := func() bool {
+ for i := len(out) - 1; i >= 0; i-- {
+ trimmed := strings.TrimSpace(out[i])
+ if trimmed == "" {
+ continue
+ }
+ return strings.HasSuffix(trimmed, ";")
+ }
+ return true
+ }
+ for _, ln := range lines {
+ trimmed := strings.TrimSpace(ln)
+ if strings.HasPrefix(strings.ToUpper(trimmed), "DELIMITER ") {
+ delim = strings.TrimSpace(trimmed[len("DELIMITER "):])
+ if delim == "" {
+ delim = ";"
+ }
+ continue
+ }
+ if delim == ";" {
+ out = append(out, ln)
+ continue
+ }
+ if trimmed == delim {
+ if !terminated() {
+ out = append(out, ";")
+ }
+ continue
+ }
+ if strings.HasSuffix(trimmed, delim) {
+ idx := strings.LastIndex(ln, delim)
+ body := strings.TrimRight(ln[:idx], " \t")
+ if !strings.HasSuffix(strings.TrimSpace(body), ";") {
+ body += ";"
+ }
+ out = append(out, body)
+ continue
+ }
+ out = append(out, ln)
+ }
+ return strings.Join(out, "\n")
+}
+
+// entLoadDDL creates a fresh entsdl_-prefixed scratch database on srv (dropped in
+// cleanup by newLiveDatabase) and applies ddl through the production driver path.
+func entLoadDDL(ctx context.Context, t *testing.T, srv liveServer, prefix, ddl string) string {
+ t.Helper()
+ dbName := newLiveDatabase(ctx, t, srv, prefix)
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, entNormalizeDelimiters(ddl), db.ExecuteOptions{})
+ require.NoError(t, err, "[%s] apply base DDL", srv.name)
+ return dbName
+}
+
+// entPlanStatementCount counts plan statements compound-aware: routine/trigger bodies
+// carry internal ';' that the naive statementCount (string split) over-counts, so the
+// minimality asserts split with the production splitter instead.
+func entPlanStatementCount(t *testing.T, plan string) int {
+ t.Helper()
+ stmts, err := mysqlparser.SplitSQL(plan)
+ require.NoError(t, err, "split plan for statement count:\n%s", plan)
+ n := 0
+ for _, s := range stmts {
+ if text := strings.TrimSpace(s.Text); text != "" && text != ";" {
+ n++
+ }
+ }
+ return n
+}
+
+// entOracle runs steps (2)-(4) of the oracle protocol against an already-loaded scratch
+// database: dump the canonical current SDL, diff to target, apply the generated DDL, and
+// prove convergence + idempotence. Returns the generated plan for minimality asserts.
+func entOracle(ctx context.Context, t *testing.T, srv liveServer, dbName, target, label string) string {
+ t.Helper()
+ source := dumpSDL(ctx, t, srv, dbName)
+ require.NotEqual(t, source, target, "[%s] target must differ from source", label)
+
+ plan, err := mysqlDiffSDLMigration(source, target, srv.version)
+ require.NoError(t, err, "[%s] diff source->target", label)
+ require.NotEmpty(t, plan, "[%s] expected a non-empty migration plan", label)
+ t.Logf("[%s] plan (%d stmts):\n%s", label, statementCount(plan), plan)
+
+ applyErr := applyDDL(ctx, t, srv, dbName, plan)
+ require.NoError(t, applyErr, "[%s] generated plan failed to apply:\n%s", label, plan)
+
+ after := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(after, target, srv.version)
+ require.NoError(t, err, "[%s] converge diff", label)
+ require.Empty(t, converge, "[%s] did not converge; residual:\n%s\nplan was:\n%s", label, converge, plan)
+
+ self, err := mysqlDiffSDLMigration(after, after, srv.version)
+ require.NoError(t, err, "[%s] idempotence diff", label)
+ require.Empty(t, self, "[%s] post-apply dump not idempotent:\n%s", label, self)
+ return plan
+}
+
+// ----------------------------------------------------------------------------
+// Zabbix slice extraction.
+//
+// The corpus declares tables, then standalone CREATE [UNIQUE] INDEX statements, then a
+// DELIMITER block of changelog triggers, then trailing ALTER TABLE ... ADD CONSTRAINT
+// foreign keys. A slice keeps the CREATE TABLE + CREATE INDEX statements of the included
+// tables, the triggers ON included tables (their bodies write `changelog`, so changelog
+// must be included), and only the FKs whose BOTH endpoints are included — yielding a
+// self-consistent sub-schema in the corpus's own statement order.
+// ----------------------------------------------------------------------------
+
+type entZbxKind int
+
+const (
+ entZbxTable entZbxKind = iota
+ entZbxIndex
+ entZbxFK
+ entZbxTrigger
+ entZbxOther
+)
+
+type entZbxStmt struct {
+ text string
+ kind entZbxKind
+ table string
+ refTable string
+}
+
+var (
+ entReTable = regexp.MustCompile("(?i)^CREATE TABLE `(\\w+)`")
+ entReIndex = regexp.MustCompile("(?i)^CREATE (?:UNIQUE )?INDEX `\\w+` ON `(\\w+)`")
+ entReFK = regexp.MustCompile("(?is)^ALTER TABLE `(\\w+)` ADD CONSTRAINT `\\w+` FOREIGN KEY.*?REFERENCES `(\\w+)`")
+ entReTrigger = regexp.MustCompile(`(?is)^create\s+trigger\s+\w+\s+(?:before|after)\s+(?:insert|update|delete)\s+on\s+` + "`?(\\w+)`?")
+
+ entZbxOnce sync.Once
+ entZbxStmts []entZbxStmt
+ entZbxErr error
+)
+
+// entZabbixStatements splits the normalized zabbix corpus once and classifies every
+// statement for slicing.
+func entZabbixStatements() ([]entZbxStmt, error) {
+ entZbxOnce.Do(func() {
+ stmts, err := mysqlparser.SplitSQL(entNormalizeDelimiters(entZabbixSQL))
+ if err != nil {
+ entZbxErr = err
+ return
+ }
+ for _, s := range stmts {
+ text := strings.TrimSpace(s.Text)
+ if text == "" || text == ";" {
+ continue
+ }
+ classified := entZbxStmt{text: text, kind: entZbxOther}
+ if m := entReTable.FindStringSubmatch(text); m != nil {
+ classified.kind, classified.table = entZbxTable, m[1]
+ } else if m := entReIndex.FindStringSubmatch(text); m != nil {
+ classified.kind, classified.table = entZbxIndex, m[1]
+ } else if m := entReFK.FindStringSubmatch(text); m != nil {
+ classified.kind, classified.table, classified.refTable = entZbxFK, m[1], m[2]
+ } else if m := entReTrigger.FindStringSubmatch(text); m != nil {
+ classified.kind, classified.table = entZbxTrigger, m[1]
+ }
+ entZbxStmts = append(entZbxStmts, classified)
+ }
+ })
+ return entZbxStmts, entZbxErr
+}
+
+// entZabbixSlice extracts a self-consistent slice of the zabbix corpus containing the
+// given tables (which must include changelog — the trigger bodies write to it).
+func entZabbixSlice(t *testing.T, tables ...string) string {
+ t.Helper()
+ include := make(map[string]bool, len(tables))
+ for _, tbl := range tables {
+ include[tbl] = true
+ }
+ require.True(t, include["changelog"], "zabbix slices must include changelog (trigger bodies write to it)")
+
+ stmts, err := entZabbixStatements()
+ require.NoError(t, err, "split zabbix corpus")
+
+ var b strings.Builder
+ found := map[string]bool{}
+ for _, s := range stmts {
+ keep := false
+ switch s.kind {
+ case entZbxTable, entZbxIndex, entZbxTrigger:
+ keep = include[s.table]
+ if s.kind == entZbxTable && keep {
+ found[s.table] = true
+ }
+ case entZbxFK:
+ keep = include[s.table] && include[s.refTable]
+ case entZbxOther:
+ keep = false
+ default:
+ keep = false
+ }
+ if keep {
+ b.WriteString(s.text)
+ if !strings.HasSuffix(strings.TrimSpace(s.text), ";") {
+ b.WriteString(";")
+ }
+ b.WriteString("\n")
+ }
+ }
+ for _, tbl := range tables {
+ require.True(t, found[tbl], "zabbix slice: table %q not found in corpus", tbl)
+ }
+ return b.String()
+}
+
+// entSliceCoreTables is the ~20-table Zabbix slice used by A1/A2/A5: a real 4-deep FK
+// chain (item_tag -> items -> hosts -> proxy -> proxy_group), the changelog trigger
+// pattern on 8 of the tables (hosts alone carries 5 triggers, including a BEGIN/END
+// body), self-referencing FKs (hosts.templateid, items.templateid/master_itemid), and
+// composite unique indexes (hosts_groups).
+var entSliceCoreTables = []string{
+ "changelog",
+ "role", "users", "media_type", "media",
+ "proxy_group", "proxy", "proxy_rtdata",
+ "maintenances", "hosts", "host_rtdata",
+ "hstgrp", "hosts_groups",
+ "interface", "valuemap", "items", "item_tag", "item_preproc",
+ "drules", "dchecks",
+ "connector", "connector_tag",
+}
+
+// ----------------------------------------------------------------------------
+// Targeted string-surgery helpers on the canonical dump (ent-prefixed; the shared
+// realworld helpers cover the untargeted forms).
+// ----------------------------------------------------------------------------
+
+// entTableSegment returns the [start,end) range of the CREATE TABLE statement for table
+// in source (located via the SDL splitter, so partition comments stay in-segment).
+func entTableSegment(t *testing.T, source, table string) (int, int) {
+ t.Helper()
+ header := "CREATE TABLE `" + table + "`"
+ start, end := findObjectSegment(t, source, func(stmt string) bool {
+ return strings.HasPrefix(stmt, header)
+ })
+ require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
+ return start, end
+}
+
+// entReplaceInTable replaces old with new exactly once, scoped to table's CREATE block.
+func entReplaceInTable(t *testing.T, source, table, old, replacement string) string {
+ t.Helper()
+ start, end := entTableSegment(t, source, table)
+ seg := source[start:end]
+ require.Contains(t, seg, old, "table %q block must contain %q", table, old)
+ return source[:start] + strings.Replace(seg, old, replacement, 1) + source[end:]
+}
+
+// entReplaceAllInTable replaces every occurrence of old within table's CREATE block.
+func entReplaceAllInTable(t *testing.T, source, table, old, replacement string) string {
+ t.Helper()
+ start, end := entTableSegment(t, source, table)
+ seg := source[start:end]
+ require.Contains(t, seg, old, "table %q block must contain %q", table, old)
+ return source[:start] + strings.ReplaceAll(seg, old, replacement) + source[end:]
+}
+
+// entDropTableBlock removes the whole CREATE TABLE statement for table.
+func entDropTableBlock(t *testing.T, source, table string) string {
+ t.Helper()
+ start, end := entTableSegment(t, source, table)
+ return removeSegment(source, start, end)
+}
+
+// entDropObjectNamed removes the CREATE statement whose text contains `<KEYWORD>
+// `+"`name`"+“ (EVENT and other kinds the shared dropObjectBlock does not cover).
+func entDropObjectNamed(t *testing.T, source, keyword, name string) string {
+ t.Helper()
+ needle := keyword + " `" + strings.ToUpper(name) + "`"
+ start, end := findObjectSegment(t, source, func(stmt string) bool {
+ u := strings.ToUpper(stmt)
+ return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, needle)
+ })
+ require.GreaterOrEqual(t, start, 0, "%s %q not found in source", keyword, name)
+ return removeSegment(source, start, end)
+}
+
+// entDropLineInTable removes the single body line containing marker from table's CREATE
+// block, fixing the dangling comma when the removed line was the last body element.
+func entDropLineInTable(t *testing.T, source, table, marker string) string {
+ t.Helper()
+ return entEditLineInTable(t, source, table, marker, "")
+}
+
+// entReplaceLineInTable swaps the single body line containing marker for newLine
+// (indentation and trailing comma are managed here).
+func entReplaceLineInTable(t *testing.T, source, table, marker, newLine string) string {
+ t.Helper()
+ return entEditLineInTable(t, source, table, marker, newLine)
+}
+
+// entEditLineInTable is the shared core of drop/replace-line: newLine == "" drops the
+// marker line, otherwise the line is replaced by newLine (re-indented, comma preserved).
+func entEditLineInTable(t *testing.T, source, table, marker, newLine string) string {
+ t.Helper()
+ start, end := entTableSegment(t, source, table)
+ seg := source[start:end]
+ lines := strings.Split(seg, "\n")
+ idx := -1
+ for i, ln := range lines {
+ if i == 0 {
+ continue // never the CREATE TABLE header
+ }
+ if strings.Contains(ln, marker) {
+ idx = i
+ break
+ }
+ }
+ require.GreaterOrEqual(t, idx, 0, "table %q has no body line containing %q:\n%s", table, marker, seg)
+ hadComma := strings.HasSuffix(strings.TrimSpace(lines[idx]), ",")
+ if newLine == "" {
+ lines = append(lines[:idx], lines[idx+1:]...)
+ if !hadComma {
+ // Removed the last body element: strip the now-dangling comma above it.
+ for j := idx - 1; j > 0; j-- {
+ trimmed := strings.TrimRight(lines[j], " \t")
+ if strings.HasSuffix(trimmed, ",") {
+ lines[j] = strings.TrimSuffix(trimmed, ",")
+ break
+ }
+ if strings.TrimSpace(trimmed) != "" {
+ break
+ }
+ }
+ }
+ } else {
+ replaced := " " + strings.TrimSpace(newLine)
+ if hadComma {
+ replaced += ","
+ }
+ lines[idx] = replaced
+ }
+ return source[:start] + strings.Join(lines, "\n") + source[end:]
+}
+
+// entSetPartitionClause replaces table's whole partition clause (the dumper's
+// /*!NNNNN PARTITION BY ... */ executable comment, or a plain clause) with newClause
+// (plain form; pass "" to departition). The clause runs from the comment opener (or
+// PARTITION BY) to the end of the statement, so it is rebuilt rather than patched.
+func entSetPartitionClause(t *testing.T, source, table, newClause string) string {
+ t.Helper()
+ start, end := entTableSegment(t, source, table)
+ seg := source[start:end]
+ pIdx := strings.Index(strings.ToUpper(seg), "PARTITION BY")
+ require.GreaterOrEqual(t, pIdx, 0, "table %q has no PARTITION BY clause:\n%s", table, seg)
+ clauseStart := pIdx
+ if c := strings.LastIndex(seg[:pIdx], "/*!"); c >= 0 {
+ clauseStart = c
+ }
+ // The clause (with its optional comment close) runs to the statement end; the
+ // trailing ";" (when in-segment) is preserved.
+ tail := ""
+ rest := strings.TrimRight(seg[clauseStart:], " \t\n")
+ if strings.HasSuffix(rest, ";") {
+ tail = ";"
+ }
+ prefix := strings.TrimRight(seg[:clauseStart], " \t\n")
+ if newClause == "" {
+ return source[:start] + prefix + tail + source[end:]
+ }
+ return source[:start] + prefix + "\n" + newClause + tail + source[end:]
+}
+
+// entAppendTableClause appends clause (e.g. a plain PARTITION BY) to the end of table's
+// CREATE statement, before the trailing semicolon.
+func entAppendTableClause(t *testing.T, source, table, clause string) string {
+ t.Helper()
+ start, end := entTableSegment(t, source, table)
+ seg := source[start:end]
+ trimmed := strings.TrimRight(seg, " \t\n")
+ require.True(t, strings.HasSuffix(trimmed, ";"), "table %q statement must end with ';':\n%s", table, seg)
+ body := strings.TrimSuffix(trimmed, ";")
+ return source[:start] + body + "\n" + clause + ";" + source[end:]
+}
+
+// entUintType returns the canonical dump spelling of INT UNSIGNED on srv. The sync
+// driver's columnTypeCanonicalSynonyms map folds the SIGNED default display widths
+// (int(11) -> int) but not the unsigned ones, so a 5.7 dump renders `int(10) unsigned`
+// verbatim while a plain int renders `int` on both versions (an asymmetry the omni
+// canonicalizer absorbs — recorded as an observation in the campaign findings).
+func entUintType(srv liveServer) string {
+ if srv.version == "5.7" {
+ return "int(10) unsigned"
+ }
+ return "int unsigned"
+}
+
+// ----------------------------------------------------------------------------
+// Baseline: every corpus loads on every version, and its canonical dump is a fixed
+// point — self-diff empty AND the production schema.SDLMigration no-op empty.
+// ----------------------------------------------------------------------------
+
+//nolint:tparallel
+func TestSDLEnterpriseBaseline(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, corpus := range entCorpora() {
+ corpus := corpus
+ t.Run(corpus.name, func(t *testing.T) {
+ dbName := entLoadCorpus(ctx, t, srv, "entsdl_bl_", corpus)
+ meta := syncMetaForDB(ctx, t, srv, dbName)
+ tbl, vw, fn, pr, tg := objectCounts(meta)
+ t.Logf("[BASELINE %s/%s] loaded: tables=%d views=%d functions=%d procedures=%d triggers=%d (db=%s)",
+ srv.name, corpus.name, tbl, vw, fn, pr, tg, dbName)
+
+ source := dumpSDL(ctx, t, srv, dbName)
+ require.NotEmpty(t, source, "[%s/%s] MetadataToSDL produced empty SDL", srv.name, corpus.name)
+
+ selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, selfDiff, "[%s/%s] canonical dump must self-diff empty, got:\n%s",
+ srv.name, corpus.name, selfDiff)
+
+ noop, err := schema.SDLMigration(storepb.Engine_MYSQL, source, meta, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, noop, "[%s/%s] production-path no-op must be empty, got:\n%s",
+ srv.name, corpus.name, noop)
+ })
+ }
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A1: single-object CRUD — for each object kind, one create, one semantic modify, and
+// one drop, chained on a freshly loaded Zabbix core slice (each phase runs the full
+// oracle protocol from a fresh canonical dump).
+// ----------------------------------------------------------------------------
+
+// entA1Aux seeds the slice with one object of each kind that needs a pre-existing
+// instance to modify/drop, plus a RANGE-partitioned table for the partition kind.
+//
+//go:embed testdata/sdl/ent_a1_aux.sql
+var entA1Aux string
+
+// entCRUDPhase is one oracle round (create / modify / drop) within a kind.
+type entCRUDPhase struct {
+ name string
+ mutate func(t *testing.T, srv liveServer, source string) string
+ // want are uppercased substrings the plan must contain.
+ want []string
+ // exactStmts, when > 0, asserts the plan is exactly this many statements
+ // (single-object minimality).
+ exactStmts int
+}
+
+// entCRUDKind is the create+modify+drop triple for one object kind.
+type entCRUDKind struct {
+ name string
+ skip57 string
+ phases []entCRUDPhase
+}
+
+func entCRUDKinds() []entCRUDKind {
+ return []entCRUDKind{
+ {
+ name: "table",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + "\nCREATE TABLE ent_crud_t (\n id bigint unsigned NOT NULL,\n label varchar(40) DEFAULT '' NOT NULL,\n PRIMARY KEY (id)\n) ENGINE=InnoDB;\n"
+ },
+ want: []string{"CREATE TABLE", "ENT_CRUD_T"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "ent_crud_t", ") ENGINE=InnoDB", ") ENGINE=InnoDB COMMENT='ent crud table'")
+ },
+ want: []string{"ENT_CRUD_T", "COMMENT"},
+ exactStmts: 1,
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropTableBlock(t, source, "ent_crud_t")
+ },
+ want: []string{"DROP TABLE", "ENT_CRUD_T"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "column",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addColumnToTable(t, source, "users", "`ent_crud_col` varchar(50) DEFAULT NULL")
+ },
+ want: []string{"ALTER TABLE", "ENT_CRUD_COL"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "users", "`ent_crud_col` varchar(50)", "`ent_crud_col` varchar(120)")
+ },
+ want: []string{"ENT_CRUD_COL", "VARCHAR(120)"},
+ exactStmts: 1,
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropLineInTable(t, source, "users", "`ent_crud_col`")
+ },
+ want: []string{"DROP COLUMN", "ENT_CRUD_COL"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "index_plain",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addIndexToTable(t, source, "users", "KEY `ent_idx_theme` (`theme`)")
+ },
+ want: []string{"ENT_IDX_THEME"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceLineInTable(t, source, "users", "`ent_idx_theme`", "KEY `ent_idx_theme` (`theme`,`lang`)")
+ },
+ want: []string{"ENT_IDX_THEME"},
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropLineInTable(t, source, "users", "`ent_idx_theme`")
+ },
+ want: []string{"ENT_IDX_THEME"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "index_unique",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addIndexToTable(t, source, "users", "UNIQUE KEY `ent_uk_passwd` (`passwd`)")
+ },
+ want: []string{"ENT_UK_PASSWD", "UNIQUE"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceLineInTable(t, source, "users", "`ent_uk_passwd`", "UNIQUE KEY `ent_uk_passwd` (`passwd`,`lang`)")
+ },
+ want: []string{"ENT_UK_PASSWD"},
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropLineInTable(t, source, "users", "`ent_uk_passwd`")
+ },
+ want: []string{"ENT_UK_PASSWD"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "index_prefix",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addIndexToTable(t, source, "hosts", "KEY `ent_idx_desc` (`description`(32))")
+ },
+ want: []string{"ENT_IDX_DESC", "(32)"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceLineInTable(t, source, "hosts", "`ent_idx_desc`", "KEY `ent_idx_desc` (`description`(64))")
+ },
+ want: []string{"ENT_IDX_DESC", "(64)"},
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropLineInTable(t, source, "hosts", "`ent_idx_desc`")
+ },
+ want: []string{"ENT_IDX_DESC"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "index_composite",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addIndexToTable(t, source, "maintenances", "KEY `ent_idx_window` (`active_till`,`maintenance_type`)")
+ },
+ want: []string{"ENT_IDX_WINDOW"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify", // reorder the key parts — a semantic index change
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceLineInTable(t, source, "maintenances", "`ent_idx_window`", "KEY `ent_idx_window` (`maintenance_type`,`active_till`)")
+ },
+ want: []string{"ENT_IDX_WINDOW"},
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropLineInTable(t, source, "maintenances", "`ent_idx_window`")
+ },
+ want: []string{"ENT_IDX_WINDOW"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "foreign_key",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addIndexToTable(t, source, "ent_ranked", "CONSTRAINT `ent_fk_owner` FOREIGN KEY (`owner_userid`) REFERENCES `users` (`userid`) ON DELETE SET NULL")
+ },
+ want: []string{"ENT_FK_OWNER", "FOREIGN KEY"},
+ },
+ {
+ name: "modify", // referential-action change
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "ent_ranked", "ON DELETE SET NULL", "ON DELETE CASCADE")
+ },
+ want: []string{"ENT_FK_OWNER"},
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropLineInTable(t, source, "ent_ranked", "CONSTRAINT `ent_fk_owner`")
+ },
+ want: []string{"ENT_FK_OWNER"},
+ },
+ },
+ },
+ {
+ name: "check",
+ skip57: "5.7 parses-and-ignores CHECK constraints (nothing syncs back)",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addIndexToTable(t, source, "ent_ranked", "CONSTRAINT `ent_chk_score` CHECK ((`score` >= 0))")
+ },
+ want: []string{"ENT_CHK_SCORE", "CHECK"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceLineInTable(t, source, "ent_ranked", "`ent_chk_score`", "CONSTRAINT `ent_chk_score` CHECK ((`score` <= 1000000))")
+ },
+ want: []string{"ENT_CHK_SCORE"},
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropLineInTable(t, source, "ent_ranked", "`ent_chk_score`")
+ },
+ want: []string{"ENT_CHK_SCORE"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "view",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + "\nCREATE VIEW ent_crud_v AS SELECT roleid, name FROM role;\n"
+ },
+ want: []string{"ENT_CRUD_V"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addColumnToView(t, source, "ent_crud_v", "`role`.`type` AS `type`")
+ },
+ want: []string{"ENT_CRUD_V"},
+ exactStmts: 1,
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropObjectBlock(t, source, "VIEW", "ent_crud_v")
+ },
+ want: []string{"DROP VIEW", "ENT_CRUD_V"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "function",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + "\nCREATE FUNCTION ent_crud_f() RETURNS INT DETERMINISTIC RETURN 41;\n"
+ },
+ want: []string{"ENT_CRUD_F"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify", // body change — this omni build renders it as DROP + CREATE
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return mustReplace(t, source, "RETURN 41", "RETURN 42")
+ },
+ want: []string{"ENT_CRUD_F"},
+ exactStmts: 2,
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropObjectBlock(t, source, "FUNCTION", "ent_crud_f")
+ },
+ want: []string{"DROP FUNCTION", "ENT_CRUD_F"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "procedure",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + "\nCREATE PROCEDURE ent_crud_p(IN rid BIGINT UNSIGNED) BEGIN UPDATE role SET readonly = readonly WHERE roleid = rid; END;\n"
+ },
+ want: []string{"ENT_CRUD_P"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return mustReplace(t, source, "SET readonly = readonly", "SET readonly = 0")
+ },
+ want: []string{"ENT_CRUD_P"},
+ exactStmts: 2,
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropObjectBlock(t, source, "PROCEDURE", "ent_crud_p")
+ },
+ want: []string{"DROP PROCEDURE", "ENT_CRUD_P"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "trigger",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + "\nCREATE TRIGGER ent_crud_trg BEFORE UPDATE ON media_type FOR EACH ROW SET NEW.name = NEW.name;\n"
+ },
+ want: []string{"ENT_CRUD_TRG"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return mustReplace(t, source, "SET NEW.name = NEW.name", "SET NEW.name = TRIM(NEW.name)")
+ },
+ want: []string{"ENT_CRUD_TRG"},
+ exactStmts: 2,
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return dropTrigger(t, source, "ent_crud_trg")
+ },
+ want: []string{"DROP TRIGGER", "ENT_CRUD_TRG"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ name: "event",
+ phases: []entCRUDPhase{
+ {
+ name: "create",
+ mutate: func(_ *testing.T, _ liveServer, source string) string {
+ return source + "\nCREATE EVENT ent_crud_ev ON SCHEDULE EVERY 12 HOUR DO DELETE FROM changelog WHERE clock < 100;\n"
+ },
+ want: []string{"ENT_CRUD_EV"},
+ exactStmts: 1,
+ },
+ {
+ name: "modify",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return mustReplace(t, source, "EVERY 12 HOUR", "EVERY 6 HOUR")
+ },
+ want: []string{"ENT_CRUD_EV"},
+ },
+ {
+ name: "drop",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entDropObjectNamed(t, source, "EVENT", "ent_crud_ev")
+ },
+ want: []string{"DROP EVENT", "ENT_CRUD_EV"},
+ exactStmts: 1,
+ },
+ },
+ },
+ {
+ // NOTE (observation, not a failure): for every partition change the differ
+ // emits a full `ALTER TABLE ... PARTITION BY` REPARTITION statement rather
+ // than the targeted ADD/DROP/REORGANIZE PARTITION — semantically correct and
+ // converging, but a whole-table rebuild on large enterprise tables. See the
+ // campaign findings.
+ name: "partition",
+ phases: []entCRUDPhase{
+ {
+ name: "create", // add a partition
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entSetPartitionClause(t, source, "ent_part_log",
+ "PARTITION BY RANGE (`bucket`)\n(PARTITION p0 VALUES LESS THAN (100),\n PARTITION p1 VALUES LESS THAN (200),\n PARTITION p2 VALUES LESS THAN (300))")
+ },
+ want: []string{"PARTITION BY", "P2"},
+ },
+ {
+ name: "modify", // reorganize: merge p0+p1 into p01
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entSetPartitionClause(t, source, "ent_part_log",
+ "PARTITION BY RANGE (`bucket`)\n(PARTITION p01 VALUES LESS THAN (200),\n PARTITION p2 VALUES LESS THAN (300))")
+ },
+ want: []string{"PARTITION BY", "P01"},
+ },
+ {
+ name: "drop", // drop the p2 partition (the repartition plan re-lists survivors)
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entSetPartitionClause(t, source, "ent_part_log",
+ "PARTITION BY RANGE (`bucket`)\n(PARTITION p01 VALUES LESS THAN (200))")
+ },
+ want: []string{"PARTITION BY", "P01"},
+ },
+ },
+ },
+ }
+}
+
+//nolint:tparallel
+func TestSDLEnterpriseSingleObjectCRUD(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, kind := range entCRUDKinds() {
+ kind := kind
+ t.Run(kind.name, func(t *testing.T) {
+ if srv.version == "5.7" && kind.skip57 != "" {
+ t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, kind.name, kind.skip57)
+ }
+ base := entZabbixSlice(t, entSliceCoreTables...) + entA1Aux
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a1", base)
+
+ for _, phase := range kind.phases {
+ label := srv.name + "/A1/" + kind.name + "/" + phase.name
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := phase.mutate(t, srv, source)
+ plan := entOracle(ctx, t, srv, dbName, target, label)
+
+ upper := strings.ToUpper(plan)
+ for _, want := range phase.want {
+ require.Contains(t, upper, want, "[%s] plan missing %q:\n%s", label, want, plan)
+ }
+ if phase.exactStmts > 0 {
+ require.Equal(t, phase.exactStmts, entPlanStatementCount(t, plan),
+ "[%s] plan not minimal (%d stmts expected):\n%s", label, phase.exactStmts, plan)
+ }
+ }
+ })
+ }
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A2: dependent objects — the enterprise differentiator. FK chains, trigger→table
+// dependencies (Zabbix changelog pattern), view-on-view stacks, function-used-by-view,
+// and a circular FK pair, each as one or more oracle rounds on the core slice.
+// ----------------------------------------------------------------------------
+
+//nolint:tparallel
+func TestSDLEnterpriseDependentObjects(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ t.Run("fk_chain_extend", func(t *testing.T) {
+ // Extend the real item_tag -> items -> hosts -> proxy -> proxy_group chain
+ // with two new tables in ONE release: ent_item_note -> items and
+ // ent_note_tag -> ent_item_note. Creation must be topological.
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := source + `
+CREATE TABLE ent_item_note (
+ noteid bigint unsigned NOT NULL,
+ itemid bigint unsigned NOT NULL,
+ note varchar(255) DEFAULT '' NOT NULL,
+ PRIMARY KEY (noteid),
+ KEY ent_item_note_1 (itemid),
+ CONSTRAINT ent_c_item_note_1 FOREIGN KEY (itemid) REFERENCES items (itemid) ON DELETE CASCADE
+) ENGINE=InnoDB;
+CREATE TABLE ent_note_tag (
+ notetagid bigint unsigned NOT NULL,
+ noteid bigint unsigned NOT NULL,
+ tag varchar(64) DEFAULT '' NOT NULL,
+ PRIMARY KEY (notetagid),
+ KEY ent_note_tag_1 (noteid),
+ CONSTRAINT ent_c_note_tag_1 FOREIGN KEY (noteid) REFERENCES ent_item_note (noteid) ON DELETE CASCADE
+) ENGINE=InnoDB;
+`
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/fk_chain_extend")
+ upper := strings.ToUpper(plan)
+ noteIdx := strings.Index(upper, "CREATE TABLE `ENT_ITEM_NOTE`")
+ tagIdx := strings.Index(upper, "CREATE TABLE `ENT_NOTE_TAG`")
+ require.GreaterOrEqual(t, noteIdx, 0, "plan must create ent_item_note:\n%s", plan)
+ require.GreaterOrEqual(t, tagIdx, 0, "plan must create ent_note_tag:\n%s", plan)
+ })
+
+ t.Run("fk_chain_drop_mid", func(t *testing.T) {
+ // Drop the middle of the chain — items — together with its dependents
+ // item_tag and item_preproc (each carrying 3 changelog triggers) in ONE
+ // release. The plan must drop FKs/tables in dependency order and must not
+ // drop a trigger after its table is already gone.
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := source
+ for _, trg := range []string{
+ "items_insert", "items_update", "items_delete",
+ "item_tag_insert", "item_tag_update", "item_tag_delete",
+ "item_preproc_insert", "item_preproc_update", "item_preproc_delete",
+ } {
+ target = dropTrigger(t, target, trg)
+ }
+ for _, tbl := range []string{"item_tag", "item_preproc", "items"} {
+ target = entDropTableBlock(t, target, tbl)
+ }
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/fk_chain_drop_mid")
+ upper := strings.ToUpper(plan)
+ for _, want := range []string{"`ITEMS`", "`ITEM_TAG`", "`ITEM_PREPROC`"} {
+ require.Contains(t, upper, want, "plan must drop %s:\n%s", want, plan)
+ }
+ })
+
+ t.Run("trigger_table_widen", func(t *testing.T) {
+ // The Zabbix changelog pattern: hosts carries 5 triggers, two of which
+ // (hosts_name_upper_*) read/write hosts.name and hosts.name_upper. Widen
+ // BOTH columns in one release; the triggers must survive the ALTERs.
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := entReplaceInTable(t, source, "hosts", "`name` varchar(128)", "`name` varchar(190)")
+ target = entReplaceInTable(t, target, "hosts", "`name_upper` varchar(128)", "`name_upper` varchar(190)")
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/trigger_table_widen")
+ upper := strings.ToUpper(plan)
+ require.Contains(t, upper, "VARCHAR(190)", "plan must widen the columns:\n%s", plan)
+ require.NotContains(t, upper, "DROP TABLE", "widening must not recreate hosts:\n%s", plan)
+ })
+
+ t.Run("trigger_modify_one_of_five", func(t *testing.T) {
+ // Redefine ONE of hosts' five triggers (hosts_update) and prove the other
+ // four are untouched by the plan.
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := mustReplace(t, source, "values (1,old.hostid,2,", "values (1,new.hostid,2,")
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/trigger_modify_one_of_five")
+ upper := strings.ToUpper(plan)
+ require.Contains(t, upper, "HOSTS_UPDATE", "plan must redefine hosts_update:\n%s", plan)
+ for _, untouched := range []string{"HOSTS_INSERT", "HOSTS_DELETE", "HOSTS_NAME_UPPER_INSERT", "HOSTS_NAME_UPPER_UPDATE"} {
+ require.NotContains(t, upper, untouched, "plan must not touch %s:\n%s", untouched, plan)
+ }
+ require.Equal(t, 2, entPlanStatementCount(t, plan), "trigger redefine should be DROP+CREATE:\n%s", plan)
+ })
+
+ t.Run("view_stack", func(t *testing.T) {
+ // 3-deep view stack v3 -> v2 -> v1 -> users: create the whole stack in one
+ // release, modify the MIDDLE view, then drop the whole stack.
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
+
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := source + `
+CREATE VIEW ent_vs1 AS SELECT userid, username, surname FROM users;
+CREATE VIEW ent_vs2 AS SELECT userid, username FROM ent_vs1;
+CREATE VIEW ent_vs3 AS SELECT userid FROM ent_vs2;
+`
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/view_stack/create")
+ upper := strings.ToUpper(plan)
+ v1 := strings.Index(upper, "`ENT_VS1`")
+ v2 := strings.Index(upper, "`ENT_VS2`")
+ v3 := strings.Index(upper, "`ENT_VS3`")
+ require.True(t, v1 >= 0 && v2 >= 0 && v3 >= 0, "plan must create all three views:\n%s", plan)
+ require.True(t, v1 < v2 && v2 < v3, "views must be created base-first (v1<v2<v3):\n%s", plan)
+
+ source = dumpSDL(ctx, t, srv, dbName)
+ target = addColumnToView(t, source, "ent_vs2", "`ent_vs1`.`surname` AS `surname`")
+ plan = entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/view_stack/modify_middle")
+ upper = strings.ToUpper(plan)
+ require.Contains(t, upper, "VIEW `ENT_VS2`")
+ require.NotContains(t, upper, "ENT_VS3", "modifying v2 must not churn v3:\n%s", plan)
+ // v2's body legitimately references ent_vs1 in FROM; minimality is proven
+ // by the single-statement plan (v1 and v3 are untouched).
+ require.Equal(t, 1, entPlanStatementCount(t, plan), "modifying v2 must be a single statement:\n%s", plan)
+
+ source = dumpSDL(ctx, t, srv, dbName)
+ target = dropObjectBlock(t, source, "VIEW", "ent_vs3")
+ target = dropObjectBlock(t, target, "VIEW", "ent_vs2")
+ target = dropObjectBlock(t, target, "VIEW", "ent_vs1")
+ plan = entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/view_stack/drop")
+ require.Equal(t, 3, entPlanStatementCount(t, plan), "stack drop should be exactly 3 DROP VIEW:\n%s", plan)
+ })
+
+ t.Run("function_used_by_view", func(t *testing.T) {
+ // Create a function and a view CALLING it in one release (MySQL rejects a
+ // view over a missing function, so creation order is enforced by apply);
+ // then drop both in one release.
+ //
+ // Was PINNED-BUG (view created before the function it calls → Error 1305):
+ // fixed by omni #361 (routine creates precede view creates; drops inverse).
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
+
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := source + `
+CREATE FUNCTION ent_ucount() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM users);
+CREATE VIEW ent_v_fn AS SELECT ent_ucount() AS n;
+`
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/function_used_by_view/create")
+ upper := strings.ToUpper(plan)
+ require.Contains(t, upper, "ENT_UCOUNT")
+ require.Contains(t, upper, "ENT_V_FN")
+
+ source = dumpSDL(ctx, t, srv, dbName)
+ target = dropObjectBlock(t, source, "VIEW", "ent_v_fn")
+ target = dropObjectBlock(t, target, "FUNCTION", "ent_ucount")
+ plan = entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/function_used_by_view/drop")
+ require.Equal(t, 2, entPlanStatementCount(t, plan), "should be exactly DROP VIEW + DROP FUNCTION:\n%s", plan)
+ })
+
+ t.Run("circular_fk_pair", func(t *testing.T) {
+ // Create two mutually-referencing tables in ONE release. The generated DDL
+ // must break the cycle itself (deferred ALTER or FK-checks toggling) — the
+ // apply connection runs with default settings.
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a2", entZabbixSlice(t, entSliceCoreTables...))
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := source + `
+CREATE TABLE ent_circ_a (
+ aid bigint unsigned NOT NULL,
+ bid bigint unsigned NULL,
+ PRIMARY KEY (aid),
+ KEY ent_circ_a_1 (bid),
+ CONSTRAINT ent_c_circ_a FOREIGN KEY (bid) REFERENCES ent_circ_b (bid) ON DELETE SET NULL
+) ENGINE=InnoDB;
+CREATE TABLE ent_circ_b (
+ bid bigint unsigned NOT NULL,
+ aid bigint unsigned NULL,
+ PRIMARY KEY (bid),
+ KEY ent_circ_b_1 (aid),
+ CONSTRAINT ent_c_circ_b FOREIGN KEY (aid) REFERENCES ent_circ_a (aid) ON DELETE SET NULL
+) ENGINE=InnoDB;
+`
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A2/circular_fk_pair")
+ upper := strings.ToUpper(plan)
+ require.Contains(t, upper, "ENT_CIRC_A")
+ require.Contains(t, upper, "ENT_CIRC_B")
+ })
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A3: special types — AUTO_INCREMENT, generated columns, ENUM/SET members,
+// charset/collation, temporal precision, DECIMAL, TEXT/BLOB families, signedness,
+// prefix-indexed TEXT, and partition operations. Small synthetic bases, one oracle
+// round each.
+// ----------------------------------------------------------------------------
+
+type entTypeCase struct {
+ name string
+ base string
+ skip57 string
+ // pinned, when non-empty, skips the case with a PINNED-BUG classification (a
+ // confirmed engine bug being fixed upstream; the case re-arms when the skip drops).
+ pinned string
+ mutate func(t *testing.T, srv liveServer, source string) string
+ want []string
+ // exactStmts, when > 0, asserts the plan statement count.
+ exactStmts int
+}
+
+func entTypeCases() []entTypeCase {
+ return []entTypeCase{
+ {
+ // REPRO: add `seq bigint unsigned NOT NULL AUTO_INCREMENT` + UNIQUE KEY uk_seq
+ // (seq) in one release. The plan emits TWO statements — ADD COLUMN, then ADD
+ // UNIQUE KEY — and MySQL rejects the first with Error 1075 (an auto column
+ // must be defined as a key). The clauses must be grouped into one ALTER.
+ name: "ai_add_column",
+ base: "CREATE TABLE t3 (id int NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ // Was PINNED-BUG (AI column and its key split into separate ALTERs →
+ // Error 1075): fixed by omni #364 (mergeAutoIncrementKeyOps grouping).
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := addColumnToTable(t, source, "t3", "`seq` bigint unsigned NOT NULL AUTO_INCREMENT")
+ return addIndexToTable(t, s, "t3", "UNIQUE KEY `uk_seq` (`seq`)")
+ },
+ want: []string{"SEQ", "AUTO_INCREMENT"},
+ },
+ {
+ name: "ai_drop_attribute",
+ base: "CREATE TABLE t3 (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3",
+ "`id` int NOT NULL AUTO_INCREMENT", "`id` int NOT NULL")
+ },
+ want: []string{"`ID`"},
+ exactStmts: 1,
+ },
+ {
+ name: "ai_composite_pk_member",
+ base: "CREATE TABLE t3 (a int NOT NULL, b int NOT NULL AUTO_INCREMENT, PRIMARY KEY (a,b), KEY idx_b (b)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3",
+ "`b` int NOT NULL AUTO_INCREMENT", "`b` bigint NOT NULL AUTO_INCREMENT")
+ },
+ want: []string{"BIGINT", "AUTO_INCREMENT"},
+ exactStmts: 1,
+ },
+ {
+ name: "generated_stored_add",
+ base: "CREATE TABLE t3 (id int NOT NULL, price decimal(8,2) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addColumnToTable(t, source, "t3", "`total` decimal(10,2) GENERATED ALWAYS AS ((`price` * 1.1)) STORED")
+ },
+ want: []string{"TOTAL", "STORED"},
+ exactStmts: 1,
+ },
+ {
+ name: "generated_virtual_add",
+ base: "CREATE TABLE t3 (id int NOT NULL, price decimal(8,2) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return addColumnToTable(t, source, "t3", "`total_v` decimal(10,2) GENERATED ALWAYS AS ((`price` * 2)) VIRTUAL")
+ },
+ want: []string{"TOTAL_V"},
+ exactStmts: 1,
+ },
+ {
+ name: "generated_dependent_alter",
+ // Widen the BASE column and change the generated column depending on it in the
+ // SAME release — the plan must sequence the two ALTERs so MySQL accepts them.
+ base: "CREATE TABLE t3 (id int NOT NULL, price decimal(8,2) NOT NULL, total decimal(10,2) GENERATED ALWAYS AS (price * 2) STORED, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := entReplaceInTable(t, source, "t3", "`price` decimal(8,2)", "`price` decimal(10,2)")
+ return entReplaceInTable(t, s, "t3", "* 2", "* 3")
+ },
+ want: []string{"PRICE"},
+ },
+ {
+ name: "enum_member_add",
+ base: "CREATE TABLE t3 (id int NOT NULL, kind enum('a','b') NOT NULL DEFAULT 'a', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3", "enum('a','b')", "enum('a','b','c')")
+ },
+ want: []string{"ENUM"},
+ exactStmts: 1,
+ },
+ {
+ name: "enum_member_remove",
+ base: "CREATE TABLE t3 (id int NOT NULL, kind enum('a','b','c') NOT NULL DEFAULT 'a', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3", "enum('a','b','c')", "enum('a','b')")
+ },
+ want: []string{"ENUM"},
+ exactStmts: 1,
+ },
+ {
+ name: "enum_member_reorder",
+ base: "CREATE TABLE t3 (id int NOT NULL, kind enum('a','b','c') NOT NULL DEFAULT 'a', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3", "enum('a','b','c')", "enum('c','a','b')")
+ },
+ want: []string{"ENUM"},
+ exactStmts: 1,
+ },
+ {
+ name: "set_member_add_remove",
+ base: "CREATE TABLE t3 (id int NOT NULL, tags set('x','y') NOT NULL DEFAULT '', flags set('p','q','r') NOT NULL DEFAULT '', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := entReplaceInTable(t, source, "t3", "set('x','y')", "set('x','y','z')")
+ return entReplaceInTable(t, s, "t3", "set('p','q','r')", "set('p','q')")
+ },
+ want: []string{"SET("},
+ },
+ {
+ // utf8mb4_unicode_ci is non-default on BOTH versions (8.0 default 0900_ai_ci,
+ // 5.7 default general_ci), so the dump renders the explicit column COLLATE on
+ // both — a stable anchor. When the collation matches the table default the
+ // dump omits CHARACTER SET/COLLATE entirely.
+ name: "charset_column_collation",
+ base: "CREATE TABLE t3 (id int NOT NULL, label varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3",
+ "`label` varchar(80) COLLATE utf8mb4_unicode_ci",
+ "`label` varchar(80) COLLATE utf8mb4_bin")
+ },
+ want: []string{"UTF8MB4_BIN"},
+ exactStmts: 1,
+ },
+ {
+ name: "charset_table_level",
+ base: "CREATE TABLE t3 (id int NOT NULL, title varchar(60) NOT NULL, body varchar(200) DEFAULT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=latin1;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ // Replace the whole table-options line so the (version-dependent) COLLATE
+ // suffix can never survive as an invalid latin1/utf8mb4 hybrid. The options
+ // line is the segment's last line, so everything from ') ENGINE=InnoDB' to
+ // the segment end is rewritten.
+ start, end := entTableSegment(t, source, "t3")
+ seg := source[start:end]
+ optIdx := strings.Index(seg, ") ENGINE=InnoDB")
+ require.GreaterOrEqual(t, optIdx, 0, "t3 options line not found:\n%s", seg)
+ tail := ""
+ if nl := strings.Index(seg[optIdx:], "\n"); nl >= 0 {
+ tail = seg[optIdx+nl:]
+ }
+ newSeg := seg[:optIdx] + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" + tail
+ return source[:start] + newSeg + source[end:]
+ },
+ want: []string{"UTF8MB4"},
+ },
+ {
+ name: "temporal_precision",
+ base: "CREATE TABLE t3 (id int NOT NULL, ts timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), dt datetime NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := entReplaceAllInTable(t, source, "t3", "timestamp(3)", "timestamp(6)")
+ s = entReplaceAllInTable(t, s, "t3", "CURRENT_TIMESTAMP(3)", "CURRENT_TIMESTAMP(6)")
+ return entReplaceInTable(t, s, "t3", "`dt` datetime ", "`dt` datetime(3) ")
+ },
+ want: []string{"TIMESTAMP(6)", "DATETIME(3)"},
+ },
+ {
+ name: "decimal_widen",
+ base: "CREATE TABLE t3 (id int NOT NULL, amount decimal(10,2) NOT NULL DEFAULT '0.00', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3", "decimal(10,2)", "decimal(12,4)")
+ },
+ want: []string{"DECIMAL(12,4)"},
+ exactStmts: 1,
+ },
+ {
+ name: "decimal_narrow",
+ base: "CREATE TABLE t3 (id int NOT NULL, amount decimal(12,4) NOT NULL DEFAULT '0.0000', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := entReplaceInTable(t, source, "t3", "decimal(12,4)", "decimal(10,2)")
+ return entReplaceInTable(t, s, "t3", "'0.0000'", "'0.00'")
+ },
+ want: []string{"DECIMAL(10,2)"},
+ exactStmts: 1,
+ },
+ {
+ name: "text_blob_family",
+ base: "CREATE TABLE t3 (id int NOT NULL, body text, payload blob, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ s := entReplaceInTable(t, source, "t3", "`body` text", "`body` mediumtext")
+ return entReplaceInTable(t, s, "t3", "`payload` blob", "`payload` longblob")
+ },
+ want: []string{"MEDIUMTEXT", "LONGBLOB"},
+ },
+ {
+ name: "unsigned_signed_flip",
+ base: "CREATE TABLE t3 (id int NOT NULL, cnt int unsigned NOT NULL DEFAULT '0', pos int NOT NULL DEFAULT '0', PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, srv liveServer, source string) string {
+ s := entReplaceInTable(t, source, "t3", "`cnt` "+entUintType(srv), "`cnt` int")
+ return entReplaceInTable(t, s, "t3", "`pos` int", "`pos` "+entUintType(srv))
+ },
+ want: []string{"`CNT`", "`POS`"},
+ },
+ {
+ name: "prefix_indexed_text_type_change",
+ base: "CREATE TABLE t3 (id int NOT NULL, body text NOT NULL, PRIMARY KEY (id), KEY idx_body (body(24))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entReplaceInTable(t, source, "t3", "`body` text", "`body` mediumtext")
+ },
+ want: []string{"MEDIUMTEXT"},
+ },
+ {
+ name: "partition_add",
+ base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY RANGE (bucket) (PARTITION p0 VALUES LESS THAN (100), PARTITION p1 VALUES LESS THAN (200));",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entSetPartitionClause(t, source, "t3",
+ "PARTITION BY RANGE (`bucket`)\n(PARTITION p0 VALUES LESS THAN (100),\n PARTITION p1 VALUES LESS THAN (200),\n PARTITION p2 VALUES LESS THAN (300))")
+ },
+ want: []string{"P2"},
+ },
+ {
+ name: "partition_drop",
+ base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY RANGE (bucket) (PARTITION p0 VALUES LESS THAN (100), PARTITION p1 VALUES LESS THAN (200), PARTITION p2 VALUES LESS THAN (300));",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entSetPartitionClause(t, source, "t3",
+ "PARTITION BY RANGE (`bucket`)\n(PARTITION p0 VALUES LESS THAN (100),\n PARTITION p1 VALUES LESS THAN (200))")
+ },
+ // The differ emits a full repartition listing only the survivors (see the
+ // partition-minimality observation in the campaign findings).
+ want: []string{"PARTITION BY", "P0"},
+ },
+ {
+ name: "partition_reorganize",
+ base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY RANGE (bucket) (PARTITION p0 VALUES LESS THAN (100), PARTITION p1 VALUES LESS THAN (200), PARTITION pmax VALUES LESS THAN MAXVALUE);",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entSetPartitionClause(t, source, "t3",
+ "PARTITION BY RANGE (`bucket`)\n(PARTITION p01 VALUES LESS THAN (200),\n PARTITION pmax VALUES LESS THAN MAXVALUE)")
+ },
+ want: []string{"P01"},
+ },
+ {
+ name: "partition_existing_table",
+ base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entAppendTableClause(t, source, "t3", "PARTITION BY HASH (`bucket`)\nPARTITIONS 4")
+ },
+ want: []string{"PARTITION BY", "HASH"},
+ },
+ {
+ name: "departition",
+ base: "CREATE TABLE t3 (id int NOT NULL, bucket int NOT NULL, PRIMARY KEY (id,bucket)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY HASH (bucket) PARTITIONS 4;",
+ mutate: func(t *testing.T, _ liveServer, source string) string {
+ return entSetPartitionClause(t, source, "t3", "")
+ },
+ want: []string{"T3"},
+ },
+ }
+}
+
+//nolint:tparallel
+func TestSDLEnterpriseSpecialTypes(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, tc := range entTypeCases() {
+ tc := tc
+ t.Run(tc.name, func(t *testing.T) {
+ if srv.version == "5.7" && tc.skip57 != "" {
+ t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, tc.name, tc.skip57)
+ }
+ if tc.pinned != "" {
+ t.Skip(tc.pinned)
+ }
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a3", tc.base)
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := tc.mutate(t, srv, source)
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A3/"+tc.name)
+
+ upper := strings.ToUpper(plan)
+ for _, want := range tc.want {
+ require.Contains(t, upper, want, "[%s/A3/%s] plan missing %q:\n%s", srv.name, tc.name, want, plan)
+ }
+ if tc.exactStmts > 0 {
+ require.Equal(t, tc.exactStmts, entPlanStatementCount(t, plan),
+ "[%s/A3/%s] plan not minimal:\n%s", srv.name, tc.name, plan)
+ }
+ })
+ }
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A5(a): sequential chain S0 -> S1 -> S2 -> S3 -> S4 on the core slice. Every step must
+// converge, and the endpoint must be REACHABLE FROM S0 IN ONE SHOT on a second database
+// — both databases must dump to the same canonical schema.
+// ----------------------------------------------------------------------------
+
+//nolint:tparallel
+func TestSDLEnterpriseSequentialChain(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ base := entZabbixSlice(t, entSliceCoreTables...)
+ chainDB := entLoadDDL(ctx, t, srv, "entsdl_a5chain", base)
+
+ // The chain targets are built by cumulative TEXTUAL mutation of the canonical
+ // S0 dump, so the final text is identical for the chained and one-shot legs.
+ s0 := dumpSDL(ctx, t, srv, chainDB)
+
+ // The appended blocks use backticked names so the later chain steps can locate
+ // and mutate them with the same segment helpers that work on canonical dumps.
+ s1 := addColumnToTable(t, s0, "users", "`ent_ch_flag` int NOT NULL DEFAULT '0'")
+ s1 += `
+CREATE TABLE ` + "`ent_ch_audit`" + ` (
+ auditid bigint unsigned NOT NULL,
+ userid bigint unsigned NULL,
+ note varchar(60) DEFAULT '' NOT NULL,
+ PRIMARY KEY (auditid)
+) ENGINE=InnoDB;
+`
+ s2 := addIndexToTable(t, s1, "users", "KEY `ent_ch_idx` (`ent_ch_flag`)")
+ s2 += `
+CREATE VIEW ` + "`ent_ch_v`" + ` AS SELECT auditid, note FROM ent_ch_audit;
+`
+ s3 := addIndexToTable(t, s2, "ent_ch_audit", "KEY `ent_ch_fk` (`userid`)")
+ s3 = addIndexToTable(t, s3, "ent_ch_audit", "CONSTRAINT `ent_ch_fk` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON DELETE SET NULL")
+ s3 = entReplaceInTable(t, s3, "maintenances", "`name` varchar(128)", "`name` varchar(190)")
+ s3 += `
+CREATE TRIGGER ent_ch_trg BEFORE INSERT ON ent_ch_audit FOR EACH ROW SET NEW.note = TRIM(NEW.note);
+`
+ s4 := entDropLineInTable(t, s3, "users", "`ent_ch_idx`")
+ s4 = addColumnToView(t, s4, "ent_ch_v", "`ent_ch_audit`.`userid` AS `userid`")
+ s4 += `
+CREATE PROCEDURE ent_ch_p(IN aid BIGINT UNSIGNED) BEGIN DELETE FROM ent_ch_audit WHERE auditid = aid; END;
+`
+ for i, step := range []string{s1, s2, s3, s4} {
+ entOracle(ctx, t, srv, chainDB, step, srv.name+"/A5/chain/S"+string(rune('1'+i)))
+ }
+
+ // One-shot leg: a second fresh database goes S0 -> S4 in a single release.
+ oneshotDB := entLoadDDL(ctx, t, srv, "entsdl_a5one", base)
+ entOracle(ctx, t, srv, oneshotDB, s4, srv.name+"/A5/chain/oneshot")
+
+ // Same endpoint: the two databases' canonical dumps must be equivalent.
+ chainDump := dumpSDL(ctx, t, srv, chainDB)
+ oneshotDump := dumpSDL(ctx, t, srv, oneshotDB)
+ diffAB, err := mysqlDiffSDLMigration(chainDump, oneshotDump, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, diffAB, "chained and one-shot endpoints diverge (chain->oneshot):\n%s", diffAB)
+ diffBA, err := mysqlDiffSDLMigration(oneshotDump, chainDump, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, diffBA, "chained and one-shot endpoints diverge (oneshot->chain):\n%s", diffBA)
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A5(b): apply-back — migrate B -> T (destructive + additive mix), then T -> B using the
+// ORIGINAL canonical dump as the target; the database must land exactly back on B.
+// ----------------------------------------------------------------------------
+
+//nolint:tparallel
+func TestSDLEnterpriseApplyBack(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a5back", entZabbixSlice(t, entSliceCoreTables...))
+ c0 := dumpSDL(ctx, t, srv, dbName)
+
+ // Forward: drop a plain index, drop an unindexed column, widen a column, and
+ // add a table — a realistic mixed release.
+ target := entDropLineInTable(t, c0, "users", "`users_2`")
+ target = entDropLineInTable(t, target, "users", "`attempt_ip`")
+ target = entReplaceInTable(t, target, "media_type", "`smtp_helo` varchar(255)", "`smtp_helo` varchar(300)")
+ target += `
+CREATE TABLE ent_ab_t (
+ id bigint unsigned NOT NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB;
+`
+ entOracle(ctx, t, srv, dbName, target, srv.name+"/A5/apply_back/forward")
+
+ // Backward: the original canonical dump is the target again.
+ entOracle(ctx, t, srv, dbName, c0, srv.name+"/A5/apply_back/backward")
+
+ // The database must be exactly B again (canonical-dump equivalence).
+ final := dumpSDL(ctx, t, srv, dbName)
+ backDiff, err := mysqlDiffSDLMigration(final, c0, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, backDiff, "apply-back did not restore B:\n%s", backDiff)
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A5(c): combined release — table + column + index + FK + view + trigger + routine all
+// changed in ONE diff.
+// ----------------------------------------------------------------------------
+
+// entA5cAux seeds the view/function the combined release modifies.
+//
+//go:embed testdata/sdl/ent_a5c_aux.sql
+var entA5cAux string
+
+//nolint:tparallel
+func TestSDLEnterpriseCombinedRelease(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ base := entZabbixSlice(t, entSliceCoreTables...) + entA5cAux
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_a5cmb", base)
+ source := dumpSDL(ctx, t, srv, dbName)
+
+ // One release, seven object kinds:
+ target := addColumnToTable(t, source, "media_type", "`ent_cmb_col` varchar(32) DEFAULT NULL") // column
+ target = addIndexToTable(t, target, "users", "KEY `ent_cmb_idx` (`theme`)") // index
+ target = addColumnToTable(t, target, "dchecks", "`ent_cmb_ref` bigint unsigned NULL") // FK column +
+ target = addIndexToTable(t, target, "dchecks", "KEY `ent_cmb_fk` (`ent_cmb_ref`)")
+ target = addIndexToTable(t, target, "dchecks", "CONSTRAINT `ent_cmb_fk` FOREIGN KEY (`ent_cmb_ref`) REFERENCES `connector` (`connectorid`) ON DELETE SET NULL") // FK
+ target = addColumnToView(t, target, "ent_cmb_v", "`role`.`type` AS `type`") // view
+ target = mustReplace(t, target, "RETURN 7", "RETURN 8") // routine
+ target += `
+CREATE TABLE ent_cmb_t (
+ id bigint unsigned NOT NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB;
+CREATE TRIGGER ent_cmb_trg BEFORE UPDATE ON maintenances FOR EACH ROW SET NEW.name = TRIM(NEW.name);
+`
+ plan := entOracle(ctx, t, srv, dbName, target, srv.name+"/A5/combined_release")
+ upper := strings.ToUpper(plan)
+ for _, want := range []string{
+ "ENT_CMB_COL", "ENT_CMB_IDX", "ENT_CMB_REF", "ENT_CMB_FK",
+ "ENT_CMB_V", "ENT_CMB_F", "ENT_CMB_T", "ENT_CMB_TRG",
+ } {
+ require.Contains(t, upper, want, "combined release plan missing %q:\n%s", want, plan)
+ }
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A5(e): scale timing guard on the largest corpus (OpenEMR, 283 tables) — the no-op
+// diff must stay under 30s, and a single-column change must be exactly one ALTER,
+// also computed under 30s.
+// ----------------------------------------------------------------------------
+
+//nolint:tparallel
+func TestSDLEnterpriseScaleGuard(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+ const budget = 30 * time.Second
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ dbName := entLoadDDL(ctx, t, srv, "entsdl_scale", entOpenemrSQL)
+ source := dumpSDL(ctx, t, srv, dbName)
+
+ startNoop := time.Now()
+ selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
+ noopDur := time.Since(startNoop)
+ require.NoError(t, err)
+ require.Empty(t, selfDiff, "openemr no-op diff must be empty:\n%s", selfDiff)
+ t.Logf("[SCALE %s] openemr no-op diff: %v (budget %v)", srv.name, noopDur, budget)
+ require.Less(t, noopDur, budget, "openemr no-op diff exceeded the %v budget", budget)
+
+ // Single-column change: exactly one ALTER, computed within budget.
+ target := addColumnToTable(t, source, "patient_data", "`ent_scale_col` varchar(40) DEFAULT NULL")
+ startOne := time.Now()
+ plan, err := mysqlDiffSDLMigration(source, target, srv.version)
+ oneDur := time.Since(startOne)
+ require.NoError(t, err)
+ t.Logf("[SCALE %s] openemr single-column diff: %v, plan:\n%s", srv.name, oneDur, plan)
+ require.Less(t, oneDur, budget, "openemr single-column diff exceeded the %v budget", budget)
+ require.Equal(t, 1, entPlanStatementCount(t, plan), "single-column change must be exactly 1 statement:\n%s", plan)
+ upper := strings.ToUpper(plan)
+ require.Contains(t, upper, "ALTER TABLE")
+ require.Contains(t, upper, "ENT_SCALE_COL")
+
+ // Close the loop: apply + converge (the full oracle contract at scale).
+ require.NoError(t, applyDDL(ctx, t, srv, dbName, plan), "scale plan failed to apply:\n%s", plan)
+ after := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(after, target, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "openemr single-column change did not converge:\n%s", converge)
+ })
+ }
+}
+
+// ----------------------------------------------------------------------------
+// A5(f): multi-file export round-trip at enterprise scale — for every corpus, the
+// concatenated GetMultiFileDatabaseDefinition output must describe the identical schema
+// as the single-file dump (diff empty both ways) and must be self-idempotent.
+// ----------------------------------------------------------------------------
+
+//nolint:tparallel
+func TestSDLEnterpriseMultiFileRoundTrip(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ t.Run(srv.name, func(t *testing.T) {
+ for _, corpus := range entCorpora() {
+ corpus := corpus
+ t.Run(corpus.name, func(t *testing.T) {
+ dbName := entLoadCorpus(ctx, t, srv, "entsdl_mf_", corpus)
+ meta := syncMetaForDB(ctx, t, srv, dbName)
+
+ result, err := schema.GetMultiFileDatabaseDefinition(storepb.Engine_MYSQL, schema.GetDefinitionContext{
+ SkipBackupSchema: true,
+ }, meta.GetProto())
+ require.NoError(t, err)
+ require.NotEmpty(t, result.Files, "[%s/%s] multi-file export produced no files", srv.name, corpus.name)
+
+ single := dumpSDL(ctx, t, srv, dbName)
+ concat := concatMultiFile(result)
+ t.Logf("[MULTIFILE %s/%s] %d files, concat %d bytes, single %d bytes",
+ srv.name, corpus.name, len(result.Files), len(concat), len(single))
+
+ forward, err := mysqlDiffSDLMigration(concat, single, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, forward, "[%s/%s] concat(multi-file) != single dump (concat->single):\n%s",
+ srv.name, corpus.name, forward)
+ backward, err := mysqlDiffSDLMigration(single, concat, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, backward, "[%s/%s] concat(multi-file) != single dump (single->concat):\n%s",
+ srv.name, corpus.name, backward)
+ self, err := mysqlDiffSDLMigration(concat, concat, srv.version)
+ require.NoError(t, err)
+ require.Empty(t, self, "[%s/%s] concat(multi-file) not idempotent:\n%s", srv.name, corpus.name, self)
+ })
+ }
+ })
+ }
+}
+
+// A7 of the ENTERPRISE smoke axes: CROSS-VERSION upgrade —
+// the migration-during-upgrade scenario where a schema is authored/dumped on one MySQL
+// version and the target is applied on the other.
+//
+// Legs, per corpus (zabbix / prestashop / openemr; sys stays behind its entCorpora gates):
+//
+// upgrade_57_dump_targets_80_db — load the corpus on 5.7, take its canonical dump D57,
+// load the SAME original DDL on 8.0, then use D57 as the TARGET against the 8.0
+// database (version-aware diff as 8.0.32). The plan must apply cleanly and
+// converge; 5.7-isms (integer display widths) must normalize away — asserted by the
+// no-width-in-ALTER guard — and nothing may phantom-drop. Genuine semantic
+// differences (the server-default charset a version applied at load time) are
+// LEGITIMATE plan content: a 5.7-authored dump of a charset-less corpus says
+// latin1, and declarative semantics honor it.
+// minimal_mutation_after_upgrade — (zabbix) one semantic edit (add column + index) on
+// top of D57, targeted at the upgraded 8.0 database → exactly the minimal DDL, no
+// cross-version noise re-emerging.
+// fresh_80_from_57_dump — D57 against an EMPTY 8.0 database (fresh create from
+// a 5.7-authored dump) → apply → re-dump → converges to D57 and self-diffs empty.
+// downgrade_80_dump_targets_57_db — the reverse: D80 as target on the 5.7 database.
+// Probed: where an 8.0-ism blocks the 5.7 apply (utf8mb4_0900_ai_ci — 8.0's default
+// utf8mb4 collation does not exist on 5.7), the leg skips with the named reason.
+// aligned_zabbix_utf8mb4_general_ci — the distilled asymmetry probe: both databases
+// created with the SAME 5.7-legal default (utf8mb4/utf8mb4_general_ci) before
+// loading zabbix, so the only cross-dump differences left are pure version
+// renderings (display widths). Both cross-diffs must then be EMPTY — any residual
+// is a phantom diff by definition.
+//
+// prestashop note: the corpus itself leads with SET SESSION sql_mode='' (two
+// '0000-00-00 00:00:00' defaults need non-strict mode). Generated plans cannot carry
+// session state, so plan APPLICATION for that corpus is prefixed with the corpus's own
+// preamble — the exact concession the corpus already requires at load time.
+//
+// Shared helpers (liveServers, entLoadDDL, dumpSDL, applyDDL, entPlanStatementCount,
+// entNormalizeDelimiters, newLiveDatabase, createLiveMySQLDriver, addColumnToTable,
+// addIndexToTable, entCorpora + embedded corpora) come from the sibling files.
+
+// Full server versions are threaded into the diff for this axis (the release path passes
+// the synced server version string, e.g. "8.0.32", not a bare major.minor).
+const (
+ entUpg57Version = "5.7.25"
+ entUpg80Version = "8.0.32"
+)
+
+type entUpgCorpus struct {
+ name string
+ ddl string
+ // preamble is prepended to every generated plan before application (see the
+ // prestashop note above). It mirrors the corpus's own leading session setup.
+ preamble string
+ // gate, when non-empty, skips the corpus with the given reason.
+ gate string
+}
+
+// entUpgCorpora derives the cross-version corpus list from the shared entCorpora().
+// The axis authors the SAME original DDL on BOTH versions, so a corpus gated on either
+// version (sys: gate + gate57) is gated here too.
+//
+// openemr note: the corpus's uuid columns (`uuid` binary(16) NOT NULL DEFAULT ”) once
+// gated it out of this axis — the 8.0 sync stored the information_schema hex NOTATION
+// ('0x…') verbatim and every cross-version leg looped on a phantom
+// `MODIFY COLUMN ... DEFAULT '0x…'` residual. The sync now canonicalizes binary-family
+// defaults (see TestSDLEnterpriseUpgradeBinaryDefaultCanonicalForm), so the corpus runs
+// un-gated.
+func entUpgCorpora() []entUpgCorpus {
+ var out []entUpgCorpus
+ for _, c := range entCorpora() {
+ u := entUpgCorpus{name: c.name, ddl: c.ddl, gate: c.gate}
+ if u.gate == "" && c.gate57 != "" {
+ u.gate = c.gate57
+ }
+ if u.gate == "" && c.canonical {
+ u.gate = "corpus ships as a canonical dump requiring the engine create-plan loader; A7 authors from raw upstream DDL"
+ }
+ if c.name == "prestashop" {
+ u.preamble = "SET SESSION sql_mode='';\n"
+ }
+ out = append(out, u)
+ }
+ return out
+}
+
+func entUpgServers(t *testing.T) (srv57, srv80 liveServer) {
+ t.Helper()
+ found57, found80 := false, false
+ for _, srv := range liveServers {
+ switch srv.version {
+ case "5.7":
+ srv57, found57 = srv, true
+ case "8.0":
+ srv80, found80 = srv, true
+ default:
+ }
+ }
+ require.True(t, found57 && found80, "cross-version axis needs both a 5.7 and an 8.0 live server")
+ return srv57, srv80
+}
+
+// entUpgTrim caps logged plan/residual text so a 250-table plan stays readable.
+func entUpgTrim(s string) string {
+ const limit = 6000
+ if len(s) <= limit {
+ return s
+ }
+ return s[:limit] + fmt.Sprintf("\n... (%d more bytes)", len(s)-limit)
+}
+
+// entUpgReDrop matches destructive operations that can NEVER be legitimate when both
+// databases were loaded from the same original DDL — any hit is a phantom diff.
+var entUpgReDrop = regexp.MustCompile(`(?i)\bDROP\s+(TABLE|COLUMN|TRIGGER|VIEW|FUNCTION|PROCEDURE|INDEX|KEY|CHECK|CONSTRAINT|FOREIGN)`)
+
+func entUpgAssertNoDrops(t *testing.T, label, plan string) {
+ t.Helper()
+ if m := entUpgReDrop.FindString(plan); m != "" {
+ require.Failf(t, "phantom destructive operation in cross-version plan",
+ "[%s] plan contains %q — with identical source DDL on both versions nothing may be dropped:\n%s",
+ label, m, entUpgTrim(plan))
+ }
+}
+
+// entUpgReIntWidth matches integer display widths — the 5.7 stored form 8.0 must
+// normalize away. tinyint(1) is exempt (the BOOLEAN spelling, canonical on BOTH
+// versions).
+var entUpgReIntWidth = regexp.MustCompile(`(?i)\b(?:tiny|small|medium|big)?int\(\d+\)`)
+
+// entUpgAssertNoWidthInAlters proves no ALTER statement carries a 5.7 display width —
+// a width inside an ALTER means the 5.7 rendering leaked through normalization and
+// phantom-modified a column. CREATE TABLE statements are not judged: a pass-through
+// width in a fresh CREATE is cosmetic, converges, and is not a phantom diff.
+func entUpgAssertNoWidthInAlters(t *testing.T, label, plan string) {
+ t.Helper()
+ if strings.TrimSpace(plan) == "" {
+ return
+ }
+ stmts, err := mysqlparser.SplitSQL(plan)
+ require.NoError(t, err, "[%s] split plan for width guard", label)
+ for _, s := range stmts {
+ text := strings.TrimSpace(s.Text)
+ if !strings.HasPrefix(strings.ToUpper(text), "ALTER TABLE") {
+ continue
+ }
+ for _, m := range entUpgReIntWidth.FindAllString(text, -1) {
+ if strings.EqualFold(m, "tinyint(1)") {
+ continue
+ }
+ require.Failf(t, "integer display width leaked into a cross-version ALTER",
+ "[%s] ALTER carries %q (5.7 display width not normalized away):\n%s", label, m, text)
+ }
+ }
+}
+
+// entUpgConverge diffs the live database against target (as version), applies the plan
+// (with the corpus preamble), and proves convergence + idempotence. Returns the plan
+// ("" when the schemas were already equivalent).
+func entUpgConverge(ctx context.Context, t *testing.T, srv liveServer, dbName, target, version, preamble, label string) string {
+ t.Helper()
+ source := dumpSDL(ctx, t, srv, dbName)
+ plan, err := mysqlDiffSDLMigration(source, target, version)
+ require.NoError(t, err, "[%s] diff", label)
+ if strings.TrimSpace(plan) == "" {
+ t.Logf("[%s] empty plan — schemas already equivalent", label)
+ } else {
+ t.Logf("[%s] plan: %d statements, %d bytes:\n%s", label, entPlanStatementCount(t, plan), len(plan), entUpgTrim(plan))
+ require.NoError(t, applyDDL(ctx, t, srv, dbName, preamble+plan),
+ "[%s] generated plan failed to apply:\n%s", label, entUpgTrim(plan))
+ }
+ after := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(after, target, version)
+ require.NoError(t, err, "[%s] converge diff", label)
+ require.Empty(t, converge, "[%s] did not converge; residual:\n%s", label, entUpgTrim(converge))
+ self, err := mysqlDiffSDLMigration(after, after, version)
+ require.NoError(t, err, "[%s] idempotence diff", label)
+ require.Empty(t, self, "[%s] post-apply dump not idempotent:\n%s", label, entUpgTrim(self))
+ return plan
+}
+
+// entUpgLoadAligned creates a database whose DEFAULT charset/collation is pinned to the
+// 5.7-legal utf8mb4/utf8mb4_general_ci on either server, then loads ddl — removing the
+// server-default charset divergence so cross-dumps differ only by version renderings.
+func entUpgLoadAligned(ctx context.Context, t *testing.T, srv liveServer, prefix, ddl string) string {
+ t.Helper()
+ dbName := newLiveDatabase(ctx, t, srv, prefix)
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, "ALTER DATABASE `"+dbName+"` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", db.ExecuteOptions{})
+ require.NoError(t, err, "[%s] align database default collation", srv.name)
+ _, err = driver.Execute(ctx, entNormalizeDelimiters(ddl), db.ExecuteOptions{})
+ require.NoError(t, err, "[%s] apply aligned base DDL", srv.name)
+ return dbName
+}
+
+//nolint:tparallel
+func TestSDLEnterpriseCrossVersionUpgrade(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+ srv57, srv80 := entUpgServers(t)
+
+ for _, corpus := range entUpgCorpora() {
+ corpus := corpus
+ t.Run(corpus.name, func(t *testing.T) {
+ if corpus.gate != "" {
+ t.Skipf("[A7/%s] %s", corpus.name, corpus.gate)
+ }
+
+ // Author on 5.7 and on 8.0 from the SAME original DDL; capture both
+ // canonical dumps up front so later legs judge fixed texts.
+ db57 := entLoadDDL(ctx, t, srv57, "entsdl_up57_"+corpus.name, corpus.ddl)
+ d57 := dumpSDL(ctx, t, srv57, db57)
+ db80 := entLoadDDL(ctx, t, srv80, "entsdl_up80_"+corpus.name, corpus.ddl)
+ d80 := dumpSDL(ctx, t, srv80, db80)
+ t.Logf("[A7/%s] D57 %d bytes, D80 %d bytes", corpus.name, len(d57), len(d80))
+
+ t.Run("upgrade_57_dump_targets_80_db", func(t *testing.T) {
+ label := "A7/" + corpus.name + "/upgrade_57_to_80"
+ plan := entUpgConverge(ctx, t, srv80, db80, d57, entUpg80Version, corpus.preamble, label)
+ entUpgAssertNoDrops(t, label, plan)
+ entUpgAssertNoWidthInAlters(t, label, plan)
+ })
+
+ if corpus.name == "zabbix" {
+ t.Run("minimal_mutation_after_upgrade", func(t *testing.T) {
+ label := "A7/" + corpus.name + "/minimal_mutation_after_upgrade"
+ // Self-sufficient: land the database on D57 first (a no-op when the
+ // upgrade leg just ran).
+ entUpgConverge(ctx, t, srv80, db80, d57, entUpg80Version, corpus.preamble, label+"/pre")
+
+ // One semantic edit on top of the 5.7-authored dump.
+ target := addColumnToTable(t, d57, "users", "`ent_up_col` varchar(50) DEFAULT NULL")
+ target = addIndexToTable(t, target, "users", "KEY `ent_up_idx` (`ent_up_col`)")
+
+ source := dumpSDL(ctx, t, srv80, db80)
+ plan, err := mysqlDiffSDLMigration(source, target, entUpg80Version)
+ require.NoError(t, err, "[%s] diff", label)
+ require.NotEmpty(t, plan, "[%s] the semantic edit must produce a plan", label)
+ t.Logf("[%s] plan:\n%s", label, plan)
+
+ n := entPlanStatementCount(t, plan)
+ require.LessOrEqual(t, n, 2, "[%s] plan must be the minimal DDL (add column + add index), got %d statements:\n%s", label, n, plan)
+ stmts, err := mysqlparser.SplitSQL(plan)
+ require.NoError(t, err)
+ for _, s := range stmts {
+ text := strings.TrimSpace(s.Text)
+ if text == "" || text == ";" {
+ continue
+ }
+ require.True(t, strings.HasPrefix(strings.ToUpper(text), "ALTER TABLE `USERS`"),
+ "[%s] plan statement strays off the edited table (cross-version noise re-emerged):\n%s", label, text)
+ }
+ upper := strings.ToUpper(plan)
+ require.Contains(t, upper, "ENT_UP_COL", "[%s] plan missing the added column:\n%s", label, plan)
+ require.Contains(t, upper, "ENT_UP_IDX", "[%s] plan missing the added index:\n%s", label, plan)
+
+ require.NoError(t, applyDDL(ctx, t, srv80, db80, plan), "[%s] minimal plan failed to apply:\n%s", label, plan)
+ after := dumpSDL(ctx, t, srv80, db80)
+ converge, err := mysqlDiffSDLMigration(after, target, entUpg80Version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "[%s] minimal mutation did not converge:\n%s", label, converge)
+ })
+ }
+
+ t.Run("fresh_80_from_57_dump", func(t *testing.T) {
+ label := "A7/" + corpus.name + "/fresh_80_from_57_dump"
+ dbEmpty := newLiveDatabase(ctx, t, srv80, "entsdl_upe80_"+corpus.name)
+ plan := entUpgConverge(ctx, t, srv80, dbEmpty, d57, entUpg80Version, corpus.preamble, label)
+ require.NotEmpty(t, plan, "[%s] creating from a 5.7-authored dump on an empty database must emit DDL", label)
+ entUpgAssertNoWidthInAlters(t, label, plan)
+ })
+
+ t.Run("downgrade_80_dump_targets_57_db", func(t *testing.T) {
+ label := "A7/" + corpus.name + "/downgrade_80_to_57"
+ source := dumpSDL(ctx, t, srv57, db57)
+ plan, err := mysqlDiffSDLMigration(source, d80, entUpg57Version)
+ require.NoError(t, err, "[%s] diff", label)
+ if strings.TrimSpace(plan) != "" {
+ t.Logf("[%s] plan: %d statements, %d bytes:\n%s", label, entPlanStatementCount(t, plan), len(plan), entUpgTrim(plan))
+ if applyErr := applyDDL(ctx, t, srv57, db57, corpus.preamble+plan); applyErr != nil {
+ // Probe outcome: an 8.0-authored dump can carry 8.0-isms no 5.7
+ // server accepts. The only one these corpora produce is 8.0's
+ // utf8mb4 default collation.
+ if strings.Contains(plan, "utf8mb4_0900_ai_ci") {
+ t.Skipf("[%s] 8.0-authored dump is not 5.7-legal: plan carries utf8mb4_0900_ai_ci "+
+ "(8.0's utf8mb4 default collation, unknown to 5.7); apply failed as expected: %v", label, applyErr)
+ }
+ require.NoErrorf(t, applyErr, "[%s] downgrade plan failed to apply and no known 8.0-ism explains it:\n%s",
+ label, entUpgTrim(plan))
+ }
+ }
+ after := dumpSDL(ctx, t, srv57, db57)
+ converge, err := mysqlDiffSDLMigration(after, d80, entUpg57Version)
+ require.NoError(t, err, "[%s] converge diff", label)
+ require.Empty(t, converge, "[%s] did not converge; residual:\n%s", label, entUpgTrim(converge))
+ self, err := mysqlDiffSDLMigration(after, after, entUpg57Version)
+ require.NoError(t, err)
+ require.Empty(t, self, "[%s] post-apply dump not idempotent:\n%s", label, entUpgTrim(self))
+ })
+ })
+ }
+
+ // The distilled asymmetry probe: with the server-default charset divergence removed,
+ // BOTH cross-diffs must be empty — the remaining differences are exactly the
+ // version renderings (5.7 display widths) the normalizer must absorb.
+ t.Run("aligned_zabbix_utf8mb4_general_ci", func(t *testing.T) {
+ db57 := entUpgLoadAligned(ctx, t, srv57, "entsdl_upal57", entZabbixSQL)
+ db80 := entUpgLoadAligned(ctx, t, srv80, "entsdl_upal80", entZabbixSQL)
+ d57 := dumpSDL(ctx, t, srv57, db57)
+ d80 := dumpSDL(ctx, t, srv80, db80)
+
+ t.Run("57_dump_targets_80_db", func(t *testing.T) {
+ cur := dumpSDL(ctx, t, srv80, db80)
+ plan, err := mysqlDiffSDLMigration(cur, d57, entUpg80Version)
+ require.NoError(t, err, "aligned 57->80 diff")
+ require.Empty(t, plan,
+ "collation-aligned 5.7 dump phantom-diffs an equivalent 8.0 database (widths/collation defaults did not normalize away):\n%s",
+ entUpgTrim(plan))
+ })
+ t.Run("80_dump_targets_57_db", func(t *testing.T) {
+ cur := dumpSDL(ctx, t, srv57, db57)
+ plan, err := mysqlDiffSDLMigration(cur, d80, entUpg57Version)
+ require.NoError(t, err, "aligned 80->57 diff")
+ require.Empty(t, plan,
+ "collation-aligned 8.0 dump phantom-diffs an equivalent 5.7 database (widths/collation defaults did not normalize away):\n%s",
+ entUpgTrim(plan))
+ })
+ })
+}
+
+// ----------------------------------------------------------------------------
+// REGRESSION (formerly the pinned bug this axis found, openemr corpus): binary-family
+// column defaults.
+//
+// MySQL reports binary/varbinary literal defaults in information_schema.COLUMNS in
+// version-specific encodings: 8.0 as hex NOTATION text built from the value truncated
+// at its first NUL byte ("0x" for DEFAULT '', "0x6162" for DEFAULT 'ab'), 5.7 as the
+// RAW BYTES, NUL-padded to the declared width for binary(N). The sync used to store
+// the 8.0 notation verbatim and the dumper re-quoted it as a STRING literal
+// (DEFAULT '0x6162') — value-unfaithful dumps, cross-version phantom MODIFY loops (the
+// openemr uuid columns that once gated that corpus out of the A7 legs), and round-trip
+// double-encoding ('0x6162' -> '0x307836313632').
+//
+// The sync now decodes both encodings into one canonical form — '' when the value is
+// empty (binary(N) padding stripped), a plain quoted string for clean text, an
+// unquoted hex literal otherwise; see canonicalBinaryDefault in
+// backend/plugin/db/mysql/sync.go. This test pins the fixed behavior: value-faithful
+// dumps on both versions, EMPTY cross-version diffs (the DDL pins the table charset so
+// the dumps may differ by nothing at all), and a clean round-trip through a fresh 8.0
+// database. The per-version legs then cover the hex-literal fallback for values no
+// quoted SDL string can carry — probed on the version whose information_schema reports
+// them faithfully (0xFF61 on 8.0; a significant trailing NUL, 0x6100, on 5.7).
+// ----------------------------------------------------------------------------
+
+//go:embed testdata/sdl/ent_upg_binary_default_ddl.sql
+var entUpgBinaryDefaultDDL string
+
+//nolint:tparallel
+func TestSDLEnterpriseUpgradeBinaryDefaultCanonicalForm(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+ srv57, srv80 := entUpgServers(t)
+
+ // Both versions dump the same value-faithful canonical form.
+ db80 := entLoadDDL(ctx, t, srv80, "entsdl_upbin80", entUpgBinaryDefaultDDL)
+ d80 := dumpSDL(ctx, t, srv80, db80)
+ db57 := entLoadDDL(ctx, t, srv57, "entsdl_upbin57", entUpgBinaryDefaultDDL)
+ d57 := dumpSDL(ctx, t, srv57, db57)
+ for _, d := range []struct{ version, dump string }{{"8.0", d80}, {"5.7", d57}} {
+ require.Contains(t, d.dump, "`u` binary(16) NOT NULL DEFAULT ''",
+ "%s dump must render the empty binary(16) default as '' (not the I_S encoding):\n%s", d.version, d.dump)
+ require.Contains(t, d.dump, "`v` varbinary(24) NOT NULL DEFAULT 'ab'",
+ "%s dump must render the varbinary default value-faithfully:\n%s", d.version, d.dump)
+ require.NotContains(t, d.dump, "0x",
+ "%s dump leaked an information_schema hex encoding for a clean-text default:\n%s", d.version, d.dump)
+ }
+
+ // Cross-version: with the table charset pinned, the dumps describe the same schema
+ // and BOTH cross-diffs must be empty — the shape that once looped on
+ // MODIFY COLUMN ... DEFAULT '0x…'.
+ cross80, err := mysqlDiffSDLMigration(d80, d57, entUpg80Version)
+ require.NoError(t, err)
+ require.Empty(t, cross80, "5.7 dump phantom-diffs the equivalent 8.0 database:\n%s", cross80)
+ cross57, err := mysqlDiffSDLMigration(d57, d80, entUpg57Version)
+ require.NoError(t, err)
+ require.Empty(t, cross57, "8.0 dump phantom-diffs the equivalent 5.7 database:\n%s", cross57)
+
+ // Round-trip the 8.0 dump through a FRESH database: the create plan must apply, the
+ // re-dump must converge to the dump (no double-encoding), and self-diff empty.
+ dbFresh := newLiveDatabase(ctx, t, srv80, "entsdl_upbinfresh")
+ empty := dumpSDL(ctx, t, srv80, dbFresh)
+ plan, err := mysqlDiffSDLMigration(empty, d80, entUpg80Version)
+ require.NoError(t, err)
+ require.NotEmpty(t, plan)
+ require.NoError(t, applyDDL(ctx, t, srv80, dbFresh, plan), "create plan from the 8.0 dump failed to apply:\n%s", plan)
+ redump := dumpSDL(ctx, t, srv80, dbFresh)
+ require.NotContains(t, redump, "0x3078",
+ "the 8.0 dump round-trip double-encodes the default again ('0x6162' -> '0x307836313632'):\n%s", redump)
+ converge, err := mysqlDiffSDLMigration(redump, d80, entUpg80Version)
+ require.NoError(t, err)
+ require.Empty(t, converge, "8.0 dump did not converge through a fresh database; residual:\n%s", converge)
+
+ // Values no quoted SDL string can carry fall back to an unquoted hex literal, each
+ // probed on the version whose information_schema reports the value faithfully (8.0
+ // truncates at NUL bytes; 5.7 truncates at non-UTF8 bytes).
+ t.Run("hex_literal_fallback_nonutf8_80", func(t *testing.T) {
+ ddl := "CREATE TABLE ent_up_binhex (id int NOT NULL, w varbinary(8) NOT NULL DEFAULT 0xFF61, PRIMARY KEY (id)) ENGINE=InnoDB;"
+ dbName := entLoadDDL(ctx, t, srv80, "entsdl_upbinhex80", ddl)
+ dump := dumpSDL(ctx, t, srv80, dbName)
+ require.Contains(t, dump, "`w` varbinary(8) NOT NULL DEFAULT 0xff61",
+ "8.0 dump must keep the non-UTF8 default as an unquoted hex literal:\n%s", dump)
+ self, err := mysqlDiffSDLMigration(dump, dump, entUpg80Version)
+ require.NoError(t, err)
+ require.Empty(t, self, "hex-literal dump not idempotent:\n%s", self)
+
+ fresh := newLiveDatabase(ctx, t, srv80, "entsdl_upbinhexf80")
+ plan, err := mysqlDiffSDLMigration(dumpSDL(ctx, t, srv80, fresh), dump, entUpg80Version)
+ require.NoError(t, err)
+ require.NoError(t, applyDDL(ctx, t, srv80, fresh, plan), "hex-literal create plan failed to apply:\n%s", plan)
+ require.Equal(t, dump, dumpSDL(ctx, t, srv80, fresh), "hex-literal default did not round-trip byte-identically")
+ })
+ t.Run("hex_literal_fallback_trailnul_57", func(t *testing.T) {
+ ddl := "CREATE TABLE ent_up_binnul (id int NOT NULL, w varbinary(8) NOT NULL DEFAULT 0x6100, PRIMARY KEY (id)) ENGINE=InnoDB;"
+ dbName := entLoadDDL(ctx, t, srv57, "entsdl_upbinnul57", ddl)
+ dump := dumpSDL(ctx, t, srv57, dbName)
+ require.Contains(t, dump, "`w` varbinary(8) NOT NULL DEFAULT 0x6100",
+ "5.7 dump must keep the trailing-NUL default as an unquoted hex literal (the NUL is significant on varbinary):\n%s", dump)
+ self, err := mysqlDiffSDLMigration(dump, dump, entUpg57Version)
+ require.NoError(t, err)
+ require.Empty(t, self, "hex-literal dump not idempotent:\n%s", self)
+
+ fresh := newLiveDatabase(ctx, t, srv57, "entsdl_upbinnulf57")
+ plan, err := mysqlDiffSDLMigration(dumpSDL(ctx, t, srv57, fresh), dump, entUpg57Version)
+ require.NoError(t, err)
+ require.NoError(t, applyDDL(ctx, t, srv57, fresh, plan), "hex-literal create plan failed to apply:\n%s", plan)
+ require.Equal(t, dump, dumpSDL(ctx, t, srv57, fresh), "hex-literal default did not round-trip byte-identically")
+ })
+}
+
+// A4 of the ENTERPRISE smoke axes: a SEEDED, deterministic
+// random-migration fuzzer over a 30-table Zabbix slice. Each seed derives K ∈ [3,8]
+// independent mutations from a menu (add/drop/modify column, add/drop index, add/drop FK
+// with valid targets only, add/drop/modify view, add/drop trigger, add/drop routine,
+// partition/departition), applies them TEXTUALLY to the canonical dump to build the
+// target SDL, and runs the oracle protocol (diff → apply → converge → idempotence).
+//
+// Determinism: the mutation stream is a pure function of the seed (math/rand with a
+// fixed source; the schema model is iterated in sorted order), so a failing seed replays
+// exactly. Every failure prints the seed and a ddmin-minimized mutation list, each
+// minimization trial re-running the full oracle on a fresh scratch database.
+//
+// ≥20 seeds run on 8.0 and ≥10 on 5.7 (the menu is 5.7-legal, so both use the same
+// generator). Scratch databases are entsdl_-prefixed and dropped eagerly per trial.
+
+// entFuzzSliceTables is the ~30-table fuzz base: the core slice plus the httptest family
+// (6 more triggered tables, FK-chained through httptest/httpstep into hosts and items)
+// and token (a 2-FK fan-in on users).
+var entFuzzSliceTables = append(append([]string{}, entSliceCoreTables...),
+ "httptest", "httpstep", "httptestitem", "httpstepitem", "httptest_field", "httpstep_field",
+ "token",
+)
+
+// entFuzzAux seeds the droppable/modifiable object pool the menu needs: two views, one
+// function, one procedure, and a partitioned table (the departition target).
+//
+//go:embed testdata/sdl/ent_fuzz_aux.sql
+var entFuzzAux string
+
+// entFzProtectedTables are never touched by column-level mutations: the changelog
+// trigger bodies name changelog's columns outright (not via NEW/OLD), and ent_fz_part
+// belongs to the partition mutations.
+var entFzProtectedTables = map[string]bool{"changelog": true, "ent_fz_part": true}
+
+// entFzProtectedColumns are referenced by the seeded views (or their modify-view
+// extension columns) and must survive column drops.
+var entFzProtectedColumns = map[string]bool{
+ "users.username": true, "users.name": true,
+ "hosts.host": true, "hosts.status": true,
+}
+
+// ----------------------------------------------------------------------------
+// Canonical-dump schema model (guides valid mutation choices; the mutations themselves
+// are textual).
+// ----------------------------------------------------------------------------
+
+type entFzIndex struct {
+ name string
+ cols []string
+ unique bool
+}
+
+type entFzFK struct {
+ name string
+ cols []string
+}
+
+type entFzTable struct {
+ name string
+ cols []string // ordered
+ colLine map[string]string // name -> trimmed definition line
+ pk map[string]bool
+ indexes []entFzIndex
+ fks []entFzFK
+ partitioned bool
+}
+
+type entFzModel struct {
+ tables []*entFzTable // sorted by name
+ byName map[string]*entFzTable
+ referenced map[string]bool // tables referenced by any FK
+ triggerCols map[string]map[string]bool // table -> NEW./OLD.-referenced columns
+ triggers []string // sorted trigger names
+ views []string // seeded, droppable views
+ functions []string // seeded, droppable functions
+ procedures []string // seeded, droppable procedures
+}
+
+var (
+ entFzReColLine = regexp.MustCompile("^`(\\w+)` (.+?),?$")
+ entFzRePK = regexp.MustCompile(`^PRIMARY KEY \((.+)\),?$`)
+ entFzReIndex = regexp.MustCompile("^(UNIQUE )?KEY `(\\w+)` \\((.+)\\),?$")
+ entFzReFK = regexp.MustCompile("^CONSTRAINT `(\\w+)` FOREIGN KEY \\((.+?)\\) REFERENCES `(\\w+)`")
+ entFzReTrigger = regexp.MustCompile("(?i)^CREATE TRIGGER `(\\w+)`.* ON `(\\w+)`")
+ entFzReNewOld = regexp.MustCompile(`(?i)\b(?:new|old)\.(\w+)`)
+ entFzReCol = regexp.MustCompile("`(\\w+)`")
+ entFzReVarLen = regexp.MustCompile(`varchar\((\d+)\)`)
+)
+
+// entFuzzModel parses the canonical dump into the mutation-planning model.
+func entFuzzModel(t *testing.T, source string) *entFzModel {
+ t.Helper()
+ stmts, err := mysqlparser.SplitSQL(source)
+ require.NoError(t, err, "split canonical dump for fuzz model")
+
+ m := &entFzModel{
+ byName: map[string]*entFzTable{},
+ referenced: map[string]bool{},
+ triggerCols: map[string]map[string]bool{},
+ }
+ for _, s := range stmts {
+ text := strings.TrimSpace(s.Text)
+ upper := strings.ToUpper(text)
+ switch {
+ case strings.HasPrefix(upper, "CREATE TABLE `"):
+ tbl := entFzParseTable(text)
+ if tbl != nil {
+ m.tables = append(m.tables, tbl)
+ m.byName[tbl.name] = tbl
+ }
+ case strings.HasPrefix(upper, "CREATE TRIGGER `") || (strings.HasPrefix(upper, "CREATE ") && strings.Contains(upper, " TRIGGER `")):
+ if mm := entFzReTrigger.FindStringSubmatch(text); mm != nil {
+ m.triggers = append(m.triggers, mm[1])
+ onTable := mm[2]
+ if m.triggerCols[onTable] == nil {
+ m.triggerCols[onTable] = map[string]bool{}
+ }
+ for _, ref := range entFzReNewOld.FindAllStringSubmatch(text, -1) {
+ m.triggerCols[onTable][ref[1]] = true
+ }
+ }
+ default:
+ }
+ }
+ // FK-referenced tables (for partition candidacy).
+ for _, s := range stmts {
+ for _, mm := range entFzReFKRefs.FindAllStringSubmatch(s.Text, -1) {
+ m.referenced[mm[1]] = true
+ }
+ }
+ // Seeded droppable objects (fixed names — the fuzz base owns them).
+ m.views = []string{"ent_fz_v1", "ent_fz_v2"}
+ m.functions = []string{"ent_fz_fn1"}
+ m.procedures = []string{"ent_fz_pr1"}
+ return m
+}
+
+var entFzReFKRefs = regexp.MustCompile("FOREIGN KEY \\(.+?\\) REFERENCES `(\\w+)`")
+
+// entFzParseTable parses one canonical CREATE TABLE statement.
+func entFzParseTable(stmt string) *entFzTable {
+ header := regexp.MustCompile("^CREATE TABLE `(\\w+)`").FindStringSubmatch(stmt)
+ if header == nil {
+ return nil
+ }
+ tbl := &entFzTable{
+ name: header[1],
+ colLine: map[string]string{},
+ pk: map[string]bool{},
+ partitioned: strings.Contains(strings.ToUpper(stmt), "PARTITION BY"),
+ }
+ for _, raw := range strings.Split(stmt, "\n")[1:] {
+ line := strings.TrimSpace(raw)
+ if line == "" || strings.HasPrefix(line, ")") {
+ break
+ }
+ if mm := entFzRePK.FindStringSubmatch(line); mm != nil {
+ for _, c := range entFzReCol.FindAllStringSubmatch(mm[1], -1) {
+ tbl.pk[c[1]] = true
+ }
+ continue
+ }
+ if mm := entFzReIndex.FindStringSubmatch(line); mm != nil {
+ idx := entFzIndex{name: mm[2], unique: mm[1] != ""}
+ for _, c := range entFzReCol.FindAllStringSubmatch(mm[3], -1) {
+ idx.cols = append(idx.cols, c[1])
+ }
+ tbl.indexes = append(tbl.indexes, idx)
+ continue
+ }
+ if mm := entFzReFK.FindStringSubmatch(line); mm != nil {
+ fk := entFzFK{name: mm[1]}
+ for _, c := range entFzReCol.FindAllStringSubmatch(mm[2], -1) {
+ fk.cols = append(fk.cols, c[1])
+ }
+ tbl.fks = append(tbl.fks, fk)
+ continue
+ }
+ if strings.HasPrefix(line, "CONSTRAINT `") || strings.HasPrefix(line, "FULLTEXT KEY") {
+ continue // CHECK constraints / fulltext — not mutation targets
+ }
+ if mm := entFzReColLine.FindStringSubmatch(line); mm != nil {
+ tbl.cols = append(tbl.cols, mm[1])
+ tbl.colLine[mm[1]] = strings.TrimSuffix(line, ",")
+ }
+ }
+ return tbl
+}
+
+// entFzIndexedCols returns every column participating in any index or FK of tbl.
+func entFzIndexedCols(tbl *entFzTable) map[string]bool {
+ used := map[string]bool{}
+ for c := range tbl.pk {
+ used[c] = true
+ }
+ for _, idx := range tbl.indexes {
+ for _, c := range idx.cols {
+ used[c] = true
+ }
+ }
+ for _, fk := range tbl.fks {
+ for _, c := range fk.cols {
+ used[c] = true
+ }
+ }
+ return used
+}
+
+// ----------------------------------------------------------------------------
+// Mutation generation.
+// ----------------------------------------------------------------------------
+
+// entFzMutation is one textual schema mutation with a replayable description.
+type entFzMutation struct {
+ desc string
+ apply func(t *testing.T, source string) string
+}
+
+// entFzState tracks per-round conflicts so the K mutations stay independent.
+type entFzState struct {
+ usedCols map[string]bool // "table.col" touched by drop/modify
+ usedIndexes map[string]bool // "table.index" dropped
+ usedFKs map[string]bool
+ usedViews map[string]bool
+ usedTriggers map[string]bool
+ usedRoutines map[string]bool
+ lockedTables map[string]bool // partition/departition/fk-structure locks
+}
+
+func entFzNewState() *entFzState {
+ return &entFzState{
+ usedCols: map[string]bool{},
+ usedIndexes: map[string]bool{},
+ usedFKs: map[string]bool{},
+ usedViews: map[string]bool{},
+ usedTriggers: map[string]bool{},
+ usedRoutines: map[string]bool{},
+ lockedTables: map[string]bool{},
+ }
+}
+
+var entFzMenu = []string{
+ "add_column", "drop_column", "modify_column",
+ "add_index", "drop_index",
+ "add_fk", "drop_fk",
+ "add_view", "drop_view", "modify_view",
+ "add_trigger", "drop_trigger",
+ "add_routine", "drop_routine",
+ "partition", "departition",
+}
+
+// entFuzzMutations derives k independent mutations from the model, deterministically in
+// (model order, rng stream).
+func entFuzzMutations(t *testing.T, model *entFzModel, rng *rand.Rand, k int) []entFzMutation {
+ t.Helper()
+ state := entFzNewState()
+ var muts []entFzMutation
+ for attempt := 0; attempt < 400 && len(muts) < k; attempt++ {
+ kind := entFzMenu[rng.Intn(len(entFzMenu))]
+ seq := len(muts)
+ if m, ok := entFzGenerate(kind, model, rng, state, seq); ok {
+ muts = append(muts, m)
+ }
+ }
+ require.Len(t, muts, k, "fuzz generator starved (only %d of %d mutations)", len(muts), k)
+ return muts
+}
+
+// entFzPickTable picks a random mutable (non-protected, non-locked) table.
+func entFzPickTable(model *entFzModel, rng *rand.Rand, state *entFzState) *entFzTable {
+ var candidates []*entFzTable
+ for _, tbl := range model.tables {
+ if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
+ continue
+ }
+ candidates = append(candidates, tbl)
+ }
+ if len(candidates) == 0 {
+ return nil
+ }
+ return candidates[rng.Intn(len(candidates))]
+}
+
+// entFzGenerate builds one mutation of the given kind, or reports it infeasible.
+func entFzGenerate(kind string, model *entFzModel, rng *rand.Rand, state *entFzState, seq int) (entFzMutation, bool) {
+ switch kind {
+ case "add_column":
+ tbl := entFzPickTable(model, rng, state)
+ if tbl == nil {
+ return entFzMutation{}, false
+ }
+ defs := []string{"varchar(40) DEFAULT NULL", "int NOT NULL DEFAULT '0'", "decimal(8,2) DEFAULT NULL", "bigint unsigned DEFAULT NULL"}
+ def := defs[rng.Intn(len(defs))]
+ col := fmt.Sprintf("ent_fz_c%d", seq)
+ table := tbl.name
+ return entFzMutation{
+ desc: fmt.Sprintf("add_column %s.%s (%s)", table, col, def),
+ apply: func(t *testing.T, source string) string {
+ return addColumnToTable(t, source, table, "`"+col+"` "+def)
+ },
+ }, true
+
+ case "drop_column":
+ type cand struct{ table, col string }
+ var cands []cand
+ for _, tbl := range model.tables {
+ if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
+ continue
+ }
+ indexed := entFzIndexedCols(tbl)
+ trigRef := model.triggerCols[tbl.name]
+ for _, c := range tbl.cols {
+ if indexed[c] || trigRef[c] || entFzProtectedColumns[tbl.name+"."+c] {
+ continue
+ }
+ if state.usedCols[tbl.name+"."+c] {
+ continue
+ }
+ if strings.Contains(tbl.colLine[c], "GENERATED") {
+ continue
+ }
+ cands = append(cands, cand{tbl.name, c})
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.col] = true
+ return entFzMutation{
+ desc: fmt.Sprintf("drop_column %s.%s", pick.table, pick.col),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "`"+pick.col+"`")
+ },
+ }, true
+
+ case "modify_column":
+ type cand struct {
+ table, col string
+ from, to int
+ }
+ var cands []cand
+ for _, tbl := range model.tables {
+ if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
+ continue
+ }
+ for _, c := range tbl.cols {
+ if state.usedCols[tbl.name+"."+c] {
+ continue
+ }
+ mm := entFzReVarLen.FindStringSubmatch(tbl.colLine[c])
+ if mm == nil {
+ continue
+ }
+ n := entFzAtoi(mm[1])
+ if n < 8 || n > 150 {
+ continue
+ }
+ cands = append(cands, cand{tbl.name, c, n, n + 37})
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.col] = true
+ return entFzMutation{
+ desc: fmt.Sprintf("modify_column widen %s.%s varchar(%d)->varchar(%d)", pick.table, pick.col, pick.from, pick.to),
+ apply: func(t *testing.T, source string) string {
+ return entReplaceInTable(t, source, pick.table,
+ fmt.Sprintf("`%s` varchar(%d)", pick.col, pick.from),
+ fmt.Sprintf("`%s` varchar(%d)", pick.col, pick.to))
+ },
+ }, true
+
+ case "add_index":
+ type cand struct{ table, col string }
+ var cands []cand
+ for _, tbl := range model.tables {
+ if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
+ continue
+ }
+ for _, c := range tbl.cols {
+ line := tbl.colLine[c]
+ rest := strings.TrimPrefix(line, "`"+c+"` ")
+ ok := strings.HasPrefix(rest, "bigint") || strings.HasPrefix(rest, "int") || strings.HasPrefix(rest, "integer")
+ if mm := entFzReVarLen.FindStringSubmatch(rest); mm != nil && strings.HasPrefix(rest, "varchar") {
+ ok = entFzAtoi(mm[1]) <= 150
+ }
+ if ok {
+ cands = append(cands, cand{tbl.name, c})
+ }
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ idx := fmt.Sprintf("ent_fz_i%d", seq)
+ return entFzMutation{
+ desc: fmt.Sprintf("add_index %s on %s(%s)", idx, pick.table, pick.col),
+ apply: func(t *testing.T, source string) string {
+ return addIndexToTable(t, source, pick.table, "KEY `"+idx+"` (`"+pick.col+"`)")
+ },
+ }, true
+
+ case "drop_index":
+ type cand struct{ table, index string }
+ var cands []cand
+ for _, tbl := range model.tables {
+ if state.lockedTables[tbl.name] {
+ continue
+ }
+ fkCols := map[string]bool{}
+ for _, fk := range tbl.fks {
+ for _, c := range fk.cols {
+ fkCols[c] = true
+ }
+ }
+ for _, idx := range tbl.indexes {
+ if state.usedIndexes[tbl.name+"."+idx.name] {
+ continue
+ }
+ overlap := false
+ for _, c := range idx.cols {
+ if fkCols[c] {
+ overlap = true
+ break
+ }
+ }
+ if !overlap {
+ cands = append(cands, cand{tbl.name, idx.name})
+ }
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedIndexes[pick.table+"."+pick.index] = true
+ return entFzMutation{
+ desc: fmt.Sprintf("drop_index %s.%s", pick.table, pick.index),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "`"+pick.index+"`")
+ },
+ }, true
+
+ case "add_fk":
+ src := entFzPickTable(model, rng, state)
+ if src == nil || src.partitioned {
+ return entFzMutation{}, false
+ }
+ var refs []*entFzTable
+ for _, tbl := range model.tables {
+ if tbl.partitioned || state.lockedTables[tbl.name] || len(tbl.pk) != 1 {
+ continue
+ }
+ pkCol := ""
+ for c := range tbl.pk {
+ pkCol = c
+ }
+ if strings.HasPrefix(strings.TrimPrefix(tbl.colLine[pkCol], "`"+pkCol+"` "), "bigint") &&
+ strings.Contains(tbl.colLine[pkCol], "unsigned") {
+ refs = append(refs, tbl)
+ }
+ }
+ if len(refs) == 0 {
+ return entFzMutation{}, false
+ }
+ ref := refs[rng.Intn(len(refs))]
+ refPK := ""
+ for c := range ref.pk {
+ refPK = c
+ }
+ srcName, refName := src.name, ref.name
+ state.lockedTables[srcName] = true
+ state.lockedTables[refName] = true
+ col := fmt.Sprintf("ent_fz_r%d", seq)
+ fk := fmt.Sprintf("ent_fz_fk%d", seq)
+ return entFzMutation{
+ desc: fmt.Sprintf("add_fk %s.%s -> %s.%s (%s)", srcName, col, refName, refPK, fk),
+ apply: func(t *testing.T, source string) string {
+ s := addColumnToTable(t, source, srcName, "`"+col+"` bigint unsigned DEFAULT NULL")
+ s = addIndexToTable(t, s, srcName, "KEY `"+fk+"` (`"+col+"`)")
+ return addIndexToTable(t, s, srcName,
+ "CONSTRAINT `"+fk+"` FOREIGN KEY (`"+col+"`) REFERENCES `"+refName+"` (`"+refPK+"`) ON DELETE SET NULL")
+ },
+ }, true
+
+ case "drop_fk":
+ type cand struct{ table, fk string }
+ var cands []cand
+ for _, tbl := range model.tables {
+ if state.lockedTables[tbl.name] {
+ continue
+ }
+ for _, fk := range tbl.fks {
+ if !state.usedFKs[tbl.name+"."+fk.name] {
+ cands = append(cands, cand{tbl.name, fk.name})
+ }
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedFKs[pick.table+"."+pick.fk] = true
+ return entFzMutation{
+ desc: fmt.Sprintf("drop_fk %s.%s", pick.table, pick.fk),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "CONSTRAINT `"+pick.fk+"`")
+ },
+ }, true
+
+ case "add_view":
+ tbl := entFzPickTable(model, rng, state)
+ if tbl == nil || len(tbl.pk) == 0 {
+ return entFzMutation{}, false
+ }
+ pkCol := ""
+ for _, c := range tbl.cols {
+ if tbl.pk[c] {
+ pkCol = c
+ break
+ }
+ }
+ if pkCol == "" {
+ return entFzMutation{}, false
+ }
+ name := fmt.Sprintf("ent_fz_v%d", 10+seq)
+ table := tbl.name
+ return entFzMutation{
+ desc: fmt.Sprintf("add_view %s over %s(%s)", name, table, pkCol),
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE VIEW %s AS SELECT %s FROM %s;\n", name, pkCol, table)
+ },
+ }, true
+
+ case "drop_view":
+ var cands []string
+ for _, v := range model.views {
+ if !state.usedViews[v] {
+ cands = append(cands, v)
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedViews[pick] = true
+ return entFzMutation{
+ desc: "drop_view " + pick,
+ apply: func(t *testing.T, source string) string {
+ return dropObjectBlock(t, source, "VIEW", pick)
+ },
+ }, true
+
+ case "modify_view":
+ extension := map[string]string{
+ "ent_fz_v1": "`users`.`name` AS `name`",
+ "ent_fz_v2": "`hosts`.`status` AS `status`",
+ }
+ var cands []string
+ for _, v := range model.views {
+ if !state.usedViews[v] && extension[v] != "" {
+ cands = append(cands, v)
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedViews[pick] = true
+ ext := extension[pick]
+ return entFzMutation{
+ desc: "modify_view " + pick,
+ apply: func(t *testing.T, source string) string {
+ return addColumnToView(t, source, pick, ext)
+ },
+ }, true
+
+ case "add_trigger":
+ tbl := entFzPickTable(model, rng, state)
+ if tbl == nil || len(tbl.pk) == 0 {
+ return entFzMutation{}, false
+ }
+ pkCol := ""
+ for _, c := range tbl.cols {
+ if tbl.pk[c] {
+ pkCol = c
+ break
+ }
+ }
+ if pkCol == "" {
+ return entFzMutation{}, false
+ }
+ name := fmt.Sprintf("ent_fz_t%d", seq)
+ table := tbl.name
+ return entFzMutation{
+ desc: fmt.Sprintf("add_trigger %s on %s", name, table),
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE TRIGGER %s BEFORE UPDATE ON %s FOR EACH ROW SET NEW.%s = NEW.%s;\n",
+ name, table, pkCol, pkCol)
+ },
+ }, true
+
+ case "drop_trigger":
+ var cands []string
+ for _, trg := range model.triggers {
+ if !state.usedTriggers[trg] {
+ cands = append(cands, trg)
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedTriggers[pick] = true
+ return entFzMutation{
+ desc: "drop_trigger " + pick,
+ apply: func(t *testing.T, source string) string {
+ return dropTrigger(t, source, pick)
+ },
+ }, true
+
+ case "add_routine":
+ if rng.Intn(2) == 0 {
+ name := fmt.Sprintf("ent_fz_fn%d", 10+seq)
+ ret := seq + 100
+ return entFzMutation{
+ desc: "add_routine function " + name,
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE FUNCTION %s() RETURNS INT DETERMINISTIC RETURN %d;\n", name, ret)
+ },
+ }, true
+ }
+ name := fmt.Sprintf("ent_fz_pr%d", 10+seq)
+ sel := seq + 200
+ return entFzMutation{
+ desc: "add_routine procedure " + name,
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE PROCEDURE %s() BEGIN SELECT %d; END;\n", name, sel)
+ },
+ }, true
+
+ case "drop_routine":
+ type cand struct{ kind, name string }
+ var cands []cand
+ for _, fn := range model.functions {
+ if !state.usedRoutines[fn] {
+ cands = append(cands, cand{"FUNCTION", fn})
+ }
+ }
+ for _, pr := range model.procedures {
+ if !state.usedRoutines[pr] {
+ cands = append(cands, cand{"PROCEDURE", pr})
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedRoutines[pick.name] = true
+ return entFzMutation{
+ desc: "drop_routine " + strings.ToLower(pick.kind) + " " + pick.name,
+ apply: func(t *testing.T, source string) string {
+ return dropObjectBlock(t, source, pick.kind, pick.name)
+ },
+ }, true
+
+ case "partition":
+ var cands []*entFzTable
+ for _, tbl := range model.tables {
+ // changelog is column-protected but IS the partition candidate (the only
+ // FK-free, unreferenced, single-PK table in the slice); ent_fz_part is
+ // already partitioned.
+ if tbl.partitioned || state.lockedTables[tbl.name] {
+ continue
+ }
+ if len(tbl.fks) > 0 || model.referenced[tbl.name] || len(tbl.pk) != 1 {
+ continue
+ }
+ // The partition key must be part of every unique key: single-col-PK tables
+ // with no other UNIQUE index qualify.
+ hasUnique := false
+ for _, idx := range tbl.indexes {
+ if idx.unique {
+ hasUnique = true
+ break
+ }
+ }
+ if hasUnique {
+ continue
+ }
+ cands = append(cands, tbl)
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ pkCol := ""
+ for c := range pick.pk {
+ pkCol = c
+ }
+ state.lockedTables[pick.name] = true
+ table := pick.name
+ return entFzMutation{
+ desc: fmt.Sprintf("partition %s by hash(%s)", table, pkCol),
+ apply: func(t *testing.T, source string) string {
+ return entAppendTableClause(t, source, table, "PARTITION BY HASH (`"+pkCol+"`)\nPARTITIONS 4")
+ },
+ }, true
+
+ case "departition":
+ var cands []*entFzTable
+ for _, tbl := range model.tables {
+ if tbl.partitioned && !state.lockedTables[tbl.name] {
+ cands = append(cands, tbl)
+ }
+ }
+ if len(cands) == 0 {
+ return entFzMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.lockedTables[pick.name] = true
+ table := pick.name
+ return entFzMutation{
+ desc: "departition " + table,
+ apply: func(t *testing.T, source string) string {
+ return entSetPartitionClause(t, source, table, "")
+ },
+ }, true
+
+ default:
+ return entFzMutation{}, false
+ }
+}
+
+// entFzAtoi is a no-error atoi for regex-captured digits.
+func entFzAtoi(s string) int {
+ n := 0
+ for _, r := range s {
+ n = n*10 + int(r-'0')
+ }
+ return n
+}
+
+// ----------------------------------------------------------------------------
+// Trial runner + ddmin minimization.
+// ----------------------------------------------------------------------------
+
+// entFuzzScratchDB creates an eagerly-droppable scratch database (fuzz + minimization
+// churn through many databases inside a single test, so cleanup cannot wait for test
+// end).
+func entFuzzScratchDB(ctx context.Context, t *testing.T, srv liveServer) (string, func()) {
+ t.Helper()
+ dbName := newLiveDatabase(ctx, t, srv, "entsdl_fz")
+ drop := func() {
+ c, err := createLiveMySQLDriver(ctx, srv, "")
+ if err != nil {
+ return
+ }
+ defer c.Close(ctx)
+ _, _ = c.Execute(ctx, "DROP DATABASE IF EXISTS `"+dbName+"`", db.ExecuteOptions{})
+ }
+ return dbName, drop
+}
+
+// entFuzzTrial runs the whole oracle protocol for one mutation set on a fresh scratch
+// database. Infrastructure failures (server down, base DDL broken, mutation anchors
+// missing) fail the test hard; ORACLE violations (diff error, apply error, residual
+// after apply, non-idempotent dump) are returned as errors so the caller can minimize.
+func entFuzzTrial(ctx context.Context, t *testing.T, srv liveServer, baseDDL string, muts []entFzMutation) error {
+ t.Helper()
+ dbName, drop := entFuzzScratchDB(ctx, t, srv)
+ defer drop()
+
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ _, err = driver.Execute(ctx, entNormalizeDelimiters(baseDDL), db.ExecuteOptions{})
+ driver.Close(ctx)
+ require.NoError(t, err, "[%s] fuzz base failed to load", srv.name)
+
+ source := dumpSDL(ctx, t, srv, dbName)
+ target := source
+ for _, m := range muts {
+ target = m.apply(t, target)
+ }
+
+ plan, err := mysqlDiffSDLMigration(source, target, srv.version)
+ if err != nil {
+ return errors.Wrap(err, "diff failed")
+ }
+ if strings.TrimSpace(plan) == "" {
+ return errors.New("empty plan for a non-empty mutation set")
+ }
+ if err := applyDDL(ctx, t, srv, dbName, plan); err != nil {
+ return errors.Wrapf(err, "plan failed to apply; plan was:\n%s", plan)
+ }
+ after := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(after, target, srv.version)
+ if err != nil {
+ return errors.Wrap(err, "converge diff failed")
+ }
+ if converge != "" {
+ return errors.Errorf("did not converge; residual:\n%s\nplan was:\n%s", converge, plan)
+ }
+ self, err := mysqlDiffSDLMigration(after, after, srv.version)
+ if err != nil {
+ return errors.Wrap(err, "idempotence diff failed")
+ }
+ if self != "" {
+ return errors.Errorf("post-apply dump not idempotent:\n%s", self)
+ }
+ return nil
+}
+
+// entFuzzMinimize greedily shrinks a failing mutation set (ddmin-lite: drop one mutation
+// at a time, keep the removal whenever the rest still fails). Returns the minimized set
+// and its error.
+func entFuzzMinimize(ctx context.Context, t *testing.T, srv liveServer, baseDDL string, muts []entFzMutation, firstErr error) ([]entFzMutation, error) {
+ t.Helper()
+ minimized := muts
+ lastErr := firstErr
+ for i := 0; i < len(minimized) && len(minimized) > 1; {
+ trial := make([]entFzMutation, 0, len(minimized)-1)
+ trial = append(trial, minimized[:i]...)
+ trial = append(trial, minimized[i+1:]...)
+ if err := entFuzzTrial(ctx, t, srv, baseDDL, trial); err != nil {
+ minimized = trial
+ lastErr = err
+ continue // same index now names the next mutation
+ }
+ i++
+ }
+ return minimized, lastErr
+}
+
+func entFzDescs(muts []entFzMutation) []string {
+ out := make([]string, len(muts))
+ for i, m := range muts {
+ out[i] = m.desc
+ }
+ return out
+}
+
+// ----------------------------------------------------------------------------
+// The fuzz test: ≥20 seeds on 8.0, ≥10 on 5.7 (deterministic; the seed is in the
+// subtest name).
+// ----------------------------------------------------------------------------
+
+//nolint:tparallel
+func TestSDLEnterpriseFuzz(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ seedCount := 20
+ if srv.version == "5.7" {
+ seedCount = 10
+ }
+ t.Run(srv.name, func(t *testing.T) {
+ baseDDL := entZabbixSlice(t, entFuzzSliceTables...) + entFuzzAux
+
+ // The model is built once per server from a reference load (the canonical dump
+ // is deterministic, so it matches every trial's source).
+ refDB, refDrop := entFuzzScratchDB(ctx, t, srv)
+ driver, err := createLiveMySQLDriver(ctx, srv, refDB)
+ require.NoError(t, err)
+ _, err = driver.Execute(ctx, entNormalizeDelimiters(baseDDL), db.ExecuteOptions{})
+ driver.Close(ctx)
+ require.NoError(t, err, "[%s] fuzz base failed to load", srv.name)
+ refSource := dumpSDL(ctx, t, srv, refDB)
+ refDrop()
+ model := entFuzzModel(t, refSource)
+ require.NotEmpty(t, model.tables, "fuzz model parsed no tables")
+ require.NotEmpty(t, model.triggers, "fuzz model parsed no triggers")
+
+ for seed := int64(1); seed <= int64(seedCount); seed++ {
+ seed := seed
+ t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
+ rng := rand.New(rand.NewSource(seed)) //nolint:gosec // deterministic fuzz, not crypto
+ k := 3 + rng.Intn(6) // K ∈ [3,8]
+ muts := entFuzzMutations(t, model, rng, k)
+ t.Logf("[FUZZ %s seed=%d] K=%d mutations:\n %s", srv.name, seed, k, strings.Join(entFzDescs(muts), "\n "))
+
+ err := entFuzzTrial(ctx, t, srv, baseDDL, muts)
+ if err == nil {
+ return
+ }
+ minimized, minErr := entFuzzMinimize(ctx, t, srv, baseDDL, muts, err)
+ t.Errorf("[FUZZ %s seed=%d] FAILED\nminimized mutations (%d of %d):\n %s\nerror:\n%v",
+ srv.name, seed, len(minimized), len(muts), strings.Join(entFzDescs(minimized), "\n "), minErr)
+ })
+ }
+ })
+ }
+}
+
+// A6 of the ENTERPRISE smoke axes: a SEEDED, deterministic
+// MULTI-ROUND STATEFUL fuzzer. The single-round fuzzer (A4)
+// mutates a pristine dump once; real projects mutate the LAST dump repeatedly, so drift
+// and text-accretion bugs that only appear across dump->mutate->apply->dump cycles are
+// invisible to it (real example this campaign: omni used to accrete one paren layer per
+// dump cycle around subquery wrappers until the fold fixed it — a per-round differ never
+// sees the growth).
+//
+// Protocol per seed, on a 30-table Zabbix slice base:
+//
+// dump_0 = canonical dump of the freshly loaded base
+// for round r in 1..5:
+// target_r = mutate(dump_{r-1}, K ∈ [2,5] mutations from the menu)
+// oracle = diff(dump_{r-1} -> target_r) → apply → converge → idempotence
+// dump_r = fresh canonical dump, and ADDITIONALLY:
+// (a) dump_r reloads through the STRICT LoadSDL path (no LoadSQL fallback mask),
+// (b) len(dump_r) stays under len(dump_0) + the cumulative added-object budget
+// (linear ledger — catches unbounded text accretion), and every statement NOT
+// touched by this round's mutations is BYTE-IDENTICAL to its dump_{r-1} form
+// (catches per-cycle accretion and collateral churn on untouched objects),
+// (c) object counts (tables/views/routines/triggers/columns/indexes/FKs/checks)
+// match the mutation ledger exactly.
+//
+// The menu is the A4 menu PLUS (8.0-only where marked): add/drop CHECK constraint (8.0),
+// add/drop FULLTEXT index, add/drop SPATIAL column+index with SRID (8.0 — the SRID
+// attribute is 8.0-only), add/modify/drop STORED and VIRTUAL generated columns, toggle
+// column/index INVISIBLE (8.0), and ENUM member append. 5.7-illegal mutations are gated
+// off the 5.7 menu.
+//
+// Determinism: the mutation stream is a pure function of the seed (the seed is in the
+// subtest name); the schema model is parsed from the previous round's dump in statement
+// order, so candidate lists are stable. Oracle violations are ddmin-minimized (one
+// mutation at a time, each trial reloading dump_{r-1} into a fresh scratch database) and
+// reported with seed + round + minimized ledger. Scratch databases are entsdl_-prefixed
+// and dropped eagerly.
+//
+// ≥12 seeds run on 8.0 and ≥6 on 5.7.
+//
+// Shared helpers (liveServers, createLiveMySQLDriver, newLiveDatabase, dumpSDL, applyDDL,
+// syncMetaForDB, objectCounts, addColumnToTable, addIndexToTable, addColumnToView,
+// dropObjectBlock, dropTrigger, entZabbixSlice, entFuzzSliceTables, entFuzzAux,
+// entNormalizeDelimiters, entDropLineInTable, entReplaceLineInTable, entAppendTableClause,
+// entSetPartitionClause, entFzProtectedTables, entFzProtectedColumns, entFuzzModel,
+// entFzIndexedCols, entFzState, entFzMutation, withDatabaseContext, mysqlVersionFor) come
+// from the sibling files in this package.
+
+const (
+ entSfRounds = 5
+ // entSfRoundSlack is the per-round allowance on top of the per-mutation text budgets
+ // (absorbs benign re-rendering, e.g. a DEFAULT NULL a fresh dump makes explicit).
+ // Deliberately small so systematic accretion across 30+ objects trips the guard.
+ entSfRoundSlack = 400
+)
+
+// ----------------------------------------------------------------------------
+// Stateful-fuzz aux schema: seeds the droppable/modifiable object pool for the NEW menu
+// kinds. entSfAuxCommon is 5.7-legal; entSfAux80 seeds the 8.0-only kinds (SRID spatial,
+// INVISIBLE, CHECK) and is appended on 8.0 only.
+// ----------------------------------------------------------------------------
+
+//go:embed testdata/sdl/ent_sf_aux_common.sql
+var entSfAuxCommon string
+
+//go:embed testdata/sdl/ent_sf_aux_80.sql
+var entSfAux80 string
+
+func entSfBaseDDL(t *testing.T, srv liveServer) string {
+ t.Helper()
+ base := entZabbixSlice(t, entFuzzSliceTables...) + entFuzzAux + entSfAuxCommon
+ if srv.version != "5.7" {
+ base += entSfAux80
+ }
+ return base
+}
+
+// ----------------------------------------------------------------------------
+// Object-count ledger.
+// ----------------------------------------------------------------------------
+
+// entSfCounts is the schema-shape summary compared against the mutation ledger after
+// every round. Comparable (all ints) so `!=` works.
+type entSfCounts struct {
+ tables, views, functions, procedures, triggers int
+ columns, indexes, fks, checks int
+}
+
+func (c entSfCounts) plus(d entSfCounts) entSfCounts {
+ return entSfCounts{
+ tables: c.tables + d.tables, views: c.views + d.views,
+ functions: c.functions + d.functions, procedures: c.procedures + d.procedures,
+ triggers: c.triggers + d.triggers, columns: c.columns + d.columns,
+ indexes: c.indexes + d.indexes, fks: c.fks + d.fks, checks: c.checks + d.checks,
+ }
+}
+
+func entSfCountObjects(meta *model.DatabaseMetadata) entSfCounts {
+ c := entSfCounts{}
+ c.tables, c.views, c.functions, c.procedures, c.triggers = objectCounts(meta)
+ proto := meta.GetProto()
+ if proto == nil {
+ return c
+ }
+ for _, sm := range proto.GetSchemas() {
+ for _, tbl := range sm.GetTables() {
+ c.columns += len(tbl.GetColumns())
+ c.indexes += len(tbl.GetIndexes())
+ c.fks += len(tbl.GetForeignKeys())
+ c.checks += len(tbl.GetCheckConstraints())
+ }
+ }
+ return c
+}
+
+// entSfMutation wraps the replayable A4 mutation with the ledger metadata the stateful
+// protocol needs: the expected object-count delta, the dump-text growth budget, and the
+// statement keys the mutation may legitimately rewrite (everything else must stay
+// byte-identical across the round).
+type entSfMutation struct {
+ entFzMutation
+ delta entSfCounts
+ budget int
+ touched []string
+}
+
+// entSfLedger is the cross-round state: expected counts, and the once-only guards for
+// mutations that cannot repeat on the same object across rounds.
+type entSfLedger struct {
+ expect entSfCounts
+ viewModified map[string]bool
+}
+
+// ----------------------------------------------------------------------------
+// Extended schema model: everything the A4 model parses, plus the constructs the new
+// menu mutates (FULLTEXT/SPATIAL keys, CHECK constraints, generated columns, INVISIBLE
+// columns/indexes, ENUM columns) and the referential protections they imply.
+// ----------------------------------------------------------------------------
+
+type entSfIdxRec struct {
+ table, name, line string
+ cols []string
+ invisible bool
+ fkOverlap bool
+}
+
+type entSfNamedRec struct{ table, name string }
+
+type entSfColRec struct{ table, col string }
+
+type entSfGenRec struct{ table, name, line string }
+
+type entSfSchema struct {
+ base *entFzModel
+ views []string
+ functions []string
+ procedures []string
+ idx []entSfIdxRec // non-PK B-tree indexes, visible AND invisible
+ fulltext []entSfNamedRec
+ spatial []entSfIdxRec // cols[0] is the single spatial column
+ checks []entSfNamedRec
+ genCols []entSfGenRec
+ enumCols []entSfColRec
+ invisCols []entSfColRec
+ // visibleCols counts non-INVISIBLE columns per table (a toggle must leave >= 1).
+ visibleCols map[string]int
+ // protected are "table.col" keys drop/modify must not touch beyond the base model's
+ // own exclusions: fulltext/spatial/invisible-index parts, generated-expression and
+ // CHECK-expression references.
+ protected map[string]bool
+ // noPartition marks tables that cannot gain a partition clause (FULLTEXT/SPATIAL
+ // indexes are unsupported on partitioned tables).
+ noPartition map[string]bool
+}
+
+var (
+ entSfReIdxLine = regexp.MustCompile("^(UNIQUE )?KEY `(\\w+)` \\((.+?)\\)( /\\*!80000 INVISIBLE \\*/)?,?$")
+ entSfReFtLine = regexp.MustCompile("^FULLTEXT KEY `(\\w+)` \\((.+?)\\),?$")
+ entSfReSpLine = regexp.MustCompile("^SPATIAL KEY `(\\w+)` \\(`(\\w+)`\\),?$")
+ entSfReChkLine = regexp.MustCompile("^CONSTRAINT `(\\w+)` CHECK \\((.+)\\),?$")
+ entSfReColLine = regexp.MustCompile("^`(\\w+)` (.+?),?$")
+ entSfReViewHdr = regexp.MustCompile("(?i)^CREATE .*?VIEW `(\\w+)`")
+ entSfReFnHdr = regexp.MustCompile("(?i)^CREATE .*?FUNCTION `(\\w+)`")
+ entSfRePrHdr = regexp.MustCompile("(?i)^CREATE .*?PROCEDURE `(\\w+)`")
+ entSfReGenConst = regexp.MustCompile(`\+ (\d+)\)`)
+ entSfReEnumSpec = regexp.MustCompile(`enum\(([^)]*)\)`)
+)
+
+const (
+ entSfInvisibleColToken = " /*!80023 INVISIBLE */"
+ entSfInvisibleIdxToken = " /*!80000 INVISIBLE */"
+)
+
+// entSfParse builds the extended model from a canonical dump.
+func entSfParse(t *testing.T, source string) *entSfSchema {
+ t.Helper()
+ m := &entSfSchema{
+ base: entFuzzModel(t, source),
+ visibleCols: map[string]int{},
+ protected: map[string]bool{},
+ noPartition: map[string]bool{},
+ }
+ stmts, err := mysqlparser.SplitSQL(source)
+ require.NoError(t, err, "split canonical dump for stateful-fuzz model")
+ for _, s := range stmts {
+ text := strings.TrimSpace(s.Text)
+ upper := strings.ToUpper(text)
+ switch {
+ case strings.HasPrefix(upper, "CREATE TABLE `"):
+ m.parseTableExtras(text)
+ case entSfReViewHdr.MatchString(text):
+ m.views = append(m.views, entSfReViewHdr.FindStringSubmatch(text)[1])
+ case entSfReFnHdr.MatchString(text):
+ m.functions = append(m.functions, entSfReFnHdr.FindStringSubmatch(text)[1])
+ case entSfRePrHdr.MatchString(text):
+ m.procedures = append(m.procedures, entSfRePrHdr.FindStringSubmatch(text)[1])
+ default:
+ }
+ }
+ return m
+}
+
+func (m *entSfSchema) parseTableExtras(stmt string) {
+ header := entReTable.FindStringSubmatch(stmt)
+ if header == nil {
+ return
+ }
+ table := header[1]
+ fkCols := map[string]bool{}
+ if tbl := m.base.byName[table]; tbl != nil {
+ for _, fk := range tbl.fks {
+ for _, c := range fk.cols {
+ fkCols[c] = true
+ }
+ }
+ }
+ for _, raw := range strings.Split(stmt, "\n")[1:] {
+ line := strings.TrimSpace(raw)
+ if line == "" || strings.HasPrefix(line, ")") {
+ break
+ }
+ if mm := entSfReFtLine.FindStringSubmatch(line); mm != nil {
+ m.fulltext = append(m.fulltext, entSfNamedRec{table: table, name: mm[1]})
+ m.noPartition[table] = true
+ for _, c := range entFzReCol.FindAllStringSubmatch(mm[2], -1) {
+ m.protected[table+"."+c[1]] = true
+ }
+ continue
+ }
+ if mm := entSfReSpLine.FindStringSubmatch(line); mm != nil {
+ m.spatial = append(m.spatial, entSfIdxRec{table: table, name: mm[1], cols: []string{mm[2]}})
+ m.noPartition[table] = true
+ m.protected[table+"."+mm[2]] = true
+ continue
+ }
+ if mm := entSfReChkLine.FindStringSubmatch(line); mm != nil {
+ m.checks = append(m.checks, entSfNamedRec{table: table, name: mm[1]})
+ for _, c := range entFzReCol.FindAllStringSubmatch(mm[2], -1) {
+ m.protected[table+"."+c[1]] = true
+ }
+ continue
+ }
+ if mm := entSfReIdxLine.FindStringSubmatch(line); mm != nil {
+ rec := entSfIdxRec{table: table, name: mm[2], line: strings.TrimSuffix(line, ","), invisible: mm[4] != ""}
+ for _, c := range entFzReCol.FindAllStringSubmatch(mm[3], -1) {
+ rec.cols = append(rec.cols, c[1])
+ if rec.invisible {
+ m.protected[table+"."+c[1]] = true
+ }
+ if fkCols[c[1]] {
+ rec.fkOverlap = true
+ }
+ }
+ m.idx = append(m.idx, rec)
+ continue
+ }
+ if mm := entSfReColLine.FindStringSubmatch(line); mm != nil {
+ col, def := mm[1], strings.TrimSuffix(line, ",")
+ if genIdx := strings.Index(def, "GENERATED ALWAYS AS"); genIdx >= 0 {
+ m.genCols = append(m.genCols, entSfGenRec{table: table, name: col, line: def})
+ for _, c := range entFzReCol.FindAllStringSubmatch(def[genIdx:], -1) {
+ m.protected[table+"."+c[1]] = true
+ }
+ }
+ if strings.HasPrefix(strings.TrimPrefix(def, "`"+col+"` "), "enum(") {
+ m.enumCols = append(m.enumCols, entSfColRec{table: table, col: col})
+ }
+ if strings.HasSuffix(def, strings.TrimSpace(entSfInvisibleColToken)) {
+ m.invisCols = append(m.invisCols, entSfColRec{table: table, col: col})
+ } else {
+ m.visibleCols[table]++
+ }
+ }
+ }
+}
+
+// colDef returns the trimmed definition line of table.col from the base model.
+func (m *entSfSchema) colDef(table, col string) string {
+ if tbl := m.base.byName[table]; tbl != nil {
+ return tbl.colLine[col]
+ }
+ return ""
+}
+
+// ----------------------------------------------------------------------------
+// Mutation generation.
+// ----------------------------------------------------------------------------
+
+func entSfMenu(version string) []string {
+ menu := []string{
+ "add_column", "drop_column", "modify_column",
+ "add_index", "drop_index",
+ "add_fk", "drop_fk",
+ "add_view", "drop_view", "modify_view",
+ "add_trigger", "drop_trigger",
+ "add_routine", "drop_routine",
+ "partition", "departition",
+ "add_fulltext", "drop_fulltext",
+ "add_gen_stored", "add_gen_virtual", "modify_gen", "drop_gen",
+ "enum_append",
+ }
+ if version != "5.7" {
+ // 5.7-illegal kinds: CHECK is parsed-and-ignored on 5.7 (nothing syncs back, so
+ // convergence is impossible); the SRID column attribute and column/index
+ // INVISIBLE are 8.0-only syntax.
+ menu = append(menu,
+ "add_check", "drop_check",
+ "add_spatial", "drop_spatial",
+ "toggle_col_invisible", "toggle_idx_invisible",
+ )
+ }
+ return menu
+}
+
+// entSfMutations derives k independent mutations for round r, deterministically in
+// (model order, rng stream).
+func entSfMutations(t *testing.T, m *entSfSchema, rng *rand.Rand, srv liveServer, ledger *entSfLedger, r, k int) []entSfMutation {
+ t.Helper()
+ return entSfMutationsFromMenu(t, m, rng, entSfMenu(srv.version), ledger, r, k)
+}
+
+// entSfMutationsFromMenu is entSfMutations with an explicit menu. The cross-version
+// stateful fuzzer (A8) passes the 5.7-legal menu
+// regardless of the authoring side — its targets apply to BOTH versions.
+func entSfMutationsFromMenu(t *testing.T, m *entSfSchema, rng *rand.Rand, menu []string, ledger *entSfLedger, r, k int) []entSfMutation {
+ t.Helper()
+ state := entFzNewState()
+ var muts []entSfMutation
+ for attempt := 0; attempt < 500 && len(muts) < k; attempt++ {
+ kind := menu[rng.Intn(len(menu))]
+ if mut, ok := entSfGenerate(t, kind, m, rng, state, ledger, r, len(muts)); ok {
+ muts = append(muts, mut)
+ }
+ }
+ require.Len(t, muts, k, "stateful-fuzz generator starved (only %d of %d mutations, round %d)", len(muts), k, r)
+ return muts
+}
+
+// entSfMutableTables returns the non-protected, non-locked tables.
+func entSfMutableTables(m *entSfSchema, state *entFzState) []*entFzTable {
+ var out []*entFzTable
+ for _, tbl := range m.base.tables {
+ if entFzProtectedTables[tbl.name] || state.lockedTables[tbl.name] {
+ continue
+ }
+ out = append(out, tbl)
+ }
+ return out
+}
+
+// entSfColCandidates lists table.col pairs whose (dump-form) definition rest satisfies
+// accept, excluding protected/used/locked/generated columns.
+// entSfIsFKMemberCol reports whether table.col participates in any child-side
+// foreign key of the base model. Used to keep STORED generated columns off FK
+// member bases (A8 finding: both servers refuse that shape; 5.7 opaquely).
+func entSfIsFKMemberCol(m *entSfSchema, table, col string) bool {
+ tbl := m.base.byName[table]
+ if tbl == nil {
+ return false
+ }
+ for _, fk := range tbl.fks {
+ for _, c := range fk.cols {
+ if c == col {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func entSfColCandidates(m *entSfSchema, state *entFzState, accept func(rest string) bool) []entSfColRec {
+ var out []entSfColRec
+ for _, tbl := range entSfMutableTables(m, state) {
+ for _, c := range tbl.cols {
+ key := tbl.name + "." + c
+ if state.usedCols[key] || m.protected[key] || entFzProtectedColumns[key] {
+ continue
+ }
+ def := tbl.colLine[c]
+ if strings.Contains(def, "GENERATED ALWAYS AS") {
+ continue
+ }
+ rest := strings.TrimPrefix(def, "`"+c+"` ")
+ if accept(rest) {
+ out = append(out, entSfColRec{table: tbl.name, col: c})
+ }
+ }
+ }
+ return out
+}
+
+func entSfIsIntRest(rest string) bool {
+ return strings.HasPrefix(rest, "bigint") || strings.HasPrefix(rest, "int") || strings.HasPrefix(rest, "integer")
+}
+
+// entSfGenerate builds one mutation of the given kind, or reports it infeasible. seq
+// namespaces generated object names within the round; r namespaces across rounds.
+//
+//nolint:gocyclo
+func entSfGenerate(t *testing.T, kind string, m *entSfSchema, rng *rand.Rand, state *entFzState, ledger *entSfLedger, r, seq int) (entSfMutation, bool) {
+ t.Helper()
+ name := func(tag string) string { return fmt.Sprintf("ent_sf_r%d_%s%d", r, tag, seq) }
+ tableKey := func(tbl string) []string { return []string{"table:" + tbl} }
+
+ switch kind {
+ case "add_column":
+ tables := entSfMutableTables(m, state)
+ if len(tables) == 0 {
+ return entSfMutation{}, false
+ }
+ tbl := tables[rng.Intn(len(tables))].name
+ defs := []string{"varchar(40) DEFAULT NULL", "int NOT NULL DEFAULT '0'", "decimal(8,2) DEFAULT NULL", "bigint unsigned DEFAULT NULL"}
+ def := defs[rng.Intn(len(defs))]
+ col := name("c")
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_column %s.%s (%s)", tbl, col, def),
+ apply: func(t *testing.T, source string) string {
+ return addColumnToTable(t, source, tbl, "`"+col+"` "+def)
+ },
+ },
+ delta: entSfCounts{columns: 1}, budget: 150, touched: tableKey(tbl),
+ }, true
+
+ case "drop_column":
+ var cands []entSfColRec
+ for _, tbl := range entSfMutableTables(m, state) {
+ indexed := entFzIndexedCols(tbl)
+ trigRef := m.base.triggerCols[tbl.name]
+ for _, c := range tbl.cols {
+ key := tbl.name + "." + c
+ if indexed[c] || trigRef[c] || entFzProtectedColumns[key] || m.protected[key] || state.usedCols[key] {
+ continue
+ }
+ if strings.Contains(tbl.colLine[c], "GENERATED") {
+ continue
+ }
+ cands = append(cands, entSfColRec{table: tbl.name, col: c})
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.col] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("drop_column %s.%s", pick.table, pick.col),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "`"+pick.col+"`")
+ },
+ },
+ delta: entSfCounts{columns: -1}, touched: tableKey(pick.table),
+ }, true
+
+ case "modify_column":
+ type cand struct {
+ rec entSfColRec
+ from, to int
+ }
+ var cands []cand
+ for _, tbl := range entSfMutableTables(m, state) {
+ for _, c := range tbl.cols {
+ key := tbl.name + "." + c
+ if state.usedCols[key] || m.protected[key] {
+ continue
+ }
+ mm := entFzReVarLen.FindStringSubmatch(tbl.colLine[c])
+ if mm == nil || strings.Contains(tbl.colLine[c], "GENERATED") {
+ continue
+ }
+ n := entFzAtoi(mm[1])
+ if n < 8 || n > 150 {
+ continue
+ }
+ cands = append(cands, cand{rec: entSfColRec{table: tbl.name, col: c}, from: n, to: n + 23})
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.rec.table+"."+pick.rec.col] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("modify_column widen %s.%s varchar(%d)->varchar(%d)", pick.rec.table, pick.rec.col, pick.from, pick.to),
+ apply: func(t *testing.T, source string) string {
+ return entReplaceInTable(t, source, pick.rec.table,
+ fmt.Sprintf("`%s` varchar(%d)", pick.rec.col, pick.from),
+ fmt.Sprintf("`%s` varchar(%d)", pick.rec.col, pick.to))
+ },
+ },
+ budget: 80, touched: tableKey(pick.rec.table),
+ }, true
+
+ case "add_index":
+ cands := entSfColCandidates(m, state, func(rest string) bool {
+ if entSfIsIntRest(rest) {
+ return true
+ }
+ mm := entFzReVarLen.FindStringSubmatch(rest)
+ return mm != nil && strings.HasPrefix(rest, "varchar") && entFzAtoi(mm[1]) <= 150
+ })
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ idx := name("i")
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_index %s on %s(%s)", idx, pick.table, pick.col),
+ apply: func(t *testing.T, source string) string {
+ return addIndexToTable(t, source, pick.table, "KEY `"+idx+"` (`"+pick.col+"`)")
+ },
+ },
+ delta: entSfCounts{indexes: 1}, budget: 150, touched: tableKey(pick.table),
+ }, true
+
+ case "drop_index":
+ var cands []entSfIdxRec
+ for _, rec := range m.idx {
+ if rec.invisible || rec.fkOverlap || state.lockedTables[rec.table] || state.usedIndexes[rec.table+"."+rec.name] {
+ continue
+ }
+ cands = append(cands, rec)
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedIndexes[pick.table+"."+pick.name] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("drop_index %s.%s", pick.table, pick.name),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "`"+pick.name+"`")
+ },
+ },
+ delta: entSfCounts{indexes: -1}, touched: tableKey(pick.table),
+ }, true
+
+ case "add_fk":
+ var srcs []*entFzTable
+ for _, tbl := range entSfMutableTables(m, state) {
+ if !tbl.partitioned {
+ srcs = append(srcs, tbl)
+ }
+ }
+ if len(srcs) == 0 {
+ return entSfMutation{}, false
+ }
+ src := srcs[rng.Intn(len(srcs))]
+ var refs []*entFzTable
+ for _, tbl := range m.base.tables {
+ if tbl.partitioned || state.lockedTables[tbl.name] || len(tbl.pk) != 1 {
+ continue
+ }
+ pkCol := ""
+ for c := range tbl.pk {
+ pkCol = c
+ }
+ if strings.HasPrefix(strings.TrimPrefix(tbl.colLine[pkCol], "`"+pkCol+"` "), "bigint") &&
+ strings.Contains(tbl.colLine[pkCol], "unsigned") {
+ refs = append(refs, tbl)
+ }
+ }
+ if len(refs) == 0 {
+ return entSfMutation{}, false
+ }
+ ref := refs[rng.Intn(len(refs))]
+ refPK := ""
+ for c := range ref.pk {
+ refPK = c
+ }
+ srcName, refName := src.name, ref.name
+ state.lockedTables[srcName] = true
+ state.lockedTables[refName] = true
+ col, fk := name("r"), name("fk")
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_fk %s.%s -> %s.%s (%s)", srcName, col, refName, refPK, fk),
+ apply: func(t *testing.T, source string) string {
+ s := addColumnToTable(t, source, srcName, "`"+col+"` bigint unsigned DEFAULT NULL")
+ s = addIndexToTable(t, s, srcName, "KEY `"+fk+"` (`"+col+"`)")
+ return addIndexToTable(t, s, srcName,
+ "CONSTRAINT `"+fk+"` FOREIGN KEY (`"+col+"`) REFERENCES `"+refName+"` (`"+refPK+"`) ON DELETE SET NULL")
+ },
+ },
+ delta: entSfCounts{columns: 1, indexes: 1, fks: 1}, budget: 450, touched: tableKey(srcName),
+ }, true
+
+ case "drop_fk":
+ var cands []entSfNamedRec
+ for _, tbl := range m.base.tables {
+ if state.lockedTables[tbl.name] {
+ continue
+ }
+ for _, fk := range tbl.fks {
+ if !state.usedFKs[tbl.name+"."+fk.name] {
+ cands = append(cands, entSfNamedRec{table: tbl.name, name: fk.name})
+ }
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedFKs[pick.table+"."+pick.name] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("drop_fk %s.%s", pick.table, pick.name),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "CONSTRAINT `"+pick.name+"`")
+ },
+ },
+ delta: entSfCounts{fks: -1}, touched: tableKey(pick.table),
+ }, true
+
+ case "add_view":
+ tables := entSfMutableTables(m, state)
+ if len(tables) == 0 {
+ return entSfMutation{}, false
+ }
+ tbl := tables[rng.Intn(len(tables))]
+ pkCol := ""
+ for _, c := range tbl.cols {
+ if tbl.pk[c] {
+ pkCol = c
+ break
+ }
+ }
+ if pkCol == "" {
+ return entSfMutation{}, false
+ }
+ vw, table := name("v"), tbl.name
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_view %s over %s(%s)", vw, table, pkCol),
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE VIEW %s AS SELECT %s FROM %s;\n", vw, pkCol, table)
+ },
+ },
+ delta: entSfCounts{views: 1}, budget: 600, touched: nil,
+ }, true
+
+ case "drop_view":
+ var cands []string
+ for _, v := range m.views {
+ if !state.usedViews[v] {
+ cands = append(cands, v)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedViews[pick] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: "drop_view " + pick,
+ apply: func(t *testing.T, source string) string {
+ return dropObjectBlock(t, source, "VIEW", pick)
+ },
+ },
+ delta: entSfCounts{views: -1}, touched: []string{"view:" + pick},
+ }, true
+
+ case "modify_view":
+ // The extension is only appendable once per view over the whole seed (a second
+ // append would duplicate the column), so the guard lives in the LEDGER, not the
+ // per-round state.
+ extension := map[string]string{
+ "ent_fz_v1": "`users`.`name` AS `name`",
+ "ent_fz_v2": "`hosts`.`status` AS `status`",
+ }
+ var cands []string
+ for _, v := range m.views {
+ if !state.usedViews[v] && !ledger.viewModified[v] && extension[v] != "" {
+ cands = append(cands, v)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedViews[pick] = true
+ ledger.viewModified[pick] = true
+ ext := extension[pick]
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: "modify_view " + pick,
+ apply: func(t *testing.T, source string) string {
+ return addColumnToView(t, source, pick, ext)
+ },
+ },
+ budget: 80, touched: []string{"view:" + pick},
+ }, true
+
+ case "add_trigger":
+ tables := entSfMutableTables(m, state)
+ if len(tables) == 0 {
+ return entSfMutation{}, false
+ }
+ tbl := tables[rng.Intn(len(tables))]
+ pkCol := ""
+ for _, c := range tbl.cols {
+ if tbl.pk[c] {
+ pkCol = c
+ break
+ }
+ }
+ if pkCol == "" {
+ return entSfMutation{}, false
+ }
+ trg, table := name("t"), tbl.name
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_trigger %s on %s", trg, table),
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE TRIGGER %s BEFORE UPDATE ON %s FOR EACH ROW SET NEW.%s = NEW.%s;\n",
+ trg, table, pkCol, pkCol)
+ },
+ },
+ delta: entSfCounts{triggers: 1}, budget: 500, touched: nil,
+ }, true
+
+ case "drop_trigger":
+ var cands []string
+ for _, trg := range m.base.triggers {
+ if !state.usedTriggers[trg] {
+ cands = append(cands, trg)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedTriggers[pick] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: "drop_trigger " + pick,
+ apply: func(t *testing.T, source string) string {
+ return dropTrigger(t, source, pick)
+ },
+ },
+ delta: entSfCounts{triggers: -1}, touched: []string{"trigger:" + pick},
+ }, true
+
+ case "add_routine":
+ if rng.Intn(2) == 0 {
+ fn := name("fn")
+ ret := 100*r + seq
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: "add_routine function " + fn,
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE FUNCTION %s() RETURNS INT DETERMINISTIC RETURN %d;\n", fn, ret)
+ },
+ },
+ delta: entSfCounts{functions: 1}, budget: 500, touched: nil,
+ }, true
+ }
+ pr := name("pr")
+ sel := 200*r + seq
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: "add_routine procedure " + pr,
+ apply: func(_ *testing.T, source string) string {
+ return source + fmt.Sprintf("\nCREATE PROCEDURE %s() BEGIN SELECT %d; END;\n", pr, sel)
+ },
+ },
+ delta: entSfCounts{procedures: 1}, budget: 500, touched: nil,
+ }, true
+
+ case "drop_routine":
+ type cand struct{ kind, name string }
+ var cands []cand
+ for _, fn := range m.functions {
+ if !state.usedRoutines[fn] {
+ cands = append(cands, cand{"FUNCTION", fn})
+ }
+ }
+ for _, pr := range m.procedures {
+ if !state.usedRoutines[pr] {
+ cands = append(cands, cand{"PROCEDURE", pr})
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedRoutines[pick.name] = true
+ delta := entSfCounts{functions: -1}
+ key := "function:" + pick.name
+ if pick.kind == "PROCEDURE" {
+ delta = entSfCounts{procedures: -1}
+ key = "procedure:" + pick.name
+ }
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: "drop_routine " + strings.ToLower(pick.kind) + " " + pick.name,
+ apply: func(t *testing.T, source string) string {
+ return dropObjectBlock(t, source, pick.kind, pick.name)
+ },
+ },
+ delta: delta, touched: []string{key},
+ }, true
+
+ case "partition":
+ var cands []*entFzTable
+ for _, tbl := range m.base.tables {
+ if tbl.partitioned || state.lockedTables[tbl.name] || m.noPartition[tbl.name] {
+ continue
+ }
+ if len(tbl.fks) > 0 || m.base.referenced[tbl.name] || len(tbl.pk) != 1 {
+ continue
+ }
+ hasUnique := false
+ for _, idx := range tbl.indexes {
+ if idx.unique {
+ hasUnique = true
+ break
+ }
+ }
+ if hasUnique {
+ continue
+ }
+ cands = append(cands, tbl)
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ pkCol := ""
+ for c := range pick.pk {
+ pkCol = c
+ }
+ state.lockedTables[pick.name] = true
+ table := pick.name
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("partition %s by hash(%s)", table, pkCol),
+ apply: func(t *testing.T, source string) string {
+ return entAppendTableClause(t, source, table, "PARTITION BY HASH (`"+pkCol+"`)\nPARTITIONS 4")
+ },
+ },
+ budget: 300, touched: tableKey(table),
+ }, true
+
+ case "departition":
+ var cands []*entFzTable
+ for _, tbl := range m.base.tables {
+ if tbl.partitioned && !state.lockedTables[tbl.name] {
+ cands = append(cands, tbl)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.lockedTables[pick.name] = true
+ table := pick.name
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: "departition " + table,
+ apply: func(t *testing.T, source string) string {
+ return entSetPartitionClause(t, source, table, "")
+ },
+ },
+ touched: tableKey(table),
+ }, true
+
+ case "add_fulltext":
+ // One FULLTEXT creation per table per round (InnoDB builds them one at a time),
+ // and never on a partitioned table — the whole table is locked for the round.
+ var cands []entSfColRec
+ for _, rec := range entSfColCandidates(m, state, func(rest string) bool {
+ return strings.HasPrefix(rest, "varchar") || strings.HasPrefix(rest, "text") ||
+ strings.HasPrefix(rest, "mediumtext") || strings.HasPrefix(rest, "longtext")
+ }) {
+ if tbl := m.base.byName[rec.table]; tbl != nil && tbl.partitioned {
+ continue
+ }
+ cands = append(cands, rec)
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.lockedTables[pick.table] = true
+ idx := name("ft")
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_fulltext %s on %s(%s)", idx, pick.table, pick.col),
+ apply: func(t *testing.T, source string) string {
+ return addIndexToTable(t, source, pick.table, "FULLTEXT KEY `"+idx+"` (`"+pick.col+"`)")
+ },
+ },
+ delta: entSfCounts{indexes: 1}, budget: 150, touched: tableKey(pick.table),
+ }, true
+
+ case "drop_fulltext":
+ var cands []entSfNamedRec
+ for _, rec := range m.fulltext {
+ if !state.lockedTables[rec.table] && !state.usedIndexes[rec.table+"."+rec.name] {
+ cands = append(cands, rec)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedIndexes[pick.table+"."+pick.name] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("drop_fulltext %s.%s", pick.table, pick.name),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "FULLTEXT KEY `"+pick.name+"`")
+ },
+ },
+ delta: entSfCounts{indexes: -1}, touched: tableKey(pick.table),
+ }, true
+
+ case "add_spatial":
+ // A NOT NULL POINT column with an SRID plus its SPATIAL index, in one mutation.
+ // SPATIAL indexes are unsupported on partitioned tables; the table is locked so
+ // no same-round mutation interleaves with the two-clause add.
+ var cands []*entFzTable
+ for _, tbl := range entSfMutableTables(m, state) {
+ if !tbl.partitioned {
+ cands = append(cands, tbl)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.lockedTables[pick.name] = true
+ table, col, idx := pick.name, name("sp"), name("spi")
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_spatial %s.%s (SRID 4326) + %s", table, col, idx),
+ apply: func(t *testing.T, source string) string {
+ s := addColumnToTable(t, source, table, "`"+col+"` point NOT NULL /*!80003 SRID 4326 */")
+ return addIndexToTable(t, s, table, "SPATIAL KEY `"+idx+"` (`"+col+"`)")
+ },
+ },
+ delta: entSfCounts{columns: 1, indexes: 1}, budget: 400, touched: tableKey(table),
+ }, true
+
+ case "drop_spatial":
+ // Index and column leave together (a dangling SPATIAL KEY over a dropped column
+ // would be an invalid target).
+ var cands []entSfIdxRec
+ for _, rec := range m.spatial {
+ if !state.lockedTables[rec.table] {
+ cands = append(cands, rec)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.lockedTables[pick.table] = true
+ col := pick.cols[0]
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("drop_spatial %s.%s (+column %s)", pick.table, pick.name, col),
+ apply: func(t *testing.T, source string) string {
+ s := entDropLineInTable(t, source, pick.table, "SPATIAL KEY `"+pick.name+"`")
+ return entDropLineInTable(t, s, pick.table, "`"+col+"` point")
+ },
+ },
+ delta: entSfCounts{columns: -1, indexes: -1}, touched: tableKey(pick.table),
+ }, true
+
+ case "add_check":
+ cands := entSfColCandidates(m, state, entSfIsIntRest)
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.col] = true
+ chk := name("ck")
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("add_check %s on %s(%s)", chk, pick.table, pick.col),
+ apply: func(t *testing.T, source string) string {
+ return addIndexToTable(t, source, pick.table, "CONSTRAINT `"+chk+"` CHECK ((`"+pick.col+"` >= 0))")
+ },
+ },
+ delta: entSfCounts{checks: 1}, budget: 250, touched: tableKey(pick.table),
+ }, true
+
+ case "drop_check":
+ var cands []entSfNamedRec
+ for _, rec := range m.checks {
+ if !state.lockedTables[rec.table] && !state.usedIndexes[rec.table+"."+rec.name] {
+ cands = append(cands, rec)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedIndexes[pick.table+"."+pick.name] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("drop_check %s.%s", pick.table, pick.name),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "CONSTRAINT `"+pick.name+"`")
+ },
+ },
+ delta: entSfCounts{checks: -1}, touched: tableKey(pick.table),
+ }, true
+
+ case "add_gen_stored", "add_gen_virtual":
+ mode, tag := "STORED", "gs"
+ if kind == "add_gen_virtual" {
+ mode, tag = "VIRTUAL", "gv"
+ }
+ cands := entSfColCandidates(m, state, entSfIsIntRest)
+ if mode == "STORED" {
+ // A8 finding: BOTH servers refuse a STORED generated column whose base
+ // column is a child-side FK member (8.0 clean 1215; 5.7 opaque errno
+ // 150 at ALGORITHM=COPY rename). VIRTUAL stays eligible.
+ kept := cands[:0]
+ for _, c := range cands {
+ if !entSfIsFKMemberCol(m, c.table, c.col) {
+ kept = append(kept, c)
+ }
+ }
+ cands = kept
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.col] = true
+ col := name(tag)
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("%s %s.%s as (%s + 7)", kind, pick.table, col, pick.col),
+ apply: func(t *testing.T, source string) string {
+ return addColumnToTable(t, source, pick.table,
+ "`"+col+"` bigint GENERATED ALWAYS AS ((`"+pick.col+"` + 7)) "+mode)
+ },
+ },
+ delta: entSfCounts{columns: 1}, budget: 300, touched: tableKey(pick.table),
+ }, true
+
+ case "modify_gen":
+ // Bump the additive constant of a fuzz-shaped generated expression (`col` + N).
+ var cands []entSfGenRec
+ for _, rec := range m.genCols {
+ if state.lockedTables[rec.table] || state.usedCols[rec.table+"."+rec.name] {
+ continue
+ }
+ if entSfReGenConst.MatchString(rec.line) {
+ cands = append(cands, rec)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.name] = true
+ old := entSfReGenConst.FindStringSubmatch(pick.line)
+ newLine := entSfReGenConst.ReplaceAllString(pick.line, fmt.Sprintf("+ %d)", entFzAtoi(old[1])+1))
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("modify_gen %s.%s (+%s -> +%d)", pick.table, pick.name, old[1], entFzAtoi(old[1])+1),
+ apply: func(t *testing.T, source string) string {
+ return entReplaceLineInTable(t, source, pick.table, "`"+pick.name+"`", newLine)
+ },
+ },
+ budget: 80, touched: tableKey(pick.table),
+ }, true
+
+ case "drop_gen":
+ var cands []entSfGenRec
+ for _, rec := range m.genCols {
+ if state.lockedTables[rec.table] || state.usedCols[rec.table+"."+rec.name] || entFzProtectedTables[rec.table] {
+ continue
+ }
+ cands = append(cands, rec)
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.name] = true
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("drop_gen %s.%s", pick.table, pick.name),
+ apply: func(t *testing.T, source string) string {
+ return entDropLineInTable(t, source, pick.table, "`"+pick.name+"`")
+ },
+ },
+ delta: entSfCounts{columns: -1}, touched: tableKey(pick.table),
+ }, true
+
+ case "toggle_col_invisible":
+ // Visible -> invisible needs >= 2 visible columns left; invisible -> visible is
+ // always legal. Both directions render/strip the /*!80023 INVISIBLE */ token the
+ // dumper and omni loader agree on.
+ type cand struct {
+ rec entSfColRec
+ on bool
+ }
+ var cands []cand
+ for _, rec := range m.invisCols {
+ key := rec.table + "." + rec.col
+ if !state.usedCols[key] && !state.lockedTables[rec.table] && !entFzProtectedTables[rec.table] {
+ cands = append(cands, cand{rec: rec, on: false})
+ }
+ }
+ for _, rec := range entSfColCandidates(m, state, func(rest string) bool {
+ return entSfIsIntRest(rest) || strings.HasPrefix(rest, "varchar")
+ }) {
+ if m.visibleCols[rec.table] < 2 {
+ continue
+ }
+ if strings.Contains(m.colDef(rec.table, rec.col), "INVISIBLE") {
+ continue
+ }
+ cands = append(cands, cand{rec: rec, on: true})
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.rec.table+"."+pick.rec.col] = true
+ line := m.colDef(pick.rec.table, pick.rec.col)
+ newLine := line + entSfInvisibleColToken
+ dir := "->invisible"
+ if !pick.on {
+ newLine = strings.TrimSuffix(line, entSfInvisibleColToken)
+ dir = "->visible"
+ }
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("toggle_col_invisible %s.%s %s", pick.rec.table, pick.rec.col, dir),
+ apply: func(t *testing.T, source string) string {
+ return entReplaceLineInTable(t, source, pick.rec.table, "`"+pick.rec.col+"`", newLine)
+ },
+ },
+ budget: 60, touched: tableKey(pick.rec.table),
+ }, true
+
+ case "toggle_idx_invisible":
+ var cands []entSfIdxRec
+ for _, rec := range m.idx {
+ if rec.fkOverlap || state.lockedTables[rec.table] || state.usedIndexes[rec.table+"."+rec.name] {
+ continue
+ }
+ cands = append(cands, rec)
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedIndexes[pick.table+"."+pick.name] = true
+ newLine := pick.line + entSfInvisibleIdxToken
+ dir := "->invisible"
+ if pick.invisible {
+ newLine = strings.TrimSuffix(pick.line, entSfInvisibleIdxToken)
+ dir = "->visible"
+ }
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("toggle_idx_invisible %s.%s %s", pick.table, pick.name, dir),
+ apply: func(t *testing.T, source string) string {
+ return entReplaceLineInTable(t, source, pick.table, "`"+pick.name+"`", newLine)
+ },
+ },
+ budget: 60, touched: tableKey(pick.table),
+ }, true
+
+ case "enum_append":
+ var cands []entSfColRec
+ for _, rec := range m.enumCols {
+ key := rec.table + "." + rec.col
+ if !state.usedCols[key] && !state.lockedTables[rec.table] && !entFzProtectedTables[rec.table] && !m.protected[key] {
+ cands = append(cands, rec)
+ }
+ }
+ if len(cands) == 0 {
+ return entSfMutation{}, false
+ }
+ pick := cands[rng.Intn(len(cands))]
+ state.usedCols[pick.table+"."+pick.col] = true
+ line := m.colDef(pick.table, pick.col)
+ member := fmt.Sprintf("m%d_%d", r, seq)
+ newLine := entSfReEnumSpec.ReplaceAllString(line, "enum($1,'"+member+"')")
+ if newLine == line {
+ return entSfMutation{}, false
+ }
+ return entSfMutation{
+ entFzMutation: entFzMutation{
+ desc: fmt.Sprintf("enum_append %s.%s += '%s'", pick.table, pick.col, member),
+ apply: func(t *testing.T, source string) string {
+ return entReplaceLineInTable(t, source, pick.table, "`"+pick.col+"`", newLine)
+ },
+ },
+ budget: 60, touched: tableKey(pick.table),
+ }, true
+
+ default:
+ return entSfMutation{}, false
+ }
+}
+
+// ----------------------------------------------------------------------------
+// Round runner, trial (for ddmin), and the untouched-statement stability guard.
+// ----------------------------------------------------------------------------
+
+// entSfScratchDB creates an eagerly-droppable scratch database (minimization churns
+// through many databases inside a single test).
+func entSfScratchDB(ctx context.Context, t *testing.T, srv liveServer, prefix string) (string, func()) {
+ t.Helper()
+ dbName := newLiveDatabase(ctx, t, srv, prefix)
+ drop := func() {
+ c, err := createLiveMySQLDriver(ctx, srv, "")
+ if err != nil {
+ return
+ }
+ defer c.Close(ctx)
+ _, _ = c.Execute(ctx, "DROP DATABASE IF EXISTS `"+dbName+"`", db.ExecuteOptions{})
+ }
+ return dbName, drop
+}
+
+// entSfRound runs one full stateful round against an already-loaded database: mutate
+// source textually, oracle (diff → apply → converge → idempotence), then the round-local
+// guards — (a) the fresh dump reloads through the STRICT LoadSDL path, (c) object counts
+// match want. Infrastructure failures fail t hard; protocol violations return an error
+// so the caller can ddmin. Returns the fresh canonical dump.
+func entSfRound(ctx context.Context, t *testing.T, srv liveServer, dbName, source string, muts []entSfMutation, want entSfCounts) (string, error) {
+ t.Helper()
+ target := source
+ for _, m := range muts {
+ target = m.apply(t, target)
+ }
+ after, _, err := entSfRoundToTarget(ctx, t, srv, dbName, source, target, srv.version, want)
+ return after, err
+}
+
+// entSfRoundToTarget is the target-explicit core of entSfRound: the caller supplies the
+// mutated target text and the version threaded into every diff. The cross-version
+// stateful fuzzer (A8) authors ONE target from one side's dump and drives BOTH sides'
+// databases through it, so the target cannot be derived from this side's source. Returns
+// the fresh canonical dump and the generated plan.
+func entSfRoundToTarget(ctx context.Context, t *testing.T, srv liveServer, dbName, source, target, version string, want entSfCounts) (string, string, error) {
+ t.Helper()
+ plan, err := mysqlDiffSDLMigration(source, target, version)
+ if err != nil {
+ return "", "", errors.Wrap(err, "diff failed")
+ }
+ if strings.TrimSpace(plan) == "" {
+ return "", "", errors.New("empty plan for a non-empty mutation set")
+ }
+ if err := applyDDL(ctx, t, srv, dbName, plan); err != nil {
+ return "", "", errors.Wrapf(err, "plan failed to apply; plan was:\n%s", plan)
+ }
+ after := dumpSDL(ctx, t, srv, dbName)
+ converge, err := mysqlDiffSDLMigration(after, target, version)
+ if err != nil {
+ return "", "", errors.Wrap(err, "converge diff failed")
+ }
+ if converge != "" {
+ return "", "", errors.Errorf("did not converge; residual:\n%s\nplan was:\n%s", converge, plan)
+ }
+ self, err := mysqlDiffSDLMigration(after, after, version)
+ if err != nil {
+ return "", "", errors.Wrap(err, "idempotence diff failed")
+ }
+ if self != "" {
+ return "", "", errors.Errorf("post-apply dump not idempotent:\n%s", self)
+ }
+ // (a) LoadSDL must accept its own output — checked directly (DiffSDLMigration would
+ // mask an SDL rejection behind the LoadSQL fallback).
+ if _, err := catalog.LoadSDLWithVersion(withDatabaseContext(after), mysqlVersionFor(version)); err != nil {
+ return "", "", errors.Wrap(err, "canonical dump does not reload through LoadSDL")
+ }
+ // (c) object counts must match the mutation ledger.
+ got := entSfCountObjects(syncMetaForDB(ctx, t, srv, dbName))
+ if got != want {
+ return "", "", errors.Errorf("object counts diverge from the mutation ledger:\n want %+v\n got %+v\nplan was:\n%s", want, got, plan)
+ }
+ return after, plan, nil
+}
+
+// entSfTrial reproduces one round from scratch: load baseSDL (a canonical dump) into a
+// fresh database and run entSfRound over it. Used by ddmin.
+func entSfTrial(ctx context.Context, t *testing.T, srv liveServer, baseSDL string, muts []entSfMutation) error {
+ t.Helper()
+ dbName, drop := entSfScratchDB(ctx, t, srv, "entsdl_sf")
+ defer drop()
+
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ // FOREIGN_KEY_CHECKS off for the session: baseSDL here is a CANONICAL dump
+ // (alphabetical table order), so inline foreign keys may forward-reference —
+ // same handling as entA8ScratchFromDump.
+ _, err = driver.Execute(ctx, "SET FOREIGN_KEY_CHECKS=0;\n"+entNormalizeDelimiters(baseSDL), db.ExecuteOptions{})
+ driver.Close(ctx)
+ require.NoError(t, err, "[%s] stateful-fuzz trial base failed to load", srv.name)
+
+ want := entSfCountObjects(syncMetaForDB(ctx, t, srv, dbName))
+ for _, m := range muts {
+ want = want.plus(m.delta)
+ }
+ source := dumpSDL(ctx, t, srv, dbName)
+ _, err = entSfRound(ctx, t, srv, dbName, source, muts, want)
+ return err
+}
+
+// entSfMinimize greedily shrinks a failing round's mutation set (ddmin-lite, mirroring
+// entFuzzMinimize but over the stateful trial).
+func entSfMinimize(ctx context.Context, t *testing.T, srv liveServer, baseSDL string, muts []entSfMutation, firstErr error) ([]entSfMutation, error) {
+ t.Helper()
+ return entSfMinimizeWith(muts, firstErr, func(trial []entSfMutation) error {
+ return entSfTrial(ctx, t, srv, baseSDL, trial)
+ })
+}
+
+// entSfMinimizeWith is the trial-generic ddmin-lite core, shared by the same-version
+// (A6) and cross-version (A8) stateful fuzzers.
+func entSfMinimizeWith(muts []entSfMutation, firstErr error, trial func([]entSfMutation) error) ([]entSfMutation, error) {
+ minimized := muts
+ lastErr := firstErr
+ for i := 0; i < len(minimized) && len(minimized) > 1; {
+ candidate := make([]entSfMutation, 0, len(minimized)-1)
+ candidate = append(candidate, minimized[:i]...)
+ candidate = append(candidate, minimized[i+1:]...)
+ if err := trial(candidate); err != nil {
+ minimized = candidate
+ lastErr = err
+ continue
+ }
+ i++
+ }
+ return minimized, lastErr
+}
+
+func entSfDescs(muts []entSfMutation) []string {
+ out := make([]string, len(muts))
+ for i, m := range muts {
+ out[i] = m.desc
+ }
+ return out
+}
+
+// entSfStmtKeys splits a canonical dump and keys every statement by kind:name so the
+// stability guard can pair statements across rounds.
+func entSfStmtKeys(t *testing.T, dump string) map[string]string {
+ t.Helper()
+ stmts, err := mysqlparser.SplitSQL(dump)
+ require.NoError(t, err, "split dump for stability keys")
+ out := map[string]string{}
+ for i, s := range stmts {
+ text := strings.TrimSpace(s.Text)
+ if text == "" || text == ";" {
+ continue
+ }
+ key := ""
+ switch {
+ case entReTable.MatchString(text):
+ key = "table:" + entReTable.FindStringSubmatch(text)[1]
+ case entSfReViewHdr.MatchString(text):
+ key = "view:" + entSfReViewHdr.FindStringSubmatch(text)[1]
+ case entSfReFnHdr.MatchString(text):
+ key = "function:" + entSfReFnHdr.FindStringSubmatch(text)[1]
+ case entSfRePrHdr.MatchString(text):
+ key = "procedure:" + entSfRePrHdr.FindStringSubmatch(text)[1]
+ case entFzReTrigger.MatchString(text):
+ key = "trigger:" + entFzReTrigger.FindStringSubmatch(text)[1]
+ default:
+ key = fmt.Sprintf("other:%d", i)
+ }
+ out[key] = text
+ }
+ return out
+}
+
+// entSfAssertUntouchedStable proves every statement NOT named by this round's mutations
+// is byte-identical across the round — the sharp form of the accretion guard (the
+// historical paren-accretion bug regenerated untouched objects with one extra wrapper
+// per cycle while converging and self-diffing empty).
+func entSfAssertUntouchedStable(t *testing.T, srv liveServer, seed int64, r int, prev, after string, muts []entSfMutation) {
+ t.Helper()
+ touched := map[string]bool{}
+ for _, m := range muts {
+ for _, k := range m.touched {
+ touched[k] = true
+ }
+ }
+ prevKeys := entSfStmtKeys(t, prev)
+ afterKeys := entSfStmtKeys(t, after)
+ keys := make([]string, 0, len(prevKeys))
+ for k := range prevKeys {
+ if _, ok := afterKeys[k]; ok && !touched[k] {
+ keys = append(keys, k)
+ }
+ }
+ slices.Sort(keys)
+ for _, k := range keys {
+ require.Equal(t, prevKeys[k], afterKeys[k],
+ "[STATEFUZZ %s seed=%d round=%d] untouched object %s changed text across the round (collateral churn / accretion)",
+ srv.name, seed, r, k)
+ }
+}
+
+// ----------------------------------------------------------------------------
+// The stateful fuzz test: >=12 seeds on 8.0, >=6 on 5.7, 5 rounds each.
+// ----------------------------------------------------------------------------
+
+func entSfRunSeed(ctx context.Context, t *testing.T, srv liveServer, baseDDL string, seed int64) {
+ t.Helper()
+ rng := rand.New(rand.NewSource(seed)) //nolint:gosec // deterministic fuzz, not crypto
+
+ dbName, drop := entSfScratchDB(ctx, t, srv, "entsdl_sf")
+ defer drop()
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ _, err = driver.Execute(ctx, entNormalizeDelimiters(baseDDL), db.ExecuteOptions{})
+ driver.Close(ctx)
+ require.NoError(t, err, "[%s] stateful-fuzz base failed to load", srv.name)
+
+ dump0 := dumpSDL(ctx, t, srv, dbName)
+ ledger := &entSfLedger{
+ expect: entSfCountObjects(syncMetaForDB(ctx, t, srv, dbName)),
+ viewModified: map[string]bool{},
+ }
+ prev := dump0
+ budget := 0
+ var history []string
+
+ for r := 1; r <= entSfRounds; r++ {
+ m := entSfParse(t, prev)
+ k := 2 + rng.Intn(4) // K ∈ [2,5]
+ muts := entSfMutations(t, m, rng, srv, ledger, r, k)
+ for _, mut := range muts {
+ history = append(history, fmt.Sprintf("r%d: %s", r, mut.desc))
+ }
+ t.Logf("[STATEFUZZ %s seed=%d round=%d] K=%d mutations:\n %s",
+ srv.name, seed, r, k, strings.Join(entSfDescs(muts), "\n "))
+
+ want := ledger.expect
+ for _, mut := range muts {
+ want = want.plus(mut.delta)
+ }
+ after, err := entSfRound(ctx, t, srv, dbName, prev, muts, want)
+ if err != nil {
+ minimized, minErr := entSfMinimize(ctx, t, srv, prev, muts, err)
+ t.Fatalf("[STATEFUZZ %s seed=%d round=%d] FAILED\nminimized mutations (%d of %d):\n %s\nerror:\n%v\nseed history:\n %s",
+ srv.name, seed, r, len(minimized), len(muts), strings.Join(entSfDescs(minimized), "\n "),
+ minErr, strings.Join(history, "\n "))
+ }
+ ledger.expect = want
+
+ // (b) accretion guards: the linear text budget and the untouched-statement
+ // stability check.
+ for _, mut := range muts {
+ budget += mut.budget
+ }
+ require.Less(t, len(after), len(dump0)+budget+entSfRoundSlack*r,
+ "[STATEFUZZ %s seed=%d round=%d] dump grew past the added-object budget: len(dump_%d)=%d, len(dump_0)=%d, budget=%d — text accretion suspected.\nseed history:\n %s",
+ srv.name, seed, r, r, len(after), len(dump0), budget+entSfRoundSlack*r, strings.Join(history, "\n "))
+ entSfAssertUntouchedStable(t, srv, seed, r, prev, after, muts)
+
+ t.Logf("[STATEFUZZ %s seed=%d round=%d] ok: dump %d -> %d bytes (budget %d), counts %+v",
+ srv.name, seed, r, len(prev), len(after), len(dump0)+budget+entSfRoundSlack*r, ledger.expect)
+ prev = after
+ }
+}
+
+//nolint:tparallel
+func TestSDLEnterpriseStatefulFuzz(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+
+ for _, srv := range liveServers {
+ srv := srv
+ seedCount := 12
+ if srv.version == "5.7" {
+ seedCount = 6
+ }
+ t.Run(srv.name, func(t *testing.T) {
+ baseDDL := entSfBaseDDL(t, srv)
+ for seed := int64(1); seed <= int64(seedCount); seed++ {
+ seed := seed
+ t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
+ entSfRunSeed(ctx, t, srv, baseDDL, seed)
+ })
+ }
+ })
+ }
+}
+
+// A8 of the ENTERPRISE smoke axes: CROSS-VERSION STATEFUL
+// FUZZ — the composition of A6 (same-version multi-round stateful fuzz) and A7
+// (single-shot cross-version upgrade). Real teams run MIXED fleets: a schema authored
+// and dumped on 5.7 drives targets applied to 8.0 (and vice versa during migrations),
+// repeatedly, while the schema keeps evolving. A6 and A7 both pass in isolation; this
+// axis hunts what only their composition shows — version-normalization drift
+// COMPOUNDING across rounds (a residual one round becomes the authored text of the
+// next, so a per-round phantom that a single-shot test shrugs off snowballs here).
+//
+// Protocol per seed, on the 5.7-legal A6 base (30-table Zabbix slice + fuzz aux +
+// stateful aux; the 8.0-only aux tables are excluded — the SAME logical schema must
+// load on BOTH versions), both databases collation-aligned (utf8mb4/utf8mb4_general_ci,
+// the A7 aligned-probe posture) so the fleets start logically identical:
+//
+// round 0: the two fresh dumps must already cross-diff EMPTY (all four
+// direction x version-normalization combos).
+// for round r in 1..4:
+// T_r = mutate(authored side's dump_{r-1}, K ∈ [2,4] from the 5.7-LEGAL menu)
+// each side s ∈ {5.7, 8.0}: plan_s = Diff(dump_s, T_r, full server version) must
+// be non-empty, apply cleanly, converge (re-dump diffs empty against T_r
+// under s's version), self-diff empty, reload through STRICT LoadSDL, and
+// match the mutation ledger's object counts (per-side ledgers: baselines may
+// differ, deltas are shared);
+// plan_80 must carry no 5.7 integer display width in any ALTER (entUpgAssert-
+// NoWidthInAlters — a leaked width is a phantom column MODIFY);
+// CROSS-CHECK: the two fresh dumps must cross-diff EMPTY in all four combos —
+// the fleets stay logically identical despite version-different stored forms;
+// ACCRETION (A6's guards, on BOTH sides): each side's dump stays under its own
+// dump_0 length + the cumulative added-object budget, and every statement not
+// named by this round's mutations is byte-identical to its round-start form.
+//
+// forward_57_authored seeds mutate the 5.7 dump; reverse_80_authored seeds mutate the
+// 8.0 dump (the authored text then carries NO display widths while the 5.7 side's
+// stored form does — the opposite normalization direction). Both directions use the
+// 5.7-legal menu: every target applies to both fleets, so 5.7-illegal mutations (CHECK,
+// SRID spatial, INVISIBLE) are structurally excluded. Additionally, child-side FK
+// member columns are protected from the generated-column kinds — both servers refuse
+// a STORED generated column based on a cascading-FK member (see
+// entA8ProtectFKMemberColumns for the empirical matrix this axis established).
+//
+// Determinism: the mutation stream is a pure function of the seed (in the subtest
+// name). Failures ddmin-minimize over a cross-version trial (both round-start dumps
+// reloaded into fresh aligned scratch databases) and report seed + round + the failing
+// check (apply/converge/idempotence/reload/counts per side, or the cross combo) + the
+// minimized ledger. Accretion-guard failures are terminal requires with the same
+// seed/round/check labeling. Scratch databases are entsdl_-prefixed and dropped.
+//
+// ≥8 forward seeds + ≥4 reverse seeds.
+//
+// Shared machinery: entSfBaseDDL, entSfParse, entSfMenu, entSfMutationsFromMenu,
+// entSfRoundToTarget, entSfMinimizeWith, entSfScratchDB, entSfCountObjects,
+// entSfAssertUntouchedStable, entSfDescs, entSfLedger, entSfRoundSlack (A6);
+// entUpgServers, entUpgLoadAligned, entUpgAssertNoWidthInAlters, entUpgTrim,
+// entUpg57Version, entUpg80Version (A7); liveServers, dumpSDL, syncMetaForDB,
+// newLiveDatabase, createLiveMySQLDriver, entNormalizeDelimiters (siblings).
+
+const (
+ entA8Rounds = 4
+ entA8ForwardSeeds = 8
+ entA8ReverseSeeds = 4
+)
+
+// entA8Fleet is one side of the mixed-version fleet.
+type entA8Fleet struct {
+ srv liveServer
+ // version is the FULL server version threaded into every diff for this side (the
+ // release path passes the synced version string, not a bare major.minor).
+ version string
+ dbName string
+ prev string // canonical dump at the current round boundary
+ dump0 int // round-0 dump length (text-accretion baseline)
+ budget int // cumulative added-object text budget
+ expect entSfCounts
+}
+
+// entA8CrossCheck proves the two fleets are logically identical: all four
+// (direction x version-normalization) cross-diffs of the two canonical dumps must be
+// empty. The two "apply" pairings mirror A7's aligned probe — the plan a fleet would
+// receive to be driven to the OTHER fleet's schema; the remaining two probe the
+// canonicalizer's version symmetry (the same pair re-judged under the other version's
+// stored form).
+func entA8CrossCheck(d57, d80 string) error {
+ for _, c := range []struct{ label, source, target, version string }{
+ {"80db<=57dump(as 8.0.32)", d80, d57, entUpg80Version},
+ {"57db<=80dump(as 5.7.25)", d57, d80, entUpg57Version},
+ {"57src->80tgt(as 8.0.32)", d57, d80, entUpg80Version},
+ {"80src->57tgt(as 5.7.25)", d80, d57, entUpg57Version},
+ } {
+ plan, err := mysqlDiffSDLMigration(c.source, c.target, c.version)
+ if err != nil {
+ return errors.Wrapf(err, "check=cross %s: diff failed", c.label)
+ }
+ if strings.TrimSpace(plan) != "" {
+ return errors.Errorf("check=cross %s: fleets drifted apart; residual plan:\n%s", c.label, entUpgTrim(plan))
+ }
+ }
+ return nil
+}
+
+// entA8ProtectFKMemberColumns marks child-side foreign-key member columns as protected
+// in the mutation-planning model, keeping the A8 menu off a server-refused shape this
+// axis found empirically: BOTH oracle servers reject a STORED generated column whose
+// base column participates in a foreign key with a cascading referential action —
+// 8.0.32 with a clean 1215 "Cannot add foreign key constraint" at DDL time, 5.7.25
+// with an opaque errno-150 failure at ALGORITHM=COPY rename (the shape seed 18 of a
+// widened sweep minimized to: ADD COLUMN ... GENERATED ALWAYS AS ((groupid + 7)) STORED
+// on hosts_groups, whose groupid carries zabbix's usual ON DELETE CASCADE FK).
+// Parent-side referenced columns, VIRTUAL generated columns, and RESTRICT/NO ACTION
+// FKs are all accepted by both servers and stay eligible. The guard is
+// action-agnostic (membership, not action): zabbix FKs are almost all
+// ON DELETE CASCADE and the add_fk mutation plants ON DELETE SET NULL, so an
+// action-aware refinement would buy nothing here.
+func entA8ProtectFKMemberColumns(m *entSfSchema) {
+ for _, tbl := range m.base.tables {
+ for _, fk := range tbl.fks {
+ for _, c := range fk.cols {
+ m.protected[tbl.name+"."+c] = true
+ }
+ }
+ }
+}
+
+// entA8Round drives one shared target through both fleets and cross-checks the
+// results: each side runs the full A6 round oracle (diff -> apply -> converge ->
+// idempotence -> strict LoadSDL reload -> ledger counts) under its own version, then
+// the two fresh dumps must cross-diff empty. Protocol violations return stage-tagged
+// errors (for ddmin); infrastructure failures fail t hard. On success both fleets'
+// prev/expect/budget are advanced and the 8.0-side plan is returned for the width
+// guard (the 5.7-side plan may carry display widths legitimately — they ARE its
+// stored form — so nothing judges it beyond the round oracle).
+func entA8Round(ctx context.Context, t *testing.T, f57, f80 *entA8Fleet, target string, muts []entSfMutation) (string, error) {
+ t.Helper()
+ want57, want80 := f57.expect, f80.expect
+ for _, m := range muts {
+ want57 = want57.plus(m.delta)
+ want80 = want80.plus(m.delta)
+ }
+ after57, _, err := entSfRoundToTarget(ctx, t, f57.srv, f57.dbName, f57.prev, target, f57.version, want57)
+ if err != nil {
+ return "", errors.Wrapf(err, "check=side %s", f57.srv.name)
+ }
+ after80, plan80, err := entSfRoundToTarget(ctx, t, f80.srv, f80.dbName, f80.prev, target, f80.version, want80)
+ if err != nil {
+ return "", errors.Wrapf(err, "check=side %s", f80.srv.name)
+ }
+ if err := entA8CrossCheck(after57, after80); err != nil {
+ return "", err
+ }
+ for _, m := range muts {
+ f57.budget += m.budget
+ f80.budget += m.budget
+ }
+ f57.prev, f57.expect = after57, want57
+ f80.prev, f80.expect = after80, want80
+ return plan80, nil
+}
+
+// entA8ScratchFromDump reloads a canonical dump into a fresh, eagerly-droppable,
+// collation-aligned scratch database. The dump text executes with FOREIGN_KEY_CHECKS
+// off for the session (canonical dumps order tables alphabetically, so inline foreign
+// keys may forward-reference; the driver runs the whole batch on one connection, so
+// the session setting holds).
+func entA8ScratchFromDump(ctx context.Context, t *testing.T, srv liveServer, dump string) (string, func()) {
+ t.Helper()
+ dbName, drop := entSfScratchDB(ctx, t, srv, "entsdl_a8t")
+ driver, err := createLiveMySQLDriver(ctx, srv, dbName)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+ _, err = driver.Execute(ctx, "ALTER DATABASE `"+dbName+"` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", db.ExecuteOptions{})
+ require.NoError(t, err, "[%s] align trial database collation", srv.name)
+ _, err = driver.Execute(ctx, "SET FOREIGN_KEY_CHECKS=0;\n"+entNormalizeDelimiters(dump), db.ExecuteOptions{})
+ require.NoError(t, err, "[%s] reload round-start dump into trial database", srv.name)
+ return dbName, drop
+}
+
+// entA8Trial reproduces one cross-version round from scratch for ddmin: both
+// round-start dumps reload into fresh aligned scratch databases, the mutation subset
+// re-authors the target from the authored side's dump, and the full round runs.
+func entA8Trial(ctx context.Context, t *testing.T, srv57, srv80 liveServer, prev57, prev80 string, authored80 bool, muts []entSfMutation) error {
+ t.Helper()
+ db57, drop57 := entA8ScratchFromDump(ctx, t, srv57, prev57)
+ defer drop57()
+ db80, drop80 := entA8ScratchFromDump(ctx, t, srv80, prev80)
+ defer drop80()
+
+ f57 := &entA8Fleet{srv: srv57, version: entUpg57Version, dbName: db57}
+ f80 := &entA8Fleet{srv: srv80, version: entUpg80Version, dbName: db80}
+ for _, f := range []*entA8Fleet{f57, f80} {
+ f.prev = dumpSDL(ctx, t, f.srv, f.dbName)
+ f.expect = entSfCountObjects(syncMetaForDB(ctx, t, f.srv, f.dbName))
+ }
+ authored := f57.prev
+ if authored80 {
+ authored = f80.prev
+ }
+ target := authored
+ for _, m := range muts {
+ target = m.apply(t, target)
+ }
+ _, err := entA8Round(ctx, t, f57, f80, target, muts)
+ return err
+}
+
+// entA8RunSeed runs one full cross-version stateful seed: 4 rounds of
+// mutate-on-the-authored-side -> apply-to-both-fleets -> cross-check -> accretion
+// guards on both sides.
+func entA8RunSeed(ctx context.Context, t *testing.T, srv57, srv80 liveServer, baseDDL string, seed int64, authored80 bool) {
+ t.Helper()
+ rng := rand.New(rand.NewSource(seed)) //nolint:gosec // deterministic fuzz, not crypto
+
+ f57 := &entA8Fleet{srv: srv57, version: entUpg57Version}
+ f80 := &entA8Fleet{srv: srv80, version: entUpg80Version}
+ f57.dbName = entUpgLoadAligned(ctx, t, srv57, "entsdl_a8m57", baseDDL)
+ f80.dbName = entUpgLoadAligned(ctx, t, srv80, "entsdl_a8m80", baseDDL)
+ for _, f := range []*entA8Fleet{f57, f80} {
+ f.prev = dumpSDL(ctx, t, f.srv, f.dbName)
+ f.dump0 = len(f.prev)
+ f.expect = entSfCountObjects(syncMetaForDB(ctx, t, f.srv, f.dbName))
+ }
+ dir, author := "fwd57", f57
+ if authored80 {
+ dir, author = "rev80", f80
+ }
+
+ // Round 0: freshly loaded from the same DDL under aligned defaults, the fleets must
+ // already be logically identical — a failure here is a base cross-version phantom,
+ // not mutation drift.
+ require.NoError(t, entA8CrossCheck(f57.prev, f80.prev),
+ "[A8 %s seed=%d round=0] fleets diverge before any mutation (base cross-check)", dir, seed)
+
+ ledger := &entSfLedger{viewModified: map[string]bool{}}
+ var history []string
+ for r := 1; r <= entA8Rounds; r++ {
+ m := entSfParse(t, author.prev)
+ entA8ProtectFKMemberColumns(m)
+ k := 2 + rng.Intn(3) // K ∈ [2,4]
+ muts := entSfMutationsFromMenu(t, m, rng, entSfMenu("5.7"), ledger, r, k)
+ for _, mut := range muts {
+ history = append(history, fmt.Sprintf("r%d: %s", r, mut.desc))
+ }
+ t.Logf("[A8 %s seed=%d round=%d] K=%d mutations:\n %s",
+ dir, seed, r, k, strings.Join(entSfDescs(muts), "\n "))
+
+ target := author.prev
+ for _, mut := range muts {
+ target = mut.apply(t, target)
+ }
+ prev57, prev80 := f57.prev, f80.prev
+ plan80, err := entA8Round(ctx, t, f57, f80, target, muts)
+ if err != nil {
+ minimized, minErr := entSfMinimizeWith(muts, err, func(trial []entSfMutation) error {
+ return entA8Trial(ctx, t, srv57, srv80, prev57, prev80, authored80, trial)
+ })
+ t.Fatalf("[A8 %s seed=%d round=%d] FAILED\nminimized mutations (%d of %d):\n %s\nerror:\n%v\nseed history:\n %s",
+ dir, seed, r, len(minimized), len(muts), strings.Join(entSfDescs(minimized), "\n "),
+ minErr, strings.Join(history, "\n "))
+ }
+
+ // A leaked 5.7 display width inside an 8.0-side ALTER is a phantom column MODIFY
+ // (the 5.7-side plan may carry widths legitimately — they ARE its stored form).
+ entUpgAssertNoWidthInAlters(t, fmt.Sprintf("A8 %s seed=%d round=%d plan80", dir, seed, r), plan80)
+
+ // Accretion guards on BOTH sides' dumps (each against its own round-0 baseline).
+ for _, s := range []struct {
+ f *entA8Fleet
+ prev string
+ }{{f57, prev57}, {f80, prev80}} {
+ require.Less(t, len(s.f.prev), s.f.dump0+s.f.budget+entSfRoundSlack*r,
+ "[A8 %s seed=%d round=%d side=%s] check=accretion: dump grew past the added-object budget: len(dump_%d)=%d, len(dump_0)=%d, budget=%d — text accretion suspected.\nseed history:\n %s",
+ dir, seed, r, s.f.srv.name, r, len(s.f.prev), s.f.dump0, s.f.budget+entSfRoundSlack*r, strings.Join(history, "\n "))
+ entSfAssertUntouchedStable(t, s.f.srv, seed, r, s.prev, s.f.prev, muts)
+ }
+
+ t.Logf("[A8 %s seed=%d round=%d] ok: 57 %d -> %d bytes, 80 %d -> %d bytes (budget 57=%d 80=%d), counts 57=%+v 80=%+v",
+ dir, seed, r, len(prev57), len(f57.prev), len(prev80), len(f80.prev),
+ f57.dump0+f57.budget+entSfRoundSlack*r, f80.dump0+f80.budget+entSfRoundSlack*r, f57.expect, f80.expect)
+ }
+}
+
+//nolint:tparallel
+func TestSDLEnterpriseCrossVersionStatefulFuzz(t *testing.T) {
+ skipUnlessLiveOracle(t)
+ ctx := context.Background()
+ srv57, srv80 := entUpgServers(t)
+ // The fleet schema must load on BOTH versions, so the base is the 5.7 shape of the
+ // A6 stateful-fuzz base (entSfAux80's SRID/INVISIBLE/CHECK tables are 8.0-only).
+ baseDDL := entSfBaseDDL(t, srv57)
+
+ t.Run("forward_57_authored", func(t *testing.T) {
+ for seed := int64(1); seed <= entA8ForwardSeeds; seed++ {
+ seed := seed
+ t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
+ entA8RunSeed(ctx, t, srv57, srv80, baseDDL, seed, false)
+ })
+ }
+ })
+ // Reverse-authored seeds use a disjoint seed range: the two dumps' candidate models
+ // are logically identical, so reusing 1..N would largely replay the forward streams.
+ t.Run("reverse_80_authored", func(t *testing.T) {
+ for seed := int64(101); seed < 101+entA8ReverseSeeds; seed++ {
+ seed := seed
+ t.Run(fmt.Sprintf("seed_%d", seed), func(t *testing.T) {
+ entA8RunSeed(ctx, t, srv57, srv80, baseDDL, seed, true)
+ })
+ }
+ })
+}
diff --git a/backend/plugin/schema/mysql/sdl_realworld_test.go b/backend/plugin/schema/mysql/sdl_realworld_test.go
deleted file mode 100644
index fe6f805d13..0000000000
--- a/backend/plugin/schema/mysql/sdl_realworld_test.go
+++ /dev/null
@@ -1,909 +0,0 @@
-package mysql
-
-// Real-world-schema smoke test for the MySQL declarative (SDL) migration path. Unlike the
-// synthetic stress suites (sdl_stress_live_test.go, sdl_deep_stress_test.go) this file drives
-// ACTUAL production schemas — Sakila, employees, employees(partitioned), Roundcube, and
-// MediaWiki — through the exact production entry points end to end against live MySQL 5.7 +
-// 8.0:
-//
-// - schema.SDLMigration(MYSQL, userSDL, syncedMetadata, engineVersion),
-// - mysqlDiffSDLMigration(source, target, engineVersion),
-// - schema.SDLDropAdvices(MYSQL, userSDL, syncedMetadata, engineVersion),
-// - the real db/mysql sync + schema.MetadataToSDL dumper.
-//
-// The schemas are embedded preprocessed fixtures (testdata/realworld/*.sql) so the test is
-// self-contained. Sakila is THE priority: it carries real view / function / procedure /
-// trigger bodies (incl. a SQL SECURITY INVOKER view with fully schema-qualified references),
-// which is where the at-scale view/routine round-trip is won or lost.
-//
-// Shared helpers (createLiveMySQLDriver, newLiveDatabase, liveServers, liveOraclePassword,
-// statementCount, syncMetadata, dumpSDL, applyDDL) come from the sibling _test.go files in
-// this package.
-
-import (
- "context"
- _ "embed"
- "fmt"
- "regexp"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-// ----------------------------------------------------------------------------
-// Embedded preprocessed real-world schema fixtures.
-//
-// Preprocessing applied when the corpus was lifted into testdata (so the committed test runs
-// without the external corpus):
-// - sakila: DROP/CREATE SCHEMA + USE + session SET @OLD_* lines stripped; a single
-// leading SET FOREIGN_KEY_CHECKS=0 prepended (the dump's forward FK refs
-// require it). DELIMITER routine/trigger blocks kept verbatim. The
-// actor_info view keeps its hardcoded `sakila.`-qualified body, so sakila
-// MUST be loaded into a database literally named `sakila`.
-// - employees: DB-context + data-load tail (flush logs / SELECT 'LOADING' / source ...)
-// stripped; CREATE TABLE/VIEW kept. Wrapped in a scratch DB.
-// - employees_part: same as employees, keeping the /*!50510 ALTER ... PARTITION BY RANGE
-// COLUMNS */ version-gated blocks for `titles` and `salaries`.
-// - roundcube: no CREATE DATABASE — wrapped in a scratch DB. Every table is
-// ROW_FORMAT=DYNAMIC (a known table-option round-trip stressor) with real
-// charset variety (utf8mb4_unicode_ci, binary). Trailing INSERT stripped.
-// - mediawiki: MediaWiki placeholders preprocessed: /*_*/ and /*i*/ deleted,
-// /*$wgDBTableOptions*/ -> "ENGINE=InnoDB DEFAULT CHARSET=binary",
-// /*$wgDBprefix*/ -> empty. 58 tables incl. a MyISAM FULLTEXT searchindex.
-
-//go:embed testdata/realworld/sakila.sql
-var sakilaSchema string
-
-//go:embed testdata/realworld/employees.sql
-var employeesSchema string
-
-//go:embed testdata/realworld/employees_part.sql
-var employeesPartSchema string
-
-//go:embed testdata/realworld/roundcube.sql
-var roundcubeSchema string
-
-//go:embed testdata/realworld/mediawiki.sql
-var mediawikiSchema string
-
-// omniViewParseBug is the precise classification for the (B) omni SDL-parser limitation that
-// blocks every multi-table view in the real-world corpus.
-//
-// REPRO: schema.MetadataToSDL dumps a multi-table view's FROM clause in MySQL's canonical
-// parenthesized left-deep join form, e.g. (from sakila.film_list):
-//
-// ... from ((((`category` left join `film_category` on(...)) left join `film` on(...))
-// join `film_actor` on(...)) join `actor` on(...)) group by ...
-//
-// Re-loading that dump through the SDL path (catalog.LoadSDLWithVersion / LoadSQL, called by
-// mysqlDiffSDLMigration and schema.SDLMigration) fails with
-//
-// expected SELECT, TABLE, VALUES, or '('
-//
-// LOCUS: omni mysql/parser/select.go:390 — the FROM-clause table_reference parser does not
-// accept a parenthesized join group `( t1 JOIN t2 ON ... )`; the leading '(' falls through to
-// the query-primary parser, which only expects a parenthesized sub-SELECT. omni's top-level
-// Parse() accepts the same construct, so the gap is specifically in the SDL catalog loader's
-// view-body SELECT sub-parser. This is DISTINCT from the known varchar(N) BINARY -> _bin
-// collation phantom-MODIFY bug. Until omni fixes the parenthesized-join table_reference and the
-// pin updates, any fixture whose synced+dumped form carries a multi-table view (sakila and the
-// employees family, via current_dept_emp) cannot round-trip — these legs are pending, not red.
-const omniViewParseBug = "omni SDL view-body parser rejects MySQL's canonical parenthesized JOIN in a view FROM clause (omni mysql/parser/select.go:390); blocks every multi-table view"
-
-// omniVarcharBinaryBug is the OTHER known, being-fixed-in-parallel (B) omni bug: the omni SDL
-// loader drops the `BINARY` modifier on a `varchar(N) BINARY` column (which MySQL stores as the
-// `_bin` collation), so the original-vs-synced diff emits a phantom
-// `ALTER TABLE ... MODIFY COLUMN ... varchar(N)`. roundcube's cache/users tables use that form.
-// Pending until the omni fix merges and the pin updates.
-const omniVarcharBinaryBug = "omni SDL loader drops varchar(N) BINARY -> _bin collation, producing a phantom MODIFY COLUMN (confirmed (B) bug, fixed in omni separately)"
-
-// realWorldSchema describes one embedded production schema fixture.
-type realWorldSchema struct {
- name string
- ddl string
- // fixedDBName, when non-empty, forces the scratch database to this literal name instead of
- // a unique-suffixed one. Required for sakila, whose actor_info view body hardcodes the
- // `sakila.` schema qualifier and therefore only resolves in a database named `sakila`.
- fixedDBName string
- // only57Skip marks a fixture that does not load on 5.7 (so the 5.7 leg is skipped with a
- // classification rather than failing).
- only57Skip string
- // viewParsePending, when non-empty, marks a fixture whose canonical dump carries a
- // multi-table view that the current omni SDL parser cannot re-read (see omniViewParseBug).
- // The SDL-path legs (idempotence, original-vs-synced, and every migration scenario on this
- // schema) are skipped with this classification rather than failing on an out-of-scope,
- // being-fixed-upstream engine bug. The live LOAD + sync + MetadataToSDL dump still run and
- // are asserted non-empty in Phase 0, so the dumper half of the round-trip stays covered.
- viewParsePending string
-}
-
-func realWorldSchemas() []realWorldSchema {
- return []realWorldSchema{
- {
- name: "sakila",
- ddl: sakilaSchema,
- fixedDBName: "sakila",
- // On 5.7 the SQL SECURITY INVOKER actor_info view, whose body is fully `sakila.`
- // qualified, fails to CREATE even into a database named sakila because 5.7 resolves
- // the invoker view's referenced tables more strictly at create time (errno 1146 on
- // sakila.actor under FOREIGN_KEY_CHECKS=0). 8.0 creates it fine. So the 5.7 leg is
- // skipped (harness/engine-version classification — see report). All other sakila
- // objects (16 tables, 6 plain views, 3 functions, 3 procedures, 3 triggers) load on
- // both versions.
- only57Skip: "actor_info SQL SECURITY INVOKER view (hardcoded sakila.* refs) fails to CREATE on 5.7",
- // Multi-table-join views (film_list, customer_list, ...) now round-trip: the omni SDL
- // parser's parenthesized-JOIN FROM fix is merged + pinned (#356).
- },
- {name: "employees", ddl: employeesSchema},
- {name: "employees_part", ddl: employeesPartSchema},
- {name: "roundcube", ddl: roundcubeSchema},
- {name: "mediawiki", ddl: mediawikiSchema},
- }
-}
-
-// newNamedLiveDatabase is newLiveDatabase with an explicit database name (for fixtures that
-// must use a fixed name, e.g. sakila). It drops any pre-existing database of that name first
-// and registers cleanup.
-func newNamedLiveDatabase(ctx context.Context, t *testing.T, srv liveServer, dbName string) string {
- t.Helper()
- admin, err := createLiveMySQLDriver(ctx, srv, "")
- require.NoError(t, err)
- _, err = admin.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`; CREATE DATABASE `%s`", dbName, dbName), db.ExecuteOptions{})
- require.NoError(t, err)
- admin.Close(ctx)
- t.Cleanup(func() {
- c, err := createLiveMySQLDriver(ctx, srv, "")
- if err != nil {
- return
- }
- defer c.Close(ctx)
- _, _ = c.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName), db.ExecuteOptions{})
- })
- return dbName
-}
-
-// loadRealWorld creates a scratch database (fixed-named or unique), applies the fixture DDL
-// through the real db/mysql driver, syncs it, and returns the synced model metadata plus the
-// database name. The fixture DDL may contain DELIMITER blocks and SET FOREIGN_KEY_CHECKS=0;
-// the driver's DealWithDelimiter + single-connection execution handle both.
-func loadRealWorld(ctx context.Context, t *testing.T, srv liveServer, rw realWorldSchema) (*model.DatabaseMetadata, string) {
- t.Helper()
- var dbName string
- if rw.fixedDBName != "" {
- dbName = newNamedLiveDatabase(ctx, t, srv, rw.fixedDBName)
- } else {
- dbName = newLiveDatabase(ctx, t, srv, "sdl_rw_"+rw.name)
- }
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, normalizeDelimiters(rw.ddl), db.ExecuteOptions{})
- require.NoError(t, err, "[%s/%s] apply fixture DDL", srv.name, rw.name)
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true), dbName
-}
-
-// normalizeDelimiters rewrites a mysqldump-style multi-DELIMITER fixture into the no-DELIMITER
-// form the production split path (mysqlparser.DealWithDelimiter -> omni Split) handles cleanly.
-//
-// The sakila fixture interleaves regular ";"-delimited DDL with several DELIMITER blocks in
-// different styles (";;", "//", "$$") for its triggers/routines. The production path's
-// DealWithDelimiter only reliably rewrites a single uniform block; on the interleaved mix omni's
-// splitter silently truncates the statement stream (it stopped after the first routine, so only
-// 10 of 16 tables and 0 of 7 views ever loaded). Normalizing here — drop every standalone
-// DELIMITER line and rewrite each body's custom terminator (e.g. "END //") back to a plain
-// "END;" — yields the exact form the passing stress suites use (BEGIN ... END; with internal
-// ";", no DELIMITER), which omni's compound-statement-aware splitter segments correctly. The
-// fixtures without DELIMITER blocks pass through unchanged.
-func normalizeDelimiters(ddl string) string {
- if !strings.Contains(ddl, "DELIMITER") {
- return ddl
- }
- lines := strings.Split(ddl, "\n")
- out := make([]string, 0, len(lines))
- delim := ";"
- for _, ln := range lines {
- trimmed := strings.TrimSpace(ln)
- if strings.HasPrefix(strings.ToUpper(trimmed), "DELIMITER ") {
- delim = strings.TrimSpace(trimmed[len("DELIMITER "):])
- if delim == "" {
- delim = ";"
- }
- continue
- }
- if delim != ";" && strings.HasSuffix(trimmed, delim) {
- if idx := strings.LastIndex(ln, delim); idx >= 0 {
- ln = ln[:idx] + ";"
- }
- }
- out = append(out, ln)
- }
- return strings.Join(out, "\n")
-}
-
-// objectCounts summarizes a synced schema for the Phase 0 report. It walks the raw proto so
-// it is agnostic to the (single, unnamed) schema MySQL uses.
-func objectCounts(meta *model.DatabaseMetadata) (tables, views, funcs, procs, triggers int) {
- proto := meta.GetProto()
- if proto == nil {
- return 0, 0, 0, 0, 0
- }
- for _, sm := range proto.GetSchemas() {
- tables += len(sm.GetTables())
- views += len(sm.GetViews())
- funcs += len(sm.GetFunctions())
- procs += len(sm.GetProcedures())
- for _, tbl := range sm.GetTables() {
- triggers += len(tbl.GetTriggers())
- }
- }
- return tables, views, funcs, procs, triggers
-}
-
-// ============================================================================
-// Phase 0 + Phase 1: load each real schema, then prove SDL idempotence.
-// ============================================================================
-
-// TestSDLRealWorldIdempotence is the baseline that matters most: every loaded production
-// schema must round-trip through the SDL path with NO phantom diff. For each schema × version:
-//
-// (0) load the fixture live, sync, and log object counts (Phase 0 usability),
-// (1a) source-vs-source determinism: source = MetadataToSDL(synced);
-// mysqlDiffSDLMigration(source, source, version) MUST be empty,
-// (1b) production path: schema.SDLMigration(MYSQL, source, synced, version) MUST be empty
-// (this is the exact call the release executor makes, with the canonical dump as the
-// user target — proving the dumper output re-imports to the identical metadata).
-//
-// Any non-empty no-op is a finding (logged with the spurious DDL for classification).
-//
-//nolint:tparallel
-func TestSDLRealWorldIdempotence(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, rw := range realWorldSchemas() {
- rw := rw
- t.Run(rw.name, func(t *testing.T) {
- if srv.version == "5.7" && rw.only57Skip != "" {
- t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, rw.name, rw.only57Skip)
- }
-
- meta, dbName := loadRealWorld(ctx, t, srv, rw)
- tbl, vw, fn, pr, tg := objectCounts(meta)
- t.Logf("[PHASE0 %s/%s] loaded: tables=%d views=%d functions=%d procedures=%d triggers=%d (db=%s)",
- srv.name, rw.name, tbl, vw, fn, pr, tg, dbName)
-
- source := dumpSDL(ctx, t, srv, dbName)
- require.NotEmpty(t, source, "[%s/%s] MetadataToSDL produced empty SDL", srv.name, rw.name)
-
- if rw.viewParsePending != "" {
- // Load + sync + dump (Phase 0) covered above; the SDL re-import (Phase 1)
- // is blocked by an out-of-scope omni parser bug. Skip with classification.
- t.Skipf("[%s/%s] SDL re-import pending: %s", srv.name, rw.name, rw.viewParsePending)
- }
-
- // (1a) determinism.
- selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
- require.NoError(t, err)
- if selfDiff != "" {
- t.Logf("[PHASE1a %s/%s] NON-EMPTY source-vs-source (%d stmts):\n%s",
- srv.name, rw.name, statementCount(selfDiff), selfDiff)
- }
- require.Empty(t, selfDiff, "[%s/%s] source-vs-source must be empty, got:\n%s", srv.name, rw.name, selfDiff)
-
- // (1b) production path, canonical dump as user target.
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, source, meta, srv.version)
- require.NoError(t, err)
- if noop != "" {
- t.Logf("[PHASE1b %s/%s] NON-EMPTY production no-op (%d stmts):\n%s",
- srv.name, rw.name, statementCount(noop), noop)
- t.Logf("[PHASE1b %s/%s] dumped source:\n%s", srv.name, rw.name, source)
- }
- require.Empty(t, noop, "[%s/%s] production-path no-op must be empty, got:\n%s", srv.name, rw.name, noop)
- })
- }
- })
- }
-}
-
-// TestSDLRealWorldOriginalVsSynced is the adversarial original-vs-synced probe: it diffs the
-// ORIGINAL hand-authored fixture SDL (as a user would commit it) against the engine-synced
-// metadata. This is stricter than the canonical-dump round-trip because the original carries
-// non-canonical forms (utf8 vs utf8mb3, int display widths, charset defaults, hardcoded view
-// qualifiers, ROW_FORMAT options, ...) that the engine rewrites on store. A non-empty result
-// here pins a real normalization gap the production path would surface to a user editing the
-// committed schema.
-//
-// NOTE: this is run only for fixtures whose original text re-imports cleanly under the omni
-// SDL loader (which disables FK checks but parses the raw DDL). Fixtures that carry DELIMITER
-// blocks, version-gated /*! */ partition comments, or MediaWiki-style standalone CREATE INDEX
-// statements are NOT valid single-document SDL inputs to the omni loader, so they are diffed
-// only through the canonical-dump round-trip in TestSDLRealWorldIdempotence. The viable
-// original-vs-synced fixtures are roundcube (pure CREATE TABLE) and employees (CREATE TABLE +
-// CREATE OR REPLACE VIEW). They are the at-scale normalization stressors.
-//
-//nolint:tparallel
-func TestSDLRealWorldOriginalVsSynced(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- // Only fixtures that are valid as a single SDL document (no DELIMITER / no /*! */ gated
- // ALTERs / no standalone CREATE INDEX) are eligible. employees uses CREATE OR REPLACE VIEW
- // which the omni loader accepts; roundcube is pure CREATE TABLE.
- //
- // Both eligible fixtures currently surface a distinct, being-fixed-upstream (B) omni bug, so
- // the assertion is pending with a precise classification (the load + sync still run):
- // - roundcube: varchar(N) BINARY -> _bin phantom MODIFY (omniVarcharBinaryBug),
- // - employees: current_dept_emp's parenthesized-join dump fails to re-parse (omniViewParseBug).
- eligible := map[string]string{"roundcube": omniVarcharBinaryBug, "employees": omniViewParseBug}
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, rw := range realWorldSchemas() {
- rw := rw
- pending, ok := eligible[rw.name]
- if !ok {
- continue
- }
- t.Run(rw.name, func(t *testing.T) {
- meta, _ := loadRealWorld(ctx, t, srv, rw)
- if pending != "" {
- t.Skipf("[%s/%s] original-vs-synced pending: %s", srv.name, rw.name, pending)
- }
-
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, rw.ddl, meta, srv.version)
- require.NoError(t, err)
- if noop != "" {
- t.Logf("[ORIGINAL-VS-SYNCED %s/%s] NON-EMPTY (%d stmts):\n%s",
- srv.name, rw.name, statementCount(noop), noop)
- }
- require.Empty(t, noop, "[%s/%s] original-vs-synced no-op must be empty, got:\n%s", srv.name, rw.name, noop)
- })
- }
- })
- }
-}
-
-// ============================================================================
-// Phase 2: realistic daily-dev migrations on the live DB, mixing object types.
-//
-// Each scenario: source = MetadataToSDL(sync(current live DB)); build a modified target;
-// plan = mysqlDiffSDLMigration(source, target, version); assert the plan is minimal +
-// correctly ordered (per scenario); apply the plan to the live DB; re-sync; assert the next
-// diff is empty (convergence).
-//
-// Sakila is the primary vehicle (real views/functions/procedures/triggers). employees_part
-// covers a partition change; roundcube/mediawiki cover table/column/index/FK breadth at scale.
-// ============================================================================
-
-// rwScenario is one daily-dev migration applied on top of a freshly-loaded real schema.
-type rwScenario struct {
- name string
- // schema selects which fixture to load as the live baseline.
- schema string
- // mutate transforms the canonical source SDL into the developer's modified target. It
- // receives the dumped source so it can do targeted string surgery on the real stored form.
- mutate func(t *testing.T, srv liveServer, source string) string
- // wantContains are uppercased substrings the generated plan MUST contain (minimality /
- // correctness anchors). Empty means "just assert non-empty + converges".
- wantContains []string
- // skip57 marks scenarios that only run on 8.0 (e.g. sakila-based ones).
- skip57 string
-}
-
-// mustReplace asserts the replacement actually changed the text (guards against fixture drift
-// silently turning a scenario into a no-op).
-func mustReplace(t *testing.T, s, old, replacement string) string {
- t.Helper()
- require.Contains(t, s, old, "scenario setup: source must contain %q to mutate", old)
- return strings.Replace(s, old, replacement, 1)
-}
-
-func rwScenarios() []rwScenario {
- return []rwScenario{
- // ---- Sakila: VIEW changes ----
- {
- name: "sakila_view_alter_select",
- schema: "sakila",
- skip57: "sakila view scenarios run on 8.0 (sakila does not fully load on 5.7)",
- // film_list is a real view joining film/category/film_actor/actor. Append a genuinely
- // new column (replacement_cost) to its SELECT — a developer broadening a reporting view.
- // (price/rental_rate is already projected, so we add a column not already present.)
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addColumnToView(t, source, "film_list", "`film`.`replacement_cost` AS `replacement_cost`")
- },
- wantContains: []string{"FILM_LIST"},
- },
- {
- name: "sakila_view_add_new_and_view_on_view",
- schema: "sakila",
- skip57: "sakila view scenarios run on 8.0",
- // Add a brand-new view AND a view that depends on it (view-on-view), appended to the
- // source. The plan must create both, ordered base-before-dependent.
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + `
-CREATE VIEW v_actor_min AS SELECT actor_id, first_name FROM actor;
-CREATE VIEW v_actor_min2 AS SELECT actor_id FROM v_actor_min;
-`
- },
- wantContains: []string{"V_ACTOR_MIN", "V_ACTOR_MIN2"},
- },
- {
- name: "sakila_view_drop",
- schema: "sakila",
- skip57: "sakila view scenarios run on 8.0",
- // Drop the sales_by_store view (a real aggregate view) by removing it from the source.
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropObjectBlock(t, source, "VIEW", "sales_by_store")
- },
- wantContains: []string{"DROP VIEW", "SALES_BY_STORE"},
- },
- // ---- Sakila: FUNCTION changes ----
- {
- name: "sakila_function_add",
- schema: "sakila",
- skip57: "sakila routine scenarios run on 8.0",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + `
-CREATE FUNCTION f_film_count() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM film);
-`
- },
- wantContains: []string{"F_FILM_COUNT"},
- },
- {
- name: "sakila_function_drop",
- schema: "sakila",
- skip57: "sakila routine scenarios run on 8.0",
- // Drop inventory_held_by_customer (a real function with a body).
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropObjectBlock(t, source, "FUNCTION", "inventory_held_by_customer")
- },
- wantContains: []string{"DROP FUNCTION", "INVENTORY_HELD_BY_CUSTOMER"},
- },
- // ---- Sakila: PROCEDURE changes ----
- {
- name: "sakila_procedure_add",
- schema: "sakila",
- skip57: "sakila routine scenarios run on 8.0",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + `
-CREATE PROCEDURE p_touch_film(IN fid INT) BEGIN UPDATE film SET last_update = NOW() WHERE film_id = fid; END;
-`
- },
- wantContains: []string{"P_TOUCH_FILM"},
- },
- {
- name: "sakila_procedure_drop",
- schema: "sakila",
- skip57: "sakila routine scenarios run on 8.0",
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropObjectBlock(t, source, "PROCEDURE", "film_not_in_stock")
- },
- wantContains: []string{"DROP PROCEDURE", "FILM_NOT_IN_STOCK"},
- },
- // ---- Sakila: TRIGGER changes ----
- {
- name: "sakila_trigger_add",
- schema: "sakila",
- skip57: "sakila trigger scenarios run on 8.0",
- mutate: func(_ *testing.T, _ liveServer, source string) string {
- return source + `
-CREATE TRIGGER trg_payment_bi BEFORE INSERT ON payment FOR EACH ROW SET NEW.last_update = NOW();
-`
- },
- wantContains: []string{"TRG_PAYMENT_BI"},
- },
- {
- name: "sakila_trigger_drop",
- schema: "sakila",
- skip57: "sakila trigger scenarios run on 8.0",
- // Drop the ins_film trigger (real multi-statement trigger on film).
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropTrigger(t, source, "ins_film")
- },
- wantContains: []string{"DROP TRIGGER", "INS_FILM"},
- },
- // ---- Sakila: TABLE / COLUMN / INDEX / FK changes ----
- {
- name: "sakila_table_add_column_index",
- schema: "sakila",
- skip57: "run on 8.0 alongside the other sakila scenarios",
- // Add a NOT NULL column with a default + a secondary index to the real `category`
- // table (widening daily dev: a new flag + an index on it).
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := addColumnToTable(t, source, "category", "`is_featured` tinyint(1) NOT NULL DEFAULT '0'")
- return addIndexToTable(t, s, "category", "KEY `idx_cat_featured` (`is_featured`)")
- },
- wantContains: []string{"IS_FEATURED", "IDX_CAT_FEATURED"},
- },
- {
- name: "sakila_widen_varchar",
- schema: "sakila",
- skip57: "run on 8.0 alongside the other sakila scenarios",
- // Widen actor.first_name VARCHAR(45) -> VARCHAR(80): a classic column-type change.
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return widenActorFirstName(t, source)
- },
- wantContains: []string{"FIRST_NAME"},
- },
- // ---- Sakila: COMBINED feature migration (table + dependent view + function + trigger) ----
- {
- name: "sakila_combined_feature_release",
- schema: "sakila",
- skip57: "run on 8.0 alongside the other sakila scenarios",
- // One release that, together: adds a column to `staff`, adds a function, adds a
- // trigger on `staff`, and adds a new view that reads the new column. Exercises correct
- // cross-object ordering in a single diff.
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := addColumnToTable(t, source, "staff", "`shift_note` varchar(120) DEFAULT NULL")
- s += `
-CREATE FUNCTION f_staff_count() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM staff);
-CREATE VIEW v_staff_notes AS SELECT staff_id, shift_note FROM staff;
-CREATE TRIGGER trg_staff_bu BEFORE UPDATE ON staff FOR EACH ROW SET NEW.last_update = NOW();
-`
- return s
- },
- wantContains: []string{"SHIFT_NOTE", "F_STAFF_COUNT", "V_STAFF_NOTES", "TRG_STAFF_BU"},
- },
- // ---- employees_part: PARTITION change ----
- {
- name: "employees_part_partition_change",
- schema: "employees_part",
- // salaries is RANGE COLUMNS(from_date) partitioned. Coalesce its tail by replacing the
- // whole partition definition with a coarser 3-partition scheme. The plan must emit a
- // partition reorganization, apply, and converge.
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return repartitionSalaries(t, source)
- },
- wantContains: []string{"PARTITION"},
- },
- // ---- roundcube: table/column/index/FK at scale ----
- {
- name: "roundcube_add_column_index",
- schema: "roundcube",
- // Add a column + index to the real `users` table.
- mutate: func(t *testing.T, _ liveServer, source string) string {
- s := addColumnToTable(t, source, "users", "`timezone` varchar(64) DEFAULT NULL")
- return addIndexToTable(t, s, "users", "KEY `idx_users_tz` (`timezone`)")
- },
- wantContains: []string{"TIMEZONE", "IDX_USERS_TZ"},
- },
- {
- name: "roundcube_drop_index",
- schema: "roundcube",
- // Drop a genuine non-FK secondary index so the plan emits a clean DROP INDEX. `session`
- // has no foreign keys and exactly one secondary index (expires_at_index), so dropping it
- // can never hit errno 1553. (contacts' only secondary index, user_contacts_index, backs
- // its user_id FK and cannot be dropped on its own — that would be a different, FK-ordering
- // scenario.)
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return dropFirstSecondaryIndex(t, source, "session")
- },
- wantContains: []string{"DROP", "EXPIRES_AT_INDEX"},
- },
- // ---- mediawiki: table/column/index at scale (59-table schema) ----
- {
- name: "mediawiki_add_column",
- schema: "mediawiki",
- // Add a column to the real `page` table in a 58-table schema. The diff must be minimal
- // (touch only `page`), not churn the other 57 tables.
- mutate: func(t *testing.T, _ liveServer, source string) string {
- return addColumnToTable(t, source, "page", "`page_extra_flag` tinyint(1) NOT NULL DEFAULT '0'")
- },
- wantContains: []string{"PAGE_EXTRA_FLAG"},
- },
- }
-}
-
-// TestSDLRealWorldMigrations is Phase 2: realistic daily-dev migrations, mixing object types,
-// verified end to end (emit minimal+ordered DDL -> apply to the live DB -> converge).
-//
-//nolint:tparallel
-func TestSDLRealWorldMigrations(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- byName := map[string]realWorldSchema{}
- for _, rw := range realWorldSchemas() {
- byName[rw.name] = rw
- }
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, sc := range rwScenarios() {
- sc := sc
- t.Run(sc.name, func(t *testing.T) {
- if srv.version == "5.7" && sc.skip57 != "" {
- t.Skipf("[%s/%s] skipped on 5.7: %s", srv.name, sc.name, sc.skip57)
- }
- rw, ok := byName[sc.schema]
- require.True(t, ok, "unknown schema %q", sc.schema)
- if srv.version == "5.7" && rw.only57Skip != "" {
- t.Skipf("[%s/%s] schema %s not loadable on 5.7: %s", srv.name, sc.name, rw.name, rw.only57Skip)
- }
- if rw.viewParsePending != "" {
- // The mysqlDiffSDLMigration plan loads the dumped source, which carries a
- // multi-table view the omni SDL parser cannot re-read. Every scenario on this
- // schema is blocked at the source-load step (not by the scenario's own change),
- // so skip with classification rather than fail. NOTE: this gates the entire
- // sakila view/function/procedure/trigger migration path — see report headline.
- t.Skipf("[%s/%s] migration plan pending: %s", srv.name, sc.name, rw.viewParsePending)
- }
-
- _, dbName := loadRealWorld(ctx, t, srv, rw)
- source := dumpSDL(ctx, t, srv, dbName)
-
- target := sc.mutate(t, srv, source)
- require.NotEqual(t, source, target, "[%s/%s] scenario produced an identical target (no change)", srv.name, sc.name)
-
- plan, err := mysqlDiffSDLMigration(source, target, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, plan, "[%s/%s] scenario must produce DDL", srv.name, sc.name)
- t.Logf("[PHASE2 %s/%s] plan (%d stmts):\n%s", srv.name, sc.name, statementCount(plan), plan)
-
- upper := strings.ToUpper(plan)
- for _, want := range sc.wantContains {
- require.Contains(t, upper, want, "[%s/%s] plan missing %q:\n%s", srv.name, sc.name, want, plan)
- }
-
- // Apply the generated DDL to the live DB and confirm convergence.
- applyErr := applyDDL(ctx, t, srv, dbName, plan)
- require.NoError(t, applyErr, "[%s/%s] plan failed to apply:\n%s", srv.name, sc.name, plan)
-
- newSource := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(newSource, target, srv.version)
- require.NoError(t, err)
- if converge != "" {
- t.Logf("[PHASE2 %s/%s] NON-EMPTY residual after apply (%d stmts):\n%s",
- srv.name, sc.name, statementCount(converge), converge)
- }
- require.Empty(t, converge, "[%s/%s] did not converge, residual:\n%s", srv.name, sc.name, converge)
- })
- }
- })
- }
-}
-
-// ============================================================================
-// String-surgery helpers for Phase 2 target construction. They operate on the canonical
-// MetadataToSDL dump (backtick-quoted, lowercase keywords for column defs, uppercase for
-// table-level CREATE) and assert their anchor exists so a fixture drift fails loudly.
-// ============================================================================
-
-// findObjectSegment locates the whole CREATE statement for an object in the dumped source and
-// returns its [start,end) byte range. It splits the source with the SAME splitter the SDL
-// loader uses (mysqlparser.SplitSQL -> omni Split), so a multi-statement routine body (DECLARE
-// ...; ...; END;) is treated as one segment — naive ";"-scanning truncated such bodies at their
-// first internal semicolon. headerMatch must hold for the (whitespace-trimmed) segment text.
-func findObjectSegment(t *testing.T, source string, headerMatch func(stmt string) bool) (int, int) {
- t.Helper()
- stmts, err := mysqlparser.SplitSQL(source)
- require.NoError(t, err, "split source for object lookup")
- for _, s := range stmts {
- if headerMatch(strings.TrimSpace(s.Text)) {
- return int(s.Range.Start), int(s.Range.End)
- }
- }
- return -1, -1
-}
-
-// dumpedViewHeader reports whether stmt is the CREATE statement for the named view in the
-// canonical dump. The dumper emits "CREATE OR REPLACE ALGORITHM=UNDEFINED SQL SECURITY DEFINER
-// VIEW `name` AS ..." (or, for an INVOKER view, "... SQL SECURITY INVOKER VIEW `name`"), so the
-// modifiers between VIEW and the name vary — match on the "VIEW `name`" token regardless of the
-// ALGORITHM/SQL SECURITY clause.
-func dumpedViewHeader(stmt, view string) bool {
- u := strings.ToUpper(stmt)
- return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, " VIEW `"+strings.ToUpper(view)+"` ")
-}
-
-// removeSegment excises [start,end) from source and trims now-orphaned blank lines.
-func removeSegment(source string, start, end int) string {
- // Swallow a trailing newline so we don't leave a blank gap where the statement was.
- for end < len(source) && (source[end] == '\n' || source[end] == '\r') {
- end++
- }
- return source[:start] + source[end:]
-}
-
-// addColumnToView appends a column expression to a view's SELECT list by inserting it before the
-// FROM of that view's definition. It operates on the whole dumped view statement (located via
-// the splitter) so it is robust to the real "CREATE OR REPLACE ALGORITHM=... VIEW" header form.
-// The FROM anchor tolerates any surrounding whitespace: the pretty-printed dump puts the
-// top-level from on its own line (…AS `x`\nfrom `t`…).
-func addColumnToView(t *testing.T, source, view, colExpr string) string {
- t.Helper()
- start, end := findObjectSegment(t, source, func(stmt string) bool { return dumpedViewHeader(stmt, view) })
- require.GreaterOrEqual(t, start, 0, "view %q not found in source", view)
- stmt := source[start:end]
- loc := reViewFromAnchor.FindStringIndex(stmt)
- require.NotNil(t, loc, "view %q body has no FROM to anchor on:\n%s", view, stmt)
- fromIdx := loc[0]
- newStmt := stmt[:fromIdx] + "," + colExpr + stmt[fromIdx:]
- return source[:start] + newStmt + source[end:]
-}
-
-// reViewFromAnchor matches the first whitespace-delimited FROM keyword in a dumped view
-// statement (case-insensitive, any whitespace kind on both sides).
-var reViewFromAnchor = regexp.MustCompile(`(?i)[ \t\r\n]from[ \t\r\n]`)
-
-// dropObjectBlock removes the whole CREATE <kind> ... block (VIEW/FUNCTION/PROCEDURE) for the
-// named object so the diff emits a DROP. It locates the statement via the SDL splitter, so a
-// routine with a multi-statement body is removed in full.
-func dropObjectBlock(t *testing.T, source, kind, name string) string {
- t.Helper()
- var match func(stmt string) bool
- switch kind {
- case "VIEW":
- match = func(stmt string) bool { return dumpedViewHeader(stmt, name) }
- case "FUNCTION":
- match = func(stmt string) bool {
- u := strings.ToUpper(stmt)
- return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, "FUNCTION `"+strings.ToUpper(name)+"`")
- }
- case "PROCEDURE":
- match = func(stmt string) bool {
- u := strings.ToUpper(stmt)
- return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, "PROCEDURE `"+strings.ToUpper(name)+"`")
- }
- default:
- t.Fatalf("unsupported kind %q", kind)
- }
- start, end := findObjectSegment(t, source, match)
- require.GreaterOrEqual(t, start, 0, "%s %q not found in source", kind, name)
- return removeSegment(source, start, end)
-}
-
-// dropTrigger removes the whole CREATE TRIGGER `name` ... block (located via the SDL splitter,
-// so a BEGIN ... END; body is removed in full).
-func dropTrigger(t *testing.T, source, name string) string {
- t.Helper()
- start, end := findObjectSegment(t, source, func(stmt string) bool {
- u := strings.ToUpper(stmt)
- return strings.HasPrefix(u, "CREATE ") && strings.Contains(u, "TRIGGER `"+strings.ToUpper(name)+"`")
- })
- require.GreaterOrEqual(t, start, 0, "trigger %q not found in source", name)
- return removeSegment(source, start, end)
-}
-
-// addColumnToTable inserts a column definition into a table's CREATE block, right after the
-// table's PRIMARY KEY line (or before the closing paren if no PK line is found). It anchors on
-// the canonical "CREATE TABLE `name` (" header.
-func addColumnToTable(t *testing.T, source, table, colDef string) string {
- t.Helper()
- header := "CREATE TABLE `" + table + "` ("
- start := strings.Index(source, header)
- require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
- // Find the closing ")" of this CREATE TABLE at the start of a line (the dumper emits the
- // closing paren + options on its own line beginning with ')').
- bodyStart := start + len(header)
- closeIdx := findTableClose(source, bodyStart)
- require.GreaterOrEqual(t, closeIdx, 0, "table %q close paren not found", table)
- // Insert the column as a new line just before the close. Strip any trailing comma handling:
- // the dumper's last body line has no trailing comma, so we add ",\n <colDef>" after the
- // last body line's content. Simplest robust form: insert " <colDef>,\n" right after the
- // body start (first column position) — MySQL accepts column order changes via SDL.
- insertion := " " + colDef + ",\n"
- return source[:bodyStart] + "\n" + insertion + source[bodyStart:closeIdx] + source[closeIdx:]
-}
-
-// addIndexToTable inserts an index definition into a table's CREATE block before the closing
-// paren.
-func addIndexToTable(t *testing.T, source, table, idxDef string) string {
- t.Helper()
- header := "CREATE TABLE `" + table + "` ("
- start := strings.Index(source, header)
- require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
- bodyStart := start + len(header)
- closeIdx := findTableClose(source, bodyStart)
- require.GreaterOrEqual(t, closeIdx, 0, "table %q close paren not found", table)
- // The line immediately before closeIdx ends a body element without a trailing comma. Insert
- // ",\n <idxDef>" right before the close paren, after trimming the trailing newline.
- body := source[bodyStart:closeIdx]
- trimmed := strings.TrimRight(body, "\n")
- newBody := trimmed + ",\n " + idxDef + "\n"
- return source[:bodyStart] + newBody + source[closeIdx:]
-}
-
-// findTableClose returns the index of the line-leading ')' that closes the CREATE TABLE body
-// beginning at bodyStart. The dumper emits ")\n" or ") ENGINE=..." with the ')' at column 0.
-func findTableClose(source string, bodyStart int) int {
- nl := strings.Index(source[bodyStart:], "\n)")
- if nl < 0 {
- return -1
- }
- return bodyStart + nl + 1
-}
-
-// dropFirstSecondaryIndex removes the first " KEY `...` (...)," line from the named table so
-// the diff emits a DROP INDEX. It skips PRIMARY/UNIQUE to keep the change a plain index drop.
-func dropFirstSecondaryIndex(t *testing.T, source, table string) string {
- t.Helper()
- header := "CREATE TABLE `" + table + "` ("
- start := strings.Index(source, header)
- require.GreaterOrEqual(t, start, 0, "table %q not found in source", table)
- bodyStart := start + len(header)
- closeIdx := findTableClose(source, bodyStart)
- require.GreaterOrEqual(t, closeIdx, 0, "table %q close paren not found", table)
- body := source[bodyStart:closeIdx]
- lines := strings.Split(body, "\n")
- out := make([]string, 0, len(lines))
- dropped := false
- for _, ln := range lines {
- trimmed := strings.TrimSpace(ln)
- if !dropped && strings.HasPrefix(trimmed, "KEY `") {
- dropped = true
- // Remove a dangling comma on the previous line if this was the last body element.
- continue
- }
- out = append(out, ln)
- }
- require.True(t, dropped, "table %q had no secondary KEY to drop:\n%s", table, body)
- newBody := strings.Join(out, "\n")
- // Fix a possible trailing comma before the close paren.
- newBody = strings.TrimRight(newBody, "\n")
- newBody = strings.TrimRight(newBody, ",")
- newBody += "\n"
- return source[:bodyStart] + newBody + source[closeIdx:]
-}
-
-// widenActorFirstName changes actor.first_name varchar(45) -> varchar(80) in the source.
-func widenActorFirstName(t *testing.T, source string) string {
- t.Helper()
- // The dumper renders the column as: `first_name` varchar(45) ...
- old := "`first_name` varchar(45)"
- require.Contains(t, source, old, "actor.first_name varchar(45) not found in source")
- return strings.Replace(source, old, "`first_name` varchar(80)", 1)
-}
-
-// repartitionSalaries replaces the salaries table partition clause with a coarser 3-partition
-// RANGE COLUMNS scheme. It anchors on the canonical PARTITION BY block the dumper emits.
-func repartitionSalaries(t *testing.T, source string) string {
- t.Helper()
- header := "CREATE TABLE `salaries`"
- start := strings.Index(source, header)
- require.GreaterOrEqual(t, start, 0, "salaries table not found in source")
- // Find the PARTITION BY clause within the salaries statement.
- rest := source[start:]
- semi := strings.Index(rest, ";")
- require.GreaterOrEqual(t, semi, 0, "salaries statement has no terminating semicolon")
- stmt := rest[:semi]
- pIdx := strings.Index(strings.ToUpper(stmt), "PARTITION BY")
- require.GreaterOrEqual(t, pIdx, 0, "salaries has no PARTITION BY clause to change:\n%s", stmt)
- // The dumper wraps partitioning in a /*!NNNNN PARTITION BY ... */ executable comment. Replace
- // the whole partition portion INCLUDING the wrapper opener, else we leave an unterminated
- // /*! comment (and produce invalid SQL).
- replStart := pIdx
- if c := strings.LastIndex(stmt[:pIdx], "/*!"); c >= 0 {
- replStart = c
- }
- // Build a coarser partition scheme covering the same range column.
- newPart := "PARTITION BY RANGE COLUMNS(`from_date`)\n" +
- "(PARTITION p_old VALUES LESS THAN ('1990-01-01'),\n" +
- " PARTITION p_mid VALUES LESS THAN ('2000-01-01'),\n" +
- " PARTITION p_max VALUES LESS THAN (MAXVALUE))"
- newStmt := stmt[:replStart] + newPart
- return source[:start] + newStmt + source[start+semi:]
-}
-
-// uniqueDBName mirrors newLiveDatabase's naming for ad hoc probes (kept for symmetry/debug).
-//
-//nolint:unused
-func uniqueDBName(prefix string) string {
- return fmt.Sprintf("%s_%s", prefix, strings.ReplaceAll(uuid.New().String(), "-", "_"))
-}
diff --git a/backend/plugin/schema/mysql/sdl_session_context_live_test.go b/backend/plugin/schema/mysql/sdl_session_context_live_test.go
deleted file mode 100644
index 01f3df5adb..0000000000
--- a/backend/plugin/schema/mysql/sdl_session_context_live_test.go
+++ /dev/null
@@ -1,368 +0,0 @@
-package mysql
-
-import (
- "context"
- "database/sql"
- "fmt"
- "strings"
- "testing"
-
- mysqldriver "github.com/go-sql-driver/mysql"
- "github.com/google/uuid"
- "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"
-)
-
-// End-to-end proof for BYT-9832 STAGE 2 (bytebase wiring) against the LIVE 5.7 (:13307)
-// and 8.0 (:13306) oracles. It drives the REAL production entry point,
-// schema.SDLMigration (the function database_migrate_executor.go's diff() calls), so the
-// whole path exercised is: seed object under a distinctive session context → sync to
-// metadata (which captures sql_mode/charset/collation, and event time_zone) →
-// schema.SDLMigration(userSDL, syncedMetadata, version) → the generated migration is
-// APPLIED on a deploy session whose DEFAULT sql_mode differs → read back
-// information_schema to prove the ORIGINAL context survived the recreate/ALTER.
-//
-// Without the STAGE 2 wiring (buildSDLSessionContextMap + ApplySessionContext on the
-// source catalog) the object comes back stamped with the deploy session's mode — which is
-// exactly the regression these assertions catch.
-
-// origRoutineMode is a non-default sql_mode whose semantics visibly differ from the deploy
-// default: PIPES_AS_CONCAT makes `||` string concat (not OR), NO_BACKSLASH_ESCAPES changes
-// escaping. A bare recreate under the deploy default would silently lose both.
-const origRoutineMode = "PIPES_AS_CONCAT,NO_BACKSLASH_ESCAPES"
-
-// deploySessionMode is the (different) sql_mode the migration is applied under, standing in
-// for a deploy connection whose session default is not the object's authoring mode.
-const deploySessionMode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
-
-// openLiveDB opens a raw *sql.DB against a live oracle with multi-statement support so a
-// framed migration (SET …; CREATE …; SET …) runs as one script on one session, and returns
-// it with a cleanup. It connects plaintext (no tls param), the equivalent of the mysql
-// CLI's --ssl-mode=DISABLED that 5.7 needs.
-func openLiveDB(t *testing.T, srv liveServer, database string) *sql.DB {
- t.Helper()
- cfg := mysqldriver.NewConfig()
- cfg.User = "root"
- cfg.Passwd = liveOraclePassword
- cfg.Net = "tcp"
- cfg.Addr = fmt.Sprintf("%s:%s", srv.host, srv.port)
- cfg.DBName = database
- cfg.MultiStatements = true
- cfg.AllowNativePasswords = true
- sqlDB, err := sql.Open("mysql", cfg.FormatDSN())
- require.NoError(t, err, "[%s] sql.Open", srv.name)
- t.Cleanup(func() { _ = sqlDB.Close() })
- return sqlDB
-}
-
-// scConn grabs a dedicated single connection (so session-variable state is stable across
-// statements) and registers its cleanup.
-func scConn(ctx context.Context, t *testing.T, sqlDB *sql.DB, srv liveServer) *sql.Conn {
- t.Helper()
- conn, err := sqlDB.Conn(ctx)
- require.NoError(t, err, "[%s] grab conn", srv.name)
- t.Cleanup(func() { _ = conn.Close() })
- return conn
-}
-
-// newSCDatabase creates a fresh database on srv and registers cleanup, returning its name.
-func newSCDatabase(ctx context.Context, t *testing.T, srv liveServer, prefix string) string {
- t.Helper()
- dbName := fmt.Sprintf("%s_%s", prefix, strings.ReplaceAll(uuid.New().String(), "-", "_"))
- admin := openLiveDB(t, srv, "")
- _, err := admin.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE `%s`", dbName))
- require.NoError(t, err, "[%s] create db", srv.name)
- t.Cleanup(func() {
- c := openLiveDB(t, srv, "")
- _, _ = c.ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName))
- })
- return dbName
-}
-
-// syncDBToMetadata syncs dbName on srv to the model.DatabaseMetadata the production release
-// path feeds schema.SDLMigration — the value whose per-object sql_mode/charset/collation
-// (and event time_zone) STAGE 2 threads into the diff.
-func syncDBToMetadata(ctx context.Context, t *testing.T, srv liveServer, dbName string) *model.DatabaseMetadata {
- t.Helper()
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err, "[%s] open driver", srv.name)
- defer driver.Close(ctx)
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err, "[%s] sync", srv.name)
- return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true)
-}
-
-// applyMigrationUnderDeployMode runs the generated migration on a session whose sql_mode is
-// first set to deploySessionMode, proving the framing (not the ambient session) governs the
-// stored context.
-func applyMigrationUnderDeployMode(ctx context.Context, t *testing.T, conn *sql.Conn, srv, migrationSQL string) {
- t.Helper()
- _, err := conn.ExecContext(ctx, "SET SESSION sql_mode = "+quoteLiteral(deploySessionMode))
- require.NoError(t, err, "[%s] set deploy mode", srv)
- _, err = conn.ExecContext(ctx, migrationSQL)
- require.NoError(t, err, "[%s] APPLY FAILED:\n%s", srv, migrationSQL)
-}
-
-// quoteLiteral single-quotes a MySQL string literal for the test's own SET statements
-// (the omni generator does its own quoting inside the migration).
-func quoteLiteral(s string) string {
- return "'" + strings.ReplaceAll(s, "'", "''") + "'"
-}
-
-func execUnderMode(ctx context.Context, t *testing.T, conn *sql.Conn, srv liveServer, sqlMode, timeZone string, stmts ...string) {
- t.Helper()
- _, err := conn.ExecContext(ctx, "SET SESSION sql_mode = "+quoteLiteral(sqlMode))
- require.NoError(t, err, "[%s] set authoring sql_mode", srv.name)
- if timeZone != "" {
- _, err = conn.ExecContext(ctx, "SET SESSION time_zone = "+quoteLiteral(timeZone))
- require.NoError(t, err, "[%s] set authoring time_zone", srv.name)
- }
- for _, s := range stmts {
- _, err = conn.ExecContext(ctx, s)
- require.NoError(t, err, "[%s] seed stmt %q", srv.name, s)
- }
-}
-
-func readRoutineSQLMode(ctx context.Context, t *testing.T, conn *sql.Conn, dbName, name string) string {
- t.Helper()
- var mode sql.NullString
- err := conn.QueryRowContext(ctx,
- "SELECT SQL_MODE FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA=? AND ROUTINE_NAME=?",
- dbName, name).Scan(&mode)
- require.NoError(t, err, "read routine sql_mode")
- return mode.String
-}
-
-func readTriggerSQLMode(ctx context.Context, t *testing.T, conn *sql.Conn, dbName, name string) string {
- t.Helper()
- var mode sql.NullString
- err := conn.QueryRowContext(ctx,
- "SELECT SQL_MODE FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA=? AND TRIGGER_NAME=?",
- dbName, name).Scan(&mode)
- require.NoError(t, err, "read trigger sql_mode")
- return mode.String
-}
-
-func readEventModeTZ(ctx context.Context, t *testing.T, conn *sql.Conn, dbName, name string) (string, string) {
- t.Helper()
- var mode, tz sql.NullString
- err := conn.QueryRowContext(ctx,
- "SELECT SQL_MODE, TIME_ZONE FROM information_schema.EVENTS WHERE EVENT_SCHEMA=? AND EVENT_NAME=?",
- dbName, name).Scan(&mode, &tz)
- require.NoError(t, err, "read event sql_mode/time_zone")
- return mode.String, tz.String
-}
-
-// TestSDLSessionContextRoutineLive proves a routine whose BODY changes is re-emitted under
-// its ORIGINAL sql_mode across the whole production wiring (schema.SDLMigration).
-//
-//nolint:tparallel
-func TestSDLSessionContextRoutineLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- dbName := newSCDatabase(ctx, t, srv, "sc_rtn")
- conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
-
- // Seed the function under the ORIGINAL mode.
- execUnderMode(ctx, t, conn, srv, origRoutineMode, "",
- "CREATE FUNCTION f(a INT) RETURNS INT DETERMINISTIC RETURN a + 1")
-
- // This is the exact value the release path hands schema.SDLMigration.
- metadata := syncDBToMetadata(ctx, t, srv, dbName)
-
- // Desired SDL: same routine, body edited (a + 2). Bare — carries no session framing.
- userSDL := "CREATE FUNCTION f(a INT) RETURNS INT DETERMINISTIC RETURN a + 2;"
- migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, migrationSQL, "[%s] expected a recreate migration", srv.name)
- t.Logf("[%s] routine migration:\n%s", srv.name, migrationSQL)
- // The framing must be present (proof STAGE 2 wired the context through).
- require.Contains(t, migrationSQL, "SET sql_mode",
- "[%s] migration lacks sql_mode framing:\n%s", srv.name, migrationSQL)
-
- applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
-
- got := readRoutineSQLMode(ctx, t, conn, dbName, "f")
- require.Equal(t, origRoutineMode, got,
- "[%s] routine sql_mode not preserved across recreate\nmigration:\n%s", srv.name, migrationSQL)
- })
- }
-}
-
-// TestSDLSessionContextTriggerLive proves a trigger whose BODY changes is re-emitted under
-// its ORIGINAL sql_mode across schema.SDLMigration.
-//
-//nolint:tparallel
-func TestSDLSessionContextTriggerLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- dbName := newSCDatabase(ctx, t, srv, "sc_trg")
- conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
-
- base := []string{
- "CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
- "CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
- }
- // Tables under the deploy mode; the TRIGGER under the ORIGINAL mode.
- execUnderMode(ctx, t, conn, srv, deploySessionMode, "", base...)
- execUnderMode(ctx, t, conn, srv, origRoutineMode, "",
- "CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val); END")
-
- metadata := syncDBToMetadata(ctx, t, srv, dbName)
-
- userSDL := strings.Join([]string{
- "CREATE TABLE t (id INT PRIMARY KEY, val INT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- "CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, tid INT, oldv INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- "CREATE TRIGGER t_audit AFTER UPDATE ON t FOR EACH ROW BEGIN INSERT INTO audit (tid, oldv) VALUES (NEW.id, OLD.val + 1); END;",
- }, "\n")
- migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, migrationSQL, "[%s] expected a trigger recreate migration", srv.name)
- t.Logf("[%s] trigger migration:\n%s", srv.name, migrationSQL)
- require.Contains(t, migrationSQL, "SET sql_mode",
- "[%s] trigger migration lacks sql_mode framing:\n%s", srv.name, migrationSQL)
-
- applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
-
- got := readTriggerSQLMode(ctx, t, conn, dbName, "t_audit")
- require.Equal(t, origRoutineMode, got,
- "[%s] trigger sql_mode not preserved across recreate\nmigration:\n%s", srv.name, migrationSQL)
- })
- }
-}
-
-// TestSDLSessionContextEventLive proves an event whose BODY changes preserves both sql_mode
-// and the ORIGINAL time_zone across schema.SDLMigration. The event modify path is an ALTER
-// EVENT … DO (which empirically re-stamps sql_mode from the session), so a bare apply would
-// silently lose the original mode — the case the framing exists for.
-//
-//nolint:tparallel
-func TestSDLSessionContextEventLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- const origEventMode = "PIPES_AS_CONCAT"
- const origTZ = "+08:00"
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- dbName := newSCDatabase(ctx, t, srv, "sc_evt")
- conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
-
- // event_scheduler toggle may lack privilege; ignore its error only.
- if _, err := conn.ExecContext(ctx, "SET GLOBAL event_scheduler = OFF"); err != nil {
- t.Logf("[%s] event_scheduler off (ignored): %v", srv.name, err)
- }
- execUnderMode(ctx, t, conn, srv, origEventMode, origTZ,
- "CREATE EVENT e ON SCHEDULE EVERY 1 HOUR DISABLE DO SET @x = 1")
-
- metadata := syncDBToMetadata(ctx, t, srv, dbName)
-
- userSDL := "CREATE EVENT e ON SCHEDULE EVERY 1 HOUR DISABLE DO SET @x = 2;"
- migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, migrationSQL, "[%s] expected an event modify migration", srv.name)
- t.Logf("[%s] event migration:\n%s", srv.name, migrationSQL)
- require.Contains(t, migrationSQL, "SET sql_mode",
- "[%s] event migration lacks sql_mode framing:\n%s", srv.name, migrationSQL)
-
- applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
-
- gotMode, gotTZ := readEventModeTZ(ctx, t, conn, dbName, "e")
- require.Equal(t, origEventMode, gotMode,
- "[%s] event sql_mode not preserved\nmigration:\n%s", srv.name, migrationSQL)
- require.Equal(t, origTZ, gotTZ,
- "[%s] event time_zone not preserved\nmigration:\n%s", srv.name, migrationSQL)
- })
- }
-}
-
-// TestSDLSessionContextNewObjectDefaultLive proves a routine ABSENT from the source (a
-// first-time create) is emitted BARE — no session framing — so it adopts the server default
-// mode. STAGE 2 must only preserve context for objects that already exist in the source.
-//
-//nolint:tparallel
-func TestSDLSessionContextNewObjectDefaultLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- dbName := newSCDatabase(ctx, t, srv, "sc_new")
- conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
-
- // Source has only a table; the routine is brand new in the desired SDL.
- execUnderMode(ctx, t, conn, srv, deploySessionMode, "",
- "CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4")
-
- metadata := syncDBToMetadata(ctx, t, srv, dbName)
-
- userSDL := strings.Join([]string{
- "CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
- "CREATE FUNCTION g(a INT) RETURNS INT DETERMINISTIC RETURN a + 5;",
- }, "\n")
- migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, migrationSQL, "[%s] expected a create migration", srv.name)
- t.Logf("[%s] new-object migration:\n%s", srv.name, migrationSQL)
- // A first-time create must NOT be wrapped in session framing.
- require.NotContains(t, migrationSQL, "SET sql_mode",
- "[%s] first-time create should be bare (no session framing):\n%s", srv.name, migrationSQL)
-
- // applyMigrationUnderDeployMode runs it on a session whose sql_mode is
- // deploySessionMode; a bare CREATE therefore captures that mode. This proves a
- // brand-new object adopts the deploy session default (no preserved context).
- applyMigrationUnderDeployMode(ctx, t, conn, srv.name, migrationSQL)
-
- got := readRoutineSQLMode(ctx, t, conn, dbName, "g")
- require.Equal(t, deploySessionMode, got,
- "[%s] new routine should adopt the deploy session mode, got %q", srv.name, got)
- })
- }
-}
-
-// TestSDLSessionContextNoChurnLive proves that an identical source and desired routine —
-// same body, with the source's session context carried in from synced metadata — produces
-// NO migration. The session context is deliberately excluded from declarative identity, so
-// a mode-only carry must never manufacture a phantom recreate.
-//
-//nolint:tparallel
-func TestSDLSessionContextNoChurnLive(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- dbName := newSCDatabase(ctx, t, srv, "sc_noop")
- conn := scConn(ctx, t, openLiveDB(t, srv, dbName), srv)
-
- execUnderMode(ctx, t, conn, srv, origRoutineMode, "",
- "CREATE FUNCTION f(a INT) RETURNS INT DETERMINISTIC RETURN a + 1")
-
- metadata := syncDBToMetadata(ctx, t, srv, dbName)
-
- // Desired SDL == the dumped current object (same body). No change intended.
- userSDL, err := schema.MetadataToSDL(storepb.Engine_MYSQL, metadata)
- require.NoError(t, err)
-
- migrationSQL, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, metadata, srv.version)
- require.NoError(t, err)
- require.Empty(t, migrationSQL,
- "[%s] identical schema (context carried) must produce no migration, got:\n%s", srv.name, migrationSQL)
- })
- }
-}
diff --git a/backend/plugin/schema/mysql/sdl_stress_live_test.go b/backend/plugin/schema/mysql/sdl_stress_live_test.go
deleted file mode 100644
index 01e8844c47..0000000000
--- a/backend/plugin/schema/mysql/sdl_stress_live_test.go
+++ /dev/null
@@ -1,1312 +0,0 @@
-package mysql
-
-// Stress smoke test for the MySQL declarative (SDL) migration path, driving the
-// PRODUCTION entry points end to end against live MySQL 5.7 + 8.0:
-//
-// - schema.SDLMigration(MYSQL, userSDL, syncedMetadata, engineVersion) — the exact
-// call database_migrate_executor.diff() makes,
-// - mysqlDiffSDLMigration(source, target, engineVersion) — version-aware diff,
-// - schema.SDLDropAdvices(MYSQL, userSDL, syncedMetadata, engineVersion) — drop advices.
-//
-// The per-type basics live in sdl_migration_omni_live_test.go. This file pushes harder:
-// a large combined schema with many interacting objects, deliberately non-canonical user
-// forms (where idempotence is won or lost), a multi-change release that must order DDL
-// correctly, a drop-heavy release with advices, and explicit 5.7-vs-8.0 divergence guards.
-//
-// Shared helpers (createLiveMySQLDriver, newLiveDatabase, applyAndDump, statementCount,
-// liveServers, liveServer, liveOraclePassword) come from sdl_migration_omni_live_test.go
-// (same package).
-
-import (
- "context"
- "strings"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-// syncMetadata applies ddl to a fresh database on srv, syncs it, and returns the
-// model.DatabaseMetadata (the value the production release path feeds to
-// schema.SDLMigration) together with the database name so the caller can apply DDL back.
-func syncMetadata(ctx context.Context, t *testing.T, srv liveServer, prefix, ddl string) (*model.DatabaseMetadata, string) {
- t.Helper()
- dbName := newLiveDatabase(ctx, t, srv, prefix)
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, ddl, db.ExecuteOptions{})
- require.NoError(t, err, "[%s] apply setup DDL:\n%s", srv.name, ddl)
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- return model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true), dbName
-}
-
-// dumpSDL syncs dbName on srv and returns the canonical MetadataToSDL dump.
-func dumpSDL(ctx context.Context, t *testing.T, srv liveServer, dbName string) string {
- t.Helper()
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- metadata, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
- source, err := schema.MetadataToSDL(storepb.Engine_MYSQL, model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_MYSQL, true))
- require.NoError(t, err)
- return source
-}
-
-// applyDDL executes ddl against dbName on srv.
-func applyDDL(ctx context.Context, t *testing.T, srv liveServer, dbName, ddl string) error {
- t.Helper()
- driver, err := createLiveMySQLDriver(ctx, srv, dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
- _, err = driver.Execute(ctx, ddl, db.ExecuteOptions{})
- return err
-}
-
-// ============================================================================
-// Scenario 1: Large combined-schema idempotence (the headline test).
-// ============================================================================
-
-// bigSchemaCore is the shared body of the large combined schema: a single realistic
-// database with many interacting objects. It spans the normalization-sensitive constructs
-// plus PKs, composite/unique/prefix/fulltext indexes, foreign keys (circular, self-ref,
-// composite), CHECK constraints, RANGE + HASH partitions, views (view-on-view, joined),
-// stored functions + procedures, multiple triggers per table, and an event. The
-// CTE/derived-table view (v_project_load) diverges by version (5.7 has no CTE support) and
-// is appended by bigSchemaProjectLoadView.
-//
-// CHECK note: 8.0 honors CHECK; 5.7 parses-and-ignores it (a no-op there). The same body
-// therefore applies on both, and the synced metadata differs only by the absent checks on
-// 5.7 — which is exactly the per-version idempotence each side must satisfy.
-//
-// FK note: department<->employee is circular, so the FK that closes the cycle
-// (department.manager_id -> employee) is added with a trailing ALTER TABLE after both
-// tables exist (the SDL loader disables foreign_key_checks, but the live setup apply does
-// not).
-const bigSchemaCore = `
-CREATE TABLE department (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- manager_id INT NULL,
- parent_id INT NULL,
- budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
- PRIMARY KEY (id),
- UNIQUE KEY uk_dept_name (name),
- CONSTRAINT fk_dept_parent FOREIGN KEY (parent_id) REFERENCES department (id) ON DELETE SET NULL,
- CONSTRAINT chk_budget CHECK (budget >= 0)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE employee (
- id INT NOT NULL AUTO_INCREMENT,
- dept_id INT NOT NULL,
- first_name VARCHAR(50) NOT NULL,
- last_name VARCHAR(50) NOT NULL,
- full_name VARCHAR(101) AS (CONCAT(first_name, ' ', last_name)) VIRTUAL,
- email VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
- active BOOLEAN NOT NULL DEFAULT TRUE,
- salary DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- hired_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- rank_kind ENUM('junior','mid','senior','staff') NOT NULL DEFAULT 'junior',
- tags SET('remote','contractor','lead') NOT NULL DEFAULT '',
- notes TEXT,
- PRIMARY KEY (id),
- UNIQUE KEY uk_emp_email (email),
- KEY idx_emp_name (last_name, first_name),
- KEY idx_emp_notes_prefix (notes(20)),
- FULLTEXT KEY ft_emp_notes (notes),
- CONSTRAINT chk_salary CHECK (salary >= 0)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE project (
- id INT NOT NULL AUTO_INCREMENT,
- dept_id INT NOT NULL,
- code VARCHAR(20) NOT NULL,
- title VARCHAR(200) NOT NULL,
- budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
- spent DECIMAL(14,2) NOT NULL DEFAULT 0.00,
- remaining DECIMAL(14,2) AS (budget - spent) STORED,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- UNIQUE KEY uk_proj_code (code),
- KEY idx_proj_dept (dept_id),
- CONSTRAINT fk_proj_dept FOREIGN KEY (dept_id) REFERENCES department (id) ON DELETE CASCADE ON UPDATE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE assignment (
- employee_id INT NOT NULL,
- project_id INT NOT NULL,
- role VARCHAR(50) NOT NULL DEFAULT 'member',
- allocation DECIMAL(5,2) NOT NULL DEFAULT 100.00,
- assigned_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (employee_id, project_id),
- KEY idx_assign_project (project_id),
- CONSTRAINT fk_assign_emp FOREIGN KEY (employee_id) REFERENCES employee (id) ON DELETE CASCADE,
- CONSTRAINT fk_assign_proj FOREIGN KEY (project_id) REFERENCES project (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE event_log (
- id BIGINT NOT NULL AUTO_INCREMENT,
- occurred_on DATE NOT NULL,
- kind VARCHAR(40) NOT NULL,
- payload JSON,
- PRIMARY KEY (id, occurred_on)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY RANGE (YEAR(occurred_on)) (
- PARTITION p2023 VALUES LESS THAN (2024),
- PARTITION p2024 VALUES LESS THAN (2025),
- PARTITION p_future VALUES LESS THAN MAXVALUE
-);
-
-CREATE TABLE metric (
- id INT NOT NULL AUTO_INCREMENT,
- bucket INT NOT NULL,
- name VARCHAR(60) NOT NULL,
- value DOUBLE NOT NULL DEFAULT 0,
- PRIMARY KEY (id, bucket)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY HASH (bucket) PARTITIONS 4;
-
-CREATE TABLE audit_trail (
- id BIGINT NOT NULL AUTO_INCREMENT,
- emp_id INT NOT NULL,
- old_salary DECIMAL(10,2),
- new_salary DECIMAL(10,2),
- changed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE category (
- id INT NOT NULL AUTO_INCREMENT,
- parent_id INT NULL,
- name VARCHAR(80) NOT NULL,
- PRIMARY KEY (id),
- KEY idx_cat_parent (parent_id),
- CONSTRAINT fk_cat_parent FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE doc (
- id INT NOT NULL AUTO_INCREMENT,
- category_id INT NOT NULL,
- owner_id INT NOT NULL,
- body MEDIUMTEXT,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- KEY idx_doc_cat (category_id),
- KEY idx_doc_owner (owner_id),
- CONSTRAINT fk_doc_cat FOREIGN KEY (category_id) REFERENCES category (id),
- CONSTRAINT fk_doc_owner FOREIGN KEY (owner_id) REFERENCES employee (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE tag_map (
- doc_id INT NOT NULL,
- tag VARCHAR(40) NOT NULL,
- weight INT NOT NULL DEFAULT 1,
- PRIMARY KEY (doc_id, tag),
- CONSTRAINT fk_tagmap_doc FOREIGN KEY (doc_id) REFERENCES doc (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_active_employees AS
-SELECT e.id, e.full_name, e.email, e.salary, d.name AS dept_name
-FROM employee e JOIN department d ON e.dept_id = d.id
-WHERE e.active = TRUE;
-
-CREATE VIEW v_dept_payroll AS
-SELECT dept_name, COUNT(*) AS headcount, SUM(salary) AS payroll
-FROM v_active_employees
-GROUP BY dept_name;
-
-CREATE FUNCTION emp_annual_salary(monthly DECIMAL(10,2)) RETURNS DECIMAL(12,2) DETERMINISTIC
-RETURN monthly * 12;
-
-CREATE FUNCTION dept_headcount(d_id INT) RETURNS INT READS SQL DATA
-RETURN (SELECT COUNT(*) FROM employee WHERE dept_id = d_id);
-
-CREATE PROCEDURE give_raise(IN emp INT, IN pct DECIMAL(5,2))
-BEGIN
- UPDATE employee SET salary = salary * (1 + pct / 100) WHERE id = emp;
-END;
-
-CREATE PROCEDURE close_project(IN proj INT)
-BEGIN
- DELETE FROM assignment WHERE project_id = proj;
- UPDATE project SET spent = budget WHERE id = proj;
-END;
-
-CREATE TRIGGER trg_emp_before_ins BEFORE INSERT ON employee FOR EACH ROW
-SET NEW.email = LOWER(NEW.email);
-
-CREATE TRIGGER trg_emp_after_upd AFTER UPDATE ON employee FOR EACH ROW
-BEGIN
- IF OLD.salary <> NEW.salary THEN
- INSERT INTO audit_trail (emp_id, old_salary, new_salary) VALUES (NEW.id, OLD.salary, NEW.salary);
- END IF;
-END;
-
-CREATE TRIGGER trg_proj_before_ins BEFORE INSERT ON project FOR EACH ROW
-SET NEW.created_at = NOW();
-
-CREATE EVENT IF NOT EXISTS ev_daily_metric ON SCHEDULE EVERY 1 DAY
-DO INSERT INTO metric (bucket, name, value) VALUES (0, 'daily', 1);
-`
-
-// circularFKClose closes the department<->employee circular dependency after both
-// tables exist (the live setup apply keeps foreign_key_checks on, unlike the SDL loader).
-const circularFKClose = `
-ALTER TABLE department ADD CONSTRAINT fk_dept_manager FOREIGN KEY (manager_id) REFERENCES employee (id) ON DELETE SET NULL;
-`
-
-// bigSchemaProjectLoadView returns the version-correct v_project_load definition.
-//
-// - 8.0 uses a CTE (WITH ...) — exercising the CTE construct and confirming the dump
-// preserves it verbatim. (8.0 also stores derived-table inner refs unqualified, so the
-// CTE form round-trips cleanly.)
-// - 5.7 has no CTE support AND mis-handles derived tables in the SDL no-op path (see
-// TestSDLStressViewDerivedTable57 — 5.7 db-qualifies derived-table inner refs, which the
-// bbcatalog-wrapped omni diff cannot fold). So 5.7 uses a view-on-view form
-// (v_active_employees-style, with a real aggregate join) that is known-idempotent and
-// keeps the large-schema test focused on finding OTHER at-scale bugs.
-func bigSchemaProjectLoadView(version string) string {
- if version == "5.7" {
- return `
-CREATE VIEW v_proj_member_count AS
-SELECT project_id, COUNT(*) AS members, SUM(allocation) AS total_alloc
-FROM assignment GROUP BY project_id;
-
-CREATE VIEW v_project_load AS
-SELECT p.code, p.title, pmc.members, pmc.total_alloc
-FROM project p JOIN v_proj_member_count pmc ON p.id = pmc.project_id;
-`
- }
- return `
-CREATE VIEW v_project_load AS
-WITH per_proj AS (
- SELECT project_id, COUNT(*) AS members, SUM(allocation) AS total_alloc
- FROM assignment GROUP BY project_id
-)
-SELECT p.code, p.title, pp.members, pp.total_alloc
-FROM project p JOIN per_proj pp ON p.id = pp.project_id;
-`
-}
-
-// bigSchemaSetup is the full apply sequence for the live database: the shared core, the
-// version-correct CTE/derived view, then the closing circular FK (added last so both
-// referenced tables exist under foreign_key_checks=ON). CHECK constraints are valid on
-// 8.0; 5.7 silently parses-and-ignores CHECK, so the same body applies on both — the
-// synced metadata differs only by the absent checks on 5.7, which is exactly the
-// per-version idempotence each side must satisfy.
-func bigSchemaSetup(version string) string {
- return bigSchemaCore + bigSchemaProjectLoadView(version) + circularFKClose
-}
-
-// bigSchemaUserSDL is what a user would author: the table bodies (circular FK inlined into
-// department) plus the version-correct project-load view. Declarative SDL has no ordering
-// constraint (the omni loader disables FK checks during load). This is the single-document
-// target D the production path diffs against.
-func bigSchemaUserSDL(version string) string {
- return bigSchemaUserSDLCore + bigSchemaProjectLoadView(version)
-}
-
-// bigSchemaUserSDLCore is the table/routine/trigger body the user authors, sans the
-// version-specific project-load view (appended by bigSchemaUserSDL).
-const bigSchemaUserSDLCore = `
-CREATE TABLE department (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(100) NOT NULL,
- manager_id INT NULL,
- parent_id INT NULL,
- budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
- PRIMARY KEY (id),
- UNIQUE KEY uk_dept_name (name),
- CONSTRAINT fk_dept_parent FOREIGN KEY (parent_id) REFERENCES department (id) ON DELETE SET NULL,
- CONSTRAINT fk_dept_manager FOREIGN KEY (manager_id) REFERENCES employee (id) ON DELETE SET NULL,
- CONSTRAINT chk_budget CHECK (budget >= 0)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE employee (
- id INT NOT NULL AUTO_INCREMENT,
- dept_id INT NOT NULL,
- first_name VARCHAR(50) NOT NULL,
- last_name VARCHAR(50) NOT NULL,
- full_name VARCHAR(101) AS (CONCAT(first_name, ' ', last_name)) VIRTUAL,
- email VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
- active BOOLEAN NOT NULL DEFAULT TRUE,
- salary DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- hired_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- rank_kind ENUM('junior','mid','senior','staff') NOT NULL DEFAULT 'junior',
- tags SET('remote','contractor','lead') NOT NULL DEFAULT '',
- notes TEXT,
- PRIMARY KEY (id),
- UNIQUE KEY uk_emp_email (email),
- KEY idx_emp_name (last_name, first_name),
- KEY idx_emp_notes_prefix (notes(20)),
- FULLTEXT KEY ft_emp_notes (notes),
- CONSTRAINT chk_salary CHECK (salary >= 0)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE project (
- id INT NOT NULL AUTO_INCREMENT,
- dept_id INT NOT NULL,
- code VARCHAR(20) NOT NULL,
- title VARCHAR(200) NOT NULL,
- budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
- spent DECIMAL(14,2) NOT NULL DEFAULT 0.00,
- remaining DECIMAL(14,2) AS (budget - spent) STORED,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- UNIQUE KEY uk_proj_code (code),
- KEY idx_proj_dept (dept_id),
- CONSTRAINT fk_proj_dept FOREIGN KEY (dept_id) REFERENCES department (id) ON DELETE CASCADE ON UPDATE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE assignment (
- employee_id INT NOT NULL,
- project_id INT NOT NULL,
- role VARCHAR(50) NOT NULL DEFAULT 'member',
- allocation DECIMAL(5,2) NOT NULL DEFAULT 100.00,
- assigned_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (employee_id, project_id),
- KEY idx_assign_project (project_id),
- CONSTRAINT fk_assign_emp FOREIGN KEY (employee_id) REFERENCES employee (id) ON DELETE CASCADE,
- CONSTRAINT fk_assign_proj FOREIGN KEY (project_id) REFERENCES project (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE event_log (
- id BIGINT NOT NULL AUTO_INCREMENT,
- occurred_on DATE NOT NULL,
- kind VARCHAR(40) NOT NULL,
- payload JSON,
- PRIMARY KEY (id, occurred_on)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY RANGE (YEAR(occurred_on)) (
- PARTITION p2023 VALUES LESS THAN (2024),
- PARTITION p2024 VALUES LESS THAN (2025),
- PARTITION p_future VALUES LESS THAN MAXVALUE
-);
-
-CREATE TABLE metric (
- id INT NOT NULL AUTO_INCREMENT,
- bucket INT NOT NULL,
- name VARCHAR(60) NOT NULL,
- value DOUBLE NOT NULL DEFAULT 0,
- PRIMARY KEY (id, bucket)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY HASH (bucket) PARTITIONS 4;
-
-CREATE TABLE audit_trail (
- id BIGINT NOT NULL AUTO_INCREMENT,
- emp_id INT NOT NULL,
- old_salary DECIMAL(10,2),
- new_salary DECIMAL(10,2),
- changed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE category (
- id INT NOT NULL AUTO_INCREMENT,
- parent_id INT NULL,
- name VARCHAR(80) NOT NULL,
- PRIMARY KEY (id),
- KEY idx_cat_parent (parent_id),
- CONSTRAINT fk_cat_parent FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE doc (
- id INT NOT NULL AUTO_INCREMENT,
- category_id INT NOT NULL,
- owner_id INT NOT NULL,
- body MEDIUMTEXT,
- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (id),
- KEY idx_doc_cat (category_id),
- KEY idx_doc_owner (owner_id),
- CONSTRAINT fk_doc_cat FOREIGN KEY (category_id) REFERENCES category (id),
- CONSTRAINT fk_doc_owner FOREIGN KEY (owner_id) REFERENCES employee (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE tag_map (
- doc_id INT NOT NULL,
- tag VARCHAR(40) NOT NULL,
- weight INT NOT NULL DEFAULT 1,
- PRIMARY KEY (doc_id, tag),
- CONSTRAINT fk_tagmap_doc FOREIGN KEY (doc_id) REFERENCES doc (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_active_employees AS
-SELECT e.id, e.full_name, e.email, e.salary, d.name AS dept_name
-FROM employee e JOIN department d ON e.dept_id = d.id
-WHERE e.active = TRUE;
-
-CREATE VIEW v_dept_payroll AS
-SELECT dept_name, COUNT(*) AS headcount, SUM(salary) AS payroll
-FROM v_active_employees
-GROUP BY dept_name;
-
-CREATE FUNCTION emp_annual_salary(monthly DECIMAL(10,2)) RETURNS DECIMAL(12,2) DETERMINISTIC
-RETURN monthly * 12;
-
-CREATE FUNCTION dept_headcount(d_id INT) RETURNS INT READS SQL DATA
-RETURN (SELECT COUNT(*) FROM employee WHERE dept_id = d_id);
-
-CREATE PROCEDURE give_raise(IN emp INT, IN pct DECIMAL(5,2))
-BEGIN
- UPDATE employee SET salary = salary * (1 + pct / 100) WHERE id = emp;
-END;
-
-CREATE PROCEDURE close_project(IN proj INT)
-BEGIN
- DELETE FROM assignment WHERE project_id = proj;
- UPDATE project SET spent = budget WHERE id = proj;
-END;
-
-CREATE TRIGGER trg_emp_before_ins BEFORE INSERT ON employee FOR EACH ROW
-SET NEW.email = LOWER(NEW.email);
-
-CREATE TRIGGER trg_emp_after_upd AFTER UPDATE ON employee FOR EACH ROW
-BEGIN
- IF OLD.salary <> NEW.salary THEN
- INSERT INTO audit_trail (emp_id, old_salary, new_salary) VALUES (NEW.id, OLD.salary, NEW.salary);
- END IF;
-END;
-
-CREATE TRIGGER trg_proj_before_ins BEFORE INSERT ON project FOR EACH ROW
-SET NEW.created_at = NOW();
-
-CREATE EVENT IF NOT EXISTS ev_daily_metric ON SCHEDULE EVERY 1 DAY
-DO INSERT INTO metric (bucket, name, value) VALUES (0, 'daily', 1);
-`
-
-// TestSDLStressLargeSchemaIdempotence is the headline at-scale idempotence proof. One
-// realistic ~13-table schema with views, view-on-view, CTE view, functions, procedures,
-// multiple triggers, an event, RANGE + HASH partitions, circular/self-ref/composite FKs,
-// and CHECK constraints (8.0) is applied to a live database, synced, dumped via
-// MetadataToSDL, then diffed against the user-authored SDL through the PRODUCTION
-// version-aware path. The diff MUST be empty on both 5.7 and 8.0.
-//
-//nolint:tparallel
-func TestSDLStressLargeSchemaIdempotence(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_big", bigSchemaSetup(srv.version))
- userSDL := bigSchemaUserSDL(srv.version)
-
- // (a) Production-path no-op: schema.SDLMigration converts meta -> SDL and diffs
- // against the user SDL, threading the version. MUST be empty.
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, meta, srv.version)
- require.NoError(t, err)
- if noop != "" {
- t.Logf("[%s] NON-EMPTY no-op diff (%d statements):\n%s", srv.name, statementCount(noop), noop)
- }
- require.Empty(t, noop, "[%s] large-schema production-path no-op must be empty, got:\n%s", srv.name, noop)
-
- // (b) source-vs-source determinism: dump and diff against itself.
- source := dumpSDL(ctx, t, srv, dbName)
- selfDiff, err := mysqlDiffSDLMigration(source, source, srv.version)
- require.NoError(t, err)
- require.Empty(t, selfDiff, "[%s] large-schema source-vs-source must be empty, got:\n%s", srv.name, selfDiff)
-
- // (c) source-vs-user-SDL via the version-aware diff (the same property as (a) but
- // through the lower-level entry the breadth tests use).
- noop2, err := mysqlDiffSDLMigration(source, userSDL, srv.version)
- require.NoError(t, err)
- require.Empty(t, noop2, "[%s] large-schema source-vs-D diff must be empty, got:\n%s", srv.name, noop2)
- })
- }
-}
-
-// TestSDLStressViewDerivedTable57 PINS a real (B) wiring bug found by the stress test:
-// on MySQL 5.7 a view whose body contains a derived table (a subquery in the FROM clause)
-// is NOT idempotent through the SDL no-op path — it emits a spurious CREATE OR REPLACE
-// VIEW.
-//
-// Root cause: MySQL 5.7's view canonicalizer fully-qualifies the table references INSIDE a
-// FROM-clause derived table with the schema name (`<synced_db>`.`assignment`), whereas 8.0
-// stores them unqualified. MetadataToSDL emits that body verbatim. The omni SDL diff loads
-// both inputs under the synthetic database `bbcatalog`, and its canonicalViewBody only
-// folds away the view's OWN-database prefix — which is now `bbcatalog`, not the real synced
-// DB name embedded in the body. So the `from` body keeps `<synced_db>`.`assignment` while
-// the `to` (user) body resolves to `bbcatalog`.`assignment` and is folded to unqualified;
-// the two bodies differ and the diff emits a no-op CREATE OR REPLACE VIEW.
-//
-// Blast radius (mapped live): 5.7 ONLY, and ONLY FROM-clause derived tables. Plain joins,
-// view-on-view, scalar subqueries (SELECT list), and IN-subqueries (WHERE) are all
-// idempotent on 5.7; 8.0 is fully idempotent including derived tables and CTEs.
-//
-// Likely fix locus: bytebase MetadataToSDL / get_database_definition.go should emit view
-// bodies with the synced own-database qualifier stripped (db-neutral) before the omni
-// loader sees them; or the omni SDL wrapping should rewrite the synced DB name to the
-// bbcatalog context. The omni canonicalViewBody itself is correct — it just can't fold a
-// qualifier naming a database other than the view's loaded own-database.
-//
-// This test is SKIPPED so the suite stays green; remove the Skip to reproduce the failure.
-//
-//nolint:tparallel
-func TestSDLStressViewDerivedTable57(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
- srv := liveServer{name: "mysql57", host: "127.0.0.1", port: "13307", version: "5.7"}
-
- ddl := `CREATE TABLE project (id INT PRIMARY KEY, code VARCHAR(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE assignment (employee_id INT, project_id INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE VIEW vx AS
-SELECT p.code, d.c
-FROM project p JOIN (SELECT project_id, COUNT(*) c FROM assignment GROUP BY project_id) d
-ON p.id = d.project_id;`
-
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_dtv", ddl)
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, ddl, meta, srv.version)
- require.NoError(t, err)
- source := dumpSDL(ctx, t, srv, dbName)
- t.Logf("dumped source:\n%s", source)
- require.Empty(t, noop, "5.7 derived-table view no-op must be empty, got:\n%s", noop)
-}
-
-// ============================================================================
-// Scenario 2: Normalization-heavy user forms (the real idempotence challenge).
-// ============================================================================
-
-// normCase authors a table in a deliberately non-canonical form. The schema is applied
-// to a live DB, synced, dumped, and the production version-aware diff of (dumped vs the
-// user form) MUST be empty — the engine must canonicalize the user form to the stored
-// form on each version.
-type normCase struct {
- name string
- ddl string
-}
-
-// normCases each name exactly one normalization the engine must absorb. They are NOT
-// merged into one table so a single failure pinpoints the offending construct.
-func normCases() []normCase {
- return []normCase{
- {
- name: "int_widths",
- // 8.0 drops widths (int/bigint/tinyint); 5.7 keeps int(11)/bigint(20)/tinyint(4).
- ddl: `CREATE TABLE t (
- a INT(11) NOT NULL,
- b BIGINT(20) NOT NULL DEFAULT 0,
- c SMALLINT(6) NOT NULL DEFAULT 0,
- d TINYINT(4) NOT NULL DEFAULT 0,
- e MEDIUMINT(9) NOT NULL DEFAULT 0,
- PRIMARY KEY (a)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "bool_boolean",
- // BOOL / BOOLEAN both store tinyint(1); TRUE/FALSE -> 1/0.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- f1 BOOL NOT NULL DEFAULT FALSE,
- f2 BOOLEAN NOT NULL DEFAULT TRUE,
- f3 BOOL NOT NULL DEFAULT 0,
- f4 BOOLEAN NOT NULL DEFAULT 1
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "bare_charset_utf8mb4",
- // No COLLATE: resolves to the version default (utf8mb4_general_ci on 5.7,
- // utf8mb4_0900_ai_ci on 8.0) — the headline 5.7-vs-8.0 collation case.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a VARCHAR(50) CHARSET utf8mb4,
- b VARCHAR(50)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "utf8_to_utf8mb3",
- // utf8 -> utf8mb3 on 8.0; stays utf8 on 5.7.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a VARCHAR(50) CHARSET utf8,
- b TEXT CHARACTER SET utf8
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "int_defaults",
- // DEFAULT 0 on int and DEFAULT '0' both store '0'.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a INT NOT NULL DEFAULT 0,
- b INT NOT NULL DEFAULT '0',
- c DECIMAL(10,2) NOT NULL DEFAULT 0,
- d VARCHAR(10) NOT NULL DEFAULT '0'
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "unnamed_index_and_fk",
- // Unnamed index + unnamed FK: the engine auto-names both. Idempotence requires
- // the dump's auto-name to canonicalize against the user's unnamed form.
- ddl: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE t (
- id INT PRIMARY KEY,
- pid INT NOT NULL,
- name VARCHAR(50) NOT NULL,
- INDEX (name),
- FOREIGN KEY (pid) REFERENCES parent (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "restrict_fk_actions",
- // RESTRICT == no clause. On 5.7 the dump renders ON UPDATE RESTRICT explicitly;
- // on 8.0 it is omitted. Either way the user RESTRICT form must canonicalize equal.
- ddl: `CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE TABLE t (
- id INT PRIMARY KEY,
- pid INT NOT NULL,
- CONSTRAINT fk_t_parent FOREIGN KEY (pid) REFERENCES parent (id) ON DELETE RESTRICT ON UPDATE RESTRICT
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "using_hash_on_innodb",
- // USING HASH is dropped on InnoDB (B-tree only).
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- a INT NOT NULL,
- KEY idx_a (a) USING HASH
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "generated_col_spacing",
- // Odd spacing/casing in the generated expression must canonicalize to the stored form.
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- price DECIMAL(10,2) NOT NULL DEFAULT 0,
- qty INT NOT NULL DEFAULT 0,
- total DECIMAL(20,2) AS ( price * qty ) STORED,
- label VARCHAR(20) AS (CONCAT('x', id)) VIRTUAL
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- // NOTE: ROW_FORMAT=DYNAMIC (and other table CREATE_OPTIONS) are NOT idempotent — see
- // TestSDLStressTableCreateOptions, a documented (B) bug. Pulled out of the passing set.
- {
- name: "enum_set",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- color ENUM('red','green','blue') NOT NULL DEFAULT 'red',
- flags SET('a','b','c') NOT NULL DEFAULT 'a,b'
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "timestamp_defaults",
- ddl: `CREATE TABLE t (
- id INT PRIMARY KEY,
- created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
- },
- {
- name: "auto_increment_seed",
- // AUTO_INCREMENT seed in the table option: the dump may carry AUTO_INCREMENT=N;
- // idempotence requires it not to phantom-diff against the user form without it.
- ddl: `CREATE TABLE t (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(50) NOT NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8mb4;`,
- },
- }
-}
-
-// TestSDLStressNormalization is where idempotence is won or lost. Each non-canonical user
-// form is applied live, synced, dumped, and the PRODUCTION version-aware diff (dumped vs
-// the user form) MUST be empty on both 5.7 and 8.0. Run as the production entry
-// schema.SDLMigration AND the version-aware mysqlDiffSDLMigration.
-//
-//nolint:tparallel
-func TestSDLStressNormalization(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- for _, nc := range normCases() {
- nc := nc
- t.Run(nc.name, func(t *testing.T) {
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_norm", nc.ddl)
-
- // Production entry: schema.SDLMigration (meta -> SDL internally).
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, nc.ddl, meta, srv.version)
- require.NoError(t, err)
- if noop != "" {
- source := dumpSDL(ctx, t, srv, dbName)
- t.Logf("[%s/%s] stored/dumped SDL:\n%s", srv.name, nc.name, source)
- t.Logf("[%s/%s] user form:\n%s", srv.name, nc.name, nc.ddl)
- t.Logf("[%s/%s] NON-EMPTY no-op diff:\n%s", srv.name, nc.name, noop)
- }
- require.Empty(t, noop, "[%s/%s] normalization no-op (SDLMigration) must be empty, got:\n%s", srv.name, nc.name, noop)
-
- // Version-aware lower-level entry, same property.
- source := dumpSDL(ctx, t, srv, dbName)
- noop2, err := mysqlDiffSDLMigration(source, nc.ddl, srv.version)
- require.NoError(t, err)
- require.Empty(t, noop2, "[%s/%s] normalization no-op (mysqlDiffSDLMigration) must be empty, got:\n%s", srv.name, nc.name, noop2)
- })
- }
- })
- }
-}
-
-// TestSDLStressTableCreateOptions PINS a second real (B) wiring bug found by the stress
-// test: a table authored with a CREATE_OPTION such as ROW_FORMAT=DYNAMIC is NOT idempotent
-// through the SDL no-op path on EITHER 5.7 or 8.0 — it emits a spurious
-// `ALTER TABLE t ROW_FORMAT=DYNAMIC`.
-//
-// Root cause is purely in bytebase's SDL renderer, not omni: MySQL stores ROW_FORMAT in
-// CREATE_OPTIONS, and bytebase's MySQL sync DOES capture it into TableMetadata.CreateOptions
-// (backend/plugin/db/mysql/sync.go:561). But the SDL dumper
-// get_database_definition.go's table-option block (~L587-609) renders only ENGINE,
-// DEFAULT CHARSET, COLLATE, COMMENT, and partitions — it never emits table.CreateOptions.
-// So the dumped `from` SDL drops ROW_FORMAT while the user `to` SDL keeps it; the omni diff
-// (correctly) sees a table-option delta and emits the ALTER. This applies to any
-// create-option (ROW_FORMAT, KEY_BLOCK_SIZE, COMPRESSION, STATS_PERSISTENT, ...).
-//
-// Likely fix locus: bytebase get_database_definition.go — emit table.CreateOptions in the
-// SDL table-option block (filtering the synthetic 'partitioned' token, which is surfaced
-// via Partitions, not as a literal option). The omni differ already round-trips ROW_FORMAT
-// when it is present on both sides.
-//
-// SKIPPED so the suite stays green; remove the Skip to reproduce.
-//
-//nolint:tparallel
-func TestSDLStressTableCreateOptions(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- ddl := `CREATE TABLE t (
- id INT PRIMARY KEY,
- a VARCHAR(100) NOT NULL
-) ENGINE=InnoDB ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4;`
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_rf", ddl)
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, ddl, meta, srv.version)
- require.NoError(t, err)
- source := dumpSDL(ctx, t, srv, dbName)
- t.Logf("[%s] dumped source (ROW_FORMAT dropped):\n%s", srv.name, source)
- require.Empty(t, noop, "[%s] ROW_FORMAT no-op must be empty, got:\n%s", srv.name, noop)
- })
- }
-}
-
-// ============================================================================
-// Scenario 3: Multi-change release (combined DDL + ordering).
-// ============================================================================
-
-// multiChangeBase is the baseline schema for the multi-change release.
-const multiChangeBase = `
-CREATE TABLE category (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(80) NOT NULL,
- legacy_code VARCHAR(40) NOT NULL DEFAULT '',
- PRIMARY KEY (id),
- KEY idx_cat_legacy (legacy_code)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE product (
- id INT NOT NULL AUTO_INCREMENT,
- category_id INT NOT NULL,
- name VARCHAR(120) NOT NULL,
- price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
- PRIMARY KEY (id),
- KEY idx_prod_cat (category_id),
- CONSTRAINT fk_prod_cat FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE legacy_audit (
- id INT NOT NULL AUTO_INCREMENT,
- product_id INT NOT NULL,
- note VARCHAR(200),
- PRIMARY KEY (id),
- KEY idx_legacy_audit_prod (product_id),
- CONSTRAINT fk_legacy_audit_prod FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE daily_stat (
- id INT NOT NULL AUTO_INCREMENT,
- bucket INT NOT NULL,
- val DOUBLE NOT NULL DEFAULT 0,
- PRIMARY KEY (id, bucket)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY HASH (bucket) PARTITIONS 2;
-
-CREATE VIEW v_catalog AS
-SELECT p.id, p.name, p.price, c.name AS category
-FROM product p JOIN category c ON p.category_id = c.id;
-`
-
-// multiChangeTarget applies MANY simultaneous changes in ONE diff:
-// - add table `review` referencing product via FK,
-// - drop the legacy_code column (which has index idx_cat_legacy) from category,
-// - modify product.price type DECIMAL(10,2) -> DECIMAL(12,4),
-// - add an index on product(name),
-// - drop table legacy_audit (an FK CHILD of product — its own FK must drop first),
-// - add a CHECK on product (8.0),
-// - replace view v_catalog,
-// - add a trigger on product,
-// - change daily_stat partitioning (HASH 2 -> HASH 4 partitions).
-//
-// The 8.0 variant includes the CHECK; multiChangeTarget57 omits it.
-const multiChangeTarget80 = `
-CREATE TABLE category (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(80) NOT NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE product (
- id INT NOT NULL AUTO_INCREMENT,
- category_id INT NOT NULL,
- name VARCHAR(120) NOT NULL,
- price DECIMAL(12,4) NOT NULL DEFAULT 0.0000,
- PRIMARY KEY (id),
- KEY idx_prod_cat (category_id),
- KEY idx_prod_name (name),
- CONSTRAINT fk_prod_cat FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE,
- CONSTRAINT chk_price CHECK (price >= 0)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE review (
- id INT NOT NULL AUTO_INCREMENT,
- product_id INT NOT NULL,
- stars INT NOT NULL DEFAULT 5,
- body VARCHAR(500),
- PRIMARY KEY (id),
- KEY idx_review_prod (product_id),
- CONSTRAINT fk_review_prod FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE daily_stat (
- id INT NOT NULL AUTO_INCREMENT,
- bucket INT NOT NULL,
- val DOUBLE NOT NULL DEFAULT 0,
- PRIMARY KEY (id, bucket)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY HASH (bucket) PARTITIONS 4;
-
-CREATE VIEW v_catalog AS
-SELECT p.id, p.name, p.price, c.name AS category, p.category_id
-FROM product p JOIN category c ON p.category_id = c.id;
-
-CREATE TRIGGER trg_prod_ins BEFORE INSERT ON product FOR EACH ROW
-SET NEW.name = TRIM(NEW.name);
-`
-
-// multiChangeTarget57 is multiChangeTarget80 without the CHECK constraint (5.7 ignores CHECK).
-const multiChangeTarget57 = `
-CREATE TABLE category (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(80) NOT NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE product (
- id INT NOT NULL AUTO_INCREMENT,
- category_id INT NOT NULL,
- name VARCHAR(120) NOT NULL,
- price DECIMAL(12,4) NOT NULL DEFAULT 0.0000,
- PRIMARY KEY (id),
- KEY idx_prod_cat (category_id),
- KEY idx_prod_name (name),
- CONSTRAINT fk_prod_cat FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE review (
- id INT NOT NULL AUTO_INCREMENT,
- product_id INT NOT NULL,
- stars INT NOT NULL DEFAULT 5,
- body VARCHAR(500),
- PRIMARY KEY (id),
- KEY idx_review_prod (product_id),
- CONSTRAINT fk_review_prod FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE daily_stat (
- id INT NOT NULL AUTO_INCREMENT,
- bucket INT NOT NULL,
- val DOUBLE NOT NULL DEFAULT 0,
- PRIMARY KEY (id, bucket)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
-PARTITION BY HASH (bucket) PARTITIONS 4;
-
-CREATE VIEW v_catalog AS
-SELECT p.id, p.name, p.price, c.name AS category, p.category_id
-FROM product p JOIN category c ON p.category_id = c.id;
-
-CREATE TRIGGER trg_prod_ins BEFORE INSERT ON product FOR EACH ROW
-SET NEW.name = TRIM(NEW.name);
-`
-
-// indexOf returns the byte index of the first occurrence of substr in s, or -1. Used to
-// assert relative ordering between two statements in a generated plan.
-func indexOf(s, substr string) int {
- return strings.Index(s, substr)
-}
-
-// TestSDLStressMultiChange applies many simultaneous changes in ONE diff and asserts the
-// plan is correct, minimal-ish, correctly ordered, and converges when applied back. Both
-// versions.
-//
-//nolint:tparallel
-func TestSDLStressMultiChange(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- target := multiChangeTarget80
- if srv.version == "5.7" {
- target = multiChangeTarget57
- }
-
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_multi", multiChangeBase)
-
- // Production path: compute the combined plan.
- plan, err := schema.SDLMigration(storepb.Engine_MYSQL, target, meta, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, plan, "[%s] multi-change must produce DDL", srv.name)
- t.Logf("[%s] multi-change plan:\n%s", srv.name, plan)
- upper := strings.ToUpper(plan)
-
- // Correctness: every intended change is present.
- require.Contains(t, upper, "CREATE TABLE `REVIEW`", "[%s] expected new review table:\n%s", srv.name, plan)
- require.Contains(t, upper, "DROP COLUMN `LEGACY_CODE`", "[%s] expected drop of legacy_code:\n%s", srv.name, plan)
- require.True(t, strings.Contains(upper, "DECIMAL(12,4)"), "[%s] expected price type change:\n%s", srv.name, plan)
- require.Contains(t, upper, "IDX_PROD_NAME", "[%s] expected new index on product(name):\n%s", srv.name, plan)
- require.Contains(t, upper, "DROP TABLE `LEGACY_AUDIT`", "[%s] expected drop of legacy_audit:\n%s", srv.name, plan)
- require.Contains(t, upper, "TRG_PROD_INS", "[%s] expected new trigger:\n%s", srv.name, plan)
- require.Contains(t, upper, "V_CATALOG", "[%s] expected view replace:\n%s", srv.name, plan)
- require.Contains(t, upper, "PARTITION BY HASH", "[%s] expected daily_stat partition change:\n%s", srv.name, plan)
- if srv.version == "8.0" {
- require.Contains(t, upper, "CHK_PRICE", "[%s] expected CHECK add:\n%s", srv.name, plan)
- }
-
- // --- Ordering correctness ---
- dropLegacyFK := indexOf(upper, "DROP FOREIGN KEY `FK_LEGACY_AUDIT_PROD`")
- dropLegacyTable := indexOf(upper, "DROP TABLE `LEGACY_AUDIT`")
- require.GreaterOrEqual(t, dropLegacyFK, 0, "[%s] expected legacy_audit FK drop:\n%s", srv.name, plan)
- require.GreaterOrEqual(t, dropLegacyTable, 0, "[%s] expected legacy_audit table drop:\n%s", srv.name, plan)
- require.Less(t, dropLegacyFK, dropLegacyTable,
- "[%s] FK drop must precede table drop:\n%s", srv.name, plan)
-
- // Index drop must precede the column drop on category (dropping legacy_code).
- dropCatIndex := indexOf(upper, "DROP INDEX `IDX_CAT_LEGACY`")
- dropCatColumn := indexOf(upper, "DROP COLUMN `LEGACY_CODE`")
- require.GreaterOrEqual(t, dropCatIndex, 0, "[%s] expected category index drop:\n%s", srv.name, plan)
- require.Less(t, dropCatIndex, dropCatColumn,
- "[%s] index drop must precede column drop:\n%s", srv.name, plan)
-
- // The review FK is deferred to PhasePost: a standalone ADD CONSTRAINT FK_REVIEW_PROD
- // must appear AFTER the review table is created (and not be inlined in the CREATE).
- createReview := indexOf(upper, "CREATE TABLE `REVIEW`")
- require.GreaterOrEqual(t, createReview, 0, "[%s] review table create not found:\n%s", srv.name, plan)
- addReviewFK := indexOf(upper, "ADD CONSTRAINT `FK_REVIEW_PROD`")
- require.GreaterOrEqual(t, addReviewFK, 0, "[%s] expected deferred review FK add:\n%s", srv.name, plan)
- require.Less(t, createReview, addReviewFK,
- "[%s] review FK add must follow the review table create (PhasePost):\n%s", srv.name, plan)
-
- // Apply the WHOLE plan back to the real DB and confirm convergence (re-diff empty).
- applyErr := applyDDL(ctx, t, srv, dbName, plan)
- require.NoError(t, applyErr, "[%s] multi-change plan failed to apply:\n%s", srv.name, plan)
-
- newSource := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(newSource, target, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "[%s] multi-change did not converge, residual:\n%s", srv.name, converge)
- })
- }
-}
-
-// ============================================================================
-// Scenario 4: Drop-heavy release + advices.
-// ============================================================================
-
-// dropHeavyBase has tables, indexes, views, and routines to drop.
-const dropHeavyBase = `
-CREATE TABLE a (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(50) NOT NULL,
- scratch VARCHAR(50),
- PRIMARY KEY (id),
- KEY idx_a_name (name),
- KEY idx_a_scratch (scratch)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE b (
- id INT NOT NULL AUTO_INCREMENT,
- a_id INT NOT NULL,
- PRIMARY KEY (id),
- KEY idx_b_a (a_id),
- CONSTRAINT fk_b_a FOREIGN KEY (a_id) REFERENCES a (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE scratch_table (
- id INT PRIMARY KEY,
- v INT NOT NULL DEFAULT 0
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE VIEW v_a AS SELECT id, name FROM a;
-
-CREATE FUNCTION f_double(x INT) RETURNS INT DETERMINISTIC RETURN x * 2;
-
-CREATE PROCEDURE p_reset() BEGIN UPDATE scratch_table SET v = 0; END;
-`
-
-// dropHeavyTarget drops scratch_table (whole table), a.scratch column (+ its index),
-// a.idx_a_name index, the v_a view, f_double function, and p_reset procedure. Table b
-// loses its FK target only if a is dropped — here a survives, b survives.
-const dropHeavyTarget = `
-CREATE TABLE a (
- id INT NOT NULL AUTO_INCREMENT,
- name VARCHAR(50) NOT NULL,
- PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
-CREATE TABLE b (
- id INT NOT NULL AUTO_INCREMENT,
- a_id INT NOT NULL,
- PRIMARY KEY (id),
- KEY idx_b_a (a_id),
- CONSTRAINT fk_b_a FOREIGN KEY (a_id) REFERENCES a (id) ON DELETE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-`
-
-// TestSDLStressDropHeavy asserts SDLDropAdvices emits WARNING advices for each destructive
-// op, and that the generated destructive DDL applies and converges. Both versions.
-//
-//nolint:tparallel
-func TestSDLStressDropHeavy(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_drop", dropHeavyBase)
-
- // Drop advices: the table/column/index drops ARE warned. (The view/function/
- // procedure drops are MISSING — a confirmed (B) bug pinned by
- // TestSDLStressDropAdvicesViewRoutineGap.)
- advices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, dropHeavyTarget, meta, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, advices, "[%s] drop-heavy target must yield advices", srv.name)
- for _, a := range advices {
- require.Equal(t, storepb.Advice_WARNING, a.Status, "[%s] drop advice must be WARNING: %+v", srv.name, a)
- }
- joined := ""
- for _, a := range advices {
- joined += a.Content + "\n"
- }
- t.Logf("[%s] drop advices:\n%s", srv.name, joined)
-
- // The table/column/index drops are warned (>=3: scratch_table, scratch column,
- // two indexes). The dropped table must be named.
- dropCount := countAdviceCode(advices, code.SDLDropOperation.Int32())
- require.GreaterOrEqual(t, dropCount, 3,
- "[%s] expected >=3 drop warnings (table, column, index), got %d:\n%s", srv.name, dropCount, joined)
- require.Contains(t, joined, "scratch_table", "[%s] expected dropped table named:\n%s", srv.name, joined)
- require.Contains(t, joined, "scratch", "[%s] expected dropped column named:\n%s", srv.name, joined)
-
- // Generate and apply the destructive DDL; confirm convergence. The PLAN correctly
- // drops the view/function/procedure even though the advices omit them.
- plan, err := schema.SDLMigration(storepb.Engine_MYSQL, dropHeavyTarget, meta, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, plan, "[%s] drop-heavy plan must be non-empty", srv.name)
- t.Logf("[%s] drop-heavy plan:\n%s", srv.name, plan)
-
- applyErr := applyDDL(ctx, t, srv, dbName, plan)
- require.NoError(t, applyErr, "[%s] drop-heavy plan failed to apply:\n%s", srv.name, plan)
-
- newSource := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(newSource, dropHeavyTarget, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "[%s] drop-heavy did not converge, residual:\n%s", srv.name, converge)
- })
- }
-}
-
-// TestSDLStressDropAdvicesViewRoutineGap PINS a third real (B) bug found by the stress
-// test: mysqlSDLDropAdvices (backend/plugin/schema/mysql/sdl_migration_omni.go) emits NO
-// advice at all for any view / function / procedure / trigger / event operation — neither a
-// DROP advice for a standalone drop nor a REPLACE advice for a redefinition. So a
-// declarative release that drops or replaces a view/routine/trigger/event gives the user
-// ZERO destructive-operation warning. Affects both 5.7 and 8.0; the generated migration DDL
-// itself is correct (it does drop/replace) — only the advice walker is wrong.
-//
-// Two compounding faults in the replace-pair detection (sdl_migration_omni.go L205-294):
-//
-// 1. The premise is wrong for this omni build. The code assumes a redefinition is rendered
-// as an OpDrop<Obj> followed by an OpCreate<Obj> of the same name, and classifies the
-// CREATE as a replace. But omni emits a redefinition as a SINGLE OpCreate<Obj> op (no
-// paired drop — verified: a view redefine yields exactly `CreateView v`). So isReplace is
-// never set for the create, and the OpCreateView/Function/... replace branch never fires
-// -> no REPLACE advice.
-//
-// 2. markDropped self-poisons the standalone-drop path. For every OpDropView it does
-// markDropped(OpCreateView, name); isReplace(OpCreateView, name) then reads that SAME map
-// and returns true for the drop itself. So the OpDropView branch's `if !isReplace(...)`
-// is false -> the DROP advice is SUPPRESSED even though there is no matching CREATE.
-//
-// Net: every view/routine/trigger/event drop is silently swallowed, and every redefinition
-// is unwarned. The table/column/index/constraint/FK/check advices are unaffected (they don't
-// go through the replace-pair logic).
-//
-// Likely fix: build the dropped/created name sets in a FIRST pass over plan.Ops, classify a
-// name as "replace" only when BOTH a drop AND a create of that name exist, and emit a REPLACE
-// advice from whichever op is present (omni's lone CreateView for a redefine), a DROP advice
-// for a drop with no matching create, and suppress the drop half only of a genuine pair.
-//
-// SKIPPED so the suite stays green; remove the Skip to reproduce.
-//
-//nolint:tparallel
-func TestSDLStressDropAdvicesViewRoutineGap(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- base := `CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-CREATE VIEW v AS SELECT id, a FROM t;
-CREATE FUNCTION f(x INT) RETURNS INT DETERMINISTIC RETURN x*2;`
- meta, _ := syncMetadata(ctx, t, srv, "sdl_stress_advgap", base)
-
- // Drop the view + function (table t survives).
- dropTarget := `CREATE TABLE t (id INT PRIMARY KEY, a INT, b INT) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`
-
- // The migration DDL DOES drop them (proving the diff is correct; only advices fail).
- plan, err := schema.SDLMigration(storepb.Engine_MYSQL, dropTarget, meta, srv.version)
- require.NoError(t, err)
- require.Contains(t, strings.ToUpper(plan), "DROP VIEW", "[%s] plan should drop the view:\n%s", srv.name, plan)
- require.Contains(t, strings.ToUpper(plan), "DROP FUNCTION", "[%s] plan should drop the function:\n%s", srv.name, plan)
-
- // But the advices are EMPTY (the bug).
- advices, err := schema.SDLDropAdvices(storepb.Engine_MYSQL, dropTarget, meta, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, advices, "[%s] BUG: dropping a view + function must yield drop advices, got none", srv.name)
- })
- }
-}
-
-// ============================================================================
-// Scenario 5: 5.7-vs-8.0 divergence guards.
-// ============================================================================
-
-// divergeSchema authors constructs whose stored form diverges by version: bare utf8mb4
-// (collation), utf8 (mb3 on 8.0), int widths, and a CHECK (8.0 only).
-const divergeSchema = `
-CREATE TABLE t (
- id INT(11) NOT NULL AUTO_INCREMENT,
- flag BOOLEAN NOT NULL DEFAULT TRUE,
- a VARCHAR(50) CHARSET utf8mb4,
- b VARCHAR(50) CHARSET utf8,
- amount DECIMAL(10,2) NOT NULL DEFAULT 0,
- PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-`
-
-// divergeSchemaWithCheck adds a CHECK (8.0 honors it; 5.7 parses-and-ignores).
-const divergeSchemaWithCheck = `
-CREATE TABLE t (
- id INT(11) NOT NULL AUTO_INCREMENT,
- flag BOOLEAN NOT NULL DEFAULT TRUE,
- a VARCHAR(50) CHARSET utf8mb4,
- b VARCHAR(50) CHARSET utf8,
- amount DECIMAL(10,2) NOT NULL DEFAULT 0,
- PRIMARY KEY (id),
- CONSTRAINT chk_amount CHECK (amount >= 0)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-`
-
-// TestSDLStressVersionDivergence confirms each version is idempotent against its OWN
-// stored form with no cross-version contamination: 5.7 never emits utf8mb4_0900_ai_ci;
-// 8.0 handles utf8mb3; CHECK is present on 8.0, absent on 5.7. It dumps the synced schema
-// and asserts version-specific properties on the canonical SDL, then proves no-op
-// idempotence per version.
-//
-//nolint:tparallel
-func TestSDLStressVersionDivergence(t *testing.T) {
- skipUnlessLiveOracle(t)
- ctx := context.Background()
-
- for _, srv := range liveServers {
- srv := srv
- t.Run(srv.name, func(t *testing.T) {
- userSDL := divergeSchema
- if srv.version == "8.0" {
- userSDL = divergeSchemaWithCheck
- }
-
- meta, dbName := syncMetadata(ctx, t, srv, "sdl_stress_diverge", userSDL)
- source := dumpSDL(ctx, t, srv, dbName)
- t.Logf("[%s] dumped SDL:\n%s", srv.name, source)
-
- // Version-specific stored-form guards on the canonical dump. Note MetadataToSDL
- // renders collations from the synced metadata (server-accurate, hence divergent)
- // but integer display widths in the 8.0-canonical form (widths dropped) on BOTH
- // versions — the int-width divergence is enforced by the omni diff normalizer, not
- // the dumper, so it is asserted via the no-op idempotence below rather than the dump
- // string. CHECK is honored on 8.0 and parsed-and-ignored on 5.7, so it appears in
- // the 8.0 dump only.
- if srv.version == "5.7" {
- require.NotContains(t, source, "utf8mb4_0900_ai_ci",
- "[%s] 5.7 dump must not contain the 8.0-only collation:\n%s", srv.name, source)
- require.NotContains(t, source, "utf8mb3",
- "[%s] 5.7 dump must not normalize utf8 -> utf8mb3:\n%s", srv.name, source)
- require.Contains(t, source, "utf8mb4_general_ci",
- "[%s] 5.7 dump must use the 5.7 default collation:\n%s", srv.name, source)
- require.NotContains(t, source, "chk_amount",
- "[%s] 5.7 ignores CHECK, so it must be absent from the dump:\n%s", srv.name, source)
- } else {
- require.Contains(t, source, "utf8mb4_0900_ai_ci",
- "[%s] 8.0 dump must use the 8.0 default collation:\n%s", srv.name, source)
- require.Contains(t, source, "utf8mb3",
- "[%s] 8.0 dump must normalize utf8 -> utf8mb3:\n%s", srv.name, source)
- require.Contains(t, source, "chk_amount",
- "[%s] 8.0 honors CHECK, so it must be present in the dump:\n%s", srv.name, source)
- }
-
- // No-op idempotence per version (production path).
- noop, err := schema.SDLMigration(storepb.Engine_MYSQL, userSDL, meta, srv.version)
- require.NoError(t, err)
- require.Empty(t, noop, "[%s] divergence no-op must be empty, got:\n%s", srv.name, noop)
-
- // A 5.7 CHANGE must never name the 8.0-only collation. Add a column to force a
- // minimal ALTER and apply it back.
- changeSDL := strings.Replace(userSDL,
- "\tPRIMARY KEY (id)",
- "\textra VARCHAR(30) NULL,\n\tPRIMARY KEY (id)", 1)
- require.NotEqual(t, userSDL, changeSDL, "[%s] test setup: change SDL must differ", srv.name)
- change, err := schema.SDLMigration(storepb.Engine_MYSQL, changeSDL, meta, srv.version)
- require.NoError(t, err)
- require.NotEmpty(t, change, "[%s] divergence change must be non-empty", srv.name)
- if srv.version == "5.7" {
- require.NotContains(t, change, "utf8mb4_0900_ai_ci",
- "[%s] 5.7 change names an 8.0-only collation (errno 1273):\n%s", srv.name, change)
- }
- applyErr := applyDDL(ctx, t, srv, dbName, change)
- require.NoError(t, applyErr, "[%s] divergence change failed to apply:\n%s", srv.name, change)
-
- // Converge.
- newSource := dumpSDL(ctx, t, srv, dbName)
- converge, err := mysqlDiffSDLMigration(newSource, changeSDL, srv.version)
- require.NoError(t, err)
- require.Empty(t, converge, "[%s] divergence change did not converge, residual:\n%s", srv.name, converge)
- })
- }
-}
diff --git a/backend/plugin/schema/mysql/testdata/sdl/big_schema_core.sql b/backend/plugin/schema/mysql/testdata/sdl/big_schema_core.sql
new file mode 100644
index 0000000000..a9d8a4937d
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/big_schema_core.sql
@@ -0,0 +1,165 @@
+
+CREATE TABLE department (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ manager_id INT NULL,
+ parent_id INT NULL,
+ budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_dept_name (name),
+ CONSTRAINT fk_dept_parent FOREIGN KEY (parent_id) REFERENCES department (id) ON DELETE SET NULL,
+ CONSTRAINT chk_budget CHECK (budget >= 0)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE employee (
+ id INT NOT NULL AUTO_INCREMENT,
+ dept_id INT NOT NULL,
+ first_name VARCHAR(50) NOT NULL,
+ last_name VARCHAR(50) NOT NULL,
+ full_name VARCHAR(101) AS (CONCAT(first_name, ' ', last_name)) VIRTUAL,
+ email VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
+ active BOOLEAN NOT NULL DEFAULT TRUE,
+ salary DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ hired_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ rank_kind ENUM('junior','mid','senior','staff') NOT NULL DEFAULT 'junior',
+ tags SET('remote','contractor','lead') NOT NULL DEFAULT '',
+ notes TEXT,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_emp_email (email),
+ KEY idx_emp_name (last_name, first_name),
+ KEY idx_emp_notes_prefix (notes(20)),
+ FULLTEXT KEY ft_emp_notes (notes),
+ CONSTRAINT chk_salary CHECK (salary >= 0)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE project (
+ id INT NOT NULL AUTO_INCREMENT,
+ dept_id INT NOT NULL,
+ code VARCHAR(20) NOT NULL,
+ title VARCHAR(200) NOT NULL,
+ budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
+ spent DECIMAL(14,2) NOT NULL DEFAULT 0.00,
+ remaining DECIMAL(14,2) AS (budget - spent) STORED,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_proj_code (code),
+ KEY idx_proj_dept (dept_id),
+ CONSTRAINT fk_proj_dept FOREIGN KEY (dept_id) REFERENCES department (id) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE assignment (
+ employee_id INT NOT NULL,
+ project_id INT NOT NULL,
+ role VARCHAR(50) NOT NULL DEFAULT 'member',
+ allocation DECIMAL(5,2) NOT NULL DEFAULT 100.00,
+ assigned_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (employee_id, project_id),
+ KEY idx_assign_project (project_id),
+ CONSTRAINT fk_assign_emp FOREIGN KEY (employee_id) REFERENCES employee (id) ON DELETE CASCADE,
+ CONSTRAINT fk_assign_proj FOREIGN KEY (project_id) REFERENCES project (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE event_log (
+ id BIGINT NOT NULL AUTO_INCREMENT,
+ occurred_on DATE NOT NULL,
+ kind VARCHAR(40) NOT NULL,
+ payload JSON,
+ PRIMARY KEY (id, occurred_on)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY RANGE (YEAR(occurred_on)) (
+ PARTITION p2023 VALUES LESS THAN (2024),
+ PARTITION p2024 VALUES LESS THAN (2025),
+ PARTITION p_future VALUES LESS THAN MAXVALUE
+);
+
+CREATE TABLE metric (
+ id INT NOT NULL AUTO_INCREMENT,
+ bucket INT NOT NULL,
+ name VARCHAR(60) NOT NULL,
+ value DOUBLE NOT NULL DEFAULT 0,
+ PRIMARY KEY (id, bucket)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY HASH (bucket) PARTITIONS 4;
+
+CREATE TABLE audit_trail (
+ id BIGINT NOT NULL AUTO_INCREMENT,
+ emp_id INT NOT NULL,
+ old_salary DECIMAL(10,2),
+ new_salary DECIMAL(10,2),
+ changed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE category (
+ id INT NOT NULL AUTO_INCREMENT,
+ parent_id INT NULL,
+ name VARCHAR(80) NOT NULL,
+ PRIMARY KEY (id),
+ KEY idx_cat_parent (parent_id),
+ CONSTRAINT fk_cat_parent FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE doc (
+ id INT NOT NULL AUTO_INCREMENT,
+ category_id INT NOT NULL,
+ owner_id INT NOT NULL,
+ body MEDIUMTEXT,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ KEY idx_doc_cat (category_id),
+ KEY idx_doc_owner (owner_id),
+ CONSTRAINT fk_doc_cat FOREIGN KEY (category_id) REFERENCES category (id),
+ CONSTRAINT fk_doc_owner FOREIGN KEY (owner_id) REFERENCES employee (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE tag_map (
+ doc_id INT NOT NULL,
+ tag VARCHAR(40) NOT NULL,
+ weight INT NOT NULL DEFAULT 1,
+ PRIMARY KEY (doc_id, tag),
+ CONSTRAINT fk_tagmap_doc FOREIGN KEY (doc_id) REFERENCES doc (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_active_employees AS
+SELECT e.id, e.full_name, e.email, e.salary, d.name AS dept_name
+FROM employee e JOIN department d ON e.dept_id = d.id
+WHERE e.active = TRUE;
+
+CREATE VIEW v_dept_payroll AS
+SELECT dept_name, COUNT(*) AS headcount, SUM(salary) AS payroll
+FROM v_active_employees
+GROUP BY dept_name;
+
+CREATE FUNCTION emp_annual_salary(monthly DECIMAL(10,2)) RETURNS DECIMAL(12,2) DETERMINISTIC
+RETURN monthly * 12;
+
+CREATE FUNCTION dept_headcount(d_id INT) RETURNS INT READS SQL DATA
+RETURN (SELECT COUNT(*) FROM employee WHERE dept_id = d_id);
+
+CREATE PROCEDURE give_raise(IN emp INT, IN pct DECIMAL(5,2))
+BEGIN
+ UPDATE employee SET salary = salary * (1 + pct / 100) WHERE id = emp;
+END;
+
+CREATE PROCEDURE close_project(IN proj INT)
+BEGIN
+ DELETE FROM assignment WHERE project_id = proj;
+ UPDATE project SET spent = budget WHERE id = proj;
+END;
+
+CREATE TRIGGER trg_emp_before_ins BEFORE INSERT ON employee FOR EACH ROW
+SET NEW.email = LOWER(NEW.email);
+
+CREATE TRIGGER trg_emp_after_upd AFTER UPDATE ON employee FOR EACH ROW
+BEGIN
+ IF OLD.salary <> NEW.salary THEN
+ INSERT INTO audit_trail (emp_id, old_salary, new_salary) VALUES (NEW.id, OLD.salary, NEW.salary);
+ END IF;
+END;
+
+CREATE TRIGGER trg_proj_before_ins BEFORE INSERT ON project FOR EACH ROW
+SET NEW.created_at = NOW();
+
+CREATE EVENT IF NOT EXISTS ev_daily_metric ON SCHEDULE EVERY 1 DAY
+DO INSERT INTO metric (bucket, name, value) VALUES (0, 'daily', 1);
diff --git a/backend/plugin/schema/mysql/testdata/sdl/big_schema_user_sdl_core.sql b/backend/plugin/schema/mysql/testdata/sdl/big_schema_user_sdl_core.sql
new file mode 100644
index 0000000000..a9c5f1e726
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/big_schema_user_sdl_core.sql
@@ -0,0 +1,166 @@
+
+CREATE TABLE department (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ manager_id INT NULL,
+ parent_id INT NULL,
+ budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_dept_name (name),
+ CONSTRAINT fk_dept_parent FOREIGN KEY (parent_id) REFERENCES department (id) ON DELETE SET NULL,
+ CONSTRAINT fk_dept_manager FOREIGN KEY (manager_id) REFERENCES employee (id) ON DELETE SET NULL,
+ CONSTRAINT chk_budget CHECK (budget >= 0)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE employee (
+ id INT NOT NULL AUTO_INCREMENT,
+ dept_id INT NOT NULL,
+ first_name VARCHAR(50) NOT NULL,
+ last_name VARCHAR(50) NOT NULL,
+ full_name VARCHAR(101) AS (CONCAT(first_name, ' ', last_name)) VIRTUAL,
+ email VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
+ active BOOLEAN NOT NULL DEFAULT TRUE,
+ salary DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ hired_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ rank_kind ENUM('junior','mid','senior','staff') NOT NULL DEFAULT 'junior',
+ tags SET('remote','contractor','lead') NOT NULL DEFAULT '',
+ notes TEXT,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_emp_email (email),
+ KEY idx_emp_name (last_name, first_name),
+ KEY idx_emp_notes_prefix (notes(20)),
+ FULLTEXT KEY ft_emp_notes (notes),
+ CONSTRAINT chk_salary CHECK (salary >= 0)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE project (
+ id INT NOT NULL AUTO_INCREMENT,
+ dept_id INT NOT NULL,
+ code VARCHAR(20) NOT NULL,
+ title VARCHAR(200) NOT NULL,
+ budget DECIMAL(14,2) NOT NULL DEFAULT 0.00,
+ spent DECIMAL(14,2) NOT NULL DEFAULT 0.00,
+ remaining DECIMAL(14,2) AS (budget - spent) STORED,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_proj_code (code),
+ KEY idx_proj_dept (dept_id),
+ CONSTRAINT fk_proj_dept FOREIGN KEY (dept_id) REFERENCES department (id) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE assignment (
+ employee_id INT NOT NULL,
+ project_id INT NOT NULL,
+ role VARCHAR(50) NOT NULL DEFAULT 'member',
+ allocation DECIMAL(5,2) NOT NULL DEFAULT 100.00,
+ assigned_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (employee_id, project_id),
+ KEY idx_assign_project (project_id),
+ CONSTRAINT fk_assign_emp FOREIGN KEY (employee_id) REFERENCES employee (id) ON DELETE CASCADE,
+ CONSTRAINT fk_assign_proj FOREIGN KEY (project_id) REFERENCES project (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE event_log (
+ id BIGINT NOT NULL AUTO_INCREMENT,
+ occurred_on DATE NOT NULL,
+ kind VARCHAR(40) NOT NULL,
+ payload JSON,
+ PRIMARY KEY (id, occurred_on)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY RANGE (YEAR(occurred_on)) (
+ PARTITION p2023 VALUES LESS THAN (2024),
+ PARTITION p2024 VALUES LESS THAN (2025),
+ PARTITION p_future VALUES LESS THAN MAXVALUE
+);
+
+CREATE TABLE metric (
+ id INT NOT NULL AUTO_INCREMENT,
+ bucket INT NOT NULL,
+ name VARCHAR(60) NOT NULL,
+ value DOUBLE NOT NULL DEFAULT 0,
+ PRIMARY KEY (id, bucket)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY HASH (bucket) PARTITIONS 4;
+
+CREATE TABLE audit_trail (
+ id BIGINT NOT NULL AUTO_INCREMENT,
+ emp_id INT NOT NULL,
+ old_salary DECIMAL(10,2),
+ new_salary DECIMAL(10,2),
+ changed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE category (
+ id INT NOT NULL AUTO_INCREMENT,
+ parent_id INT NULL,
+ name VARCHAR(80) NOT NULL,
+ PRIMARY KEY (id),
+ KEY idx_cat_parent (parent_id),
+ CONSTRAINT fk_cat_parent FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE doc (
+ id INT NOT NULL AUTO_INCREMENT,
+ category_id INT NOT NULL,
+ owner_id INT NOT NULL,
+ body MEDIUMTEXT,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ KEY idx_doc_cat (category_id),
+ KEY idx_doc_owner (owner_id),
+ CONSTRAINT fk_doc_cat FOREIGN KEY (category_id) REFERENCES category (id),
+ CONSTRAINT fk_doc_owner FOREIGN KEY (owner_id) REFERENCES employee (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE tag_map (
+ doc_id INT NOT NULL,
+ tag VARCHAR(40) NOT NULL,
+ weight INT NOT NULL DEFAULT 1,
+ PRIMARY KEY (doc_id, tag),
+ CONSTRAINT fk_tagmap_doc FOREIGN KEY (doc_id) REFERENCES doc (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_active_employees AS
+SELECT e.id, e.full_name, e.email, e.salary, d.name AS dept_name
+FROM employee e JOIN department d ON e.dept_id = d.id
+WHERE e.active = TRUE;
+
+CREATE VIEW v_dept_payroll AS
+SELECT dept_name, COUNT(*) AS headcount, SUM(salary) AS payroll
+FROM v_active_employees
+GROUP BY dept_name;
+
+CREATE FUNCTION emp_annual_salary(monthly DECIMAL(10,2)) RETURNS DECIMAL(12,2) DETERMINISTIC
+RETURN monthly * 12;
+
+CREATE FUNCTION dept_headcount(d_id INT) RETURNS INT READS SQL DATA
+RETURN (SELECT COUNT(*) FROM employee WHERE dept_id = d_id);
+
+CREATE PROCEDURE give_raise(IN emp INT, IN pct DECIMAL(5,2))
+BEGIN
+ UPDATE employee SET salary = salary * (1 + pct / 100) WHERE id = emp;
+END;
+
+CREATE PROCEDURE close_project(IN proj INT)
+BEGIN
+ DELETE FROM assignment WHERE project_id = proj;
+ UPDATE project SET spent = budget WHERE id = proj;
+END;
+
+CREATE TRIGGER trg_emp_before_ins BEFORE INSERT ON employee FOR EACH ROW
+SET NEW.email = LOWER(NEW.email);
+
+CREATE TRIGGER trg_emp_after_upd AFTER UPDATE ON employee FOR EACH ROW
+BEGIN
+ IF OLD.salary <> NEW.salary THEN
+ INSERT INTO audit_trail (emp_id, old_salary, new_salary) VALUES (NEW.id, OLD.salary, NEW.salary);
+ END IF;
+END;
+
+CREATE TRIGGER trg_proj_before_ins BEFORE INSERT ON project FOR EACH ROW
+SET NEW.created_at = NOW();
+
+CREATE EVENT IF NOT EXISTS ev_daily_metric ON SCHEDULE EVERY 1 DAY
+DO INSERT INTO metric (bucket, name, value) VALUES (0, 'daily', 1);
diff --git a/backend/plugin/schema/mysql/testdata/sdl/chain_s0.sql b/backend/plugin/schema/mysql/testdata/sdl/chain_s0.sql
new file mode 100644
index 0000000000..d6d31f37c8
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/chain_s0.sql
@@ -0,0 +1,22 @@
+
+CREATE TABLE customer (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ email VARCHAR(255) NOT NULL,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_cust_email (email)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE ord (
+ id INT NOT NULL AUTO_INCREMENT,
+ customer_id INT NOT NULL,
+ total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ KEY idx_ord_cust (customer_id),
+ CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_cust_orders AS
+SELECT c.name, o.id AS order_id, o.total
+FROM customer c JOIN ord o ON o.customer_id = c.id;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/chain_s1_common.sql b/backend/plugin/schema/mysql/testdata/sdl/chain_s1_common.sql
new file mode 100644
index 0000000000..90ad19a206
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/chain_s1_common.sql
@@ -0,0 +1,43 @@
+
+CREATE TABLE customer (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ email VARCHAR(255) NOT NULL,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_cust_email (email)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE ord (
+ id INT NOT NULL AUTO_INCREMENT,
+ customer_id INT NOT NULL,
+ total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ KEY idx_ord_cust (customer_id),
+ CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE product (
+ id INT NOT NULL AUTO_INCREMENT,
+ sku VARCHAR(40) NOT NULL,
+ name VARCHAR(120) NOT NULL,
+ price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_prod_sku (sku)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE order_item (
+ id INT NOT NULL AUTO_INCREMENT,
+ order_id INT NOT NULL,
+ product_id INT NOT NULL,
+ qty INT NOT NULL DEFAULT 1,
+ PRIMARY KEY (id),
+ KEY idx_oi_order (order_id),
+ KEY idx_oi_product (product_id),
+ CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE,
+ CONSTRAINT fk_oi_product FOREIGN KEY (product_id) REFERENCES product (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_cust_orders AS
+SELECT c.name, o.id AS order_id, o.total
+FROM customer c JOIN ord o ON o.customer_id = c.id;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/chain_s2_common.sql b/backend/plugin/schema/mysql/testdata/sdl/chain_s2_common.sql
new file mode 100644
index 0000000000..8e8d30aec5
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/chain_s2_common.sql
@@ -0,0 +1,46 @@
+
+CREATE TABLE customer (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ email VARCHAR(255) NOT NULL,
+ loyalty_points INT NOT NULL DEFAULT 0,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_cust_email (email)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE ord (
+ id INT NOT NULL AUTO_INCREMENT,
+ customer_id INT NOT NULL,
+ total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ KEY idx_ord_cust (customer_id),
+ KEY idx_ord_created (created_at),
+ CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE product (
+ id INT NOT NULL AUTO_INCREMENT,
+ sku VARCHAR(40) NOT NULL,
+ name VARCHAR(120) NOT NULL,
+ price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ price_with_tax DECIMAL(12,4) AS (price * 1.1) STORED,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_prod_sku (sku)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE order_item (
+ id INT NOT NULL AUTO_INCREMENT,
+ order_id INT NOT NULL,
+ product_id INT NOT NULL,
+ qty INT NOT NULL DEFAULT 1,
+ PRIMARY KEY (id),
+ KEY idx_oi_order (order_id),
+ KEY idx_oi_product (product_id),
+ CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE,
+ CONSTRAINT fk_oi_product FOREIGN KEY (product_id) REFERENCES product (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_cust_orders AS
+SELECT c.name, o.id AS order_id, o.total
+FROM customer c JOIN ord o ON o.customer_id = c.id;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/chain_s3_common.sql b/backend/plugin/schema/mysql/testdata/sdl/chain_s3_common.sql
new file mode 100644
index 0000000000..4aaf9c86e5
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/chain_s3_common.sql
@@ -0,0 +1,46 @@
+
+CREATE TABLE customer (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(200) NOT NULL,
+ email VARCHAR(255) NOT NULL,
+ loyalty_points INT NOT NULL DEFAULT 100,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_cust_email (email)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE ord (
+ id INT NOT NULL AUTO_INCREMENT,
+ customer_id INT NOT NULL,
+ total DECIMAL(14,4) NOT NULL DEFAULT 0.0000,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ KEY idx_ord_cust (customer_id),
+ KEY idx_ord_created (created_at),
+ CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE product (
+ id INT NOT NULL AUTO_INCREMENT,
+ sku VARCHAR(40) NOT NULL,
+ name VARCHAR(120) NOT NULL,
+ price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ price_with_tax DECIMAL(12,4) AS (price * 1.1) STORED,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_prod_sku (sku)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE order_item (
+ id INT NOT NULL AUTO_INCREMENT,
+ order_id INT NOT NULL,
+ product_id INT NOT NULL,
+ qty INT NOT NULL DEFAULT 1,
+ PRIMARY KEY (id),
+ KEY idx_oi_order (order_id),
+ KEY idx_oi_product (product_id),
+ CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE,
+ CONSTRAINT fk_oi_product FOREIGN KEY (product_id) REFERENCES product (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_cust_orders AS
+SELECT c.name, o.id AS order_id, o.total
+FROM customer c JOIN ord o ON o.customer_id = c.id;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/chain_s4_base.sql b/backend/plugin/schema/mysql/testdata/sdl/chain_s4_base.sql
new file mode 100644
index 0000000000..9ecaf00b43
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/chain_s4_base.sql
@@ -0,0 +1,36 @@
+
+CREATE TABLE customer (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(200) NOT NULL,
+ email VARCHAR(255) NOT NULL,
+ loyalty_points INT NOT NULL DEFAULT 100,
+ PRIMARY KEY (id),
+ UNIQUE KEY uk_cust_email (email)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE ord (
+ id INT NOT NULL AUTO_INCREMENT,
+ customer_id INT NOT NULL,
+ total DECIMAL(14,4) NOT NULL DEFAULT 0.0000,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (id),
+ KEY idx_ord_cust (customer_id),
+ KEY idx_ord_created (created_at){{ORD_CHECK}}
+ ,CONSTRAINT fk_ord_cust FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE order_item (
+ id INT NOT NULL AUTO_INCREMENT,
+ order_id INT NOT NULL,
+ qty INT NOT NULL DEFAULT 1,
+ PRIMARY KEY (id),
+ KEY idx_oi_order (order_id),
+ CONSTRAINT fk_oi_order FOREIGN KEY (order_id) REFERENCES ord (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_cust_orders AS
+SELECT c.name, c.email, o.id AS order_id, o.total
+FROM customer c JOIN ord o ON o.customer_id = c.id;
+
+CREATE TRIGGER trg_ord_ins BEFORE INSERT ON ord FOR EACH ROW
+SET NEW.created_at = NOW();
diff --git a/backend/plugin/schema/mysql/testdata/sdl/diverge_schema.sql b/backend/plugin/schema/mysql/testdata/sdl/diverge_schema.sql
new file mode 100644
index 0000000000..cfb8da1aa3
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/diverge_schema.sql
@@ -0,0 +1,9 @@
+
+CREATE TABLE t (
+ id INT(11) NOT NULL AUTO_INCREMENT,
+ flag BOOLEAN NOT NULL DEFAULT TRUE,
+ a VARCHAR(50) CHARSET utf8mb4,
+ b VARCHAR(50) CHARSET utf8,
+ amount DECIMAL(10,2) NOT NULL DEFAULT 0,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/diverge_schema_with_check.sql b/backend/plugin/schema/mysql/testdata/sdl/diverge_schema_with_check.sql
new file mode 100644
index 0000000000..33152a06bf
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/diverge_schema_with_check.sql
@@ -0,0 +1,10 @@
+
+CREATE TABLE t (
+ id INT(11) NOT NULL AUTO_INCREMENT,
+ flag BOOLEAN NOT NULL DEFAULT TRUE,
+ a VARCHAR(50) CHARSET utf8mb4,
+ b VARCHAR(50) CHARSET utf8,
+ amount DECIMAL(10,2) NOT NULL DEFAULT 0,
+ PRIMARY KEY (id),
+ CONSTRAINT chk_amount CHECK (amount >= 0)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/drop_heavy_base.sql b/backend/plugin/schema/mysql/testdata/sdl/drop_heavy_base.sql
new file mode 100644
index 0000000000..a47df17146
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/drop_heavy_base.sql
@@ -0,0 +1,28 @@
+
+CREATE TABLE a (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(50) NOT NULL,
+ scratch VARCHAR(50),
+ PRIMARY KEY (id),
+ KEY idx_a_name (name),
+ KEY idx_a_scratch (scratch)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE b (
+ id INT NOT NULL AUTO_INCREMENT,
+ a_id INT NOT NULL,
+ PRIMARY KEY (id),
+ KEY idx_b_a (a_id),
+ CONSTRAINT fk_b_a FOREIGN KEY (a_id) REFERENCES a (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE scratch_table (
+ id INT PRIMARY KEY,
+ v INT NOT NULL DEFAULT 0
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE VIEW v_a AS SELECT id, name FROM a;
+
+CREATE FUNCTION f_double(x INT) RETURNS INT DETERMINISTIC RETURN x * 2;
+
+CREATE PROCEDURE p_reset() BEGIN UPDATE scratch_table SET v = 0; END;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/drop_heavy_target.sql b/backend/plugin/schema/mysql/testdata/sdl/drop_heavy_target.sql
new file mode 100644
index 0000000000..17fc8cfdb6
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/drop_heavy_target.sql
@@ -0,0 +1,14 @@
+
+CREATE TABLE a (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(50) NOT NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE b (
+ id INT NOT NULL AUTO_INCREMENT,
+ a_id INT NOT NULL,
+ PRIMARY KEY (id),
+ KEY idx_b_a (a_id),
+ CONSTRAINT fk_b_a FOREIGN KEY (a_id) REFERENCES a (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/ent_a1_aux.sql b/backend/plugin/schema/mysql/testdata/sdl/ent_a1_aux.sql
new file mode 100644
index 0000000000..c983d6130c
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/ent_a1_aux.sql
@@ -0,0 +1,23 @@
+
+CREATE TABLE ent_ranked (
+ id bigint unsigned NOT NULL,
+ score integer DEFAULT '0' NOT NULL,
+ ratio decimal(5,2) DEFAULT '0.00' NOT NULL,
+ owner_userid bigint unsigned NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB;
+CREATE TABLE ent_part_log (
+ id bigint unsigned NOT NULL,
+ bucket integer NOT NULL,
+ note varchar(64) DEFAULT '' NOT NULL,
+ PRIMARY KEY (id,bucket)
+) ENGINE=InnoDB
+PARTITION BY RANGE (bucket) (
+ PARTITION p0 VALUES LESS THAN (100),
+ PARTITION p1 VALUES LESS THAN (200)
+);
+CREATE VIEW ent_v_users AS SELECT userid, username, name FROM users;
+CREATE FUNCTION ent_f_host_count() RETURNS INT READS SQL DATA RETURN (SELECT COUNT(*) FROM hosts);
+CREATE PROCEDURE ent_p_touch_user(IN uid BIGINT UNSIGNED) BEGIN UPDATE users SET name = name WHERE userid = uid; END;
+CREATE TRIGGER ent_trg_role_bi BEFORE INSERT ON role FOR EACH ROW SET NEW.name = TRIM(NEW.name);
+CREATE EVENT ent_ev_hk ON SCHEDULE EVERY 1 DAY DO DELETE FROM changelog WHERE clock < 0;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/ent_a5c_aux.sql b/backend/plugin/schema/mysql/testdata/sdl/ent_a5c_aux.sql
new file mode 100644
index 0000000000..0a25e766b5
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/ent_a5c_aux.sql
@@ -0,0 +1,3 @@
+
+CREATE VIEW ent_cmb_v AS SELECT roleid, name FROM role;
+CREATE FUNCTION ent_cmb_f() RETURNS INT DETERMINISTIC RETURN 7;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/ent_fuzz_aux.sql b/backend/plugin/schema/mysql/testdata/sdl/ent_fuzz_aux.sql
new file mode 100644
index 0000000000..0f617f83b8
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/ent_fuzz_aux.sql
@@ -0,0 +1,12 @@
+
+CREATE TABLE ent_fz_part (
+ id bigint unsigned NOT NULL,
+ bucket integer NOT NULL,
+ note varchar(40) DEFAULT '' NOT NULL,
+ PRIMARY KEY (id,bucket)
+) ENGINE=InnoDB
+PARTITION BY HASH (bucket) PARTITIONS 4;
+CREATE VIEW ent_fz_v1 AS SELECT userid, username FROM users;
+CREATE VIEW ent_fz_v2 AS SELECT hostid, host FROM hosts;
+CREATE FUNCTION ent_fz_fn1() RETURNS INT DETERMINISTIC RETURN 1;
+CREATE PROCEDURE ent_fz_pr1() BEGIN SELECT 1; END;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/ent_sf_aux_80.sql b/backend/plugin/schema/mysql/testdata/sdl/ent_sf_aux_80.sql
new file mode 100644
index 0000000000..dd2110c8c8
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/ent_sf_aux_80.sql
@@ -0,0 +1,23 @@
+
+CREATE TABLE ent_sf_geo (
+ id bigint unsigned NOT NULL,
+ pt point NOT NULL SRID 4326,
+ PRIMARY KEY (id),
+ SPATIAL KEY ent_sf_sp_seed (pt)
+) ENGINE=InnoDB;
+CREATE TABLE ent_sf_vis (
+ id bigint unsigned NOT NULL,
+ visa int NOT NULL DEFAULT '0',
+ visb int INVISIBLE NOT NULL DEFAULT '0',
+ visc int NOT NULL DEFAULT '0',
+ PRIMARY KEY (id),
+ KEY ent_sf_ki_a (visa) INVISIBLE,
+ KEY ent_sf_ki_b (visc)
+) ENGINE=InnoDB;
+CREATE TABLE ent_sf_chk (
+ id bigint unsigned NOT NULL,
+ score int NOT NULL DEFAULT '0',
+ bound int NOT NULL DEFAULT '10',
+ PRIMARY KEY (id),
+ CONSTRAINT ent_sf_chk_seed CHECK ((score >= 0))
+) ENGINE=InnoDB;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/ent_sf_aux_common.sql b/backend/plugin/schema/mysql/testdata/sdl/ent_sf_aux_common.sql
new file mode 100644
index 0000000000..3ba2a26b39
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/ent_sf_aux_common.sql
@@ -0,0 +1,23 @@
+
+CREATE TABLE ent_sf_enum (
+ id bigint unsigned NOT NULL,
+ mood enum('sunny','cloudy') NOT NULL DEFAULT 'sunny',
+ grade enum('a','b','c') NOT NULL DEFAULT 'a',
+ PRIMARY KEY (id)
+) ENGINE=InnoDB;
+CREATE TABLE ent_sf_text (
+ id bigint unsigned NOT NULL,
+ title varchar(120) NOT NULL DEFAULT '',
+ body text,
+ summary text,
+ PRIMARY KEY (id),
+ FULLTEXT KEY ent_sf_ft_seed (body)
+) ENGINE=InnoDB;
+CREATE TABLE ent_sf_gen (
+ id bigint unsigned NOT NULL,
+ price decimal(8,2) NOT NULL DEFAULT '0.00',
+ qty int NOT NULL DEFAULT '0',
+ total decimal(10,2) GENERATED ALWAYS AS ((price * 2)) STORED,
+ total2 bigint GENERATED ALWAYS AS ((qty + 7)) VIRTUAL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/ent_upg_binary_default_ddl.sql b/backend/plugin/schema/mysql/testdata/sdl/ent_upg_binary_default_ddl.sql
new file mode 100644
index 0000000000..a801778226
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/ent_upg_binary_default_ddl.sql
@@ -0,0 +1,7 @@
+
+CREATE TABLE ent_up_bin (
+ id int NOT NULL,
+ u binary(16) NOT NULL DEFAULT '',
+ v varbinary(24) NOT NULL DEFAULT 'ab',
+ PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/multi_change_base.sql b/backend/plugin/schema/mysql/testdata/sdl/multi_change_base.sql
new file mode 100644
index 0000000000..c893603c20
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/multi_change_base.sql
@@ -0,0 +1,39 @@
+
+CREATE TABLE category (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(80) NOT NULL,
+ legacy_code VARCHAR(40) NOT NULL DEFAULT '',
+ PRIMARY KEY (id),
+ KEY idx_cat_legacy (legacy_code)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE product (
+ id INT NOT NULL AUTO_INCREMENT,
+ category_id INT NOT NULL,
+ name VARCHAR(120) NOT NULL,
+ price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
+ PRIMARY KEY (id),
+ KEY idx_prod_cat (category_id),
+ CONSTRAINT fk_prod_cat FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE legacy_audit (
+ id INT NOT NULL AUTO_INCREMENT,
+ product_id INT NOT NULL,
+ note VARCHAR(200),
+ PRIMARY KEY (id),
+ KEY idx_legacy_audit_prod (product_id),
+ CONSTRAINT fk_legacy_audit_prod FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE daily_stat (
+ id INT NOT NULL AUTO_INCREMENT,
+ bucket INT NOT NULL,
+ val DOUBLE NOT NULL DEFAULT 0,
+ PRIMARY KEY (id, bucket)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY HASH (bucket) PARTITIONS 2;
+
+CREATE VIEW v_catalog AS
+SELECT p.id, p.name, p.price, c.name AS category
+FROM product p JOIN category c ON p.category_id = c.id;
diff --git a/backend/plugin/schema/mysql/testdata/sdl/multi_change_target_57.sql b/backend/plugin/schema/mysql/testdata/sdl/multi_change_target_57.sql
new file mode 100644
index 0000000000..5bfe4d12d5
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/multi_change_target_57.sql
@@ -0,0 +1,42 @@
+
+CREATE TABLE category (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(80) NOT NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE product (
+ id INT NOT NULL AUTO_INCREMENT,
+ category_id INT NOT NULL,
+ name VARCHAR(120) NOT NULL,
+ price DECIMAL(12,4) NOT NULL DEFAULT 0.0000,
+ PRIMARY KEY (id),
+ KEY idx_prod_cat (category_id),
+ KEY idx_prod_name (name),
+ CONSTRAINT fk_prod_cat FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE review (
+ id INT NOT NULL AUTO_INCREMENT,
+ product_id INT NOT NULL,
+ stars INT NOT NULL DEFAULT 5,
+ body VARCHAR(500),
+ PRIMARY KEY (id),
+ KEY idx_review_prod (product_id),
+ CONSTRAINT fk_review_prod FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE daily_stat (
+ id INT NOT NULL AUTO_INCREMENT,
+ bucket INT NOT NULL,
+ val DOUBLE NOT NULL DEFAULT 0,
+ PRIMARY KEY (id, bucket)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY HASH (bucket) PARTITIONS 4;
+
+CREATE VIEW v_catalog AS
+SELECT p.id, p.name, p.price, c.name AS category, p.category_id
+FROM product p JOIN category c ON p.category_id = c.id;
+
+CREATE TRIGGER trg_prod_ins BEFORE INSERT ON product FOR EACH ROW
+SET NEW.name = TRIM(NEW.name);
diff --git a/backend/plugin/schema/mysql/testdata/sdl/multi_change_target_80.sql b/backend/plugin/schema/mysql/testdata/sdl/multi_change_target_80.sql
new file mode 100644
index 0000000000..97fc8d6965
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/multi_change_target_80.sql
@@ -0,0 +1,43 @@
+
+CREATE TABLE category (
+ id INT NOT NULL AUTO_INCREMENT,
+ name VARCHAR(80) NOT NULL,
+ PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE product (
+ id INT NOT NULL AUTO_INCREMENT,
+ category_id INT NOT NULL,
+ name VARCHAR(120) NOT NULL,
+ price DECIMAL(12,4) NOT NULL DEFAULT 0.0000,
+ PRIMARY KEY (id),
+ KEY idx_prod_cat (category_id),
+ KEY idx_prod_name (name),
+ CONSTRAINT fk_prod_cat FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE,
+ CONSTRAINT chk_price CHECK (price >= 0)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE review (
+ id INT NOT NULL AUTO_INCREMENT,
+ product_id INT NOT NULL,
+ stars INT NOT NULL DEFAULT 5,
+ body VARCHAR(500),
+ PRIMARY KEY (id),
+ KEY idx_review_prod (product_id),
+ CONSTRAINT fk_review_prod FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE daily_stat (
+ id INT NOT NULL AUTO_INCREMENT,
+ bucket INT NOT NULL,
+ val DOUBLE NOT NULL DEFAULT 0,
+ PRIMARY KEY (id, bucket)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
+PARTITION BY HASH (bucket) PARTITIONS 4;
+
+CREATE VIEW v_catalog AS
+SELECT p.id, p.name, p.price, c.name AS category, p.category_id
+FROM product p JOIN category c ON p.category_id = c.id;
+
+CREATE TRIGGER trg_prod_ins BEFORE INSERT ON product FOR EACH ROW
+SET NEW.name = TRIM(NEW.name);
diff --git a/backend/plugin/schema/mysql/testdata/sdl/representative_ddl.sql b/backend/plugin/schema/mysql/testdata/sdl/representative_ddl.sql
new file mode 100644
index 0000000000..f794d3fb96
--- /dev/null
+++ b/backend/plugin/schema/mysql/testdata/sdl/representative_ddl.sql
@@ -0,0 +1,27 @@
+
+CREATE TABLE author (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ name VARCHAR(100) NOT NULL,
+ active BOOLEAN NOT NULL DEFAULT TRUE,
+ rating DECIMAL(5, 2) NOT NULL DEFAULT 0.00,
+ bio TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ INDEX idx_author_name (name)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE TABLE book (
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ author_id INT NOT NULL,
+ title VARCHAR(200) NOT NULL,
+ price DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
+ discount DECIMAL(4, 2) NOT NULL DEFAULT 0.00,
+ final_price DECIMAL(12, 4) AS (price * (1 - discount)) STORED,
+ page_count INT NOT NULL DEFAULT 0,
+ CONSTRAINT fk_book_author FOREIGN KEY (author_id) REFERENCES author (id) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+CREATE OR REPLACE VIEW active_books AS
+SELECT b.id, b.title, b.final_price
+FROM book b JOIN author a ON b.author_id = a.id
+WHERE a.active = TRUE;
diff --git a/backend/plugin/schema/mysql/walk_through_test.go b/backend/plugin/schema/mysql/walk_through_omni_test.go
similarity index 100%
rename from backend/plugin/schema/mysql/walk_through_test.go
rename to backend/plugin/schema/mysql/walk_through_omni_test.go
diff --git a/backend/plugin/schema/oracle/generate_migration_cases_test.go b/backend/plugin/schema/oracle/generate_migration_cases_test.go
deleted file mode 100644
index 256d6dd794..0000000000
--- a/backend/plugin/schema/oracle/generate_migration_cases_test.go
+++ /dev/null
@@ -1,1684 +0,0 @@
-package oracle
-
-// generateMigrationCase is one schema-change fixture: apply initialSchema to an
-// empty schema, then apply migrationDDL, and the generator must produce the DDL
-// that takes the second state back to the first.
-type generateMigrationCase struct {
- name string
- initialSchema string
- migrationDDL string
- description string
-}
-
-// generateMigrationCases returns the fixtures shared by the golden test and the
-// container-backed recorder that captures their metadata.
-func generateMigrationCases() []generateMigrationCase {
- return []generateMigrationCase{
- {
- name: "create_tables_with_fk",
- initialSchema: `
-CREATE TABLE USERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USERNAME VARCHAR2(50) NOT NULL,
- EMAIL VARCHAR2(100) NOT NULL,
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE UNIQUE INDEX UK_EMAIL ON USERS(EMAIL);
-CREATE INDEX IDX_USERNAME ON USERS(USERNAME);
-
-CREATE TABLE POSTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USER_ID NUMBER NOT NULL,
- TITLE VARCHAR2(200) NOT NULL,
- CONTENT CLOB,
- PUBLISHED_AT TIMESTAMP,
- CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID) ON DELETE CASCADE
-);
-
-CREATE INDEX IDX_USER_ID ON POSTS(USER_ID);
-`,
- migrationDDL: `
-DROP TABLE POSTS CASCADE CONSTRAINTS;
-DROP TABLE USERS CASCADE CONSTRAINTS;`,
- description: "Create tables with foreign key constraints",
- },
- {
- name: "basic_table_operations",
- initialSchema: `
-CREATE TABLE USERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USERNAME VARCHAR2(50) NOT NULL,
- EMAIL VARCHAR2(100) NOT NULL,
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE POSTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USER_ID NUMBER NOT NULL,
- TITLE VARCHAR2(200) NOT NULL,
- CONTENT CLOB,
- CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID)
-);
-
-CREATE INDEX IDX_POSTS_USER_ID ON POSTS(USER_ID);
-`,
- migrationDDL: `
--- Add new column
-ALTER TABLE USERS ADD IS_ACTIVE NUMBER(1) DEFAULT 1;
-
--- Create new table
-CREATE TABLE COMMENTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- POST_ID NUMBER NOT NULL,
- USER_ID NUMBER NOT NULL,
- CONTENT CLOB NOT NULL,
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT FK_POST FOREIGN KEY (POST_ID) REFERENCES POSTS(ID),
- CONSTRAINT FK_COMMENT_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID)
-);
-
--- Add new index
-CREATE INDEX IDX_USERS_EMAIL ON USERS(EMAIL);
-
--- Add check constraint
-ALTER TABLE POSTS ADD CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) > 0);
-`,
- description: "Basic table operations with columns, constraints, and indexes",
- },
- {
- name: "views_and_functions",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- PRICE NUMBER(10, 2) NOT NULL,
- STOCK NUMBER DEFAULT 0
-);
-
-CREATE TABLE ORDERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- PRODUCT_ID NUMBER NOT NULL,
- QUANTITY NUMBER NOT NULL,
- TOTAL NUMBER(10, 2),
- CONSTRAINT FK_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
-);
-`,
- migrationDDL: `
--- Create view
-CREATE VIEW PRODUCT_INVENTORY AS
-SELECT
- P.ID,
- P.NAME,
- P.PRICE,
- P.STOCK,
- NVL(SUM(O.QUANTITY), 0) AS TOTAL_ORDERED
-FROM PRODUCTS P
-LEFT JOIN ORDERS O ON P.ID = O.PRODUCT_ID
-GROUP BY P.ID, P.NAME, P.PRICE, P.STOCK;
-
--- Create function
-CREATE OR REPLACE FUNCTION CALCULATE_ORDER_TOTAL(P_PRODUCT_ID NUMBER, P_QUANTITY NUMBER)
-RETURN NUMBER
-IS
- V_PRICE NUMBER(10, 2);
-BEGIN
- SELECT PRICE INTO V_PRICE FROM PRODUCTS WHERE ID = P_PRODUCT_ID;
- RETURN P_QUANTITY * V_PRICE;
-EXCEPTION
- WHEN NO_DATA_FOUND THEN
- RETURN 0;
-END;
-/
-
--- Create materialized view
-CREATE MATERIALIZED VIEW PRODUCT_STATS AS
-SELECT
- PRODUCT_ID,
- COUNT(*) AS ORDER_COUNT,
- SUM(QUANTITY) AS TOTAL_QUANTITY,
- SUM(TOTAL) AS TOTAL_REVENUE
-FROM ORDERS
-GROUP BY PRODUCT_ID;
-
--- Create index on materialized view
-CREATE INDEX IDX_PRODUCT_STATS_REVENUE ON PRODUCT_STATS(TOTAL_REVENUE DESC);
-`,
- description: "Views, functions, and materialized views",
- },
- {
- name: "sequences",
- initialSchema: `
-CREATE TABLE ITEMS (
- ID NUMBER PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL
-);
-`,
- migrationDDL: `
--- Create sequence
-CREATE SEQUENCE ORDER_SEQ START WITH 1000 INCREMENT BY 10;
-
--- Create table using sequence
-CREATE TABLE SALES_ORDERS (
- ID NUMBER DEFAULT ORDER_SEQ.NEXTVAL PRIMARY KEY,
- ITEM_ID NUMBER NOT NULL,
- QUANTITY NUMBER NOT NULL,
- CONSTRAINT FK_ITEM FOREIGN KEY (ITEM_ID) REFERENCES ITEMS(ID)
-);
-`,
- description: "Sequences and their usage in tables",
- },
- {
- name: "drop_indexes_and_constraints",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- CATEGORY VARCHAR2(50),
- PRICE NUMBER(10, 2),
- SUPPLIER_ID NUMBER
-);
-
-CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
-CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
-CREATE UNIQUE INDEX IDX_PRODUCTS_NAME ON PRODUCTS(NAME);
-
-ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
-ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_NAME_LENGTH CHECK (LENGTH(NAME) >= 3);
-`,
- migrationDDL: `
--- Drop indexes and constraints
-DROP INDEX IDX_PRODUCTS_CATEGORY;
-DROP INDEX IDX_PRODUCTS_PRICE;
-DROP INDEX IDX_PRODUCTS_NAME;
-ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_PRICE_POSITIVE;
-ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_NAME_LENGTH;
-`,
- description: "Drop indexes and constraints from tables",
- },
- {
- name: "drop_views_and_functions",
- initialSchema: `
-CREATE TABLE SALES (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- PRODUCT_NAME VARCHAR2(100) NOT NULL,
- SALE_AMOUNT NUMBER(10, 2) NOT NULL,
- SALE_DATE DATE NOT NULL
-);
-
-CREATE VIEW MONTHLY_SALES AS
-SELECT
- EXTRACT(YEAR FROM SALE_DATE) AS YEAR_VAL,
- EXTRACT(MONTH FROM SALE_DATE) AS MONTH_VAL,
- SUM(SALE_AMOUNT) AS TOTAL_SALES
-FROM SALES
-GROUP BY EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE);
-
-CREATE MATERIALIZED VIEW TOP_PRODUCTS AS
-SELECT
- PRODUCT_NAME,
- COUNT(*) AS SALE_COUNT,
- SUM(SALE_AMOUNT) AS TOTAL_REVENUE
-FROM SALES
-GROUP BY PRODUCT_NAME
-ORDER BY SUM(SALE_AMOUNT) DESC;
-
-CREATE OR REPLACE FUNCTION GET_MONTHLY_TOTAL(YEAR_PARAM NUMBER, MONTH_PARAM NUMBER)
-RETURN NUMBER
-IS
- V_TOTAL NUMBER;
-BEGIN
- SELECT NVL(SUM(SALE_AMOUNT), 0) INTO V_TOTAL
- FROM SALES
- WHERE EXTRACT(YEAR FROM SALE_DATE) = YEAR_PARAM
- AND EXTRACT(MONTH FROM SALE_DATE) = MONTH_PARAM;
-
- RETURN V_TOTAL;
-END;
-/
-
-CREATE OR REPLACE FUNCTION CALCULATE_DISCOUNT(AMOUNT NUMBER)
-RETURN NUMBER
-IS
-BEGIN
- RETURN AMOUNT * 0.1;
-END;
-/
-`,
- migrationDDL: `
--- Drop views and functions
-DROP MATERIALIZED VIEW TOP_PRODUCTS;
-DROP VIEW MONTHLY_SALES;
-DROP FUNCTION GET_MONTHLY_TOTAL;
-DROP FUNCTION CALCULATE_DISCOUNT;
-`,
- description: "Drop views and functions that depend on tables",
- },
- {
- name: "alter_table_columns",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(50) NOT NULL,
- PRICE NUMBER(8, 2) NOT NULL,
- DESCRIPTION CLOB,
- CATEGORY VARCHAR2(30),
- IS_ACTIVE NUMBER(1) DEFAULT 1
-);
-
-CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
-CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
-`,
- migrationDDL: `
--- Alter table operations
-ALTER TABLE PRODUCTS ADD CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
-ALTER TABLE PRODUCTS ADD STOCK_QUANTITY NUMBER DEFAULT 0;
-ALTER TABLE PRODUCTS ADD WEIGHT NUMBER(5, 2);
-
--- Change column types and constraints
-ALTER TABLE PRODUCTS MODIFY NAME VARCHAR2(100);
-ALTER TABLE PRODUCTS MODIFY PRICE NUMBER(10, 2);
-ALTER TABLE PRODUCTS MODIFY DESCRIPTION NOT NULL;
-
--- Add constraints
-ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
-ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_STOCK_NON_NEGATIVE CHECK (STOCK_QUANTITY >= 0);
-
--- Add new index
-CREATE INDEX IDX_PRODUCTS_CREATED_AT ON PRODUCTS(CREATED_AT);
-CREATE UNIQUE INDEX IDX_PRODUCTS_NAME_CATEGORY ON PRODUCTS(NAME, CATEGORY);
-`,
- description: "Alter table with column additions, type changes, and constraints",
- },
- {
- name: "drop_and_recreate_constraints",
- initialSchema: `
-CREATE TABLE AUTHORS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- EMAIL VARCHAR2(100) UNIQUE
-);
-
-CREATE TABLE BOOKS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- TITLE VARCHAR2(200) NOT NULL,
- AUTHOR_ID NUMBER NOT NULL,
- ISBN VARCHAR2(20) UNIQUE,
- PUBLISHED_YEAR NUMBER,
- PRICE NUMBER(8, 2),
- CONSTRAINT FK_AUTHOR FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID),
- CONSTRAINT CHECK_YEAR_VALID CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2100),
- CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0)
-);
-
-CREATE INDEX IDX_BOOKS_AUTHOR ON BOOKS(AUTHOR_ID);
-CREATE INDEX IDX_BOOKS_YEAR ON BOOKS(PUBLISHED_YEAR);
-`,
- migrationDDL: `
--- Drop and recreate foreign key with different options
-ALTER TABLE BOOKS DROP CONSTRAINT FK_AUTHOR;
-ALTER TABLE BOOKS ADD CONSTRAINT FK_AUTHOR_NEW FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID) ON DELETE CASCADE;
-
--- Drop and modify check constraints
-ALTER TABLE BOOKS DROP CONSTRAINT CHECK_YEAR_VALID;
-ALTER TABLE BOOKS ADD CONSTRAINT CHECK_YEAR_EXTENDED CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2030);
-
--- Add new constraints
-ALTER TABLE BOOKS ADD CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) >= 3);
-`,
- description: "Drop and recreate constraints with different definitions",
- },
- {
- name: "drop_sequence",
- initialSchema: `
-CREATE SEQUENCE TRANSACTION_SEQ START WITH 1000;
-
-CREATE TABLE SIMPLE_LOG (
- ID NUMBER DEFAULT TRANSACTION_SEQ.NEXTVAL PRIMARY KEY,
- MESSAGE CLOB NOT NULL
-);
-`,
- migrationDDL: `
--- Drop table and sequence
-DROP TABLE SIMPLE_LOG;
-DROP SEQUENCE TRANSACTION_SEQ;
-`,
- description: "Drop sequence and dependent table",
- },
- {
- name: "complex_view_dependencies",
- initialSchema: `
-CREATE TABLE DEPARTMENTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- MANAGER_ID NUMBER
-);
-
-CREATE TABLE EMPLOYEES (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- DEPARTMENT_ID NUMBER,
- SALARY NUMBER(10, 2),
- CONSTRAINT FK_DEPT FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENTS(ID)
-);
-
--- Self-referencing foreign key
-ALTER TABLE DEPARTMENTS ADD CONSTRAINT FK_MANAGER FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES(ID);
-`,
- migrationDDL: `
--- Create base view
-CREATE VIEW DEPT_EMPLOYEE_COUNT AS
-SELECT D.ID AS DEPT_ID, D.NAME AS DEPT_NAME, COUNT(E.ID) AS EMP_COUNT
-FROM DEPARTMENTS D
-LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
-GROUP BY D.ID, D.NAME;
-
--- Create dependent view
-CREATE VIEW DEPT_SUMMARY AS
-SELECT
- DEC.DEPT_ID,
- DEC.DEPT_NAME,
- DEC.EMP_COUNT,
- NVL(AVG(E.SALARY), 0) AS AVG_SALARY
-FROM DEPT_EMPLOYEE_COUNT DEC
-LEFT JOIN EMPLOYEES E ON DEC.DEPT_ID = E.DEPARTMENT_ID
-GROUP BY DEC.DEPT_ID, DEC.DEPT_NAME, DEC.EMP_COUNT;
-
--- Create highly dependent view
-CREATE VIEW DEPT_MANAGER_SUMMARY AS
-SELECT
- DS.DEPT_ID,
- DS.DEPT_NAME,
- DS.EMP_COUNT,
- DS.AVG_SALARY,
- M.NAME AS MANAGER_NAME
-FROM DEPT_SUMMARY DS
-JOIN DEPARTMENTS D ON DS.DEPT_ID = D.ID
-LEFT JOIN EMPLOYEES M ON D.MANAGER_ID = M.ID;
-
--- Create materialized view with complex dependencies
-CREATE MATERIALIZED VIEW MV_DEPT_STATS AS
-SELECT
- D.ID AS DEPT_ID,
- D.NAME AS DEPT_NAME,
- COUNT(E.ID) AS TOTAL_EMPLOYEES,
- NVL(SUM(E.SALARY), 0) AS TOTAL_PAYROLL,
- NVL(AVG(E.SALARY), 0) AS AVG_SALARY,
- NVL(MAX(E.SALARY), 0) AS MAX_SALARY,
- NVL(MIN(E.SALARY), 0) AS MIN_SALARY
-FROM DEPARTMENTS D
-LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
-GROUP BY D.ID, D.NAME;
-
--- Create index on materialized view
-CREATE INDEX IDX_MV_DEPT_PAYROLL ON MV_DEPT_STATS(TOTAL_PAYROLL DESC);
-`,
- description: "Complex view dependencies with multiple levels and materialized views",
- },
- {
- name: "circular_foreign_key_dependencies",
- initialSchema: `
-CREATE TABLE CUSTOMERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- PREFERRED_ORDER_ID NUMBER
-);
-
-CREATE TABLE ORDERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- CUSTOMER_ID NUMBER NOT NULL,
- ORDER_DATE DATE DEFAULT SYSDATE,
- TOTAL_AMOUNT NUMBER(10, 2)
-);
-`,
- migrationDDL: `
--- Create circular foreign key dependencies
-ALTER TABLE CUSTOMERS ADD CONSTRAINT FK_PREFERRED_ORDER FOREIGN KEY (PREFERRED_ORDER_ID) REFERENCES ORDERS(ID);
-ALTER TABLE ORDERS ADD CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID);
-
--- Add more tables with complex relationships
-CREATE TABLE ORDER_ITEMS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- ORDER_ID NUMBER NOT NULL,
- PRODUCT_NAME VARCHAR2(100) NOT NULL,
- QUANTITY NUMBER NOT NULL,
- UNIT_PRICE NUMBER(10, 2) NOT NULL,
- CONSTRAINT FK_ORDER_ITEM FOREIGN KEY (ORDER_ID) REFERENCES ORDERS(ID) ON DELETE CASCADE
-);
-
--- Create trigger to update order total
-CREATE OR REPLACE TRIGGER TRG_UPDATE_ORDER_TOTAL
-AFTER INSERT OR UPDATE OR DELETE ON ORDER_ITEMS
-FOR EACH ROW
-DECLARE
- V_ORDER_ID NUMBER;
- V_TOTAL NUMBER(10, 2);
-BEGIN
- -- Get order ID
- IF INSERTING OR UPDATING THEN
- V_ORDER_ID := :NEW.ORDER_ID;
- ELSE
- V_ORDER_ID := :OLD.ORDER_ID;
- END IF;
-
- -- Calculate new total
- SELECT NVL(SUM(QUANTITY * UNIT_PRICE), 0) INTO V_TOTAL
- FROM ORDER_ITEMS
- WHERE ORDER_ID = V_ORDER_ID;
-
- -- Update order total
- UPDATE ORDERS SET TOTAL_AMOUNT = V_TOTAL WHERE ID = V_ORDER_ID;
-END;
-/
-`,
- description: "Circular foreign key dependencies and triggers",
- },
- {
- name: "advanced_indexes_and_constraints",
- initialSchema: `
-CREATE TABLE SALES_DATA (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY,
- SALE_DATE DATE NOT NULL,
- CUSTOMER_ID NUMBER NOT NULL,
- PRODUCT_ID NUMBER NOT NULL,
- AMOUNT NUMBER(10, 2) NOT NULL,
- REGION VARCHAR2(50) NOT NULL
-);
-`,
- migrationDDL: `
--- Create function-based index
-CREATE INDEX IDX_SALES_MONTH_YEAR ON SALES_DATA(EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE));
-
--- Create composite index with mixed sort order
-CREATE INDEX IDX_SALES_REGION_AMOUNT ON SALES_DATA(REGION ASC, AMOUNT DESC);
-
--- Create unique constraint on composite columns
-ALTER TABLE SALES_DATA ADD CONSTRAINT UK_SALES_DATE_CUSTOMER UNIQUE (SALE_DATE, CUSTOMER_ID, PRODUCT_ID);
-
--- Create check constraint with complex expression
-ALTER TABLE SALES_DATA ADD CONSTRAINT CHK_AMOUNT_RANGE CHECK (AMOUNT > 0 AND AMOUNT <= 999999.99);
-
--- Create conditional check constraint
-ALTER TABLE SALES_DATA ADD CONSTRAINT CHK_REGION_VALID CHECK (REGION IN ('NORTH', 'SOUTH', 'EAST', 'WEST', 'CENTRAL'));
-`,
- description: "Advanced Oracle-specific indexes and complex constraints",
- },
- {
- name: "stored_procedures_and_functions",
- initialSchema: `
-CREATE TABLE AUDIT_LOG (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- TABLE_NAME VARCHAR2(100) NOT NULL,
- OPERATION VARCHAR2(10) NOT NULL,
- OLD_VALUES CLOB,
- NEW_VALUES CLOB,
- TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- USER_NAME VARCHAR2(100) DEFAULT USER
-);
-`,
- migrationDDL: `
--- Create stored procedure
-CREATE OR REPLACE PROCEDURE LOG_AUDIT(
- P_TABLE_NAME VARCHAR2,
- P_OPERATION VARCHAR2,
- P_OLD_VALUES CLOB DEFAULT NULL,
- P_NEW_VALUES CLOB DEFAULT NULL
-)
-IS
-BEGIN
- INSERT INTO AUDIT_LOG (TABLE_NAME, OPERATION, OLD_VALUES, NEW_VALUES)
- VALUES (P_TABLE_NAME, P_OPERATION, P_OLD_VALUES, P_NEW_VALUES);
- COMMIT;
-EXCEPTION
- WHEN OTHERS THEN
- ROLLBACK;
- RAISE;
-END;
-/
-
--- Create function with complex logic
-CREATE OR REPLACE FUNCTION CALCULATE_BUSINESS_DAYS(
- P_START_DATE DATE,
- P_END_DATE DATE
-) RETURN NUMBER
-IS
- V_DAYS NUMBER := 0;
- V_CURRENT_DATE DATE := P_START_DATE;
-BEGIN
- WHILE V_CURRENT_DATE <= P_END_DATE LOOP
- -- Skip weekends (Saturday = 7, Sunday = 1)
- IF TO_CHAR(V_CURRENT_DATE, 'D') NOT IN ('1', '7') THEN
- V_DAYS := V_DAYS + 1;
- END IF;
- V_CURRENT_DATE := V_CURRENT_DATE + 1;
- END LOOP;
-
- RETURN V_DAYS;
-END;
-/
-
--- Create another function for testing dependencies
-CREATE OR REPLACE FUNCTION GET_AUDIT_COUNT(P_TABLE_NAME VARCHAR2) RETURN NUMBER
-IS
- V_COUNT NUMBER;
-BEGIN
- SELECT COUNT(*) INTO V_COUNT
- FROM AUDIT_LOG
- WHERE TABLE_NAME = P_TABLE_NAME;
-
- RETURN V_COUNT;
-END;
-/
-`,
- description: "Stored procedures and functions with dependencies",
- },
- {
- name: "edge_case_column_operations",
- initialSchema: `
-CREATE TABLE TEST_COLUMNS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- COL_VARCHAR VARCHAR2(50),
- COL_NUMBER NUMBER(10,2),
- COL_DATE DATE,
- COL_CLOB CLOB,
- COL_NULLABLE VARCHAR2(100),
- COL_WITH_DEFAULT VARCHAR2(50) DEFAULT 'DEFAULT_VALUE'
-);
-`,
- migrationDDL: `
--- Test various column modifications
-ALTER TABLE TEST_COLUMNS MODIFY COL_VARCHAR VARCHAR2(200);
-ALTER TABLE TEST_COLUMNS MODIFY COL_NUMBER NUMBER(15,3);
-ALTER TABLE TEST_COLUMNS MODIFY COL_NULLABLE NOT NULL;
-ALTER TABLE TEST_COLUMNS MODIFY COL_WITH_DEFAULT DEFAULT 'NEW_DEFAULT';
-
--- Add columns with various data types and constraints
-ALTER TABLE TEST_COLUMNS ADD COL_TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
-ALTER TABLE TEST_COLUMNS ADD COL_BOOLEAN NUMBER(1) CHECK (COL_BOOLEAN IN (0,1));
-
--- Drop some columns
-ALTER TABLE TEST_COLUMNS DROP COLUMN COL_CLOB;
-
--- Add constraints
-ALTER TABLE TEST_COLUMNS ADD CONSTRAINT CHK_VARCHAR_LENGTH CHECK (LENGTH(COL_VARCHAR) >= 5);
-ALTER TABLE TEST_COLUMNS ADD CONSTRAINT CHK_NUMBER_POSITIVE CHECK (COL_NUMBER > 0);
-`,
- description: "Edge cases for column operations and data type modifications",
- },
- {
- name: "reverse_create_tables_with_fk",
- initialSchema: "",
- migrationDDL: `
-CREATE TABLE USERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USERNAME VARCHAR2(50) NOT NULL,
- EMAIL VARCHAR2(100) NOT NULL,
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE UNIQUE INDEX UK_EMAIL ON USERS(EMAIL);
-CREATE INDEX IDX_USERNAME ON USERS(USERNAME);
-
-CREATE TABLE POSTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USER_ID NUMBER NOT NULL,
- TITLE VARCHAR2(200) NOT NULL,
- CONTENT CLOB,
- PUBLISHED_AT TIMESTAMP,
- CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID) ON DELETE CASCADE
-);
-
-CREATE INDEX IDX_USER_ID ON POSTS(USER_ID);
-`,
- description: "Reverse: Drop tables with foreign key constraints",
- },
- {
- name: "reverse_basic_table_operations",
- initialSchema: `
-CREATE TABLE USERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USERNAME VARCHAR2(50) NOT NULL,
- EMAIL VARCHAR2(100) NOT NULL,
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- IS_ACTIVE NUMBER(1) DEFAULT 1
-);
-
-CREATE TABLE POSTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- USER_ID NUMBER NOT NULL,
- TITLE VARCHAR2(200) NOT NULL,
- CONTENT CLOB,
- CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID),
- CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) > 0)
-);
-
-CREATE TABLE COMMENTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- POST_ID NUMBER NOT NULL,
- USER_ID NUMBER NOT NULL,
- CONTENT CLOB NOT NULL,
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT FK_POST FOREIGN KEY (POST_ID) REFERENCES POSTS(ID),
- CONSTRAINT FK_COMMENT_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID)
-);
-
-CREATE INDEX IDX_POSTS_USER_ID ON POSTS(USER_ID);
-CREATE INDEX IDX_USERS_EMAIL ON USERS(EMAIL);
-`,
- migrationDDL: `
--- Drop table
-DROP TABLE COMMENTS CASCADE CONSTRAINTS;
-
--- Drop index
-DROP INDEX IDX_USERS_EMAIL;
-
--- Drop check constraint
-ALTER TABLE POSTS DROP CONSTRAINT CHECK_TITLE_LENGTH;
-
--- Drop column
-ALTER TABLE USERS DROP COLUMN IS_ACTIVE;
-`,
- description: "Reverse: Drop tables, columns, constraints, and indexes",
- },
- {
- name: "reverse_views_and_functions",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- PRICE NUMBER(10, 2) NOT NULL,
- STOCK NUMBER DEFAULT 0
-);
-
-CREATE TABLE ORDERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- PRODUCT_ID NUMBER NOT NULL,
- QUANTITY NUMBER NOT NULL,
- TOTAL NUMBER(10, 2),
- CONSTRAINT FK_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
-);
-
-CREATE VIEW PRODUCT_INVENTORY AS
-SELECT
- P.ID,
- P.NAME,
- P.PRICE,
- P.STOCK,
- NVL(SUM(O.QUANTITY), 0) AS TOTAL_ORDERED
-FROM PRODUCTS P
-LEFT JOIN ORDERS O ON P.ID = O.PRODUCT_ID
-GROUP BY P.ID, P.NAME, P.PRICE, P.STOCK;
-
-CREATE OR REPLACE FUNCTION CALCULATE_ORDER_TOTAL(P_PRODUCT_ID NUMBER, P_QUANTITY NUMBER)
-RETURN NUMBER
-IS
- V_PRICE NUMBER(10, 2);
-BEGIN
- SELECT PRICE INTO V_PRICE FROM PRODUCTS WHERE ID = P_PRODUCT_ID;
- RETURN P_QUANTITY * V_PRICE;
-EXCEPTION
- WHEN NO_DATA_FOUND THEN
- RETURN 0;
-END;
-/
-
-CREATE MATERIALIZED VIEW PRODUCT_STATS AS
-SELECT
- PRODUCT_ID,
- COUNT(*) AS ORDER_COUNT,
- SUM(QUANTITY) AS TOTAL_QUANTITY,
- SUM(TOTAL) AS TOTAL_REVENUE
-FROM ORDERS
-GROUP BY PRODUCT_ID;
-
-CREATE INDEX IDX_PRODUCT_STATS_REVENUE ON PRODUCT_STATS(TOTAL_REVENUE DESC);
-`,
- migrationDDL: `
--- Drop index on materialized view
-DROP INDEX IDX_PRODUCT_STATS_REVENUE;
-
--- Drop materialized view
-DROP MATERIALIZED VIEW PRODUCT_STATS;
-
--- Drop function
-DROP FUNCTION CALCULATE_ORDER_TOTAL;
-
--- Drop view
-DROP VIEW PRODUCT_INVENTORY;
-`,
- description: "Reverse: Drop views, functions, and materialized views",
- },
- {
- name: "reverse_sequences",
- initialSchema: `
-CREATE SEQUENCE ORDER_SEQ START WITH 1000 INCREMENT BY 10;
-
-CREATE TABLE ITEMS (
- ID NUMBER PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL
-);
-
-CREATE TABLE SALES_ORDERS (
- ID NUMBER DEFAULT ORDER_SEQ.NEXTVAL PRIMARY KEY,
- ITEM_ID NUMBER NOT NULL,
- QUANTITY NUMBER NOT NULL,
- CONSTRAINT FK_ITEM FOREIGN KEY (ITEM_ID) REFERENCES ITEMS(ID)
-);
-`,
- migrationDDL: `
--- Drop table using sequence
-DROP TABLE SALES_ORDERS CASCADE CONSTRAINTS;
-
--- Drop sequence
-DROP SEQUENCE ORDER_SEQ;
-`,
- description: "Reverse: Drop sequences and tables using them",
- },
- {
- name: "reverse_drop_indexes_and_constraints",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- CATEGORY VARCHAR2(50),
- PRICE NUMBER(10, 2),
- SUPPLIER_ID NUMBER
-);
-`,
- migrationDDL: `
--- Create indexes and constraints
-CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
-CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
-CREATE UNIQUE INDEX IDX_PRODUCTS_NAME ON PRODUCTS(NAME);
-
-ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
-ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_NAME_LENGTH CHECK (LENGTH(NAME) >= 3);
-`,
- description: "Reverse: Create indexes and constraints on tables",
- },
- {
- name: "reverse_drop_views_and_functions",
- initialSchema: `
-CREATE TABLE SALES (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- PRODUCT_NAME VARCHAR2(100) NOT NULL,
- SALE_AMOUNT NUMBER(10, 2) NOT NULL,
- SALE_DATE DATE NOT NULL
-);
-`,
- migrationDDL: `
--- Create views and functions
-CREATE VIEW MONTHLY_SALES AS
-SELECT
- EXTRACT(YEAR FROM SALE_DATE) AS YEAR_VAL,
- EXTRACT(MONTH FROM SALE_DATE) AS MONTH_VAL,
- SUM(SALE_AMOUNT) AS TOTAL_SALES
-FROM SALES
-GROUP BY EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE);
-
-CREATE MATERIALIZED VIEW TOP_PRODUCTS AS
-SELECT
- PRODUCT_NAME,
- COUNT(*) AS SALE_COUNT,
- SUM(SALE_AMOUNT) AS TOTAL_REVENUE
-FROM SALES
-GROUP BY PRODUCT_NAME
-ORDER BY SUM(SALE_AMOUNT) DESC;
-
-CREATE OR REPLACE FUNCTION GET_MONTHLY_TOTAL(YEAR_PARAM NUMBER, MONTH_PARAM NUMBER)
-RETURN NUMBER
-IS
- V_TOTAL NUMBER;
-BEGIN
- SELECT NVL(SUM(SALE_AMOUNT), 0) INTO V_TOTAL
- FROM SALES
- WHERE EXTRACT(YEAR FROM SALE_DATE) = YEAR_PARAM
- AND EXTRACT(MONTH FROM SALE_DATE) = MONTH_PARAM;
-
- RETURN V_TOTAL;
-END;
-/
-
-CREATE OR REPLACE FUNCTION CALCULATE_DISCOUNT(AMOUNT NUMBER)
-RETURN NUMBER
-IS
-BEGIN
- RETURN AMOUNT * 0.1;
-END;
-/
-`,
- description: "Reverse: Create views and functions that depend on tables",
- },
- {
- name: "reverse_alter_table_columns",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- PRICE NUMBER(10, 2) NOT NULL,
- DESCRIPTION CLOB NOT NULL,
- CATEGORY VARCHAR2(30),
- IS_ACTIVE NUMBER(1) DEFAULT 1,
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- STOCK_QUANTITY NUMBER DEFAULT 0,
- WEIGHT NUMBER(5, 2),
- CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0),
- CONSTRAINT CHECK_STOCK_NON_NEGATIVE CHECK (STOCK_QUANTITY >= 0)
-);
-
-CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
-CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
-CREATE INDEX IDX_PRODUCTS_CREATED_AT ON PRODUCTS(CREATED_AT);
-CREATE UNIQUE INDEX IDX_PRODUCTS_NAME_CATEGORY ON PRODUCTS(NAME, CATEGORY);
-`,
- migrationDDL: `
--- Drop indexes
-DROP INDEX IDX_PRODUCTS_NAME_CATEGORY;
-DROP INDEX IDX_PRODUCTS_CREATED_AT;
-
--- Drop constraints
-ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_STOCK_NON_NEGATIVE;
-ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_PRICE_POSITIVE;
-
--- Revert column changes
-ALTER TABLE PRODUCTS MODIFY DESCRIPTION NULL;
-ALTER TABLE PRODUCTS MODIFY PRICE NUMBER(8, 2);
-ALTER TABLE PRODUCTS MODIFY NAME VARCHAR2(50);
-
--- Drop columns
-ALTER TABLE PRODUCTS DROP COLUMN WEIGHT;
-ALTER TABLE PRODUCTS DROP COLUMN STOCK_QUANTITY;
-ALTER TABLE PRODUCTS DROP COLUMN CREATED_AT;
-`,
- description: "Reverse: Remove columns, constraints and revert type changes",
- },
- {
- name: "reverse_drop_and_recreate_constraints",
- initialSchema: `
-CREATE TABLE AUTHORS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- EMAIL VARCHAR2(100) UNIQUE
-);
-
-CREATE TABLE BOOKS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- TITLE VARCHAR2(200) NOT NULL,
- AUTHOR_ID NUMBER NOT NULL,
- ISBN VARCHAR2(20) UNIQUE,
- PUBLISHED_YEAR NUMBER,
- PRICE NUMBER(8, 2),
- CONSTRAINT FK_AUTHOR_NEW FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID) ON DELETE CASCADE,
- CONSTRAINT CHECK_YEAR_EXTENDED CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2030),
- CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) >= 3)
-);
-
-CREATE INDEX IDX_BOOKS_AUTHOR ON BOOKS(AUTHOR_ID);
-CREATE INDEX IDX_BOOKS_YEAR ON BOOKS(PUBLISHED_YEAR);
-`,
- migrationDDL: `
--- Drop new constraints
-ALTER TABLE BOOKS DROP CONSTRAINT CHECK_TITLE_LENGTH;
-ALTER TABLE BOOKS DROP CONSTRAINT CHECK_YEAR_EXTENDED;
-ALTER TABLE BOOKS DROP CONSTRAINT FK_AUTHOR_NEW;
-
--- Recreate original constraints
-ALTER TABLE BOOKS ADD CONSTRAINT FK_AUTHOR FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID);
-ALTER TABLE BOOKS ADD CONSTRAINT CHECK_YEAR_VALID CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2100);
-ALTER TABLE BOOKS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
-`,
- description: "Reverse: Revert constraint modifications",
- },
- {
- name: "reverse_drop_sequence",
- initialSchema: "",
- migrationDDL: `
--- Create sequence and table
-CREATE SEQUENCE TRANSACTION_SEQ START WITH 1000;
-
-CREATE TABLE SIMPLE_LOG (
- ID NUMBER DEFAULT TRANSACTION_SEQ.NEXTVAL PRIMARY KEY,
- MESSAGE CLOB NOT NULL
-);
-`,
- description: "Reverse: Create sequence and dependent table",
- },
- {
- name: "reverse_complex_view_dependencies",
- initialSchema: `
-CREATE TABLE DEPARTMENTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- MANAGER_ID NUMBER
-);
-
-CREATE TABLE EMPLOYEES (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- DEPARTMENT_ID NUMBER,
- SALARY NUMBER(10, 2),
- CONSTRAINT FK_DEPT FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENTS(ID)
-);
-
-ALTER TABLE DEPARTMENTS ADD CONSTRAINT FK_MANAGER FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES(ID);
-
-CREATE VIEW DEPT_EMPLOYEE_COUNT AS
-SELECT D.ID AS DEPT_ID, D.NAME AS DEPT_NAME, COUNT(E.ID) AS EMP_COUNT
-FROM DEPARTMENTS D
-LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
-GROUP BY D.ID, D.NAME;
-
-CREATE VIEW DEPT_SUMMARY AS
-SELECT
- DEC.DEPT_ID,
- DEC.DEPT_NAME,
- DEC.EMP_COUNT,
- NVL(AVG(E.SALARY), 0) AS AVG_SALARY
-FROM DEPT_EMPLOYEE_COUNT DEC
-LEFT JOIN EMPLOYEES E ON DEC.DEPT_ID = E.DEPARTMENT_ID
-GROUP BY DEC.DEPT_ID, DEC.DEPT_NAME, DEC.EMP_COUNT;
-
-CREATE VIEW DEPT_MANAGER_SUMMARY AS
-SELECT
- DS.DEPT_ID,
- DS.DEPT_NAME,
- DS.EMP_COUNT,
- DS.AVG_SALARY,
- M.NAME AS MANAGER_NAME
-FROM DEPT_SUMMARY DS
-JOIN DEPARTMENTS D ON DS.DEPT_ID = D.ID
-LEFT JOIN EMPLOYEES M ON D.MANAGER_ID = M.ID;
-
-CREATE MATERIALIZED VIEW MV_DEPT_STATS AS
-SELECT
- D.ID AS DEPT_ID,
- D.NAME AS DEPT_NAME,
- COUNT(E.ID) AS TOTAL_EMPLOYEES,
- NVL(SUM(E.SALARY), 0) AS TOTAL_PAYROLL,
- NVL(AVG(E.SALARY), 0) AS AVG_SALARY,
- NVL(MAX(E.SALARY), 0) AS MAX_SALARY,
- NVL(MIN(E.SALARY), 0) AS MIN_SALARY
-FROM DEPARTMENTS D
-LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
-GROUP BY D.ID, D.NAME;
-
-CREATE INDEX IDX_MV_DEPT_PAYROLL ON MV_DEPT_STATS(TOTAL_PAYROLL DESC);
-`,
- migrationDDL: `
--- Drop index on materialized view
-DROP INDEX IDX_MV_DEPT_PAYROLL;
-
--- Drop materialized view
-DROP MATERIALIZED VIEW MV_DEPT_STATS;
-
--- Drop views in dependency order
-DROP VIEW DEPT_MANAGER_SUMMARY;
-DROP VIEW DEPT_SUMMARY;
-DROP VIEW DEPT_EMPLOYEE_COUNT;
-`,
- description: "Reverse: Drop complex view dependencies",
- },
- {
- name: "reverse_circular_foreign_key_dependencies",
- initialSchema: `
-CREATE TABLE CUSTOMERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- PREFERRED_ORDER_ID NUMBER
-);
-
-CREATE TABLE ORDERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- CUSTOMER_ID NUMBER NOT NULL,
- ORDER_DATE DATE DEFAULT SYSDATE,
- TOTAL_AMOUNT NUMBER(10, 2)
-);
-
-ALTER TABLE CUSTOMERS ADD CONSTRAINT FK_PREFERRED_ORDER FOREIGN KEY (PREFERRED_ORDER_ID) REFERENCES ORDERS(ID);
-ALTER TABLE ORDERS ADD CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID);
-
-CREATE TABLE ORDER_ITEMS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- ORDER_ID NUMBER NOT NULL,
- PRODUCT_NAME VARCHAR2(100) NOT NULL,
- QUANTITY NUMBER NOT NULL,
- UNIT_PRICE NUMBER(10, 2) NOT NULL,
- CONSTRAINT FK_ORDER_ITEM FOREIGN KEY (ORDER_ID) REFERENCES ORDERS(ID) ON DELETE CASCADE
-);
-
-CREATE OR REPLACE TRIGGER TRG_UPDATE_ORDER_TOTAL
-AFTER INSERT OR UPDATE OR DELETE ON ORDER_ITEMS
-FOR EACH ROW
-DECLARE
- V_ORDER_ID NUMBER;
- V_TOTAL NUMBER(10, 2);
-BEGIN
- -- Get order ID
- IF INSERTING OR UPDATING THEN
- V_ORDER_ID := :NEW.ORDER_ID;
- ELSE
- V_ORDER_ID := :OLD.ORDER_ID;
- END IF;
-
- -- Calculate new total
- SELECT NVL(SUM(QUANTITY * UNIT_PRICE), 0) INTO V_TOTAL
- FROM ORDER_ITEMS
- WHERE ORDER_ID = V_ORDER_ID;
-
- -- Update order total
- UPDATE ORDERS SET TOTAL_AMOUNT = V_TOTAL WHERE ID = V_ORDER_ID;
-END;
-/
-`,
- migrationDDL: `
--- Drop trigger
-DROP TRIGGER TRG_UPDATE_ORDER_TOTAL;
-
--- Drop table with foreign key
-DROP TABLE ORDER_ITEMS CASCADE CONSTRAINTS;
-
--- Drop circular foreign keys
-ALTER TABLE ORDERS DROP CONSTRAINT FK_CUSTOMER;
-ALTER TABLE CUSTOMERS DROP CONSTRAINT FK_PREFERRED_ORDER;
-`,
- description: "Reverse: Remove circular foreign key dependencies and triggers",
- },
- {
- name: "reverse_advanced_indexes_and_constraints",
- initialSchema: `
-CREATE TABLE SALES_DATA (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY,
- SALE_DATE DATE NOT NULL,
- CUSTOMER_ID NUMBER NOT NULL,
- PRODUCT_ID NUMBER NOT NULL,
- AMOUNT NUMBER(10, 2) NOT NULL,
- REGION VARCHAR2(50) NOT NULL,
- CONSTRAINT UK_SALES_DATE_CUSTOMER UNIQUE (SALE_DATE, CUSTOMER_ID, PRODUCT_ID),
- CONSTRAINT CHK_AMOUNT_RANGE CHECK (AMOUNT > 0 AND AMOUNT <= 999999.99),
- CONSTRAINT CHK_REGION_VALID CHECK (REGION IN ('NORTH', 'SOUTH', 'EAST', 'WEST', 'CENTRAL'))
-);
-
-CREATE INDEX IDX_SALES_MONTH_YEAR ON SALES_DATA(EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE));
-CREATE INDEX IDX_SALES_REGION_AMOUNT ON SALES_DATA(REGION ASC, AMOUNT DESC);
-`,
- migrationDDL: `
--- Drop indexes
-DROP INDEX IDX_SALES_REGION_AMOUNT;
-DROP INDEX IDX_SALES_MONTH_YEAR;
-
--- Drop constraints
-ALTER TABLE SALES_DATA DROP CONSTRAINT CHK_REGION_VALID;
-ALTER TABLE SALES_DATA DROP CONSTRAINT CHK_AMOUNT_RANGE;
-ALTER TABLE SALES_DATA DROP CONSTRAINT UK_SALES_DATE_CUSTOMER;
-`,
- description: "Reverse: Remove advanced indexes and complex constraints",
- },
- {
- name: "reverse_stored_procedures_and_functions",
- initialSchema: `
-CREATE TABLE AUDIT_LOG (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- TABLE_NAME VARCHAR2(100) NOT NULL,
- OPERATION VARCHAR2(10) NOT NULL,
- OLD_VALUES CLOB,
- NEW_VALUES CLOB,
- TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- USER_NAME VARCHAR2(100) DEFAULT USER
-);
-
-CREATE OR REPLACE PROCEDURE LOG_AUDIT(
- P_TABLE_NAME VARCHAR2,
- P_OPERATION VARCHAR2,
- P_OLD_VALUES CLOB DEFAULT NULL,
- P_NEW_VALUES CLOB DEFAULT NULL
-)
-IS
-BEGIN
- INSERT INTO AUDIT_LOG (TABLE_NAME, OPERATION, OLD_VALUES, NEW_VALUES)
- VALUES (P_TABLE_NAME, P_OPERATION, P_OLD_VALUES, P_NEW_VALUES);
- COMMIT;
-EXCEPTION
- WHEN OTHERS THEN
- ROLLBACK;
- RAISE;
-END;
-/
-
-CREATE OR REPLACE FUNCTION CALCULATE_BUSINESS_DAYS(
- P_START_DATE DATE,
- P_END_DATE DATE
-) RETURN NUMBER
-IS
- V_DAYS NUMBER := 0;
- V_CURRENT_DATE DATE := P_START_DATE;
-BEGIN
- WHILE V_CURRENT_DATE <= P_END_DATE LOOP
- -- Skip weekends (Saturday = 7, Sunday = 1)
- IF TO_CHAR(V_CURRENT_DATE, 'D') NOT IN ('1', '7') THEN
- V_DAYS := V_DAYS + 1;
- END IF;
- V_CURRENT_DATE := V_CURRENT_DATE + 1;
- END LOOP;
-
- RETURN V_DAYS;
-END;
-/
-
-CREATE OR REPLACE FUNCTION GET_AUDIT_COUNT(P_TABLE_NAME VARCHAR2) RETURN NUMBER
-IS
- V_COUNT NUMBER;
-BEGIN
- SELECT COUNT(*) INTO V_COUNT
- FROM AUDIT_LOG
- WHERE TABLE_NAME = P_TABLE_NAME;
-
- RETURN V_COUNT;
-END;
-/
-`,
- migrationDDL: `
--- Drop functions and procedures
-DROP FUNCTION GET_AUDIT_COUNT;
-DROP FUNCTION CALCULATE_BUSINESS_DAYS;
-DROP PROCEDURE LOG_AUDIT;
-`,
- description: "Reverse: Remove stored procedures and functions",
- },
- {
- name: "reverse_edge_case_column_operations",
- initialSchema: `
-CREATE TABLE TEST_COLUMNS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- COL_VARCHAR VARCHAR2(200) NOT NULL,
- COL_NUMBER NUMBER(15,3),
- COL_DATE DATE,
- COL_NULLABLE VARCHAR2(100) NOT NULL,
- COL_WITH_DEFAULT VARCHAR2(50) DEFAULT 'NEW_DEFAULT',
- COL_TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- COL_BOOLEAN NUMBER(1) CHECK (COL_BOOLEAN IN (0,1)),
- CONSTRAINT CHK_VARCHAR_LENGTH CHECK (LENGTH(COL_VARCHAR) >= 5),
- CONSTRAINT CHK_NUMBER_POSITIVE CHECK (COL_NUMBER > 0)
-);
-`,
- migrationDDL: `
--- Drop constraints
-ALTER TABLE TEST_COLUMNS DROP CONSTRAINT CHK_NUMBER_POSITIVE;
-ALTER TABLE TEST_COLUMNS DROP CONSTRAINT CHK_VARCHAR_LENGTH;
-
--- Drop columns
-ALTER TABLE TEST_COLUMNS DROP COLUMN COL_BOOLEAN;
-ALTER TABLE TEST_COLUMNS DROP COLUMN COL_TIMESTAMP;
-
--- Add back dropped column
-ALTER TABLE TEST_COLUMNS ADD COL_CLOB CLOB;
-
--- Revert column modifications
-ALTER TABLE TEST_COLUMNS MODIFY COL_WITH_DEFAULT DEFAULT 'DEFAULT_VALUE';
-ALTER TABLE TEST_COLUMNS MODIFY COL_NULLABLE NULL;
-ALTER TABLE TEST_COLUMNS MODIFY COL_NUMBER NUMBER(10,2);
-ALTER TABLE TEST_COLUMNS MODIFY COL_VARCHAR VARCHAR2(50);
-`,
- description: "Reverse: Revert edge case column operations",
- },
- {
- name: "table_and_column_comments",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- PRICE NUMBER(10, 2) NOT NULL,
- DESCRIPTION CLOB,
- CATEGORY VARCHAR2(50),
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE CUSTOMERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- EMAIL VARCHAR2(100),
- PHONE VARCHAR2(20),
- ADDRESS CLOB
-);
-
-CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
-CREATE UNIQUE INDEX IDX_CUSTOMERS_EMAIL ON CUSTOMERS(EMAIL);
-`,
- migrationDDL: `
--- Add table comments
-COMMENT ON TABLE PRODUCTS IS 'Product catalog table containing all product information';
-COMMENT ON TABLE CUSTOMERS IS 'Customer information and contact details';
-
--- Add column comments
-COMMENT ON COLUMN PRODUCTS.ID IS 'Unique product identifier (auto-generated)';
-COMMENT ON COLUMN PRODUCTS.NAME IS 'Product name - must be unique and descriptive';
-COMMENT ON COLUMN PRODUCTS.PRICE IS 'Product price in USD with 2 decimal places';
-COMMENT ON COLUMN PRODUCTS.DESCRIPTION IS 'Detailed product description with HTML formatting support';
-COMMENT ON COLUMN PRODUCTS.CATEGORY IS 'Product category for filtering and organization';
-COMMENT ON COLUMN PRODUCTS.CREATED_AT IS 'Timestamp when the product was added to catalog';
-
-COMMENT ON COLUMN CUSTOMERS.ID IS 'Primary key for customer records';
-COMMENT ON COLUMN CUSTOMERS.NAME IS 'Full name of the customer';
-COMMENT ON COLUMN CUSTOMERS.EMAIL IS 'Customer email address (unique)';
-COMMENT ON COLUMN CUSTOMERS.PHONE IS 'Contact phone number';
-COMMENT ON COLUMN CUSTOMERS.ADDRESS IS 'Full mailing address with multiple lines';
-
--- Create new table with comments immediately
-CREATE TABLE ORDERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- CUSTOMER_ID NUMBER NOT NULL,
- PRODUCT_ID NUMBER NOT NULL,
- ORDER_DATE DATE DEFAULT SYSDATE,
- QUANTITY NUMBER NOT NULL,
- TOTAL_AMOUNT NUMBER(12, 2),
- STATUS VARCHAR2(20) DEFAULT 'PENDING',
- CONSTRAINT FK_ORDER_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID),
- CONSTRAINT FK_ORDER_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
-);
-
--- Add comments to the new table
-COMMENT ON TABLE ORDERS IS 'Customer orders linking products and customers';
-COMMENT ON COLUMN ORDERS.ID IS 'Unique order identifier';
-COMMENT ON COLUMN ORDERS.CUSTOMER_ID IS 'Reference to customer who placed the order';
-COMMENT ON COLUMN ORDERS.PRODUCT_ID IS 'Reference to ordered product';
-COMMENT ON COLUMN ORDERS.ORDER_DATE IS 'Date when the order was placed';
-COMMENT ON COLUMN ORDERS.QUANTITY IS 'Number of items ordered';
-COMMENT ON COLUMN ORDERS.TOTAL_AMOUNT IS 'Total order amount including taxes';
-COMMENT ON COLUMN ORDERS.STATUS IS 'Current order status (PENDING, SHIPPED, DELIVERED, CANCELLED)';
-`,
- description: "Add comprehensive comments to tables and columns using Oracle COMMENT syntax",
- },
- {
- name: "modify_and_drop_comments",
- initialSchema: `
-CREATE TABLE INVENTORY (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- PRODUCT_CODE VARCHAR2(50) NOT NULL,
- STOCK_LEVEL NUMBER NOT NULL,
- MIN_STOCK NUMBER DEFAULT 10,
- MAX_STOCK NUMBER DEFAULT 1000,
- WAREHOUSE_ID NUMBER,
- LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
--- Add initial comments
-COMMENT ON TABLE INVENTORY IS 'Initial inventory tracking table';
-COMMENT ON COLUMN INVENTORY.ID IS 'Primary key for inventory records';
-COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Product identification code';
-COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Current stock quantity';
-COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Minimum stock threshold';
-COMMENT ON COLUMN INVENTORY.MAX_STOCK IS 'Maximum stock capacity';
-COMMENT ON COLUMN INVENTORY.WAREHOUSE_ID IS 'Warehouse location identifier';
-
-CREATE TABLE SUPPLIERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- CONTACT_EMAIL VARCHAR2(100),
- RATING NUMBER(2,1)
-);
-
-COMMENT ON TABLE SUPPLIERS IS 'Supplier information table';
-COMMENT ON COLUMN SUPPLIERS.NAME IS 'Supplier company name';
-COMMENT ON COLUMN SUPPLIERS.CONTACT_EMAIL IS 'Primary contact email';
-`,
- migrationDDL: `
--- Modify existing table comment
-COMMENT ON TABLE INVENTORY IS 'Updated inventory management system with enhanced tracking capabilities';
-
--- Modify existing column comments
-COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Updated: Standardized product SKU following new naming convention';
-COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Updated: Real-time stock quantity with automated updates';
-COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Updated: Dynamic minimum threshold based on sales velocity';
-
--- Remove comments by setting to empty string
-COMMENT ON COLUMN INVENTORY.MAX_STOCK IS '';
-COMMENT ON COLUMN INVENTORY.WAREHOUSE_ID IS '';
-
--- Remove table comment
-COMMENT ON TABLE SUPPLIERS IS '';
-
--- Remove specific column comments using empty string
-COMMENT ON COLUMN SUPPLIERS.CONTACT_EMAIL IS '';
-
--- Add new comment to previously uncommented column
-COMMENT ON COLUMN INVENTORY.LAST_UPDATED IS 'Timestamp of last inventory update or modification';
-
--- Add new column with comment
-ALTER TABLE INVENTORY ADD REORDER_POINT NUMBER DEFAULT 0;
-COMMENT ON COLUMN INVENTORY.REORDER_POINT IS 'Calculated reorder point based on lead time and demand';
-
--- Modify existing column comment after it was cleared
-COMMENT ON COLUMN SUPPLIERS.RATING IS 'Supplier performance rating (1.0 to 5.0 scale)';
-`,
- description: "Modify existing comments and remove comments using empty string",
- },
- {
- name: "comments_with_special_characters",
- initialSchema: `
-CREATE TABLE SPECIAL_DATA (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- DATA_VALUE VARCHAR2(500),
- METADATA_JSON CLOB,
- UNICODE_TEXT NVARCHAR2(200),
- BINARY_DATA BLOB,
- CATEGORY VARCHAR2(100),
- STATUS VARCHAR2(50)
-);
-
-CREATE TABLE MULTILANG_CONTENT (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- TITLE_EN VARCHAR2(200),
- TITLE_ES VARCHAR2(200),
- TITLE_ZH NVARCHAR2(200),
- CONTENT_TEXT CLOB,
- TAGS VARCHAR2(1000)
-);
-`,
- migrationDDL: `
--- Comments with single quotes and escaping
-COMMENT ON TABLE SPECIAL_DATA IS 'Table containing "special" data with various formats & types';
-COMMENT ON COLUMN SPECIAL_DATA.DATA_VALUE IS 'Data value field - supports NULL, empty strings, and ''quoted'' content';
-
--- Comments with double quotes
-COMMENT ON COLUMN SPECIAL_DATA.METADATA_JSON IS 'JSON metadata: {"key": "value", "array": [1,2,3]}';
-
--- Multi-line comments with line breaks and special characters
-COMMENT ON COLUMN SPECIAL_DATA.UNICODE_TEXT IS 'Unicode text support:
-Line 1: Basic ASCII text
-Line 2: Special chars !@#$%^&*()_+-={}[]|;:,.<>?
-Line 3: Math symbols ≤ ≥ ± × ÷ ∑ ∏ ∆ π
-Line 4: Currencies $ € £ ¥ ₹';
-
--- Comments with backslashes and escape sequences
-COMMENT ON COLUMN SPECIAL_DATA.BINARY_DATA IS 'Binary data storage\nSupports: images, documents, archives\tTab-separated info\r\nWindows line endings';
-
--- Comments with percent signs and wildcards
-COMMENT ON COLUMN SPECIAL_DATA.CATEGORY IS 'Category field: use % for wildcard searches, _ for single char matching';
-
--- Comments with SQL injection-like patterns (should be safely handled)
-COMMENT ON COLUMN SPECIAL_DATA.STATUS IS 'Status field - values: ''ACTIVE'', ''INACTIVE'', ''PENDING''; DROP TABLE users; --';
-
--- Unicode comments with international characters
-COMMENT ON TABLE MULTILANG_CONTENT IS 'Multilingual content: English, Español, 中文, العربية, Русский, 日本語';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_EN IS 'English title field';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ES IS 'Título en español';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ZH IS '中文标题字段 - 支持简体和繁体中文';
-
--- Long comment with mixed content
-COMMENT ON COLUMN MULTILANG_CONTENT.CONTENT_TEXT IS 'Content field supporting:
-1. HTML tags: <b>bold</b>, <i>italic</i>, <a href="http://example.com">links</a>
-2. Markdown: **bold**, *italic*, [link](url), backtick-code-backtick
-3. Special symbols: © ® ™ § ¶ † ‡ • … ‰ ′ ″
-4. Emojis: 😀 😃 😄 😁 🚀 ⭐ 🎉 💯
-5. Mathematical: ∀x∈ℝ: x² ≥ 0, ∑(i=1 to n) i = n(n+1)/2';
-
--- Comment with XML/HTML content
-COMMENT ON COLUMN MULTILANG_CONTENT.TAGS IS 'XML tags: <tag type="category" value="news"/><tag type="priority" value="high"/>End of XML';
-
--- Create additional table to test more complex scenarios
-CREATE TABLE COMPLEX_TYPES (
- ID NUMBER PRIMARY KEY,
- XML_DATA XMLTYPE,
- TIMESTAMP_TZ TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP,
- INTERVAL_DS INTERVAL DAY TO SECOND,
- RAW_DATA RAW(100),
- LONG_RAW_DATA LONG RAW
-);
-
--- Comments with technical specifications
-COMMENT ON TABLE COMPLEX_TYPES IS 'Advanced data types testing table
-Supports: XMLType, Timestamps with timezone, Intervals, RAW data
-Performance: Optimized for OLTP workloads @ 10K+ TPS
-Security: Row-level security enabled, audit trails maintained';
-
-COMMENT ON COLUMN COMPLEX_TYPES.XML_DATA IS 'XMLType column for structured XML storage
-Example: <?xml version="1.0" encoding="UTF-8"?>
-<root><item id="1" name="test">Content</item></root>';
-
-COMMENT ON COLUMN COMPLEX_TYPES.TIMESTAMP_TZ IS 'Timestamp with timezone: YYYY-MM-DD HH24:MI:SS.FF TZR
-Default: SYSTIMESTAMP (system timestamp with session timezone)';
-
-COMMENT ON COLUMN COMPLEX_TYPES.INTERVAL_DS IS 'Interval Day to Second: +/-DD HH:MI:SS.FFFFFF
-Range: +/-999999999 days, precision up to nanoseconds';
-`,
- description: "Test comments with special characters, quotes, Unicode, multi-line text, and complex content",
- },
- {
- name: "reverse_table_and_column_comments",
- initialSchema: `
-CREATE TABLE PRODUCTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- PRICE NUMBER(10, 2) NOT NULL,
- DESCRIPTION CLOB,
- CATEGORY VARCHAR2(50),
- CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE CUSTOMERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- EMAIL VARCHAR2(100),
- PHONE VARCHAR2(20),
- ADDRESS CLOB
-);
-
-CREATE TABLE ORDERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- CUSTOMER_ID NUMBER NOT NULL,
- PRODUCT_ID NUMBER NOT NULL,
- ORDER_DATE DATE DEFAULT SYSDATE,
- QUANTITY NUMBER NOT NULL,
- TOTAL_AMOUNT NUMBER(12, 2),
- STATUS VARCHAR2(20) DEFAULT 'PENDING',
- CONSTRAINT FK_ORDER_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID),
- CONSTRAINT FK_ORDER_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
-);
-
-CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
-CREATE UNIQUE INDEX IDX_CUSTOMERS_EMAIL ON CUSTOMERS(EMAIL);
-
--- Add all comments
-COMMENT ON TABLE PRODUCTS IS 'Product catalog table containing all product information';
-COMMENT ON TABLE CUSTOMERS IS 'Customer information and contact details';
-COMMENT ON COLUMN PRODUCTS.ID IS 'Unique product identifier (auto-generated)';
-COMMENT ON COLUMN PRODUCTS.NAME IS 'Product name - must be unique and descriptive';
-COMMENT ON COLUMN PRODUCTS.PRICE IS 'Product price in USD with 2 decimal places';
-COMMENT ON COLUMN PRODUCTS.DESCRIPTION IS 'Detailed product description with HTML formatting support';
-COMMENT ON COLUMN PRODUCTS.CATEGORY IS 'Product category for filtering and organization';
-COMMENT ON COLUMN PRODUCTS.CREATED_AT IS 'Timestamp when the product was added to catalog';
-COMMENT ON COLUMN CUSTOMERS.ID IS 'Primary key for customer records';
-COMMENT ON COLUMN CUSTOMERS.NAME IS 'Full name of the customer';
-COMMENT ON COLUMN CUSTOMERS.EMAIL IS 'Customer email address (unique)';
-COMMENT ON COLUMN CUSTOMERS.PHONE IS 'Contact phone number';
-COMMENT ON COLUMN CUSTOMERS.ADDRESS IS 'Full mailing address with multiple lines';
-COMMENT ON TABLE ORDERS IS 'Customer orders linking products and customers';
-COMMENT ON COLUMN ORDERS.ID IS 'Unique order identifier';
-COMMENT ON COLUMN ORDERS.CUSTOMER_ID IS 'Reference to customer who placed the order';
-COMMENT ON COLUMN ORDERS.PRODUCT_ID IS 'Reference to ordered product';
-COMMENT ON COLUMN ORDERS.ORDER_DATE IS 'Date when the order was placed';
-COMMENT ON COLUMN ORDERS.QUANTITY IS 'Number of items ordered';
-COMMENT ON COLUMN ORDERS.TOTAL_AMOUNT IS 'Total order amount including taxes';
-COMMENT ON COLUMN ORDERS.STATUS IS 'Current order status (PENDING, SHIPPED, DELIVERED, CANCELLED)';
-`,
- migrationDDL: `
--- Remove all comments
-COMMENT ON COLUMN ORDERS.STATUS IS '';
-COMMENT ON COLUMN ORDERS.TOTAL_AMOUNT IS '';
-COMMENT ON COLUMN ORDERS.QUANTITY IS '';
-COMMENT ON COLUMN ORDERS.ORDER_DATE IS '';
-COMMENT ON COLUMN ORDERS.PRODUCT_ID IS '';
-COMMENT ON COLUMN ORDERS.CUSTOMER_ID IS '';
-COMMENT ON COLUMN ORDERS.ID IS '';
-COMMENT ON TABLE ORDERS IS '';
-
-COMMENT ON COLUMN CUSTOMERS.ADDRESS IS '';
-COMMENT ON COLUMN CUSTOMERS.PHONE IS '';
-COMMENT ON COLUMN CUSTOMERS.EMAIL IS '';
-COMMENT ON COLUMN CUSTOMERS.NAME IS '';
-COMMENT ON COLUMN CUSTOMERS.ID IS '';
-
-COMMENT ON COLUMN PRODUCTS.CREATED_AT IS '';
-COMMENT ON COLUMN PRODUCTS.CATEGORY IS '';
-COMMENT ON COLUMN PRODUCTS.DESCRIPTION IS '';
-COMMENT ON COLUMN PRODUCTS.PRICE IS '';
-COMMENT ON COLUMN PRODUCTS.NAME IS '';
-COMMENT ON COLUMN PRODUCTS.ID IS '';
-
-COMMENT ON TABLE CUSTOMERS IS '';
-COMMENT ON TABLE PRODUCTS IS '';
-
--- Drop the ORDERS table
-DROP TABLE ORDERS CASCADE CONSTRAINTS;
-`,
- description: "Reverse: Remove all comments and drop a table",
- },
- {
- name: "reverse_modify_and_drop_comments",
- initialSchema: `
-CREATE TABLE INVENTORY (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- PRODUCT_CODE VARCHAR2(50) NOT NULL,
- STOCK_LEVEL NUMBER NOT NULL,
- MIN_STOCK NUMBER DEFAULT 10,
- MAX_STOCK NUMBER DEFAULT 1000,
- WAREHOUSE_ID NUMBER,
- LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- REORDER_POINT NUMBER DEFAULT 0
-);
-
-CREATE TABLE SUPPLIERS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- CONTACT_EMAIL VARCHAR2(100),
- RATING NUMBER(2,1)
-);
-
--- Modified comments
-COMMENT ON TABLE INVENTORY IS 'Updated inventory management system with enhanced tracking capabilities';
-COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Updated: Standardized product SKU following new naming convention';
-COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Updated: Real-time stock quantity with automated updates';
-COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Updated: Dynamic minimum threshold based on sales velocity';
-COMMENT ON COLUMN INVENTORY.LAST_UPDATED IS 'Timestamp of last inventory update or modification';
-COMMENT ON COLUMN INVENTORY.REORDER_POINT IS 'Calculated reorder point based on lead time and demand';
-COMMENT ON COLUMN SUPPLIERS.RATING IS 'Supplier performance rating (1.0 to 5.0 scale)';
-`,
- migrationDDL: `
--- Drop column with comment
-ALTER TABLE INVENTORY DROP COLUMN REORDER_POINT;
-
--- Add comment to new column
-COMMENT ON COLUMN INVENTORY.WAREHOUSE_ID IS 'Warehouse location identifier';
-
--- Restore original comment
-COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Minimum stock threshold';
-
--- Remove other updated comments
-COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Current stock quantity';
-COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Product identification code';
-
--- Add comments that were removed
-COMMENT ON TABLE SUPPLIERS IS 'Supplier information table';
-COMMENT ON COLUMN SUPPLIERS.CONTACT_EMAIL IS 'Primary contact email';
-
--- Restore other columns that had comments removed
-COMMENT ON COLUMN INVENTORY.MAX_STOCK IS 'Maximum stock capacity';
-
--- Change table comment
-COMMENT ON TABLE INVENTORY IS 'Initial inventory tracking table';
-
--- Remove comments
-COMMENT ON COLUMN SUPPLIERS.RATING IS '';
-COMMENT ON COLUMN INVENTORY.LAST_UPDATED IS '';
-`,
- description: "Reverse: Restore original comments and modify state",
- },
- {
- name: "reverse_comments_with_special_characters",
- initialSchema: `
-CREATE TABLE SPECIAL_DATA (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- DATA_VALUE VARCHAR2(500),
- METADATA_JSON CLOB,
- UNICODE_TEXT NVARCHAR2(200),
- BINARY_DATA BLOB,
- CATEGORY VARCHAR2(100),
- STATUS VARCHAR2(50)
-);
-
-CREATE TABLE MULTILANG_CONTENT (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- TITLE_EN VARCHAR2(200),
- TITLE_ES VARCHAR2(200),
- TITLE_ZH NVARCHAR2(200),
- CONTENT_TEXT CLOB,
- TAGS VARCHAR2(1000)
-);
-
-CREATE TABLE COMPLEX_TYPES (
- ID NUMBER PRIMARY KEY,
- XML_DATA XMLTYPE,
- TIMESTAMP_TZ TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP,
- INTERVAL_DS INTERVAL DAY TO SECOND,
- RAW_DATA RAW(100),
- LONG_RAW_DATA LONG RAW
-);
-
--- All comments added
-COMMENT ON TABLE SPECIAL_DATA IS 'Table containing "special" data with various formats & types';
-COMMENT ON COLUMN SPECIAL_DATA.DATA_VALUE IS 'Data value field - supports NULL, empty strings, and ''quoted'' content';
-COMMENT ON COLUMN SPECIAL_DATA.METADATA_JSON IS 'JSON metadata: {"key": "value", "array": [1,2,3]}';
-COMMENT ON COLUMN SPECIAL_DATA.UNICODE_TEXT IS 'Unicode text support:
-Line 1: Basic ASCII text
-Line 2: Special chars !@#$%^&*()_+-={}[]|;:,.<>?
-Line 3: Math symbols ≤ ≥ ± × ÷ ∑ ∏ ∆ π
-Line 4: Currencies $ € £ ¥ ₹';
-COMMENT ON COLUMN SPECIAL_DATA.BINARY_DATA IS 'Binary data storage\nSupports: images, documents, archives\tTab-separated info\r\nWindows line endings';
-COMMENT ON COLUMN SPECIAL_DATA.CATEGORY IS 'Category field: use % for wildcard searches, _ for single char matching';
-COMMENT ON COLUMN SPECIAL_DATA.STATUS IS 'Status field - values: ''ACTIVE'', ''INACTIVE'', ''PENDING''; DROP TABLE users; --';
-COMMENT ON TABLE MULTILANG_CONTENT IS 'Multilingual content: English, Español, 中文, العربية, Русский, 日本語';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_EN IS 'English title field';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ES IS 'Título en español';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ZH IS '中文标题字段 - 支持简体和繁体中文';
-COMMENT ON COLUMN MULTILANG_CONTENT.CONTENT_TEXT IS 'Content field supporting:
-1. HTML tags: <b>bold</b>, <i>italic</i>, <a href="http://example.com">links</a>
-2. Markdown: **bold**, *italic*, [link](url), backtick-code-backtick
-3. Special symbols: © ® ™ § ¶ † ‡ • … ‰ ′ ″
-4. Emojis: 😀 😃 😄 😁 🚀 ⭐ 🎉 💯
-5. Mathematical: ∀x∈ℝ: x² ≥ 0, ∑(i=1 to n) i = n(n+1)/2';
-COMMENT ON COLUMN MULTILANG_CONTENT.TAGS IS 'XML tags: <tag type="category" value="news"/><tag type="priority" value="high"/>End of XML';
-COMMENT ON TABLE COMPLEX_TYPES IS 'Advanced data types testing table
-Supports: XMLType, Timestamps with timezone, Intervals, RAW data
-Performance: Optimized for OLTP workloads @ 10K+ TPS
-Security: Row-level security enabled, audit trails maintained';
-COMMENT ON COLUMN COMPLEX_TYPES.XML_DATA IS 'XMLType column for structured XML storage
-Example: <?xml version="1.0" encoding="UTF-8"?>
-<root><item id="1" name="test">Content</item></root>';
-COMMENT ON COLUMN COMPLEX_TYPES.TIMESTAMP_TZ IS 'Timestamp with timezone: YYYY-MM-DD HH24:MI:SS.FF TZR
-Default: SYSTIMESTAMP (system timestamp with session timezone)';
-COMMENT ON COLUMN COMPLEX_TYPES.INTERVAL_DS IS 'Interval Day to Second: +/-DD HH:MI:SS.FFFFFF
-Range: +/-999999999 days, precision up to nanoseconds';
-`,
- migrationDDL: `
--- Drop table with all its comments
-DROP TABLE COMPLEX_TYPES CASCADE CONSTRAINTS;
-
--- Remove all comments from remaining tables
-COMMENT ON COLUMN MULTILANG_CONTENT.TAGS IS '';
-COMMENT ON COLUMN MULTILANG_CONTENT.CONTENT_TEXT IS '';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ZH IS '';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ES IS '';
-COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_EN IS '';
-COMMENT ON TABLE MULTILANG_CONTENT IS '';
-
-COMMENT ON COLUMN SPECIAL_DATA.STATUS IS '';
-COMMENT ON COLUMN SPECIAL_DATA.CATEGORY IS '';
-COMMENT ON COLUMN SPECIAL_DATA.BINARY_DATA IS '';
-COMMENT ON COLUMN SPECIAL_DATA.UNICODE_TEXT IS '';
-COMMENT ON COLUMN SPECIAL_DATA.METADATA_JSON IS '';
-COMMENT ON COLUMN SPECIAL_DATA.DATA_VALUE IS '';
-COMMENT ON TABLE SPECIAL_DATA IS '';
-`,
- description: "Reverse: Remove all comments with special characters",
- },
- }
-}
diff --git a/backend/plugin/schema/oracle/generate_migration_fixture_test.go b/backend/plugin/schema/oracle/generate_migration_fixture_test.go
deleted file mode 100644
index ba84e45a3d..0000000000
--- a/backend/plugin/schema/oracle/generate_migration_fixture_test.go
+++ /dev/null
@@ -1,50 +0,0 @@
-package oracle
-
-import (
- "encoding/json"
- "os"
- "path/filepath"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-)
-
-const generateMigrationFixtureDir = "testdata/generate_migration"
-
-// generateMigrationFixture is one case's pair of engine-reported schemas: the
-// state after initialSchema, and the state after migrationDDL. Both are
-// protojson because storepb messages do not round-trip through encoding/json.
-type generateMigrationFixture struct {
- SchemaA json.RawMessage `json:"schemaA"`
- SchemaB json.RawMessage `json:"schemaB"`
-}
-
-func generateMigrationFixturePath(name string) string {
- return filepath.Join(generateMigrationFixtureDir, name+".json")
-}
-
-func generateMigrationGoldenPath(name string) string {
- return filepath.Join(generateMigrationFixtureDir, name+".sql")
-}
-
-// loadGenerateMigrationFixture reads the metadata Oracle reported for a case.
-func loadGenerateMigrationFixture(t *testing.T, name string) (*storepb.DatabaseSchemaMetadata, *storepb.DatabaseSchemaMetadata) {
- t.Helper()
-
- path := generateMigrationFixturePath(name)
- raw, err := os.ReadFile(path)
- require.NoErrorf(t, err, "missing fixture %s; regenerate with: go test -tags oracle_record -run TestRecordGenerateMigrationFixtures ./backend/plugin/schema/oracle/", path)
-
- var fixture generateMigrationFixture
- require.NoError(t, json.Unmarshal(raw, &fixture), "failed to read %s", path)
-
- schemaA := &storepb.DatabaseSchemaMetadata{}
- require.NoError(t, common.ProtojsonUnmarshaler.Unmarshal(fixture.SchemaA, schemaA), "failed to decode schemaA in %s", path)
- schemaB := &storepb.DatabaseSchemaMetadata{}
- require.NoError(t, common.ProtojsonUnmarshaler.Unmarshal(fixture.SchemaB, schemaB), "failed to decode schemaB in %s", path)
-
- return schemaA, schemaB
-}
diff --git a/backend/plugin/schema/oracle/generate_migration_record_test.go b/backend/plugin/schema/oracle/generate_migration_record_test.go
deleted file mode 100644
index c337eafe4d..0000000000
--- a/backend/plugin/schema/oracle/generate_migration_record_test.go
+++ /dev/null
@@ -1,204 +0,0 @@
-//go:build oracle_record
-
-// This file is the provenance of testdata/generate_migration/*.json: it applies
-// each fixture's DDL to a real Oracle, verifies the generated rollback against
-// that Oracle, and records what the engine reports, so the golden test can run
-// the generator against true synced metadata without a container. Refresh the
-// fixtures with:
-//
-// go test -tags oracle_record -count=1 -timeout 40m \
-// ./backend/plugin/schema/oracle/ -run TestRecordGenerateMigrationFixtures
-//
-// The live round trip the golden test cannot do lives here: generate the
-// rollback, execute it, and require that it restores the initial schema. A
-// generator that emits invalid or incomplete DDL fails the re-record instead of
-// silently blessing a new golden.
-//
-// Recorded metadata is what the engine reported, minus two adjustments that
-// would otherwise churn every fixture on re-record: table stats are zeroed, and
-// Oracle-allocated identifiers are canonicalized (see canonicalizeGeneratedNames).
-
-package oracle
-
-import (
- "context"
- "encoding/json"
- "fmt"
- "os"
- "regexp"
- "strings"
- "testing"
-
- "github.com/google/go-cmp/cmp"
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
- "google.golang.org/protobuf/encoding/protojson"
- "google.golang.org/protobuf/testing/protocmp"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-// fixtureSchemaName replaces the throwaway Oracle user this recorder creates, so
-// a re-record does not churn every fixture with a new UUID-derived schema name.
-const fixtureSchemaName = "TESTSCHEMA"
-
-// oracleGeneratedName matches the identifiers Oracle allocates for itself:
-// identity-column sequences and the names it invents for unnamed constraints.
-// Both embed a database object number that depends on allocation order across
-// concurrently recording subtests, so the same schema recorded twice yields
-// different numbers.
-var oracleGeneratedName = regexp.MustCompile(`ISEQ\$\$_[0-9]+|SYS_C[0-9]+`)
-
-//nolint:tparallel
-func TestRecordGenerateMigrationFixtures(t *testing.T) {
- ctx := context.Background()
-
- container := testcontainer.SharedOracleContainer(t)
- systemDB := container.GetDB()
-
- require.NoError(t, os.MkdirAll(generateMigrationFixtureDir, 0o755))
-
- // Oracle Free slows down heavily when too many schema-heavy cases run
- // concurrently against one container.
- const maxConcurrent = 4
- gate := make(chan struct{}, maxConcurrent)
-
- for _, tc := range generateMigrationCases() {
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
- gate <- struct{}{}
- defer func() { <-gate }()
-
- testUser := fmt.Sprintf("U_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
- require.NoError(t, createOracleUser(systemDB, testUser))
-
- driver, err := createOracleDriver(ctx, container.GetHost(), container.GetPort(), testUser)
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- require.NoError(t, executeStatements(ctx, driver, tc.initialSchema), "failed to execute initial schema")
- schemaA, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- require.NoError(t, executeStatements(ctx, driver, tc.migrationDDL), "failed to execute migration DDL")
- schemaB, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Render the fixture before the round trip below mutates these
- // messages, but write it only once Oracle has accepted the rollback.
- fixture := renderFixture(t, schemaA, schemaB, testUser)
-
- verifyRollbackAgainstOracle(ctx, t, driver, schemaA, schemaB)
-
- out, err := json.MarshalIndent(fixture, "", " ")
- require.NoError(t, err)
- require.NoError(t, os.WriteFile(generateMigrationFixturePath(tc.name), append(out, '\n'), 0o644))
- })
- }
-}
-
-// verifyRollbackAgainstOracle is the live check the offline golden test cannot
-// make: generate the rollback from B back to A, execute it, and require that the
-// engine ends up where it started.
-func verifyRollbackAgainstOracle(ctx context.Context, t *testing.T, driver db.Driver, schemaA, schemaB *storepb.DatabaseSchemaMetadata) {
- t.Helper()
-
- dbSchemaA := model.NewDatabaseMetadata(schemaA, nil, nil, storepb.Engine_ORACLE, false)
- dbSchemaB := model.NewDatabaseMetadata(schemaB, nil, nil, storepb.Engine_ORACLE, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_ORACLE, dbSchemaB, dbSchemaA)
- require.NoError(t, err)
-
- rollbackDDL, err := schema.GenerateMigration(storepb.Engine_ORACLE, diff)
- require.NoError(t, err)
-
- require.NoError(t, executeStatements(ctx, driver, rollbackDDL), "Oracle rejected the generated rollback:\n%s", rollbackDDL)
-
- schemaC, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- normalizeMetadataForComparison(schemaA)
- normalizeMetadataForComparison(schemaC)
- normalizeColumnPositions(schemaA)
- normalizeColumnPositions(schemaC)
-
- if mismatch := cmp.Diff(schemaA, schemaC, protocmp.Transform()); mismatch != "" {
- t.Errorf("rollback did not restore the initial schema (-want +got):\n%s\ngenerated DDL:\n%s", mismatch, rollbackDDL)
- }
-}
-
-// renderFixture marshals the pair for storage. Both schemas go through one
-// canonicalization pass so an identifier Oracle allocated once carries the same
-// stable name on both sides.
-func renderFixture(t *testing.T, schemaA, schemaB *storepb.DatabaseSchemaMetadata, testUser string) generateMigrationFixture {
- t.Helper()
-
- rawA := marshalSchema(t, schemaA, testUser)
- rawB := marshalSchema(t, schemaB, testUser)
-
- canonical := canonicalizeGeneratedNames(rawA, rawB)
- return generateMigrationFixture{
- SchemaA: json.RawMessage(canonical[0]),
- SchemaB: json.RawMessage(canonical[1]),
- }
-}
-
-// marshalSchema renders one synced schema, substituting the throwaway user name.
-func marshalSchema(t *testing.T, metadata *storepb.DatabaseSchemaMetadata, testUser string) string {
- t.Helper()
-
- zeroVolatileStats(metadata)
-
- raw, err := protojson.MarshalOptions{Multiline: true, Indent: " "}.Marshal(metadata)
- require.NoError(t, err)
-
- // The user name reaches the metadata as the database name and inside view,
- // routine and constraint text, so substitute across the whole document.
- return strings.ReplaceAll(string(raw), strings.ToUpper(testUser), fixtureSchemaName)
-}
-
-// canonicalizeGeneratedNames renumbers Oracle-allocated identifiers by order of
-// first appearance across the documents, so re-recording an unchanged schema
-// produces an unchanged fixture. Prefixes are preserved because the generator
-// keys on them: get_database_definition.go and generate_migration.go both test
-// for "ISEQ$$_" to recognize identity-column sequences.
-func canonicalizeGeneratedNames(docs ...string) []string {
- canonical := make(map[string]string)
- seen := make(map[string]int)
-
- rename := func(match string) string {
- if replacement, ok := canonical[match]; ok {
- return replacement
- }
- prefix := "SYS_C"
- if strings.HasPrefix(match, "ISEQ") {
- prefix = "ISEQ$$_"
- }
- seen[prefix]++
- replacement := fmt.Sprintf("%s%07d", prefix, seen[prefix])
- canonical[match] = replacement
- return replacement
- }
-
- out := make([]string, 0, len(docs))
- for _, doc := range docs {
- out = append(out, oracleGeneratedName.ReplaceAllStringFunc(doc, rename))
- }
- return out
-}
-
-// zeroVolatileStats clears the row and byte counts, which differ run to run and
-// are never read by the migration generator.
-func zeroVolatileStats(metadata *storepb.DatabaseSchemaMetadata) {
- for _, schemaMetadata := range metadata.Schemas {
- for _, table := range schemaMetadata.Tables {
- table.DataSize = 0
- table.IndexSize = 0
- table.RowCount = 0
- }
- }
-}
diff --git a/backend/plugin/schema/oracle/generate_migration_test.go b/backend/plugin/schema/oracle/generate_migration_test.go
index 739c385460..2220c5f8c8 100644
--- a/backend/plugin/schema/oracle/generate_migration_test.go
+++ b/backend/plugin/schema/oracle/generate_migration_test.go
@@ -1,12 +1,26 @@
package oracle
import (
+ "context"
+ "encoding/json"
+ "fmt"
"os"
+ "path/filepath"
+ "regexp"
+ "strings"
"testing"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/uuid"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/testing/protocmp"
+ "github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/schema"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -14,10 +28,10 @@ import (
// TestGenerateMigration runs the rollback generator over metadata Oracle itself
// reported for each fixture: diff the post-migration schema back to the initial
// one and compare the DDL against the recorded golden. The metadata comes from
-// testdata/generate_migration/*.json, captured by TestRecordGenerateMigrationFixtures.
+// testdata/generate_migration/*.json, captured by TestGenerateMigrationAgainstOracle.
//
// Set record to regenerate the .sql goldens; it needs no Oracle. Refreshing the
-// .json metadata does, and is the recorder's job.
+// .json metadata does, and is TestGenerateMigrationAgainstOracle's job.
func TestGenerateMigration(t *testing.T) {
t.Parallel()
@@ -51,3 +65,2281 @@ func TestGenerateMigration(t *testing.T) {
})
}
}
+
+const generateMigrationFixtureDir = "testdata/generate_migration"
+
+// generateMigrationFixture is one case's pair of engine-reported schemas: the
+// state after initialSchema, and the state after migrationDDL. Both are
+// protojson because storepb messages do not round-trip through encoding/json.
+type generateMigrationFixture struct {
+ SchemaA json.RawMessage `json:"schemaA"`
+ SchemaB json.RawMessage `json:"schemaB"`
+}
+
+func generateMigrationFixturePath(name string) string {
+ return filepath.Join(generateMigrationFixtureDir, name+".json")
+}
+
+func generateMigrationGoldenPath(name string) string {
+ return filepath.Join(generateMigrationFixtureDir, name+".sql")
+}
+
+// loadGenerateMigrationFixture reads the metadata Oracle reported for a case.
+func loadGenerateMigrationFixture(t *testing.T, name string) (*storepb.DatabaseSchemaMetadata, *storepb.DatabaseSchemaMetadata) {
+ t.Helper()
+
+ path := generateMigrationFixturePath(name)
+ raw, err := os.ReadFile(path)
+ require.NoErrorf(t, err, "missing fixture %s; regenerate by setting record = true in TestGenerateMigrationAgainstOracle, then running: go test -run TestGenerateMigrationAgainstOracle ./backend/plugin/schema/oracle/", path)
+
+ var fixture generateMigrationFixture
+ require.NoError(t, json.Unmarshal(raw, &fixture), "failed to read %s", path)
+
+ schemaA := &storepb.DatabaseSchemaMetadata{}
+ require.NoError(t, common.ProtojsonUnmarshaler.Unmarshal(fixture.SchemaA, schemaA), "failed to decode schemaA in %s", path)
+ schemaB := &storepb.DatabaseSchemaMetadata{}
+ require.NoError(t, common.ProtojsonUnmarshaler.Unmarshal(fixture.SchemaB, schemaB), "failed to decode schemaB in %s", path)
+
+ return schemaA, schemaB
+}
+
+// generateMigrationCase is one schema-change fixture: apply initialSchema to an
+// empty schema, then apply migrationDDL, and the generator must produce the DDL
+// that takes the second state back to the first.
+type generateMigrationCase struct {
+ name string
+ initialSchema string
+ migrationDDL string
+ description string
+}
+
+// generateMigrationCases returns the fixtures shared by the golden test and the
+// container-backed recorder that captures their metadata.
+func generateMigrationCases() []generateMigrationCase {
+ return []generateMigrationCase{
+ {
+ name: "create_tables_with_fk",
+ initialSchema: `
+CREATE TABLE USERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USERNAME VARCHAR2(50) NOT NULL,
+ EMAIL VARCHAR2(100) NOT NULL,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE UNIQUE INDEX UK_EMAIL ON USERS(EMAIL);
+CREATE INDEX IDX_USERNAME ON USERS(USERNAME);
+
+CREATE TABLE POSTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USER_ID NUMBER NOT NULL,
+ TITLE VARCHAR2(200) NOT NULL,
+ CONTENT CLOB,
+ PUBLISHED_AT TIMESTAMP,
+ CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID) ON DELETE CASCADE
+);
+
+CREATE INDEX IDX_USER_ID ON POSTS(USER_ID);
+`,
+ migrationDDL: `
+DROP TABLE POSTS CASCADE CONSTRAINTS;
+DROP TABLE USERS CASCADE CONSTRAINTS;`,
+ description: "Create tables with foreign key constraints",
+ },
+ {
+ name: "basic_table_operations",
+ initialSchema: `
+CREATE TABLE USERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USERNAME VARCHAR2(50) NOT NULL,
+ EMAIL VARCHAR2(100) NOT NULL,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE POSTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USER_ID NUMBER NOT NULL,
+ TITLE VARCHAR2(200) NOT NULL,
+ CONTENT CLOB,
+ CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID)
+);
+
+CREATE INDEX IDX_POSTS_USER_ID ON POSTS(USER_ID);
+`,
+ migrationDDL: `
+-- Add new column
+ALTER TABLE USERS ADD IS_ACTIVE NUMBER(1) DEFAULT 1;
+
+-- Create new table
+CREATE TABLE COMMENTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ POST_ID NUMBER NOT NULL,
+ USER_ID NUMBER NOT NULL,
+ CONTENT CLOB NOT NULL,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT FK_POST FOREIGN KEY (POST_ID) REFERENCES POSTS(ID),
+ CONSTRAINT FK_COMMENT_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID)
+);
+
+-- Add new index
+CREATE INDEX IDX_USERS_EMAIL ON USERS(EMAIL);
+
+-- Add check constraint
+ALTER TABLE POSTS ADD CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) > 0);
+`,
+ description: "Basic table operations with columns, constraints, and indexes",
+ },
+ {
+ name: "views_and_functions",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PRICE NUMBER(10, 2) NOT NULL,
+ STOCK NUMBER DEFAULT 0
+);
+
+CREATE TABLE ORDERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ PRODUCT_ID NUMBER NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ TOTAL NUMBER(10, 2),
+ CONSTRAINT FK_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
+);
+`,
+ migrationDDL: `
+-- Create view
+CREATE VIEW PRODUCT_INVENTORY AS
+SELECT
+ P.ID,
+ P.NAME,
+ P.PRICE,
+ P.STOCK,
+ NVL(SUM(O.QUANTITY), 0) AS TOTAL_ORDERED
+FROM PRODUCTS P
+LEFT JOIN ORDERS O ON P.ID = O.PRODUCT_ID
+GROUP BY P.ID, P.NAME, P.PRICE, P.STOCK;
+
+-- Create function
+CREATE OR REPLACE FUNCTION CALCULATE_ORDER_TOTAL(P_PRODUCT_ID NUMBER, P_QUANTITY NUMBER)
+RETURN NUMBER
+IS
+ V_PRICE NUMBER(10, 2);
+BEGIN
+ SELECT PRICE INTO V_PRICE FROM PRODUCTS WHERE ID = P_PRODUCT_ID;
+ RETURN P_QUANTITY * V_PRICE;
+EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ RETURN 0;
+END;
+/
+
+-- Create materialized view
+CREATE MATERIALIZED VIEW PRODUCT_STATS AS
+SELECT
+ PRODUCT_ID,
+ COUNT(*) AS ORDER_COUNT,
+ SUM(QUANTITY) AS TOTAL_QUANTITY,
+ SUM(TOTAL) AS TOTAL_REVENUE
+FROM ORDERS
+GROUP BY PRODUCT_ID;
+
+-- Create index on materialized view
+CREATE INDEX IDX_PRODUCT_STATS_REVENUE ON PRODUCT_STATS(TOTAL_REVENUE DESC);
+`,
+ description: "Views, functions, and materialized views",
+ },
+ {
+ name: "sequences",
+ initialSchema: `
+CREATE TABLE ITEMS (
+ ID NUMBER PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL
+);
+`,
+ migrationDDL: `
+-- Create sequence
+CREATE SEQUENCE ORDER_SEQ START WITH 1000 INCREMENT BY 10;
+
+-- Create table using sequence
+CREATE TABLE SALES_ORDERS (
+ ID NUMBER DEFAULT ORDER_SEQ.NEXTVAL PRIMARY KEY,
+ ITEM_ID NUMBER NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ CONSTRAINT FK_ITEM FOREIGN KEY (ITEM_ID) REFERENCES ITEMS(ID)
+);
+`,
+ description: "Sequences and their usage in tables",
+ },
+ {
+ name: "drop_indexes_and_constraints",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ CATEGORY VARCHAR2(50),
+ PRICE NUMBER(10, 2),
+ SUPPLIER_ID NUMBER
+);
+
+CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
+CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
+CREATE UNIQUE INDEX IDX_PRODUCTS_NAME ON PRODUCTS(NAME);
+
+ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
+ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_NAME_LENGTH CHECK (LENGTH(NAME) >= 3);
+`,
+ migrationDDL: `
+-- Drop indexes and constraints
+DROP INDEX IDX_PRODUCTS_CATEGORY;
+DROP INDEX IDX_PRODUCTS_PRICE;
+DROP INDEX IDX_PRODUCTS_NAME;
+ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_PRICE_POSITIVE;
+ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_NAME_LENGTH;
+`,
+ description: "Drop indexes and constraints from tables",
+ },
+ {
+ name: "drop_views_and_functions",
+ initialSchema: `
+CREATE TABLE SALES (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ PRODUCT_NAME VARCHAR2(100) NOT NULL,
+ SALE_AMOUNT NUMBER(10, 2) NOT NULL,
+ SALE_DATE DATE NOT NULL
+);
+
+CREATE VIEW MONTHLY_SALES AS
+SELECT
+ EXTRACT(YEAR FROM SALE_DATE) AS YEAR_VAL,
+ EXTRACT(MONTH FROM SALE_DATE) AS MONTH_VAL,
+ SUM(SALE_AMOUNT) AS TOTAL_SALES
+FROM SALES
+GROUP BY EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE);
+
+CREATE MATERIALIZED VIEW TOP_PRODUCTS AS
+SELECT
+ PRODUCT_NAME,
+ COUNT(*) AS SALE_COUNT,
+ SUM(SALE_AMOUNT) AS TOTAL_REVENUE
+FROM SALES
+GROUP BY PRODUCT_NAME
+ORDER BY SUM(SALE_AMOUNT) DESC;
+
+CREATE OR REPLACE FUNCTION GET_MONTHLY_TOTAL(YEAR_PARAM NUMBER, MONTH_PARAM NUMBER)
+RETURN NUMBER
+IS
+ V_TOTAL NUMBER;
+BEGIN
+ SELECT NVL(SUM(SALE_AMOUNT), 0) INTO V_TOTAL
+ FROM SALES
+ WHERE EXTRACT(YEAR FROM SALE_DATE) = YEAR_PARAM
+ AND EXTRACT(MONTH FROM SALE_DATE) = MONTH_PARAM;
+
+ RETURN V_TOTAL;
+END;
+/
+
+CREATE OR REPLACE FUNCTION CALCULATE_DISCOUNT(AMOUNT NUMBER)
+RETURN NUMBER
+IS
+BEGIN
+ RETURN AMOUNT * 0.1;
+END;
+/
+`,
+ migrationDDL: `
+-- Drop views and functions
+DROP MATERIALIZED VIEW TOP_PRODUCTS;
+DROP VIEW MONTHLY_SALES;
+DROP FUNCTION GET_MONTHLY_TOTAL;
+DROP FUNCTION CALCULATE_DISCOUNT;
+`,
+ description: "Drop views and functions that depend on tables",
+ },
+ {
+ name: "alter_table_columns",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(50) NOT NULL,
+ PRICE NUMBER(8, 2) NOT NULL,
+ DESCRIPTION CLOB,
+ CATEGORY VARCHAR2(30),
+ IS_ACTIVE NUMBER(1) DEFAULT 1
+);
+
+CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
+CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
+`,
+ migrationDDL: `
+-- Alter table operations
+ALTER TABLE PRODUCTS ADD CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
+ALTER TABLE PRODUCTS ADD STOCK_QUANTITY NUMBER DEFAULT 0;
+ALTER TABLE PRODUCTS ADD WEIGHT NUMBER(5, 2);
+
+-- Change column types and constraints
+ALTER TABLE PRODUCTS MODIFY NAME VARCHAR2(100);
+ALTER TABLE PRODUCTS MODIFY PRICE NUMBER(10, 2);
+ALTER TABLE PRODUCTS MODIFY DESCRIPTION NOT NULL;
+
+-- Add constraints
+ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
+ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_STOCK_NON_NEGATIVE CHECK (STOCK_QUANTITY >= 0);
+
+-- Add new index
+CREATE INDEX IDX_PRODUCTS_CREATED_AT ON PRODUCTS(CREATED_AT);
+CREATE UNIQUE INDEX IDX_PRODUCTS_NAME_CATEGORY ON PRODUCTS(NAME, CATEGORY);
+`,
+ description: "Alter table with column additions, type changes, and constraints",
+ },
+ {
+ name: "drop_and_recreate_constraints",
+ initialSchema: `
+CREATE TABLE AUTHORS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ EMAIL VARCHAR2(100) UNIQUE
+);
+
+CREATE TABLE BOOKS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ TITLE VARCHAR2(200) NOT NULL,
+ AUTHOR_ID NUMBER NOT NULL,
+ ISBN VARCHAR2(20) UNIQUE,
+ PUBLISHED_YEAR NUMBER,
+ PRICE NUMBER(8, 2),
+ CONSTRAINT FK_AUTHOR FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID),
+ CONSTRAINT CHECK_YEAR_VALID CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2100),
+ CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0)
+);
+
+CREATE INDEX IDX_BOOKS_AUTHOR ON BOOKS(AUTHOR_ID);
+CREATE INDEX IDX_BOOKS_YEAR ON BOOKS(PUBLISHED_YEAR);
+`,
+ migrationDDL: `
+-- Drop and recreate foreign key with different options
+ALTER TABLE BOOKS DROP CONSTRAINT FK_AUTHOR;
+ALTER TABLE BOOKS ADD CONSTRAINT FK_AUTHOR_NEW FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID) ON DELETE CASCADE;
+
+-- Drop and modify check constraints
+ALTER TABLE BOOKS DROP CONSTRAINT CHECK_YEAR_VALID;
+ALTER TABLE BOOKS ADD CONSTRAINT CHECK_YEAR_EXTENDED CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2030);
+
+-- Add new constraints
+ALTER TABLE BOOKS ADD CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) >= 3);
+`,
+ description: "Drop and recreate constraints with different definitions",
+ },
+ {
+ name: "drop_sequence",
+ initialSchema: `
+CREATE SEQUENCE TRANSACTION_SEQ START WITH 1000;
+
+CREATE TABLE SIMPLE_LOG (
+ ID NUMBER DEFAULT TRANSACTION_SEQ.NEXTVAL PRIMARY KEY,
+ MESSAGE CLOB NOT NULL
+);
+`,
+ migrationDDL: `
+-- Drop table and sequence
+DROP TABLE SIMPLE_LOG;
+DROP SEQUENCE TRANSACTION_SEQ;
+`,
+ description: "Drop sequence and dependent table",
+ },
+ {
+ name: "complex_view_dependencies",
+ initialSchema: `
+CREATE TABLE DEPARTMENTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ MANAGER_ID NUMBER
+);
+
+CREATE TABLE EMPLOYEES (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ DEPARTMENT_ID NUMBER,
+ SALARY NUMBER(10, 2),
+ CONSTRAINT FK_DEPT FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENTS(ID)
+);
+
+-- Self-referencing foreign key
+ALTER TABLE DEPARTMENTS ADD CONSTRAINT FK_MANAGER FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES(ID);
+`,
+ migrationDDL: `
+-- Create base view
+CREATE VIEW DEPT_EMPLOYEE_COUNT AS
+SELECT D.ID AS DEPT_ID, D.NAME AS DEPT_NAME, COUNT(E.ID) AS EMP_COUNT
+FROM DEPARTMENTS D
+LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
+GROUP BY D.ID, D.NAME;
+
+-- Create dependent view
+CREATE VIEW DEPT_SUMMARY AS
+SELECT
+ DEC.DEPT_ID,
+ DEC.DEPT_NAME,
+ DEC.EMP_COUNT,
+ NVL(AVG(E.SALARY), 0) AS AVG_SALARY
+FROM DEPT_EMPLOYEE_COUNT DEC
+LEFT JOIN EMPLOYEES E ON DEC.DEPT_ID = E.DEPARTMENT_ID
+GROUP BY DEC.DEPT_ID, DEC.DEPT_NAME, DEC.EMP_COUNT;
+
+-- Create highly dependent view
+CREATE VIEW DEPT_MANAGER_SUMMARY AS
+SELECT
+ DS.DEPT_ID,
+ DS.DEPT_NAME,
+ DS.EMP_COUNT,
+ DS.AVG_SALARY,
+ M.NAME AS MANAGER_NAME
+FROM DEPT_SUMMARY DS
+JOIN DEPARTMENTS D ON DS.DEPT_ID = D.ID
+LEFT JOIN EMPLOYEES M ON D.MANAGER_ID = M.ID;
+
+-- Create materialized view with complex dependencies
+CREATE MATERIALIZED VIEW MV_DEPT_STATS AS
+SELECT
+ D.ID AS DEPT_ID,
+ D.NAME AS DEPT_NAME,
+ COUNT(E.ID) AS TOTAL_EMPLOYEES,
+ NVL(SUM(E.SALARY), 0) AS TOTAL_PAYROLL,
+ NVL(AVG(E.SALARY), 0) AS AVG_SALARY,
+ NVL(MAX(E.SALARY), 0) AS MAX_SALARY,
+ NVL(MIN(E.SALARY), 0) AS MIN_SALARY
+FROM DEPARTMENTS D
+LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
+GROUP BY D.ID, D.NAME;
+
+-- Create index on materialized view
+CREATE INDEX IDX_MV_DEPT_PAYROLL ON MV_DEPT_STATS(TOTAL_PAYROLL DESC);
+`,
+ description: "Complex view dependencies with multiple levels and materialized views",
+ },
+ {
+ name: "circular_foreign_key_dependencies",
+ initialSchema: `
+CREATE TABLE CUSTOMERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PREFERRED_ORDER_ID NUMBER
+);
+
+CREATE TABLE ORDERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ CUSTOMER_ID NUMBER NOT NULL,
+ ORDER_DATE DATE DEFAULT SYSDATE,
+ TOTAL_AMOUNT NUMBER(10, 2)
+);
+`,
+ migrationDDL: `
+-- Create circular foreign key dependencies
+ALTER TABLE CUSTOMERS ADD CONSTRAINT FK_PREFERRED_ORDER FOREIGN KEY (PREFERRED_ORDER_ID) REFERENCES ORDERS(ID);
+ALTER TABLE ORDERS ADD CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID);
+
+-- Add more tables with complex relationships
+CREATE TABLE ORDER_ITEMS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ ORDER_ID NUMBER NOT NULL,
+ PRODUCT_NAME VARCHAR2(100) NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ UNIT_PRICE NUMBER(10, 2) NOT NULL,
+ CONSTRAINT FK_ORDER_ITEM FOREIGN KEY (ORDER_ID) REFERENCES ORDERS(ID) ON DELETE CASCADE
+);
+
+-- Create trigger to update order total
+CREATE OR REPLACE TRIGGER TRG_UPDATE_ORDER_TOTAL
+AFTER INSERT OR UPDATE OR DELETE ON ORDER_ITEMS
+FOR EACH ROW
+DECLARE
+ V_ORDER_ID NUMBER;
+ V_TOTAL NUMBER(10, 2);
+BEGIN
+ -- Get order ID
+ IF INSERTING OR UPDATING THEN
+ V_ORDER_ID := :NEW.ORDER_ID;
+ ELSE
+ V_ORDER_ID := :OLD.ORDER_ID;
+ END IF;
+
+ -- Calculate new total
+ SELECT NVL(SUM(QUANTITY * UNIT_PRICE), 0) INTO V_TOTAL
+ FROM ORDER_ITEMS
+ WHERE ORDER_ID = V_ORDER_ID;
+
+ -- Update order total
+ UPDATE ORDERS SET TOTAL_AMOUNT = V_TOTAL WHERE ID = V_ORDER_ID;
+END;
+/
+`,
+ description: "Circular foreign key dependencies and triggers",
+ },
+ {
+ name: "advanced_indexes_and_constraints",
+ initialSchema: `
+CREATE TABLE SALES_DATA (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY,
+ SALE_DATE DATE NOT NULL,
+ CUSTOMER_ID NUMBER NOT NULL,
+ PRODUCT_ID NUMBER NOT NULL,
+ AMOUNT NUMBER(10, 2) NOT NULL,
+ REGION VARCHAR2(50) NOT NULL
+);
+`,
+ migrationDDL: `
+-- Create function-based index
+CREATE INDEX IDX_SALES_MONTH_YEAR ON SALES_DATA(EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE));
+
+-- Create composite index with mixed sort order
+CREATE INDEX IDX_SALES_REGION_AMOUNT ON SALES_DATA(REGION ASC, AMOUNT DESC);
+
+-- Create unique constraint on composite columns
+ALTER TABLE SALES_DATA ADD CONSTRAINT UK_SALES_DATE_CUSTOMER UNIQUE (SALE_DATE, CUSTOMER_ID, PRODUCT_ID);
+
+-- Create check constraint with complex expression
+ALTER TABLE SALES_DATA ADD CONSTRAINT CHK_AMOUNT_RANGE CHECK (AMOUNT > 0 AND AMOUNT <= 999999.99);
+
+-- Create conditional check constraint
+ALTER TABLE SALES_DATA ADD CONSTRAINT CHK_REGION_VALID CHECK (REGION IN ('NORTH', 'SOUTH', 'EAST', 'WEST', 'CENTRAL'));
+`,
+ description: "Advanced Oracle-specific indexes and complex constraints",
+ },
+ {
+ name: "stored_procedures_and_functions",
+ initialSchema: `
+CREATE TABLE AUDIT_LOG (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ TABLE_NAME VARCHAR2(100) NOT NULL,
+ OPERATION VARCHAR2(10) NOT NULL,
+ OLD_VALUES CLOB,
+ NEW_VALUES CLOB,
+ TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ USER_NAME VARCHAR2(100) DEFAULT USER
+);
+`,
+ migrationDDL: `
+-- Create stored procedure
+CREATE OR REPLACE PROCEDURE LOG_AUDIT(
+ P_TABLE_NAME VARCHAR2,
+ P_OPERATION VARCHAR2,
+ P_OLD_VALUES CLOB DEFAULT NULL,
+ P_NEW_VALUES CLOB DEFAULT NULL
+)
+IS
+BEGIN
+ INSERT INTO AUDIT_LOG (TABLE_NAME, OPERATION, OLD_VALUES, NEW_VALUES)
+ VALUES (P_TABLE_NAME, P_OPERATION, P_OLD_VALUES, P_NEW_VALUES);
+ COMMIT;
+EXCEPTION
+ WHEN OTHERS THEN
+ ROLLBACK;
+ RAISE;
+END;
+/
+
+-- Create function with complex logic
+CREATE OR REPLACE FUNCTION CALCULATE_BUSINESS_DAYS(
+ P_START_DATE DATE,
+ P_END_DATE DATE
+) RETURN NUMBER
+IS
+ V_DAYS NUMBER := 0;
+ V_CURRENT_DATE DATE := P_START_DATE;
+BEGIN
+ WHILE V_CURRENT_DATE <= P_END_DATE LOOP
+ -- Skip weekends (Saturday = 7, Sunday = 1)
+ IF TO_CHAR(V_CURRENT_DATE, 'D') NOT IN ('1', '7') THEN
+ V_DAYS := V_DAYS + 1;
+ END IF;
+ V_CURRENT_DATE := V_CURRENT_DATE + 1;
+ END LOOP;
+
+ RETURN V_DAYS;
+END;
+/
+
+-- Create another function for testing dependencies
+CREATE OR REPLACE FUNCTION GET_AUDIT_COUNT(P_TABLE_NAME VARCHAR2) RETURN NUMBER
+IS
+ V_COUNT NUMBER;
+BEGIN
+ SELECT COUNT(*) INTO V_COUNT
+ FROM AUDIT_LOG
+ WHERE TABLE_NAME = P_TABLE_NAME;
+
+ RETURN V_COUNT;
+END;
+/
+`,
+ description: "Stored procedures and functions with dependencies",
+ },
+ {
+ name: "edge_case_column_operations",
+ initialSchema: `
+CREATE TABLE TEST_COLUMNS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ COL_VARCHAR VARCHAR2(50),
+ COL_NUMBER NUMBER(10,2),
+ COL_DATE DATE,
+ COL_CLOB CLOB,
+ COL_NULLABLE VARCHAR2(100),
+ COL_WITH_DEFAULT VARCHAR2(50) DEFAULT 'DEFAULT_VALUE'
+);
+`,
+ migrationDDL: `
+-- Test various column modifications
+ALTER TABLE TEST_COLUMNS MODIFY COL_VARCHAR VARCHAR2(200);
+ALTER TABLE TEST_COLUMNS MODIFY COL_NUMBER NUMBER(15,3);
+ALTER TABLE TEST_COLUMNS MODIFY COL_NULLABLE NOT NULL;
+ALTER TABLE TEST_COLUMNS MODIFY COL_WITH_DEFAULT DEFAULT 'NEW_DEFAULT';
+
+-- Add columns with various data types and constraints
+ALTER TABLE TEST_COLUMNS ADD COL_TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
+ALTER TABLE TEST_COLUMNS ADD COL_BOOLEAN NUMBER(1) CHECK (COL_BOOLEAN IN (0,1));
+
+-- Drop some columns
+ALTER TABLE TEST_COLUMNS DROP COLUMN COL_CLOB;
+
+-- Add constraints
+ALTER TABLE TEST_COLUMNS ADD CONSTRAINT CHK_VARCHAR_LENGTH CHECK (LENGTH(COL_VARCHAR) >= 5);
+ALTER TABLE TEST_COLUMNS ADD CONSTRAINT CHK_NUMBER_POSITIVE CHECK (COL_NUMBER > 0);
+`,
+ description: "Edge cases for column operations and data type modifications",
+ },
+ {
+ name: "reverse_create_tables_with_fk",
+ initialSchema: "",
+ migrationDDL: `
+CREATE TABLE USERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USERNAME VARCHAR2(50) NOT NULL,
+ EMAIL VARCHAR2(100) NOT NULL,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE UNIQUE INDEX UK_EMAIL ON USERS(EMAIL);
+CREATE INDEX IDX_USERNAME ON USERS(USERNAME);
+
+CREATE TABLE POSTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USER_ID NUMBER NOT NULL,
+ TITLE VARCHAR2(200) NOT NULL,
+ CONTENT CLOB,
+ PUBLISHED_AT TIMESTAMP,
+ CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID) ON DELETE CASCADE
+);
+
+CREATE INDEX IDX_USER_ID ON POSTS(USER_ID);
+`,
+ description: "Reverse: Drop tables with foreign key constraints",
+ },
+ {
+ name: "reverse_basic_table_operations",
+ initialSchema: `
+CREATE TABLE USERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USERNAME VARCHAR2(50) NOT NULL,
+ EMAIL VARCHAR2(100) NOT NULL,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ IS_ACTIVE NUMBER(1) DEFAULT 1
+);
+
+CREATE TABLE POSTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ USER_ID NUMBER NOT NULL,
+ TITLE VARCHAR2(200) NOT NULL,
+ CONTENT CLOB,
+ CONSTRAINT FK_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID),
+ CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) > 0)
+);
+
+CREATE TABLE COMMENTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ POST_ID NUMBER NOT NULL,
+ USER_ID NUMBER NOT NULL,
+ CONTENT CLOB NOT NULL,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT FK_POST FOREIGN KEY (POST_ID) REFERENCES POSTS(ID),
+ CONSTRAINT FK_COMMENT_USER FOREIGN KEY (USER_ID) REFERENCES USERS(ID)
+);
+
+CREATE INDEX IDX_POSTS_USER_ID ON POSTS(USER_ID);
+CREATE INDEX IDX_USERS_EMAIL ON USERS(EMAIL);
+`,
+ migrationDDL: `
+-- Drop table
+DROP TABLE COMMENTS CASCADE CONSTRAINTS;
+
+-- Drop index
+DROP INDEX IDX_USERS_EMAIL;
+
+-- Drop check constraint
+ALTER TABLE POSTS DROP CONSTRAINT CHECK_TITLE_LENGTH;
+
+-- Drop column
+ALTER TABLE USERS DROP COLUMN IS_ACTIVE;
+`,
+ description: "Reverse: Drop tables, columns, constraints, and indexes",
+ },
+ {
+ name: "reverse_views_and_functions",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PRICE NUMBER(10, 2) NOT NULL,
+ STOCK NUMBER DEFAULT 0
+);
+
+CREATE TABLE ORDERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ PRODUCT_ID NUMBER NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ TOTAL NUMBER(10, 2),
+ CONSTRAINT FK_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
+);
+
+CREATE VIEW PRODUCT_INVENTORY AS
+SELECT
+ P.ID,
+ P.NAME,
+ P.PRICE,
+ P.STOCK,
+ NVL(SUM(O.QUANTITY), 0) AS TOTAL_ORDERED
+FROM PRODUCTS P
+LEFT JOIN ORDERS O ON P.ID = O.PRODUCT_ID
+GROUP BY P.ID, P.NAME, P.PRICE, P.STOCK;
+
+CREATE OR REPLACE FUNCTION CALCULATE_ORDER_TOTAL(P_PRODUCT_ID NUMBER, P_QUANTITY NUMBER)
+RETURN NUMBER
+IS
+ V_PRICE NUMBER(10, 2);
+BEGIN
+ SELECT PRICE INTO V_PRICE FROM PRODUCTS WHERE ID = P_PRODUCT_ID;
+ RETURN P_QUANTITY * V_PRICE;
+EXCEPTION
+ WHEN NO_DATA_FOUND THEN
+ RETURN 0;
+END;
+/
+
+CREATE MATERIALIZED VIEW PRODUCT_STATS AS
+SELECT
+ PRODUCT_ID,
+ COUNT(*) AS ORDER_COUNT,
+ SUM(QUANTITY) AS TOTAL_QUANTITY,
+ SUM(TOTAL) AS TOTAL_REVENUE
+FROM ORDERS
+GROUP BY PRODUCT_ID;
+
+CREATE INDEX IDX_PRODUCT_STATS_REVENUE ON PRODUCT_STATS(TOTAL_REVENUE DESC);
+`,
+ migrationDDL: `
+-- Drop index on materialized view
+DROP INDEX IDX_PRODUCT_STATS_REVENUE;
+
+-- Drop materialized view
+DROP MATERIALIZED VIEW PRODUCT_STATS;
+
+-- Drop function
+DROP FUNCTION CALCULATE_ORDER_TOTAL;
+
+-- Drop view
+DROP VIEW PRODUCT_INVENTORY;
+`,
+ description: "Reverse: Drop views, functions, and materialized views",
+ },
+ {
+ name: "reverse_sequences",
+ initialSchema: `
+CREATE SEQUENCE ORDER_SEQ START WITH 1000 INCREMENT BY 10;
+
+CREATE TABLE ITEMS (
+ ID NUMBER PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL
+);
+
+CREATE TABLE SALES_ORDERS (
+ ID NUMBER DEFAULT ORDER_SEQ.NEXTVAL PRIMARY KEY,
+ ITEM_ID NUMBER NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ CONSTRAINT FK_ITEM FOREIGN KEY (ITEM_ID) REFERENCES ITEMS(ID)
+);
+`,
+ migrationDDL: `
+-- Drop table using sequence
+DROP TABLE SALES_ORDERS CASCADE CONSTRAINTS;
+
+-- Drop sequence
+DROP SEQUENCE ORDER_SEQ;
+`,
+ description: "Reverse: Drop sequences and tables using them",
+ },
+ {
+ name: "reverse_drop_indexes_and_constraints",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ CATEGORY VARCHAR2(50),
+ PRICE NUMBER(10, 2),
+ SUPPLIER_ID NUMBER
+);
+`,
+ migrationDDL: `
+-- Create indexes and constraints
+CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
+CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
+CREATE UNIQUE INDEX IDX_PRODUCTS_NAME ON PRODUCTS(NAME);
+
+ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
+ALTER TABLE PRODUCTS ADD CONSTRAINT CHECK_NAME_LENGTH CHECK (LENGTH(NAME) >= 3);
+`,
+ description: "Reverse: Create indexes and constraints on tables",
+ },
+ {
+ name: "reverse_drop_views_and_functions",
+ initialSchema: `
+CREATE TABLE SALES (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ PRODUCT_NAME VARCHAR2(100) NOT NULL,
+ SALE_AMOUNT NUMBER(10, 2) NOT NULL,
+ SALE_DATE DATE NOT NULL
+);
+`,
+ migrationDDL: `
+-- Create views and functions
+CREATE VIEW MONTHLY_SALES AS
+SELECT
+ EXTRACT(YEAR FROM SALE_DATE) AS YEAR_VAL,
+ EXTRACT(MONTH FROM SALE_DATE) AS MONTH_VAL,
+ SUM(SALE_AMOUNT) AS TOTAL_SALES
+FROM SALES
+GROUP BY EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE);
+
+CREATE MATERIALIZED VIEW TOP_PRODUCTS AS
+SELECT
+ PRODUCT_NAME,
+ COUNT(*) AS SALE_COUNT,
+ SUM(SALE_AMOUNT) AS TOTAL_REVENUE
+FROM SALES
+GROUP BY PRODUCT_NAME
+ORDER BY SUM(SALE_AMOUNT) DESC;
+
+CREATE OR REPLACE FUNCTION GET_MONTHLY_TOTAL(YEAR_PARAM NUMBER, MONTH_PARAM NUMBER)
+RETURN NUMBER
+IS
+ V_TOTAL NUMBER;
+BEGIN
+ SELECT NVL(SUM(SALE_AMOUNT), 0) INTO V_TOTAL
+ FROM SALES
+ WHERE EXTRACT(YEAR FROM SALE_DATE) = YEAR_PARAM
+ AND EXTRACT(MONTH FROM SALE_DATE) = MONTH_PARAM;
+
+ RETURN V_TOTAL;
+END;
+/
+
+CREATE OR REPLACE FUNCTION CALCULATE_DISCOUNT(AMOUNT NUMBER)
+RETURN NUMBER
+IS
+BEGIN
+ RETURN AMOUNT * 0.1;
+END;
+/
+`,
+ description: "Reverse: Create views and functions that depend on tables",
+ },
+ {
+ name: "reverse_alter_table_columns",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PRICE NUMBER(10, 2) NOT NULL,
+ DESCRIPTION CLOB NOT NULL,
+ CATEGORY VARCHAR2(30),
+ IS_ACTIVE NUMBER(1) DEFAULT 1,
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ STOCK_QUANTITY NUMBER DEFAULT 0,
+ WEIGHT NUMBER(5, 2),
+ CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0),
+ CONSTRAINT CHECK_STOCK_NON_NEGATIVE CHECK (STOCK_QUANTITY >= 0)
+);
+
+CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
+CREATE INDEX IDX_PRODUCTS_PRICE ON PRODUCTS(PRICE);
+CREATE INDEX IDX_PRODUCTS_CREATED_AT ON PRODUCTS(CREATED_AT);
+CREATE UNIQUE INDEX IDX_PRODUCTS_NAME_CATEGORY ON PRODUCTS(NAME, CATEGORY);
+`,
+ migrationDDL: `
+-- Drop indexes
+DROP INDEX IDX_PRODUCTS_NAME_CATEGORY;
+DROP INDEX IDX_PRODUCTS_CREATED_AT;
+
+-- Drop constraints
+ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_STOCK_NON_NEGATIVE;
+ALTER TABLE PRODUCTS DROP CONSTRAINT CHECK_PRICE_POSITIVE;
+
+-- Revert column changes
+ALTER TABLE PRODUCTS MODIFY DESCRIPTION NULL;
+ALTER TABLE PRODUCTS MODIFY PRICE NUMBER(8, 2);
+ALTER TABLE PRODUCTS MODIFY NAME VARCHAR2(50);
+
+-- Drop columns
+ALTER TABLE PRODUCTS DROP COLUMN WEIGHT;
+ALTER TABLE PRODUCTS DROP COLUMN STOCK_QUANTITY;
+ALTER TABLE PRODUCTS DROP COLUMN CREATED_AT;
+`,
+ description: "Reverse: Remove columns, constraints and revert type changes",
+ },
+ {
+ name: "reverse_drop_and_recreate_constraints",
+ initialSchema: `
+CREATE TABLE AUTHORS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ EMAIL VARCHAR2(100) UNIQUE
+);
+
+CREATE TABLE BOOKS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ TITLE VARCHAR2(200) NOT NULL,
+ AUTHOR_ID NUMBER NOT NULL,
+ ISBN VARCHAR2(20) UNIQUE,
+ PUBLISHED_YEAR NUMBER,
+ PRICE NUMBER(8, 2),
+ CONSTRAINT FK_AUTHOR_NEW FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID) ON DELETE CASCADE,
+ CONSTRAINT CHECK_YEAR_EXTENDED CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2030),
+ CONSTRAINT CHECK_TITLE_LENGTH CHECK (LENGTH(TITLE) >= 3)
+);
+
+CREATE INDEX IDX_BOOKS_AUTHOR ON BOOKS(AUTHOR_ID);
+CREATE INDEX IDX_BOOKS_YEAR ON BOOKS(PUBLISHED_YEAR);
+`,
+ migrationDDL: `
+-- Drop new constraints
+ALTER TABLE BOOKS DROP CONSTRAINT CHECK_TITLE_LENGTH;
+ALTER TABLE BOOKS DROP CONSTRAINT CHECK_YEAR_EXTENDED;
+ALTER TABLE BOOKS DROP CONSTRAINT FK_AUTHOR_NEW;
+
+-- Recreate original constraints
+ALTER TABLE BOOKS ADD CONSTRAINT FK_AUTHOR FOREIGN KEY (AUTHOR_ID) REFERENCES AUTHORS(ID);
+ALTER TABLE BOOKS ADD CONSTRAINT CHECK_YEAR_VALID CHECK (PUBLISHED_YEAR >= 1000 AND PUBLISHED_YEAR <= 2100);
+ALTER TABLE BOOKS ADD CONSTRAINT CHECK_PRICE_POSITIVE CHECK (PRICE > 0);
+`,
+ description: "Reverse: Revert constraint modifications",
+ },
+ {
+ name: "reverse_drop_sequence",
+ initialSchema: "",
+ migrationDDL: `
+-- Create sequence and table
+CREATE SEQUENCE TRANSACTION_SEQ START WITH 1000;
+
+CREATE TABLE SIMPLE_LOG (
+ ID NUMBER DEFAULT TRANSACTION_SEQ.NEXTVAL PRIMARY KEY,
+ MESSAGE CLOB NOT NULL
+);
+`,
+ description: "Reverse: Create sequence and dependent table",
+ },
+ {
+ name: "reverse_complex_view_dependencies",
+ initialSchema: `
+CREATE TABLE DEPARTMENTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ MANAGER_ID NUMBER
+);
+
+CREATE TABLE EMPLOYEES (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ DEPARTMENT_ID NUMBER,
+ SALARY NUMBER(10, 2),
+ CONSTRAINT FK_DEPT FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENTS(ID)
+);
+
+ALTER TABLE DEPARTMENTS ADD CONSTRAINT FK_MANAGER FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES(ID);
+
+CREATE VIEW DEPT_EMPLOYEE_COUNT AS
+SELECT D.ID AS DEPT_ID, D.NAME AS DEPT_NAME, COUNT(E.ID) AS EMP_COUNT
+FROM DEPARTMENTS D
+LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
+GROUP BY D.ID, D.NAME;
+
+CREATE VIEW DEPT_SUMMARY AS
+SELECT
+ DEC.DEPT_ID,
+ DEC.DEPT_NAME,
+ DEC.EMP_COUNT,
+ NVL(AVG(E.SALARY), 0) AS AVG_SALARY
+FROM DEPT_EMPLOYEE_COUNT DEC
+LEFT JOIN EMPLOYEES E ON DEC.DEPT_ID = E.DEPARTMENT_ID
+GROUP BY DEC.DEPT_ID, DEC.DEPT_NAME, DEC.EMP_COUNT;
+
+CREATE VIEW DEPT_MANAGER_SUMMARY AS
+SELECT
+ DS.DEPT_ID,
+ DS.DEPT_NAME,
+ DS.EMP_COUNT,
+ DS.AVG_SALARY,
+ M.NAME AS MANAGER_NAME
+FROM DEPT_SUMMARY DS
+JOIN DEPARTMENTS D ON DS.DEPT_ID = D.ID
+LEFT JOIN EMPLOYEES M ON D.MANAGER_ID = M.ID;
+
+CREATE MATERIALIZED VIEW MV_DEPT_STATS AS
+SELECT
+ D.ID AS DEPT_ID,
+ D.NAME AS DEPT_NAME,
+ COUNT(E.ID) AS TOTAL_EMPLOYEES,
+ NVL(SUM(E.SALARY), 0) AS TOTAL_PAYROLL,
+ NVL(AVG(E.SALARY), 0) AS AVG_SALARY,
+ NVL(MAX(E.SALARY), 0) AS MAX_SALARY,
+ NVL(MIN(E.SALARY), 0) AS MIN_SALARY
+FROM DEPARTMENTS D
+LEFT JOIN EMPLOYEES E ON D.ID = E.DEPARTMENT_ID
+GROUP BY D.ID, D.NAME;
+
+CREATE INDEX IDX_MV_DEPT_PAYROLL ON MV_DEPT_STATS(TOTAL_PAYROLL DESC);
+`,
+ migrationDDL: `
+-- Drop index on materialized view
+DROP INDEX IDX_MV_DEPT_PAYROLL;
+
+-- Drop materialized view
+DROP MATERIALIZED VIEW MV_DEPT_STATS;
+
+-- Drop views in dependency order
+DROP VIEW DEPT_MANAGER_SUMMARY;
+DROP VIEW DEPT_SUMMARY;
+DROP VIEW DEPT_EMPLOYEE_COUNT;
+`,
+ description: "Reverse: Drop complex view dependencies",
+ },
+ {
+ name: "reverse_circular_foreign_key_dependencies",
+ initialSchema: `
+CREATE TABLE CUSTOMERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PREFERRED_ORDER_ID NUMBER
+);
+
+CREATE TABLE ORDERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ CUSTOMER_ID NUMBER NOT NULL,
+ ORDER_DATE DATE DEFAULT SYSDATE,
+ TOTAL_AMOUNT NUMBER(10, 2)
+);
+
+ALTER TABLE CUSTOMERS ADD CONSTRAINT FK_PREFERRED_ORDER FOREIGN KEY (PREFERRED_ORDER_ID) REFERENCES ORDERS(ID);
+ALTER TABLE ORDERS ADD CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID);
+
+CREATE TABLE ORDER_ITEMS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ ORDER_ID NUMBER NOT NULL,
+ PRODUCT_NAME VARCHAR2(100) NOT NULL,
+ QUANTITY NUMBER NOT NULL,
+ UNIT_PRICE NUMBER(10, 2) NOT NULL,
+ CONSTRAINT FK_ORDER_ITEM FOREIGN KEY (ORDER_ID) REFERENCES ORDERS(ID) ON DELETE CASCADE
+);
+
+CREATE OR REPLACE TRIGGER TRG_UPDATE_ORDER_TOTAL
+AFTER INSERT OR UPDATE OR DELETE ON ORDER_ITEMS
+FOR EACH ROW
+DECLARE
+ V_ORDER_ID NUMBER;
+ V_TOTAL NUMBER(10, 2);
+BEGIN
+ -- Get order ID
+ IF INSERTING OR UPDATING THEN
+ V_ORDER_ID := :NEW.ORDER_ID;
+ ELSE
+ V_ORDER_ID := :OLD.ORDER_ID;
+ END IF;
+
+ -- Calculate new total
+ SELECT NVL(SUM(QUANTITY * UNIT_PRICE), 0) INTO V_TOTAL
+ FROM ORDER_ITEMS
+ WHERE ORDER_ID = V_ORDER_ID;
+
+ -- Update order total
+ UPDATE ORDERS SET TOTAL_AMOUNT = V_TOTAL WHERE ID = V_ORDER_ID;
+END;
+/
+`,
+ migrationDDL: `
+-- Drop trigger
+DROP TRIGGER TRG_UPDATE_ORDER_TOTAL;
+
+-- Drop table with foreign key
+DROP TABLE ORDER_ITEMS CASCADE CONSTRAINTS;
+
+-- Drop circular foreign keys
+ALTER TABLE ORDERS DROP CONSTRAINT FK_CUSTOMER;
+ALTER TABLE CUSTOMERS DROP CONSTRAINT FK_PREFERRED_ORDER;
+`,
+ description: "Reverse: Remove circular foreign key dependencies and triggers",
+ },
+ {
+ name: "reverse_advanced_indexes_and_constraints",
+ initialSchema: `
+CREATE TABLE SALES_DATA (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY,
+ SALE_DATE DATE NOT NULL,
+ CUSTOMER_ID NUMBER NOT NULL,
+ PRODUCT_ID NUMBER NOT NULL,
+ AMOUNT NUMBER(10, 2) NOT NULL,
+ REGION VARCHAR2(50) NOT NULL,
+ CONSTRAINT UK_SALES_DATE_CUSTOMER UNIQUE (SALE_DATE, CUSTOMER_ID, PRODUCT_ID),
+ CONSTRAINT CHK_AMOUNT_RANGE CHECK (AMOUNT > 0 AND AMOUNT <= 999999.99),
+ CONSTRAINT CHK_REGION_VALID CHECK (REGION IN ('NORTH', 'SOUTH', 'EAST', 'WEST', 'CENTRAL'))
+);
+
+CREATE INDEX IDX_SALES_MONTH_YEAR ON SALES_DATA(EXTRACT(YEAR FROM SALE_DATE), EXTRACT(MONTH FROM SALE_DATE));
+CREATE INDEX IDX_SALES_REGION_AMOUNT ON SALES_DATA(REGION ASC, AMOUNT DESC);
+`,
+ migrationDDL: `
+-- Drop indexes
+DROP INDEX IDX_SALES_REGION_AMOUNT;
+DROP INDEX IDX_SALES_MONTH_YEAR;
+
+-- Drop constraints
+ALTER TABLE SALES_DATA DROP CONSTRAINT CHK_REGION_VALID;
+ALTER TABLE SALES_DATA DROP CONSTRAINT CHK_AMOUNT_RANGE;
+ALTER TABLE SALES_DATA DROP CONSTRAINT UK_SALES_DATE_CUSTOMER;
+`,
+ description: "Reverse: Remove advanced indexes and complex constraints",
+ },
+ {
+ name: "reverse_stored_procedures_and_functions",
+ initialSchema: `
+CREATE TABLE AUDIT_LOG (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ TABLE_NAME VARCHAR2(100) NOT NULL,
+ OPERATION VARCHAR2(10) NOT NULL,
+ OLD_VALUES CLOB,
+ NEW_VALUES CLOB,
+ TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ USER_NAME VARCHAR2(100) DEFAULT USER
+);
+
+CREATE OR REPLACE PROCEDURE LOG_AUDIT(
+ P_TABLE_NAME VARCHAR2,
+ P_OPERATION VARCHAR2,
+ P_OLD_VALUES CLOB DEFAULT NULL,
+ P_NEW_VALUES CLOB DEFAULT NULL
+)
+IS
+BEGIN
+ INSERT INTO AUDIT_LOG (TABLE_NAME, OPERATION, OLD_VALUES, NEW_VALUES)
+ VALUES (P_TABLE_NAME, P_OPERATION, P_OLD_VALUES, P_NEW_VALUES);
+ COMMIT;
+EXCEPTION
+ WHEN OTHERS THEN
+ ROLLBACK;
+ RAISE;
+END;
+/
+
+CREATE OR REPLACE FUNCTION CALCULATE_BUSINESS_DAYS(
+ P_START_DATE DATE,
+ P_END_DATE DATE
+) RETURN NUMBER
+IS
+ V_DAYS NUMBER := 0;
+ V_CURRENT_DATE DATE := P_START_DATE;
+BEGIN
+ WHILE V_CURRENT_DATE <= P_END_DATE LOOP
+ -- Skip weekends (Saturday = 7, Sunday = 1)
+ IF TO_CHAR(V_CURRENT_DATE, 'D') NOT IN ('1', '7') THEN
+ V_DAYS := V_DAYS + 1;
+ END IF;
+ V_CURRENT_DATE := V_CURRENT_DATE + 1;
+ END LOOP;
+
+ RETURN V_DAYS;
+END;
+/
+
+CREATE OR REPLACE FUNCTION GET_AUDIT_COUNT(P_TABLE_NAME VARCHAR2) RETURN NUMBER
+IS
+ V_COUNT NUMBER;
+BEGIN
+ SELECT COUNT(*) INTO V_COUNT
+ FROM AUDIT_LOG
+ WHERE TABLE_NAME = P_TABLE_NAME;
+
+ RETURN V_COUNT;
+END;
+/
+`,
+ migrationDDL: `
+-- Drop functions and procedures
+DROP FUNCTION GET_AUDIT_COUNT;
+DROP FUNCTION CALCULATE_BUSINESS_DAYS;
+DROP PROCEDURE LOG_AUDIT;
+`,
+ description: "Reverse: Remove stored procedures and functions",
+ },
+ {
+ name: "reverse_edge_case_column_operations",
+ initialSchema: `
+CREATE TABLE TEST_COLUMNS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ COL_VARCHAR VARCHAR2(200) NOT NULL,
+ COL_NUMBER NUMBER(15,3),
+ COL_DATE DATE,
+ COL_NULLABLE VARCHAR2(100) NOT NULL,
+ COL_WITH_DEFAULT VARCHAR2(50) DEFAULT 'NEW_DEFAULT',
+ COL_TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ COL_BOOLEAN NUMBER(1) CHECK (COL_BOOLEAN IN (0,1)),
+ CONSTRAINT CHK_VARCHAR_LENGTH CHECK (LENGTH(COL_VARCHAR) >= 5),
+ CONSTRAINT CHK_NUMBER_POSITIVE CHECK (COL_NUMBER > 0)
+);
+`,
+ migrationDDL: `
+-- Drop constraints
+ALTER TABLE TEST_COLUMNS DROP CONSTRAINT CHK_NUMBER_POSITIVE;
+ALTER TABLE TEST_COLUMNS DROP CONSTRAINT CHK_VARCHAR_LENGTH;
+
+-- Drop columns
+ALTER TABLE TEST_COLUMNS DROP COLUMN COL_BOOLEAN;
+ALTER TABLE TEST_COLUMNS DROP COLUMN COL_TIMESTAMP;
+
+-- Add back dropped column
+ALTER TABLE TEST_COLUMNS ADD COL_CLOB CLOB;
+
+-- Revert column modifications
+ALTER TABLE TEST_COLUMNS MODIFY COL_WITH_DEFAULT DEFAULT 'DEFAULT_VALUE';
+ALTER TABLE TEST_COLUMNS MODIFY COL_NULLABLE NULL;
+ALTER TABLE TEST_COLUMNS MODIFY COL_NUMBER NUMBER(10,2);
+ALTER TABLE TEST_COLUMNS MODIFY COL_VARCHAR VARCHAR2(50);
+`,
+ description: "Reverse: Revert edge case column operations",
+ },
+ {
+ name: "table_and_column_comments",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PRICE NUMBER(10, 2) NOT NULL,
+ DESCRIPTION CLOB,
+ CATEGORY VARCHAR2(50),
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE CUSTOMERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ EMAIL VARCHAR2(100),
+ PHONE VARCHAR2(20),
+ ADDRESS CLOB
+);
+
+CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
+CREATE UNIQUE INDEX IDX_CUSTOMERS_EMAIL ON CUSTOMERS(EMAIL);
+`,
+ migrationDDL: `
+-- Add table comments
+COMMENT ON TABLE PRODUCTS IS 'Product catalog table containing all product information';
+COMMENT ON TABLE CUSTOMERS IS 'Customer information and contact details';
+
+-- Add column comments
+COMMENT ON COLUMN PRODUCTS.ID IS 'Unique product identifier (auto-generated)';
+COMMENT ON COLUMN PRODUCTS.NAME IS 'Product name - must be unique and descriptive';
+COMMENT ON COLUMN PRODUCTS.PRICE IS 'Product price in USD with 2 decimal places';
+COMMENT ON COLUMN PRODUCTS.DESCRIPTION IS 'Detailed product description with HTML formatting support';
+COMMENT ON COLUMN PRODUCTS.CATEGORY IS 'Product category for filtering and organization';
+COMMENT ON COLUMN PRODUCTS.CREATED_AT IS 'Timestamp when the product was added to catalog';
+
+COMMENT ON COLUMN CUSTOMERS.ID IS 'Primary key for customer records';
+COMMENT ON COLUMN CUSTOMERS.NAME IS 'Full name of the customer';
+COMMENT ON COLUMN CUSTOMERS.EMAIL IS 'Customer email address (unique)';
+COMMENT ON COLUMN CUSTOMERS.PHONE IS 'Contact phone number';
+COMMENT ON COLUMN CUSTOMERS.ADDRESS IS 'Full mailing address with multiple lines';
+
+-- Create new table with comments immediately
+CREATE TABLE ORDERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ CUSTOMER_ID NUMBER NOT NULL,
+ PRODUCT_ID NUMBER NOT NULL,
+ ORDER_DATE DATE DEFAULT SYSDATE,
+ QUANTITY NUMBER NOT NULL,
+ TOTAL_AMOUNT NUMBER(12, 2),
+ STATUS VARCHAR2(20) DEFAULT 'PENDING',
+ CONSTRAINT FK_ORDER_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID),
+ CONSTRAINT FK_ORDER_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
+);
+
+-- Add comments to the new table
+COMMENT ON TABLE ORDERS IS 'Customer orders linking products and customers';
+COMMENT ON COLUMN ORDERS.ID IS 'Unique order identifier';
+COMMENT ON COLUMN ORDERS.CUSTOMER_ID IS 'Reference to customer who placed the order';
+COMMENT ON COLUMN ORDERS.PRODUCT_ID IS 'Reference to ordered product';
+COMMENT ON COLUMN ORDERS.ORDER_DATE IS 'Date when the order was placed';
+COMMENT ON COLUMN ORDERS.QUANTITY IS 'Number of items ordered';
+COMMENT ON COLUMN ORDERS.TOTAL_AMOUNT IS 'Total order amount including taxes';
+COMMENT ON COLUMN ORDERS.STATUS IS 'Current order status (PENDING, SHIPPED, DELIVERED, CANCELLED)';
+`,
+ description: "Add comprehensive comments to tables and columns using Oracle COMMENT syntax",
+ },
+ {
+ name: "modify_and_drop_comments",
+ initialSchema: `
+CREATE TABLE INVENTORY (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ PRODUCT_CODE VARCHAR2(50) NOT NULL,
+ STOCK_LEVEL NUMBER NOT NULL,
+ MIN_STOCK NUMBER DEFAULT 10,
+ MAX_STOCK NUMBER DEFAULT 1000,
+ WAREHOUSE_ID NUMBER,
+ LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+-- Add initial comments
+COMMENT ON TABLE INVENTORY IS 'Initial inventory tracking table';
+COMMENT ON COLUMN INVENTORY.ID IS 'Primary key for inventory records';
+COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Product identification code';
+COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Current stock quantity';
+COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Minimum stock threshold';
+COMMENT ON COLUMN INVENTORY.MAX_STOCK IS 'Maximum stock capacity';
+COMMENT ON COLUMN INVENTORY.WAREHOUSE_ID IS 'Warehouse location identifier';
+
+CREATE TABLE SUPPLIERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ CONTACT_EMAIL VARCHAR2(100),
+ RATING NUMBER(2,1)
+);
+
+COMMENT ON TABLE SUPPLIERS IS 'Supplier information table';
+COMMENT ON COLUMN SUPPLIERS.NAME IS 'Supplier company name';
+COMMENT ON COLUMN SUPPLIERS.CONTACT_EMAIL IS 'Primary contact email';
+`,
+ migrationDDL: `
+-- Modify existing table comment
+COMMENT ON TABLE INVENTORY IS 'Updated inventory management system with enhanced tracking capabilities';
+
+-- Modify existing column comments
+COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Updated: Standardized product SKU following new naming convention';
+COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Updated: Real-time stock quantity with automated updates';
+COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Updated: Dynamic minimum threshold based on sales velocity';
+
+-- Remove comments by setting to empty string
+COMMENT ON COLUMN INVENTORY.MAX_STOCK IS '';
+COMMENT ON COLUMN INVENTORY.WAREHOUSE_ID IS '';
+
+-- Remove table comment
+COMMENT ON TABLE SUPPLIERS IS '';
+
+-- Remove specific column comments using empty string
+COMMENT ON COLUMN SUPPLIERS.CONTACT_EMAIL IS '';
+
+-- Add new comment to previously uncommented column
+COMMENT ON COLUMN INVENTORY.LAST_UPDATED IS 'Timestamp of last inventory update or modification';
+
+-- Add new column with comment
+ALTER TABLE INVENTORY ADD REORDER_POINT NUMBER DEFAULT 0;
+COMMENT ON COLUMN INVENTORY.REORDER_POINT IS 'Calculated reorder point based on lead time and demand';
+
+-- Modify existing column comment after it was cleared
+COMMENT ON COLUMN SUPPLIERS.RATING IS 'Supplier performance rating (1.0 to 5.0 scale)';
+`,
+ description: "Modify existing comments and remove comments using empty string",
+ },
+ {
+ name: "comments_with_special_characters",
+ initialSchema: `
+CREATE TABLE SPECIAL_DATA (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ DATA_VALUE VARCHAR2(500),
+ METADATA_JSON CLOB,
+ UNICODE_TEXT NVARCHAR2(200),
+ BINARY_DATA BLOB,
+ CATEGORY VARCHAR2(100),
+ STATUS VARCHAR2(50)
+);
+
+CREATE TABLE MULTILANG_CONTENT (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ TITLE_EN VARCHAR2(200),
+ TITLE_ES VARCHAR2(200),
+ TITLE_ZH NVARCHAR2(200),
+ CONTENT_TEXT CLOB,
+ TAGS VARCHAR2(1000)
+);
+`,
+ migrationDDL: `
+-- Comments with single quotes and escaping
+COMMENT ON TABLE SPECIAL_DATA IS 'Table containing "special" data with various formats & types';
+COMMENT ON COLUMN SPECIAL_DATA.DATA_VALUE IS 'Data value field - supports NULL, empty strings, and ''quoted'' content';
+
+-- Comments with double quotes
+COMMENT ON COLUMN SPECIAL_DATA.METADATA_JSON IS 'JSON metadata: {"key": "value", "array": [1,2,3]}';
+
+-- Multi-line comments with line breaks and special characters
+COMMENT ON COLUMN SPECIAL_DATA.UNICODE_TEXT IS 'Unicode text support:
+Line 1: Basic ASCII text
+Line 2: Special chars !@#$%^&*()_+-={}[]|;:,.<>?
+Line 3: Math symbols ≤ ≥ ± × ÷ ∑ ∏ ∆ π
+Line 4: Currencies $ € £ ¥ ₹';
+
+-- Comments with backslashes and escape sequences
+COMMENT ON COLUMN SPECIAL_DATA.BINARY_DATA IS 'Binary data storage\nSupports: images, documents, archives\tTab-separated info\r\nWindows line endings';
+
+-- Comments with percent signs and wildcards
+COMMENT ON COLUMN SPECIAL_DATA.CATEGORY IS 'Category field: use % for wildcard searches, _ for single char matching';
+
+-- Comments with SQL injection-like patterns (should be safely handled)
+COMMENT ON COLUMN SPECIAL_DATA.STATUS IS 'Status field - values: ''ACTIVE'', ''INACTIVE'', ''PENDING''; DROP TABLE users; --';
+
+-- Unicode comments with international characters
+COMMENT ON TABLE MULTILANG_CONTENT IS 'Multilingual content: English, Español, 中文, العربية, Русский, 日本語';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_EN IS 'English title field';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ES IS 'Título en español';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ZH IS '中文标题字段 - 支持简体和繁体中文';
+
+-- Long comment with mixed content
+COMMENT ON COLUMN MULTILANG_CONTENT.CONTENT_TEXT IS 'Content field supporting:
+1. HTML tags: <b>bold</b>, <i>italic</i>, <a href="http://example.com">links</a>
+2. Markdown: **bold**, *italic*, [link](url), backtick-code-backtick
+3. Special symbols: © ® ™ § ¶ † ‡ • … ‰ ′ ″
+4. Emojis: 😀 😃 😄 😁 🚀 ⭐ 🎉 💯
+5. Mathematical: ∀x∈ℝ: x² ≥ 0, ∑(i=1 to n) i = n(n+1)/2';
+
+-- Comment with XML/HTML content
+COMMENT ON COLUMN MULTILANG_CONTENT.TAGS IS 'XML tags: <tag type="category" value="news"/><tag type="priority" value="high"/>End of XML';
+
+-- Create additional table to test more complex scenarios
+CREATE TABLE COMPLEX_TYPES (
+ ID NUMBER PRIMARY KEY,
+ XML_DATA XMLTYPE,
+ TIMESTAMP_TZ TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP,
+ INTERVAL_DS INTERVAL DAY TO SECOND,
+ RAW_DATA RAW(100),
+ LONG_RAW_DATA LONG RAW
+);
+
+-- Comments with technical specifications
+COMMENT ON TABLE COMPLEX_TYPES IS 'Advanced data types testing table
+Supports: XMLType, Timestamps with timezone, Intervals, RAW data
+Performance: Optimized for OLTP workloads @ 10K+ TPS
+Security: Row-level security enabled, audit trails maintained';
+
+COMMENT ON COLUMN COMPLEX_TYPES.XML_DATA IS 'XMLType column for structured XML storage
+Example: <?xml version="1.0" encoding="UTF-8"?>
+<root><item id="1" name="test">Content</item></root>';
+
+COMMENT ON COLUMN COMPLEX_TYPES.TIMESTAMP_TZ IS 'Timestamp with timezone: YYYY-MM-DD HH24:MI:SS.FF TZR
+Default: SYSTIMESTAMP (system timestamp with session timezone)';
+
+COMMENT ON COLUMN COMPLEX_TYPES.INTERVAL_DS IS 'Interval Day to Second: +/-DD HH:MI:SS.FFFFFF
+Range: +/-999999999 days, precision up to nanoseconds';
+`,
+ description: "Test comments with special characters, quotes, Unicode, multi-line text, and complex content",
+ },
+ {
+ name: "reverse_table_and_column_comments",
+ initialSchema: `
+CREATE TABLE PRODUCTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ PRICE NUMBER(10, 2) NOT NULL,
+ DESCRIPTION CLOB,
+ CATEGORY VARCHAR2(50),
+ CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE CUSTOMERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ EMAIL VARCHAR2(100),
+ PHONE VARCHAR2(20),
+ ADDRESS CLOB
+);
+
+CREATE TABLE ORDERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ CUSTOMER_ID NUMBER NOT NULL,
+ PRODUCT_ID NUMBER NOT NULL,
+ ORDER_DATE DATE DEFAULT SYSDATE,
+ QUANTITY NUMBER NOT NULL,
+ TOTAL_AMOUNT NUMBER(12, 2),
+ STATUS VARCHAR2(20) DEFAULT 'PENDING',
+ CONSTRAINT FK_ORDER_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS(ID),
+ CONSTRAINT FK_ORDER_PRODUCT FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(ID)
+);
+
+CREATE INDEX IDX_PRODUCTS_CATEGORY ON PRODUCTS(CATEGORY);
+CREATE UNIQUE INDEX IDX_CUSTOMERS_EMAIL ON CUSTOMERS(EMAIL);
+
+-- Add all comments
+COMMENT ON TABLE PRODUCTS IS 'Product catalog table containing all product information';
+COMMENT ON TABLE CUSTOMERS IS 'Customer information and contact details';
+COMMENT ON COLUMN PRODUCTS.ID IS 'Unique product identifier (auto-generated)';
+COMMENT ON COLUMN PRODUCTS.NAME IS 'Product name - must be unique and descriptive';
+COMMENT ON COLUMN PRODUCTS.PRICE IS 'Product price in USD with 2 decimal places';
+COMMENT ON COLUMN PRODUCTS.DESCRIPTION IS 'Detailed product description with HTML formatting support';
+COMMENT ON COLUMN PRODUCTS.CATEGORY IS 'Product category for filtering and organization';
+COMMENT ON COLUMN PRODUCTS.CREATED_AT IS 'Timestamp when the product was added to catalog';
+COMMENT ON COLUMN CUSTOMERS.ID IS 'Primary key for customer records';
+COMMENT ON COLUMN CUSTOMERS.NAME IS 'Full name of the customer';
+COMMENT ON COLUMN CUSTOMERS.EMAIL IS 'Customer email address (unique)';
+COMMENT ON COLUMN CUSTOMERS.PHONE IS 'Contact phone number';
+COMMENT ON COLUMN CUSTOMERS.ADDRESS IS 'Full mailing address with multiple lines';
+COMMENT ON TABLE ORDERS IS 'Customer orders linking products and customers';
+COMMENT ON COLUMN ORDERS.ID IS 'Unique order identifier';
+COMMENT ON COLUMN ORDERS.CUSTOMER_ID IS 'Reference to customer who placed the order';
+COMMENT ON COLUMN ORDERS.PRODUCT_ID IS 'Reference to ordered product';
+COMMENT ON COLUMN ORDERS.ORDER_DATE IS 'Date when the order was placed';
+COMMENT ON COLUMN ORDERS.QUANTITY IS 'Number of items ordered';
+COMMENT ON COLUMN ORDERS.TOTAL_AMOUNT IS 'Total order amount including taxes';
+COMMENT ON COLUMN ORDERS.STATUS IS 'Current order status (PENDING, SHIPPED, DELIVERED, CANCELLED)';
+`,
+ migrationDDL: `
+-- Remove all comments
+COMMENT ON COLUMN ORDERS.STATUS IS '';
+COMMENT ON COLUMN ORDERS.TOTAL_AMOUNT IS '';
+COMMENT ON COLUMN ORDERS.QUANTITY IS '';
+COMMENT ON COLUMN ORDERS.ORDER_DATE IS '';
+COMMENT ON COLUMN ORDERS.PRODUCT_ID IS '';
+COMMENT ON COLUMN ORDERS.CUSTOMER_ID IS '';
+COMMENT ON COLUMN ORDERS.ID IS '';
+COMMENT ON TABLE ORDERS IS '';
+
+COMMENT ON COLUMN CUSTOMERS.ADDRESS IS '';
+COMMENT ON COLUMN CUSTOMERS.PHONE IS '';
+COMMENT ON COLUMN CUSTOMERS.EMAIL IS '';
+COMMENT ON COLUMN CUSTOMERS.NAME IS '';
+COMMENT ON COLUMN CUSTOMERS.ID IS '';
+
+COMMENT ON COLUMN PRODUCTS.CREATED_AT IS '';
+COMMENT ON COLUMN PRODUCTS.CATEGORY IS '';
+COMMENT ON COLUMN PRODUCTS.DESCRIPTION IS '';
+COMMENT ON COLUMN PRODUCTS.PRICE IS '';
+COMMENT ON COLUMN PRODUCTS.NAME IS '';
+COMMENT ON COLUMN PRODUCTS.ID IS '';
+
+COMMENT ON TABLE CUSTOMERS IS '';
+COMMENT ON TABLE PRODUCTS IS '';
+
+-- Drop the ORDERS table
+DROP TABLE ORDERS CASCADE CONSTRAINTS;
+`,
+ description: "Reverse: Remove all comments and drop a table",
+ },
+ {
+ name: "reverse_modify_and_drop_comments",
+ initialSchema: `
+CREATE TABLE INVENTORY (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ PRODUCT_CODE VARCHAR2(50) NOT NULL,
+ STOCK_LEVEL NUMBER NOT NULL,
+ MIN_STOCK NUMBER DEFAULT 10,
+ MAX_STOCK NUMBER DEFAULT 1000,
+ WAREHOUSE_ID NUMBER,
+ LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ REORDER_POINT NUMBER DEFAULT 0
+);
+
+CREATE TABLE SUPPLIERS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ CONTACT_EMAIL VARCHAR2(100),
+ RATING NUMBER(2,1)
+);
+
+-- Modified comments
+COMMENT ON TABLE INVENTORY IS 'Updated inventory management system with enhanced tracking capabilities';
+COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Updated: Standardized product SKU following new naming convention';
+COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Updated: Real-time stock quantity with automated updates';
+COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Updated: Dynamic minimum threshold based on sales velocity';
+COMMENT ON COLUMN INVENTORY.LAST_UPDATED IS 'Timestamp of last inventory update or modification';
+COMMENT ON COLUMN INVENTORY.REORDER_POINT IS 'Calculated reorder point based on lead time and demand';
+COMMENT ON COLUMN SUPPLIERS.RATING IS 'Supplier performance rating (1.0 to 5.0 scale)';
+`,
+ migrationDDL: `
+-- Drop column with comment
+ALTER TABLE INVENTORY DROP COLUMN REORDER_POINT;
+
+-- Add comment to new column
+COMMENT ON COLUMN INVENTORY.WAREHOUSE_ID IS 'Warehouse location identifier';
+
+-- Restore original comment
+COMMENT ON COLUMN INVENTORY.MIN_STOCK IS 'Minimum stock threshold';
+
+-- Remove other updated comments
+COMMENT ON COLUMN INVENTORY.STOCK_LEVEL IS 'Current stock quantity';
+COMMENT ON COLUMN INVENTORY.PRODUCT_CODE IS 'Product identification code';
+
+-- Add comments that were removed
+COMMENT ON TABLE SUPPLIERS IS 'Supplier information table';
+COMMENT ON COLUMN SUPPLIERS.CONTACT_EMAIL IS 'Primary contact email';
+
+-- Restore other columns that had comments removed
+COMMENT ON COLUMN INVENTORY.MAX_STOCK IS 'Maximum stock capacity';
+
+-- Change table comment
+COMMENT ON TABLE INVENTORY IS 'Initial inventory tracking table';
+
+-- Remove comments
+COMMENT ON COLUMN SUPPLIERS.RATING IS '';
+COMMENT ON COLUMN INVENTORY.LAST_UPDATED IS '';
+`,
+ description: "Reverse: Restore original comments and modify state",
+ },
+ {
+ name: "reverse_comments_with_special_characters",
+ initialSchema: `
+CREATE TABLE SPECIAL_DATA (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ DATA_VALUE VARCHAR2(500),
+ METADATA_JSON CLOB,
+ UNICODE_TEXT NVARCHAR2(200),
+ BINARY_DATA BLOB,
+ CATEGORY VARCHAR2(100),
+ STATUS VARCHAR2(50)
+);
+
+CREATE TABLE MULTILANG_CONTENT (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ TITLE_EN VARCHAR2(200),
+ TITLE_ES VARCHAR2(200),
+ TITLE_ZH NVARCHAR2(200),
+ CONTENT_TEXT CLOB,
+ TAGS VARCHAR2(1000)
+);
+
+CREATE TABLE COMPLEX_TYPES (
+ ID NUMBER PRIMARY KEY,
+ XML_DATA XMLTYPE,
+ TIMESTAMP_TZ TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP,
+ INTERVAL_DS INTERVAL DAY TO SECOND,
+ RAW_DATA RAW(100),
+ LONG_RAW_DATA LONG RAW
+);
+
+-- All comments added
+COMMENT ON TABLE SPECIAL_DATA IS 'Table containing "special" data with various formats & types';
+COMMENT ON COLUMN SPECIAL_DATA.DATA_VALUE IS 'Data value field - supports NULL, empty strings, and ''quoted'' content';
+COMMENT ON COLUMN SPECIAL_DATA.METADATA_JSON IS 'JSON metadata: {"key": "value", "array": [1,2,3]}';
+COMMENT ON COLUMN SPECIAL_DATA.UNICODE_TEXT IS 'Unicode text support:
+Line 1: Basic ASCII text
+Line 2: Special chars !@#$%^&*()_+-={}[]|;:,.<>?
+Line 3: Math symbols ≤ ≥ ± × ÷ ∑ ∏ ∆ π
+Line 4: Currencies $ € £ ¥ ₹';
+COMMENT ON COLUMN SPECIAL_DATA.BINARY_DATA IS 'Binary data storage\nSupports: images, documents, archives\tTab-separated info\r\nWindows line endings';
+COMMENT ON COLUMN SPECIAL_DATA.CATEGORY IS 'Category field: use % for wildcard searches, _ for single char matching';
+COMMENT ON COLUMN SPECIAL_DATA.STATUS IS 'Status field - values: ''ACTIVE'', ''INACTIVE'', ''PENDING''; DROP TABLE users; --';
+COMMENT ON TABLE MULTILANG_CONTENT IS 'Multilingual content: English, Español, 中文, العربية, Русский, 日本語';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_EN IS 'English title field';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ES IS 'Título en español';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ZH IS '中文标题字段 - 支持简体和繁体中文';
+COMMENT ON COLUMN MULTILANG_CONTENT.CONTENT_TEXT IS 'Content field supporting:
+1. HTML tags: <b>bold</b>, <i>italic</i>, <a href="http://example.com">links</a>
+2. Markdown: **bold**, *italic*, [link](url), backtick-code-backtick
+3. Special symbols: © ® ™ § ¶ † ‡ • … ‰ ′ ″
+4. Emojis: 😀 😃 😄 😁 🚀 ⭐ 🎉 💯
+5. Mathematical: ∀x∈ℝ: x² ≥ 0, ∑(i=1 to n) i = n(n+1)/2';
+COMMENT ON COLUMN MULTILANG_CONTENT.TAGS IS 'XML tags: <tag type="category" value="news"/><tag type="priority" value="high"/>End of XML';
+COMMENT ON TABLE COMPLEX_TYPES IS 'Advanced data types testing table
+Supports: XMLType, Timestamps with timezone, Intervals, RAW data
+Performance: Optimized for OLTP workloads @ 10K+ TPS
+Security: Row-level security enabled, audit trails maintained';
+COMMENT ON COLUMN COMPLEX_TYPES.XML_DATA IS 'XMLType column for structured XML storage
+Example: <?xml version="1.0" encoding="UTF-8"?>
+<root><item id="1" name="test">Content</item></root>';
+COMMENT ON COLUMN COMPLEX_TYPES.TIMESTAMP_TZ IS 'Timestamp with timezone: YYYY-MM-DD HH24:MI:SS.FF TZR
+Default: SYSTIMESTAMP (system timestamp with session timezone)';
+COMMENT ON COLUMN COMPLEX_TYPES.INTERVAL_DS IS 'Interval Day to Second: +/-DD HH:MI:SS.FFFFFF
+Range: +/-999999999 days, precision up to nanoseconds';
+`,
+ migrationDDL: `
+-- Drop table with all its comments
+DROP TABLE COMPLEX_TYPES CASCADE CONSTRAINTS;
+
+-- Remove all comments from remaining tables
+COMMENT ON COLUMN MULTILANG_CONTENT.TAGS IS '';
+COMMENT ON COLUMN MULTILANG_CONTENT.CONTENT_TEXT IS '';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ZH IS '';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_ES IS '';
+COMMENT ON COLUMN MULTILANG_CONTENT.TITLE_EN IS '';
+COMMENT ON TABLE MULTILANG_CONTENT IS '';
+
+COMMENT ON COLUMN SPECIAL_DATA.STATUS IS '';
+COMMENT ON COLUMN SPECIAL_DATA.CATEGORY IS '';
+COMMENT ON COLUMN SPECIAL_DATA.BINARY_DATA IS '';
+COMMENT ON COLUMN SPECIAL_DATA.UNICODE_TEXT IS '';
+COMMENT ON COLUMN SPECIAL_DATA.METADATA_JSON IS '';
+COMMENT ON COLUMN SPECIAL_DATA.DATA_VALUE IS '';
+COMMENT ON TABLE SPECIAL_DATA IS '';
+`,
+ description: "Reverse: Remove all comments with special characters",
+ },
+ }
+}
+
+// TestTopologicalOrderCreateObjects tests that objects are created in the correct dependency order
+func TestTopologicalOrderCreateObjects(t *testing.T) {
+ tests := []struct {
+ name string
+ diff *schema.MetadataDiff
+ description string
+ }{
+ {
+ name: "table_depends_on_table_via_foreign_key",
+ diff: &schema.MetadataDiff{
+ TableChanges: []*schema.TableDiff{
+ // Create orders table that depends on customers table via FK
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "ORDERS",
+ NewTable: &storepb.TableMetadata{
+ Name: "ORDERS",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "CUSTOMER_ID", Type: "NUMBER", Nullable: false},
+ },
+ ForeignKeys: []*storepb.ForeignKeyMetadata{
+ {
+ Name: "FK_ORDERS_CUSTOMER",
+ Columns: []string{"CUSTOMER_ID"},
+ ReferencedSchema: "TESTUSER",
+ ReferencedTable: "CUSTOMERS",
+ ReferencedColumns: []string{"ID"},
+ },
+ },
+ },
+ },
+ // Create customers table (should be created first)
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "CUSTOMERS",
+ NewTable: &storepb.TableMetadata{
+ Name: "CUSTOMERS",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "NAME", Type: "VARCHAR2(100)", Nullable: false},
+ },
+ },
+ },
+ },
+ },
+ description: "Customers table should be created before orders table due to FK dependency",
+ },
+ {
+ name: "view_depends_on_tables",
+ diff: &schema.MetadataDiff{
+ TableChanges: []*schema.TableDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "USERS",
+ NewTable: &storepb.TableMetadata{
+ Name: "USERS",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "NAME", Type: "VARCHAR2(100)", Nullable: false},
+ },
+ },
+ },
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "ORDERS",
+ NewTable: &storepb.TableMetadata{
+ Name: "ORDERS",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "USER_ID", Type: "NUMBER", Nullable: false},
+ },
+ },
+ },
+ },
+ ViewChanges: []*schema.ViewDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ ViewName: "USER_ORDERS",
+ NewView: &storepb.ViewMetadata{
+ Name: "USER_ORDERS",
+ Definition: "SELECT u.NAME, o.ID FROM USERS u JOIN ORDERS o ON u.ID = o.USER_ID",
+ DependencyColumns: []*storepb.DependencyColumn{
+ {Schema: "TESTUSER", Table: "USERS", Column: "ID"},
+ {Schema: "TESTUSER", Table: "USERS", Column: "NAME"},
+ {Schema: "TESTUSER", Table: "ORDERS", Column: "ID"},
+ {Schema: "TESTUSER", Table: "ORDERS", Column: "USER_ID"},
+ },
+ },
+ },
+ },
+ },
+ description: "View should be created after tables it depends on",
+ },
+ {
+ name: "column_addition_follows_table_dependency",
+ diff: &schema.MetadataDiff{
+ TableChanges: []*schema.TableDiff{
+ // Add column to orders table
+ {
+ Action: schema.MetadataDiffActionAlter,
+ SchemaName: "TESTUSER",
+ TableName: "ORDERS",
+ ColumnChanges: []*schema.ColumnDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ NewColumn: &storepb.ColumnMetadata{
+ Name: "CUSTOMER_REF",
+ Type: "NUMBER",
+ Nullable: true,
+ },
+ },
+ },
+ },
+ // Add column to customers table (should be added first in dependency order)
+ {
+ Action: schema.MetadataDiffActionAlter,
+ SchemaName: "TESTUSER",
+ TableName: "CUSTOMERS",
+ ColumnChanges: []*schema.ColumnDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ NewColumn: &storepb.ColumnMetadata{
+ Name: "STATUS",
+ Type: "VARCHAR2(20)",
+ Nullable: true,
+ },
+ },
+ },
+ },
+ },
+ },
+ description: "Column additions should follow table topological order",
+ },
+ {
+ name: "materialized_view_depends_on_view",
+ diff: &schema.MetadataDiff{
+ TableChanges: []*schema.TableDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "PRODUCTS",
+ NewTable: &storepb.TableMetadata{
+ Name: "PRODUCTS",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "NAME", Type: "VARCHAR2(100)", Nullable: false},
+ {Name: "PRICE", Type: "NUMBER(10,2)", Nullable: false},
+ },
+ },
+ },
+ },
+ ViewChanges: []*schema.ViewDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ ViewName: "EXPENSIVE_PRODUCTS",
+ NewView: &storepb.ViewMetadata{
+ Name: "EXPENSIVE_PRODUCTS",
+ Definition: "SELECT * FROM PRODUCTS WHERE PRICE > 100",
+ DependencyColumns: []*storepb.DependencyColumn{
+ {Schema: "TESTUSER", Table: "PRODUCTS", Column: "ID"},
+ {Schema: "TESTUSER", Table: "PRODUCTS", Column: "NAME"},
+ {Schema: "TESTUSER", Table: "PRODUCTS", Column: "PRICE"},
+ },
+ },
+ },
+ },
+ MaterializedViewChanges: []*schema.MaterializedViewDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ MaterializedViewName: "EXPENSIVE_PRODUCTS_MV",
+ NewMaterializedView: &storepb.MaterializedViewMetadata{
+ Name: "EXPENSIVE_PRODUCTS_MV",
+ Definition: "SELECT NAME FROM EXPENSIVE_PRODUCTS",
+ DependencyColumns: []*storepb.DependencyColumn{
+ {Schema: "TESTUSER", Table: "EXPENSIVE_PRODUCTS", Column: "NAME"},
+ },
+ },
+ },
+ },
+ },
+ description: "Materialized view should be created after view it depends on",
+ },
+ {
+ name: "function_depends_on_table",
+ diff: &schema.MetadataDiff{
+ TableChanges: []*schema.TableDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "EMPLOYEES",
+ NewTable: &storepb.TableMetadata{
+ Name: "EMPLOYEES",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "SALARY", Type: "NUMBER(10,2)", Nullable: false},
+ },
+ },
+ },
+ },
+ FunctionChanges: []*schema.FunctionDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ FunctionName: "GET_AVG_SALARY",
+ NewFunction: &storepb.FunctionMetadata{
+ Name: "GET_AVG_SALARY",
+ Definition: "CREATE OR REPLACE FUNCTION GET_AVG_SALARY RETURN NUMBER IS avg_sal NUMBER; BEGIN SELECT AVG(SALARY) INTO avg_sal FROM EMPLOYEES; RETURN avg_sal; END;",
+ DependencyTables: []*storepb.DependencyTable{
+ {Schema: "TESTUSER", Table: "EMPLOYEES"},
+ },
+ },
+ },
+ },
+ },
+ description: "Function should be created after table it depends on",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result, err := generateMigration(tt.diff)
+ require.NoError(t, err, "Generate migration should not error")
+
+ // Verify the SQL is not empty
+ assert.NotEmpty(t, result, "Generated migration should not be empty")
+
+ // Parse the generated SQL to check order
+ statements := parseStatements(result)
+
+ // Verify topological order based on test case
+ switch tt.name {
+ case "table_depends_on_table_via_foreign_key":
+ // customers table should be created before orders table
+ customersIndex := findStatementIndex(statements, "CREATE TABLE", "CUSTOMERS")
+ ordersIndex := findStatementIndex(statements, "CREATE TABLE", "ORDERS")
+ assert.True(t, customersIndex < ordersIndex,
+ "Customers table should be created before orders table. Got statements: %v", statements)
+
+ case "view_depends_on_tables":
+ // Both tables should be created before the view
+ usersIndex := findStatementIndex(statements, "CREATE TABLE", "USERS")
+ ordersIndex := findStatementIndex(statements, "CREATE TABLE", "ORDERS")
+ viewIndex := findStatementIndex(statements, "CREATE VIEW", "USER_ORDERS")
+ assert.True(t, usersIndex < viewIndex && ordersIndex < viewIndex,
+ "Tables should be created before view. Got statements: %v", statements)
+
+ case "column_addition_follows_table_dependency":
+ // Verify column additions are in the topological order
+ customersAlterIndex := findStatementIndex(statements, "ALTER TABLE", "CUSTOMERS")
+ ordersAlterIndex := findStatementIndex(statements, "ALTER TABLE", "ORDERS")
+ // Both should exist
+ assert.True(t, customersAlterIndex >= 0, "Customers ALTER should exist")
+ assert.True(t, ordersAlterIndex >= 0, "Orders ALTER should exist")
+
+ case "materialized_view_depends_on_view":
+ // Table, then view, then materialized view
+ tableIndex := findStatementIndex(statements, "CREATE TABLE", "PRODUCTS")
+ viewIndex := findStatementIndex(statements, "CREATE VIEW", "EXPENSIVE_PRODUCTS")
+ mvIndex := findStatementIndex(statements, "CREATE MATERIALIZED VIEW", "EXPENSIVE_PRODUCTS_MV")
+ assert.True(t, tableIndex < viewIndex && viewIndex < mvIndex,
+ "Objects should be created in dependency order: table -> view -> materialized view. Got statements: %v", statements)
+
+ case "function_depends_on_table":
+ // Table should be created before function
+ tableIndex := findStatementIndex(statements, "CREATE TABLE", "EMPLOYEES")
+ funcIndex := findStatementIndex(statements, "CREATE OR REPLACE FUNCTION", "GET_AVG_SALARY")
+ if funcIndex == -1 {
+ // Try alternative function creation pattern
+ funcIndex = findStatementIndex(statements, "CREATE FUNCTION", "GET_AVG_SALARY")
+ }
+ assert.True(t, tableIndex < funcIndex,
+ "Table should be created before function. Got statements: %v", statements)
+ default:
+ // No specific verification for this test case
+ }
+ })
+ }
+}
+
+// TestTopologicalOrderWithCycles tests behavior when there are circular dependencies
+func TestTopologicalOrderWithCycles(t *testing.T) {
+ // Create a diff with circular dependency (should fall back to safe order)
+ diff := &schema.MetadataDiff{
+ TableChanges: []*schema.TableDiff{
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "TABLE_A",
+ NewTable: &storepb.TableMetadata{
+ Name: "TABLE_A",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "B_ID", Type: "NUMBER", Nullable: true},
+ },
+ ForeignKeys: []*storepb.ForeignKeyMetadata{
+ {
+ Name: "FK_A_TO_B",
+ Columns: []string{"B_ID"},
+ ReferencedSchema: "TESTUSER",
+ ReferencedTable: "TABLE_B",
+ ReferencedColumns: []string{"ID"},
+ },
+ },
+ },
+ },
+ {
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: "TESTUSER",
+ TableName: "TABLE_B",
+ NewTable: &storepb.TableMetadata{
+ Name: "TABLE_B",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "ID", Type: "NUMBER", Nullable: false},
+ {Name: "A_ID", Type: "NUMBER", Nullable: true},
+ },
+ ForeignKeys: []*storepb.ForeignKeyMetadata{
+ {
+ Name: "FK_B_TO_A",
+ Columns: []string{"A_ID"},
+ ReferencedSchema: "TESTUSER",
+ ReferencedTable: "TABLE_A",
+ ReferencedColumns: []string{"ID"},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ result, err := generateMigration(diff)
+ require.NoError(t, err, "Generate migration should handle cycles gracefully")
+ assert.NotEmpty(t, result, "Generated migration should not be empty even with cycles")
+
+ // With cycles, it should fall back to creating tables without foreign keys first
+ statements := parseStatements(result)
+
+ // Both CREATE TABLE statements should exist
+ tableAIndex := findStatementIndex(statements, "CREATE TABLE", "TABLE_A")
+ tableBIndex := findStatementIndex(statements, "CREATE TABLE", "TABLE_B")
+ assert.True(t, tableAIndex >= 0, "Table A should be created")
+ assert.True(t, tableBIndex >= 0, "Table B should be created")
+
+ // Foreign key constraints should be added later as separate ALTER TABLE statements
+ fkCount := 0
+ for _, stmt := range statements {
+ if strings.Contains(strings.ToUpper(stmt), "ADD CONSTRAINT") && strings.Contains(strings.ToUpper(stmt), "FOREIGN KEY") {
+ fkCount++
+ }
+ }
+ assert.True(t, fkCount >= 2, "Both foreign keys should be added separately, found %d FK statements", fkCount)
+}
+
+// Helper function to parse SQL statements
+func parseStatements(sql string) []string {
+ var statements []string
+ lines := strings.Split(sql, "\n")
+
+ for _, line := range lines {
+ line = strings.TrimSpace(line)
+ if line != "" && !strings.HasPrefix(line, "--") {
+ statements = append(statements, line)
+ }
+ }
+ return statements
+}
+
+// Helper function to find the index of a statement containing specific keywords
+func findStatementIndex(statements []string, stmtType, objectName string) int {
+ for i, stmt := range statements {
+ upperStmt := strings.ToUpper(stmt)
+ upperType := strings.ToUpper(stmtType)
+ upperName := strings.ToUpper(objectName)
+
+ if strings.Contains(upperStmt, upperType) && strings.Contains(upperStmt, upperName) {
+ return i
+ }
+ }
+ return -1
+}
+
+// fixtureSchemaName replaces the throwaway Oracle user this recorder creates, so
+// a re-record does not churn every fixture with a new UUID-derived schema name.
+const fixtureSchemaName = "TESTSCHEMA"
+
+// oracleGeneratedName matches the identifiers Oracle allocates for itself:
+// identity-column sequences and the names it invents for unnamed constraints.
+// Both embed a database object number that depends on allocation order across
+// concurrently recording subtests, so the same schema recorded twice yields
+// different numbers.
+var oracleGeneratedName = regexp.MustCompile(`ISEQ\$\$_[0-9]+|SYS_C[0-9]+`)
+
+//nolint:tparallel
+func TestGenerateMigrationAgainstOracle(t *testing.T) {
+ const record = false
+
+ ctx := context.Background()
+
+ container := testcontainer.SharedOracleContainer(t)
+ systemDB := container.GetDB()
+
+ if record {
+ require.NoError(t, os.MkdirAll(generateMigrationFixtureDir, 0o755))
+ }
+
+ // Oracle Free slows down heavily when too many schema-heavy cases run
+ // concurrently against one container.
+ const maxConcurrent = 4
+ gate := make(chan struct{}, maxConcurrent)
+
+ for _, tc := range generateMigrationCases() {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+ gate <- struct{}{}
+ defer func() { <-gate }()
+
+ testUser := fmt.Sprintf("U_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+ require.NoError(t, createOracleUser(systemDB, testUser))
+
+ driver, err := createOracleDriver(ctx, container.GetHost(), container.GetPort(), testUser)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ require.NoError(t, executeStatements(ctx, driver, tc.initialSchema), "failed to execute initial schema")
+ schemaA, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ require.NoError(t, executeStatements(ctx, driver, tc.migrationDDL), "failed to execute migration DDL")
+ schemaB, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Render the fixture before the round trip below mutates these
+ // messages, but write it only once Oracle has accepted the rollback.
+ var fixture generateMigrationFixture
+ if record {
+ fixture = renderFixture(t, schemaA, schemaB, testUser)
+ }
+
+ verifyRollbackAgainstOracle(ctx, t, driver, schemaA, schemaB)
+
+ if record {
+ out, err := json.MarshalIndent(fixture, "", " ")
+ require.NoError(t, err)
+ require.NoError(t, os.WriteFile(generateMigrationFixturePath(tc.name), append(out, '\n'), 0o644))
+ }
+ })
+ }
+}
+
+// verifyRollbackAgainstOracle is the live check the offline golden test cannot
+// make: generate the rollback from B back to A, execute it, and require that the
+// engine ends up where it started.
+func verifyRollbackAgainstOracle(ctx context.Context, t *testing.T, driver db.Driver, schemaA, schemaB *storepb.DatabaseSchemaMetadata) {
+ t.Helper()
+
+ dbSchemaA := model.NewDatabaseMetadata(schemaA, nil, nil, storepb.Engine_ORACLE, false)
+ dbSchemaB := model.NewDatabaseMetadata(schemaB, nil, nil, storepb.Engine_ORACLE, false)
+
+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_ORACLE, dbSchemaB, dbSchemaA)
+ require.NoError(t, err)
+
+ rollbackDDL, err := schema.GenerateMigration(storepb.Engine_ORACLE, diff)
+ require.NoError(t, err)
+
+ require.NoError(t, executeStatements(ctx, driver, rollbackDDL), "Oracle rejected the generated rollback:\n%s", rollbackDDL)
+
+ schemaC, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ normalizeMetadataForComparison(schemaA)
+ normalizeMetadataForComparison(schemaC)
+ normalizeColumnPositions(schemaA)
+ normalizeColumnPositions(schemaC)
+
+ if mismatch := cmp.Diff(schemaA, schemaC, protocmp.Transform()); mismatch != "" {
+ t.Errorf("rollback did not restore the initial schema (-want +got):\n%s\ngenerated DDL:\n%s", mismatch, rollbackDDL)
+ }
+}
+
+// renderFixture marshals the pair for storage. Both schemas go through one
+// canonicalization pass so an identifier Oracle allocated once carries the same
+// stable name on both sides.
+func renderFixture(t *testing.T, schemaA, schemaB *storepb.DatabaseSchemaMetadata, testUser string) generateMigrationFixture {
+ t.Helper()
+
+ rawA := marshalSchema(t, schemaA, testUser)
+ rawB := marshalSchema(t, schemaB, testUser)
+
+ canonical := canonicalizeGeneratedNames(rawA, rawB)
+ return generateMigrationFixture{
+ SchemaA: json.RawMessage(canonical[0]),
+ SchemaB: json.RawMessage(canonical[1]),
+ }
+}
+
+// marshalSchema renders one synced schema, substituting the throwaway user name.
+func marshalSchema(t *testing.T, metadata *storepb.DatabaseSchemaMetadata, testUser string) string {
+ t.Helper()
+
+ zeroVolatileStats(metadata)
+
+ raw, err := protojson.MarshalOptions{Multiline: true, Indent: " "}.Marshal(metadata)
+ require.NoError(t, err)
+
+ // The user name reaches the metadata as the database name and inside view,
+ // routine and constraint text, so substitute across the whole document.
+ return strings.ReplaceAll(string(raw), strings.ToUpper(testUser), fixtureSchemaName)
+}
+
+// canonicalizeGeneratedNames renumbers Oracle-allocated identifiers by order of
+// first appearance across the documents, so re-recording an unchanged schema
+// produces an unchanged fixture. Prefixes are preserved because the generator
+// keys on them: get_database_definition.go and generate_migration.go both test
+// for "ISEQ$$_" to recognize identity-column sequences.
+func canonicalizeGeneratedNames(docs ...string) []string {
+ canonical := make(map[string]string)
+ seen := make(map[string]int)
+
+ rename := func(match string) string {
+ if replacement, ok := canonical[match]; ok {
+ return replacement
+ }
+ prefix := "SYS_C"
+ if strings.HasPrefix(match, "ISEQ") {
+ prefix = "ISEQ$$_"
+ }
+ seen[prefix]++
+ replacement := fmt.Sprintf("%s%07d", prefix, seen[prefix])
+ canonical[match] = replacement
+ return replacement
+ }
+
+ out := make([]string, 0, len(docs))
+ for _, doc := range docs {
+ out = append(out, oracleGeneratedName.ReplaceAllStringFunc(doc, rename))
+ }
+ return out
+}
+
+// zeroVolatileStats clears the row and byte counts, which differ run to run and
+// are never read by the migration generator.
+func zeroVolatileStats(metadata *storepb.DatabaseSchemaMetadata) {
+ for _, schemaMetadata := range metadata.Schemas {
+ for _, table := range schemaMetadata.Tables {
+ table.DataSize = 0
+ table.IndexSize = 0
+ table.RowCount = 0
+ }
+ }
+}
diff --git a/backend/plugin/schema/oracle/get_database_definition_test.go b/backend/plugin/schema/oracle/get_database_definition_test.go
index 85dc420afa..b2e7b4591c 100644
--- a/backend/plugin/schema/oracle/get_database_definition_test.go
+++ b/backend/plugin/schema/oracle/get_database_definition_test.go
@@ -1,10 +1,17 @@
package oracle
import (
+ "context"
+ "fmt"
+ "strings"
"testing"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/uuid"
"github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/testing/protocmp"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
)
@@ -266,3 +273,194 @@ func TestObjectDefinitionsRegisteredForOracle(t *testing.T) {
})
}
}
+
+// TestGetDatabaseDefinitionWithTestcontainer tests the GetDatabaseDefinition function
+// by creating a schema, getting its definition, recreating it in a new database,
+// and comparing the results.
+//
+//nolint:tparallel
+func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
+ if testing.Short() {
+ t.Skip("Skipping Oracle testcontainer test in short mode")
+ }
+
+ ctx := context.Background()
+
+ container := testcontainer.SharedOracleContainer(t)
+
+ // Get SYSTEM database connection for user management
+ systemDB := container.GetDB()
+
+ // Test cases with various schema configurations
+ testCases := []struct {
+ name string
+ initialSchema string
+ description string
+ }{
+ {
+ name: "basic_tables_and_constraints",
+ initialSchema: `
+CREATE TABLE DEPARTMENTS (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ BUDGET NUMBER(12, 2) DEFAULT 0
+);
+
+CREATE TABLE EMPLOYEES (
+ ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ NAME VARCHAR2(100) NOT NULL,
+ EMAIL VARCHAR2(100) UNIQUE,
+ DEPARTMENT_ID NUMBER,
+ SALARY NUMBER(10, 2),
+ HIRE_DATE DATE DEFAULT SYSDATE,
+ IS_ACTIVE NUMBER(1) DEFAULT 1 CHECK (IS_ACTIVE IN (0, 1)),
+ CONSTRAINT FK_DEPT FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENTS(ID)
+);
+
+CREATE INDEX IDX_EMP_DEPT ON EMPLOYEES(DEPARTMENT_ID);
+CREATE INDEX IDX_EMP_NAME ON EMPLOYEES(NAME);
+`,
+ description: "Basic tables with primary keys, foreign keys, unique constraints, check constraints, and indexes",
+ },
+ {
+ name: "view_with_instead_of_trigger",
+ initialSchema: `
+CREATE TABLE EMPLOYEES (
+ EMP_ID NUMBER PRIMARY KEY,
+ EMAIL VARCHAR2(100)
+);
+
+CREATE VIEW EMPLOYEE_VIEW AS
+SELECT EMP_ID, EMAIL
+FROM EMPLOYEES;
+
+CREATE OR REPLACE TRIGGER EMPLOYEE_VIEW_INSERT_TRG
+INSTEAD OF INSERT ON EMPLOYEE_VIEW
+FOR EACH ROW
+BEGIN
+ INSERT INTO EMPLOYEES (EMP_ID, EMAIL) VALUES (:NEW.EMP_ID, :NEW.EMAIL);
+END;
+/
+`,
+ description: "A view whose INSTEAD OF trigger must survive the definition round trip",
+ },
+ }
+
+ for _, tc := range testCases {
+ tc := tc // Capture range variable
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+
+ // Create unique Oracle user for this test (Oracle users are schemas)
+ // Use UUID to ensure uniqueness and avoid name collisions
+ testUser := fmt.Sprintf("U_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
+ // Create user using shared SYSTEM connection
+ require.NoError(t, createOracleUser(systemDB, testUser))
+
+ // Connect as the test user
+ driver, err := createOracleDriver(ctx, container.GetHost(), container.GetPort(), testUser)
+ require.NoError(t, err)
+ defer driver.Close(ctx)
+
+ // Step 1: Initialize the database schema and use SyncDBSchema to get metadata A
+ err = executeStatements(ctx, driver, tc.initialSchema)
+ require.NoError(t, err, "Failed to execute initial schema")
+
+ // Get schema metadata A
+ metadataA, err := driver.SyncDBSchema(ctx)
+ require.NoError(t, err, "Failed to get initial metadata")
+
+ // Step 2: Call GetDatabaseDefinition to generate the database definition X
+ definition, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, metadataA)
+ require.NoError(t, err, "Failed to generate database definition")
+ require.NotEmpty(t, definition, "Generated definition should not be empty")
+
+ // Log the generated definition for debugging
+ t.Logf("Generated definition:\n%s", definition)
+
+ // Step 3: Create a new user and run the database definition X
+ // Create a second unique user for recreation test
+ testUser2 := fmt.Sprintf("U_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
+ // Create the second user
+ require.NoError(t, createOracleUser(systemDB, testUser2))
+
+ // Connect as the second test user
+ driver2, err := createOracleDriver(ctx, container.GetHost(), container.GetPort(), testUser2)
+ require.NoError(t, err)
+ defer driver2.Close(ctx)
+
+ // Execute the generated definition in the new user's schema
+ err = executeStatements(ctx, driver2, definition)
+ require.NoError(t, err, "Failed to execute generated definition")
+
+ // Get metadata B after recreating from definition
+ metadataB, err := driver2.SyncDBSchema(ctx)
+ require.NoError(t, err, "Failed to get metadata after recreation")
+
+ // Step 4: Compare the database metadata A and B, should be the same
+ normalizeMetadataForComparison(metadataA)
+ normalizeMetadataForComparison(metadataB)
+
+ // Normalize column positions to 0 before comparison
+ normalizeColumnPositions(metadataA)
+ normalizeColumnPositions(metadataB)
+
+ // Additional normalization for Oracle-specific issues
+ normalizeOracleMetadata(metadataA)
+ normalizeOracleMetadata(metadataB)
+
+ // Use cmp with protocmp for proto message comparison
+ if diff := cmp.Diff(metadataA, metadataB, protocmp.Transform()); diff != "" {
+ t.Errorf("Schema mismatch after recreation (-original +recreated):\n%s", diff)
+ }
+ })
+ }
+}
+
+// normalizeOracleMetadata handles Oracle-specific normalization for metadata comparison
+func normalizeOracleMetadata(metadata *storepb.DatabaseSchemaMetadata) {
+ for _, schema := range metadata.Schemas {
+ // A view's dependency columns record their owning Oracle user, and this
+ // test recreates the schema under a second user by construction, so the
+ // two can never match.
+ for _, view := range schema.Views {
+ for _, dependency := range view.DependencyColumns {
+ dependency.Schema = ""
+ }
+ }
+ for _, view := range schema.MaterializedViews {
+ for _, dependency := range view.DependencyColumns {
+ dependency.Schema = ""
+ }
+ }
+
+ for _, table := range schema.Tables {
+ for _, column := range table.Columns {
+ // Normalize system-generated sequence references in default expressions
+ if column.Default != "" {
+ // If it's a system-generated sequence, remove the default expression
+ // since these sequences can't be manually recreated with the same name
+ if strings.Contains(column.Default, "ISEQ$$_") {
+ column.Default = ""
+ }
+ }
+
+ // Clear collation information as we skip it in DDL generation
+ column.Collation = ""
+
+ // Normalize NVARCHAR2 type size differences
+ // Oracle stores NVARCHAR2 sizes in max bytes, but DDL defines in characters
+ if strings.HasPrefix(column.Type, "NVARCHAR2") {
+ // Extract the size and convert to character count
+ if strings.Contains(column.Type, "(") && strings.Contains(column.Type, ")") {
+ // For simplicity, we'll normalize all NVARCHAR2 types to just NVARCHAR2
+ // This avoids issues with byte vs character counting
+ column.Type = "NVARCHAR2"
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/backend/plugin/schema/oracle/get_database_definition_testcontainer_test.go b/backend/plugin/schema/oracle/get_database_definition_testcontainer_test.go
deleted file mode 100644
index 1698abe2ae..0000000000
--- a/backend/plugin/schema/oracle/get_database_definition_testcontainer_test.go
+++ /dev/null
@@ -1,208 +0,0 @@
-package oracle
-
-import (
- "context"
- "fmt"
- "strings"
- "testing"
-
- "github.com/google/go-cmp/cmp"
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
- "google.golang.org/protobuf/testing/protocmp"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-// TestGetDatabaseDefinitionWithTestcontainer tests the GetDatabaseDefinition function
-// by creating a schema, getting its definition, recreating it in a new database,
-// and comparing the results.
-//
-//nolint:tparallel
-func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
- if testing.Short() {
- t.Skip("Skipping Oracle testcontainer test in short mode")
- }
-
- ctx := context.Background()
-
- container := testcontainer.SharedOracleContainer(t)
-
- // Get SYSTEM database connection for user management
- systemDB := container.GetDB()
-
- // Test cases with various schema configurations
- testCases := []struct {
- name string
- initialSchema string
- description string
- }{
- {
- name: "basic_tables_and_constraints",
- initialSchema: `
-CREATE TABLE DEPARTMENTS (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- BUDGET NUMBER(12, 2) DEFAULT 0
-);
-
-CREATE TABLE EMPLOYEES (
- ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
- NAME VARCHAR2(100) NOT NULL,
- EMAIL VARCHAR2(100) UNIQUE,
- DEPARTMENT_ID NUMBER,
- SALARY NUMBER(10, 2),
- HIRE_DATE DATE DEFAULT SYSDATE,
- IS_ACTIVE NUMBER(1) DEFAULT 1 CHECK (IS_ACTIVE IN (0, 1)),
- CONSTRAINT FK_DEPT FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENTS(ID)
-);
-
-CREATE INDEX IDX_EMP_DEPT ON EMPLOYEES(DEPARTMENT_ID);
-CREATE INDEX IDX_EMP_NAME ON EMPLOYEES(NAME);
-`,
- description: "Basic tables with primary keys, foreign keys, unique constraints, check constraints, and indexes",
- },
- {
- name: "view_with_instead_of_trigger",
- initialSchema: `
-CREATE TABLE EMPLOYEES (
- EMP_ID NUMBER PRIMARY KEY,
- EMAIL VARCHAR2(100)
-);
-
-CREATE VIEW EMPLOYEE_VIEW AS
-SELECT EMP_ID, EMAIL
-FROM EMPLOYEES;
-
-CREATE OR REPLACE TRIGGER EMPLOYEE_VIEW_INSERT_TRG
-INSTEAD OF INSERT ON EMPLOYEE_VIEW
-FOR EACH ROW
-BEGIN
- INSERT INTO EMPLOYEES (EMP_ID, EMAIL) VALUES (:NEW.EMP_ID, :NEW.EMAIL);
-END;
-/
-`,
- description: "A view whose INSTEAD OF trigger must survive the definition round trip",
- },
- }
-
- for _, tc := range testCases {
- tc := tc // Capture range variable
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
-
- // Create unique Oracle user for this test (Oracle users are schemas)
- // Use UUID to ensure uniqueness and avoid name collisions
- testUser := fmt.Sprintf("U_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
-
- // Create user using shared SYSTEM connection
- require.NoError(t, createOracleUser(systemDB, testUser))
-
- // Connect as the test user
- driver, err := createOracleDriver(ctx, container.GetHost(), container.GetPort(), testUser)
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- // Step 1: Initialize the database schema and use SyncDBSchema to get metadata A
- err = executeStatements(ctx, driver, tc.initialSchema)
- require.NoError(t, err, "Failed to execute initial schema")
-
- // Get schema metadata A
- metadataA, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err, "Failed to get initial metadata")
-
- // Step 2: Call GetDatabaseDefinition to generate the database definition X
- definition, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, metadataA)
- require.NoError(t, err, "Failed to generate database definition")
- require.NotEmpty(t, definition, "Generated definition should not be empty")
-
- // Log the generated definition for debugging
- t.Logf("Generated definition:\n%s", definition)
-
- // Step 3: Create a new user and run the database definition X
- // Create a second unique user for recreation test
- testUser2 := fmt.Sprintf("U_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
-
- // Create the second user
- require.NoError(t, createOracleUser(systemDB, testUser2))
-
- // Connect as the second test user
- driver2, err := createOracleDriver(ctx, container.GetHost(), container.GetPort(), testUser2)
- require.NoError(t, err)
- defer driver2.Close(ctx)
-
- // Execute the generated definition in the new user's schema
- err = executeStatements(ctx, driver2, definition)
- require.NoError(t, err, "Failed to execute generated definition")
-
- // Get metadata B after recreating from definition
- metadataB, err := driver2.SyncDBSchema(ctx)
- require.NoError(t, err, "Failed to get metadata after recreation")
-
- // Step 4: Compare the database metadata A and B, should be the same
- normalizeMetadataForComparison(metadataA)
- normalizeMetadataForComparison(metadataB)
-
- // Normalize column positions to 0 before comparison
- normalizeColumnPositions(metadataA)
- normalizeColumnPositions(metadataB)
-
- // Additional normalization for Oracle-specific issues
- normalizeOracleMetadata(metadataA)
- normalizeOracleMetadata(metadataB)
-
- // Use cmp with protocmp for proto message comparison
- if diff := cmp.Diff(metadataA, metadataB, protocmp.Transform()); diff != "" {
- t.Errorf("Schema mismatch after recreation (-original +recreated):\n%s", diff)
- }
- })
- }
-}
-
-// normalizeOracleMetadata handles Oracle-specific normalization for metadata comparison
-func normalizeOracleMetadata(metadata *storepb.DatabaseSchemaMetadata) {
- for _, schema := range metadata.Schemas {
- // A view's dependency columns record their owning Oracle user, and this
- // test recreates the schema under a second user by construction, so the
- // two can never match.
- for _, view := range schema.Views {
- for _, dependency := range view.DependencyColumns {
- dependency.Schema = ""
- }
- }
- for _, view := range schema.MaterializedViews {
- for _, dependency := range view.DependencyColumns {
- dependency.Schema = ""
- }
- }
-
- for _, table := range schema.Tables {
- for _, column := range table.Columns {
- // Normalize system-generated sequence references in default expressions
- if column.Default != "" {
- // If it's a system-generated sequence, remove the default expression
- // since these sequences can't be manually recreated with the same name
- if strings.Contains(column.Default, "ISEQ$$_") {
- column.Default = ""
- }
- }
-
- // Clear collation information as we skip it in DDL generation
- column.Collation = ""
-
- // Normalize NVARCHAR2 type size differences
- // Oracle stores NVARCHAR2 sizes in max bytes, but DDL defines in characters
- if strings.HasPrefix(column.Type, "NVARCHAR2") {
- // Extract the size and convert to character count
- if strings.Contains(column.Type, "(") && strings.Contains(column.Type, ")") {
- // For simplicity, we'll normalize all NVARCHAR2 types to just NVARCHAR2
- // This avoids issues with byte vs character counting
- column.Type = "NVARCHAR2"
- }
- }
- }
- }
- }
-}
diff --git a/backend/plugin/schema/oracle/topological_order_test.go b/backend/plugin/schema/oracle/topological_order_test.go
deleted file mode 100644
index 1b25908de9..0000000000
--- a/backend/plugin/schema/oracle/topological_order_test.go
+++ /dev/null
@@ -1,400 +0,0 @@
-package oracle
-
-import (
- "strings"
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-// TestTopologicalOrderCreateObjects tests that objects are created in the correct dependency order
-func TestTopologicalOrderCreateObjects(t *testing.T) {
- tests := []struct {
- name string
- diff *schema.MetadataDiff
- description string
- }{
- {
- name: "table_depends_on_table_via_foreign_key",
- diff: &schema.MetadataDiff{
- TableChanges: []*schema.TableDiff{
- // Create orders table that depends on customers table via FK
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "ORDERS",
- NewTable: &storepb.TableMetadata{
- Name: "ORDERS",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "CUSTOMER_ID", Type: "NUMBER", Nullable: false},
- },
- ForeignKeys: []*storepb.ForeignKeyMetadata{
- {
- Name: "FK_ORDERS_CUSTOMER",
- Columns: []string{"CUSTOMER_ID"},
- ReferencedSchema: "TESTUSER",
- ReferencedTable: "CUSTOMERS",
- ReferencedColumns: []string{"ID"},
- },
- },
- },
- },
- // Create customers table (should be created first)
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "CUSTOMERS",
- NewTable: &storepb.TableMetadata{
- Name: "CUSTOMERS",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "NAME", Type: "VARCHAR2(100)", Nullable: false},
- },
- },
- },
- },
- },
- description: "Customers table should be created before orders table due to FK dependency",
- },
- {
- name: "view_depends_on_tables",
- diff: &schema.MetadataDiff{
- TableChanges: []*schema.TableDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "USERS",
- NewTable: &storepb.TableMetadata{
- Name: "USERS",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "NAME", Type: "VARCHAR2(100)", Nullable: false},
- },
- },
- },
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "ORDERS",
- NewTable: &storepb.TableMetadata{
- Name: "ORDERS",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "USER_ID", Type: "NUMBER", Nullable: false},
- },
- },
- },
- },
- ViewChanges: []*schema.ViewDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- ViewName: "USER_ORDERS",
- NewView: &storepb.ViewMetadata{
- Name: "USER_ORDERS",
- Definition: "SELECT u.NAME, o.ID FROM USERS u JOIN ORDERS o ON u.ID = o.USER_ID",
- DependencyColumns: []*storepb.DependencyColumn{
- {Schema: "TESTUSER", Table: "USERS", Column: "ID"},
- {Schema: "TESTUSER", Table: "USERS", Column: "NAME"},
- {Schema: "TESTUSER", Table: "ORDERS", Column: "ID"},
- {Schema: "TESTUSER", Table: "ORDERS", Column: "USER_ID"},
- },
- },
- },
- },
- },
- description: "View should be created after tables it depends on",
- },
- {
- name: "column_addition_follows_table_dependency",
- diff: &schema.MetadataDiff{
- TableChanges: []*schema.TableDiff{
- // Add column to orders table
- {
- Action: schema.MetadataDiffActionAlter,
- SchemaName: "TESTUSER",
- TableName: "ORDERS",
- ColumnChanges: []*schema.ColumnDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- NewColumn: &storepb.ColumnMetadata{
- Name: "CUSTOMER_REF",
- Type: "NUMBER",
- Nullable: true,
- },
- },
- },
- },
- // Add column to customers table (should be added first in dependency order)
- {
- Action: schema.MetadataDiffActionAlter,
- SchemaName: "TESTUSER",
- TableName: "CUSTOMERS",
- ColumnChanges: []*schema.ColumnDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- NewColumn: &storepb.ColumnMetadata{
- Name: "STATUS",
- Type: "VARCHAR2(20)",
- Nullable: true,
- },
- },
- },
- },
- },
- },
- description: "Column additions should follow table topological order",
- },
- {
- name: "materialized_view_depends_on_view",
- diff: &schema.MetadataDiff{
- TableChanges: []*schema.TableDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "PRODUCTS",
- NewTable: &storepb.TableMetadata{
- Name: "PRODUCTS",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "NAME", Type: "VARCHAR2(100)", Nullable: false},
- {Name: "PRICE", Type: "NUMBER(10,2)", Nullable: false},
- },
- },
- },
- },
- ViewChanges: []*schema.ViewDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- ViewName: "EXPENSIVE_PRODUCTS",
- NewView: &storepb.ViewMetadata{
- Name: "EXPENSIVE_PRODUCTS",
- Definition: "SELECT * FROM PRODUCTS WHERE PRICE > 100",
- DependencyColumns: []*storepb.DependencyColumn{
- {Schema: "TESTUSER", Table: "PRODUCTS", Column: "ID"},
- {Schema: "TESTUSER", Table: "PRODUCTS", Column: "NAME"},
- {Schema: "TESTUSER", Table: "PRODUCTS", Column: "PRICE"},
- },
- },
- },
- },
- MaterializedViewChanges: []*schema.MaterializedViewDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- MaterializedViewName: "EXPENSIVE_PRODUCTS_MV",
- NewMaterializedView: &storepb.MaterializedViewMetadata{
- Name: "EXPENSIVE_PRODUCTS_MV",
- Definition: "SELECT NAME FROM EXPENSIVE_PRODUCTS",
- DependencyColumns: []*storepb.DependencyColumn{
- {Schema: "TESTUSER", Table: "EXPENSIVE_PRODUCTS", Column: "NAME"},
- },
- },
- },
- },
- },
- description: "Materialized view should be created after view it depends on",
- },
- {
- name: "function_depends_on_table",
- diff: &schema.MetadataDiff{
- TableChanges: []*schema.TableDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "EMPLOYEES",
- NewTable: &storepb.TableMetadata{
- Name: "EMPLOYEES",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "SALARY", Type: "NUMBER(10,2)", Nullable: false},
- },
- },
- },
- },
- FunctionChanges: []*schema.FunctionDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- FunctionName: "GET_AVG_SALARY",
- NewFunction: &storepb.FunctionMetadata{
- Name: "GET_AVG_SALARY",
- Definition: "CREATE OR REPLACE FUNCTION GET_AVG_SALARY RETURN NUMBER IS avg_sal NUMBER; BEGIN SELECT AVG(SALARY) INTO avg_sal FROM EMPLOYEES; RETURN avg_sal; END;",
- DependencyTables: []*storepb.DependencyTable{
- {Schema: "TESTUSER", Table: "EMPLOYEES"},
- },
- },
- },
- },
- },
- description: "Function should be created after table it depends on",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result, err := generateMigration(tt.diff)
- require.NoError(t, err, "Generate migration should not error")
-
- // Verify the SQL is not empty
- assert.NotEmpty(t, result, "Generated migration should not be empty")
-
- // Parse the generated SQL to check order
- statements := parseStatements(result)
-
- // Verify topological order based on test case
- switch tt.name {
- case "table_depends_on_table_via_foreign_key":
- // customers table should be created before orders table
- customersIndex := findStatementIndex(statements, "CREATE TABLE", "CUSTOMERS")
- ordersIndex := findStatementIndex(statements, "CREATE TABLE", "ORDERS")
- assert.True(t, customersIndex < ordersIndex,
- "Customers table should be created before orders table. Got statements: %v", statements)
-
- case "view_depends_on_tables":
- // Both tables should be created before the view
- usersIndex := findStatementIndex(statements, "CREATE TABLE", "USERS")
- ordersIndex := findStatementIndex(statements, "CREATE TABLE", "ORDERS")
- viewIndex := findStatementIndex(statements, "CREATE VIEW", "USER_ORDERS")
- assert.True(t, usersIndex < viewIndex && ordersIndex < viewIndex,
- "Tables should be created before view. Got statements: %v", statements)
-
- case "column_addition_follows_table_dependency":
- // Verify column additions are in the topological order
- customersAlterIndex := findStatementIndex(statements, "ALTER TABLE", "CUSTOMERS")
- ordersAlterIndex := findStatementIndex(statements, "ALTER TABLE", "ORDERS")
- // Both should exist
- assert.True(t, customersAlterIndex >= 0, "Customers ALTER should exist")
- assert.True(t, ordersAlterIndex >= 0, "Orders ALTER should exist")
-
- case "materialized_view_depends_on_view":
- // Table, then view, then materialized view
- tableIndex := findStatementIndex(statements, "CREATE TABLE", "PRODUCTS")
- viewIndex := findStatementIndex(statements, "CREATE VIEW", "EXPENSIVE_PRODUCTS")
- mvIndex := findStatementIndex(statements, "CREATE MATERIALIZED VIEW", "EXPENSIVE_PRODUCTS_MV")
- assert.True(t, tableIndex < viewIndex && viewIndex < mvIndex,
- "Objects should be created in dependency order: table -> view -> materialized view. Got statements: %v", statements)
-
- case "function_depends_on_table":
- // Table should be created before function
- tableIndex := findStatementIndex(statements, "CREATE TABLE", "EMPLOYEES")
- funcIndex := findStatementIndex(statements, "CREATE OR REPLACE FUNCTION", "GET_AVG_SALARY")
- if funcIndex == -1 {
- // Try alternative function creation pattern
- funcIndex = findStatementIndex(statements, "CREATE FUNCTION", "GET_AVG_SALARY")
- }
- assert.True(t, tableIndex < funcIndex,
- "Table should be created before function. Got statements: %v", statements)
- default:
- // No specific verification for this test case
- }
- })
- }
-}
-
-// TestTopologicalOrderWithCycles tests behavior when there are circular dependencies
-func TestTopologicalOrderWithCycles(t *testing.T) {
- // Create a diff with circular dependency (should fall back to safe order)
- diff := &schema.MetadataDiff{
- TableChanges: []*schema.TableDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "TABLE_A",
- NewTable: &storepb.TableMetadata{
- Name: "TABLE_A",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "B_ID", Type: "NUMBER", Nullable: true},
- },
- ForeignKeys: []*storepb.ForeignKeyMetadata{
- {
- Name: "FK_A_TO_B",
- Columns: []string{"B_ID"},
- ReferencedSchema: "TESTUSER",
- ReferencedTable: "TABLE_B",
- ReferencedColumns: []string{"ID"},
- },
- },
- },
- },
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "TESTUSER",
- TableName: "TABLE_B",
- NewTable: &storepb.TableMetadata{
- Name: "TABLE_B",
- Columns: []*storepb.ColumnMetadata{
- {Name: "ID", Type: "NUMBER", Nullable: false},
- {Name: "A_ID", Type: "NUMBER", Nullable: true},
- },
- ForeignKeys: []*storepb.ForeignKeyMetadata{
- {
- Name: "FK_B_TO_A",
- Columns: []string{"A_ID"},
- ReferencedSchema: "TESTUSER",
- ReferencedTable: "TABLE_A",
- ReferencedColumns: []string{"ID"},
- },
- },
- },
- },
- },
- }
-
- result, err := generateMigration(diff)
- require.NoError(t, err, "Generate migration should handle cycles gracefully")
- assert.NotEmpty(t, result, "Generated migration should not be empty even with cycles")
-
- // With cycles, it should fall back to creating tables without foreign keys first
- statements := parseStatements(result)
-
- // Both CREATE TABLE statements should exist
- tableAIndex := findStatementIndex(statements, "CREATE TABLE", "TABLE_A")
- tableBIndex := findStatementIndex(statements, "CREATE TABLE", "TABLE_B")
- assert.True(t, tableAIndex >= 0, "Table A should be created")
- assert.True(t, tableBIndex >= 0, "Table B should be created")
-
- // Foreign key constraints should be added later as separate ALTER TABLE statements
- fkCount := 0
- for _, stmt := range statements {
- if strings.Contains(strings.ToUpper(stmt), "ADD CONSTRAINT") && strings.Contains(strings.ToUpper(stmt), "FOREIGN KEY") {
- fkCount++
- }
- }
- assert.True(t, fkCount >= 2, "Both foreign keys should be added separately, found %d FK statements", fkCount)
-}
-
-// Helper function to parse SQL statements
-func parseStatements(sql string) []string {
- var statements []string
- lines := strings.Split(sql, "\n")
-
- for _, line := range lines {
- line = strings.TrimSpace(line)
- if line != "" && !strings.HasPrefix(line, "--") {
- statements = append(statements, line)
- }
- }
- return statements
-}
-
-// Helper function to find the index of a statement containing specific keywords
-func findStatementIndex(statements []string, stmtType, objectName string) int {
- for i, stmt := range statements {
- upperStmt := strings.ToUpper(stmt)
- upperType := strings.ToUpper(stmtType)
- upperName := strings.ToUpper(objectName)
-
- if strings.Contains(upperStmt, upperType) && strings.Contains(upperStmt, upperName) {
- return i
- }
- }
- return -1
-}
diff --git a/backend/plugin/schema/pg/check_constraint_order_test.go b/backend/plugin/schema/pg/check_constraint_order_test.go
deleted file mode 100644
index b17eb56cd0..0000000000
--- a/backend/plugin/schema/pg/check_constraint_order_test.go
+++ /dev/null
@@ -1,76 +0,0 @@
-package pg
-
-import (
- "testing"
-
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-// TestCheckConstraintOrderStability tests that CHECK constraints maintain consistent order
-// when generating SDL multiple times from the same metadata
-func TestCheckConstraintOrderStability(t *testing.T) {
- // Simulate database metadata with multiple CHECK constraints
- // (similar to the packages_debian_group_distributions table shown in the screenshot)
- dbMetadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "packages_debian_group_distributions",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "bigint", Nullable: false},
- {Name: "suite", Type: "character varying(255)", Nullable: false},
- {Name: "codename", Type: "character varying(255)", Nullable: false},
- {Name: "signed_file", Type: "text", Nullable: true},
- {Name: "description", Type: "character varying(255)", Nullable: true},
- {Name: "origin", Type: "character varying(255)", Nullable: true},
- {Name: "file", Type: "character varying(255)", Nullable: true},
- {Name: "label", Type: "character varying(255)", Nullable: true},
- {Name: "version", Type: "character varying(255)", Nullable: true},
- {Name: "file_signature", Type: "character varying(4096)", Nullable: true},
- },
- CheckConstraints: []*storepb.CheckConstraintMetadata{
- {Name: "check_e7c928a24b", Expression: "(char_length((suite)::text) <= 255)"},
- {Name: "check_590e18405a", Expression: "(char_length((codename)::text) <= 255)"},
- {Name: "check_0007e0bf61", Expression: "(char_length(signed_file) <= 255)"},
- {Name: "check_310ac457b8", Expression: "(char_length((description)::text) <= 255)"},
- {Name: "check_3d6f87fc31", Expression: "(char_length((file_signature)::text) <= 4096)"},
- {Name: "check_3fdadf4a0c", Expression: "(char_length((version)::text) <= 255)"},
- {Name: "check_b057cd840a", Expression: "(char_length((origin)::text) <= 255)"},
- {Name: "check_be5ed8d307", Expression: "(char_length((file)::text) <= 255)"},
- {Name: "check_d3244bfc0b", Expression: "(char_length((label)::text) <= 255)"},
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SkipBackupSchema: false,
- PrintHeader: false,
- SDLFormat: true,
- }
-
- // Generate SDL first time
- sdl1, err := GetDatabaseDefinition(ctx, dbMetadata)
- require.NoError(t, err)
- require.NotEmpty(t, sdl1)
-
- t.Logf("First SDL generation:\n%s", sdl1)
-
- // Generate SDL second time from the same metadata
- sdl2, err := GetDatabaseDefinition(ctx, dbMetadata)
- require.NoError(t, err)
- require.NotEmpty(t, sdl2)
-
- t.Logf("Second SDL generation:\n%s", sdl2)
-
- // The two SDL outputs should be identical
- require.Equal(t, sdl1, sdl2, "SDL generation should be deterministic - CHECK constraint order should be stable")
-}
diff --git a/backend/plugin/schema/pg/composite_type_migration_test.go b/backend/plugin/schema/pg/composite_type_migration_test.go
deleted file mode 100644
index a89953aa93..0000000000
--- a/backend/plugin/schema/pg/composite_type_migration_test.go
+++ /dev/null
@@ -1,1578 +0,0 @@
-package pg
-
-import (
- "strings"
- "testing"
-
- "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 newPGDatabaseMetadataWithCompositeTypes(compositeTypes []*storepb.CompositeTypeMetadata) *model.DatabaseMetadata {
- return model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- CompositeTypes: compositeTypes,
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-}
-
-func TestPGMetadataDiffCreateCompositeTypesInDependencyOrder(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes(nil)
- // aa_nested sorts before zz_base alphabetically but depends on it.
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "aa_nested",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "home", Type: "public.zz_base"},
- },
- },
- {
- Name: "zz_base",
- Comment: "base type",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text", Collation: `"C"`, Comment: "street line"},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Contains(t, sql, `CREATE TYPE "public"."zz_base" AS (`)
- require.Contains(t, sql, `"street" text COLLATE "C"`)
- require.Contains(t, sql, `CREATE TYPE "public"."aa_nested" AS (`)
- require.Contains(t, sql, `COMMENT ON TYPE "public"."zz_base" IS 'base type';`)
- require.Contains(t, sql, `COMMENT ON COLUMN "public"."zz_base"."street" IS 'street line';`)
- require.Less(t,
- strings.Index(sql, `CREATE TYPE "public"."zz_base"`),
- strings.Index(sql, `CREATE TYPE "public"."aa_nested"`),
- "referenced composite must be created before its dependent")
-}
-
-func TestPGMetadataDiffDropCompositeTypesInReverseDependencyOrder(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "zz_base",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- {
- Name: "aa_nested",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "home", Type: "public.zz_base"},
- },
- },
- })
- target := newPGDatabaseMetadataWithCompositeTypes(nil)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Contains(t, sql, `DROP TYPE "public"."aa_nested";`)
- require.Contains(t, sql, `DROP TYPE "public"."zz_base";`)
- require.Less(t,
- strings.Index(sql, `DROP TYPE "public"."aa_nested"`),
- strings.Index(sql, `DROP TYPE "public"."zz_base"`),
- "dependent composite must be dropped before the composite it references")
-}
-
-func TestPGMetadataDiffCompositeDropsPrecedeEnumDrops(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "status", Values: []string{"a", "b"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "uses_status",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "s", Type: "public.status"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := newPGDatabaseMetadataWithCompositeTypes(nil)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Less(t,
- strings.Index(sql, `DROP TYPE "public"."uses_status"`),
- strings.Index(sql, `DROP TYPE "public"."status"`),
- "composite referencing an enum must be dropped before the enum")
-}
-
-func TestPGMetadataDiffAlterCompositeTypeAttributes(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- {Name: "city", Type: "character varying(50)"},
- {Name: "dropped_attr", Type: "integer"},
- },
- },
- })
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- {Name: "city", Type: "character varying(100)"},
- {Name: "zip", Type: "text", Collation: `"C"`},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Contains(t, sql, `ALTER TYPE "public"."addr" DROP ATTRIBUTE "dropped_attr";`)
- require.Contains(t, sql, `ALTER TYPE "public"."addr" ADD ATTRIBUTE "zip" text COLLATE "C";`)
- require.Contains(t, sql, `ALTER TYPE "public"."addr" ALTER ATTRIBUTE "city" TYPE character varying(100);`)
- require.NotContains(t, sql, `"street"`, "unchanged attribute must not produce DDL")
-}
-
-func TestPGMetadataDiffCompositeTypeReorderOnlyWarns(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "a", Type: "text"},
- {Name: "b", Type: "integer"},
- },
- },
- })
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "b", Type: "integer"},
- {Name: "a", Type: "text"},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Contains(t, sql, `-- WARNING: PostgreSQL does not support reordering the attributes of "public"."addr"`)
- require.NotContains(t, sql, "ALTER TYPE", "reorder-only change must not produce attribute DDL")
- require.NotContains(t, sql, "DROP TYPE")
-}
-
-func TestPGMetadataDiffCompositeTypeCommentChanges(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Comment: "old type comment",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text", Comment: "old attr comment"},
- },
- },
- })
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Comment: "new type comment",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text", Comment: "new attr comment"},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Contains(t, sql, `COMMENT ON TYPE "public"."addr" IS 'new type comment';`)
- require.Contains(t, sql, `COMMENT ON COLUMN "public"."addr"."street" IS 'new attr comment';`)
- require.NotContains(t, sql, "ALTER TYPE", "comment-only change must not produce attribute DDL")
-}
-
-func TestPGMetadataDiffIdenticalCompositeTypesProduceNoMigration(t *testing.T) {
- composites := []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Comment: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text", Collation: `"C"`, Comment: "s"},
- },
- },
- }
- source := newPGDatabaseMetadataWithCompositeTypes(composites)
- target := newPGDatabaseMetadataWithCompositeTypes(composites)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Empty(t, sql)
-}
-
-func TestPGMetadataDiffCompositeDropDeferredAfterColumnRetype(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "public.addr", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- dropIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
- alterIdx := strings.Index(sql, `ALTER COLUMN "c"`)
- require.GreaterOrEqual(t, dropIdx, 0, "composite type must still be dropped: %s", sql)
- require.GreaterOrEqual(t, alterIdx, 0, "column must be retyped: %s", sql)
- require.Greater(t, dropIdx, alterIdx,
- "composite drop must run after the column retype that releases it: %s", sql)
-}
-
-func TestPGMetadataDiffSchemaDropIncludesCompositeTypes(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- {
- Name: "s",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- dropTypeIdx := strings.Index(sql, `DROP TYPE "s"."addr"`)
- dropSchemaIdx := strings.Index(sql, `DROP SCHEMA`)
- require.GreaterOrEqual(t, dropTypeIdx, 0, "schema drop must drop its composite types first: %s", sql)
- require.GreaterOrEqual(t, dropSchemaIdx, 0)
- require.Less(t, dropTypeIdx, dropSchemaIdx, "composite type drop must precede DROP SCHEMA: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeTypeMiddleInsertWarns(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "a", Type: "text"},
- {Name: "b", Type: "text"},
- },
- },
- })
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "a", Type: "text"},
- {Name: "x", Type: "text"},
- {Name: "b", Type: "text"},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Contains(t, sql, `ALTER TYPE "public"."addr" ADD ATTRIBUTE "x" text;`)
- require.Contains(t, sql, `-- WARNING: PostgreSQL does not support reordering the attributes of "public"."addr"`,
- "middle insertion is not achievable via ADD ATTRIBUTE and must warn: %s", sql)
-}
-
-func TestPGMetadataDiffDeferredCompositeDefersReferencedEnumDrop(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "public.addr", Nullable: true},
- },
- },
- },
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "status", Values: []string{"a", "b"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "s", Type: "public.status"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
- compositeIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
- enumIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
- require.GreaterOrEqual(t, retypeIdx, 0, "column must be retyped: %s", sql)
- require.GreaterOrEqual(t, compositeIdx, 0, "composite must be dropped: %s", sql)
- require.GreaterOrEqual(t, enumIdx, 0, "enum must be dropped: %s", sql)
- require.Greater(t, compositeIdx, retypeIdx, "deferred composite drop must follow the retype: %s", sql)
- require.Greater(t, enumIdx, compositeIdx, "enum referenced by the deferred composite must drop after it: %s", sql)
-}
-
-func TestPGMetadataDiffSchemaDropDeferredWhenCompositeReleasedByRetype(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "s.addr", Nullable: true},
- },
- },
- },
- },
- {
- Name: "s",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
- dropTypeIdx := strings.Index(sql, `DROP TYPE "s"."addr"`)
- dropSchemaIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "s"`)
- require.GreaterOrEqual(t, retypeIdx, 0, "column must be retyped: %s", sql)
- require.GreaterOrEqual(t, dropTypeIdx, 0, "composite in dropped schema must be dropped: %s", sql)
- require.GreaterOrEqual(t, dropSchemaIdx, 0, "schema must be dropped: %s", sql)
- require.Greater(t, dropTypeIdx, retypeIdx, "schema-owned composite drop must follow the retype releasing it: %s", sql)
- require.Greater(t, dropSchemaIdx, dropTypeIdx, "DROP SCHEMA must follow its composite drop: %s", sql)
-}
-
-func TestPGMetadataDiffSchemaDropDeferredWhenDeferredCompositeReferencesItsEnum(t *testing.T) {
- // public.addr is deferred (public.t.c retyped away from it) and references
- // s.status; schema s (containing only that enum) must defer its drop too.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "public.addr", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "st", Type: "s.status"},
- },
- },
- },
- },
- {
- Name: "s",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "status", Values: []string{"a"}},
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- compositeIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
- enumIdx := strings.Index(sql, `DROP TYPE "s"."status"`)
- schemaIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "s"`)
- require.GreaterOrEqual(t, compositeIdx, 0, "deferred composite must be dropped: %s", sql)
- require.GreaterOrEqual(t, enumIdx, 0, "enum in dropped schema must be dropped: %s", sql)
- require.GreaterOrEqual(t, schemaIdx, 0, "schema must be dropped: %s", sql)
- require.Greater(t, enumIdx, compositeIdx,
- "enum referenced by the deferred composite must drop after it, even inside a schema drop: %s", sql)
-}
-
-func TestPGMetadataDiffDeferredSchemaCompositeDefersReferencedEnumDrop(t *testing.T) {
- // Schema s is deferred (public.t.c retyped away from s.addr); s.addr
- // references public.status, a top-level dropped enum, which therefore
- // must also defer until after the schema drop removes s.addr.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "s.addr", Nullable: true},
- },
- },
- },
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "status", Values: []string{"a"}},
- },
- },
- {
- Name: "s",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "st", Type: "public.status"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
- schemaCompositeIdx := strings.Index(sql, `DROP TYPE "s"."addr"`)
- enumIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
- require.GreaterOrEqual(t, retypeIdx, 0, "column must be retyped: %s", sql)
- require.GreaterOrEqual(t, schemaCompositeIdx, 0, "deferred schema's composite must be dropped: %s", sql)
- require.GreaterOrEqual(t, enumIdx, 0, "enum must be dropped: %s", sql)
- require.Greater(t, schemaCompositeIdx, retypeIdx, "schema-owned composite drop must follow the retype: %s", sql)
- require.Greater(t, enumIdx, schemaCompositeIdx,
- "enum referenced from the deferred schema's composite must drop last: %s", sql)
-}
-
-func TestPGMetadataDiffCyclicDeferredSchemaDropsGlobalizeTypeDrops(t *testing.T) {
- // Schemas a and b reference each other: a.ca uses b.eb, b.cb uses a.ea.
- // Both are deferred (a column retype releases a.ca). Globalized type
- // drops must remove both composites before either enum, regardless of
- // the schema-level cycle.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "a.ca", Nullable: true},
- },
- },
- },
- },
- {
- Name: "a",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "ea", Values: []string{"x"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "ca",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "v", Type: "b.eb"},
- },
- },
- },
- },
- {
- Name: "b",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "eb", Values: []string{"y"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "cb",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "v", Type: "a.ea"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- caIdx := strings.Index(sql, `DROP TYPE "a"."ca"`)
- cbIdx := strings.Index(sql, `DROP TYPE "b"."cb"`)
- eaIdx := strings.Index(sql, `DROP TYPE "a"."ea"`)
- ebIdx := strings.Index(sql, `DROP TYPE "b"."eb"`)
- require.GreaterOrEqual(t, caIdx, 0, "a.ca must be dropped: %s", sql)
- require.GreaterOrEqual(t, cbIdx, 0, "b.cb must be dropped: %s", sql)
- require.GreaterOrEqual(t, eaIdx, 0, "a.ea must be dropped: %s", sql)
- require.GreaterOrEqual(t, ebIdx, 0, "b.eb must be dropped: %s", sql)
- require.Greater(t, eaIdx, cbIdx, "a.ea must drop after b.cb which references it: %s", sql)
- require.Greater(t, ebIdx, caIdx, "b.eb must drop after a.ca which references it: %s", sql)
- dropSchemaAIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "a"`)
- dropSchemaBIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "b"`)
- require.Greater(t, dropSchemaAIdx, eaIdx, "DROP SCHEMA a must come after its types: %s", sql)
- require.Greater(t, dropSchemaBIdx, ebIdx, "DROP SCHEMA b must come after its types: %s", sql)
-}
-
-func TestPGMetadataDiffImmediateSchemaDropOrdersTypesGlobally(t *testing.T) {
- // Dropping schema s (whose composite references top-level types) together
- // with those top-level types: s.child must drop before public.base and
- // public.status regardless of the schema-drop boundary.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "status", Values: []string{"a"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "base",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- },
- },
- {
- Name: "s",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "child",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "b", Type: "public.base"},
- {Name: "st", Type: "public.status"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- childIdx := strings.Index(sql, `DROP TYPE "s"."child"`)
- baseIdx := strings.Index(sql, `DROP TYPE "public"."base"`)
- statusIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
- schemaIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "s"`)
- require.GreaterOrEqual(t, childIdx, 0, "schema-owned composite must be dropped: %s", sql)
- require.GreaterOrEqual(t, baseIdx, 0)
- require.GreaterOrEqual(t, statusIdx, 0)
- require.GreaterOrEqual(t, schemaIdx, 0)
- require.Less(t, childIdx, baseIdx, "dependent s.child must drop before public.base: %s", sql)
- require.Less(t, childIdx, statusIdx, "dependent s.child must drop before public.status: %s", sql)
- require.Greater(t, schemaIdx, childIdx, "DROP SCHEMA must come after its composite drop: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeCreateWaitsForTableRowType(t *testing.T) {
- // Creating table t and composites c1(public.t) and c2(c1): both
- // composites must be created after the table.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c1",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "r", Type: "public.t"},
- },
- },
- {
- Name: "c2",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "w", Type: "public.c1"},
- },
- },
- {
- Name: "plain",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- tableIdx := strings.Index(sql, `CREATE TABLE "public"."t"`)
- c1Idx := strings.Index(sql, `CREATE TYPE "public"."c1"`)
- c2Idx := strings.Index(sql, `CREATE TYPE "public"."c2"`)
- plainIdx := strings.Index(sql, `CREATE TYPE "public"."plain"`)
- require.GreaterOrEqual(t, tableIdx, 0, "table must be created: %s", sql)
- require.GreaterOrEqual(t, c1Idx, 0)
- require.GreaterOrEqual(t, c2Idx, 0)
- require.GreaterOrEqual(t, plainIdx, 0)
- require.Greater(t, c1Idx, tableIdx, "composite using the table row type must be created after the table: %s", sql)
- require.Greater(t, c2Idx, c1Idx, "dependent of a post-table composite must follow it: %s", sql)
- require.Less(t, plainIdx, tableIdx, "independent composite stays before tables: %s", sql)
-}
-
-func TestPGMetadataDiffDeferredCompositeDefersItsCompositeDependency(t *testing.T) {
- // child(public.base) is deferred by a column retype; base, also dropped,
- // must defer too so it drops after child.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "public.child", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "base",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- {
- Name: "child",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "b", Type: "public.base"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
- childIdx := strings.Index(sql, `DROP TYPE "public"."child"`)
- baseIdx := strings.Index(sql, `DROP TYPE "public"."base"`)
- require.GreaterOrEqual(t, retypeIdx, 0)
- require.GreaterOrEqual(t, childIdx, 0)
- require.GreaterOrEqual(t, baseIdx, 0)
- require.Greater(t, childIdx, retypeIdx, "deferred child drops after the retype: %s", sql)
- require.Greater(t, baseIdx, childIdx, "base referenced by deferred child must defer and drop after it: %s", sql)
-}
-
-func TestPGMetadataDiffRowTypeCompositeDropsBeforeTable(t *testing.T) {
- // Dropping table t and composite c(r public.t): the composite must drop
- // before the table whose row type it references.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "r", Type: "public.t"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- typeIdx := strings.Index(sql, `DROP TYPE "public"."c"`)
- tableIdx := strings.Index(sql, `DROP TABLE "public"."t"`)
- require.GreaterOrEqual(t, typeIdx, 0, "composite must be dropped: %s", sql)
- require.GreaterOrEqual(t, tableIdx, 0, "table must be dropped: %s", sql)
- require.Less(t, typeIdx, tableIdx, "row-type-referencing composite must drop before the table: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeAlterWaitsForNewTableRowType(t *testing.T) {
- // Existing composite gains an attribute typed with a row type of a table
- // created in the same migration; the alter must follow CREATE TABLE.
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- })
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- {Name: "r", Type: "public.t"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- tableIdx := strings.Index(sql, `CREATE TABLE "public"."t"`)
- alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ADD ATTRIBUTE "r" public.t;`)
- require.GreaterOrEqual(t, tableIdx, 0, "table must be created: %s", sql)
- require.GreaterOrEqual(t, alterIdx, 0, "attribute must be added: %s", sql)
- require.Greater(t, alterIdx, tableIdx, "composite alter must follow the table creation it references: %s", sql)
-}
-
-func TestPGMetadataDiffCreateSandwichTableCompositeTable(t *testing.T) {
- // r (row-type provider) -> c (composite using r) -> holder (table using c):
- // creation order must interleave through the shared graph.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "r",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- {
- Name: "holder",
- Columns: []*storepb.ColumnMetadata{
- {Name: "x", Type: "public.c", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "row", Type: "public.r"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- rIdx := strings.Index(sql, `CREATE TABLE "public"."r"`)
- cIdx := strings.Index(sql, `CREATE TYPE "public"."c"`)
- holderIdx := strings.Index(sql, `CREATE TABLE "public"."holder"`)
- require.GreaterOrEqual(t, rIdx, 0)
- require.GreaterOrEqual(t, cIdx, 0)
- require.GreaterOrEqual(t, holderIdx, 0)
- require.Greater(t, cIdx, rIdx, "composite must follow its row-type provider table: %s", sql)
- require.Greater(t, holderIdx, cIdx, "consumer table must follow the composite: %s", sql)
-}
-
-func TestPGMetadataDiffDropSandwichTableCompositeTable(t *testing.T) {
- // holder (table using c) -> c (composite using r) -> r: drop order must
- // interleave through the shared graph.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "r",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- {
- Name: "holder",
- Columns: []*storepb.ColumnMetadata{
- {Name: "x", Type: "public.c", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "row", Type: "public.r"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- holderIdx := strings.Index(sql, `DROP TABLE "public"."holder"`)
- cIdx := strings.Index(sql, `DROP TYPE "public"."c"`)
- rIdx := strings.Index(sql, `DROP TABLE "public"."r"`)
- require.GreaterOrEqual(t, holderIdx, 0)
- require.GreaterOrEqual(t, cIdx, 0)
- require.GreaterOrEqual(t, rIdx, 0)
- require.Less(t, holderIdx, cIdx, "table using the composite must drop before it: %s", sql)
- require.Less(t, cIdx, rIdx, "composite must drop before its row-type provider table: %s", sql)
-}
-
-func TestPGMetadataDiffDeferredSchemaCompositeDefersTopLevelDependency(t *testing.T) {
- // Schema s is deferred (retype releases s.child); s.child references
- // top-level dropped public.base, which must defer too.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "s.child", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "base",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- },
- },
- {
- Name: "s",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "child",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "b", Type: "public.base"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
- childIdx := strings.Index(sql, `DROP TYPE "s"."child"`)
- baseIdx := strings.Index(sql, `DROP TYPE "public"."base"`)
- require.GreaterOrEqual(t, retypeIdx, 0)
- require.GreaterOrEqual(t, childIdx, 0)
- require.GreaterOrEqual(t, baseIdx, 0)
- require.Greater(t, childIdx, retypeIdx, "deferred schema composite drops after the retype: %s", sql)
- require.Greater(t, baseIdx, childIdx, "top-level base referenced by the deferred schema composite must defer and drop after it: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeDropDeferredAfterColumnDrop(t *testing.T) {
- // Dropping the only column using a composite (on a surviving table)
- // releases it in writeDropAlterTableObjects; the composite drop must
- // defer past that.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- {Name: "c", Type: "public.addr", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- columnDropIdx := strings.Index(sql, `DROP COLUMN "c"`)
- typeDropIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
- require.GreaterOrEqual(t, columnDropIdx, 0, "column must be dropped: %s", sql)
- require.GreaterOrEqual(t, typeDropIdx, 0, "composite must be dropped: %s", sql)
- require.Greater(t, typeDropIdx, columnDropIdx,
- "composite drop must follow the column drop that releases it: %s", sql)
-}
-
-func TestPGMetadataDiffReleasingCompositeAlterPrecedesTableDrop(t *testing.T) {
- // Retyping c.r away from public.t's row type releases the dropped table;
- // the releasing ALTER must precede DROP TABLE.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "r", Type: "public.t"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "r", Type: "text"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ALTER ATTRIBUTE "r" TYPE text;`)
- dropIdx := strings.Index(sql, `DROP TABLE "public"."t"`)
- require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
- require.GreaterOrEqual(t, dropIdx, 0, "table must be dropped: %s", sql)
- require.Less(t, alterIdx, dropIdx, "releasing alter must precede the table drop: %s", sql)
- require.Equal(t, strings.Count(sql, `ALTER ATTRIBUTE "r"`), 1, "releasing alter must not be emitted twice: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeArrayColumnOrdersCreate(t *testing.T) {
- // Column sync renders composite-array columns as the bare "_name" form;
- // the create graph must still order the composite first. zz table name
- // sorts after the composite to defeat accidental ordering.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "aa_holder",
- Columns: []*storepb.ColumnMetadata{
- {Name: "xs", Type: "_zz_addr", Nullable: true},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "zz_addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- typeIdx := strings.Index(sql, `CREATE TYPE "public"."zz_addr"`)
- tableIdx := strings.Index(sql, `CREATE TABLE "public"."aa_holder"`)
- require.GreaterOrEqual(t, typeIdx, 0)
- require.GreaterOrEqual(t, tableIdx, 0)
- require.Less(t, typeIdx, tableIdx, "array-typed column must order the composite before the table: %s", sql)
-}
-
-func TestPGMetadataDiffSchemaDropEmitsCompositeDropOnce(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- {
- Name: "s",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Equal(t, 1, strings.Count(sql, `DROP TYPE "s"."addr";`),
- "schema-owned composite must drop exactly once: %s", sql)
-}
-
-func TestPGMetadataDiffReleasingRetypeToCreatedTableSplitsDropReadd(t *testing.T) {
- // c.r moves from dropped old_t's row type to created new_t's row type:
- // the drop phase removes the attribute, the create phase re-adds it
- // after the new table exists.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "old_t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "r", Type: "public.old_t"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "new_t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- },
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "r", Type: "public.new_t"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- dropAttrIdx := strings.Index(sql, `ALTER TYPE "public"."c" DROP ATTRIBUTE "r";`)
- dropTableIdx := strings.Index(sql, `DROP TABLE "public"."old_t"`)
- createTableIdx := strings.Index(sql, `CREATE TABLE "public"."new_t"`)
- addAttrIdx := strings.Index(sql, `ALTER TYPE "public"."c" ADD ATTRIBUTE "r" public.new_t;`)
- require.GreaterOrEqual(t, dropAttrIdx, 0, "releasing drop must be emitted: %s", sql)
- require.GreaterOrEqual(t, dropTableIdx, 0)
- require.GreaterOrEqual(t, createTableIdx, 0)
- require.GreaterOrEqual(t, addAttrIdx, 0, "attribute must be re-added: %s", sql)
- require.Less(t, dropAttrIdx, dropTableIdx, "attribute drop must precede the table drop: %s", sql)
- require.Greater(t, addAttrIdx, createTableIdx, "re-add must follow the new table: %s", sql)
- require.NotContains(t, sql, `ALTER ATTRIBUTE "r"`, "no premature retype may be emitted: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeCreateWaitsForViewRowType(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Views: []*storepb.ViewMetadata{
- {Name: "v", Definition: "SELECT 1 AS a"},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "row", Type: "public.v"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- viewIdx := strings.Index(sql, `VIEW "public"."v"`)
- typeIdx := strings.Index(sql, `CREATE TYPE "public"."c"`)
- require.GreaterOrEqual(t, viewIdx, 0, "view must be created: %s", sql)
- require.GreaterOrEqual(t, typeIdx, 0, "composite must be created: %s", sql)
- require.Greater(t, typeIdx, viewIdx, "composite using a view row type must follow the view: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeDropReleasedByCompositeAlterDefers(t *testing.T) {
- // parent's alter (child -> text) releases dropped child only in the
- // create phase, so child's drop must defer past it.
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "child",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- {
- Name: "parent",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "c", Type: "public.child"},
- },
- },
- })
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "parent",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "c", Type: "text"},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- alterIdx := strings.Index(sql, `ALTER TYPE "public"."parent" ALTER ATTRIBUTE "c" TYPE text;`)
- dropIdx := strings.Index(sql, `DROP TYPE "public"."child"`)
- require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
- require.GreaterOrEqual(t, dropIdx, 0, "child must be dropped: %s", sql)
- require.Greater(t, dropIdx, alterIdx, "child drop must follow the alter that releases it: %s", sql)
-}
-
-func TestPGMetadataDiffEnumDropReleasedByCompositeAlterDefers(t *testing.T) {
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "status", Values: []string{"a"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "s", Type: "public.status"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "addr",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "s", Type: "text"},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- alterIdx := strings.Index(sql, `ALTER TYPE "public"."addr" ALTER ATTRIBUTE "s" TYPE text;`)
- dropIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
- require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
- require.GreaterOrEqual(t, dropIdx, 0, "enum must be dropped: %s", sql)
- require.Greater(t, dropIdx, alterIdx, "enum drop must follow the alter that releases it: %s", sql)
-}
-
-func TestPGMetadataDiffReleasingAlterPrecedesViewDrop(t *testing.T) {
- // c.row moves off dropped view v's row type; the early alter must
- // precede DROP VIEW.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Views: []*storepb.ViewMetadata{
- {Name: "v", Definition: "SELECT 1 AS a"},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "row", Type: "public.v"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "row", Type: "text"},
- },
- },
- })
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ALTER ATTRIBUTE "row" TYPE text;`)
- dropViewIdx := strings.Index(sql, `DROP VIEW "public"."v"`)
- require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
- require.GreaterOrEqual(t, dropViewIdx, 0, "view must be dropped: %s", sql)
- require.Less(t, alterIdx, dropViewIdx, "releasing alter must precede the view drop: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeAlterWaitsForCreatedViewRowType(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- })
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Views: []*storepb.ViewMetadata{
- {Name: "v", Definition: "SELECT 1 AS a"},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- {Name: "row", Type: "public.v"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- viewIdx := strings.Index(sql, `VIEW "public"."v"`)
- alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ADD ATTRIBUTE "row" public.v;`)
- require.GreaterOrEqual(t, viewIdx, 0, "view must be created: %s", sql)
- require.GreaterOrEqual(t, alterIdx, 0, "attribute must be added: %s", sql)
- require.Greater(t, alterIdx, viewIdx, "alter gaining the view row type must follow the view: %s", sql)
-}
-
-func TestPGMetadataDiffCompositeDropPrecedesReferencedViewDrop(t *testing.T) {
- // Dropping both c(row public.v) and v: the composite must drop first
- // despite the blanket views-before-composites edges.
- source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Views: []*storepb.ViewMetadata{
- {Name: "v", Definition: "SELECT 1 AS a"},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "c",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "row", Type: "public.v"},
- },
- },
- },
- },
- },
- }, nil, nil, storepb.Engine_POSTGRES, true)
- target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
- }, nil, nil, storepb.Engine_POSTGRES, true)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- typeIdx := strings.Index(sql, `DROP TYPE "public"."c"`)
- viewIdx := strings.Index(sql, `DROP VIEW "public"."v"`)
- require.GreaterOrEqual(t, typeIdx, 0, "composite must be dropped: %s", sql)
- require.GreaterOrEqual(t, viewIdx, 0, "view must be dropped: %s", sql)
- require.Less(t, typeIdx, viewIdx, "composite must drop before the view whose row type it references: %s", sql)
-}
-
-func TestFilterPostgresArchiveSchemaKeepsCompositeTypes(t *testing.T) {
- diff := &schema.MetadataDiff{
- CompositeTypeChanges: []*schema.CompositeTypeDiff{
- {Action: schema.MetadataDiffActionCreate, SchemaName: "public", CompositeTypeName: "addr"},
- {Action: schema.MetadataDiffActionCreate, SchemaName: "bbdataarchive", CompositeTypeName: "archived"},
- },
- }
-
- filtered := schema.FilterPostgresArchiveSchema(diff)
-
- require.Len(t, filtered.CompositeTypeChanges, 1)
- require.Equal(t, "public", filtered.CompositeTypeChanges[0].SchemaName)
-}
-
-func TestPGMetadataDiffSkipDumpCompositeTypesIgnored(t *testing.T) {
- source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
- {
- Name: "ext_owned",
- SkipDump: true,
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- })
- target := newPGDatabaseMetadataWithCompositeTypes(nil)
-
- sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
-
- require.NoError(t, err)
- require.Empty(t, sql, "skip_dump composite types must not be diffed")
-}
diff --git a/backend/plugin/schema/pg/composite_type_sdl_test.go b/backend/plugin/schema/pg/composite_type_sdl_test.go
deleted file mode 100644
index 581ab6c720..0000000000
--- a/backend/plugin/schema/pg/composite_type_sdl_test.go
+++ /dev/null
@@ -1,214 +0,0 @@
-package pg
-
-import (
- "strings"
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-func TestGetDatabaseDefinitionSDLFormat_CompositeTypes(t *testing.T) {
- // aa_nested sorts before its dependency zz_base alphabetically, so correct
- // output proves dependency ordering rather than name ordering.
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "order_status", Values: []string{"pending", "shipped"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "aa_nested",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "home", Type: "public.zz_base"},
- {Name: "homes", Type: "public.zz_base[]"},
- {Name: "status", Type: "public.order_status"},
- },
- },
- {
- Name: "zz_base",
- Comment: "base address type",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "street", Type: "text", Collation: `"C"`, Comment: "street line"},
- {Name: "city", Type: "character varying(50)"},
- },
- },
- {
- Name: "ext_owned",
- SkipDump: true,
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- },
- {
- Name: "empty_type",
- },
- },
- },
- {
- Name: "geo",
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "point2",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "lat", Type: "numeric(9,6)"},
- {Name: "lng", Type: "numeric(9,6)"},
- },
- },
- },
- },
- },
- }
-
- result, err := GetDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
- require.NoError(t, err)
-
- assert.Contains(t, result, `CREATE TYPE "public"."zz_base" AS (`)
- assert.Contains(t, result, `"street" text COLLATE "C"`)
- assert.Contains(t, result, `"city" character varying(50)`)
- assert.Contains(t, result, `CREATE TYPE "public"."aa_nested" AS (`)
- assert.Contains(t, result, `"home" public.zz_base`)
- assert.Contains(t, result, `"homes" public.zz_base[]`)
- assert.Contains(t, result, `CREATE TYPE "geo"."point2" AS (`)
- assert.Contains(t, result, `CREATE TYPE "public"."empty_type" AS (`)
- assert.Contains(t, result, `COMMENT ON TYPE "public"."zz_base" IS 'base address type';`)
- assert.Contains(t, result, `COMMENT ON COLUMN "public"."zz_base"."street" IS 'street line';`)
- assert.NotContains(t, result, "ext_owned", "skip_dump composite types must not be emitted")
-
- // Dependency order: zz_base must be created before aa_nested.
- baseIdx := strings.Index(result, `CREATE TYPE "public"."zz_base"`)
- nestedIdx := strings.Index(result, `CREATE TYPE "public"."aa_nested"`)
- require.GreaterOrEqual(t, baseIdx, 0)
- require.GreaterOrEqual(t, nestedIdx, 0)
- assert.Less(t, baseIdx, nestedIdx, "referenced composite must be emitted before its dependent")
-
- // Enums come before composites.
- enumIdx := strings.Index(result, `CREATE TYPE "public"."order_status"`)
- require.GreaterOrEqual(t, enumIdx, 0)
- assert.Less(t, enumIdx, baseIdx, "enums must be emitted before composite types")
-
- // The generated SDL must be loadable by the SDL migration engine.
- _, err = schema.DiffSDLMigration(storepb.Engine_POSTGRES, "", result, "")
- require.NoError(t, err, "generated SDL should load: %s", result)
-}
-
-func TestGetMultiFileDatabaseDefinition_CompositeTypes(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {Name: "mood", Values: []string{"happy", "sad"}},
- },
- CompositeTypes: []*storepb.CompositeTypeMetadata{
- {
- Name: "wrapper",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "b", Type: "public.base"},
- },
- },
- {
- Name: "base",
- Attributes: []*storepb.CompositeTypeAttribute{
- {Name: "x", Type: "integer"},
- },
- Comment: "base type",
- },
- },
- },
- },
- }
-
- result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
- require.NoError(t, err)
-
- var typesFile string
- var combined strings.Builder
- for _, file := range result.Files {
- if file.Name == "schemas/public/types.sql" {
- typesFile = file.Content
- }
- combined.WriteString(file.Content)
- combined.WriteString("\n")
- }
- require.NotEmpty(t, typesFile, "types.sql should exist")
-
- assert.Contains(t, typesFile, `CREATE TYPE "public"."mood" AS ENUM (`)
- assert.Contains(t, typesFile, `CREATE TYPE "public"."base" AS (`)
- assert.Contains(t, typesFile, `CREATE TYPE "public"."wrapper" AS (`)
- assert.Contains(t, typesFile, `COMMENT ON TYPE "public"."base" IS 'base type';`)
- assert.Less(t,
- strings.Index(typesFile, `CREATE TYPE "public"."base" AS (`),
- strings.Index(typesFile, `CREATE TYPE "public"."wrapper" AS (`),
- "referenced composite must be emitted before its dependent")
-
- _, err = schema.DiffSDLMigration(storepb.Engine_POSTGRES, "", combined.String(), "")
- require.NoError(t, err, "combined multi-file SDL should load")
-}
-
-func TestParseQualifiedTypeIdent(t *testing.T) {
- testCases := []struct {
- input string
- schema string
- name string
- ok bool
- }{
- {"integer", "", "", false},
- {"character varying(50)", "", "", false},
- {"numeric(9,6)", "", "", false},
- {"text[]", "", "", false},
- {"timestamp(6) with time zone", "", "", false},
- {"public.addr", "public", "addr", true},
- {"public.addr[]", "public", "addr", true},
- {"public.addr(5)", "public", "addr", true},
- {`"My Schema"."Weird Type"`, "My Schema", "Weird Type", true},
- {`"has""quote".t`, `has"quote`, "t", true},
- {`other.geo`, "other", "geo", true},
- }
- for _, tc := range testCases {
- schemaName, typeName, ok := parseQualifiedTypeIdent(tc.input)
- assert.Equal(t, tc.ok, ok, "input %q", tc.input)
- assert.Equal(t, tc.schema, schemaName, "input %q", tc.input)
- assert.Equal(t, tc.name, typeName, "input %q", tc.input)
- }
-}
-
-func TestSortCompositeTypesTopologically(t *testing.T) {
- // Chain across schemas: public.a3 -> other.b2 -> public.c1, with names
- // chosen so alphabetical order is the reverse of dependency order.
- c1 := &storepb.CompositeTypeMetadata{
- Name: "c1",
- Attributes: []*storepb.CompositeTypeAttribute{{Name: "x", Type: "integer"}},
- }
- b2 := &storepb.CompositeTypeMetadata{
- Name: "b2",
- Attributes: []*storepb.CompositeTypeAttribute{{Name: "c", Type: "public.c1"}},
- }
- a3 := &storepb.CompositeTypeMetadata{
- Name: "a3",
- Attributes: []*storepb.CompositeTypeAttribute{{Name: "b", Type: "other.b2[]"}},
- }
- ordered := sortCompositeTypesTopologically([]qualifiedCompositeType{
- {Schema: "public", Composite: a3},
- {Schema: "other", Composite: b2},
- {Schema: "public", Composite: c1},
- })
- require.Len(t, ordered, 3)
- assert.Equal(t, "c1", ordered[0].Composite.Name)
- assert.Equal(t, "b2", ordered[1].Composite.Name)
- assert.Equal(t, "a3", ordered[2].Composite.Name)
-
- // Independent types come out in deterministic (schema, name) order.
- ordered = sortCompositeTypesTopologically([]qualifiedCompositeType{
- {Schema: "public", Composite: &storepb.CompositeTypeMetadata{Name: "zz"}},
- {Schema: "public", Composite: &storepb.CompositeTypeMetadata{Name: "aa"}},
- })
- require.Len(t, ordered, 2)
- assert.Equal(t, "aa", ordered[0].Composite.Name)
- assert.Equal(t, "zz", ordered[1].Composite.Name)
-}
diff --git a/backend/plugin/schema/pg/event_trigger_sdl_output_test.go b/backend/plugin/schema/pg/event_trigger_sdl_output_test.go
deleted file mode 100644
index 6ad7f9ec2d..0000000000
--- a/backend/plugin/schema/pg/event_trigger_sdl_output_test.go
+++ /dev/null
@@ -1,86 +0,0 @@
-package pg
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
- "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 TestEventTriggerSDLSingleFileOutput(t *testing.T) {
- metadata := eventTriggerSDLMetadata()
-
- result, err := GetDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
- require.NoError(t, err)
- assert.Contains(t, result, `CREATE FUNCTION "public"."audit_ddl"() RETURNS event_trigger`)
- assert.Contains(t, result, `CREATE EVENT TRIGGER "audit_ddl_start" ON ddl_command_start`)
- assert.Contains(t, result, `WHEN TAG IN ('CREATE TABLE')`)
- assert.Contains(t, result, `EXECUTE FUNCTION "public"."audit_ddl"();`)
- assert.Contains(t, result, `COMMENT ON EVENT TRIGGER "audit_ddl_start" IS 'Audit DDL start';`)
-
- sdl, err := schema.MetadataToSDL(
- storepb.Engine_POSTGRES,
- model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, true),
- )
- require.NoError(t, err)
- assert.Contains(t, sdl, `CREATE EVENT TRIGGER "audit_ddl_start" ON ddl_command_start`)
- assert.Contains(t, sdl, `COMMENT ON EVENT TRIGGER "audit_ddl_start" IS 'Audit DDL start';`)
-}
-
-func TestEventTriggerSDLMultiFileOutput(t *testing.T) {
- result, err := GetMultiFileDatabaseDefinition(
- schema.GetDefinitionContext{SDLFormat: true},
- eventTriggerSDLMetadata(),
- )
- require.NoError(t, err)
- require.NotNil(t, result)
-
- fileMap := make(map[string]string)
- for _, file := range result.Files {
- fileMap[file.Name] = file.Content
- }
-
- eventTriggerFile, ok := fileMap["event_triggers.sql"]
- require.True(t, ok, "event_triggers.sql file should exist")
- assert.Contains(t, eventTriggerFile, `CREATE EVENT TRIGGER "audit_ddl_start" ON ddl_command_start`)
- assert.Contains(t, eventTriggerFile, `WHEN TAG IN ('CREATE TABLE')`)
- assert.Contains(t, eventTriggerFile, `EXECUTE FUNCTION "public"."audit_ddl"();`)
- assert.Contains(t, eventTriggerFile, `COMMENT ON EVENT TRIGGER "audit_ddl_start" IS 'Audit DDL start';`)
-}
-
-func eventTriggerSDLMetadata() *storepb.DatabaseSchemaMetadata {
- return &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "audit_ddl",
- Signature: "audit_ddl()",
- Definition: `CREATE FUNCTION "public"."audit_ddl"() RETURNS event_trigger
-LANGUAGE plpgsql
-AS $$
-BEGIN
-END;
-$$`,
- },
- },
- },
- },
- EventTriggers: []*storepb.EventTriggerMetadata{
- {
- Name: "audit_ddl_start",
- Event: "ddl_command_start",
- Tags: []string{"CREATE TABLE"},
- FunctionSchema: "public",
- FunctionName: "audit_ddl",
- Enabled: true,
- Comment: "Audit DDL start",
- },
- },
- }
-}
diff --git a/backend/plugin/schema/pg/get_database_definition_regressions_test.go b/backend/plugin/schema/pg/get_database_definition_regressions_test.go
deleted file mode 100644
index fc85b37a7f..0000000000
--- a/backend/plugin/schema/pg/get_database_definition_regressions_test.go
+++ /dev/null
@@ -1,319 +0,0 @@
-package pg
-
-import (
- "strings"
- "testing"
-
- "github.com/bytebase/omni/pg/catalog"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-// TestGetDatabaseDefinition_IndexCommentUsesRealNewline reproduces the A1
-// bug where writeIndexComment used a Go raw string literal with `\n\n`,
-// producing four literal characters `\`, `n`, `\`, `n` instead of two line
-// feeds. The regression surfaced as omni pgparser choking on the backslash
-// right after `COMMENT ON INDEX ... IS '...';`.
-func TestGetDatabaseDefinition_IndexCommentUsesRealNewline(t *testing.T) {
- meta := &storepb.DatabaseSchemaMetadata{
- Name: "db",
- Schemas: []*storepb.SchemaMetadata{{
- Name: "public",
- Tables: []*storepb.TableMetadata{{
- Name: "t",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer", Nullable: false},
- },
- Indexes: []*storepb.IndexMetadata{{
- Name: "idx_t_id",
- Type: "btree",
- Expressions: []string{"id"},
- Descending: []bool{false},
- Definition: "CREATE INDEX idx_t_id ON public.t USING btree (id);",
- Comment: "sample comment",
- }},
- }},
- }},
- }
-
- ddl, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, meta)
- if err != nil {
- t.Fatalf("GetDatabaseDefinition: %v", err)
- }
-
- if strings.Contains(ddl, `\n`) {
- t.Errorf("DDL contains literal backslash-n escape; got:\n%s", ddl)
- }
- if !strings.Contains(ddl, `COMMENT ON INDEX "public"."idx_t_id" IS 'sample comment';`) {
- t.Errorf("expected COMMENT ON INDEX statement, got:\n%s", ddl)
- }
-
- cat := catalog.New()
- if _, execErr := cat.Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); execErr != nil {
- t.Errorf("omni catalog parse error: %v\nDDL:\n%s", execErr, ddl)
- }
-}
-
-// TestGetDatabaseDefinition_TableWithOnlyCheckConstraints reproduces the A2
-// bug where a table with zero columns but with CHECK constraints produced
-// `CREATE TABLE "..."."..." (,` — an invalid leading comma. We force the
-// case by declaring a CHECK constraint on a table with no regular columns.
-func TestGetDatabaseDefinition_TableWithOnlyCheckConstraints(t *testing.T) {
- meta := &storepb.DatabaseSchemaMetadata{
- Name: "db",
- Schemas: []*storepb.SchemaMetadata{{
- Name: "public",
- Tables: []*storepb.TableMetadata{{
- Name: "cons_only",
- CheckConstraints: []*storepb.CheckConstraintMetadata{{
- Name: "cons_only_dummy",
- Expression: "(1 = 1)",
- }},
- }},
- }},
- }
-
- ddl, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, meta)
- if err != nil {
- t.Fatalf("GetDatabaseDefinition: %v", err)
- }
- if strings.Contains(ddl, "(,") {
- t.Errorf("DDL still has `(,` leading comma; got:\n%s", ddl)
- }
- if !strings.Contains(ddl, `CREATE TABLE "public"."cons_only" (`) {
- t.Errorf("expected CREATE TABLE for cons_only, got:\n%s", ddl)
- }
- // A genuine zero-column table (no indexes or foreign keys) must keep its
- // column-independent CHECK constraint — only privilege-filtered broken
- // metadata goes down the bare-table path.
- if !strings.Contains(ddl, "cons_only_dummy") {
- t.Errorf("expected CHECK constraint cons_only_dummy to be preserved, got:\n%s", ddl)
- }
- // Omni will reject a table with zero columns semantically, but it must
- // at least parse cleanly; the test asserts the parse-level contract.
- if _, parseErr := catalog.New().Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); parseErr != nil {
- t.Errorf("omni catalog parse error: %v\nDDL:\n%s", parseErr, ddl)
- }
-}
-
-// TestGetDatabaseDefinition_TableWithConstraintsButNoColumns reproduces a
-// dump replay failure observed on a real workspace export: the sync user
-// lacked column privileges on some tables, so information_schema.columns
-// returned nothing while pg_catalog still exposed the tables' constraints.
-// The dump then emitted `CREATE TABLE "s1"."t1" ();` followed by
-// `ALTER TABLE ... ADD CONSTRAINT ... PRIMARY KEY (c1, c2);`, which
-// PostgreSQL rejects because the columns do not exist. The dump must skip
-// column-dependent DDL for such tables instead of producing non-replayable
-// statements.
-func TestGetDatabaseDefinition_TableWithConstraintsButNoColumns(t *testing.T) {
- meta := &storepb.DatabaseSchemaMetadata{
- Name: "db",
- Schemas: []*storepb.SchemaMetadata{{
- Name: "s1",
- Sequences: []*storepb.SequenceMetadata{
- {
- // Owned by a column missing from t1's metadata: the sequence
- // itself must dump, its OWNED BY clause must not.
- Name: "t1_c1_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- OwnerTable: "t1",
- OwnerColumn: "c1",
- },
- {
- // Owned by a column of t4, a zero-column table with no
- // broken-metadata signal (no indexes/FKs/partitions):
- // OWNED BY must still be skipped — a sequence cannot be
- // owned by a column of a zero-column table.
- Name: "t4_id_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- OwnerTable: "t4",
- OwnerColumn: "id",
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- // Broken sync: constraints and indexes, but no columns.
- Name: "t1",
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "pk_t1",
- Primary: true,
- Unique: true,
- IsConstraint: true,
- Expressions: []string{"c1", "c2"},
- },
- {
- Name: "uk_t1_c1",
- Unique: true,
- IsConstraint: true,
- Expressions: []string{"c1"},
- },
- {
- Name: "idx_t1_c2",
- Type: "btree",
- Expressions: []string{"c2"},
- Definition: `CREATE INDEX idx_t1_c2 ON s1.t1 USING btree (c2);`,
- Comment: "comment on a never-created index",
- },
- },
- CheckConstraints: []*storepb.CheckConstraintMetadata{{
- Name: "chk_t1_c1",
- Expression: "(c1 > 0)",
- }},
- ForeignKeys: []*storepb.ForeignKeyMetadata{{
- Name: "fk_t1_t2",
- Columns: []string{"c1"},
- ReferencedSchema: "s1",
- ReferencedTable: "t2",
- ReferencedColumns: []string{"id"},
- }},
- Triggers: []*storepb.TriggerMetadata{{
- Name: "trg_t1_c1",
- Body: `CREATE TRIGGER trg_t1_c1 BEFORE UPDATE OF c1 ON s1.t1 FOR EACH ROW EXECUTE FUNCTION s1.f()`,
- Comment: "comment on a never-created trigger",
- }},
- Rules: []*storepb.RuleMetadata{{
- Name: "rule_t1",
- Event: "INSERT",
- Definition: `CREATE RULE rule_t1 AS ON INSERT TO s1.t1 WHERE new.c1 > 0 DO INSTEAD NOTHING;`,
- }},
- },
- {
- // Healthy table; its own constraints must still dump, but
- // its foreign key into the broken table must not.
- Name: "t2",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer", Nullable: false},
- {Name: "t1_c1", Type: "integer", Nullable: true},
- },
- Indexes: []*storepb.IndexMetadata{{
- Name: "pk_t2",
- Primary: true,
- Unique: true,
- IsConstraint: true,
- Expressions: []string{"id"},
- }},
- ForeignKeys: []*storepb.ForeignKeyMetadata{{
- Name: "fk_t2_t1",
- Columns: []string{"t1_c1"},
- ReferencedSchema: "s1",
- ReferencedTable: "t1",
- ReferencedColumns: []string{"c1"},
- }},
- },
- {
- // Broken sync on a partitioned table with no indexes: the
- // partition key references missing columns, so the table
- // must dump bare, without the PARTITION BY clause.
- Name: "t3",
- Partitions: []*storepb.TablePartitionMetadata{{
- Name: "t3_p1",
- Expression: "RANGE (created_at)",
- }},
- },
- {
- // Genuine zero-column table: no indexes, foreign keys, or
- // partitions. Its column-independent CHECK must be kept.
- Name: "t4",
- CheckConstraints: []*storepb.CheckConstraintMetadata{{
- Name: "chk_t4_ok",
- Expression: "(1 = 1)",
- }},
- },
- },
- }},
- }
-
- banned := []string{"pk_t1", "uk_t1_c1", "idx_t1_c2", "chk_t1_c1", "fk_t1_t2", "fk_t2_t1", "trg_t1_c1", "rule_t1", "t3_p1", "PARTITION BY", "OWNED BY", "COMMENT ON INDEX"}
-
- for name, ctx := range map[string]schema.GetDefinitionContext{
- "dump": {},
- "sdl": {SDLFormat: true},
- } {
- ddl, err := GetDatabaseDefinition(ctx, meta)
- if err != nil {
- t.Fatalf("[%s] GetDatabaseDefinition: %v", name, err)
- }
-
- if !strings.Contains(ddl, `CREATE TABLE "s1"."t1" (`) {
- t.Errorf("[%s] expected bare CREATE TABLE for t1, got:\n%s", name, ddl)
- }
- for _, b := range banned {
- if strings.Contains(ddl, b) {
- t.Errorf("[%s] DDL references %q, which depends on columns missing from t1's metadata; got:\n%s", name, b, ddl)
- }
- }
- if !strings.Contains(ddl, "pk_t2") {
- t.Errorf("[%s] expected pk_t2 on the healthy table, got:\n%s", name, ddl)
- }
- if !strings.Contains(ddl, `CREATE SEQUENCE "s1"."t1_c1_seq"`) {
- t.Errorf("[%s] expected CREATE SEQUENCE for t1_c1_seq, got:\n%s", name, ddl)
- }
- if !strings.Contains(ddl, `CREATE TABLE "s1"."t3" (`) {
- t.Errorf("[%s] expected bare CREATE TABLE for t3, got:\n%s", name, ddl)
- }
- if !strings.Contains(ddl, "chk_t4_ok") {
- t.Errorf("[%s] expected genuine zero-column table t4 to keep its CHECK constraint, got:\n%s", name, ddl)
- }
-
- if _, parseErr := catalog.New().Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); parseErr != nil {
- t.Errorf("[%s] omni catalog parse error: %v\nDDL:\n%s", name, parseErr, ddl)
- }
- }
-
- multiFile, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{}, meta)
- if err != nil {
- t.Fatalf("GetMultiFileDatabaseDefinition: %v", err)
- }
- var combined strings.Builder
- for _, file := range multiFile.Files {
- combined.WriteString(file.Content)
- }
- for _, b := range banned {
- if strings.Contains(combined.String(), b) {
- t.Errorf("[multi-file] DDL references %q, which depends on columns missing from t1's metadata; got:\n%s", b, combined.String())
- }
- }
-}
-
-// TestGetDatabaseDefinition_TableNameContainingDot reproduces A3: a table
-// whose name literally contains a period was writing out as `"".".."` —
-// because the schema + "." + object object-ID was split back on the first
-// dot and produced an empty schema. The fix uses a NUL separator.
-func TestGetDatabaseDefinition_TableNameContainingDot(t *testing.T) {
- meta := &storepb.DatabaseSchemaMetadata{
- Name: "db",
- Schemas: []*storepb.SchemaMetadata{{
- Name: "public",
- Tables: []*storepb.TableMetadata{{
- Name: "weird.name",
- Columns: []*storepb.ColumnMetadata{
- {Name: "c", Type: "text", Nullable: true},
- },
- }},
- }},
- }
-
- ddl, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, meta)
- if err != nil {
- t.Fatalf("GetDatabaseDefinition: %v", err)
- }
- if strings.Contains(ddl, `""."`) {
- t.Errorf("DDL contains empty schema `\"\".\"` prefix; got:\n%s", ddl)
- }
- if !strings.Contains(ddl, `CREATE TABLE "public"."weird.name"`) {
- t.Errorf("expected schema to be public, got:\n%s", ddl)
- }
- if _, parseErr := catalog.New().Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); parseErr != nil {
- t.Errorf("omni catalog parse error: %v\nDDL:\n%s", parseErr, ddl)
- }
-}
diff --git a/backend/plugin/schema/pg/get_database_definition_sdl_test.go b/backend/plugin/schema/pg/get_database_definition_sdl_test.go
deleted file mode 100644
index 0b2c5af62e..0000000000
--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go
+++ /dev/null
@@ -1,2222 +0,0 @@
-package pg
-
-import (
- "strings"
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-
- omnipg "github.com/bytebase/omni/pg"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-func TestGetDatabaseDefinitionSDLFormat(t *testing.T) {
- tests := []struct {
- name string
- metadata *storepb.DatabaseSchemaMetadata
- expected string
- }{
- {
- name: "Simple table with basic columns",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- {
- Name: "email",
- Type: "VARCHAR(320)",
- Nullable: true,
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."users" (
- "id" SERIAL NOT NULL,
- "name" VARCHAR(255) NOT NULL,
- "email" VARCHAR(320)
-);
-
-`,
- },
- {
- name: "Table with default values",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "products",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- {
- Name: "price",
- Type: "DECIMAL(10,2)",
- Default: "0.00",
- Nullable: false,
- },
- {
- Name: "active",
- Type: "BOOLEAN",
- Default: "true",
- Nullable: false,
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."products" (
- "id" SERIAL NOT NULL,
- "name" VARCHAR(255) NOT NULL,
- "price" DECIMAL(10,2) DEFAULT 0.00 NOT NULL,
- "active" BOOLEAN DEFAULT true NOT NULL
-);
-
-`,
- },
- {
- name: "Table with constraints",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "email",
- Type: "VARCHAR(320)",
- Nullable: false,
- },
- {
- Name: "age",
- Type: "INTEGER",
- Nullable: true,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "users_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- {
- Name: "users_email_key",
- Expressions: []string{"email"},
- Unique: true,
- IsConstraint: true,
- },
- },
- CheckConstraints: []*storepb.CheckConstraintMetadata{
- {
- Name: "users_age_check",
- Expression: "(age >= 0)",
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."users" (
- "id" SERIAL NOT NULL,
- "email" VARCHAR(320) NOT NULL,
- "age" INTEGER,
- CONSTRAINT "users_pkey" PRIMARY KEY (id),
- CONSTRAINT "users_email_key" UNIQUE (email),
- CONSTRAINT "users_age_check" CHECK (age >= 0)
-);
-
-`,
- },
- {
- name: "Table with foreign key",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "orders",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "user_id",
- Type: "INTEGER",
- Nullable: false,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "orders_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- },
- ForeignKeys: []*storepb.ForeignKeyMetadata{
- {
- Name: "orders_user_id_fkey",
- Columns: []string{"user_id"},
- ReferencedSchema: "public",
- ReferencedTable: "users",
- ReferencedColumns: []string{"id"},
- OnDelete: "CASCADE",
- OnUpdate: "NO ACTION",
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."orders" (
- "id" SERIAL NOT NULL,
- "user_id" INTEGER NOT NULL,
- CONSTRAINT "orders_pkey" PRIMARY KEY (id),
- CONSTRAINT "orders_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON DELETE CASCADE
-);
-
-`,
- },
- {
- name: "Multiple tables",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "categories",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(100)",
- Nullable: false,
- },
- },
- },
- {
- Name: "products",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "category_id",
- Type: "INTEGER",
- Nullable: true,
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."categories" (
- "id" SERIAL NOT NULL,
- "name" VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE "public"."products" (
- "id" SERIAL NOT NULL,
- "category_id" INTEGER
-);
-
-`,
- },
- {
- name: "Table with indexes",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "products",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- {
- Name: "category_id",
- Type: "INTEGER",
- Nullable: true,
- },
- {
- Name: "price",
- Type: "DECIMAL(10,2)",
- Nullable: false,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "products_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- {
- Name: "idx_products_name",
- Expressions: []string{"name"},
- },
- {
- Name: "idx_products_category_price",
- Expressions: []string{"category_id", "price"},
- Descending: []bool{false, true}, // price DESC
- },
- {
- Name: "idx_products_name_unique",
- Expressions: []string{"name"},
- Unique: true,
- IsConstraint: false, // This is a unique index, not a unique constraint
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."products" (
- "id" SERIAL NOT NULL,
- "name" VARCHAR(255) NOT NULL,
- "category_id" INTEGER,
- "price" DECIMAL(10,2) NOT NULL,
- CONSTRAINT "products_pkey" PRIMARY KEY (id)
-);
-
-CREATE INDEX "idx_products_name" ON "public"."products" (name);
-
-CREATE INDEX "idx_products_category_price" ON "public"."products" (category_id, price DESC);
-
-CREATE UNIQUE INDEX "idx_products_name_unique" ON "public"."products" (name);
-
-`,
- },
- {
- name: "Table with views",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- {
- Name: "email",
- Type: "VARCHAR(320)",
- Nullable: false,
- },
- {
- Name: "active",
- Type: "BOOLEAN",
- Default: "true",
- Nullable: false,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "users_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- },
- },
- {
- Name: "orders",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "user_id",
- Type: "INTEGER",
- Nullable: false,
- },
- {
- Name: "total",
- Type: "DECIMAL(10,2)",
- Nullable: false,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "orders_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- },
- },
- },
- Views: []*storepb.ViewMetadata{
- {
- Name: "active_users",
- Definition: `SELECT id, name, email
- FROM users
- WHERE active = true`,
- },
- {
- Name: "user_order_summary",
- Definition: `SELECT
- u.id,
- u.name,
- COUNT(o.id) as order_count,
- COALESCE(SUM(o.total), 0) as total_amount
-FROM users u
-LEFT JOIN orders o ON u.id = o.user_id
-GROUP BY u.id, u.name`,
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."users" (
- "id" SERIAL NOT NULL,
- "name" VARCHAR(255) NOT NULL,
- "email" VARCHAR(320) NOT NULL,
- "active" BOOLEAN DEFAULT true NOT NULL,
- CONSTRAINT "users_pkey" PRIMARY KEY (id)
-);
-
-CREATE TABLE "public"."orders" (
- "id" SERIAL NOT NULL,
- "user_id" INTEGER NOT NULL,
- "total" DECIMAL(10,2) NOT NULL,
- CONSTRAINT "orders_pkey" PRIMARY KEY (id)
-);
-
-CREATE VIEW "public"."active_users" AS SELECT id, name, email
- FROM users
- WHERE active = true;
-
-CREATE VIEW "public"."user_order_summary" AS SELECT
- u.id,
- u.name,
- COUNT(o.id) as order_count,
- COALESCE(SUM(o.total), 0) as total_amount
-FROM users u
-LEFT JOIN orders o ON u.id = o.user_id
-GROUP BY u.id, u.name;
-
-`,
- },
- {
- name: "Database with functions and procedures",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- {
- Name: "created_at",
- Type: "TIMESTAMP",
- Default: "CURRENT_TIMESTAMP",
- Nullable: false,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "users_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- },
- },
- },
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "get_user_count",
- Definition: `CREATE FUNCTION "public"."get_user_count"() RETURNS integer
- LANGUAGE sql
- AS $$
- SELECT COUNT(*)::integer FROM users;
-$$`,
- },
- {
- Name: "get_user_by_id",
- Definition: `CREATE FUNCTION "public"."get_user_by_id"(user_id integer) RETURNS TABLE(id integer, name character varying, created_at timestamp without time zone)
- LANGUAGE sql
- AS $$
- SELECT u.id, u.name, u.created_at
- FROM users u
- WHERE u.id = user_id;
-$$`,
- },
- {
- Name: "update_user_name",
- Definition: `CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
- LANGUAGE plpgsql
- AS $$
-BEGIN
- UPDATE users
- SET name = new_name
- WHERE id = user_id;
-
- IF NOT FOUND THEN
- RAISE EXCEPTION 'User with id % not found', user_id;
- END IF;
-END;
-$$`,
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."users" (
- "id" SERIAL NOT NULL,
- "name" VARCHAR(255) NOT NULL,
- "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
- CONSTRAINT "users_pkey" PRIMARY KEY (id)
-);
-
-CREATE FUNCTION "public"."get_user_count"() RETURNS integer
- LANGUAGE sql
- AS $$
- SELECT COUNT(*)::integer FROM users;
-$$;
-
-CREATE FUNCTION "public"."get_user_by_id"(user_id integer) RETURNS TABLE(id integer, name character varying, created_at timestamp without time zone)
- LANGUAGE sql
- AS $$
- SELECT u.id, u.name, u.created_at
- FROM users u
- WHERE u.id = user_id;
-$$;
-
-CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
- LANGUAGE plpgsql
- AS $$
-BEGIN
- UPDATE users
- SET name = new_name
- WHERE id = user_id;
-
- IF NOT FOUND THEN
- RAISE EXCEPTION 'User with id % not found', user_id;
- END IF;
-END;
-$$;
-
-`,
- },
- {
- name: "Database with sequences",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "independent_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- OwnerTable: "", // Independent sequence (not owned by any table)
- },
- {
- Name: "user_id_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- OwnerTable: "users",
- OwnerColumn: "id",
- },
- {
- Name: "order_seq",
- DataType: "integer",
- Start: "1000",
- Increment: "10",
- MinValue: "1000",
- MaxValue: "999999",
- Cycle: true,
- OwnerTable: "", // Independent sequence
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "INTEGER",
- Nullable: false,
- Default: "nextval('user_id_seq'::regclass)",
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE SEQUENCE "public"."independent_seq" AS bigint START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 NO CYCLE;
-
-CREATE SEQUENCE "public"."order_seq" AS integer START WITH 1000 INCREMENT BY 10 MINVALUE 1000 MAXVALUE 999999 CYCLE;
-
-CREATE TABLE "public"."users" (
- "id" serial,
- "name" VARCHAR(255) NOT NULL
-);
-
-`,
- },
- {
- name: "Empty database",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{},
- },
- expected: "",
- },
- {
- name: "Serial columns should use serial types",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "users_id_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- OwnerTable: "users",
- OwnerColumn: "id",
- },
- {
- Name: "products_id_seq",
- DataType: "integer",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "2147483647",
- Cycle: false,
- OwnerTable: "products",
- OwnerColumn: "id",
- },
- {
- Name: "orders_id_seq",
- DataType: "smallint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "32767",
- Cycle: false,
- OwnerTable: "orders",
- OwnerColumn: "id",
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "bigint",
- Nullable: false,
- Default: "nextval('users_id_seq'::regclass)",
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- },
- },
- {
- Name: "products",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- Nullable: false,
- Default: "nextval('products_id_seq'::regclass)",
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- },
- },
- {
- Name: "orders",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "smallint",
- Nullable: false,
- Default: "nextval('orders_id_seq'::regclass)",
- },
- {
- Name: "user_id",
- Type: "INTEGER",
- Nullable: false,
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."users" (
- "id" bigserial,
- "name" VARCHAR(255) NOT NULL
-);
-
-CREATE TABLE "public"."products" (
- "id" serial,
- "name" VARCHAR(255) NOT NULL
-);
-
-CREATE TABLE "public"."orders" (
- "id" smallserial,
- "user_id" INTEGER NOT NULL
-);
-
-`,
- },
- {
- name: "Identity columns should use GENERATED AS IDENTITY syntax",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "users_id_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- OwnerTable: "users",
- OwnerColumn: "id",
- },
- {
- Name: "products_id_seq",
- DataType: "integer",
- Start: "100",
- Increment: "5",
- MinValue: "1",
- MaxValue: "2147483647",
- Cycle: false,
- OwnerTable: "products",
- OwnerColumn: "id",
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "bigint",
- Nullable: false,
- IdentityGeneration: storepb.ColumnMetadata_ALWAYS,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- },
- },
- {
- Name: "products",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- Nullable: false,
- IdentityGeneration: storepb.ColumnMetadata_BY_DEFAULT,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."users" (
- "id" bigint GENERATED ALWAYS AS IDENTITY,
- "name" VARCHAR(255) NOT NULL
-);
-
-CREATE TABLE "public"."products" (
- "id" integer GENERATED BY DEFAULT AS IDENTITY (START WITH 100 INCREMENT BY 5),
- "name" VARCHAR(255) NOT NULL
-);
-
-`,
- },
- {
- name: "Table with indexes using custom opclass",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "documents",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "title",
- Type: "TEXT",
- Nullable: false,
- },
- {
- Name: "content",
- Type: "TEXT",
- Nullable: false,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "documents_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- {
- Name: "idx_documents_title_pattern",
- Expressions: []string{"title"},
- Type: "btree",
- OpclassNames: []string{"text_pattern_ops"},
- OpclassDefaults: []bool{false},
- },
- {
- Name: "idx_documents_title_content",
- Expressions: []string{"title", "content"},
- Type: "btree",
- OpclassNames: []string{"text_pattern_ops", "text_pattern_ops"},
- OpclassDefaults: []bool{false, false},
- },
- {
- Name: "idx_documents_default_opclass",
- Expressions: []string{"title"},
- Type: "btree",
- OpclassNames: []string{"text_ops"},
- OpclassDefaults: []bool{true}, // Default opclass should not be printed
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."documents" (
- "id" SERIAL NOT NULL,
- "title" TEXT NOT NULL,
- "content" TEXT NOT NULL,
- CONSTRAINT "documents_pkey" PRIMARY KEY (id)
-);
-
-CREATE INDEX "idx_documents_title_pattern" ON "public"."documents" (title text_pattern_ops);
-
-CREATE INDEX "idx_documents_title_content" ON "public"."documents" (title text_pattern_ops, content text_pattern_ops);
-
-CREATE INDEX "idx_documents_default_opclass" ON "public"."documents" (title);
-
-`,
- },
- {
- name: "Sequence with ownership (non-serial, non-identity)",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "custom_seq",
- DataType: "bigint",
- Start: "100",
- Increment: "5",
- MinValue: "100",
- MaxValue: "9223372036854775807",
- Cycle: false,
- CacheSize: "10",
- OwnerTable: "orders",
- OwnerColumn: "order_number",
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "orders",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "INTEGER",
- Nullable: false,
- },
- {
- Name: "order_number",
- Type: "BIGINT",
- Nullable: false,
- Default: "nextval('custom_seq'::regclass)",
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE SEQUENCE "public"."custom_seq" AS bigint START WITH 100 INCREMENT BY 5 MINVALUE 100 MAXVALUE 9223372036854775807 NO CYCLE CACHE 10;
-
-CREATE TABLE "public"."orders" (
- "id" INTEGER NOT NULL,
- "order_number" BIGINT DEFAULT nextval('custom_seq'::regclass) NOT NULL
-);
-
-ALTER SEQUENCE "public"."custom_seq" OWNED BY "public"."orders"."order_number";
-
-`,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- // TODO: Skip this test case temporarily - will be fixed in a future PR
- // to support ALTER SEQUENCE START WITH for serial columns
- if tt.name == "Sequence with ownership (non-serial, non-identity)" {
- t.Skip("Skipping test case - will support ALTER SEQUENCE START WITH for serial columns in future PR")
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, tt.metadata)
- require.NoError(t, err)
- assert.Equal(t, tt.expected, result)
- })
- }
-}
-
-func TestGetSchemaSDLDefinition(t *testing.T) {
- schemaMetadata := &storepb.SchemaMetadata{
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- },
- },
- },
- }
-
- result, err := GetSchemaSDLDefinition(schemaMetadata)
- require.NoError(t, err)
-
- expected := `CREATE TABLE "public"."users" (
- "id" SERIAL NOT NULL,
- "name" VARCHAR(255) NOT NULL
-);
-
-`
-
- assert.Equal(t, expected, result)
-}
-
-func TestGetDatabaseDefinitionNormalVsSDLFormat(t *testing.T) {
- // Create a more complex metadata to show the difference
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- },
- {
- Name: "email",
- Type: "VARCHAR(320)",
- Nullable: true,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "users_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- {
- Name: "idx_users_name",
- Expressions: []string{"name"},
- },
- {
- Name: "idx_users_email_unique",
- Expressions: []string{"email"},
- Unique: true,
- IsConstraint: false, // This is a unique index, not a constraint
- },
- },
- },
- },
- Views: []*storepb.ViewMetadata{
- {
- Name: "active_users_view",
- Definition: "SELECT id, name, email FROM users WHERE active = true",
- },
- },
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "count_active_users",
- Definition: `CREATE FUNCTION "public"."count_active_users"() RETURNS integer
- LANGUAGE sql
- AS $$
- SELECT COUNT(*)::integer FROM users WHERE active = true;
-$$`,
- },
- },
- },
- },
- }
-
- // Test SDL format is false (normal format)
- ctxNormal := schema.GetDefinitionContext{
- SDLFormat: false,
- }
-
- resultNormal, err := GetDatabaseDefinition(ctxNormal, metadata)
- require.NoError(t, err)
- assert.NotEqual(t, "", resultNormal)
- assert.Contains(t, resultNormal, "CREATE TABLE")
-
- // Test SDL format is true
- ctxSDL := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- resultSDL, err := GetDatabaseDefinition(ctxSDL, metadata)
- require.NoError(t, err)
- assert.NotEqual(t, "", resultSDL)
- assert.Contains(t, resultSDL, "CREATE TABLE")
-
- // SDL format should be different from normal format
- // Normal format has separate ALTER TABLE statements for constraints and separate CREATE INDEX
- // SDL format includes constraints within CREATE TABLE and indexes immediately after
- assert.Contains(t, resultNormal, "ALTER TABLE") // Normal format has separate constraint statements
- assert.NotContains(t, resultSDL, "ALTER TABLE") // SDL format should not have separate constraints
- assert.Contains(t, resultSDL, "CONSTRAINT") // SDL format should have inline constraints
- assert.Contains(t, resultSDL, "PRIMARY KEY") // SDL format should have inline PRIMARY KEY
- assert.Contains(t, resultSDL, "CREATE INDEX") // SDL format should have CREATE INDEX statements
- assert.Contains(t, resultSDL, "CREATE UNIQUE INDEX") // SDL format should have CREATE UNIQUE INDEX
- assert.Contains(t, resultSDL, "CREATE VIEW") // SDL format should have CREATE VIEW statements
- assert.Contains(t, resultSDL, "CREATE FUNCTION") // SDL format should have CREATE FUNCTION statements
-
- t.Logf("Normal format result: %q", resultNormal)
- t.Logf("SDL format result: %q", resultSDL)
-}
-
-// TestCheckConstraintNotValidFormat tests that CHECK constraints with NOT VALID
-// are formatted correctly without extra parentheses around the expression
-func TestCheckConstraintNotValidFormat(t *testing.T) {
- tests := []struct {
- name string
- metadata *storepb.DatabaseSchemaMetadata
- expected string
- }{
- {
- name: "Check constraint with NOT VALID",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "namespace_settings",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "namespace_id",
- Type: "bigint",
- Nullable: false,
- },
- {
- Name: "default_branch_protection_defaults",
- Type: "jsonb",
- Nullable: true,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "namespace_settings_pkey",
- Expressions: []string{"namespace_id"},
- Primary: true,
- },
- },
- CheckConstraints: []*storepb.CheckConstraintMetadata{
- {
- Name: "default_branch_protection_defaults_size_constraint",
- Expression: "(octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID",
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."namespace_settings" (
- "namespace_id" bigint NOT NULL,
- "default_branch_protection_defaults" jsonb,
- CONSTRAINT "namespace_settings_pkey" PRIMARY KEY (namespace_id),
- CONSTRAINT "default_branch_protection_defaults_size_constraint" CHECK (octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID
-);
-
-`,
- },
- {
- name: "Multiple check constraints with and without NOT VALID",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "test",
- Tables: []*storepb.TableMetadata{
- {
- Name: "table1",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "serial",
- Nullable: false,
- },
- {
- Name: "data",
- Type: "jsonb",
- Nullable: true,
- },
- {
- Name: "age",
- Type: "integer",
- Nullable: true,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "table1_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- },
- CheckConstraints: []*storepb.CheckConstraintMetadata{
- {
- Name: "table1_data_size_check",
- Expression: "(octet_length(data::text) <= 1024) NOT VALID",
- },
- {
- Name: "table1_age_check",
- Expression: "(age >= 18)",
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE SCHEMA IF NOT EXISTS "test";
-
-CREATE TABLE "test"."table1" (
- "id" serial NOT NULL,
- "data" jsonb,
- "age" integer,
- CONSTRAINT "table1_pkey" PRIMARY KEY (id),
- CONSTRAINT "table1_data_size_check" CHECK (octet_length(data::text) <= 1024) NOT VALID,
- CONSTRAINT "table1_age_check" CHECK (age >= 18)
-);
-
-`,
- },
- {
- name: "Regular check constraint without NOT VALID",
- metadata: &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "serial",
- Nullable: false,
- },
- {
- Name: "age",
- Type: "integer",
- Nullable: true,
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "users_pkey",
- Expressions: []string{"id"},
- Primary: true,
- },
- },
- CheckConstraints: []*storepb.CheckConstraintMetadata{
- {
- Name: "users_age_check",
- Expression: "(age >= 0)",
- },
- },
- },
- },
- },
- },
- },
- expected: `CREATE TABLE "public"."users" (
- "id" serial NOT NULL,
- "age" integer,
- CONSTRAINT "users_pkey" PRIMARY KEY (id),
- CONSTRAINT "users_age_check" CHECK (age >= 0)
-);
-
-`,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, tt.metadata)
- require.NoError(t, err)
- assert.Equal(t, tt.expected, result)
-
- // Additional validation: ensure NOT VALID (if present) is outside parentheses
- if strings.Contains(tt.expected, "NOT VALID") {
- // Check that we don't have the incorrect format: CHECK (...) NOT VALID)
- assert.NotContains(t, result, ") NOT VALID)", "NOT VALID should not be inside closing parenthesis")
- // Check that we have the correct format: CHECK (...) NOT VALID
- assert.Contains(t, result, ") NOT VALID", "NOT VALID should be after CHECK expression parenthesis")
- }
-
- // Validate that the generated SQL can be parsed without errors using ANTLR parser
- _, err = omnipg.Parse(result)
- require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
- })
- }
-}
-
-// TestCheckConstraintNotValidFormatNormalMode tests that CHECK constraints with NOT VALID
-// are formatted correctly in normal (non-SDL) mode
-func TestCheckConstraintNotValidFormatNormalMode(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "namespace_settings",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "namespace_id",
- Type: "bigint",
- Nullable: false,
- },
- {
- Name: "default_branch_protection_defaults",
- Type: "jsonb",
- Nullable: true,
- },
- },
- CheckConstraints: []*storepb.CheckConstraintMetadata{
- {
- Name: "default_branch_protection_defaults_size_constraint",
- Expression: "(octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID",
- },
- },
- },
- },
- },
- },
- }
-
- // Test normal format (SDLFormat: false)
- ctx := schema.GetDefinitionContext{
- SDLFormat: false,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- // Verify the CHECK constraint is output correctly in the CREATE TABLE statement
- assert.Contains(t, result, `CONSTRAINT "default_branch_protection_defaults_size_constraint" CHECK (octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID`)
-
- // Additional validation: ensure NOT VALID is outside parentheses (not inside extra parentheses)
- assert.NotContains(t, result, ") NOT VALID)", "NOT VALID should not be inside closing parenthesis")
- assert.Contains(t, result, ") NOT VALID", "NOT VALID should be after CHECK expression parenthesis")
-
- // Validate that the generated SQL can be parsed without errors using ANTLR parser
- _, err = omnipg.Parse(result)
- require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
-}
-
-func TestGetDatabaseDefinitionSDLFormat_WithComments(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "test_schema",
- Comment: "Test schema for comments",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Comment: "Users table with comments",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "SERIAL",
- Nullable: false,
- Comment: "User ID",
- },
- {
- Name: "name",
- Type: "VARCHAR(255)",
- Nullable: false,
- Comment: "User name",
- },
- {
- Name: "email",
- Type: "VARCHAR(320)",
- Nullable: true,
- Comment: "User email address",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "idx_users_email",
- Expressions: []string{"email"},
- Unique: false,
- Primary: false,
- Comment: "Index on email column",
- },
- },
- },
- },
- Views: []*storepb.ViewMetadata{
- {
- Name: "active_users",
- Definition: "SELECT id, name, email FROM test_schema.users WHERE active = true",
- Comment: "View of active users",
- },
- },
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "get_user_count",
- Signature: "get_user_count()",
- Definition: "CREATE FUNCTION test_schema.get_user_count() RETURNS INTEGER AS $$ BEGIN RETURN (SELECT COUNT(*) FROM test_schema.users); END; $$ LANGUAGE plpgsql",
- Comment: "Function to get user count",
- },
- },
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "custom_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- CacheSize: "1",
- Comment: "Custom sequence for testing",
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- // Verify schema comment
- assert.Contains(t, result, `CREATE SCHEMA IF NOT EXISTS "test_schema";`)
- assert.Contains(t, result, `COMMENT ON SCHEMA "test_schema" IS 'Test schema for comments';`)
-
- // Verify table comment
- assert.Contains(t, result, `COMMENT ON TABLE "test_schema"."users" IS 'Users table with comments';`)
-
- // Verify column comments
- assert.Contains(t, result, `COMMENT ON COLUMN "test_schema"."users"."id" IS 'User ID';`)
- assert.Contains(t, result, `COMMENT ON COLUMN "test_schema"."users"."name" IS 'User name';`)
- assert.Contains(t, result, `COMMENT ON COLUMN "test_schema"."users"."email" IS 'User email address';`)
-
- // Verify view comment
- assert.Contains(t, result, `COMMENT ON VIEW "test_schema"."active_users" IS 'View of active users';`)
-
- // Verify function comment
- assert.Contains(t, result, `COMMENT ON FUNCTION "test_schema".get_user_count() IS 'Function to get user count';`)
-
- // Verify sequence comment
- assert.Contains(t, result, `COMMENT ON SEQUENCE "test_schema"."custom_seq" IS 'Custom sequence for testing';`)
-
- // Verify index comment
- assert.Contains(t, result, `COMMENT ON INDEX "test_schema"."idx_users_email" IS 'Index on email column';`)
-
- // Validate that the generated SQL can be parsed without errors using ANTLR parser
- _, err = omnipg.Parse(result)
- require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
-}
-
-func TestGetDatabaseDefinitionSDLFormat_WithCommentsEscaping(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "test_table",
- Comment: "Table with 'single quotes' and \"double quotes\"",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "INTEGER",
- Nullable: false,
- Comment: "Column with 'quoted' text",
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- // Verify that single quotes are properly escaped
- assert.Contains(t, result, `COMMENT ON TABLE "public"."test_table" IS 'Table with ''single quotes'' and "double quotes"';`)
- assert.Contains(t, result, `COMMENT ON COLUMN "public"."test_table"."id" IS 'Column with ''quoted'' text';`)
-
- // Validate that the generated SQL can be parsed without errors using ANTLR parser
- _, err = omnipg.Parse(result)
- require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
-}
-
-func TestGetMultiFileDatabaseDefinition_WithComments(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "app_schema",
- Comment: "Application schema",
- Tables: []*storepb.TableMetadata{
- {
- Name: "products",
- Comment: "Product catalog",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "INTEGER",
- Nullable: false,
- Comment: "Product ID",
- },
- {
- Name: "name",
- Type: "TEXT",
- Nullable: false,
- Comment: "Product name",
- },
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "idx_products_name",
- Expressions: []string{"name"},
- Unique: false,
- Primary: false,
- Comment: "Index for product search",
- },
- },
- },
- },
- Views: []*storepb.ViewMetadata{
- {
- Name: "active_products",
- Definition: "SELECT id, name FROM app_schema.products WHERE active = true",
- Comment: "View of active products",
- },
- },
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "product_summary_mv",
- Definition: "SELECT id, name FROM app_schema.products",
- Comment: "Materialized view of product summary",
- },
- },
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "count_products",
- Signature: "count_products()",
- Definition: "CREATE FUNCTION app_schema.count_products() RETURNS INTEGER AS $$ BEGIN RETURN (SELECT COUNT(*) FROM app_schema.products); END; $$ LANGUAGE plpgsql",
- Comment: "Returns product count",
- },
- },
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "order_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- CacheSize: "1",
- Comment: "Sequence for orders",
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
- require.NotNil(t, result)
-
- // Build a map for easier testing
- fileMap := make(map[string]string)
- for _, file := range result.Files {
- fileMap[file.Name] = file.Content
- }
-
- // Verify schema-level file with schema comment
- schemaFile, ok := fileMap["schemas/app_schema/schema.sql"]
- require.True(t, ok, "schema.sql file should exist for non-public schema")
- assert.Contains(t, schemaFile, `CREATE SCHEMA IF NOT EXISTS "app_schema";`)
- assert.Contains(t, schemaFile, `COMMENT ON SCHEMA "app_schema" IS 'Application schema';`)
-
- // Verify consolidated sequences file with comment (independent sequences go in sequences.sql)
- sequenceFile, ok := fileMap["schemas/app_schema/sequences.sql"]
- require.True(t, ok, "sequences.sql file should exist for independent sequences")
- assert.Contains(t, sequenceFile, `CREATE SEQUENCE "app_schema"."order_seq"`)
- assert.Contains(t, sequenceFile, `COMMENT ON SEQUENCE "app_schema"."order_seq" IS 'Sequence for orders';`)
-
- // Verify table file with comments
- tableFile, ok := fileMap["schemas/app_schema/tables/products.sql"]
- require.True(t, ok, "table file should exist")
- assert.Contains(t, tableFile, `CREATE TABLE "app_schema"."products"`)
- assert.Contains(t, tableFile, `COMMENT ON TABLE "app_schema"."products" IS 'Product catalog';`)
- assert.Contains(t, tableFile, `COMMENT ON COLUMN "app_schema"."products"."id" IS 'Product ID';`)
- assert.Contains(t, tableFile, `COMMENT ON COLUMN "app_schema"."products"."name" IS 'Product name';`)
- assert.Contains(t, tableFile, `COMMENT ON INDEX "app_schema"."idx_products_name" IS 'Index for product search';`)
-
- // Verify view file with comment
- viewFile, ok := fileMap["schemas/app_schema/views/active_products.sql"]
- require.True(t, ok, "view file should exist")
- assert.Contains(t, viewFile, `CREATE VIEW "app_schema"."active_products"`)
- assert.Contains(t, viewFile, `COMMENT ON VIEW "app_schema"."active_products" IS 'View of active products';`)
-
- // Verify materialized view file with comment
- materializedViewFile, ok := fileMap["schemas/app_schema/materialized_views/product_summary_mv.sql"]
- require.True(t, ok, "materialized view file should exist")
- assert.Contains(t, materializedViewFile, `CREATE MATERIALIZED VIEW "app_schema"."product_summary_mv"`)
- assert.Contains(t, materializedViewFile, `COMMENT ON MATERIALIZED VIEW "app_schema"."product_summary_mv" IS 'Materialized view of product summary';`)
-
- // Verify function file with comment
- functionFile, ok := fileMap["schemas/app_schema/functions/count_products.sql"]
- require.True(t, ok, "function file should exist")
- assert.Contains(t, functionFile, `CREATE FUNCTION app_schema.count_products()`)
- assert.Contains(t, functionFile, `COMMENT ON FUNCTION "app_schema".count_products() IS 'Returns product count';`)
-
- // Validate that each file's SQL can be parsed
- for fileName, content := range fileMap {
- _, err := omnipg.Parse(content)
- require.NoError(t, err, "SQL in file %s should be parseable by PostgreSQL parser", fileName)
- }
-}
-
-func TestGetMultiFileDatabaseDefinition_SchemaFileMakesCombinedSDLLoadable(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "metric_helpers",
- Tables: []*storepb.TableMetadata{
- {
- Name: "metrics",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "INTEGER",
- Nullable: false,
- },
- },
- },
- },
- },
- },
- }
-
- result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
- require.NoError(t, err)
-
- fileMap := make(map[string]string)
- var combined strings.Builder
- for _, file := range result.Files {
- fileMap[file.Name] = file.Content
- combined.WriteString(file.Content)
- combined.WriteString("\n")
- }
-
- schemaFile, ok := fileMap["schemas/metric_helpers/schema.sql"]
- assert.True(t, ok, "schema.sql file should exist for non-public schema")
- assert.Contains(t, schemaFile, `CREATE SCHEMA IF NOT EXISTS "metric_helpers";`)
-
- _, err = schema.DiffSDLMigration(storepb.Engine_POSTGRES, "", combined.String(), "")
- require.NoError(t, err)
-}
-
-func TestGetDatabaseDefinitionSDLFormat_SerialColumnWithSequence(t *testing.T) {
- // This test reproduces the issue where a SERIAL column causes duplicate CREATE SEQUENCE statements
- // SERIAL columns automatically create sequences, so we should NOT output separate CREATE SEQUENCE for them
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "users_id_seq",
- DataType: "integer",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "2147483647",
- Cycle: false,
- OwnerTable: "users",
- OwnerColumn: "id",
- Comment: "Sequence for users id - should NOT appear in SDL",
- },
- {
- Name: "independent_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- OwnerTable: "", // Independent sequence - should appear in SDL
- Comment: "Independent sequence - should appear in SDL",
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- Nullable: false,
- Default: "nextval('users_id_seq'::regclass)",
- },
- {
- Name: "name",
- Type: "text",
- Nullable: false,
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- // The SDL should contain the table definition with serial type
- require.Contains(t, result, `CREATE TABLE "public"."users"`)
- require.Contains(t, result, `"id" serial`)
- require.Contains(t, result, `"name" text NOT NULL`)
-
- // The SDL SHOULD contain the independent sequence
- require.Contains(t, result, `CREATE SEQUENCE "public"."independent_seq"`,
- "SDL should contain CREATE SEQUENCE for independent sequences")
-
- // The SDL should NOT contain a separate CREATE SEQUENCE statement for users_id_seq
- // because the sequence is owned by the serial column (created implicitly by SERIAL type)
- require.NotContains(t, result, `CREATE SEQUENCE "public"."users_id_seq"`,
- "SDL should NOT contain CREATE SEQUENCE for sequence owned by a column (created implicitly by SERIAL)")
-}
-
-func TestGetMultiFileDatabaseDefinition_SerialColumnWithSequence(t *testing.T) {
- // This test verifies that multi-file format correctly handles serial columns and their sequences
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "users_id_seq",
- DataType: "integer",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "2147483647",
- Cycle: false,
- OwnerTable: "users",
- OwnerColumn: "id",
- Comment: "Sequence for users id - should NOT appear as separate file",
- },
- {
- Name: "independent_seq",
- DataType: "bigint",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "9223372036854775807",
- Cycle: false,
- OwnerTable: "", // Independent sequence - not owned by any column
- Comment: "Independent sequence - should appear as separate file",
- },
- {
- Name: "custom_seq",
- DataType: "bigint",
- Start: "100",
- Increment: "5",
- MinValue: "100",
- MaxValue: "9223372036854775807",
- Cycle: false,
- OwnerTable: "", // Not owned by a column (independent sequence)
- Comment: "Custom sequence for orders - should appear as separate file",
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- Nullable: false,
- Default: "nextval('users_id_seq'::regclass)",
- },
- {
- Name: "name",
- Type: "text",
- Nullable: false,
- },
- },
- },
- {
- Name: "orders",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- Nullable: false,
- },
- {
- Name: "order_number",
- Type: "bigint",
- Nullable: false,
- Default: "nextval('custom_seq'::regclass)",
- Comment: "Order number using custom sequence",
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- // Build a map of files for easy lookup
- fileMap := make(map[string]string)
- for _, file := range result.Files {
- fileMap[file.Name] = file.Content
- }
-
- // Verify users table file exists and contains serial column
- usersFile, ok := fileMap["schemas/public/tables/users.sql"]
- require.True(t, ok, "users table file should exist")
- require.Contains(t, usersFile, `"id" serial`, "users table should use serial type")
- require.Contains(t, usersFile, `"name" text NOT NULL`)
-
- // Verify orders table file exists
- ordersFile, ok := fileMap["schemas/public/tables/orders.sql"]
- require.True(t, ok, "orders table file should exist")
- require.Contains(t, ordersFile, `"id" integer NOT NULL`)
- require.Contains(t, ordersFile, `"order_number" bigint DEFAULT nextval('custom_seq'::regclass) NOT NULL`,
- "order_number should use custom_seq via DEFAULT nextval()")
-
- // Verify the consolidated sequences file exists with both independent sequences
- sequencesFile, ok := fileMap["schemas/public/sequences.sql"]
- require.True(t, ok, "independent_seq file should exist")
- require.Contains(t, sequencesFile, `CREATE SEQUENCE "public"."independent_seq"`)
- require.Contains(t, sequencesFile, `COMMENT ON SEQUENCE "public"."independent_seq" IS 'Independent sequence - should appear as separate file'`)
- require.Contains(t, sequencesFile, `CREATE SEQUENCE "public"."custom_seq"`)
- require.Contains(t, sequencesFile, `COMMENT ON SEQUENCE "public"."custom_seq" IS 'Custom sequence for orders - should appear as separate file'`)
- require.NotContains(t, sequencesFile, `ALTER SEQUENCE`, "Independent sequences should not have ALTER SEQUENCE OWNED BY")
-
- // Verify individual sequence files do NOT exist (they should be in the consolidated file)
- _, ok = fileMap["schemas/public/sequences/independent_seq.sql"]
- require.False(t, ok, "independent_seq should NOT have individual file (should be in sequences.sql)")
- _, ok = fileMap["schemas/public/sequences/custom_seq.sql"]
- require.False(t, ok, "custom_seq should NOT have individual file (should be in sequences.sql)")
-
- // Verify users_id_seq does NOT appear in the sequences file (it's a serial sequence)
- require.NotContains(t, sequencesFile, "users_id_seq", "users_id_seq should NOT appear in sequences.sql because it's owned by a serial column")
-
- // Total files should be: 2 tables + 1 sequences file = 3 files
- require.Equal(t, 3, len(result.Files), "Should have exactly 3 files (2 tables + 1 consolidated sequences file)")
-}
-
-func TestGetDatabaseDefinitionSDLFormat_MultipleSequencesClaimingOwnership(t *testing.T) {
- // This test verifies that when multiple sequences claim ownership of the same column,
- // only the sequence referenced in the DEFAULT clause is skipped (treated as serial sequence).
- // The other sequences should still be output as CREATE SEQUENCE statements.
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Sequences: []*storepb.SequenceMetadata{
- {
- Name: "test_sequence2",
- DataType: "integer",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "2147483647",
- Cycle: false,
- OwnerTable: "test_table",
- OwnerColumn: "id",
- },
- {
- Name: "test_table_id_seq",
- DataType: "integer",
- Start: "1",
- Increment: "1",
- MinValue: "1",
- MaxValue: "2147483647",
- Cycle: false,
- OwnerTable: "test_table",
- OwnerColumn: "id",
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "test_table",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- Nullable: false,
- Default: "nextval('test_table_id_seq'::regclass)",
- },
- {
- Name: "name",
- Type: "text",
- Nullable: false,
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- expected := `CREATE SEQUENCE "public"."test_sequence2" AS integer START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 NO CYCLE;
-
-CREATE TABLE "public"."test_table" (
- "id" serial,
- "name" text NOT NULL
-);
-
-ALTER SEQUENCE "public"."test_sequence2" OWNED BY "public"."test_table"."id";
-
-`
-
- assert.Equal(t, expected, result)
-
- // Verify that test_sequence2 is output (not skipped)
- assert.Contains(t, result, `CREATE SEQUENCE "public"."test_sequence2"`,
- "test_sequence2 should be output because it's not referenced in DEFAULT clause")
-
- // Verify that test_sequence2 has ALTER SEQUENCE OWNED BY statement
- assert.Contains(t, result, `ALTER SEQUENCE "public"."test_sequence2" OWNED BY "public"."test_table"."id"`,
- "test_sequence2 should have ALTER SEQUENCE OWNED BY because metadata indicates it's owned by the column")
-
- // Verify that test_table_id_seq is NOT output (skipped)
- assert.NotContains(t, result, `CREATE SEQUENCE "public"."test_table_id_seq"`,
- "test_table_id_seq should be skipped because it's referenced in DEFAULT clause and treated as serial")
-
- // Verify that the column uses serial type
- assert.Contains(t, result, `"id" serial`,
- "Column should use serial type because it references test_table_id_seq which is owned by it")
-
- // Validate that the generated SQL can be parsed
- _, err = omnipg.Parse(result)
- require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
-}
-
-func TestGetDatabaseDefinitionSDLFormat_ProcedureWithComment(t *testing.T) {
- // This test verifies that PROCEDURE comments are correctly generated with
- // COMMENT ON PROCEDURE (not COMMENT ON FUNCTION)
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "get_user_count",
- Signature: "get_user_count()",
- Definition: `CREATE FUNCTION "public"."get_user_count"() RETURNS integer
- LANGUAGE sql
- AS $$
- SELECT COUNT(*)::integer FROM users;
-$$`,
- Comment: "Function to count users",
- },
- {
- Name: "update_user_name",
- Signature: "update_user_name(user_id integer, new_name character varying)",
- Definition: `CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
- LANGUAGE plpgsql
- AS $$
-BEGIN
- UPDATE users
- SET name = new_name
- WHERE id = user_id;
-END;
-$$`,
- Comment: "Procedure to update user name",
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- // Verify that FUNCTION has COMMENT ON FUNCTION
- assert.Contains(t, result, `COMMENT ON FUNCTION "public".get_user_count() IS 'Function to count users';`,
- "Function comment should use COMMENT ON FUNCTION")
-
- // Verify that PROCEDURE has COMMENT ON PROCEDURE (not COMMENT ON FUNCTION)
- assert.Contains(t, result, `COMMENT ON PROCEDURE "public".update_user_name(integer, character varying) IS 'Procedure to update user name';`,
- "Procedure comment should use COMMENT ON PROCEDURE with types-only signature")
-
- // Verify that we don't incorrectly use COMMENT ON FUNCTION for the procedure
- assert.NotContains(t, result, `COMMENT ON FUNCTION "public".update_user_name`,
- "Procedure comment should NOT use COMMENT ON FUNCTION")
-
- // Validate that the generated SQL can be parsed
- _, err = omnipg.Parse(result)
- require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
-}
-
-func TestGetMultiFileDatabaseDefinition_FunctionAndProcedureSeparation(t *testing.T) {
- // This test verifies that functions and procedures are separated into different folders
- // in multi-file mode
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "get_user_count",
- Definition: `CREATE FUNCTION "public"."get_user_count"() RETURNS integer
- LANGUAGE sql
- AS $$
- SELECT COUNT(*)::integer FROM users;
-$$`,
- },
- {
- Name: "get_user_by_id",
- Definition: `CREATE FUNCTION "public"."get_user_by_id"(user_id integer) RETURNS TABLE(id integer, name character varying)
- LANGUAGE sql
- AS $$
- SELECT u.id, u.name FROM users u WHERE u.id = user_id;
-$$`,
- },
- {
- Name: "update_user_name",
- Definition: `CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
- LANGUAGE plpgsql
- AS $$
-BEGIN
- UPDATE users SET name = new_name WHERE id = user_id;
-END;
-$$`,
- },
- {
- Name: "delete_old_users",
- Definition: `CREATE PROCEDURE "public"."delete_old_users"(IN days_old integer)
- LANGUAGE plpgsql
- AS $$
-BEGIN
- DELETE FROM users WHERE created_at < NOW() - INTERVAL '1 day' * days_old;
-END;
-$$`,
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
- require.NotNil(t, result)
-
- // Build a map of files for easy lookup
- fileMap := make(map[string]string)
- for _, file := range result.Files {
- fileMap[file.Name] = file.Content
- }
-
- // Verify functions are in functions folder
- getUserCountFile, ok := fileMap["schemas/public/functions/get_user_count.sql"]
- require.True(t, ok, "get_user_count function should be in functions folder")
- assert.Contains(t, getUserCountFile, `CREATE FUNCTION "public"."get_user_count"()`)
-
- getUserByIDFile, ok := fileMap["schemas/public/functions/get_user_by_id.sql"]
- require.True(t, ok, "get_user_by_id function should be in functions folder")
- assert.Contains(t, getUserByIDFile, `CREATE FUNCTION "public"."get_user_by_id"`)
-
- // Verify procedures are in procedures folder (not functions folder)
- updateUserNameFile, ok := fileMap["schemas/public/procedures/update_user_name.sql"]
- require.True(t, ok, "update_user_name procedure should be in procedures folder")
- assert.Contains(t, updateUserNameFile, `CREATE PROCEDURE "public"."update_user_name"`)
-
- deleteOldUsersFile, ok := fileMap["schemas/public/procedures/delete_old_users.sql"]
- require.True(t, ok, "delete_old_users procedure should be in procedures folder")
- assert.Contains(t, deleteOldUsersFile, `CREATE PROCEDURE "public"."delete_old_users"`)
-
- // Verify procedures are NOT in functions folder
- _, ok = fileMap["schemas/public/functions/update_user_name.sql"]
- require.False(t, ok, "update_user_name should NOT be in functions folder")
-
- _, ok = fileMap["schemas/public/functions/delete_old_users.sql"]
- require.False(t, ok, "delete_old_users should NOT be in functions folder")
-
- // Total files should be: 2 functions + 2 procedures = 4 files
- require.Equal(t, 4, len(result.Files), "Should have exactly 4 files (2 functions + 2 procedures)")
-
- // Validate that each file's SQL can be parsed
- for fileName, content := range fileMap {
- _, err := omnipg.Parse(content)
- require.NoError(t, err, "SQL in file %s should be parseable by PostgreSQL parser", fileName)
- }
-}
-
-func TestOverloadedFunctionsPreservedInDump(t *testing.T) {
- oneArgDef := `CREATE OR REPLACE FUNCTION "public"."camel_to_snake"(camel_cased_text character varying)
- RETURNS character varying
- LANGUAGE plpgsql
- IMMUTABLE
-AS $function$
-BEGIN
- RETURN lower(camel_cased_text);
-END;
-$function$`
- twoArgDef := `CREATE OR REPLACE FUNCTION "public"."camel_to_snake"(camel_cased_text character varying, case_directive character varying)
- RETURNS character varying
- LANGUAGE plpgsql
- IMMUTABLE
-AS $function$
-BEGIN
- RETURN lower(camel_cased_text || '_' || case_directive);
-END;
-$function$`
-
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Functions: []*storepb.FunctionMetadata{
- {
- Name: "camel_to_snake",
- Signature: "camel_to_snake(camel_cased_text character varying)",
- Definition: oneArgDef,
- },
- {
- Name: "camel_to_snake",
- Signature: "camel_to_snake(camel_cased_text character varying, case_directive character varying)",
- Definition: twoArgDef,
- },
- },
- },
- },
- }
-
- oneArgSubstring := `"camel_to_snake"(camel_cased_text character varying)`
- twoArgSubstring := `"camel_to_snake"(camel_cased_text character varying, case_directive character varying)`
-
- // Test non-SDL format (GetDatabaseDefinition)
- result, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, metadata)
- require.NoError(t, err)
- assert.Contains(t, result, oneArgSubstring)
- assert.Contains(t, result, twoArgSubstring)
- assert.Equal(t, 2, strings.Count(result, "CREATE OR REPLACE FUNCTION"), "both overloads must survive in non-SDL dump")
-
- // Test SDL format
- resultSDL, err := GetDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
- require.NoError(t, err)
- assert.Contains(t, resultSDL, oneArgSubstring)
- assert.Contains(t, resultSDL, twoArgSubstring)
- assert.Equal(t, 2, strings.Count(resultSDL, "CREATE OR REPLACE FUNCTION"), "both overloads must survive in SDL dump")
-
- // Test single-schema path (GetSchemaDefinition)
- resultSchema, err := GetSchemaDefinition(metadata.Schemas[0])
- require.NoError(t, err)
- assert.Contains(t, resultSchema, oneArgSubstring)
- assert.Contains(t, resultSchema, twoArgSubstring)
- assert.Equal(t, 2, strings.Count(resultSchema, "CREATE OR REPLACE FUNCTION"), "both overloads must survive in single-schema dump")
-}
diff --git a/backend/plugin/schema/pg/get_database_definition_test.go b/backend/plugin/schema/pg/get_database_definition_test.go
new file mode 100644
index 0000000000..02e5d4057d
--- /dev/null
+++ b/backend/plugin/schema/pg/get_database_definition_test.go
@@ -0,0 +1,4388 @@
+package pg
+
+import (
+ "context"
+ "fmt"
+ "strings"
+ "testing"
+
+ omnipg "github.com/bytebase/omni/pg"
+ "github.com/bytebase/omni/pg/catalog"
+ "github.com/google/uuid"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/store/model"
+)
+
+func TestGetDatabaseDefinitionSDLFormat(t *testing.T) {
+ tests := []struct {
+ name string
+ metadata *storepb.DatabaseSchemaMetadata
+ expected string
+ }{
+ {
+ name: "Simple table with basic columns",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ {
+ Name: "email",
+ Type: "VARCHAR(320)",
+ Nullable: true,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."users" (
+ "id" SERIAL NOT NULL,
+ "name" VARCHAR(255) NOT NULL,
+ "email" VARCHAR(320)
+);
+
+`,
+ },
+ {
+ name: "Table with default values",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "products",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ {
+ Name: "price",
+ Type: "DECIMAL(10,2)",
+ Default: "0.00",
+ Nullable: false,
+ },
+ {
+ Name: "active",
+ Type: "BOOLEAN",
+ Default: "true",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."products" (
+ "id" SERIAL NOT NULL,
+ "name" VARCHAR(255) NOT NULL,
+ "price" DECIMAL(10,2) DEFAULT 0.00 NOT NULL,
+ "active" BOOLEAN DEFAULT true NOT NULL
+);
+
+`,
+ },
+ {
+ name: "Table with constraints",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "email",
+ Type: "VARCHAR(320)",
+ Nullable: false,
+ },
+ {
+ Name: "age",
+ Type: "INTEGER",
+ Nullable: true,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "users_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ {
+ Name: "users_email_key",
+ Expressions: []string{"email"},
+ Unique: true,
+ IsConstraint: true,
+ },
+ },
+ CheckConstraints: []*storepb.CheckConstraintMetadata{
+ {
+ Name: "users_age_check",
+ Expression: "(age >= 0)",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."users" (
+ "id" SERIAL NOT NULL,
+ "email" VARCHAR(320) NOT NULL,
+ "age" INTEGER,
+ CONSTRAINT "users_pkey" PRIMARY KEY (id),
+ CONSTRAINT "users_email_key" UNIQUE (email),
+ CONSTRAINT "users_age_check" CHECK (age >= 0)
+);
+
+`,
+ },
+ {
+ name: "Table with foreign key",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "user_id",
+ Type: "INTEGER",
+ Nullable: false,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "orders_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ },
+ ForeignKeys: []*storepb.ForeignKeyMetadata{
+ {
+ Name: "orders_user_id_fkey",
+ Columns: []string{"user_id"},
+ ReferencedSchema: "public",
+ ReferencedTable: "users",
+ ReferencedColumns: []string{"id"},
+ OnDelete: "CASCADE",
+ OnUpdate: "NO ACTION",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."orders" (
+ "id" SERIAL NOT NULL,
+ "user_id" INTEGER NOT NULL,
+ CONSTRAINT "orders_pkey" PRIMARY KEY (id),
+ CONSTRAINT "orders_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON DELETE CASCADE
+);
+
+`,
+ },
+ {
+ name: "Multiple tables",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "categories",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(100)",
+ Nullable: false,
+ },
+ },
+ },
+ {
+ Name: "products",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "category_id",
+ Type: "INTEGER",
+ Nullable: true,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."categories" (
+ "id" SERIAL NOT NULL,
+ "name" VARCHAR(100) NOT NULL
+);
+
+CREATE TABLE "public"."products" (
+ "id" SERIAL NOT NULL,
+ "category_id" INTEGER
+);
+
+`,
+ },
+ {
+ name: "Table with indexes",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "products",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ {
+ Name: "category_id",
+ Type: "INTEGER",
+ Nullable: true,
+ },
+ {
+ Name: "price",
+ Type: "DECIMAL(10,2)",
+ Nullable: false,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "products_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ {
+ Name: "idx_products_name",
+ Expressions: []string{"name"},
+ },
+ {
+ Name: "idx_products_category_price",
+ Expressions: []string{"category_id", "price"},
+ Descending: []bool{false, true}, // price DESC
+ },
+ {
+ Name: "idx_products_name_unique",
+ Expressions: []string{"name"},
+ Unique: true,
+ IsConstraint: false, // This is a unique index, not a unique constraint
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."products" (
+ "id" SERIAL NOT NULL,
+ "name" VARCHAR(255) NOT NULL,
+ "category_id" INTEGER,
+ "price" DECIMAL(10,2) NOT NULL,
+ CONSTRAINT "products_pkey" PRIMARY KEY (id)
+);
+
+CREATE INDEX "idx_products_name" ON "public"."products" (name);
+
+CREATE INDEX "idx_products_category_price" ON "public"."products" (category_id, price DESC);
+
+CREATE UNIQUE INDEX "idx_products_name_unique" ON "public"."products" (name);
+
+`,
+ },
+ {
+ name: "Table with views",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ {
+ Name: "email",
+ Type: "VARCHAR(320)",
+ Nullable: false,
+ },
+ {
+ Name: "active",
+ Type: "BOOLEAN",
+ Default: "true",
+ Nullable: false,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "users_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ },
+ },
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "user_id",
+ Type: "INTEGER",
+ Nullable: false,
+ },
+ {
+ Name: "total",
+ Type: "DECIMAL(10,2)",
+ Nullable: false,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "orders_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ },
+ },
+ },
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "active_users",
+ Definition: `SELECT id, name, email
+ FROM users
+ WHERE active = true`,
+ },
+ {
+ Name: "user_order_summary",
+ Definition: `SELECT
+ u.id,
+ u.name,
+ COUNT(o.id) as order_count,
+ COALESCE(SUM(o.total), 0) as total_amount
+FROM users u
+LEFT JOIN orders o ON u.id = o.user_id
+GROUP BY u.id, u.name`,
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."users" (
+ "id" SERIAL NOT NULL,
+ "name" VARCHAR(255) NOT NULL,
+ "email" VARCHAR(320) NOT NULL,
+ "active" BOOLEAN DEFAULT true NOT NULL,
+ CONSTRAINT "users_pkey" PRIMARY KEY (id)
+);
+
+CREATE TABLE "public"."orders" (
+ "id" SERIAL NOT NULL,
+ "user_id" INTEGER NOT NULL,
+ "total" DECIMAL(10,2) NOT NULL,
+ CONSTRAINT "orders_pkey" PRIMARY KEY (id)
+);
+
+CREATE VIEW "public"."active_users" AS SELECT id, name, email
+ FROM users
+ WHERE active = true;
+
+CREATE VIEW "public"."user_order_summary" AS SELECT
+ u.id,
+ u.name,
+ COUNT(o.id) as order_count,
+ COALESCE(SUM(o.total), 0) as total_amount
+FROM users u
+LEFT JOIN orders o ON u.id = o.user_id
+GROUP BY u.id, u.name;
+
+`,
+ },
+ {
+ name: "Database with functions and procedures",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ {
+ Name: "created_at",
+ Type: "TIMESTAMP",
+ Default: "CURRENT_TIMESTAMP",
+ Nullable: false,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "users_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ },
+ },
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "get_user_count",
+ Definition: `CREATE FUNCTION "public"."get_user_count"() RETURNS integer
+ LANGUAGE sql
+ AS $$
+ SELECT COUNT(*)::integer FROM users;
+$$`,
+ },
+ {
+ Name: "get_user_by_id",
+ Definition: `CREATE FUNCTION "public"."get_user_by_id"(user_id integer) RETURNS TABLE(id integer, name character varying, created_at timestamp without time zone)
+ LANGUAGE sql
+ AS $$
+ SELECT u.id, u.name, u.created_at
+ FROM users u
+ WHERE u.id = user_id;
+$$`,
+ },
+ {
+ Name: "update_user_name",
+ Definition: `CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
+ LANGUAGE plpgsql
+ AS $$
+BEGIN
+ UPDATE users
+ SET name = new_name
+ WHERE id = user_id;
+
+ IF NOT FOUND THEN
+ RAISE EXCEPTION 'User with id % not found', user_id;
+ END IF;
+END;
+$$`,
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."users" (
+ "id" SERIAL NOT NULL,
+ "name" VARCHAR(255) NOT NULL,
+ "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
+ CONSTRAINT "users_pkey" PRIMARY KEY (id)
+);
+
+CREATE FUNCTION "public"."get_user_count"() RETURNS integer
+ LANGUAGE sql
+ AS $$
+ SELECT COUNT(*)::integer FROM users;
+$$;
+
+CREATE FUNCTION "public"."get_user_by_id"(user_id integer) RETURNS TABLE(id integer, name character varying, created_at timestamp without time zone)
+ LANGUAGE sql
+ AS $$
+ SELECT u.id, u.name, u.created_at
+ FROM users u
+ WHERE u.id = user_id;
+$$;
+
+CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
+ LANGUAGE plpgsql
+ AS $$
+BEGIN
+ UPDATE users
+ SET name = new_name
+ WHERE id = user_id;
+
+ IF NOT FOUND THEN
+ RAISE EXCEPTION 'User with id % not found', user_id;
+ END IF;
+END;
+$$;
+
+`,
+ },
+ {
+ name: "Database with sequences",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "independent_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ OwnerTable: "", // Independent sequence (not owned by any table)
+ },
+ {
+ Name: "user_id_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ OwnerTable: "users",
+ OwnerColumn: "id",
+ },
+ {
+ Name: "order_seq",
+ DataType: "integer",
+ Start: "1000",
+ Increment: "10",
+ MinValue: "1000",
+ MaxValue: "999999",
+ Cycle: true,
+ OwnerTable: "", // Independent sequence
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "INTEGER",
+ Nullable: false,
+ Default: "nextval('user_id_seq'::regclass)",
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE SEQUENCE "public"."independent_seq" AS bigint START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 NO CYCLE;
+
+CREATE SEQUENCE "public"."order_seq" AS integer START WITH 1000 INCREMENT BY 10 MINVALUE 1000 MAXVALUE 999999 CYCLE;
+
+CREATE TABLE "public"."users" (
+ "id" serial,
+ "name" VARCHAR(255) NOT NULL
+);
+
+`,
+ },
+ {
+ name: "Empty database",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{},
+ },
+ expected: "",
+ },
+ {
+ name: "Serial columns should use serial types",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "users_id_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ OwnerTable: "users",
+ OwnerColumn: "id",
+ },
+ {
+ Name: "products_id_seq",
+ DataType: "integer",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "2147483647",
+ Cycle: false,
+ OwnerTable: "products",
+ OwnerColumn: "id",
+ },
+ {
+ Name: "orders_id_seq",
+ DataType: "smallint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "32767",
+ Cycle: false,
+ OwnerTable: "orders",
+ OwnerColumn: "id",
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "bigint",
+ Nullable: false,
+ Default: "nextval('users_id_seq'::regclass)",
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ },
+ },
+ {
+ Name: "products",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "integer",
+ Nullable: false,
+ Default: "nextval('products_id_seq'::regclass)",
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ },
+ },
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "smallint",
+ Nullable: false,
+ Default: "nextval('orders_id_seq'::regclass)",
+ },
+ {
+ Name: "user_id",
+ Type: "INTEGER",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."users" (
+ "id" bigserial,
+ "name" VARCHAR(255) NOT NULL
+);
+
+CREATE TABLE "public"."products" (
+ "id" serial,
+ "name" VARCHAR(255) NOT NULL
+);
+
+CREATE TABLE "public"."orders" (
+ "id" smallserial,
+ "user_id" INTEGER NOT NULL
+);
+
+`,
+ },
+ {
+ name: "Identity columns should use GENERATED AS IDENTITY syntax",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "users_id_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ OwnerTable: "users",
+ OwnerColumn: "id",
+ },
+ {
+ Name: "products_id_seq",
+ DataType: "integer",
+ Start: "100",
+ Increment: "5",
+ MinValue: "1",
+ MaxValue: "2147483647",
+ Cycle: false,
+ OwnerTable: "products",
+ OwnerColumn: "id",
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "bigint",
+ Nullable: false,
+ IdentityGeneration: storepb.ColumnMetadata_ALWAYS,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ },
+ },
+ {
+ Name: "products",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "integer",
+ Nullable: false,
+ IdentityGeneration: storepb.ColumnMetadata_BY_DEFAULT,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."users" (
+ "id" bigint GENERATED ALWAYS AS IDENTITY,
+ "name" VARCHAR(255) NOT NULL
+);
+
+CREATE TABLE "public"."products" (
+ "id" integer GENERATED BY DEFAULT AS IDENTITY (START WITH 100 INCREMENT BY 5),
+ "name" VARCHAR(255) NOT NULL
+);
+
+`,
+ },
+ {
+ name: "Table with indexes using custom opclass",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "documents",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "title",
+ Type: "TEXT",
+ Nullable: false,
+ },
+ {
+ Name: "content",
+ Type: "TEXT",
+ Nullable: false,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "documents_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ {
+ Name: "idx_documents_title_pattern",
+ Expressions: []string{"title"},
+ Type: "btree",
+ OpclassNames: []string{"text_pattern_ops"},
+ OpclassDefaults: []bool{false},
+ },
+ {
+ Name: "idx_documents_title_content",
+ Expressions: []string{"title", "content"},
+ Type: "btree",
+ OpclassNames: []string{"text_pattern_ops", "text_pattern_ops"},
+ OpclassDefaults: []bool{false, false},
+ },
+ {
+ Name: "idx_documents_default_opclass",
+ Expressions: []string{"title"},
+ Type: "btree",
+ OpclassNames: []string{"text_ops"},
+ OpclassDefaults: []bool{true}, // Default opclass should not be printed
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."documents" (
+ "id" SERIAL NOT NULL,
+ "title" TEXT NOT NULL,
+ "content" TEXT NOT NULL,
+ CONSTRAINT "documents_pkey" PRIMARY KEY (id)
+);
+
+CREATE INDEX "idx_documents_title_pattern" ON "public"."documents" (title text_pattern_ops);
+
+CREATE INDEX "idx_documents_title_content" ON "public"."documents" (title text_pattern_ops, content text_pattern_ops);
+
+CREATE INDEX "idx_documents_default_opclass" ON "public"."documents" (title);
+
+`,
+ },
+ {
+ name: "Sequence with ownership (non-serial, non-identity)",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "custom_seq",
+ DataType: "bigint",
+ Start: "100",
+ Increment: "5",
+ MinValue: "100",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ CacheSize: "10",
+ OwnerTable: "orders",
+ OwnerColumn: "order_number",
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "INTEGER",
+ Nullable: false,
+ },
+ {
+ Name: "order_number",
+ Type: "BIGINT",
+ Nullable: false,
+ Default: "nextval('custom_seq'::regclass)",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE SEQUENCE "public"."custom_seq" AS bigint START WITH 100 INCREMENT BY 5 MINVALUE 100 MAXVALUE 9223372036854775807 NO CYCLE CACHE 10;
+
+CREATE TABLE "public"."orders" (
+ "id" INTEGER NOT NULL,
+ "order_number" BIGINT DEFAULT nextval('custom_seq'::regclass) NOT NULL
+);
+
+ALTER SEQUENCE "public"."custom_seq" OWNED BY "public"."orders"."order_number";
+
+`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ // TODO: Skip this test case temporarily - will be fixed in a future PR
+ // to support ALTER SEQUENCE START WITH for serial columns
+ if tt.name == "Sequence with ownership (non-serial, non-identity)" {
+ t.Skip("Skipping test case - will support ALTER SEQUENCE START WITH for serial columns in future PR")
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, tt.metadata)
+ require.NoError(t, err)
+ assert.Equal(t, tt.expected, result)
+ })
+ }
+}
+
+func TestGetSchemaSDLDefinition(t *testing.T) {
+ schemaMetadata := &storepb.SchemaMetadata{
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ }
+
+ result, err := GetSchemaSDLDefinition(schemaMetadata)
+ require.NoError(t, err)
+
+ expected := `CREATE TABLE "public"."users" (
+ "id" SERIAL NOT NULL,
+ "name" VARCHAR(255) NOT NULL
+);
+
+`
+
+ assert.Equal(t, expected, result)
+}
+
+func TestGetDatabaseDefinitionNormalVsSDLFormat(t *testing.T) {
+ // Create a more complex metadata to show the difference
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ },
+ {
+ Name: "email",
+ Type: "VARCHAR(320)",
+ Nullable: true,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "users_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ {
+ Name: "idx_users_name",
+ Expressions: []string{"name"},
+ },
+ {
+ Name: "idx_users_email_unique",
+ Expressions: []string{"email"},
+ Unique: true,
+ IsConstraint: false, // This is a unique index, not a constraint
+ },
+ },
+ },
+ },
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "active_users_view",
+ Definition: "SELECT id, name, email FROM users WHERE active = true",
+ },
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "count_active_users",
+ Definition: `CREATE FUNCTION "public"."count_active_users"() RETURNS integer
+ LANGUAGE sql
+ AS $$
+ SELECT COUNT(*)::integer FROM users WHERE active = true;
+$$`,
+ },
+ },
+ },
+ },
+ }
+
+ // Test SDL format is false (normal format)
+ ctxNormal := schema.GetDefinitionContext{
+ SDLFormat: false,
+ }
+
+ resultNormal, err := GetDatabaseDefinition(ctxNormal, metadata)
+ require.NoError(t, err)
+ assert.NotEqual(t, "", resultNormal)
+ assert.Contains(t, resultNormal, "CREATE TABLE")
+
+ // Test SDL format is true
+ ctxSDL := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ resultSDL, err := GetDatabaseDefinition(ctxSDL, metadata)
+ require.NoError(t, err)
+ assert.NotEqual(t, "", resultSDL)
+ assert.Contains(t, resultSDL, "CREATE TABLE")
+
+ // SDL format should be different from normal format
+ // Normal format has separate ALTER TABLE statements for constraints and separate CREATE INDEX
+ // SDL format includes constraints within CREATE TABLE and indexes immediately after
+ assert.Contains(t, resultNormal, "ALTER TABLE") // Normal format has separate constraint statements
+ assert.NotContains(t, resultSDL, "ALTER TABLE") // SDL format should not have separate constraints
+ assert.Contains(t, resultSDL, "CONSTRAINT") // SDL format should have inline constraints
+ assert.Contains(t, resultSDL, "PRIMARY KEY") // SDL format should have inline PRIMARY KEY
+ assert.Contains(t, resultSDL, "CREATE INDEX") // SDL format should have CREATE INDEX statements
+ assert.Contains(t, resultSDL, "CREATE UNIQUE INDEX") // SDL format should have CREATE UNIQUE INDEX
+ assert.Contains(t, resultSDL, "CREATE VIEW") // SDL format should have CREATE VIEW statements
+ assert.Contains(t, resultSDL, "CREATE FUNCTION") // SDL format should have CREATE FUNCTION statements
+
+ t.Logf("Normal format result: %q", resultNormal)
+ t.Logf("SDL format result: %q", resultSDL)
+}
+
+// TestCheckConstraintNotValidFormat tests that CHECK constraints with NOT VALID
+// are formatted correctly without extra parentheses around the expression
+func TestCheckConstraintNotValidFormat(t *testing.T) {
+ tests := []struct {
+ name string
+ metadata *storepb.DatabaseSchemaMetadata
+ expected string
+ }{
+ {
+ name: "Check constraint with NOT VALID",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "namespace_settings",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "namespace_id",
+ Type: "bigint",
+ Nullable: false,
+ },
+ {
+ Name: "default_branch_protection_defaults",
+ Type: "jsonb",
+ Nullable: true,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "namespace_settings_pkey",
+ Expressions: []string{"namespace_id"},
+ Primary: true,
+ },
+ },
+ CheckConstraints: []*storepb.CheckConstraintMetadata{
+ {
+ Name: "default_branch_protection_defaults_size_constraint",
+ Expression: "(octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."namespace_settings" (
+ "namespace_id" bigint NOT NULL,
+ "default_branch_protection_defaults" jsonb,
+ CONSTRAINT "namespace_settings_pkey" PRIMARY KEY (namespace_id),
+ CONSTRAINT "default_branch_protection_defaults_size_constraint" CHECK (octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID
+);
+
+`,
+ },
+ {
+ name: "Multiple check constraints with and without NOT VALID",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "test",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "table1",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "serial",
+ Nullable: false,
+ },
+ {
+ Name: "data",
+ Type: "jsonb",
+ Nullable: true,
+ },
+ {
+ Name: "age",
+ Type: "integer",
+ Nullable: true,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "table1_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ },
+ CheckConstraints: []*storepb.CheckConstraintMetadata{
+ {
+ Name: "table1_data_size_check",
+ Expression: "(octet_length(data::text) <= 1024) NOT VALID",
+ },
+ {
+ Name: "table1_age_check",
+ Expression: "(age >= 18)",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE SCHEMA IF NOT EXISTS "test";
+
+CREATE TABLE "test"."table1" (
+ "id" serial NOT NULL,
+ "data" jsonb,
+ "age" integer,
+ CONSTRAINT "table1_pkey" PRIMARY KEY (id),
+ CONSTRAINT "table1_data_size_check" CHECK (octet_length(data::text) <= 1024) NOT VALID,
+ CONSTRAINT "table1_age_check" CHECK (age >= 18)
+);
+
+`,
+ },
+ {
+ name: "Regular check constraint without NOT VALID",
+ metadata: &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "serial",
+ Nullable: false,
+ },
+ {
+ Name: "age",
+ Type: "integer",
+ Nullable: true,
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "users_pkey",
+ Expressions: []string{"id"},
+ Primary: true,
+ },
+ },
+ CheckConstraints: []*storepb.CheckConstraintMetadata{
+ {
+ Name: "users_age_check",
+ Expression: "(age >= 0)",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ expected: `CREATE TABLE "public"."users" (
+ "id" serial NOT NULL,
+ "age" integer,
+ CONSTRAINT "users_pkey" PRIMARY KEY (id),
+ CONSTRAINT "users_age_check" CHECK (age >= 0)
+);
+
+`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, tt.metadata)
+ require.NoError(t, err)
+ assert.Equal(t, tt.expected, result)
+
+ // Additional validation: ensure NOT VALID (if present) is outside parentheses
+ if strings.Contains(tt.expected, "NOT VALID") {
+ // Check that we don't have the incorrect format: CHECK (...) NOT VALID)
+ assert.NotContains(t, result, ") NOT VALID)", "NOT VALID should not be inside closing parenthesis")
+ // Check that we have the correct format: CHECK (...) NOT VALID
+ assert.Contains(t, result, ") NOT VALID", "NOT VALID should be after CHECK expression parenthesis")
+ }
+
+ // Validate that the generated SQL can be parsed without errors using ANTLR parser
+ _, err = omnipg.Parse(result)
+ require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
+ })
+ }
+}
+
+// TestCheckConstraintNotValidFormatNormalMode tests that CHECK constraints with NOT VALID
+// are formatted correctly in normal (non-SDL) mode
+func TestCheckConstraintNotValidFormatNormalMode(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "namespace_settings",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "namespace_id",
+ Type: "bigint",
+ Nullable: false,
+ },
+ {
+ Name: "default_branch_protection_defaults",
+ Type: "jsonb",
+ Nullable: true,
+ },
+ },
+ CheckConstraints: []*storepb.CheckConstraintMetadata{
+ {
+ Name: "default_branch_protection_defaults_size_constraint",
+ Expression: "(octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ // Test normal format (SDLFormat: false)
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: false,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ // Verify the CHECK constraint is output correctly in the CREATE TABLE statement
+ assert.Contains(t, result, `CONSTRAINT "default_branch_protection_defaults_size_constraint" CHECK (octet_length(default_branch_protection_defaults::text) <= 1024) NOT VALID`)
+
+ // Additional validation: ensure NOT VALID is outside parentheses (not inside extra parentheses)
+ assert.NotContains(t, result, ") NOT VALID)", "NOT VALID should not be inside closing parenthesis")
+ assert.Contains(t, result, ") NOT VALID", "NOT VALID should be after CHECK expression parenthesis")
+
+ // Validate that the generated SQL can be parsed without errors using ANTLR parser
+ _, err = omnipg.Parse(result)
+ require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
+}
+
+func TestGetDatabaseDefinitionSDLFormat_WithComments(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "test_schema",
+ Comment: "Test schema for comments",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Comment: "Users table with comments",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "SERIAL",
+ Nullable: false,
+ Comment: "User ID",
+ },
+ {
+ Name: "name",
+ Type: "VARCHAR(255)",
+ Nullable: false,
+ Comment: "User name",
+ },
+ {
+ Name: "email",
+ Type: "VARCHAR(320)",
+ Nullable: true,
+ Comment: "User email address",
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "idx_users_email",
+ Expressions: []string{"email"},
+ Unique: false,
+ Primary: false,
+ Comment: "Index on email column",
+ },
+ },
+ },
+ },
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "active_users",
+ Definition: "SELECT id, name, email FROM test_schema.users WHERE active = true",
+ Comment: "View of active users",
+ },
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "get_user_count",
+ Signature: "get_user_count()",
+ Definition: "CREATE FUNCTION test_schema.get_user_count() RETURNS INTEGER AS $$ BEGIN RETURN (SELECT COUNT(*) FROM test_schema.users); END; $$ LANGUAGE plpgsql",
+ Comment: "Function to get user count",
+ },
+ },
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "custom_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ CacheSize: "1",
+ Comment: "Custom sequence for testing",
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ // Verify schema comment
+ assert.Contains(t, result, `CREATE SCHEMA IF NOT EXISTS "test_schema";`)
+ assert.Contains(t, result, `COMMENT ON SCHEMA "test_schema" IS 'Test schema for comments';`)
+
+ // Verify table comment
+ assert.Contains(t, result, `COMMENT ON TABLE "test_schema"."users" IS 'Users table with comments';`)
+
+ // Verify column comments
+ assert.Contains(t, result, `COMMENT ON COLUMN "test_schema"."users"."id" IS 'User ID';`)
+ assert.Contains(t, result, `COMMENT ON COLUMN "test_schema"."users"."name" IS 'User name';`)
+ assert.Contains(t, result, `COMMENT ON COLUMN "test_schema"."users"."email" IS 'User email address';`)
+
+ // Verify view comment
+ assert.Contains(t, result, `COMMENT ON VIEW "test_schema"."active_users" IS 'View of active users';`)
+
+ // Verify function comment
+ assert.Contains(t, result, `COMMENT ON FUNCTION "test_schema".get_user_count() IS 'Function to get user count';`)
+
+ // Verify sequence comment
+ assert.Contains(t, result, `COMMENT ON SEQUENCE "test_schema"."custom_seq" IS 'Custom sequence for testing';`)
+
+ // Verify index comment
+ assert.Contains(t, result, `COMMENT ON INDEX "test_schema"."idx_users_email" IS 'Index on email column';`)
+
+ // Validate that the generated SQL can be parsed without errors using ANTLR parser
+ _, err = omnipg.Parse(result)
+ require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
+}
+
+func TestGetDatabaseDefinitionSDLFormat_WithCommentsEscaping(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "test_table",
+ Comment: "Table with 'single quotes' and \"double quotes\"",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "INTEGER",
+ Nullable: false,
+ Comment: "Column with 'quoted' text",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ // Verify that single quotes are properly escaped
+ assert.Contains(t, result, `COMMENT ON TABLE "public"."test_table" IS 'Table with ''single quotes'' and "double quotes"';`)
+ assert.Contains(t, result, `COMMENT ON COLUMN "public"."test_table"."id" IS 'Column with ''quoted'' text';`)
+
+ // Validate that the generated SQL can be parsed without errors using ANTLR parser
+ _, err = omnipg.Parse(result)
+ require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
+}
+
+func TestGetMultiFileDatabaseDefinition_WithComments(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "app_schema",
+ Comment: "Application schema",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "products",
+ Comment: "Product catalog",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "INTEGER",
+ Nullable: false,
+ Comment: "Product ID",
+ },
+ {
+ Name: "name",
+ Type: "TEXT",
+ Nullable: false,
+ Comment: "Product name",
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "idx_products_name",
+ Expressions: []string{"name"},
+ Unique: false,
+ Primary: false,
+ Comment: "Index for product search",
+ },
+ },
+ },
+ },
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "active_products",
+ Definition: "SELECT id, name FROM app_schema.products WHERE active = true",
+ Comment: "View of active products",
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "product_summary_mv",
+ Definition: "SELECT id, name FROM app_schema.products",
+ Comment: "Materialized view of product summary",
+ },
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "count_products",
+ Signature: "count_products()",
+ Definition: "CREATE FUNCTION app_schema.count_products() RETURNS INTEGER AS $$ BEGIN RETURN (SELECT COUNT(*) FROM app_schema.products); END; $$ LANGUAGE plpgsql",
+ Comment: "Returns product count",
+ },
+ },
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "order_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ CacheSize: "1",
+ Comment: "Sequence for orders",
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+ require.NotNil(t, result)
+
+ // Build a map for easier testing
+ fileMap := make(map[string]string)
+ for _, file := range result.Files {
+ fileMap[file.Name] = file.Content
+ }
+
+ // Verify schema-level file with schema comment
+ schemaFile, ok := fileMap["schemas/app_schema/schema.sql"]
+ require.True(t, ok, "schema.sql file should exist for non-public schema")
+ assert.Contains(t, schemaFile, `CREATE SCHEMA IF NOT EXISTS "app_schema";`)
+ assert.Contains(t, schemaFile, `COMMENT ON SCHEMA "app_schema" IS 'Application schema';`)
+
+ // Verify consolidated sequences file with comment (independent sequences go in sequences.sql)
+ sequenceFile, ok := fileMap["schemas/app_schema/sequences.sql"]
+ require.True(t, ok, "sequences.sql file should exist for independent sequences")
+ assert.Contains(t, sequenceFile, `CREATE SEQUENCE "app_schema"."order_seq"`)
+ assert.Contains(t, sequenceFile, `COMMENT ON SEQUENCE "app_schema"."order_seq" IS 'Sequence for orders';`)
+
+ // Verify table file with comments
+ tableFile, ok := fileMap["schemas/app_schema/tables/products.sql"]
+ require.True(t, ok, "table file should exist")
+ assert.Contains(t, tableFile, `CREATE TABLE "app_schema"."products"`)
+ assert.Contains(t, tableFile, `COMMENT ON TABLE "app_schema"."products" IS 'Product catalog';`)
+ assert.Contains(t, tableFile, `COMMENT ON COLUMN "app_schema"."products"."id" IS 'Product ID';`)
+ assert.Contains(t, tableFile, `COMMENT ON COLUMN "app_schema"."products"."name" IS 'Product name';`)
+ assert.Contains(t, tableFile, `COMMENT ON INDEX "app_schema"."idx_products_name" IS 'Index for product search';`)
+
+ // Verify view file with comment
+ viewFile, ok := fileMap["schemas/app_schema/views/active_products.sql"]
+ require.True(t, ok, "view file should exist")
+ assert.Contains(t, viewFile, `CREATE VIEW "app_schema"."active_products"`)
+ assert.Contains(t, viewFile, `COMMENT ON VIEW "app_schema"."active_products" IS 'View of active products';`)
+
+ // Verify materialized view file with comment
+ materializedViewFile, ok := fileMap["schemas/app_schema/materialized_views/product_summary_mv.sql"]
+ require.True(t, ok, "materialized view file should exist")
+ assert.Contains(t, materializedViewFile, `CREATE MATERIALIZED VIEW "app_schema"."product_summary_mv"`)
+ assert.Contains(t, materializedViewFile, `COMMENT ON MATERIALIZED VIEW "app_schema"."product_summary_mv" IS 'Materialized view of product summary';`)
+
+ // Verify function file with comment
+ functionFile, ok := fileMap["schemas/app_schema/functions/count_products.sql"]
+ require.True(t, ok, "function file should exist")
+ assert.Contains(t, functionFile, `CREATE FUNCTION app_schema.count_products()`)
+ assert.Contains(t, functionFile, `COMMENT ON FUNCTION "app_schema".count_products() IS 'Returns product count';`)
+
+ // Validate that each file's SQL can be parsed
+ for fileName, content := range fileMap {
+ _, err := omnipg.Parse(content)
+ require.NoError(t, err, "SQL in file %s should be parseable by PostgreSQL parser", fileName)
+ }
+}
+
+func TestGetMultiFileDatabaseDefinition_SchemaFileMakesCombinedSDLLoadable(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "metric_helpers",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "metrics",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "INTEGER",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
+ require.NoError(t, err)
+
+ fileMap := make(map[string]string)
+ var combined strings.Builder
+ for _, file := range result.Files {
+ fileMap[file.Name] = file.Content
+ combined.WriteString(file.Content)
+ combined.WriteString("\n")
+ }
+
+ schemaFile, ok := fileMap["schemas/metric_helpers/schema.sql"]
+ assert.True(t, ok, "schema.sql file should exist for non-public schema")
+ assert.Contains(t, schemaFile, `CREATE SCHEMA IF NOT EXISTS "metric_helpers";`)
+
+ _, err = schema.DiffSDLMigration(storepb.Engine_POSTGRES, "", combined.String(), "")
+ require.NoError(t, err)
+}
+
+func TestGetDatabaseDefinitionSDLFormat_SerialColumnWithSequence(t *testing.T) {
+ // This test reproduces the issue where a SERIAL column causes duplicate CREATE SEQUENCE statements
+ // SERIAL columns automatically create sequences, so we should NOT output separate CREATE SEQUENCE for them
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "users_id_seq",
+ DataType: "integer",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "2147483647",
+ Cycle: false,
+ OwnerTable: "users",
+ OwnerColumn: "id",
+ Comment: "Sequence for users id - should NOT appear in SDL",
+ },
+ {
+ Name: "independent_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ OwnerTable: "", // Independent sequence - should appear in SDL
+ Comment: "Independent sequence - should appear in SDL",
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "integer",
+ Nullable: false,
+ Default: "nextval('users_id_seq'::regclass)",
+ },
+ {
+ Name: "name",
+ Type: "text",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ // The SDL should contain the table definition with serial type
+ require.Contains(t, result, `CREATE TABLE "public"."users"`)
+ require.Contains(t, result, `"id" serial`)
+ require.Contains(t, result, `"name" text NOT NULL`)
+
+ // The SDL SHOULD contain the independent sequence
+ require.Contains(t, result, `CREATE SEQUENCE "public"."independent_seq"`,
+ "SDL should contain CREATE SEQUENCE for independent sequences")
+
+ // The SDL should NOT contain a separate CREATE SEQUENCE statement for users_id_seq
+ // because the sequence is owned by the serial column (created implicitly by SERIAL type)
+ require.NotContains(t, result, `CREATE SEQUENCE "public"."users_id_seq"`,
+ "SDL should NOT contain CREATE SEQUENCE for sequence owned by a column (created implicitly by SERIAL)")
+}
+
+func TestGetMultiFileDatabaseDefinition_SerialColumnWithSequence(t *testing.T) {
+ // This test verifies that multi-file format correctly handles serial columns and their sequences
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "users_id_seq",
+ DataType: "integer",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "2147483647",
+ Cycle: false,
+ OwnerTable: "users",
+ OwnerColumn: "id",
+ Comment: "Sequence for users id - should NOT appear as separate file",
+ },
+ {
+ Name: "independent_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ OwnerTable: "", // Independent sequence - not owned by any column
+ Comment: "Independent sequence - should appear as separate file",
+ },
+ {
+ Name: "custom_seq",
+ DataType: "bigint",
+ Start: "100",
+ Increment: "5",
+ MinValue: "100",
+ MaxValue: "9223372036854775807",
+ Cycle: false,
+ OwnerTable: "", // Not owned by a column (independent sequence)
+ Comment: "Custom sequence for orders - should appear as separate file",
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "integer",
+ Nullable: false,
+ Default: "nextval('users_id_seq'::regclass)",
+ },
+ {
+ Name: "name",
+ Type: "text",
+ Nullable: false,
+ },
+ },
+ },
+ {
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "integer",
+ Nullable: false,
+ },
+ {
+ Name: "order_number",
+ Type: "bigint",
+ Nullable: false,
+ Default: "nextval('custom_seq'::regclass)",
+ Comment: "Order number using custom sequence",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ // Build a map of files for easy lookup
+ fileMap := make(map[string]string)
+ for _, file := range result.Files {
+ fileMap[file.Name] = file.Content
+ }
+
+ // Verify users table file exists and contains serial column
+ usersFile, ok := fileMap["schemas/public/tables/users.sql"]
+ require.True(t, ok, "users table file should exist")
+ require.Contains(t, usersFile, `"id" serial`, "users table should use serial type")
+ require.Contains(t, usersFile, `"name" text NOT NULL`)
+
+ // Verify orders table file exists
+ ordersFile, ok := fileMap["schemas/public/tables/orders.sql"]
+ require.True(t, ok, "orders table file should exist")
+ require.Contains(t, ordersFile, `"id" integer NOT NULL`)
+ require.Contains(t, ordersFile, `"order_number" bigint DEFAULT nextval('custom_seq'::regclass) NOT NULL`,
+ "order_number should use custom_seq via DEFAULT nextval()")
+
+ // Verify the consolidated sequences file exists with both independent sequences
+ sequencesFile, ok := fileMap["schemas/public/sequences.sql"]
+ require.True(t, ok, "independent_seq file should exist")
+ require.Contains(t, sequencesFile, `CREATE SEQUENCE "public"."independent_seq"`)
+ require.Contains(t, sequencesFile, `COMMENT ON SEQUENCE "public"."independent_seq" IS 'Independent sequence - should appear as separate file'`)
+ require.Contains(t, sequencesFile, `CREATE SEQUENCE "public"."custom_seq"`)
+ require.Contains(t, sequencesFile, `COMMENT ON SEQUENCE "public"."custom_seq" IS 'Custom sequence for orders - should appear as separate file'`)
+ require.NotContains(t, sequencesFile, `ALTER SEQUENCE`, "Independent sequences should not have ALTER SEQUENCE OWNED BY")
+
+ // Verify individual sequence files do NOT exist (they should be in the consolidated file)
+ _, ok = fileMap["schemas/public/sequences/independent_seq.sql"]
+ require.False(t, ok, "independent_seq should NOT have individual file (should be in sequences.sql)")
+ _, ok = fileMap["schemas/public/sequences/custom_seq.sql"]
+ require.False(t, ok, "custom_seq should NOT have individual file (should be in sequences.sql)")
+
+ // Verify users_id_seq does NOT appear in the sequences file (it's a serial sequence)
+ require.NotContains(t, sequencesFile, "users_id_seq", "users_id_seq should NOT appear in sequences.sql because it's owned by a serial column")
+
+ // Total files should be: 2 tables + 1 sequences file = 3 files
+ require.Equal(t, 3, len(result.Files), "Should have exactly 3 files (2 tables + 1 consolidated sequences file)")
+}
+
+func TestGetDatabaseDefinitionSDLFormat_MultipleSequencesClaimingOwnership(t *testing.T) {
+ // This test verifies that when multiple sequences claim ownership of the same column,
+ // only the sequence referenced in the DEFAULT clause is skipped (treated as serial sequence).
+ // The other sequences should still be output as CREATE SEQUENCE statements.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ Name: "test_sequence2",
+ DataType: "integer",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "2147483647",
+ Cycle: false,
+ OwnerTable: "test_table",
+ OwnerColumn: "id",
+ },
+ {
+ Name: "test_table_id_seq",
+ DataType: "integer",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "2147483647",
+ Cycle: false,
+ OwnerTable: "test_table",
+ OwnerColumn: "id",
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "test_table",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "integer",
+ Nullable: false,
+ Default: "nextval('test_table_id_seq'::regclass)",
+ },
+ {
+ Name: "name",
+ Type: "text",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ expected := `CREATE SEQUENCE "public"."test_sequence2" AS integer START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 NO CYCLE;
+
+CREATE TABLE "public"."test_table" (
+ "id" serial,
+ "name" text NOT NULL
+);
+
+ALTER SEQUENCE "public"."test_sequence2" OWNED BY "public"."test_table"."id";
+
+`
+
+ assert.Equal(t, expected, result)
+
+ // Verify that test_sequence2 is output (not skipped)
+ assert.Contains(t, result, `CREATE SEQUENCE "public"."test_sequence2"`,
+ "test_sequence2 should be output because it's not referenced in DEFAULT clause")
+
+ // Verify that test_sequence2 has ALTER SEQUENCE OWNED BY statement
+ assert.Contains(t, result, `ALTER SEQUENCE "public"."test_sequence2" OWNED BY "public"."test_table"."id"`,
+ "test_sequence2 should have ALTER SEQUENCE OWNED BY because metadata indicates it's owned by the column")
+
+ // Verify that test_table_id_seq is NOT output (skipped)
+ assert.NotContains(t, result, `CREATE SEQUENCE "public"."test_table_id_seq"`,
+ "test_table_id_seq should be skipped because it's referenced in DEFAULT clause and treated as serial")
+
+ // Verify that the column uses serial type
+ assert.Contains(t, result, `"id" serial`,
+ "Column should use serial type because it references test_table_id_seq which is owned by it")
+
+ // Validate that the generated SQL can be parsed
+ _, err = omnipg.Parse(result)
+ require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
+}
+
+func TestGetDatabaseDefinitionSDLFormat_ProcedureWithComment(t *testing.T) {
+ // This test verifies that PROCEDURE comments are correctly generated with
+ // COMMENT ON PROCEDURE (not COMMENT ON FUNCTION)
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "get_user_count",
+ Signature: "get_user_count()",
+ Definition: `CREATE FUNCTION "public"."get_user_count"() RETURNS integer
+ LANGUAGE sql
+ AS $$
+ SELECT COUNT(*)::integer FROM users;
+$$`,
+ Comment: "Function to count users",
+ },
+ {
+ Name: "update_user_name",
+ Signature: "update_user_name(user_id integer, new_name character varying)",
+ Definition: `CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
+ LANGUAGE plpgsql
+ AS $$
+BEGIN
+ UPDATE users
+ SET name = new_name
+ WHERE id = user_id;
+END;
+$$`,
+ Comment: "Procedure to update user name",
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ // Verify that FUNCTION has COMMENT ON FUNCTION
+ assert.Contains(t, result, `COMMENT ON FUNCTION "public".get_user_count() IS 'Function to count users';`,
+ "Function comment should use COMMENT ON FUNCTION")
+
+ // Verify that PROCEDURE has COMMENT ON PROCEDURE (not COMMENT ON FUNCTION)
+ assert.Contains(t, result, `COMMENT ON PROCEDURE "public".update_user_name(integer, character varying) IS 'Procedure to update user name';`,
+ "Procedure comment should use COMMENT ON PROCEDURE with types-only signature")
+
+ // Verify that we don't incorrectly use COMMENT ON FUNCTION for the procedure
+ assert.NotContains(t, result, `COMMENT ON FUNCTION "public".update_user_name`,
+ "Procedure comment should NOT use COMMENT ON FUNCTION")
+
+ // Validate that the generated SQL can be parsed
+ _, err = omnipg.Parse(result)
+ require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")
+}
+
+func TestGetMultiFileDatabaseDefinition_FunctionAndProcedureSeparation(t *testing.T) {
+ // This test verifies that functions and procedures are separated into different folders
+ // in multi-file mode
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "get_user_count",
+ Definition: `CREATE FUNCTION "public"."get_user_count"() RETURNS integer
+ LANGUAGE sql
+ AS $$
+ SELECT COUNT(*)::integer FROM users;
+$$`,
+ },
+ {
+ Name: "get_user_by_id",
+ Definition: `CREATE FUNCTION "public"."get_user_by_id"(user_id integer) RETURNS TABLE(id integer, name character varying)
+ LANGUAGE sql
+ AS $$
+ SELECT u.id, u.name FROM users u WHERE u.id = user_id;
+$$`,
+ },
+ {
+ Name: "update_user_name",
+ Definition: `CREATE PROCEDURE "public"."update_user_name"(IN user_id integer, IN new_name character varying)
+ LANGUAGE plpgsql
+ AS $$
+BEGIN
+ UPDATE users SET name = new_name WHERE id = user_id;
+END;
+$$`,
+ },
+ {
+ Name: "delete_old_users",
+ Definition: `CREATE PROCEDURE "public"."delete_old_users"(IN days_old integer)
+ LANGUAGE plpgsql
+ AS $$
+BEGIN
+ DELETE FROM users WHERE created_at < NOW() - INTERVAL '1 day' * days_old;
+END;
+$$`,
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+ require.NotNil(t, result)
+
+ // Build a map of files for easy lookup
+ fileMap := make(map[string]string)
+ for _, file := range result.Files {
+ fileMap[file.Name] = file.Content
+ }
+
+ // Verify functions are in functions folder
+ getUserCountFile, ok := fileMap["schemas/public/functions/get_user_count.sql"]
+ require.True(t, ok, "get_user_count function should be in functions folder")
+ assert.Contains(t, getUserCountFile, `CREATE FUNCTION "public"."get_user_count"()`)
+
+ getUserByIDFile, ok := fileMap["schemas/public/functions/get_user_by_id.sql"]
+ require.True(t, ok, "get_user_by_id function should be in functions folder")
+ assert.Contains(t, getUserByIDFile, `CREATE FUNCTION "public"."get_user_by_id"`)
+
+ // Verify procedures are in procedures folder (not functions folder)
+ updateUserNameFile, ok := fileMap["schemas/public/procedures/update_user_name.sql"]
+ require.True(t, ok, "update_user_name procedure should be in procedures folder")
+ assert.Contains(t, updateUserNameFile, `CREATE PROCEDURE "public"."update_user_name"`)
+
+ deleteOldUsersFile, ok := fileMap["schemas/public/procedures/delete_old_users.sql"]
+ require.True(t, ok, "delete_old_users procedure should be in procedures folder")
+ assert.Contains(t, deleteOldUsersFile, `CREATE PROCEDURE "public"."delete_old_users"`)
+
+ // Verify procedures are NOT in functions folder
+ _, ok = fileMap["schemas/public/functions/update_user_name.sql"]
+ require.False(t, ok, "update_user_name should NOT be in functions folder")
+
+ _, ok = fileMap["schemas/public/functions/delete_old_users.sql"]
+ require.False(t, ok, "delete_old_users should NOT be in functions folder")
+
+ // Total files should be: 2 functions + 2 procedures = 4 files
+ require.Equal(t, 4, len(result.Files), "Should have exactly 4 files (2 functions + 2 procedures)")
+
+ // Validate that each file's SQL can be parsed
+ for fileName, content := range fileMap {
+ _, err := omnipg.Parse(content)
+ require.NoError(t, err, "SQL in file %s should be parseable by PostgreSQL parser", fileName)
+ }
+}
+
+func TestOverloadedFunctionsPreservedInDump(t *testing.T) {
+ oneArgDef := `CREATE OR REPLACE FUNCTION "public"."camel_to_snake"(camel_cased_text character varying)
+ RETURNS character varying
+ LANGUAGE plpgsql
+ IMMUTABLE
+AS $function$
+BEGIN
+ RETURN lower(camel_cased_text);
+END;
+$function$`
+ twoArgDef := `CREATE OR REPLACE FUNCTION "public"."camel_to_snake"(camel_cased_text character varying, case_directive character varying)
+ RETURNS character varying
+ LANGUAGE plpgsql
+ IMMUTABLE
+AS $function$
+BEGIN
+ RETURN lower(camel_cased_text || '_' || case_directive);
+END;
+$function$`
+
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "camel_to_snake",
+ Signature: "camel_to_snake(camel_cased_text character varying)",
+ Definition: oneArgDef,
+ },
+ {
+ Name: "camel_to_snake",
+ Signature: "camel_to_snake(camel_cased_text character varying, case_directive character varying)",
+ Definition: twoArgDef,
+ },
+ },
+ },
+ },
+ }
+
+ oneArgSubstring := `"camel_to_snake"(camel_cased_text character varying)`
+ twoArgSubstring := `"camel_to_snake"(camel_cased_text character varying, case_directive character varying)`
+
+ // Test non-SDL format (GetDatabaseDefinition)
+ result, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, metadata)
+ require.NoError(t, err)
+ assert.Contains(t, result, oneArgSubstring)
+ assert.Contains(t, result, twoArgSubstring)
+ assert.Equal(t, 2, strings.Count(result, "CREATE OR REPLACE FUNCTION"), "both overloads must survive in non-SDL dump")
+
+ // Test SDL format
+ resultSDL, err := GetDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
+ require.NoError(t, err)
+ assert.Contains(t, resultSDL, oneArgSubstring)
+ assert.Contains(t, resultSDL, twoArgSubstring)
+ assert.Equal(t, 2, strings.Count(resultSDL, "CREATE OR REPLACE FUNCTION"), "both overloads must survive in SDL dump")
+
+ // Test single-schema path (GetSchemaDefinition)
+ resultSchema, err := GetSchemaDefinition(metadata.Schemas[0])
+ require.NoError(t, err)
+ assert.Contains(t, resultSchema, oneArgSubstring)
+ assert.Contains(t, resultSchema, twoArgSubstring)
+ assert.Equal(t, 2, strings.Count(resultSchema, "CREATE OR REPLACE FUNCTION"), "both overloads must survive in single-schema dump")
+}
+
+// TestGetDatabaseDefinition_IndexCommentUsesRealNewline reproduces the A1
+// bug where writeIndexComment used a Go raw string literal with `\n\n`,
+// producing four literal characters `\`, `n`, `\`, `n` instead of two line
+// feeds. The regression surfaced as omni pgparser choking on the backslash
+// right after `COMMENT ON INDEX ... IS '...';`.
+func TestGetDatabaseDefinition_IndexCommentUsesRealNewline(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "db",
+ Schemas: []*storepb.SchemaMetadata{{
+ Name: "public",
+ Tables: []*storepb.TableMetadata{{
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Nullable: false},
+ },
+ Indexes: []*storepb.IndexMetadata{{
+ Name: "idx_t_id",
+ Type: "btree",
+ Expressions: []string{"id"},
+ Descending: []bool{false},
+ Definition: "CREATE INDEX idx_t_id ON public.t USING btree (id);",
+ Comment: "sample comment",
+ }},
+ }},
+ }},
+ }
+
+ ddl, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, meta)
+ if err != nil {
+ t.Fatalf("GetDatabaseDefinition: %v", err)
+ }
+
+ if strings.Contains(ddl, `\n`) {
+ t.Errorf("DDL contains literal backslash-n escape; got:\n%s", ddl)
+ }
+ if !strings.Contains(ddl, `COMMENT ON INDEX "public"."idx_t_id" IS 'sample comment';`) {
+ t.Errorf("expected COMMENT ON INDEX statement, got:\n%s", ddl)
+ }
+
+ cat := catalog.New()
+ if _, execErr := cat.Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); execErr != nil {
+ t.Errorf("omni catalog parse error: %v\nDDL:\n%s", execErr, ddl)
+ }
+}
+
+// TestGetDatabaseDefinition_TableWithOnlyCheckConstraints reproduces the A2
+// bug where a table with zero columns but with CHECK constraints produced
+// `CREATE TABLE "..."."..." (,` — an invalid leading comma. We force the
+// case by declaring a CHECK constraint on a table with no regular columns.
+func TestGetDatabaseDefinition_TableWithOnlyCheckConstraints(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "db",
+ Schemas: []*storepb.SchemaMetadata{{
+ Name: "public",
+ Tables: []*storepb.TableMetadata{{
+ Name: "cons_only",
+ CheckConstraints: []*storepb.CheckConstraintMetadata{{
+ Name: "cons_only_dummy",
+ Expression: "(1 = 1)",
+ }},
+ }},
+ }},
+ }
+
+ ddl, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, meta)
+ if err != nil {
+ t.Fatalf("GetDatabaseDefinition: %v", err)
+ }
+ if strings.Contains(ddl, "(,") {
+ t.Errorf("DDL still has `(,` leading comma; got:\n%s", ddl)
+ }
+ if !strings.Contains(ddl, `CREATE TABLE "public"."cons_only" (`) {
+ t.Errorf("expected CREATE TABLE for cons_only, got:\n%s", ddl)
+ }
+ // A genuine zero-column table (no indexes or foreign keys) must keep its
+ // column-independent CHECK constraint — only privilege-filtered broken
+ // metadata goes down the bare-table path.
+ if !strings.Contains(ddl, "cons_only_dummy") {
+ t.Errorf("expected CHECK constraint cons_only_dummy to be preserved, got:\n%s", ddl)
+ }
+ // Omni will reject a table with zero columns semantically, but it must
+ // at least parse cleanly; the test asserts the parse-level contract.
+ if _, parseErr := catalog.New().Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); parseErr != nil {
+ t.Errorf("omni catalog parse error: %v\nDDL:\n%s", parseErr, ddl)
+ }
+}
+
+// TestGetDatabaseDefinition_TableWithConstraintsButNoColumns reproduces a
+// dump replay failure observed on a real workspace export: the sync user
+// lacked column privileges on some tables, so information_schema.columns
+// returned nothing while pg_catalog still exposed the tables' constraints.
+// The dump then emitted `CREATE TABLE "s1"."t1" ();` followed by
+// `ALTER TABLE ... ADD CONSTRAINT ... PRIMARY KEY (c1, c2);`, which
+// PostgreSQL rejects because the columns do not exist. The dump must skip
+// column-dependent DDL for such tables instead of producing non-replayable
+// statements.
+func TestGetDatabaseDefinition_TableWithConstraintsButNoColumns(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "db",
+ Schemas: []*storepb.SchemaMetadata{{
+ Name: "s1",
+ Sequences: []*storepb.SequenceMetadata{
+ {
+ // Owned by a column missing from t1's metadata: the sequence
+ // itself must dump, its OWNED BY clause must not.
+ Name: "t1_c1_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ OwnerTable: "t1",
+ OwnerColumn: "c1",
+ },
+ {
+ // Owned by a column of t4, a zero-column table with no
+ // broken-metadata signal (no indexes/FKs/partitions):
+ // OWNED BY must still be skipped — a sequence cannot be
+ // owned by a column of a zero-column table.
+ Name: "t4_id_seq",
+ DataType: "bigint",
+ Start: "1",
+ Increment: "1",
+ MinValue: "1",
+ MaxValue: "9223372036854775807",
+ OwnerTable: "t4",
+ OwnerColumn: "id",
+ },
+ },
+ Tables: []*storepb.TableMetadata{
+ {
+ // Broken sync: constraints and indexes, but no columns.
+ Name: "t1",
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "pk_t1",
+ Primary: true,
+ Unique: true,
+ IsConstraint: true,
+ Expressions: []string{"c1", "c2"},
+ },
+ {
+ Name: "uk_t1_c1",
+ Unique: true,
+ IsConstraint: true,
+ Expressions: []string{"c1"},
+ },
+ {
+ Name: "idx_t1_c2",
+ Type: "btree",
+ Expressions: []string{"c2"},
+ Definition: `CREATE INDEX idx_t1_c2 ON s1.t1 USING btree (c2);`,
+ Comment: "comment on a never-created index",
+ },
+ },
+ CheckConstraints: []*storepb.CheckConstraintMetadata{{
+ Name: "chk_t1_c1",
+ Expression: "(c1 > 0)",
+ }},
+ ForeignKeys: []*storepb.ForeignKeyMetadata{{
+ Name: "fk_t1_t2",
+ Columns: []string{"c1"},
+ ReferencedSchema: "s1",
+ ReferencedTable: "t2",
+ ReferencedColumns: []string{"id"},
+ }},
+ Triggers: []*storepb.TriggerMetadata{{
+ Name: "trg_t1_c1",
+ Body: `CREATE TRIGGER trg_t1_c1 BEFORE UPDATE OF c1 ON s1.t1 FOR EACH ROW EXECUTE FUNCTION s1.f()`,
+ Comment: "comment on a never-created trigger",
+ }},
+ Rules: []*storepb.RuleMetadata{{
+ Name: "rule_t1",
+ Event: "INSERT",
+ Definition: `CREATE RULE rule_t1 AS ON INSERT TO s1.t1 WHERE new.c1 > 0 DO INSTEAD NOTHING;`,
+ }},
+ },
+ {
+ // Healthy table; its own constraints must still dump, but
+ // its foreign key into the broken table must not.
+ Name: "t2",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Nullable: false},
+ {Name: "t1_c1", Type: "integer", Nullable: true},
+ },
+ Indexes: []*storepb.IndexMetadata{{
+ Name: "pk_t2",
+ Primary: true,
+ Unique: true,
+ IsConstraint: true,
+ Expressions: []string{"id"},
+ }},
+ ForeignKeys: []*storepb.ForeignKeyMetadata{{
+ Name: "fk_t2_t1",
+ Columns: []string{"t1_c1"},
+ ReferencedSchema: "s1",
+ ReferencedTable: "t1",
+ ReferencedColumns: []string{"c1"},
+ }},
+ },
+ {
+ // Broken sync on a partitioned table with no indexes: the
+ // partition key references missing columns, so the table
+ // must dump bare, without the PARTITION BY clause.
+ Name: "t3",
+ Partitions: []*storepb.TablePartitionMetadata{{
+ Name: "t3_p1",
+ Expression: "RANGE (created_at)",
+ }},
+ },
+ {
+ // Genuine zero-column table: no indexes, foreign keys, or
+ // partitions. Its column-independent CHECK must be kept.
+ Name: "t4",
+ CheckConstraints: []*storepb.CheckConstraintMetadata{{
+ Name: "chk_t4_ok",
+ Expression: "(1 = 1)",
+ }},
+ },
+ },
+ }},
+ }
+
+ banned := []string{"pk_t1", "uk_t1_c1", "idx_t1_c2", "chk_t1_c1", "fk_t1_t2", "fk_t2_t1", "trg_t1_c1", "rule_t1", "t3_p1", "PARTITION BY", "OWNED BY", "COMMENT ON INDEX"}
+
+ for name, ctx := range map[string]schema.GetDefinitionContext{
+ "dump": {},
+ "sdl": {SDLFormat: true},
+ } {
+ ddl, err := GetDatabaseDefinition(ctx, meta)
+ if err != nil {
+ t.Fatalf("[%s] GetDatabaseDefinition: %v", name, err)
+ }
+
+ if !strings.Contains(ddl, `CREATE TABLE "s1"."t1" (`) {
+ t.Errorf("[%s] expected bare CREATE TABLE for t1, got:\n%s", name, ddl)
+ }
+ for _, b := range banned {
+ if strings.Contains(ddl, b) {
+ t.Errorf("[%s] DDL references %q, which depends on columns missing from t1's metadata; got:\n%s", name, b, ddl)
+ }
+ }
+ if !strings.Contains(ddl, "pk_t2") {
+ t.Errorf("[%s] expected pk_t2 on the healthy table, got:\n%s", name, ddl)
+ }
+ if !strings.Contains(ddl, `CREATE SEQUENCE "s1"."t1_c1_seq"`) {
+ t.Errorf("[%s] expected CREATE SEQUENCE for t1_c1_seq, got:\n%s", name, ddl)
+ }
+ if !strings.Contains(ddl, `CREATE TABLE "s1"."t3" (`) {
+ t.Errorf("[%s] expected bare CREATE TABLE for t3, got:\n%s", name, ddl)
+ }
+ if !strings.Contains(ddl, "chk_t4_ok") {
+ t.Errorf("[%s] expected genuine zero-column table t4 to keep its CHECK constraint, got:\n%s", name, ddl)
+ }
+
+ if _, parseErr := catalog.New().Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); parseErr != nil {
+ t.Errorf("[%s] omni catalog parse error: %v\nDDL:\n%s", name, parseErr, ddl)
+ }
+ }
+
+ multiFile, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{}, meta)
+ if err != nil {
+ t.Fatalf("GetMultiFileDatabaseDefinition: %v", err)
+ }
+ var combined strings.Builder
+ for _, file := range multiFile.Files {
+ combined.WriteString(file.Content)
+ }
+ for _, b := range banned {
+ if strings.Contains(combined.String(), b) {
+ t.Errorf("[multi-file] DDL references %q, which depends on columns missing from t1's metadata; got:\n%s", b, combined.String())
+ }
+ }
+}
+
+// TestGetDatabaseDefinition_TableNameContainingDot reproduces A3: a table
+// whose name literally contains a period was writing out as `"".".."` —
+// because the schema + "." + object object-ID was split back on the first
+// dot and produced an empty schema. The fix uses a NUL separator.
+func TestGetDatabaseDefinition_TableNameContainingDot(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "db",
+ Schemas: []*storepb.SchemaMetadata{{
+ Name: "public",
+ Tables: []*storepb.TableMetadata{{
+ Name: "weird.name",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ }},
+ }},
+ }
+
+ ddl, err := GetDatabaseDefinition(schema.GetDefinitionContext{}, meta)
+ if err != nil {
+ t.Fatalf("GetDatabaseDefinition: %v", err)
+ }
+ if strings.Contains(ddl, `""."`) {
+ t.Errorf("DDL contains empty schema `\"\".\"` prefix; got:\n%s", ddl)
+ }
+ if !strings.Contains(ddl, `CREATE TABLE "public"."weird.name"`) {
+ t.Errorf("expected schema to be public, got:\n%s", ddl)
+ }
+ if _, parseErr := catalog.New().Exec(ddl, &catalog.ExecOptions{ContinueOnError: true}); parseErr != nil {
+ t.Errorf("omni catalog parse error: %v\nDDL:\n%s", parseErr, ddl)
+ }
+}
+
+// TestGetDatabaseDefinitionWithTestcontainer tests the get_database_definition function
+// by comparing metadata from a database created using the original DDL versus
+// metadata from a database created using the generated DDL.
+//
+//nolint:tparallel
+func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
+ ctx := context.Background()
+
+ // Get PostgreSQL container from testcontainer.go
+ pgContainer := testcontainer.SharedPgContainer(t)
+
+ // Test cases with various PostgreSQL features
+ testCases := []struct {
+ name string
+ ddl string
+ }{
+ {
+ name: "basic_tables_with_constraints",
+ ddl: `
+CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ username VARCHAR(50) NOT NULL UNIQUE,
+ email VARCHAR(100) NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP
+);
+
+CREATE TABLE posts (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER NOT NULL,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ published BOOLEAN DEFAULT false,
+ CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
+);
+
+CREATE INDEX idx_posts_user_id ON posts(user_id);
+CREATE INDEX idx_posts_published ON posts(published) WHERE published = true;
+`,
+ },
+ {
+ name: "sequences_and_custom_types",
+ ddl: `
+CREATE TYPE status_enum AS ENUM ('pending', 'active', 'inactive', 'deleted');
+CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
+
+CREATE SEQUENCE custom_id_seq START WITH 1000 INCREMENT BY 10;
+
+CREATE TABLE items (
+ id INTEGER DEFAULT nextval('custom_id_seq') PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ status status_enum DEFAULT 'pending',
+ user_mood mood
+);
+`,
+ },
+ {
+ name: "composite_types",
+ ddl: `
+-- zz_base sorts after its dependent aa_nested alphabetically, so a correct
+-- dump proves dependency ordering rather than name ordering.
+CREATE TYPE zz_base AS (street text COLLATE "C", city varchar(50));
+COMMENT ON TYPE zz_base IS 'base address type';
+COMMENT ON COLUMN zz_base.street IS 'street line';
+
+CREATE TYPE aa_nested AS (home zz_base, homes zz_base[], tags text[]);
+
+CREATE TYPE order_status AS ENUM ('pending', 'shipped');
+CREATE TYPE order_info AS (status order_status, note text);
+
+CREATE SCHEMA geo;
+CREATE TYPE geo.point2 AS (lat numeric(9,6), lng numeric(9,6));
+CREATE TYPE wrapper AS (p geo.point2);
+
+CREATE TYPE empty_type AS ();
+
+CREATE TABLE orders (
+ id SERIAL PRIMARY KEY,
+ info order_info,
+ addresses aa_nested,
+ location geo.point2
+);
+`,
+ },
+ {
+ name: "owned_sequence_dependency_order",
+ ddl: `
+-- This reproduces the customer's reported issue
+-- Create a sequence that will be owned by a column
+CREATE SEQUENCE history_cost_changes_id_seq
+ AS bigint
+ START WITH 1
+ INCREMENT BY 1
+ MINVALUE 1
+ MAXVALUE 9223372036854775807
+ NO CYCLE;
+
+-- Create table using the sequence in DEFAULT clause
+CREATE TABLE history_cost_changes (
+ id bigint DEFAULT nextval('history_cost_changes_id_seq'::regclass) NOT NULL PRIMARY KEY,
+ amount numeric(10,2),
+ description text,
+ created_at timestamp DEFAULT CURRENT_TIMESTAMP
+);
+
+-- Make the sequence owned by the column (critical for reproducing the bug!)
+ALTER SEQUENCE history_cost_changes_id_seq OWNED BY history_cost_changes.id;
+
+-- Create another table that also references the same sequence (edge case)
+CREATE TABLE history_audit (
+ audit_id bigint DEFAULT nextval('history_cost_changes_id_seq'::regclass) NOT NULL,
+ change_type varchar(50)
+);
+`,
+ },
+ {
+ name: "views_and_functions",
+ ddl: `
+CREATE TABLE employees (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ department VARCHAR(50),
+ salary DECIMAL(10, 2)
+);
+
+CREATE VIEW active_employees AS
+SELECT id, name, department
+FROM employees
+WHERE department IS NOT NULL;
+
+CREATE FUNCTION get_employee_count(dept VARCHAR) RETURNS INTEGER AS $$
+BEGIN
+ RETURN (SELECT COUNT(*) FROM employees WHERE department = dept);
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE FUNCTION calculate_bonus(emp_id INTEGER) RETURNS DECIMAL AS $$
+DECLARE
+ emp_salary DECIMAL;
+BEGIN
+ SELECT salary INTO emp_salary FROM employees WHERE id = emp_id;
+ RETURN emp_salary * 0.1;
+END;
+$$ LANGUAGE plpgsql;
+`,
+ },
+ {
+ name: "partitioned_tables",
+ ddl: `
+CREATE TABLE sales (
+ id SERIAL,
+ sale_date DATE NOT NULL,
+ amount DECIMAL(10, 2) NOT NULL,
+ region VARCHAR(50) NOT NULL
+) PARTITION BY RANGE (sale_date);
+
+CREATE TABLE sales_2023_q1 PARTITION OF sales
+FOR VALUES FROM ('2023-01-01') TO ('2023-04-01');
+
+CREATE TABLE sales_2023_q2 PARTITION OF sales
+FOR VALUES FROM ('2023-04-01') TO ('2023-07-01');
+
+CREATE TABLE sales_2023_q3 PARTITION OF sales
+FOR VALUES FROM ('2023-07-01') TO ('2023-10-01');
+
+CREATE TABLE sales_2023_q4 PARTITION OF sales
+FOR VALUES FROM ('2023-10-01') TO ('2024-01-01');
+`,
+ },
+ {
+ name: "extensions_and_advanced_features",
+ ddl: `
+CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
+
+CREATE TABLE documents (
+ id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ tags TEXT[],
+ metadata JSONB,
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE INDEX idx_documents_metadata ON documents USING GIN (metadata);
+CREATE INDEX idx_documents_tags ON documents USING GIN (tags);
+`,
+ },
+ {
+ name: "check_constraints_and_comments",
+ ddl: `
+CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ price DECIMAL(10, 2) NOT NULL,
+ quantity INTEGER NOT NULL,
+ CONSTRAINT chk_price CHECK (price > 0),
+ CONSTRAINT chk_quantity CHECK (quantity >= 0)
+);
+
+COMMENT ON TABLE products IS 'Product catalog table';
+COMMENT ON COLUMN products.name IS 'Product name';
+COMMENT ON COLUMN products.price IS 'Product price in USD';
+`,
+ },
+ {
+ name: "materialized_views",
+ ddl: `
+CREATE TABLE raw_data (
+ id SERIAL PRIMARY KEY,
+ data_value NUMERIC NOT NULL,
+ category VARCHAR(50) NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE MATERIALIZED VIEW data_summary AS
+SELECT category,
+ COUNT(*) as count,
+ AVG(data_value) as avg_value,
+ MAX(data_value) as max_value,
+ MIN(data_value) as min_value
+FROM raw_data
+GROUP BY category
+WITH NO DATA;
+
+CREATE INDEX idx_data_summary_category ON data_summary(category);
+`,
+ },
+ {
+ name: "comprehensive_views",
+ ddl: `
+CREATE TABLE customers (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ email VARCHAR(100) UNIQUE,
+ active BOOLEAN DEFAULT true,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE orders (
+ id SERIAL PRIMARY KEY,
+ customer_id INTEGER REFERENCES customers(id),
+ total_amount DECIMAL(10, 2) NOT NULL,
+ order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ status VARCHAR(20) DEFAULT 'pending'
+);
+
+-- View with public schema references and various formatting
+CREATE VIEW public.active_customer_orders AS
+ SELECT
+ c.id AS customer_id,
+ c.name,
+ c.email,
+ o.id AS order_id,
+ o.total_amount,
+ o.order_date
+ FROM
+ public.customers c
+ INNER JOIN public.orders o ON c.id = o.customer_id
+ WHERE
+ c.active = TRUE
+ AND o.status = 'completed';
+
+-- Simple view without schema qualification
+CREATE VIEW customer_summary AS
+SELECT
+ id,
+ name,
+ email,
+ (SELECT COUNT(*) FROM orders WHERE customer_id = customers.id) as order_count
+FROM customers
+WHERE active = true;
+
+-- Materialized view with complex aggregation
+CREATE MATERIALIZED VIEW monthly_sales_summary AS
+SELECT
+ DATE_TRUNC('month', order_date) as month,
+ COUNT(*) as order_count,
+ SUM(total_amount) as total_revenue,
+ AVG(total_amount) as avg_order_value,
+ COUNT(DISTINCT customer_id) as unique_customers
+FROM public.orders
+WHERE status = 'completed'
+GROUP BY DATE_TRUNC('month', order_date)
+ORDER BY month
+WITH NO DATA;
+
+CREATE UNIQUE INDEX idx_monthly_sales_month ON monthly_sales_summary(month);
+`,
+ },
+ {
+ name: "triggers",
+ ddl: `
+CREATE TABLE audit_log (
+ id SERIAL PRIMARY KEY,
+ table_name VARCHAR(50) NOT NULL,
+ operation VARCHAR(10) NOT NULL,
+ user_name VARCHAR(50),
+ timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE accounts (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ balance DECIMAL(10, 2) DEFAULT 0,
+ last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE OR REPLACE FUNCTION update_last_modified()
+RETURNS TRIGGER AS $$
+BEGIN
+ NEW.last_modified = CURRENT_TIMESTAMP;
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER trg_update_last_modified
+BEFORE UPDATE ON accounts
+FOR EACH ROW
+EXECUTE FUNCTION update_last_modified();
+
+CREATE OR REPLACE FUNCTION log_account_changes()
+RETURNS TRIGGER AS $$
+BEGIN
+ INSERT INTO audit_log (table_name, operation, user_name)
+ VALUES ('accounts', TG_OP, current_user);
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER trg_audit_accounts
+AFTER INSERT OR UPDATE OR DELETE ON accounts
+FOR EACH ROW
+EXECUTE FUNCTION log_account_changes();
+`,
+ },
+ {
+ name: "foreign_key_on_delete_cascade",
+ ddl: `
+CREATE TABLE categories (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL
+);
+
+CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ category_id INTEGER,
+ CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
+);
+
+CREATE TABLE orders (
+ id SERIAL PRIMARY KEY,
+ product_id INTEGER,
+ quantity INTEGER DEFAULT 1,
+ CONSTRAINT fk_product_cascade FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE SET NULL
+);
+`,
+ },
+ {
+ name: "foreign_key_on_update_actions",
+ ddl: `
+CREATE TABLE departments (
+ code VARCHAR(10) PRIMARY KEY,
+ name VARCHAR(100) NOT NULL
+);
+
+CREATE TABLE employees (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ dept_code VARCHAR(10),
+ manager_id INTEGER,
+ CONSTRAINT fk_dept_restrict FOREIGN KEY (dept_code) REFERENCES departments(code) ON UPDATE RESTRICT ON DELETE SET NULL,
+ CONSTRAINT fk_manager_cascade FOREIGN KEY (manager_id) REFERENCES employees(id) ON UPDATE CASCADE ON DELETE CASCADE
+);
+
+CREATE TABLE projects (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ lead_emp_id INTEGER,
+ dept_code VARCHAR(10),
+ CONSTRAINT fk_lead_setnull FOREIGN KEY (lead_emp_id) REFERENCES employees(id) ON DELETE SET NULL ON UPDATE SET NULL,
+ CONSTRAINT fk_proj_dept FOREIGN KEY (dept_code) REFERENCES departments(code) ON DELETE RESTRICT ON UPDATE RESTRICT
+);
+`,
+ },
+ {
+ name: "foreign_key_no_action",
+ ddl: `
+CREATE TABLE countries (
+ id SERIAL PRIMARY KEY,
+ code VARCHAR(3) UNIQUE NOT NULL,
+ name VARCHAR(100) NOT NULL
+);
+
+CREATE TABLE states (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ country_id INTEGER,
+ -- Explicit NO ACTION
+ CONSTRAINT fk_country_explicit FOREIGN KEY (country_id) REFERENCES countries(id) ON DELETE NO ACTION ON UPDATE NO ACTION
+);
+
+CREATE TABLE cities (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ state_id INTEGER,
+ -- Implicit NO ACTION (default behavior)
+ CONSTRAINT fk_state_implicit FOREIGN KEY (state_id) REFERENCES states(id)
+);
+`,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+
+ // Create first database with original DDL using unique name
+ dbNameA := fmt.Sprintf("test_a_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+ _, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameA))
+ require.NoError(t, err)
+
+ // Create driver for database A
+ driverA, err := createPgDriver(ctx, pgContainer.GetHost(), pgContainer.GetPort(), dbNameA)
+ require.NoError(t, err)
+ defer driverA.Close(ctx)
+
+ // Execute the original DDL
+ _, err = driverA.GetDB().ExecContext(ctx, tc.ddl)
+ require.NoError(t, err)
+
+ // Get metadata A using Driver.SyncDBSchema
+ metadataA, err := driverA.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Generate database definition using GetDatabaseDefinition
+ generatedDDL, err := GetDatabaseDefinition(schema.GetDefinitionContext{PrintHeader: true}, metadataA)
+ require.NoError(t, err)
+ require.NotEmpty(t, generatedDDL, "generated DDL should not be empty")
+
+ // Create second database and apply generated DDL using unique name
+ dbNameB := fmt.Sprintf("test_b_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+ _, err = pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameB))
+ require.NoError(t, err)
+
+ // Create driver for database B
+ driverB, err := createPgDriver(ctx, pgContainer.GetHost(), pgContainer.GetPort(), dbNameB)
+ require.NoError(t, err)
+ defer driverB.Close(ctx)
+
+ // Execute the generated DDL
+ _, err = driverB.GetDB().ExecContext(ctx, generatedDDL)
+ require.NoError(t, err, "failed to execute generated DDL: %s", generatedDDL)
+
+ // Get metadata B using Driver.SyncDBSchema
+ metadataB, err := driverB.SyncDBSchema(ctx)
+ require.NoError(t, err)
+
+ // Compare metadata A and B
+ compareFullMetadata(t, metadataA, metadataB)
+ })
+ }
+}
+
+// compareFullMetadata compares all aspects of database metadata
+func compareFullMetadata(t *testing.T, metaA, metaB *storepb.DatabaseSchemaMetadata) {
+ // Compare extensions
+ compareExtensionsDef(t, metaA.Extensions, metaB.Extensions)
+
+ // Compare schemas
+ require.Equal(t, len(metaA.Schemas), len(metaB.Schemas), "number of schemas should match")
+
+ // Find the public schema in both
+ var schemaA, schemaB *storepb.SchemaMetadata
+ for _, schema := range metaA.Schemas {
+ if schema.Name == "public" {
+ schemaA = schema
+ break
+ }
+ }
+ for _, schema := range metaB.Schemas {
+ if schema.Name == "public" {
+ schemaB = schema
+ break
+ }
+ }
+
+ require.NotNil(t, schemaA, "metadata A should have public schema")
+ require.NotNil(t, schemaB, "metadata B should have public schema")
+
+ // Compare schema contents
+ compareSchemaContents(t, schemaA, schemaB)
+}
+
+// compareSchemaContents compares the contents of two schemas
+func compareSchemaContents(t *testing.T, schemaA, schemaB *storepb.SchemaMetadata) {
+ // Compare enums
+ compareEnumsDef(t, schemaA.EnumTypes, schemaB.EnumTypes)
+
+ // Compare composite types
+ compareCompositeTypesDef(t, schemaA.CompositeTypes, schemaB.CompositeTypes)
+
+ // Compare sequences (excluding implicit sequences from SERIAL columns)
+ compareExplicitSequences(t, schemaA.Sequences, schemaB.Sequences)
+
+ // Compare tables
+ compareTablesDef(t, schemaA.Tables, schemaB.Tables)
+
+ // Compare views
+ compareViewsDef(t, schemaA.Views, schemaB.Views)
+
+ // Compare materialized views
+ compareMaterializedViewsDef(t, schemaA.MaterializedViews, schemaB.MaterializedViews)
+
+ // Compare functions
+ compareFunctionsDef(t, schemaA.Functions, schemaB.Functions)
+}
+
+// compareExplicitSequences compares only explicitly created sequences
+func compareExplicitSequences(t *testing.T, seqsA, seqsB []*storepb.SequenceMetadata) {
+ // Filter out implicit sequences (those owned by columns)
+ explicitA := filterExplicitSequences(seqsA)
+ explicitB := filterExplicitSequences(seqsB)
+
+ require.Equal(t, len(explicitA), len(explicitB), "number of explicit sequences should match")
+
+ // Create maps for comparison
+ mapA := make(map[string]*storepb.SequenceMetadata)
+ for _, seq := range explicitA {
+ mapA[seq.Name] = seq
+ }
+
+ for _, seqB := range explicitB {
+ seqA, exists := mapA[seqB.Name]
+ require.True(t, exists, "sequence %s should exist in metadata A", seqB.Name)
+
+ // Compare sequence properties
+ require.Equal(t, seqA.Start, seqB.Start, "sequence %s: start value should match", seqB.Name)
+ require.Equal(t, seqA.Increment, seqB.Increment, "sequence %s: increment should match", seqB.Name)
+ require.Equal(t, seqA.MinValue, seqB.MinValue, "sequence %s: min value should match", seqB.Name)
+ require.Equal(t, seqA.MaxValue, seqB.MaxValue, "sequence %s: max value should match", seqB.Name)
+ require.Equal(t, seqA.Cycle, seqB.Cycle, "sequence %s: cycle should match", seqB.Name)
+ require.Equal(t, seqA.Comment, seqB.Comment, "sequence %s: comment should match", seqB.Name)
+ }
+}
+
+// filterExplicitSequences returns only sequences that are not owned by columns
+func filterExplicitSequences(sequences []*storepb.SequenceMetadata) []*storepb.SequenceMetadata {
+ var result []*storepb.SequenceMetadata
+ for _, seq := range sequences {
+ if seq.OwnerTable == "" || seq.OwnerColumn == "" {
+ result = append(result, seq)
+ }
+ }
+ return result
+}
+
+// compareMaterializedViewsDef compares materialized views between two schemas using PostgreSQL view comparer
+func compareMaterializedViewsDef(t *testing.T, viewsA, viewsB []*storepb.MaterializedViewMetadata) {
+ require.Equal(t, len(viewsA), len(viewsB), "number of materialized views should match")
+
+ // Create maps for comparison
+ mapA := make(map[string]*storepb.MaterializedViewMetadata)
+ for _, view := range viewsA {
+ mapA[view.Name] = view
+ }
+
+ for _, viewB := range viewsB {
+ viewA, exists := mapA[viewB.Name]
+ require.True(t, exists, "materialized view %s should exist in metadata A", viewB.Name)
+
+ // Compare view definitions using normalized string comparison
+ defA := normalizeSQLDef(viewA.Definition)
+ defB := normalizeSQLDef(viewB.Definition)
+ definitionsEqual := defA == defB
+
+ // Assert that materialized view definitions must be equal after sophisticated PostgreSQL parsing
+ require.True(t, definitionsEqual,
+ "materialized view %s: definitions must be equal using PostgreSQL view comparer (A: %q, B: %q)",
+ viewB.Name, viewA.Definition, viewB.Definition)
+
+ require.Equal(t, viewA.Comment, viewB.Comment,
+ "materialized view %s: comment should match", viewB.Name)
+
+ // Compare indexes on materialized views
+ compareIndexesDef(t, viewB.Name, viewA.Indexes, viewB.Indexes)
+ }
+}
+
+// normalizeSQLDef normalizes SQL for comparison
+func normalizeSQLDef(sql string) string {
+ // Convert to lowercase
+ sql = strings.ToLower(sql)
+
+ // Replace multiple spaces/newlines with single space
+ sql = strings.Join(strings.Fields(sql), " ")
+
+ // Remove trailing semicolons
+ sql = strings.TrimSuffix(sql, ";")
+
+ // Remove schema qualifiers for public schema
+ sql = strings.ReplaceAll(sql, "public.", "")
+
+ // Final trim
+ sql = strings.TrimSpace(sql)
+
+ return sql
+}
+
+// normalizeExprDef normalizes an expression for comparison
+func normalizeExprDef(expr string) string {
+ // Convert to lowercase
+ expr = strings.ToLower(expr)
+
+ // Replace multiple spaces with single space
+ expr = strings.Join(strings.Fields(expr), " ")
+
+ // Remove spaces around parentheses
+ expr = strings.ReplaceAll(expr, " (", "(")
+ expr = strings.ReplaceAll(expr, "( ", "(")
+ expr = strings.ReplaceAll(expr, " )", ")")
+ expr = strings.ReplaceAll(expr, ") ", ")")
+
+ // Normalize quotes around identifiers
+ expr = strings.ReplaceAll(expr, "'", "")
+
+ return expr
+}
+
+// compareTablesDef compares tables between two schemas
+func compareTablesDef(t *testing.T, tablesA, tablesB []*storepb.TableMetadata) {
+ require.Equal(t, len(tablesA), len(tablesB), "number of tables should match")
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.TableMetadata)
+ for _, table := range tablesA {
+ mapA[table.Name] = table
+ }
+
+ mapB := make(map[string]*storepb.TableMetadata)
+ for _, table := range tablesB {
+ mapB[table.Name] = table
+ }
+
+ // Compare each table
+ for name, tableA := range mapA {
+ tableB, exists := mapB[name]
+ require.True(t, exists, "table %s should exist in metadata B", name)
+
+ // Compare columns
+ compareColumnsDef(t, name, tableA.Columns, tableB.Columns)
+
+ // Compare indexes
+ compareIndexesDef(t, name, tableA.Indexes, tableB.Indexes)
+
+ // Compare foreign keys
+ compareForeignKeysDef(t, name, tableA.ForeignKeys, tableB.ForeignKeys)
+
+ // Compare check constraints
+ compareCheckConstraintsDef(t, name, tableA.CheckConstraints, tableB.CheckConstraints)
+
+ // Compare partitions
+ comparePartitionsDef(t, name, tableA.Partitions, tableB.Partitions)
+
+ // Compare triggers
+ compareTriggersDef(t, name, tableA.Triggers, tableB.Triggers)
+
+ // Compare comments
+ require.Equal(t, tableA.Comment, tableB.Comment, "table %s: comment should match", name)
+ }
+}
+
+// compareColumnsDef compares columns between two tables
+func compareColumnsDef(t *testing.T, tableName string, colsA, colsB []*storepb.ColumnMetadata) {
+ require.Equal(t, len(colsA), len(colsB), "table %s: number of columns should match", tableName)
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.ColumnMetadata)
+ for _, col := range colsA {
+ mapA[col.Name] = col
+ }
+
+ for _, colB := range colsB {
+ colA, exists := mapA[colB.Name]
+ require.True(t, exists, "table %s: column %s should exist in metadata A", tableName, colB.Name)
+
+ // Compare column properties
+ require.Equal(t, colA.Type, colB.Type, "table %s, column %s: type should match", tableName, colB.Name)
+ require.Equal(t, colA.Nullable, colB.Nullable, "table %s, column %s: nullable should match", tableName, colB.Name)
+ require.Equal(t, colA.Comment, colB.Comment, "table %s, column %s: comment should match", tableName, colB.Name)
+
+ // Compare default values if both exist
+ hasDefaultA := colA.Default != ""
+ hasDefaultB := colB.Default != ""
+ if hasDefaultA && hasDefaultB {
+ // Default values might be represented differently, so we just check they exist
+ t.Logf("table %s, column %s: default values exist in both", tableName, colB.Name)
+ }
+ }
+}
+
+// compareIndexesDef compares indexes between two tables
+func compareIndexesDef(t *testing.T, tableName string, indexesA, indexesB []*storepb.IndexMetadata) {
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.IndexMetadata)
+ for _, idx := range indexesA {
+ mapA[idx.Name] = idx
+ }
+
+ mapB := make(map[string]*storepb.IndexMetadata)
+ for _, idx := range indexesB {
+ mapB[idx.Name] = idx
+ }
+
+ // Compare common indexes
+ for name, idxB := range mapB {
+ idxA, exists := mapA[name]
+ if !exists {
+ // Some indexes might be system-generated
+ t.Logf("table %s: index %s exists in B but not in A (might be implicit)", tableName, name)
+ continue
+ }
+
+ require.Equal(t, idxA.Primary, idxB.Primary, "table %s, index %s: primary should match", tableName, name)
+ require.Equal(t, idxA.Unique, idxB.Unique, "table %s, index %s: unique should match", tableName, name)
+
+ // Compare expressions
+ if len(idxA.Expressions) == len(idxB.Expressions) {
+ for i := range idxA.Expressions {
+ exprA := normalizeExprDef(idxA.Expressions[i])
+ exprB := normalizeExprDef(idxB.Expressions[i])
+ require.Equal(t, exprA, exprB, "table %s, index %s: expression[%d] should match", tableName, name, i)
+ }
+ }
+
+ // Compare IsConstraint field
+ require.Equal(t, idxA.IsConstraint, idxB.IsConstraint, "table %s, index %s: IsConstraint should match", tableName, name)
+
+ // Compare comment
+ require.Equal(t, idxA.Comment, idxB.Comment, "table %s, index %s: comment should match", tableName, name)
+ }
+}
+
+// compareForeignKeysDef compares foreign keys between two tables
+func compareForeignKeysDef(t *testing.T, tableName string, fksA, fksB []*storepb.ForeignKeyMetadata) {
+ require.Equal(t, len(fksA), len(fksB), "table %s: number of foreign keys should match", tableName)
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.ForeignKeyMetadata)
+ for _, fk := range fksA {
+ mapA[fk.Name] = fk
+ }
+
+ for _, fkB := range fksB {
+ fkA, exists := mapA[fkB.Name]
+ require.True(t, exists, "table %s: foreign key %s should exist in metadata A", tableName, fkB.Name)
+
+ require.ElementsMatch(t, fkA.Columns, fkB.Columns, "table %s, FK %s: columns should match", tableName, fkB.Name)
+ require.Equal(t, fkA.ReferencedTable, fkB.ReferencedTable, "table %s, FK %s: referenced table should match", tableName, fkB.Name)
+ require.ElementsMatch(t, fkA.ReferencedColumns, fkB.ReferencedColumns, "table %s, FK %s: referenced columns should match", tableName, fkB.Name)
+
+ // Compare ON DELETE and ON UPDATE actions
+ require.Equal(t, fkA.OnDelete, fkB.OnDelete, "table %s, FK %s: ON DELETE action should match", tableName, fkB.Name)
+ require.Equal(t, fkA.OnUpdate, fkB.OnUpdate, "table %s, FK %s: ON UPDATE action should match", tableName, fkB.Name)
+ }
+}
+
+// compareCheckConstraintsDef compares check constraints between two tables
+func compareCheckConstraintsDef(t *testing.T, tableName string, checksA, checksB []*storepb.CheckConstraintMetadata) {
+ require.Equal(t, len(checksA), len(checksB), "table %s: number of check constraints should match", tableName)
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.CheckConstraintMetadata)
+ for _, check := range checksA {
+ mapA[check.Name] = check
+ }
+
+ for _, checkB := range checksB {
+ checkA, exists := mapA[checkB.Name]
+ require.True(t, exists, "table %s: check constraint %s should exist in metadata A", tableName, checkB.Name)
+
+ // Normalize and compare expressions
+ exprA := normalizeExprDef(checkA.Expression)
+ exprB := normalizeExprDef(checkB.Expression)
+ require.Equal(t, exprA, exprB, "table %s, check %s: expression should match", tableName, checkB.Name)
+ }
+}
+
+// comparePartitionsDef compares partitions between two tables
+func comparePartitionsDef(t *testing.T, tableName string, partsA, partsB []*storepb.TablePartitionMetadata) {
+ require.Equal(t, len(partsA), len(partsB), "table %s: number of partitions should match", tableName)
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.TablePartitionMetadata)
+ for _, part := range partsA {
+ mapA[part.Name] = part
+ }
+
+ for _, partB := range partsB {
+ partA, exists := mapA[partB.Name]
+ require.True(t, exists, "table %s: partition %s should exist in metadata A", tableName, partB.Name)
+ require.Equal(t, partA.Expression, partB.Expression, "table %s, partition %s: expression should match", tableName, partB.Name)
+ require.Equal(t, partA.Value, partB.Value, "table %s, partition %s: value should match", tableName, partB.Name)
+ }
+}
+
+// compareTriggersDef compares triggers between two tables
+func compareTriggersDef(t *testing.T, tableName string, triggersA, triggersB []*storepb.TriggerMetadata) {
+ require.Equal(t, len(triggersA), len(triggersB), "table %s: number of triggers should match", tableName)
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.TriggerMetadata)
+ for _, trigger := range triggersA {
+ mapA[trigger.Name] = trigger
+ }
+
+ for _, triggerB := range triggersB {
+ triggerA, exists := mapA[triggerB.Name]
+ require.True(t, exists, "table %s: trigger %s should exist in metadata A", tableName, triggerB.Name)
+
+ // Compare trigger properties
+ require.Equal(t, normalizeSQLDef(triggerA.Body), normalizeSQLDef(triggerB.Body),
+ "table %s, trigger %s: body should match", tableName, triggerB.Name)
+ require.Equal(t, triggerA.Comment, triggerB.Comment,
+ "table %s, trigger %s: comment should match", tableName, triggerB.Name)
+ }
+}
+
+// compareViewsDef compares views between two schemas using PostgreSQL view comparer
+func compareViewsDef(t *testing.T, viewsA, viewsB []*storepb.ViewMetadata) {
+ require.Equal(t, len(viewsA), len(viewsB), "number of views should match")
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.ViewMetadata)
+ for _, view := range viewsA {
+ mapA[view.Name] = view
+ }
+
+ for _, viewB := range viewsB {
+ viewA, exists := mapA[viewB.Name]
+ require.True(t, exists, "view %s should exist in metadata A", viewB.Name)
+
+ // Compare view definitions using normalized string comparison
+ require.Equal(t, normalizeSQLDef(viewA.Definition), normalizeSQLDef(viewB.Definition),
+ "view %s: definitions should match", viewB.Name)
+
+ // Compare comment
+ require.Equal(t, viewA.Comment, viewB.Comment, "view %s: comment should match", viewB.Name)
+ }
+}
+
+// compareFunctionsDef compares functions between two schemas
+func compareFunctionsDef(t *testing.T, funcsA, funcsB []*storepb.FunctionMetadata) {
+ require.Equal(t, len(funcsA), len(funcsB), "number of functions should match")
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.FunctionMetadata)
+ for _, fn := range funcsA {
+ mapA[fn.Name] = fn
+ }
+
+ for _, fnB := range funcsB {
+ fnA, exists := mapA[fnB.Name]
+ require.True(t, exists, "function %s should exist in metadata A", fnB.Name)
+
+ // Compare function definitions
+ defA := normalizeSQLDef(fnA.Definition)
+ defB := normalizeSQLDef(fnB.Definition)
+ require.Equal(t, defA, defB, "function %s: definition should match", fnB.Name)
+
+ // Compare comment
+ require.Equal(t, fnA.Comment, fnB.Comment, "function %s: comment should match", fnB.Name)
+ }
+}
+
+// compareEnumsDef compares enum types between two schemas
+func compareEnumsDef(t *testing.T, enumsA, enumsB []*storepb.EnumTypeMetadata) {
+ require.Equal(t, len(enumsA), len(enumsB), "number of enums should match")
+
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.EnumTypeMetadata)
+ for _, enum := range enumsA {
+ mapA[enum.Name] = enum
+ }
+
+ for _, enumB := range enumsB {
+ enumA, exists := mapA[enumB.Name]
+ require.True(t, exists, "enum %s should exist in metadata A", enumB.Name)
+ require.ElementsMatch(t, enumA.Values, enumB.Values, "enum %s: values should match", enumB.Name)
+ require.Equal(t, enumA.Comment, enumB.Comment, "enum %s: comment should match", enumB.Name)
+ }
+}
+
+func compareCompositeTypesDef(t *testing.T, compositesA, compositesB []*storepb.CompositeTypeMetadata) {
+ require.Equal(t, len(compositesA), len(compositesB), "number of composite types should match")
+
+ mapA := make(map[string]*storepb.CompositeTypeMetadata)
+ for _, composite := range compositesA {
+ mapA[composite.Name] = composite
+ }
+
+ for _, compositeB := range compositesB {
+ compositeA, exists := mapA[compositeB.Name]
+ require.True(t, exists, "composite type %s should exist in metadata A", compositeB.Name)
+ require.Equal(t, compositeA.Comment, compositeB.Comment, "composite type %s: comment should match", compositeB.Name)
+ require.Equal(t, len(compositeA.Attributes), len(compositeB.Attributes), "composite type %s: attribute count should match", compositeB.Name)
+ for i, attributeA := range compositeA.Attributes {
+ attributeB := compositeB.Attributes[i]
+ require.Equal(t, attributeA.Name, attributeB.Name, "composite type %s: attribute %d name should match", compositeB.Name, i)
+ require.Equal(t, attributeA.Type, attributeB.Type, "composite type %s: attribute %s type should match", compositeB.Name, attributeA.Name)
+ require.Equal(t, attributeA.Collation, attributeB.Collation, "composite type %s: attribute %s collation should match", compositeB.Name, attributeA.Name)
+ require.Equal(t, attributeA.Comment, attributeB.Comment, "composite type %s: attribute %s comment should match", compositeB.Name, attributeA.Name)
+ }
+ }
+}
+
+// compareExtensionsDef compares extensions between two databases
+func compareExtensionsDef(t *testing.T, extsA, extsB []*storepb.ExtensionMetadata) {
+ // Create maps for easier comparison
+ mapA := make(map[string]*storepb.ExtensionMetadata)
+ for _, ext := range extsA {
+ mapA[ext.Name] = ext
+ }
+
+ for _, extB := range extsB {
+ extA, exists := mapA[extB.Name]
+ require.True(t, exists, "extension %s should exist in metadata A", extB.Name)
+ require.Equal(t, extA.Schema, extB.Schema, "extension %s: schema should match", extB.Name)
+ require.Equal(t, extA.Version, extB.Version, "extension %s: version should match", extB.Name)
+ }
+}
+
+// TestCheckConstraintOrderStability tests that CHECK constraints maintain consistent order
+// when generating SDL multiple times from the same metadata
+func TestCheckConstraintOrderStability(t *testing.T) {
+ // Simulate database metadata with multiple CHECK constraints
+ // (similar to the packages_debian_group_distributions table shown in the screenshot)
+ dbMetadata := &storepb.DatabaseSchemaMetadata{
+ Name: "testdb",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "packages_debian_group_distributions",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "bigint", Nullable: false},
+ {Name: "suite", Type: "character varying(255)", Nullable: false},
+ {Name: "codename", Type: "character varying(255)", Nullable: false},
+ {Name: "signed_file", Type: "text", Nullable: true},
+ {Name: "description", Type: "character varying(255)", Nullable: true},
+ {Name: "origin", Type: "character varying(255)", Nullable: true},
+ {Name: "file", Type: "character varying(255)", Nullable: true},
+ {Name: "label", Type: "character varying(255)", Nullable: true},
+ {Name: "version", Type: "character varying(255)", Nullable: true},
+ {Name: "file_signature", Type: "character varying(4096)", Nullable: true},
+ },
+ CheckConstraints: []*storepb.CheckConstraintMetadata{
+ {Name: "check_e7c928a24b", Expression: "(char_length((suite)::text) <= 255)"},
+ {Name: "check_590e18405a", Expression: "(char_length((codename)::text) <= 255)"},
+ {Name: "check_0007e0bf61", Expression: "(char_length(signed_file) <= 255)"},
+ {Name: "check_310ac457b8", Expression: "(char_length((description)::text) <= 255)"},
+ {Name: "check_3d6f87fc31", Expression: "(char_length((file_signature)::text) <= 4096)"},
+ {Name: "check_3fdadf4a0c", Expression: "(char_length((version)::text) <= 255)"},
+ {Name: "check_b057cd840a", Expression: "(char_length((origin)::text) <= 255)"},
+ {Name: "check_be5ed8d307", Expression: "(char_length((file)::text) <= 255)"},
+ {Name: "check_d3244bfc0b", Expression: "(char_length((label)::text) <= 255)"},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SkipBackupSchema: false,
+ PrintHeader: false,
+ SDLFormat: true,
+ }
+
+ // Generate SDL first time
+ sdl1, err := GetDatabaseDefinition(ctx, dbMetadata)
+ require.NoError(t, err)
+ require.NotEmpty(t, sdl1)
+
+ t.Logf("First SDL generation:\n%s", sdl1)
+
+ // Generate SDL second time from the same metadata
+ sdl2, err := GetDatabaseDefinition(ctx, dbMetadata)
+ require.NoError(t, err)
+ require.NotEmpty(t, sdl2)
+
+ t.Logf("Second SDL generation:\n%s", sdl2)
+
+ // The two SDL outputs should be identical
+ require.Equal(t, sdl1, sdl2, "SDL generation should be deterministic - CHECK constraint order should be stable")
+}
+
+func TestGetDatabaseDefinitionSDLFormat_CompositeTypes(t *testing.T) {
+ // aa_nested sorts before its dependency zz_base alphabetically, so correct
+ // output proves dependency ordering rather than name ordering.
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "order_status", Values: []string{"pending", "shipped"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "aa_nested",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "home", Type: "public.zz_base"},
+ {Name: "homes", Type: "public.zz_base[]"},
+ {Name: "status", Type: "public.order_status"},
+ },
+ },
+ {
+ Name: "zz_base",
+ Comment: "base address type",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text", Collation: `"C"`, Comment: "street line"},
+ {Name: "city", Type: "character varying(50)"},
+ },
+ },
+ {
+ Name: "ext_owned",
+ SkipDump: true,
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ {
+ Name: "empty_type",
+ },
+ },
+ },
+ {
+ Name: "geo",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "point2",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "lat", Type: "numeric(9,6)"},
+ {Name: "lng", Type: "numeric(9,6)"},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ result, err := GetDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
+ require.NoError(t, err)
+
+ assert.Contains(t, result, `CREATE TYPE "public"."zz_base" AS (`)
+ assert.Contains(t, result, `"street" text COLLATE "C"`)
+ assert.Contains(t, result, `"city" character varying(50)`)
+ assert.Contains(t, result, `CREATE TYPE "public"."aa_nested" AS (`)
+ assert.Contains(t, result, `"home" public.zz_base`)
+ assert.Contains(t, result, `"homes" public.zz_base[]`)
+ assert.Contains(t, result, `CREATE TYPE "geo"."point2" AS (`)
+ assert.Contains(t, result, `CREATE TYPE "public"."empty_type" AS (`)
+ assert.Contains(t, result, `COMMENT ON TYPE "public"."zz_base" IS 'base address type';`)
+ assert.Contains(t, result, `COMMENT ON COLUMN "public"."zz_base"."street" IS 'street line';`)
+ assert.NotContains(t, result, "ext_owned", "skip_dump composite types must not be emitted")
+
+ // Dependency order: zz_base must be created before aa_nested.
+ baseIdx := strings.Index(result, `CREATE TYPE "public"."zz_base"`)
+ nestedIdx := strings.Index(result, `CREATE TYPE "public"."aa_nested"`)
+ require.GreaterOrEqual(t, baseIdx, 0)
+ require.GreaterOrEqual(t, nestedIdx, 0)
+ assert.Less(t, baseIdx, nestedIdx, "referenced composite must be emitted before its dependent")
+
+ // Enums come before composites.
+ enumIdx := strings.Index(result, `CREATE TYPE "public"."order_status"`)
+ require.GreaterOrEqual(t, enumIdx, 0)
+ assert.Less(t, enumIdx, baseIdx, "enums must be emitted before composite types")
+
+ // The generated SDL must be loadable by the SDL migration engine.
+ _, err = schema.DiffSDLMigration(storepb.Engine_POSTGRES, "", result, "")
+ require.NoError(t, err, "generated SDL should load: %s", result)
+}
+
+func TestGetMultiFileDatabaseDefinition_CompositeTypes(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "mood", Values: []string{"happy", "sad"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "wrapper",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "b", Type: "public.base"},
+ },
+ },
+ {
+ Name: "base",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ Comment: "base type",
+ },
+ },
+ },
+ },
+ }
+
+ result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
+ require.NoError(t, err)
+
+ var typesFile string
+ var combined strings.Builder
+ for _, file := range result.Files {
+ if file.Name == "schemas/public/types.sql" {
+ typesFile = file.Content
+ }
+ combined.WriteString(file.Content)
+ combined.WriteString("\n")
+ }
+ require.NotEmpty(t, typesFile, "types.sql should exist")
+
+ assert.Contains(t, typesFile, `CREATE TYPE "public"."mood" AS ENUM (`)
+ assert.Contains(t, typesFile, `CREATE TYPE "public"."base" AS (`)
+ assert.Contains(t, typesFile, `CREATE TYPE "public"."wrapper" AS (`)
+ assert.Contains(t, typesFile, `COMMENT ON TYPE "public"."base" IS 'base type';`)
+ assert.Less(t,
+ strings.Index(typesFile, `CREATE TYPE "public"."base" AS (`),
+ strings.Index(typesFile, `CREATE TYPE "public"."wrapper" AS (`),
+ "referenced composite must be emitted before its dependent")
+
+ _, err = schema.DiffSDLMigration(storepb.Engine_POSTGRES, "", combined.String(), "")
+ require.NoError(t, err, "combined multi-file SDL should load")
+}
+
+func TestParseQualifiedTypeIdent(t *testing.T) {
+ testCases := []struct {
+ input string
+ schema string
+ name string
+ ok bool
+ }{
+ {"integer", "", "", false},
+ {"character varying(50)", "", "", false},
+ {"numeric(9,6)", "", "", false},
+ {"text[]", "", "", false},
+ {"timestamp(6) with time zone", "", "", false},
+ {"public.addr", "public", "addr", true},
+ {"public.addr[]", "public", "addr", true},
+ {"public.addr(5)", "public", "addr", true},
+ {`"My Schema"."Weird Type"`, "My Schema", "Weird Type", true},
+ {`"has""quote".t`, `has"quote`, "t", true},
+ {`other.geo`, "other", "geo", true},
+ }
+ for _, tc := range testCases {
+ schemaName, typeName, ok := parseQualifiedTypeIdent(tc.input)
+ assert.Equal(t, tc.ok, ok, "input %q", tc.input)
+ assert.Equal(t, tc.schema, schemaName, "input %q", tc.input)
+ assert.Equal(t, tc.name, typeName, "input %q", tc.input)
+ }
+}
+
+func TestSortCompositeTypesTopologically(t *testing.T) {
+ // Chain across schemas: public.a3 -> other.b2 -> public.c1, with names
+ // chosen so alphabetical order is the reverse of dependency order.
+ c1 := &storepb.CompositeTypeMetadata{
+ Name: "c1",
+ Attributes: []*storepb.CompositeTypeAttribute{{Name: "x", Type: "integer"}},
+ }
+ b2 := &storepb.CompositeTypeMetadata{
+ Name: "b2",
+ Attributes: []*storepb.CompositeTypeAttribute{{Name: "c", Type: "public.c1"}},
+ }
+ a3 := &storepb.CompositeTypeMetadata{
+ Name: "a3",
+ Attributes: []*storepb.CompositeTypeAttribute{{Name: "b", Type: "other.b2[]"}},
+ }
+ ordered := sortCompositeTypesTopologically([]qualifiedCompositeType{
+ {Schema: "public", Composite: a3},
+ {Schema: "other", Composite: b2},
+ {Schema: "public", Composite: c1},
+ })
+ require.Len(t, ordered, 3)
+ assert.Equal(t, "c1", ordered[0].Composite.Name)
+ assert.Equal(t, "b2", ordered[1].Composite.Name)
+ assert.Equal(t, "a3", ordered[2].Composite.Name)
+
+ // Independent types come out in deterministic (schema, name) order.
+ ordered = sortCompositeTypesTopologically([]qualifiedCompositeType{
+ {Schema: "public", Composite: &storepb.CompositeTypeMetadata{Name: "zz"}},
+ {Schema: "public", Composite: &storepb.CompositeTypeMetadata{Name: "aa"}},
+ })
+ require.Len(t, ordered, 2)
+ assert.Equal(t, "aa", ordered[0].Composite.Name)
+ assert.Equal(t, "zz", ordered[1].Composite.Name)
+}
+
+func TestEventTriggerSDLSingleFileOutput(t *testing.T) {
+ metadata := eventTriggerSDLMetadata()
+
+ result, err := GetDatabaseDefinition(schema.GetDefinitionContext{SDLFormat: true}, metadata)
+ require.NoError(t, err)
+ assert.Contains(t, result, `CREATE FUNCTION "public"."audit_ddl"() RETURNS event_trigger`)
+ assert.Contains(t, result, `CREATE EVENT TRIGGER "audit_ddl_start" ON ddl_command_start`)
+ assert.Contains(t, result, `WHEN TAG IN ('CREATE TABLE')`)
+ assert.Contains(t, result, `EXECUTE FUNCTION "public"."audit_ddl"();`)
+ assert.Contains(t, result, `COMMENT ON EVENT TRIGGER "audit_ddl_start" IS 'Audit DDL start';`)
+
+ sdl, err := schema.MetadataToSDL(
+ storepb.Engine_POSTGRES,
+ model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, true),
+ )
+ require.NoError(t, err)
+ assert.Contains(t, sdl, `CREATE EVENT TRIGGER "audit_ddl_start" ON ddl_command_start`)
+ assert.Contains(t, sdl, `COMMENT ON EVENT TRIGGER "audit_ddl_start" IS 'Audit DDL start';`)
+}
+
+func TestEventTriggerSDLMultiFileOutput(t *testing.T) {
+ result, err := GetMultiFileDatabaseDefinition(
+ schema.GetDefinitionContext{SDLFormat: true},
+ eventTriggerSDLMetadata(),
+ )
+ require.NoError(t, err)
+ require.NotNil(t, result)
+
+ fileMap := make(map[string]string)
+ for _, file := range result.Files {
+ fileMap[file.Name] = file.Content
+ }
+
+ eventTriggerFile, ok := fileMap["event_triggers.sql"]
+ require.True(t, ok, "event_triggers.sql file should exist")
+ assert.Contains(t, eventTriggerFile, `CREATE EVENT TRIGGER "audit_ddl_start" ON ddl_command_start`)
+ assert.Contains(t, eventTriggerFile, `WHEN TAG IN ('CREATE TABLE')`)
+ assert.Contains(t, eventTriggerFile, `EXECUTE FUNCTION "public"."audit_ddl"();`)
+ assert.Contains(t, eventTriggerFile, `COMMENT ON EVENT TRIGGER "audit_ddl_start" IS 'Audit DDL start';`)
+}
+
+func eventTriggerSDLMetadata() *storepb.DatabaseSchemaMetadata {
+ return &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Functions: []*storepb.FunctionMetadata{
+ {
+ Name: "audit_ddl",
+ Signature: "audit_ddl()",
+ Definition: `CREATE FUNCTION "public"."audit_ddl"() RETURNS event_trigger
+LANGUAGE plpgsql
+AS $$
+BEGIN
+END;
+$$`,
+ },
+ },
+ },
+ },
+ EventTriggers: []*storepb.EventTriggerMetadata{
+ {
+ Name: "audit_ddl_start",
+ Event: "ddl_command_start",
+ Tags: []string{"CREATE TABLE"},
+ FunctionSchema: "public",
+ FunctionName: "audit_ddl",
+ Enabled: true,
+ Comment: "Audit DDL start",
+ },
+ },
+ }
+}
+
+func TestTriggerSDLSingleFileOutput(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "testdb",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ Triggers: []*storepb.TriggerMetadata{
+ {
+ Name: "audit_trigger",
+ Body: "CREATE TRIGGER audit_trigger AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ assert.Contains(t, result, "CREATE TRIGGER audit_trigger")
+ assert.Contains(t, result, "AFTER INSERT ON public.users")
+ assert.Contains(t, result, "FOR EACH ROW EXECUTE FUNCTION audit_log()")
+}
+
+func TestTriggerSDLMultiFileOutput(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "testdb",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ Triggers: []*storepb.TriggerMetadata{
+ {
+ Name: "audit_trigger",
+ Body: "CREATE TRIGGER audit_trigger AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+ require.NotNil(t, result)
+
+ // Find the users table file
+ var usersFileContent string
+ for _, file := range result.Files {
+ if file.Name == "schemas/public/tables/users.sql" {
+ usersFileContent = file.Content
+ break
+ }
+ }
+
+ require.NotEmpty(t, usersFileContent, "Should have users table file")
+ assert.Contains(t, usersFileContent, "CREATE TRIGGER audit_trigger")
+ assert.Contains(t, usersFileContent, "AFTER INSERT ON public.users")
+}
+
+func TestTriggerSDLWithCompleteDefinition(t *testing.T) {
+ // Test that triggers with complete CREATE TRIGGER statements in Body are output correctly
+ // This simulates triggers dumped from the database where Body contains the full statement
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "testdb",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "ci_builds",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ Triggers: []*storepb.TriggerMetadata{
+ {
+ Name: "ci_builds_loose_fk_trigger",
+ Body: "CREATE TRIGGER ci_builds_loose_fk_trigger AFTER DELETE ON public.ci_builds REFERENCING OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE FUNCTION public.loose_foreign_key_on_builds_projects()",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ // Should contain only one CREATE TRIGGER (not duplicated)
+ createTriggerCount := strings.Count(result, "CREATE TRIGGER ci_builds_loose_fk_trigger")
+ assert.Equal(t, 1, createTriggerCount, "CREATE TRIGGER should appear exactly once, not duplicated")
+
+ // Should contain the complete trigger definition
+ assert.Contains(t, result, "AFTER DELETE ON public.ci_builds")
+ assert.Contains(t, result, "REFERENCING OLD TABLE AS old_table")
+ assert.Contains(t, result, "FOR EACH STATEMENT")
+ assert.Contains(t, result, "EXECUTE FUNCTION public.loose_foreign_key_on_builds_projects()")
+}
+
+func TestTriggerSDLWithComment(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "testdb",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ Triggers: []*storepb.TriggerMetadata{
+ {
+ Name: "audit_trigger",
+ Body: "CREATE TRIGGER audit_trigger AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
+ Comment: "Audit log trigger",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ assert.Contains(t, result, "CREATE TRIGGER audit_trigger")
+ assert.Contains(t, result, "COMMENT ON TRIGGER")
+ assert.Contains(t, result, "audit_trigger")
+ assert.Contains(t, result, "Audit log trigger")
+}
+
+func TestMultipleTriggersSDL(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Name: "testdb",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ Triggers: []*storepb.TriggerMetadata{
+ {
+ Name: "audit_insert",
+ Body: "CREATE TRIGGER audit_insert AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
+ },
+ {
+ Name: "audit_update",
+ Body: "CREATE TRIGGER audit_update AFTER UPDATE ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ ctx := schema.GetDefinitionContext{
+ SDLFormat: true,
+ }
+
+ result, err := GetDatabaseDefinition(ctx, metadata)
+ require.NoError(t, err)
+
+ assert.Contains(t, result, "audit_insert")
+ assert.Contains(t, result, "audit_update")
+}
+
+func TestIsDefinitionProcedure(t *testing.T) {
+ tests := []struct {
+ name string
+ definition string
+ isProcedure bool
+ }{
+ {
+ name: "Simple FUNCTION",
+ definition: `CREATE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "Simple PROCEDURE",
+ definition: `CREATE PROCEDURE test_procedure()
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: true,
+ },
+ {
+ name: "CREATE OR REPLACE FUNCTION",
+ definition: `CREATE OR REPLACE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "CREATE OR REPLACE PROCEDURE",
+ definition: `CREATE OR REPLACE PROCEDURE test_procedure()
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: true,
+ },
+ {
+ name: "FUNCTION with word 'procedure' in comment - should NOT be confused",
+ definition: `CREATE FUNCTION test_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ -- This is not a procedure, it's a function
+ -- The word PROCEDURE appears in this comment
+ RAISE NOTICE 'This function is not a procedure';
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "FUNCTION with word 'procedure' in string literal - should NOT be confused",
+ definition: `CREATE FUNCTION test_function()
+RETURNS text
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ RETURN 'This function is not a PROCEDURE';
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "PROCEDURE with complex body",
+ definition: `CREATE OR REPLACE PROCEDURE update_user_data(
+ user_id INTEGER,
+ new_name VARCHAR
+)
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ old_name VARCHAR;
+BEGIN
+ -- Get old name
+ SELECT name INTO old_name FROM users WHERE id = user_id;
+
+ -- Update the user name
+ UPDATE users SET name = new_name WHERE id = user_id;
+
+ -- Log the change
+ INSERT INTO audit_log (message) VALUES (
+ 'Changed user name from ' || old_name || ' to ' || new_name
+ );
+END;
+$$;`,
+ isProcedure: true,
+ },
+ {
+ name: "FUNCTION with RETURNS TABLE",
+ definition: `CREATE OR REPLACE FUNCTION get_users()
+RETURNS TABLE(id INTEGER, name VARCHAR)
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ RETURN QUERY SELECT users.id, users.name FROM users;
+END;
+$$;`,
+ isProcedure: false,
+ },
+ {
+ name: "Empty definition",
+ definition: "",
+ isProcedure: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := isDefinitionProcedure(tt.definition)
+ assert.Equal(t, tt.isProcedure, result,
+ "isDefinitionProcedure returned %v, expected %v for:\n%s",
+ result, tt.isProcedure, tt.definition)
+ })
+ }
+}
+
+// TestIsDefinitionProcedureRobustness tests edge cases that would fail with string-based detection
+func TestIsDefinitionProcedureRobustness(t *testing.T) {
+ tests := []struct {
+ name string
+ definition string
+ isProcedure bool
+ reason string
+ }{
+ {
+ name: "FUNCTION with 'CREATE PROCEDURE' in string literal",
+ definition: `CREATE FUNCTION test_function()
+RETURNS text
+LANGUAGE sql
+AS $outer$
+ SELECT 'Example: CREATE PROCEDURE foo() LANGUAGE plpgsql AS $body$ BEGIN NULL; END; $body$;'
+$outer$;`,
+ isProcedure: false,
+ reason: "String-based detection might incorrectly identify this as a PROCEDURE due to 'CREATE PROCEDURE' in the string literal",
+ },
+ {
+ name: "FUNCTION with 'PROCEDURE' in multi-line comment",
+ definition: `CREATE FUNCTION complex_function()
+RETURNS void
+LANGUAGE plpgsql
+AS $$
+BEGIN
+ /*
+ * This function performs complex operations
+ * Note: This is NOT a PROCEDURE
+ * PROCEDURE keyword appears in this comment
+ * But it should still be detected as a FUNCTION
+ */
+ RAISE NOTICE 'Processing...';
+END;
+$$;`,
+ isProcedure: false,
+ reason: "Multi-line comments with PROCEDURE keyword should not confuse the detector",
+ },
+ {
+ name: "PROCEDURE with unusual formatting",
+ definition: `CREATE OR REPLACE PROCEDURE
+test_procedure
+(
+)
+LANGUAGE plpgsql
+AS
+$$
+BEGIN
+ NULL;
+END;
+$$;`,
+ isProcedure: true,
+ reason: "Should handle unusual whitespace and formatting",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := isDefinitionProcedure(tt.definition)
+ assert.Equal(t, tt.isProcedure, result,
+ "Test failed: %s\nGot: %v, Expected: %v",
+ tt.reason, result, tt.isProcedure)
+ })
+ }
+}
+
+func TestExtractSequenceNameFromNextval(t *testing.T) {
+ testCases := []struct {
+ name string
+ defaultValue string
+ expectedName string
+ description string
+ }{
+ {
+ name: "Simple sequence name with single quotes",
+ defaultValue: "nextval('users_id_seq')",
+ expectedName: "users_id_seq",
+ description: "Basic case with simple sequence name",
+ },
+ {
+ name: "Sequence name with schema qualification",
+ defaultValue: "nextval('public.users_id_seq')",
+ expectedName: "users_id_seq",
+ description: "Schema-qualified sequence name",
+ },
+ {
+ name: "Sequence with regclass cast",
+ defaultValue: "nextval('users_id_seq'::regclass)",
+ expectedName: "users_id_seq",
+ description: "Sequence with ::regclass type cast",
+ },
+ {
+ name: "Schema and regclass",
+ defaultValue: "nextval('public.users_id_seq'::regclass)",
+ expectedName: "users_id_seq",
+ description: "Schema-qualified sequence with regclass cast",
+ },
+ {
+ name: "Quoted sequence name",
+ defaultValue: `nextval('"Users_Id_Seq"')`,
+ expectedName: "Users_Id_Seq",
+ description: "Sequence name with double quotes for case sensitivity",
+ },
+ {
+ name: "Quoted schema and sequence",
+ defaultValue: `nextval('"Public"."Users_Id_Seq"')`,
+ expectedName: "Users_Id_Seq",
+ description: "Both schema and sequence quoted",
+ },
+ {
+ name: "Mixed case with public schema",
+ defaultValue: `nextval('public."MixedCaseSeq"')`,
+ expectedName: "MixedCaseSeq",
+ description: "Unquoted schema with quoted sequence",
+ },
+ {
+ name: "Quoted schema unquoted sequence",
+ defaultValue: `nextval('"Public".users_id_seq')`,
+ expectedName: "users_id_seq",
+ description: "Quoted schema with unquoted sequence",
+ },
+ {
+ name: "Complex case with all features",
+ defaultValue: `nextval('"MySchema"."MySeq"'::regclass)`,
+ expectedName: "MySeq",
+ description: "Quoted schema, quoted sequence, and regclass",
+ },
+ {
+ name: "Uppercase NEXTVAL",
+ defaultValue: `NEXTVAL('users_id_seq')`,
+ expectedName: "users_id_seq",
+ description: "Case-insensitive NEXTVAL function name",
+ },
+ {
+ name: "Mixed case nextval",
+ defaultValue: `NextVal('users_id_seq')`,
+ expectedName: "users_id_seq",
+ description: "Mixed case nextval function name",
+ },
+ {
+ name: "Sequence with spaces",
+ defaultValue: `nextval( 'users_id_seq' )`,
+ expectedName: "users_id_seq",
+ description: "Extra spaces around sequence name",
+ },
+ {
+ name: "Double quoted outer quotes",
+ defaultValue: `nextval("users_id_seq")`,
+ expectedName: "users_id_seq",
+ description: "Double quotes as outer quotes",
+ },
+ {
+ name: "Empty default",
+ defaultValue: "",
+ expectedName: "",
+ description: "Empty string should return empty",
+ },
+ {
+ name: "No nextval",
+ defaultValue: "42",
+ expectedName: "",
+ description: "Non-nextval default should return empty",
+ },
+ {
+ name: "Malformed nextval",
+ defaultValue: "nextval(",
+ expectedName: "",
+ description: "Malformed nextval should return empty",
+ },
+ {
+ name: "Special characters in schema",
+ defaultValue: `nextval('"schema-name"."seq_name"')`,
+ expectedName: "seq_name",
+ description: "Schema with special characters",
+ },
+ {
+ name: "Multiple dots",
+ defaultValue: `nextval('"my.schema"."my.seq"')`,
+ expectedName: "my.seq",
+ description: "Dots within quoted identifiers",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ result := extractSequenceNameFromNextval(tc.defaultValue)
+ assert.Equal(t, tc.expectedName, result, tc.description)
+ })
+ }
+}
+
+func TestExtractIdentifierFromQualifiedName(t *testing.T) {
+ testCases := []struct {
+ name string
+ qualifiedName string
+ expected string
+ description string
+ }{
+ {
+ name: "Simple identifier",
+ qualifiedName: "users_id_seq",
+ expected: "users_id_seq",
+ description: "No schema qualification",
+ },
+ {
+ name: "Schema qualified",
+ qualifiedName: "public.users_id_seq",
+ expected: "users_id_seq",
+ description: "Simple schema.sequence",
+ },
+ {
+ name: "Both quoted",
+ qualifiedName: `"Public"."Users_Id_Seq"`,
+ expected: `"Users_Id_Seq"`,
+ description: "Both parts quoted",
+ },
+ {
+ name: "Only sequence quoted",
+ qualifiedName: `public."MixedCase"`,
+ expected: `"MixedCase"`,
+ description: "Only sequence name quoted",
+ },
+ {
+ name: "Dot in quoted identifier",
+ qualifiedName: `"my.schema"."my.seq"`,
+ expected: `"my.seq"`,
+ description: "Dots inside quoted parts should be ignored",
+ },
+ {
+ name: "Empty string",
+ qualifiedName: "",
+ expected: "",
+ description: "Empty input should return empty",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ result := extractIdentifierFromQualifiedName(tc.qualifiedName)
+ assert.Equal(t, tc.expected, result, tc.description)
+ })
+ }
+}
diff --git a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
deleted file mode 100644
index 35ba3d1b0a..0000000000
--- a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
+++ /dev/null
@@ -1,947 +0,0 @@
-package pg
-
-import (
- "context"
- "fmt"
- "strings"
- "testing"
-
- "github.com/google/uuid"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-// TestGetDatabaseDefinitionWithTestcontainer tests the get_database_definition function
-// by comparing metadata from a database created using the original DDL versus
-// metadata from a database created using the generated DDL.
-//
-//nolint:tparallel
-func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
- ctx := context.Background()
-
- // Get PostgreSQL container from testcontainer.go
- pgContainer := testcontainer.SharedPgContainer(t)
-
- // Test cases with various PostgreSQL features
- testCases := []struct {
- name string
- ddl string
- }{
- {
- name: "basic_tables_with_constraints",
- ddl: `
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL UNIQUE,
- email VARCHAR(100) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP
-);
-
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- published BOOLEAN DEFAULT false,
- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_posts_user_id ON posts(user_id);
-CREATE INDEX idx_posts_published ON posts(published) WHERE published = true;
-`,
- },
- {
- name: "sequences_and_custom_types",
- ddl: `
-CREATE TYPE status_enum AS ENUM ('pending', 'active', 'inactive', 'deleted');
-CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
-
-CREATE SEQUENCE custom_id_seq START WITH 1000 INCREMENT BY 10;
-
-CREATE TABLE items (
- id INTEGER DEFAULT nextval('custom_id_seq') PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- status status_enum DEFAULT 'pending',
- user_mood mood
-);
-`,
- },
- {
- name: "composite_types",
- ddl: `
--- zz_base sorts after its dependent aa_nested alphabetically, so a correct
--- dump proves dependency ordering rather than name ordering.
-CREATE TYPE zz_base AS (street text COLLATE "C", city varchar(50));
-COMMENT ON TYPE zz_base IS 'base address type';
-COMMENT ON COLUMN zz_base.street IS 'street line';
-
-CREATE TYPE aa_nested AS (home zz_base, homes zz_base[], tags text[]);
-
-CREATE TYPE order_status AS ENUM ('pending', 'shipped');
-CREATE TYPE order_info AS (status order_status, note text);
-
-CREATE SCHEMA geo;
-CREATE TYPE geo.point2 AS (lat numeric(9,6), lng numeric(9,6));
-CREATE TYPE wrapper AS (p geo.point2);
-
-CREATE TYPE empty_type AS ();
-
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- info order_info,
- addresses aa_nested,
- location geo.point2
-);
-`,
- },
- {
- name: "owned_sequence_dependency_order",
- ddl: `
--- This reproduces the customer's reported issue
--- Create a sequence that will be owned by a column
-CREATE SEQUENCE history_cost_changes_id_seq
- AS bigint
- START WITH 1
- INCREMENT BY 1
- MINVALUE 1
- MAXVALUE 9223372036854775807
- NO CYCLE;
-
--- Create table using the sequence in DEFAULT clause
-CREATE TABLE history_cost_changes (
- id bigint DEFAULT nextval('history_cost_changes_id_seq'::regclass) NOT NULL PRIMARY KEY,
- amount numeric(10,2),
- description text,
- created_at timestamp DEFAULT CURRENT_TIMESTAMP
-);
-
--- Make the sequence owned by the column (critical for reproducing the bug!)
-ALTER SEQUENCE history_cost_changes_id_seq OWNED BY history_cost_changes.id;
-
--- Create another table that also references the same sequence (edge case)
-CREATE TABLE history_audit (
- audit_id bigint DEFAULT nextval('history_cost_changes_id_seq'::regclass) NOT NULL,
- change_type varchar(50)
-);
-`,
- },
- {
- name: "views_and_functions",
- ddl: `
-CREATE TABLE employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department VARCHAR(50),
- salary DECIMAL(10, 2)
-);
-
-CREATE VIEW active_employees AS
-SELECT id, name, department
-FROM employees
-WHERE department IS NOT NULL;
-
-CREATE FUNCTION get_employee_count(dept VARCHAR) RETURNS INTEGER AS $$
-BEGIN
- RETURN (SELECT COUNT(*) FROM employees WHERE department = dept);
-END;
-$$ LANGUAGE plpgsql;
-
-CREATE FUNCTION calculate_bonus(emp_id INTEGER) RETURNS DECIMAL AS $$
-DECLARE
- emp_salary DECIMAL;
-BEGIN
- SELECT salary INTO emp_salary FROM employees WHERE id = emp_id;
- RETURN emp_salary * 0.1;
-END;
-$$ LANGUAGE plpgsql;
-`,
- },
- {
- name: "partitioned_tables",
- ddl: `
-CREATE TABLE sales (
- id SERIAL,
- sale_date DATE NOT NULL,
- amount DECIMAL(10, 2) NOT NULL,
- region VARCHAR(50) NOT NULL
-) PARTITION BY RANGE (sale_date);
-
-CREATE TABLE sales_2023_q1 PARTITION OF sales
-FOR VALUES FROM ('2023-01-01') TO ('2023-04-01');
-
-CREATE TABLE sales_2023_q2 PARTITION OF sales
-FOR VALUES FROM ('2023-04-01') TO ('2023-07-01');
-
-CREATE TABLE sales_2023_q3 PARTITION OF sales
-FOR VALUES FROM ('2023-07-01') TO ('2023-10-01');
-
-CREATE TABLE sales_2023_q4 PARTITION OF sales
-FOR VALUES FROM ('2023-10-01') TO ('2024-01-01');
-`,
- },
- {
- name: "extensions_and_advanced_features",
- ddl: `
-CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-
-CREATE TABLE documents (
- id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- tags TEXT[],
- metadata JSONB,
- created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE INDEX idx_documents_metadata ON documents USING GIN (metadata);
-CREATE INDEX idx_documents_tags ON documents USING GIN (tags);
-`,
- },
- {
- name: "check_constraints_and_comments",
- ddl: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- price DECIMAL(10, 2) NOT NULL,
- quantity INTEGER NOT NULL,
- CONSTRAINT chk_price CHECK (price > 0),
- CONSTRAINT chk_quantity CHECK (quantity >= 0)
-);
-
-COMMENT ON TABLE products IS 'Product catalog table';
-COMMENT ON COLUMN products.name IS 'Product name';
-COMMENT ON COLUMN products.price IS 'Product price in USD';
-`,
- },
- {
- name: "materialized_views",
- ddl: `
-CREATE TABLE raw_data (
- id SERIAL PRIMARY KEY,
- data_value NUMERIC NOT NULL,
- category VARCHAR(50) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE MATERIALIZED VIEW data_summary AS
-SELECT category,
- COUNT(*) as count,
- AVG(data_value) as avg_value,
- MAX(data_value) as max_value,
- MIN(data_value) as min_value
-FROM raw_data
-GROUP BY category
-WITH NO DATA;
-
-CREATE INDEX idx_data_summary_category ON data_summary(category);
-`,
- },
- {
- name: "comprehensive_views",
- ddl: `
-CREATE TABLE customers (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(100) UNIQUE,
- active BOOLEAN DEFAULT true,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- customer_id INTEGER REFERENCES customers(id),
- total_amount DECIMAL(10, 2) NOT NULL,
- order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- status VARCHAR(20) DEFAULT 'pending'
-);
-
--- View with public schema references and various formatting
-CREATE VIEW public.active_customer_orders AS
- SELECT
- c.id AS customer_id,
- c.name,
- c.email,
- o.id AS order_id,
- o.total_amount,
- o.order_date
- FROM
- public.customers c
- INNER JOIN public.orders o ON c.id = o.customer_id
- WHERE
- c.active = TRUE
- AND o.status = 'completed';
-
--- Simple view without schema qualification
-CREATE VIEW customer_summary AS
-SELECT
- id,
- name,
- email,
- (SELECT COUNT(*) FROM orders WHERE customer_id = customers.id) as order_count
-FROM customers
-WHERE active = true;
-
--- Materialized view with complex aggregation
-CREATE MATERIALIZED VIEW monthly_sales_summary AS
-SELECT
- DATE_TRUNC('month', order_date) as month,
- COUNT(*) as order_count,
- SUM(total_amount) as total_revenue,
- AVG(total_amount) as avg_order_value,
- COUNT(DISTINCT customer_id) as unique_customers
-FROM public.orders
-WHERE status = 'completed'
-GROUP BY DATE_TRUNC('month', order_date)
-ORDER BY month
-WITH NO DATA;
-
-CREATE UNIQUE INDEX idx_monthly_sales_month ON monthly_sales_summary(month);
-`,
- },
- {
- name: "triggers",
- ddl: `
-CREATE TABLE audit_log (
- id SERIAL PRIMARY KEY,
- table_name VARCHAR(50) NOT NULL,
- operation VARCHAR(10) NOT NULL,
- user_name VARCHAR(50),
- timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE accounts (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- balance DECIMAL(10, 2) DEFAULT 0,
- last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE OR REPLACE FUNCTION update_last_modified()
-RETURNS TRIGGER AS $$
-BEGIN
- NEW.last_modified = CURRENT_TIMESTAMP;
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-
-CREATE TRIGGER trg_update_last_modified
-BEFORE UPDATE ON accounts
-FOR EACH ROW
-EXECUTE FUNCTION update_last_modified();
-
-CREATE OR REPLACE FUNCTION log_account_changes()
-RETURNS TRIGGER AS $$
-BEGIN
- INSERT INTO audit_log (table_name, operation, user_name)
- VALUES ('accounts', TG_OP, current_user);
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-
-CREATE TRIGGER trg_audit_accounts
-AFTER INSERT OR UPDATE OR DELETE ON accounts
-FOR EACH ROW
-EXECUTE FUNCTION log_account_changes();
-`,
- },
- {
- name: "foreign_key_on_delete_cascade",
- ddl: `
-CREATE TABLE categories (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- category_id INTEGER,
- CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
-);
-
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- product_id INTEGER,
- quantity INTEGER DEFAULT 1,
- CONSTRAINT fk_product_cascade FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE SET NULL
-);
-`,
- },
- {
- name: "foreign_key_on_update_actions",
- ddl: `
-CREATE TABLE departments (
- code VARCHAR(10) PRIMARY KEY,
- name VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- dept_code VARCHAR(10),
- manager_id INTEGER,
- CONSTRAINT fk_dept_restrict FOREIGN KEY (dept_code) REFERENCES departments(code) ON UPDATE RESTRICT ON DELETE SET NULL,
- CONSTRAINT fk_manager_cascade FOREIGN KEY (manager_id) REFERENCES employees(id) ON UPDATE CASCADE ON DELETE CASCADE
-);
-
-CREATE TABLE projects (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- lead_emp_id INTEGER,
- dept_code VARCHAR(10),
- CONSTRAINT fk_lead_setnull FOREIGN KEY (lead_emp_id) REFERENCES employees(id) ON DELETE SET NULL ON UPDATE SET NULL,
- CONSTRAINT fk_proj_dept FOREIGN KEY (dept_code) REFERENCES departments(code) ON DELETE RESTRICT ON UPDATE RESTRICT
-);
-`,
- },
- {
- name: "foreign_key_no_action",
- ddl: `
-CREATE TABLE countries (
- id SERIAL PRIMARY KEY,
- code VARCHAR(3) UNIQUE NOT NULL,
- name VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE states (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- country_id INTEGER,
- -- Explicit NO ACTION
- CONSTRAINT fk_country_explicit FOREIGN KEY (country_id) REFERENCES countries(id) ON DELETE NO ACTION ON UPDATE NO ACTION
-);
-
-CREATE TABLE cities (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- state_id INTEGER,
- -- Implicit NO ACTION (default behavior)
- CONSTRAINT fk_state_implicit FOREIGN KEY (state_id) REFERENCES states(id)
-);
-`,
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
-
- // Create first database with original DDL using unique name
- dbNameA := fmt.Sprintf("test_a_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
- _, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameA))
- require.NoError(t, err)
-
- // Create driver for database A
- driverA, err := createPgDriver(ctx, pgContainer.GetHost(), pgContainer.GetPort(), dbNameA)
- require.NoError(t, err)
- defer driverA.Close(ctx)
-
- // Execute the original DDL
- _, err = driverA.GetDB().ExecContext(ctx, tc.ddl)
- require.NoError(t, err)
-
- // Get metadata A using Driver.SyncDBSchema
- metadataA, err := driverA.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Generate database definition using GetDatabaseDefinition
- generatedDDL, err := GetDatabaseDefinition(schema.GetDefinitionContext{PrintHeader: true}, metadataA)
- require.NoError(t, err)
- require.NotEmpty(t, generatedDDL, "generated DDL should not be empty")
-
- // Create second database and apply generated DDL using unique name
- dbNameB := fmt.Sprintf("test_b_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
- _, err = pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameB))
- require.NoError(t, err)
-
- // Create driver for database B
- driverB, err := createPgDriver(ctx, pgContainer.GetHost(), pgContainer.GetPort(), dbNameB)
- require.NoError(t, err)
- defer driverB.Close(ctx)
-
- // Execute the generated DDL
- _, err = driverB.GetDB().ExecContext(ctx, generatedDDL)
- require.NoError(t, err, "failed to execute generated DDL: %s", generatedDDL)
-
- // Get metadata B using Driver.SyncDBSchema
- metadataB, err := driverB.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Compare metadata A and B
- compareFullMetadata(t, metadataA, metadataB)
- })
- }
-}
-
-// compareFullMetadata compares all aspects of database metadata
-func compareFullMetadata(t *testing.T, metaA, metaB *storepb.DatabaseSchemaMetadata) {
- // Compare extensions
- compareExtensionsDef(t, metaA.Extensions, metaB.Extensions)
-
- // Compare schemas
- require.Equal(t, len(metaA.Schemas), len(metaB.Schemas), "number of schemas should match")
-
- // Find the public schema in both
- var schemaA, schemaB *storepb.SchemaMetadata
- for _, schema := range metaA.Schemas {
- if schema.Name == "public" {
- schemaA = schema
- break
- }
- }
- for _, schema := range metaB.Schemas {
- if schema.Name == "public" {
- schemaB = schema
- break
- }
- }
-
- require.NotNil(t, schemaA, "metadata A should have public schema")
- require.NotNil(t, schemaB, "metadata B should have public schema")
-
- // Compare schema contents
- compareSchemaContents(t, schemaA, schemaB)
-}
-
-// compareSchemaContents compares the contents of two schemas
-func compareSchemaContents(t *testing.T, schemaA, schemaB *storepb.SchemaMetadata) {
- // Compare enums
- compareEnumsDef(t, schemaA.EnumTypes, schemaB.EnumTypes)
-
- // Compare composite types
- compareCompositeTypesDef(t, schemaA.CompositeTypes, schemaB.CompositeTypes)
-
- // Compare sequences (excluding implicit sequences from SERIAL columns)
- compareExplicitSequences(t, schemaA.Sequences, schemaB.Sequences)
-
- // Compare tables
- compareTablesDef(t, schemaA.Tables, schemaB.Tables)
-
- // Compare views
- compareViewsDef(t, schemaA.Views, schemaB.Views)
-
- // Compare materialized views
- compareMaterializedViewsDef(t, schemaA.MaterializedViews, schemaB.MaterializedViews)
-
- // Compare functions
- compareFunctionsDef(t, schemaA.Functions, schemaB.Functions)
-}
-
-// compareExplicitSequences compares only explicitly created sequences
-func compareExplicitSequences(t *testing.T, seqsA, seqsB []*storepb.SequenceMetadata) {
- // Filter out implicit sequences (those owned by columns)
- explicitA := filterExplicitSequences(seqsA)
- explicitB := filterExplicitSequences(seqsB)
-
- require.Equal(t, len(explicitA), len(explicitB), "number of explicit sequences should match")
-
- // Create maps for comparison
- mapA := make(map[string]*storepb.SequenceMetadata)
- for _, seq := range explicitA {
- mapA[seq.Name] = seq
- }
-
- for _, seqB := range explicitB {
- seqA, exists := mapA[seqB.Name]
- require.True(t, exists, "sequence %s should exist in metadata A", seqB.Name)
-
- // Compare sequence properties
- require.Equal(t, seqA.Start, seqB.Start, "sequence %s: start value should match", seqB.Name)
- require.Equal(t, seqA.Increment, seqB.Increment, "sequence %s: increment should match", seqB.Name)
- require.Equal(t, seqA.MinValue, seqB.MinValue, "sequence %s: min value should match", seqB.Name)
- require.Equal(t, seqA.MaxValue, seqB.MaxValue, "sequence %s: max value should match", seqB.Name)
- require.Equal(t, seqA.Cycle, seqB.Cycle, "sequence %s: cycle should match", seqB.Name)
- require.Equal(t, seqA.Comment, seqB.Comment, "sequence %s: comment should match", seqB.Name)
- }
-}
-
-// filterExplicitSequences returns only sequences that are not owned by columns
-func filterExplicitSequences(sequences []*storepb.SequenceMetadata) []*storepb.SequenceMetadata {
- var result []*storepb.SequenceMetadata
- for _, seq := range sequences {
- if seq.OwnerTable == "" || seq.OwnerColumn == "" {
- result = append(result, seq)
- }
- }
- return result
-}
-
-// compareMaterializedViewsDef compares materialized views between two schemas using PostgreSQL view comparer
-func compareMaterializedViewsDef(t *testing.T, viewsA, viewsB []*storepb.MaterializedViewMetadata) {
- require.Equal(t, len(viewsA), len(viewsB), "number of materialized views should match")
-
- // Create maps for comparison
- mapA := make(map[string]*storepb.MaterializedViewMetadata)
- for _, view := range viewsA {
- mapA[view.Name] = view
- }
-
- for _, viewB := range viewsB {
- viewA, exists := mapA[viewB.Name]
- require.True(t, exists, "materialized view %s should exist in metadata A", viewB.Name)
-
- // Compare view definitions using normalized string comparison
- defA := normalizeSQLDef(viewA.Definition)
- defB := normalizeSQLDef(viewB.Definition)
- definitionsEqual := defA == defB
-
- // Assert that materialized view definitions must be equal after sophisticated PostgreSQL parsing
- require.True(t, definitionsEqual,
- "materialized view %s: definitions must be equal using PostgreSQL view comparer (A: %q, B: %q)",
- viewB.Name, viewA.Definition, viewB.Definition)
-
- require.Equal(t, viewA.Comment, viewB.Comment,
- "materialized view %s: comment should match", viewB.Name)
-
- // Compare indexes on materialized views
- compareIndexesDef(t, viewB.Name, viewA.Indexes, viewB.Indexes)
- }
-}
-
-// normalizeSQLDef normalizes SQL for comparison
-func normalizeSQLDef(sql string) string {
- // Convert to lowercase
- sql = strings.ToLower(sql)
-
- // Replace multiple spaces/newlines with single space
- sql = strings.Join(strings.Fields(sql), " ")
-
- // Remove trailing semicolons
- sql = strings.TrimSuffix(sql, ";")
-
- // Remove schema qualifiers for public schema
- sql = strings.ReplaceAll(sql, "public.", "")
-
- // Final trim
- sql = strings.TrimSpace(sql)
-
- return sql
-}
-
-// normalizeExprDef normalizes an expression for comparison
-func normalizeExprDef(expr string) string {
- // Convert to lowercase
- expr = strings.ToLower(expr)
-
- // Replace multiple spaces with single space
- expr = strings.Join(strings.Fields(expr), " ")
-
- // Remove spaces around parentheses
- expr = strings.ReplaceAll(expr, " (", "(")
- expr = strings.ReplaceAll(expr, "( ", "(")
- expr = strings.ReplaceAll(expr, " )", ")")
- expr = strings.ReplaceAll(expr, ") ", ")")
-
- // Normalize quotes around identifiers
- expr = strings.ReplaceAll(expr, "'", "")
-
- return expr
-}
-
-// compareTablesDef compares tables between two schemas
-func compareTablesDef(t *testing.T, tablesA, tablesB []*storepb.TableMetadata) {
- require.Equal(t, len(tablesA), len(tablesB), "number of tables should match")
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.TableMetadata)
- for _, table := range tablesA {
- mapA[table.Name] = table
- }
-
- mapB := make(map[string]*storepb.TableMetadata)
- for _, table := range tablesB {
- mapB[table.Name] = table
- }
-
- // Compare each table
- for name, tableA := range mapA {
- tableB, exists := mapB[name]
- require.True(t, exists, "table %s should exist in metadata B", name)
-
- // Compare columns
- compareColumnsDef(t, name, tableA.Columns, tableB.Columns)
-
- // Compare indexes
- compareIndexesDef(t, name, tableA.Indexes, tableB.Indexes)
-
- // Compare foreign keys
- compareForeignKeysDef(t, name, tableA.ForeignKeys, tableB.ForeignKeys)
-
- // Compare check constraints
- compareCheckConstraintsDef(t, name, tableA.CheckConstraints, tableB.CheckConstraints)
-
- // Compare partitions
- comparePartitionsDef(t, name, tableA.Partitions, tableB.Partitions)
-
- // Compare triggers
- compareTriggersDef(t, name, tableA.Triggers, tableB.Triggers)
-
- // Compare comments
- require.Equal(t, tableA.Comment, tableB.Comment, "table %s: comment should match", name)
- }
-}
-
-// compareColumnsDef compares columns between two tables
-func compareColumnsDef(t *testing.T, tableName string, colsA, colsB []*storepb.ColumnMetadata) {
- require.Equal(t, len(colsA), len(colsB), "table %s: number of columns should match", tableName)
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.ColumnMetadata)
- for _, col := range colsA {
- mapA[col.Name] = col
- }
-
- for _, colB := range colsB {
- colA, exists := mapA[colB.Name]
- require.True(t, exists, "table %s: column %s should exist in metadata A", tableName, colB.Name)
-
- // Compare column properties
- require.Equal(t, colA.Type, colB.Type, "table %s, column %s: type should match", tableName, colB.Name)
- require.Equal(t, colA.Nullable, colB.Nullable, "table %s, column %s: nullable should match", tableName, colB.Name)
- require.Equal(t, colA.Comment, colB.Comment, "table %s, column %s: comment should match", tableName, colB.Name)
-
- // Compare default values if both exist
- hasDefaultA := colA.Default != ""
- hasDefaultB := colB.Default != ""
- if hasDefaultA && hasDefaultB {
- // Default values might be represented differently, so we just check they exist
- t.Logf("table %s, column %s: default values exist in both", tableName, colB.Name)
- }
- }
-}
-
-// compareIndexesDef compares indexes between two tables
-func compareIndexesDef(t *testing.T, tableName string, indexesA, indexesB []*storepb.IndexMetadata) {
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.IndexMetadata)
- for _, idx := range indexesA {
- mapA[idx.Name] = idx
- }
-
- mapB := make(map[string]*storepb.IndexMetadata)
- for _, idx := range indexesB {
- mapB[idx.Name] = idx
- }
-
- // Compare common indexes
- for name, idxB := range mapB {
- idxA, exists := mapA[name]
- if !exists {
- // Some indexes might be system-generated
- t.Logf("table %s: index %s exists in B but not in A (might be implicit)", tableName, name)
- continue
- }
-
- require.Equal(t, idxA.Primary, idxB.Primary, "table %s, index %s: primary should match", tableName, name)
- require.Equal(t, idxA.Unique, idxB.Unique, "table %s, index %s: unique should match", tableName, name)
-
- // Compare expressions
- if len(idxA.Expressions) == len(idxB.Expressions) {
- for i := range idxA.Expressions {
- exprA := normalizeExprDef(idxA.Expressions[i])
- exprB := normalizeExprDef(idxB.Expressions[i])
- require.Equal(t, exprA, exprB, "table %s, index %s: expression[%d] should match", tableName, name, i)
- }
- }
-
- // Compare IsConstraint field
- require.Equal(t, idxA.IsConstraint, idxB.IsConstraint, "table %s, index %s: IsConstraint should match", tableName, name)
-
- // Compare comment
- require.Equal(t, idxA.Comment, idxB.Comment, "table %s, index %s: comment should match", tableName, name)
- }
-}
-
-// compareForeignKeysDef compares foreign keys between two tables
-func compareForeignKeysDef(t *testing.T, tableName string, fksA, fksB []*storepb.ForeignKeyMetadata) {
- require.Equal(t, len(fksA), len(fksB), "table %s: number of foreign keys should match", tableName)
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.ForeignKeyMetadata)
- for _, fk := range fksA {
- mapA[fk.Name] = fk
- }
-
- for _, fkB := range fksB {
- fkA, exists := mapA[fkB.Name]
- require.True(t, exists, "table %s: foreign key %s should exist in metadata A", tableName, fkB.Name)
-
- require.ElementsMatch(t, fkA.Columns, fkB.Columns, "table %s, FK %s: columns should match", tableName, fkB.Name)
- require.Equal(t, fkA.ReferencedTable, fkB.ReferencedTable, "table %s, FK %s: referenced table should match", tableName, fkB.Name)
- require.ElementsMatch(t, fkA.ReferencedColumns, fkB.ReferencedColumns, "table %s, FK %s: referenced columns should match", tableName, fkB.Name)
-
- // Compare ON DELETE and ON UPDATE actions
- require.Equal(t, fkA.OnDelete, fkB.OnDelete, "table %s, FK %s: ON DELETE action should match", tableName, fkB.Name)
- require.Equal(t, fkA.OnUpdate, fkB.OnUpdate, "table %s, FK %s: ON UPDATE action should match", tableName, fkB.Name)
- }
-}
-
-// compareCheckConstraintsDef compares check constraints between two tables
-func compareCheckConstraintsDef(t *testing.T, tableName string, checksA, checksB []*storepb.CheckConstraintMetadata) {
- require.Equal(t, len(checksA), len(checksB), "table %s: number of check constraints should match", tableName)
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.CheckConstraintMetadata)
- for _, check := range checksA {
- mapA[check.Name] = check
- }
-
- for _, checkB := range checksB {
- checkA, exists := mapA[checkB.Name]
- require.True(t, exists, "table %s: check constraint %s should exist in metadata A", tableName, checkB.Name)
-
- // Normalize and compare expressions
- exprA := normalizeExprDef(checkA.Expression)
- exprB := normalizeExprDef(checkB.Expression)
- require.Equal(t, exprA, exprB, "table %s, check %s: expression should match", tableName, checkB.Name)
- }
-}
-
-// comparePartitionsDef compares partitions between two tables
-func comparePartitionsDef(t *testing.T, tableName string, partsA, partsB []*storepb.TablePartitionMetadata) {
- require.Equal(t, len(partsA), len(partsB), "table %s: number of partitions should match", tableName)
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.TablePartitionMetadata)
- for _, part := range partsA {
- mapA[part.Name] = part
- }
-
- for _, partB := range partsB {
- partA, exists := mapA[partB.Name]
- require.True(t, exists, "table %s: partition %s should exist in metadata A", tableName, partB.Name)
- require.Equal(t, partA.Expression, partB.Expression, "table %s, partition %s: expression should match", tableName, partB.Name)
- require.Equal(t, partA.Value, partB.Value, "table %s, partition %s: value should match", tableName, partB.Name)
- }
-}
-
-// compareTriggersDef compares triggers between two tables
-func compareTriggersDef(t *testing.T, tableName string, triggersA, triggersB []*storepb.TriggerMetadata) {
- require.Equal(t, len(triggersA), len(triggersB), "table %s: number of triggers should match", tableName)
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.TriggerMetadata)
- for _, trigger := range triggersA {
- mapA[trigger.Name] = trigger
- }
-
- for _, triggerB := range triggersB {
- triggerA, exists := mapA[triggerB.Name]
- require.True(t, exists, "table %s: trigger %s should exist in metadata A", tableName, triggerB.Name)
-
- // Compare trigger properties
- require.Equal(t, normalizeSQLDef(triggerA.Body), normalizeSQLDef(triggerB.Body),
- "table %s, trigger %s: body should match", tableName, triggerB.Name)
- require.Equal(t, triggerA.Comment, triggerB.Comment,
- "table %s, trigger %s: comment should match", tableName, triggerB.Name)
- }
-}
-
-// compareViewsDef compares views between two schemas using PostgreSQL view comparer
-func compareViewsDef(t *testing.T, viewsA, viewsB []*storepb.ViewMetadata) {
- require.Equal(t, len(viewsA), len(viewsB), "number of views should match")
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.ViewMetadata)
- for _, view := range viewsA {
- mapA[view.Name] = view
- }
-
- for _, viewB := range viewsB {
- viewA, exists := mapA[viewB.Name]
- require.True(t, exists, "view %s should exist in metadata A", viewB.Name)
-
- // Compare view definitions using normalized string comparison
- require.Equal(t, normalizeSQLDef(viewA.Definition), normalizeSQLDef(viewB.Definition),
- "view %s: definitions should match", viewB.Name)
-
- // Compare comment
- require.Equal(t, viewA.Comment, viewB.Comment, "view %s: comment should match", viewB.Name)
- }
-}
-
-// compareFunctionsDef compares functions between two schemas
-func compareFunctionsDef(t *testing.T, funcsA, funcsB []*storepb.FunctionMetadata) {
- require.Equal(t, len(funcsA), len(funcsB), "number of functions should match")
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.FunctionMetadata)
- for _, fn := range funcsA {
- mapA[fn.Name] = fn
- }
-
- for _, fnB := range funcsB {
- fnA, exists := mapA[fnB.Name]
- require.True(t, exists, "function %s should exist in metadata A", fnB.Name)
-
- // Compare function definitions
- defA := normalizeSQLDef(fnA.Definition)
- defB := normalizeSQLDef(fnB.Definition)
- require.Equal(t, defA, defB, "function %s: definition should match", fnB.Name)
-
- // Compare comment
- require.Equal(t, fnA.Comment, fnB.Comment, "function %s: comment should match", fnB.Name)
- }
-}
-
-// compareEnumsDef compares enum types between two schemas
-func compareEnumsDef(t *testing.T, enumsA, enumsB []*storepb.EnumTypeMetadata) {
- require.Equal(t, len(enumsA), len(enumsB), "number of enums should match")
-
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.EnumTypeMetadata)
- for _, enum := range enumsA {
- mapA[enum.Name] = enum
- }
-
- for _, enumB := range enumsB {
- enumA, exists := mapA[enumB.Name]
- require.True(t, exists, "enum %s should exist in metadata A", enumB.Name)
- require.ElementsMatch(t, enumA.Values, enumB.Values, "enum %s: values should match", enumB.Name)
- require.Equal(t, enumA.Comment, enumB.Comment, "enum %s: comment should match", enumB.Name)
- }
-}
-
-func compareCompositeTypesDef(t *testing.T, compositesA, compositesB []*storepb.CompositeTypeMetadata) {
- require.Equal(t, len(compositesA), len(compositesB), "number of composite types should match")
-
- mapA := make(map[string]*storepb.CompositeTypeMetadata)
- for _, composite := range compositesA {
- mapA[composite.Name] = composite
- }
-
- for _, compositeB := range compositesB {
- compositeA, exists := mapA[compositeB.Name]
- require.True(t, exists, "composite type %s should exist in metadata A", compositeB.Name)
- require.Equal(t, compositeA.Comment, compositeB.Comment, "composite type %s: comment should match", compositeB.Name)
- require.Equal(t, len(compositeA.Attributes), len(compositeB.Attributes), "composite type %s: attribute count should match", compositeB.Name)
- for i, attributeA := range compositeA.Attributes {
- attributeB := compositeB.Attributes[i]
- require.Equal(t, attributeA.Name, attributeB.Name, "composite type %s: attribute %d name should match", compositeB.Name, i)
- require.Equal(t, attributeA.Type, attributeB.Type, "composite type %s: attribute %s type should match", compositeB.Name, attributeA.Name)
- require.Equal(t, attributeA.Collation, attributeB.Collation, "composite type %s: attribute %s collation should match", compositeB.Name, attributeA.Name)
- require.Equal(t, attributeA.Comment, attributeB.Comment, "composite type %s: attribute %s comment should match", compositeB.Name, attributeA.Name)
- }
- }
-}
-
-// compareExtensionsDef compares extensions between two databases
-func compareExtensionsDef(t *testing.T, extsA, extsB []*storepb.ExtensionMetadata) {
- // Create maps for easier comparison
- mapA := make(map[string]*storepb.ExtensionMetadata)
- for _, ext := range extsA {
- mapA[ext.Name] = ext
- }
-
- for _, extB := range extsB {
- extA, exists := mapA[extB.Name]
- require.True(t, exists, "extension %s should exist in metadata A", extB.Name)
- require.Equal(t, extA.Schema, extB.Schema, "extension %s: schema should match", extB.Name)
- require.Equal(t, extA.Version, extB.Version, "extension %s: version should match", extB.Name)
- }
-}
diff --git a/backend/plugin/schema/pg/is_definition_procedure_test.go b/backend/plugin/schema/pg/is_definition_procedure_test.go
deleted file mode 100644
index 21661aa3b5..0000000000
--- a/backend/plugin/schema/pg/is_definition_procedure_test.go
+++ /dev/null
@@ -1,205 +0,0 @@
-package pg
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
-)
-
-func TestIsDefinitionProcedure(t *testing.T) {
- tests := []struct {
- name string
- definition string
- isProcedure bool
- }{
- {
- name: "Simple FUNCTION",
- definition: `CREATE FUNCTION test_function()
-RETURNS void
-LANGUAGE plpgsql
-AS $$
-BEGIN
- NULL;
-END;
-$$;`,
- isProcedure: false,
- },
- {
- name: "Simple PROCEDURE",
- definition: `CREATE PROCEDURE test_procedure()
-LANGUAGE plpgsql
-AS $$
-BEGIN
- NULL;
-END;
-$$;`,
- isProcedure: true,
- },
- {
- name: "CREATE OR REPLACE FUNCTION",
- definition: `CREATE OR REPLACE FUNCTION test_function()
-RETURNS void
-LANGUAGE plpgsql
-AS $$
-BEGIN
- NULL;
-END;
-$$;`,
- isProcedure: false,
- },
- {
- name: "CREATE OR REPLACE PROCEDURE",
- definition: `CREATE OR REPLACE PROCEDURE test_procedure()
-LANGUAGE plpgsql
-AS $$
-BEGIN
- NULL;
-END;
-$$;`,
- isProcedure: true,
- },
- {
- name: "FUNCTION with word 'procedure' in comment - should NOT be confused",
- definition: `CREATE FUNCTION test_function()
-RETURNS void
-LANGUAGE plpgsql
-AS $$
-BEGIN
- -- This is not a procedure, it's a function
- -- The word PROCEDURE appears in this comment
- RAISE NOTICE 'This function is not a procedure';
-END;
-$$;`,
- isProcedure: false,
- },
- {
- name: "FUNCTION with word 'procedure' in string literal - should NOT be confused",
- definition: `CREATE FUNCTION test_function()
-RETURNS text
-LANGUAGE plpgsql
-AS $$
-BEGIN
- RETURN 'This function is not a PROCEDURE';
-END;
-$$;`,
- isProcedure: false,
- },
- {
- name: "PROCEDURE with complex body",
- definition: `CREATE OR REPLACE PROCEDURE update_user_data(
- user_id INTEGER,
- new_name VARCHAR
-)
-LANGUAGE plpgsql
-AS $$
-DECLARE
- old_name VARCHAR;
-BEGIN
- -- Get old name
- SELECT name INTO old_name FROM users WHERE id = user_id;
-
- -- Update the user name
- UPDATE users SET name = new_name WHERE id = user_id;
-
- -- Log the change
- INSERT INTO audit_log (message) VALUES (
- 'Changed user name from ' || old_name || ' to ' || new_name
- );
-END;
-$$;`,
- isProcedure: true,
- },
- {
- name: "FUNCTION with RETURNS TABLE",
- definition: `CREATE OR REPLACE FUNCTION get_users()
-RETURNS TABLE(id INTEGER, name VARCHAR)
-LANGUAGE plpgsql
-AS $$
-BEGIN
- RETURN QUERY SELECT users.id, users.name FROM users;
-END;
-$$;`,
- isProcedure: false,
- },
- {
- name: "Empty definition",
- definition: "",
- isProcedure: false,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := isDefinitionProcedure(tt.definition)
- assert.Equal(t, tt.isProcedure, result,
- "isDefinitionProcedure returned %v, expected %v for:\n%s",
- result, tt.isProcedure, tt.definition)
- })
- }
-}
-
-// TestIsDefinitionProcedureRobustness tests edge cases that would fail with string-based detection
-func TestIsDefinitionProcedureRobustness(t *testing.T) {
- tests := []struct {
- name string
- definition string
- isProcedure bool
- reason string
- }{
- {
- name: "FUNCTION with 'CREATE PROCEDURE' in string literal",
- definition: `CREATE FUNCTION test_function()
-RETURNS text
-LANGUAGE sql
-AS $outer$
- SELECT 'Example: CREATE PROCEDURE foo() LANGUAGE plpgsql AS $body$ BEGIN NULL; END; $body$;'
-$outer$;`,
- isProcedure: false,
- reason: "String-based detection might incorrectly identify this as a PROCEDURE due to 'CREATE PROCEDURE' in the string literal",
- },
- {
- name: "FUNCTION with 'PROCEDURE' in multi-line comment",
- definition: `CREATE FUNCTION complex_function()
-RETURNS void
-LANGUAGE plpgsql
-AS $$
-BEGIN
- /*
- * This function performs complex operations
- * Note: This is NOT a PROCEDURE
- * PROCEDURE keyword appears in this comment
- * But it should still be detected as a FUNCTION
- */
- RAISE NOTICE 'Processing...';
-END;
-$$;`,
- isProcedure: false,
- reason: "Multi-line comments with PROCEDURE keyword should not confuse the detector",
- },
- {
- name: "PROCEDURE with unusual formatting",
- definition: `CREATE OR REPLACE PROCEDURE
-test_procedure
-(
-)
-LANGUAGE plpgsql
-AS
-$$
-BEGIN
- NULL;
-END;
-$$;`,
- isProcedure: true,
- reason: "Should handle unusual whitespace and formatting",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := isDefinitionProcedure(tt.definition)
- assert.Equal(t, tt.isProcedure, result,
- "Test failed: %s\nGot: %v, Expected: %v",
- tt.reason, result, tt.isProcedure)
- })
- }
-}
diff --git a/backend/plugin/schema/pg/metadata_migration_test.go b/backend/plugin/schema/pg/metadata_migration_test.go
index 0fdcb4d3e8..8e5d7debd8 100644
--- a/backend/plugin/schema/pg/metadata_migration_test.go
+++ b/backend/plugin/schema/pg/metadata_migration_test.go
@@ -1613,3 +1613,1569 @@ func newDatabaseMetadataWithEngine(engine storepb.Engine, tables []*storepb.Tabl
},
}, nil, nil, engine, true)
}
+
+func newPGDatabaseMetadataWithCompositeTypes(compositeTypes []*storepb.CompositeTypeMetadata) *model.DatabaseMetadata {
+ return model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ CompositeTypes: compositeTypes,
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+}
+
+func TestPGMetadataDiffCreateCompositeTypesInDependencyOrder(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes(nil)
+ // aa_nested sorts before zz_base alphabetically but depends on it.
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "aa_nested",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "home", Type: "public.zz_base"},
+ },
+ },
+ {
+ Name: "zz_base",
+ Comment: "base type",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text", Collation: `"C"`, Comment: "street line"},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Contains(t, sql, `CREATE TYPE "public"."zz_base" AS (`)
+ require.Contains(t, sql, `"street" text COLLATE "C"`)
+ require.Contains(t, sql, `CREATE TYPE "public"."aa_nested" AS (`)
+ require.Contains(t, sql, `COMMENT ON TYPE "public"."zz_base" IS 'base type';`)
+ require.Contains(t, sql, `COMMENT ON COLUMN "public"."zz_base"."street" IS 'street line';`)
+ require.Less(t,
+ strings.Index(sql, `CREATE TYPE "public"."zz_base"`),
+ strings.Index(sql, `CREATE TYPE "public"."aa_nested"`),
+ "referenced composite must be created before its dependent")
+}
+
+func TestPGMetadataDiffDropCompositeTypesInReverseDependencyOrder(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "zz_base",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ {
+ Name: "aa_nested",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "home", Type: "public.zz_base"},
+ },
+ },
+ })
+ target := newPGDatabaseMetadataWithCompositeTypes(nil)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Contains(t, sql, `DROP TYPE "public"."aa_nested";`)
+ require.Contains(t, sql, `DROP TYPE "public"."zz_base";`)
+ require.Less(t,
+ strings.Index(sql, `DROP TYPE "public"."aa_nested"`),
+ strings.Index(sql, `DROP TYPE "public"."zz_base"`),
+ "dependent composite must be dropped before the composite it references")
+}
+
+func TestPGMetadataDiffCompositeDropsPrecedeEnumDrops(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "status", Values: []string{"a", "b"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "uses_status",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "s", Type: "public.status"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := newPGDatabaseMetadataWithCompositeTypes(nil)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Less(t,
+ strings.Index(sql, `DROP TYPE "public"."uses_status"`),
+ strings.Index(sql, `DROP TYPE "public"."status"`),
+ "composite referencing an enum must be dropped before the enum")
+}
+
+func TestPGMetadataDiffAlterCompositeTypeAttributes(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ {Name: "city", Type: "character varying(50)"},
+ {Name: "dropped_attr", Type: "integer"},
+ },
+ },
+ })
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ {Name: "city", Type: "character varying(100)"},
+ {Name: "zip", Type: "text", Collation: `"C"`},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Contains(t, sql, `ALTER TYPE "public"."addr" DROP ATTRIBUTE "dropped_attr";`)
+ require.Contains(t, sql, `ALTER TYPE "public"."addr" ADD ATTRIBUTE "zip" text COLLATE "C";`)
+ require.Contains(t, sql, `ALTER TYPE "public"."addr" ALTER ATTRIBUTE "city" TYPE character varying(100);`)
+ require.NotContains(t, sql, `"street"`, "unchanged attribute must not produce DDL")
+}
+
+func TestPGMetadataDiffCompositeTypeReorderOnlyWarns(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "a", Type: "text"},
+ {Name: "b", Type: "integer"},
+ },
+ },
+ })
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "b", Type: "integer"},
+ {Name: "a", Type: "text"},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Contains(t, sql, `-- WARNING: PostgreSQL does not support reordering the attributes of "public"."addr"`)
+ require.NotContains(t, sql, "ALTER TYPE", "reorder-only change must not produce attribute DDL")
+ require.NotContains(t, sql, "DROP TYPE")
+}
+
+func TestPGMetadataDiffCompositeTypeCommentChanges(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Comment: "old type comment",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text", Comment: "old attr comment"},
+ },
+ },
+ })
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Comment: "new type comment",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text", Comment: "new attr comment"},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Contains(t, sql, `COMMENT ON TYPE "public"."addr" IS 'new type comment';`)
+ require.Contains(t, sql, `COMMENT ON COLUMN "public"."addr"."street" IS 'new attr comment';`)
+ require.NotContains(t, sql, "ALTER TYPE", "comment-only change must not produce attribute DDL")
+}
+
+func TestPGMetadataDiffIdenticalCompositeTypesProduceNoMigration(t *testing.T) {
+ composites := []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Comment: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text", Collation: `"C"`, Comment: "s"},
+ },
+ },
+ }
+ source := newPGDatabaseMetadataWithCompositeTypes(composites)
+ target := newPGDatabaseMetadataWithCompositeTypes(composites)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Empty(t, sql)
+}
+
+func TestPGMetadataDiffCompositeDropDeferredAfterColumnRetype(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "public.addr", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ dropIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
+ alterIdx := strings.Index(sql, `ALTER COLUMN "c"`)
+ require.GreaterOrEqual(t, dropIdx, 0, "composite type must still be dropped: %s", sql)
+ require.GreaterOrEqual(t, alterIdx, 0, "column must be retyped: %s", sql)
+ require.Greater(t, dropIdx, alterIdx,
+ "composite drop must run after the column retype that releases it: %s", sql)
+}
+
+func TestPGMetadataDiffSchemaDropIncludesCompositeTypes(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ {
+ Name: "s",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ dropTypeIdx := strings.Index(sql, `DROP TYPE "s"."addr"`)
+ dropSchemaIdx := strings.Index(sql, `DROP SCHEMA`)
+ require.GreaterOrEqual(t, dropTypeIdx, 0, "schema drop must drop its composite types first: %s", sql)
+ require.GreaterOrEqual(t, dropSchemaIdx, 0)
+ require.Less(t, dropTypeIdx, dropSchemaIdx, "composite type drop must precede DROP SCHEMA: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeTypeMiddleInsertWarns(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "a", Type: "text"},
+ {Name: "b", Type: "text"},
+ },
+ },
+ })
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "a", Type: "text"},
+ {Name: "x", Type: "text"},
+ {Name: "b", Type: "text"},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Contains(t, sql, `ALTER TYPE "public"."addr" ADD ATTRIBUTE "x" text;`)
+ require.Contains(t, sql, `-- WARNING: PostgreSQL does not support reordering the attributes of "public"."addr"`,
+ "middle insertion is not achievable via ADD ATTRIBUTE and must warn: %s", sql)
+}
+
+func TestPGMetadataDiffDeferredCompositeDefersReferencedEnumDrop(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "public.addr", Nullable: true},
+ },
+ },
+ },
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "status", Values: []string{"a", "b"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "s", Type: "public.status"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
+ compositeIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
+ enumIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
+ require.GreaterOrEqual(t, retypeIdx, 0, "column must be retyped: %s", sql)
+ require.GreaterOrEqual(t, compositeIdx, 0, "composite must be dropped: %s", sql)
+ require.GreaterOrEqual(t, enumIdx, 0, "enum must be dropped: %s", sql)
+ require.Greater(t, compositeIdx, retypeIdx, "deferred composite drop must follow the retype: %s", sql)
+ require.Greater(t, enumIdx, compositeIdx, "enum referenced by the deferred composite must drop after it: %s", sql)
+}
+
+func TestPGMetadataDiffSchemaDropDeferredWhenCompositeReleasedByRetype(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "s.addr", Nullable: true},
+ },
+ },
+ },
+ },
+ {
+ Name: "s",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
+ dropTypeIdx := strings.Index(sql, `DROP TYPE "s"."addr"`)
+ dropSchemaIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "s"`)
+ require.GreaterOrEqual(t, retypeIdx, 0, "column must be retyped: %s", sql)
+ require.GreaterOrEqual(t, dropTypeIdx, 0, "composite in dropped schema must be dropped: %s", sql)
+ require.GreaterOrEqual(t, dropSchemaIdx, 0, "schema must be dropped: %s", sql)
+ require.Greater(t, dropTypeIdx, retypeIdx, "schema-owned composite drop must follow the retype releasing it: %s", sql)
+ require.Greater(t, dropSchemaIdx, dropTypeIdx, "DROP SCHEMA must follow its composite drop: %s", sql)
+}
+
+func TestPGMetadataDiffSchemaDropDeferredWhenDeferredCompositeReferencesItsEnum(t *testing.T) {
+ // public.addr is deferred (public.t.c retyped away from it) and references
+ // s.status; schema s (containing only that enum) must defer its drop too.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "public.addr", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "st", Type: "s.status"},
+ },
+ },
+ },
+ },
+ {
+ Name: "s",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "status", Values: []string{"a"}},
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ compositeIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
+ enumIdx := strings.Index(sql, `DROP TYPE "s"."status"`)
+ schemaIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "s"`)
+ require.GreaterOrEqual(t, compositeIdx, 0, "deferred composite must be dropped: %s", sql)
+ require.GreaterOrEqual(t, enumIdx, 0, "enum in dropped schema must be dropped: %s", sql)
+ require.GreaterOrEqual(t, schemaIdx, 0, "schema must be dropped: %s", sql)
+ require.Greater(t, enumIdx, compositeIdx,
+ "enum referenced by the deferred composite must drop after it, even inside a schema drop: %s", sql)
+}
+
+func TestPGMetadataDiffDeferredSchemaCompositeDefersReferencedEnumDrop(t *testing.T) {
+ // Schema s is deferred (public.t.c retyped away from s.addr); s.addr
+ // references public.status, a top-level dropped enum, which therefore
+ // must also defer until after the schema drop removes s.addr.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "s.addr", Nullable: true},
+ },
+ },
+ },
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "status", Values: []string{"a"}},
+ },
+ },
+ {
+ Name: "s",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "st", Type: "public.status"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
+ schemaCompositeIdx := strings.Index(sql, `DROP TYPE "s"."addr"`)
+ enumIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
+ require.GreaterOrEqual(t, retypeIdx, 0, "column must be retyped: %s", sql)
+ require.GreaterOrEqual(t, schemaCompositeIdx, 0, "deferred schema's composite must be dropped: %s", sql)
+ require.GreaterOrEqual(t, enumIdx, 0, "enum must be dropped: %s", sql)
+ require.Greater(t, schemaCompositeIdx, retypeIdx, "schema-owned composite drop must follow the retype: %s", sql)
+ require.Greater(t, enumIdx, schemaCompositeIdx,
+ "enum referenced from the deferred schema's composite must drop last: %s", sql)
+}
+
+func TestPGMetadataDiffCyclicDeferredSchemaDropsGlobalizeTypeDrops(t *testing.T) {
+ // Schemas a and b reference each other: a.ca uses b.eb, b.cb uses a.ea.
+ // Both are deferred (a column retype releases a.ca). Globalized type
+ // drops must remove both composites before either enum, regardless of
+ // the schema-level cycle.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "a.ca", Nullable: true},
+ },
+ },
+ },
+ },
+ {
+ Name: "a",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "ea", Values: []string{"x"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "ca",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "v", Type: "b.eb"},
+ },
+ },
+ },
+ },
+ {
+ Name: "b",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "eb", Values: []string{"y"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "cb",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "v", Type: "a.ea"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ caIdx := strings.Index(sql, `DROP TYPE "a"."ca"`)
+ cbIdx := strings.Index(sql, `DROP TYPE "b"."cb"`)
+ eaIdx := strings.Index(sql, `DROP TYPE "a"."ea"`)
+ ebIdx := strings.Index(sql, `DROP TYPE "b"."eb"`)
+ require.GreaterOrEqual(t, caIdx, 0, "a.ca must be dropped: %s", sql)
+ require.GreaterOrEqual(t, cbIdx, 0, "b.cb must be dropped: %s", sql)
+ require.GreaterOrEqual(t, eaIdx, 0, "a.ea must be dropped: %s", sql)
+ require.GreaterOrEqual(t, ebIdx, 0, "b.eb must be dropped: %s", sql)
+ require.Greater(t, eaIdx, cbIdx, "a.ea must drop after b.cb which references it: %s", sql)
+ require.Greater(t, ebIdx, caIdx, "b.eb must drop after a.ca which references it: %s", sql)
+ dropSchemaAIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "a"`)
+ dropSchemaBIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "b"`)
+ require.Greater(t, dropSchemaAIdx, eaIdx, "DROP SCHEMA a must come after its types: %s", sql)
+ require.Greater(t, dropSchemaBIdx, ebIdx, "DROP SCHEMA b must come after its types: %s", sql)
+}
+
+func TestPGMetadataDiffImmediateSchemaDropOrdersTypesGlobally(t *testing.T) {
+ // Dropping schema s (whose composite references top-level types) together
+ // with those top-level types: s.child must drop before public.base and
+ // public.status regardless of the schema-drop boundary.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "status", Values: []string{"a"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "base",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ },
+ },
+ {
+ Name: "s",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "child",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "b", Type: "public.base"},
+ {Name: "st", Type: "public.status"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ childIdx := strings.Index(sql, `DROP TYPE "s"."child"`)
+ baseIdx := strings.Index(sql, `DROP TYPE "public"."base"`)
+ statusIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
+ schemaIdx := strings.Index(sql, `DROP SCHEMA IF EXISTS "s"`)
+ require.GreaterOrEqual(t, childIdx, 0, "schema-owned composite must be dropped: %s", sql)
+ require.GreaterOrEqual(t, baseIdx, 0)
+ require.GreaterOrEqual(t, statusIdx, 0)
+ require.GreaterOrEqual(t, schemaIdx, 0)
+ require.Less(t, childIdx, baseIdx, "dependent s.child must drop before public.base: %s", sql)
+ require.Less(t, childIdx, statusIdx, "dependent s.child must drop before public.status: %s", sql)
+ require.Greater(t, schemaIdx, childIdx, "DROP SCHEMA must come after its composite drop: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeCreateWaitsForTableRowType(t *testing.T) {
+ // Creating table t and composites c1(public.t) and c2(c1): both
+ // composites must be created after the table.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c1",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "r", Type: "public.t"},
+ },
+ },
+ {
+ Name: "c2",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "w", Type: "public.c1"},
+ },
+ },
+ {
+ Name: "plain",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ tableIdx := strings.Index(sql, `CREATE TABLE "public"."t"`)
+ c1Idx := strings.Index(sql, `CREATE TYPE "public"."c1"`)
+ c2Idx := strings.Index(sql, `CREATE TYPE "public"."c2"`)
+ plainIdx := strings.Index(sql, `CREATE TYPE "public"."plain"`)
+ require.GreaterOrEqual(t, tableIdx, 0, "table must be created: %s", sql)
+ require.GreaterOrEqual(t, c1Idx, 0)
+ require.GreaterOrEqual(t, c2Idx, 0)
+ require.GreaterOrEqual(t, plainIdx, 0)
+ require.Greater(t, c1Idx, tableIdx, "composite using the table row type must be created after the table: %s", sql)
+ require.Greater(t, c2Idx, c1Idx, "dependent of a post-table composite must follow it: %s", sql)
+ require.Less(t, plainIdx, tableIdx, "independent composite stays before tables: %s", sql)
+}
+
+func TestPGMetadataDiffDeferredCompositeDefersItsCompositeDependency(t *testing.T) {
+ // child(public.base) is deferred by a column retype; base, also dropped,
+ // must defer too so it drops after child.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "public.child", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "base",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ {
+ Name: "child",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "b", Type: "public.base"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
+ childIdx := strings.Index(sql, `DROP TYPE "public"."child"`)
+ baseIdx := strings.Index(sql, `DROP TYPE "public"."base"`)
+ require.GreaterOrEqual(t, retypeIdx, 0)
+ require.GreaterOrEqual(t, childIdx, 0)
+ require.GreaterOrEqual(t, baseIdx, 0)
+ require.Greater(t, childIdx, retypeIdx, "deferred child drops after the retype: %s", sql)
+ require.Greater(t, baseIdx, childIdx, "base referenced by deferred child must defer and drop after it: %s", sql)
+}
+
+func TestPGMetadataDiffRowTypeCompositeDropsBeforeTable(t *testing.T) {
+ // Dropping table t and composite c(r public.t): the composite must drop
+ // before the table whose row type it references.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "r", Type: "public.t"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ typeIdx := strings.Index(sql, `DROP TYPE "public"."c"`)
+ tableIdx := strings.Index(sql, `DROP TABLE "public"."t"`)
+ require.GreaterOrEqual(t, typeIdx, 0, "composite must be dropped: %s", sql)
+ require.GreaterOrEqual(t, tableIdx, 0, "table must be dropped: %s", sql)
+ require.Less(t, typeIdx, tableIdx, "row-type-referencing composite must drop before the table: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeAlterWaitsForNewTableRowType(t *testing.T) {
+ // Existing composite gains an attribute typed with a row type of a table
+ // created in the same migration; the alter must follow CREATE TABLE.
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ })
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ {Name: "r", Type: "public.t"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ tableIdx := strings.Index(sql, `CREATE TABLE "public"."t"`)
+ alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ADD ATTRIBUTE "r" public.t;`)
+ require.GreaterOrEqual(t, tableIdx, 0, "table must be created: %s", sql)
+ require.GreaterOrEqual(t, alterIdx, 0, "attribute must be added: %s", sql)
+ require.Greater(t, alterIdx, tableIdx, "composite alter must follow the table creation it references: %s", sql)
+}
+
+func TestPGMetadataDiffCreateSandwichTableCompositeTable(t *testing.T) {
+ // r (row-type provider) -> c (composite using r) -> holder (table using c):
+ // creation order must interleave through the shared graph.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "r",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ {
+ Name: "holder",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "x", Type: "public.c", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "row", Type: "public.r"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ rIdx := strings.Index(sql, `CREATE TABLE "public"."r"`)
+ cIdx := strings.Index(sql, `CREATE TYPE "public"."c"`)
+ holderIdx := strings.Index(sql, `CREATE TABLE "public"."holder"`)
+ require.GreaterOrEqual(t, rIdx, 0)
+ require.GreaterOrEqual(t, cIdx, 0)
+ require.GreaterOrEqual(t, holderIdx, 0)
+ require.Greater(t, cIdx, rIdx, "composite must follow its row-type provider table: %s", sql)
+ require.Greater(t, holderIdx, cIdx, "consumer table must follow the composite: %s", sql)
+}
+
+func TestPGMetadataDiffDropSandwichTableCompositeTable(t *testing.T) {
+ // holder (table using c) -> c (composite using r) -> r: drop order must
+ // interleave through the shared graph.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "r",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ {
+ Name: "holder",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "x", Type: "public.c", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "row", Type: "public.r"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ holderIdx := strings.Index(sql, `DROP TABLE "public"."holder"`)
+ cIdx := strings.Index(sql, `DROP TYPE "public"."c"`)
+ rIdx := strings.Index(sql, `DROP TABLE "public"."r"`)
+ require.GreaterOrEqual(t, holderIdx, 0)
+ require.GreaterOrEqual(t, cIdx, 0)
+ require.GreaterOrEqual(t, rIdx, 0)
+ require.Less(t, holderIdx, cIdx, "table using the composite must drop before it: %s", sql)
+ require.Less(t, cIdx, rIdx, "composite must drop before its row-type provider table: %s", sql)
+}
+
+func TestPGMetadataDiffDeferredSchemaCompositeDefersTopLevelDependency(t *testing.T) {
+ // Schema s is deferred (retype releases s.child); s.child references
+ // top-level dropped public.base, which must defer too.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "s.child", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "base",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ },
+ },
+ {
+ Name: "s",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "child",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "b", Type: "public.base"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "c", Type: "text", Nullable: true},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ retypeIdx := strings.Index(sql, `ALTER COLUMN "c"`)
+ childIdx := strings.Index(sql, `DROP TYPE "s"."child"`)
+ baseIdx := strings.Index(sql, `DROP TYPE "public"."base"`)
+ require.GreaterOrEqual(t, retypeIdx, 0)
+ require.GreaterOrEqual(t, childIdx, 0)
+ require.GreaterOrEqual(t, baseIdx, 0)
+ require.Greater(t, childIdx, retypeIdx, "deferred schema composite drops after the retype: %s", sql)
+ require.Greater(t, baseIdx, childIdx, "top-level base referenced by the deferred schema composite must defer and drop after it: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeDropDeferredAfterColumnDrop(t *testing.T) {
+ // Dropping the only column using a composite (on a surviving table)
+ // releases it in writeDropAlterTableObjects; the composite drop must
+ // defer past that.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "c", Type: "public.addr", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ columnDropIdx := strings.Index(sql, `DROP COLUMN "c"`)
+ typeDropIdx := strings.Index(sql, `DROP TYPE "public"."addr"`)
+ require.GreaterOrEqual(t, columnDropIdx, 0, "column must be dropped: %s", sql)
+ require.GreaterOrEqual(t, typeDropIdx, 0, "composite must be dropped: %s", sql)
+ require.Greater(t, typeDropIdx, columnDropIdx,
+ "composite drop must follow the column drop that releases it: %s", sql)
+}
+
+func TestPGMetadataDiffReleasingCompositeAlterPrecedesTableDrop(t *testing.T) {
+ // Retyping c.r away from public.t's row type releases the dropped table;
+ // the releasing ALTER must precede DROP TABLE.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "r", Type: "public.t"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "r", Type: "text"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ALTER ATTRIBUTE "r" TYPE text;`)
+ dropIdx := strings.Index(sql, `DROP TABLE "public"."t"`)
+ require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
+ require.GreaterOrEqual(t, dropIdx, 0, "table must be dropped: %s", sql)
+ require.Less(t, alterIdx, dropIdx, "releasing alter must precede the table drop: %s", sql)
+ require.Equal(t, strings.Count(sql, `ALTER ATTRIBUTE "r"`), 1, "releasing alter must not be emitted twice: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeArrayColumnOrdersCreate(t *testing.T) {
+ // Column sync renders composite-array columns as the bare "_name" form;
+ // the create graph must still order the composite first. zz table name
+ // sorts after the composite to defeat accidental ordering.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "aa_holder",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "xs", Type: "_zz_addr", Nullable: true},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "zz_addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ typeIdx := strings.Index(sql, `CREATE TYPE "public"."zz_addr"`)
+ tableIdx := strings.Index(sql, `CREATE TABLE "public"."aa_holder"`)
+ require.GreaterOrEqual(t, typeIdx, 0)
+ require.GreaterOrEqual(t, tableIdx, 0)
+ require.Less(t, typeIdx, tableIdx, "array-typed column must order the composite before the table: %s", sql)
+}
+
+func TestPGMetadataDiffSchemaDropEmitsCompositeDropOnce(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ {
+ Name: "s",
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "street", Type: "text"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Equal(t, 1, strings.Count(sql, `DROP TYPE "s"."addr";`),
+ "schema-owned composite must drop exactly once: %s", sql)
+}
+
+func TestPGMetadataDiffReleasingRetypeToCreatedTableSplitsDropReadd(t *testing.T) {
+ // c.r moves from dropped old_t's row type to created new_t's row type:
+ // the drop phase removes the attribute, the create phase re-adds it
+ // after the new table exists.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "old_t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "r", Type: "public.old_t"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "new_t",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ },
+ },
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "r", Type: "public.new_t"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ dropAttrIdx := strings.Index(sql, `ALTER TYPE "public"."c" DROP ATTRIBUTE "r";`)
+ dropTableIdx := strings.Index(sql, `DROP TABLE "public"."old_t"`)
+ createTableIdx := strings.Index(sql, `CREATE TABLE "public"."new_t"`)
+ addAttrIdx := strings.Index(sql, `ALTER TYPE "public"."c" ADD ATTRIBUTE "r" public.new_t;`)
+ require.GreaterOrEqual(t, dropAttrIdx, 0, "releasing drop must be emitted: %s", sql)
+ require.GreaterOrEqual(t, dropTableIdx, 0)
+ require.GreaterOrEqual(t, createTableIdx, 0)
+ require.GreaterOrEqual(t, addAttrIdx, 0, "attribute must be re-added: %s", sql)
+ require.Less(t, dropAttrIdx, dropTableIdx, "attribute drop must precede the table drop: %s", sql)
+ require.Greater(t, addAttrIdx, createTableIdx, "re-add must follow the new table: %s", sql)
+ require.NotContains(t, sql, `ALTER ATTRIBUTE "r"`, "no premature retype may be emitted: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeCreateWaitsForViewRowType(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Views: []*storepb.ViewMetadata{
+ {Name: "v", Definition: "SELECT 1 AS a"},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "row", Type: "public.v"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ viewIdx := strings.Index(sql, `VIEW "public"."v"`)
+ typeIdx := strings.Index(sql, `CREATE TYPE "public"."c"`)
+ require.GreaterOrEqual(t, viewIdx, 0, "view must be created: %s", sql)
+ require.GreaterOrEqual(t, typeIdx, 0, "composite must be created: %s", sql)
+ require.Greater(t, typeIdx, viewIdx, "composite using a view row type must follow the view: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeDropReleasedByCompositeAlterDefers(t *testing.T) {
+ // parent's alter (child -> text) releases dropped child only in the
+ // create phase, so child's drop must defer past it.
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "child",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ {
+ Name: "parent",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "c", Type: "public.child"},
+ },
+ },
+ })
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "parent",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "c", Type: "text"},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ alterIdx := strings.Index(sql, `ALTER TYPE "public"."parent" ALTER ATTRIBUTE "c" TYPE text;`)
+ dropIdx := strings.Index(sql, `DROP TYPE "public"."child"`)
+ require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
+ require.GreaterOrEqual(t, dropIdx, 0, "child must be dropped: %s", sql)
+ require.Greater(t, dropIdx, alterIdx, "child drop must follow the alter that releases it: %s", sql)
+}
+
+func TestPGMetadataDiffEnumDropReleasedByCompositeAlterDefers(t *testing.T) {
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {Name: "status", Values: []string{"a"}},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "s", Type: "public.status"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "addr",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "s", Type: "text"},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ alterIdx := strings.Index(sql, `ALTER TYPE "public"."addr" ALTER ATTRIBUTE "s" TYPE text;`)
+ dropIdx := strings.Index(sql, `DROP TYPE "public"."status"`)
+ require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
+ require.GreaterOrEqual(t, dropIdx, 0, "enum must be dropped: %s", sql)
+ require.Greater(t, dropIdx, alterIdx, "enum drop must follow the alter that releases it: %s", sql)
+}
+
+func TestPGMetadataDiffReleasingAlterPrecedesViewDrop(t *testing.T) {
+ // c.row moves off dropped view v's row type; the early alter must
+ // precede DROP VIEW.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Views: []*storepb.ViewMetadata{
+ {Name: "v", Definition: "SELECT 1 AS a"},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "row", Type: "public.v"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "row", Type: "text"},
+ },
+ },
+ })
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ALTER ATTRIBUTE "row" TYPE text;`)
+ dropViewIdx := strings.Index(sql, `DROP VIEW "public"."v"`)
+ require.GreaterOrEqual(t, alterIdx, 0, "releasing alter must be emitted: %s", sql)
+ require.GreaterOrEqual(t, dropViewIdx, 0, "view must be dropped: %s", sql)
+ require.Less(t, alterIdx, dropViewIdx, "releasing alter must precede the view drop: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeAlterWaitsForCreatedViewRowType(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ })
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Views: []*storepb.ViewMetadata{
+ {Name: "v", Definition: "SELECT 1 AS a"},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ {Name: "row", Type: "public.v"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ viewIdx := strings.Index(sql, `VIEW "public"."v"`)
+ alterIdx := strings.Index(sql, `ALTER TYPE "public"."c" ADD ATTRIBUTE "row" public.v;`)
+ require.GreaterOrEqual(t, viewIdx, 0, "view must be created: %s", sql)
+ require.GreaterOrEqual(t, alterIdx, 0, "attribute must be added: %s", sql)
+ require.Greater(t, alterIdx, viewIdx, "alter gaining the view row type must follow the view: %s", sql)
+}
+
+func TestPGMetadataDiffCompositeDropPrecedesReferencedViewDrop(t *testing.T) {
+ // Dropping both c(row public.v) and v: the composite must drop first
+ // despite the blanket views-before-composites edges.
+ source := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Views: []*storepb.ViewMetadata{
+ {Name: "v", Definition: "SELECT 1 AS a"},
+ },
+ CompositeTypes: []*storepb.CompositeTypeMetadata{
+ {
+ Name: "c",
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "row", Type: "public.v"},
+ },
+ },
+ },
+ },
+ },
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+ target := model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{{Name: "public"}},
+ }, nil, nil, storepb.Engine_POSTGRES, true)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ typeIdx := strings.Index(sql, `DROP TYPE "public"."c"`)
+ viewIdx := strings.Index(sql, `DROP VIEW "public"."v"`)
+ require.GreaterOrEqual(t, typeIdx, 0, "composite must be dropped: %s", sql)
+ require.GreaterOrEqual(t, viewIdx, 0, "view must be dropped: %s", sql)
+ require.Less(t, typeIdx, viewIdx, "composite must drop before the view whose row type it references: %s", sql)
+}
+
+func TestFilterPostgresArchiveSchemaKeepsCompositeTypes(t *testing.T) {
+ diff := &schema.MetadataDiff{
+ CompositeTypeChanges: []*schema.CompositeTypeDiff{
+ {Action: schema.MetadataDiffActionCreate, SchemaName: "public", CompositeTypeName: "addr"},
+ {Action: schema.MetadataDiffActionCreate, SchemaName: "bbdataarchive", CompositeTypeName: "archived"},
+ },
+ }
+
+ filtered := schema.FilterPostgresArchiveSchema(diff)
+
+ require.Len(t, filtered.CompositeTypeChanges, 1)
+ require.Equal(t, "public", filtered.CompositeTypeChanges[0].SchemaName)
+}
+
+func TestPGMetadataDiffSkipDumpCompositeTypesIgnored(t *testing.T) {
+ source := newPGDatabaseMetadataWithCompositeTypes([]*storepb.CompositeTypeMetadata{
+ {
+ Name: "ext_owned",
+ SkipDump: true,
+ Attributes: []*storepb.CompositeTypeAttribute{
+ {Name: "x", Type: "integer"},
+ },
+ },
+ })
+ target := newPGDatabaseMetadataWithCompositeTypes(nil)
+
+ sql, err := schema.DiffMigration(storepb.Engine_POSTGRES, source, target)
+
+ require.NoError(t, err)
+ require.Empty(t, sql, "skip_dump composite types must not be diffed")
+}
diff --git a/backend/plugin/schema/pg/omni_filter_test.go b/backend/plugin/schema/pg/omni_filter_test.go
deleted file mode 100644
index 733ab3600e..0000000000
--- a/backend/plugin/schema/pg/omni_filter_test.go
+++ /dev/null
@@ -1,144 +0,0 @@
-package pg
-
-import (
- "testing"
-
- "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"
-)
-
-// TestOmniFilter_BbdataarchiveSchemaFiltered verifies that objects in the
-// bbdataarchive schema are excluded from migration output.
-func TestOmniFilter_BbdataarchiveSchemaFiltered(t *testing.T) {
- // Source has tables in both public and bbdataarchive schemas.
- sourceMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- },
- },
- {
- Name: "bbdataarchive",
- Tables: []*storepb.TableMetadata{
- {Name: "archived_users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- },
- },
- },
- }
- // Target is empty — should only generate DROP for public, not bbdataarchive.
- targetSDL := ""
-
- sourceMeta := model.NewDatabaseMetadata(sourceMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- sourceSDL, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, sourceMeta)
- require.NoError(t, err)
-
- sql, err := schema.DiffSDLMigration(storepb.Engine_POSTGRES, sourceSDL, targetSDL, "")
- require.NoError(t, err)
-
- // bbdataarchive objects must not appear in migration.
- require.NotContains(t, sql, "bbdataarchive")
- require.NotContains(t, sql, "archived_users")
- // public objects should be dropped.
- require.Contains(t, sql, "users")
-}
-
-// TestOmniFilter_SkipBackupSchemaInSDLGeneration verifies that MetadataToSDL
-// excludes the backup schema entirely from generated SDL text.
-func TestOmniFilter_SkipBackupSchemaInSDLGeneration(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "t1", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- },
- },
- {
- Name: "bbdataarchive",
- Tables: []*storepb.TableMetadata{
- {Name: "backup_t1", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- },
- },
- },
- }
- meta := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- sdl, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, meta)
- require.NoError(t, err)
-
- require.NotContains(t, sdl, "bbdataarchive")
- require.NotContains(t, sdl, "backup_t1")
- require.Contains(t, sdl, "t1")
-}
-
-// TestOmniFilter_SkipDumpObjectsExcluded verifies that objects marked with
-// SkipDump (e.g., extension-created objects) are excluded from SDL generation.
-func TestOmniFilter_SkipDumpObjectsExcluded(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "user_table", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- {Name: "ext_table", SkipDump: true, Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- },
- Functions: []*storepb.FunctionMetadata{
- {Name: "my_func", Definition: "CREATE FUNCTION my_func() RETURNS void LANGUAGE sql AS $$ SELECT 1 $$;"},
- {Name: "ext_func", SkipDump: true, Definition: "CREATE FUNCTION ext_func() RETURNS void LANGUAGE sql AS $$ SELECT 1 $$;"},
- },
- },
- },
- }
- meta := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- sdl, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, meta)
- require.NoError(t, err)
-
- require.Contains(t, sdl, "user_table")
- require.Contains(t, sdl, "my_func")
- require.NotContains(t, sdl, "ext_table")
- require.NotContains(t, sdl, "ext_func")
-}
-
-// TestOmniFilter_ExtensionPreservedWhenArchiveFiltered verifies that extension
-// operations are preserved even when bbdataarchive objects are filtered.
-func TestOmniFilter_ExtensionPreservedWhenArchiveFiltered(t *testing.T) {
- sql := omniSDLMigration(t, "", `CREATE EXTENSION IF NOT EXISTS "citext";`)
- // Extension creation should not be filtered.
- require.Contains(t, sql, "citext")
-}
-
-// TestOmniFilter_NoChangesForIdenticalSchemas verifies that two identical
-// schemas with backup objects produce no migration (backup objects are
-// excluded from both sides consistently).
-func TestOmniFilter_NoChangesForIdenticalSchemas(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- },
- },
- {
- Name: "bbdataarchive",
- Tables: []*storepb.TableMetadata{
- {Name: "old_users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
- },
- },
- },
- }
-
- meta := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)
- // Use MetadataToSDL to get the exact SDL that would be generated, then diff against itself.
- sourceSDL, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, meta)
- require.NoError(t, err)
- sql, err := schema.SDLMigration(storepb.Engine_POSTGRES, sourceSDL, meta, "")
- require.NoError(t, err)
- require.Empty(t, sql, "identical public schemas with backup objects should produce no migration")
-}
diff --git a/backend/plugin/schema/pg/sdl_migration_omni_test.go b/backend/plugin/schema/pg/sdl_migration_omni_test.go
index b54b405ccb..065f0c8094 100644
--- a/backend/plugin/schema/pg/sdl_migration_omni_test.go
+++ b/backend/plugin/schema/pg/sdl_migration_omni_test.go
@@ -4,12 +4,12 @@ import (
"strings"
"testing"
- "github.com/stretchr/testify/require"
-
"github.com/bytebase/omni/pg/catalog"
+ "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"
)
const eventTriggerFunctionSDL = `
@@ -433,3 +433,136 @@ func TestOmniSDLMigration_MultipleChanges(t *testing.T) {
require.Contains(t, sql, "email")
require.Contains(t, sql, "new_table")
}
+
+// TestOmniFilter_BbdataarchiveSchemaFiltered verifies that objects in the
+// bbdataarchive schema are excluded from migration output.
+func TestOmniFilter_BbdataarchiveSchemaFiltered(t *testing.T) {
+ // Source has tables in both public and bbdataarchive schemas.
+ sourceMetadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ },
+ },
+ {
+ Name: "bbdataarchive",
+ Tables: []*storepb.TableMetadata{
+ {Name: "archived_users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ },
+ },
+ },
+ }
+ // Target is empty — should only generate DROP for public, not bbdataarchive.
+ targetSDL := ""
+
+ sourceMeta := model.NewDatabaseMetadata(sourceMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ sourceSDL, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, sourceMeta)
+ require.NoError(t, err)
+
+ sql, err := schema.DiffSDLMigration(storepb.Engine_POSTGRES, sourceSDL, targetSDL, "")
+ require.NoError(t, err)
+
+ // bbdataarchive objects must not appear in migration.
+ require.NotContains(t, sql, "bbdataarchive")
+ require.NotContains(t, sql, "archived_users")
+ // public objects should be dropped.
+ require.Contains(t, sql, "users")
+}
+
+// TestOmniFilter_SkipBackupSchemaInSDLGeneration verifies that MetadataToSDL
+// excludes the backup schema entirely from generated SDL text.
+func TestOmniFilter_SkipBackupSchemaInSDLGeneration(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {Name: "t1", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ },
+ },
+ {
+ Name: "bbdataarchive",
+ Tables: []*storepb.TableMetadata{
+ {Name: "backup_t1", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ },
+ },
+ },
+ }
+ meta := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)
+
+ sdl, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, meta)
+ require.NoError(t, err)
+
+ require.NotContains(t, sdl, "bbdataarchive")
+ require.NotContains(t, sdl, "backup_t1")
+ require.Contains(t, sdl, "t1")
+}
+
+// TestOmniFilter_SkipDumpObjectsExcluded verifies that objects marked with
+// SkipDump (e.g., extension-created objects) are excluded from SDL generation.
+func TestOmniFilter_SkipDumpObjectsExcluded(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {Name: "user_table", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ {Name: "ext_table", SkipDump: true, Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ },
+ Functions: []*storepb.FunctionMetadata{
+ {Name: "my_func", Definition: "CREATE FUNCTION my_func() RETURNS void LANGUAGE sql AS $$ SELECT 1 $$;"},
+ {Name: "ext_func", SkipDump: true, Definition: "CREATE FUNCTION ext_func() RETURNS void LANGUAGE sql AS $$ SELECT 1 $$;"},
+ },
+ },
+ },
+ }
+ meta := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)
+
+ sdl, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, meta)
+ require.NoError(t, err)
+
+ require.Contains(t, sdl, "user_table")
+ require.Contains(t, sdl, "my_func")
+ require.NotContains(t, sdl, "ext_table")
+ require.NotContains(t, sdl, "ext_func")
+}
+
+// TestOmniFilter_ExtensionPreservedWhenArchiveFiltered verifies that extension
+// operations are preserved even when bbdataarchive objects are filtered.
+func TestOmniFilter_ExtensionPreservedWhenArchiveFiltered(t *testing.T) {
+ sql := omniSDLMigration(t, "", `CREATE EXTENSION IF NOT EXISTS "citext";`)
+ // Extension creation should not be filtered.
+ require.Contains(t, sql, "citext")
+}
+
+// TestOmniFilter_NoChangesForIdenticalSchemas verifies that two identical
+// schemas with backup objects produce no migration (backup objects are
+// excluded from both sides consistently).
+func TestOmniFilter_NoChangesForIdenticalSchemas(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {Name: "users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ },
+ },
+ {
+ Name: "bbdataarchive",
+ Tables: []*storepb.TableMetadata{
+ {Name: "old_users", Columns: []*storepb.ColumnMetadata{{Name: "id", Type: "integer"}}},
+ },
+ },
+ },
+ }
+
+ meta := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)
+ // Use MetadataToSDL to get the exact SDL that would be generated, then diff against itself.
+ sourceSDL, err := schema.MetadataToSDL(storepb.Engine_POSTGRES, meta)
+ require.NoError(t, err)
+ sql, err := schema.SDLMigration(storepb.Engine_POSTGRES, sourceSDL, meta, "")
+ require.NoError(t, err)
+ require.Empty(t, sql, "identical public schemas with backup objects should produce no migration")
+}
diff --git a/backend/plugin/schema/pg/sequence_name_extraction_test.go b/backend/plugin/schema/pg/sequence_name_extraction_test.go
deleted file mode 100644
index f1c978e93e..0000000000
--- a/backend/plugin/schema/pg/sequence_name_extraction_test.go
+++ /dev/null
@@ -1,185 +0,0 @@
-package pg
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
-)
-
-func TestExtractSequenceNameFromNextval(t *testing.T) {
- testCases := []struct {
- name string
- defaultValue string
- expectedName string
- description string
- }{
- {
- name: "Simple sequence name with single quotes",
- defaultValue: "nextval('users_id_seq')",
- expectedName: "users_id_seq",
- description: "Basic case with simple sequence name",
- },
- {
- name: "Sequence name with schema qualification",
- defaultValue: "nextval('public.users_id_seq')",
- expectedName: "users_id_seq",
- description: "Schema-qualified sequence name",
- },
- {
- name: "Sequence with regclass cast",
- defaultValue: "nextval('users_id_seq'::regclass)",
- expectedName: "users_id_seq",
- description: "Sequence with ::regclass type cast",
- },
- {
- name: "Schema and regclass",
- defaultValue: "nextval('public.users_id_seq'::regclass)",
- expectedName: "users_id_seq",
- description: "Schema-qualified sequence with regclass cast",
- },
- {
- name: "Quoted sequence name",
- defaultValue: `nextval('"Users_Id_Seq"')`,
- expectedName: "Users_Id_Seq",
- description: "Sequence name with double quotes for case sensitivity",
- },
- {
- name: "Quoted schema and sequence",
- defaultValue: `nextval('"Public"."Users_Id_Seq"')`,
- expectedName: "Users_Id_Seq",
- description: "Both schema and sequence quoted",
- },
- {
- name: "Mixed case with public schema",
- defaultValue: `nextval('public."MixedCaseSeq"')`,
- expectedName: "MixedCaseSeq",
- description: "Unquoted schema with quoted sequence",
- },
- {
- name: "Quoted schema unquoted sequence",
- defaultValue: `nextval('"Public".users_id_seq')`,
- expectedName: "users_id_seq",
- description: "Quoted schema with unquoted sequence",
- },
- {
- name: "Complex case with all features",
- defaultValue: `nextval('"MySchema"."MySeq"'::regclass)`,
- expectedName: "MySeq",
- description: "Quoted schema, quoted sequence, and regclass",
- },
- {
- name: "Uppercase NEXTVAL",
- defaultValue: `NEXTVAL('users_id_seq')`,
- expectedName: "users_id_seq",
- description: "Case-insensitive NEXTVAL function name",
- },
- {
- name: "Mixed case nextval",
- defaultValue: `NextVal('users_id_seq')`,
- expectedName: "users_id_seq",
- description: "Mixed case nextval function name",
- },
- {
- name: "Sequence with spaces",
- defaultValue: `nextval( 'users_id_seq' )`,
- expectedName: "users_id_seq",
- description: "Extra spaces around sequence name",
- },
- {
- name: "Double quoted outer quotes",
- defaultValue: `nextval("users_id_seq")`,
- expectedName: "users_id_seq",
- description: "Double quotes as outer quotes",
- },
- {
- name: "Empty default",
- defaultValue: "",
- expectedName: "",
- description: "Empty string should return empty",
- },
- {
- name: "No nextval",
- defaultValue: "42",
- expectedName: "",
- description: "Non-nextval default should return empty",
- },
- {
- name: "Malformed nextval",
- defaultValue: "nextval(",
- expectedName: "",
- description: "Malformed nextval should return empty",
- },
- {
- name: "Special characters in schema",
- defaultValue: `nextval('"schema-name"."seq_name"')`,
- expectedName: "seq_name",
- description: "Schema with special characters",
- },
- {
- name: "Multiple dots",
- defaultValue: `nextval('"my.schema"."my.seq"')`,
- expectedName: "my.seq",
- description: "Dots within quoted identifiers",
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- result := extractSequenceNameFromNextval(tc.defaultValue)
- assert.Equal(t, tc.expectedName, result, tc.description)
- })
- }
-}
-
-func TestExtractIdentifierFromQualifiedName(t *testing.T) {
- testCases := []struct {
- name string
- qualifiedName string
- expected string
- description string
- }{
- {
- name: "Simple identifier",
- qualifiedName: "users_id_seq",
- expected: "users_id_seq",
- description: "No schema qualification",
- },
- {
- name: "Schema qualified",
- qualifiedName: "public.users_id_seq",
- expected: "users_id_seq",
- description: "Simple schema.sequence",
- },
- {
- name: "Both quoted",
- qualifiedName: `"Public"."Users_Id_Seq"`,
- expected: `"Users_Id_Seq"`,
- description: "Both parts quoted",
- },
- {
- name: "Only sequence quoted",
- qualifiedName: `public."MixedCase"`,
- expected: `"MixedCase"`,
- description: "Only sequence name quoted",
- },
- {
- name: "Dot in quoted identifier",
- qualifiedName: `"my.schema"."my.seq"`,
- expected: `"my.seq"`,
- description: "Dots inside quoted parts should be ignored",
- },
- {
- name: "Empty string",
- qualifiedName: "",
- expected: "",
- description: "Empty input should return empty",
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- result := extractIdentifierFromQualifiedName(tc.qualifiedName)
- assert.Equal(t, tc.expected, result, tc.description)
- })
- }
-}
diff --git a/backend/plugin/schema/pg/trigger_sdl_output_test.go b/backend/plugin/schema/pg/trigger_sdl_output_test.go
deleted file mode 100644
index 23ed1506d4..0000000000
--- a/backend/plugin/schema/pg/trigger_sdl_output_test.go
+++ /dev/null
@@ -1,215 +0,0 @@
-package pg
-
-import (
- "strings"
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-func TestTriggerSDLSingleFileOutput(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "audit_trigger",
- Body: "CREATE TRIGGER audit_trigger AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- assert.Contains(t, result, "CREATE TRIGGER audit_trigger")
- assert.Contains(t, result, "AFTER INSERT ON public.users")
- assert.Contains(t, result, "FOR EACH ROW EXECUTE FUNCTION audit_log()")
-}
-
-func TestTriggerSDLMultiFileOutput(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "audit_trigger",
- Body: "CREATE TRIGGER audit_trigger AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetMultiFileDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
- require.NotNil(t, result)
-
- // Find the users table file
- var usersFileContent string
- for _, file := range result.Files {
- if file.Name == "schemas/public/tables/users.sql" {
- usersFileContent = file.Content
- break
- }
- }
-
- require.NotEmpty(t, usersFileContent, "Should have users table file")
- assert.Contains(t, usersFileContent, "CREATE TRIGGER audit_trigger")
- assert.Contains(t, usersFileContent, "AFTER INSERT ON public.users")
-}
-
-func TestTriggerSDLWithCompleteDefinition(t *testing.T) {
- // Test that triggers with complete CREATE TRIGGER statements in Body are output correctly
- // This simulates triggers dumped from the database where Body contains the full statement
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "ci_builds",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "ci_builds_loose_fk_trigger",
- Body: "CREATE TRIGGER ci_builds_loose_fk_trigger AFTER DELETE ON public.ci_builds REFERENCING OLD TABLE AS old_table FOR EACH STATEMENT EXECUTE FUNCTION public.loose_foreign_key_on_builds_projects()",
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- // Should contain only one CREATE TRIGGER (not duplicated)
- createTriggerCount := strings.Count(result, "CREATE TRIGGER ci_builds_loose_fk_trigger")
- assert.Equal(t, 1, createTriggerCount, "CREATE TRIGGER should appear exactly once, not duplicated")
-
- // Should contain the complete trigger definition
- assert.Contains(t, result, "AFTER DELETE ON public.ci_builds")
- assert.Contains(t, result, "REFERENCING OLD TABLE AS old_table")
- assert.Contains(t, result, "FOR EACH STATEMENT")
- assert.Contains(t, result, "EXECUTE FUNCTION public.loose_foreign_key_on_builds_projects()")
-}
-
-func TestTriggerSDLWithComment(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "audit_trigger",
- Body: "CREATE TRIGGER audit_trigger AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
- Comment: "Audit log trigger",
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- assert.Contains(t, result, "CREATE TRIGGER audit_trigger")
- assert.Contains(t, result, "COMMENT ON TRIGGER")
- assert.Contains(t, result, "audit_trigger")
- assert.Contains(t, result, "Audit log trigger")
-}
-
-func TestMultipleTriggersSDL(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer"},
- },
- Triggers: []*storepb.TriggerMetadata{
- {
- Name: "audit_insert",
- Body: "CREATE TRIGGER audit_insert AFTER INSERT ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
- },
- {
- Name: "audit_update",
- Body: "CREATE TRIGGER audit_update AFTER UPDATE ON public.users FOR EACH ROW EXECUTE FUNCTION audit_log()",
- },
- },
- },
- },
- },
- },
- }
-
- ctx := schema.GetDefinitionContext{
- SDLFormat: true,
- }
-
- result, err := GetDatabaseDefinition(ctx, metadata)
- require.NoError(t, err)
-
- assert.Contains(t, result, "audit_insert")
- assert.Contains(t, result, "audit_update")
-}
diff --git a/backend/plugin/schema/pg/walk_through_clone_test.go b/backend/plugin/schema/pg/walk_through_clone_test.go
deleted file mode 100644
index 0f581fdd9c..0000000000
--- a/backend/plugin/schema/pg/walk_through_clone_test.go
+++ /dev/null
@@ -1,215 +0,0 @@
-package pg
-
-import (
- "context"
- "testing"
-
- "github.com/bytebase/omni/pg/catalog"
- "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"
-)
-
-// TestClone_BasicIsolation verifies that Clone produces an independent catalog:
-// mutations on the clone do not affect the original.
-func TestClone_BasicIsolation(t *testing.T) {
- original := catalog.New()
- _, err := original.Exec(`
- CREATE SCHEMA test_schema;
- CREATE TABLE test_schema.t1 (id serial PRIMARY KEY, name text NOT NULL);
- CREATE INDEX t1_name_idx ON test_schema.t1 (name);
- `, nil)
- require.NoError(t, err)
-
- clone := original.Clone()
-
- // Mutate clone
- _, err = clone.Exec(`
- ALTER TABLE test_schema.t1 ADD COLUMN email text;
- CREATE TABLE test_schema.t2 (id int PRIMARY KEY);
- DROP INDEX test_schema.t1_name_idx;
- `, nil)
- require.NoError(t, err)
-
- // Verify original is untouched
- origRel := original.GetRelation("test_schema", "t1")
- require.NotNil(t, origRel, "original t1 should still exist")
- require.Equal(t, 2, len(origRel.Columns), "original t1 should have 2 columns (id, name), not 3")
-
- require.Nil(t, original.GetRelation("test_schema", "t2"), "original should NOT have t2")
-
- origIndexes := original.IndexesOf(origRel.OID)
- found := false
- for _, idx := range origIndexes {
- if idx.Name == "t1_name_idx" {
- found = true
- }
- }
- require.True(t, found, "original should still have t1_name_idx")
-
- // Verify clone has the changes
- cloneRel := clone.GetRelation("test_schema", "t1")
- require.NotNil(t, cloneRel)
- require.Equal(t, 3, len(cloneRel.Columns), "clone t1 should have 3 columns")
- require.NotNil(t, clone.GetRelation("test_schema", "t2"), "clone should have t2")
-
- // Diff should show changes
- diff := catalog.Diff(original, clone)
- require.False(t, diff.IsEmpty(), "diff should not be empty")
- require.Greater(t, len(diff.Relations), 0, "should have relation diffs")
-}
-
-// TestClone_WalkThroughIntegration tests the full walk-through flow using Clone:
-// load catalog → clone → exec user DDL on clone → diff → apply.
-func TestClone_WalkThroughIntegration(t *testing.T) {
- meta := &storepb.DatabaseSchemaMetadata{
- Name: "postgres",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer", Position: 1},
- {Name: "name", Type: "text", Position: 2, Nullable: true},
- },
- Indexes: []*storepb.IndexMetadata{
- {Name: "users_pkey", Expressions: []string{"id"}, Unique: true, Primary: true},
- },
- },
- },
- },
- },
- }
-
- catBefore := catalog.New()
- err := loadWalkThroughCatalog(context.Background(), catBefore, meta)
- require.NoError(t, err)
-
- catAfter := catBefore.Clone()
-
- userSQL := `
- ALTER TABLE public.users ADD COLUMN email text NOT NULL DEFAULT 'unknown';
- CREATE INDEX users_email_idx ON public.users (email);
- CREATE TABLE public.posts (id serial PRIMARY KEY, user_id int REFERENCES public.users(id), title text);
- `
- results, err := catAfter.Exec(userSQL, &catalog.ExecOptions{ContinueOnError: true})
- require.NoError(t, err)
- for _, r := range results {
- require.NoError(t, r.Error, "DDL should succeed: %s", r.SQL)
- }
-
- // Diff
- diff := catalog.Diff(catBefore, catAfter)
- require.False(t, diff.IsEmpty())
-
- // Apply diff to original metadata
- newProto := applyDiffToMetadata(meta, catBefore, catAfter, diff)
- newMeta := model.NewDatabaseMetadata(newProto, nil, nil, storepb.Engine_POSTGRES, true)
-
- // Verify: users table should have 3 columns
- usersSchema := newMeta.GetSchemaMetadata("public")
- require.NotNil(t, usersSchema)
- usersTbl := usersSchema.GetTable("users")
- require.NotNil(t, usersTbl)
- require.Equal(t, 3, len(usersTbl.GetProto().Columns), "users should have id, name, email")
-
- // Verify: users should have 2 indexes (pkey + email)
- require.GreaterOrEqual(t, len(usersTbl.GetProto().Indexes), 2)
-
- // Verify: posts table should exist
- postsTbl := usersSchema.GetTable("posts")
- require.NotNil(t, postsTbl, "posts table should exist")
-
- // Verify: original metadata should NOT have posts or email column
- origMeta := model.NewDatabaseMetadata(meta, nil, nil, storepb.Engine_POSTGRES, true)
- origUsers := origMeta.GetSchemaMetadata("public").GetTable("users")
- require.Equal(t, 2, len(origUsers.GetProto().Columns), "original should still have 2 columns")
- require.Nil(t, origMeta.GetSchemaMetadata("public").GetTable("posts"), "original should not have posts")
-}
-
-// TestClone_SearchPath tests Clone preserves search path and session user.
-func TestClone_SearchPath(t *testing.T) {
- meta := &storepb.DatabaseSchemaMetadata{
- Name: "postgres",
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- {Name: "alice"},
- },
- }
-
- catBefore := catalog.New()
- catBefore.SetSessionUser("alice")
- catBefore.SetSearchPath([]string{"$user", "public"})
- err := loadWalkThroughCatalog(context.Background(), catBefore, meta)
- require.NoError(t, err)
-
- catAfter := catBefore.Clone()
-
- // CREATE TABLE without schema should use search path ($user → alice)
- _, err = catAfter.Exec(`CREATE TABLE my_table (id int);`, nil)
- require.NoError(t, err)
-
- // Table should be in alice schema on clone
- require.NotNil(t, catAfter.GetRelation("alice", "my_table"))
-
- // Original should NOT have it
- require.Nil(t, catBefore.GetRelation("alice", "my_table"))
-
- // Diff should show the new table
- diff := catalog.Diff(catBefore, catAfter)
- require.False(t, diff.IsEmpty())
-
- foundAliceTable := false
- for _, rel := range diff.Relations {
- if rel.SchemaName == "alice" && rel.Name == "my_table" && rel.Action == catalog.DiffAdd {
- foundAliceTable = true
- }
- }
- require.True(t, foundAliceTable, "diff should show alice.my_table as added")
-}
-
-// TestClone_WalkThroughOmniFunction tests the actual WalkThroughOmni function
-// using Clone (if enabled) produces correct FinalMetadata.
-func TestClone_WalkThroughOmniFunction(t *testing.T) {
- meta := &storepb.DatabaseSchemaMetadata{
- Name: "postgres",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "test",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "integer", Position: 1},
- {Name: "name", Type: "text", Position: 2, Nullable: true},
- },
- Indexes: []*storepb.IndexMetadata{
- {Name: "test_pkey", Expressions: []string{"id"}, Unique: true, Primary: true},
- },
- },
- },
- },
- },
- }
-
- state := model.NewDatabaseMetadata(meta, nil, nil, storepb.Engine_POSTGRES, true)
- ctx := schema.WalkThroughContext{
- RawSQL: `CREATE TABLE public.new_table (id int PRIMARY KEY, val text);`,
- }
-
- advice := WalkThroughOmni(ctx, state, nil)
- require.Nil(t, advice, "walk-through should succeed")
-
- // Check FinalMetadata has the new table
- newTbl := state.GetSchemaMetadata("public").GetTable("new_table")
- require.NotNil(t, newTbl, "new_table should exist in FinalMetadata")
-
- // Check original table is preserved
- origTbl := state.GetSchemaMetadata("public").GetTable("test")
- require.NotNil(t, origTbl, "test table should still exist")
- require.Equal(t, 2, len(origTbl.GetProto().Columns))
-}
diff --git a/backend/plugin/schema/pg/composite_type_walkthrough_test.go b/backend/plugin/schema/pg/walk_through_loader_test.go
similarity index 59%
rename from backend/plugin/schema/pg/composite_type_walkthrough_test.go
rename to backend/plugin/schema/pg/walk_through_loader_test.go
index aeb538f16d..b738b06abd 100644
--- a/backend/plugin/schema/pg/composite_type_walkthrough_test.go
+++ b/backend/plugin/schema/pg/walk_through_loader_test.go
@@ -4,14 +4,216 @@ import (
"context"
"testing"
- "github.com/stretchr/testify/require"
-
"github.com/bytebase/omni/pg/catalog"
+ "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"
)
+// TestClone_BasicIsolation verifies that Clone produces an independent catalog:
+// mutations on the clone do not affect the original.
+func TestClone_BasicIsolation(t *testing.T) {
+ original := catalog.New()
+ _, err := original.Exec(`
+ CREATE SCHEMA test_schema;
+ CREATE TABLE test_schema.t1 (id serial PRIMARY KEY, name text NOT NULL);
+ CREATE INDEX t1_name_idx ON test_schema.t1 (name);
+ `, nil)
+ require.NoError(t, err)
+
+ clone := original.Clone()
+
+ // Mutate clone
+ _, err = clone.Exec(`
+ ALTER TABLE test_schema.t1 ADD COLUMN email text;
+ CREATE TABLE test_schema.t2 (id int PRIMARY KEY);
+ DROP INDEX test_schema.t1_name_idx;
+ `, nil)
+ require.NoError(t, err)
+
+ // Verify original is untouched
+ origRel := original.GetRelation("test_schema", "t1")
+ require.NotNil(t, origRel, "original t1 should still exist")
+ require.Equal(t, 2, len(origRel.Columns), "original t1 should have 2 columns (id, name), not 3")
+
+ require.Nil(t, original.GetRelation("test_schema", "t2"), "original should NOT have t2")
+
+ origIndexes := original.IndexesOf(origRel.OID)
+ found := false
+ for _, idx := range origIndexes {
+ if idx.Name == "t1_name_idx" {
+ found = true
+ }
+ }
+ require.True(t, found, "original should still have t1_name_idx")
+
+ // Verify clone has the changes
+ cloneRel := clone.GetRelation("test_schema", "t1")
+ require.NotNil(t, cloneRel)
+ require.Equal(t, 3, len(cloneRel.Columns), "clone t1 should have 3 columns")
+ require.NotNil(t, clone.GetRelation("test_schema", "t2"), "clone should have t2")
+
+ // Diff should show changes
+ diff := catalog.Diff(original, clone)
+ require.False(t, diff.IsEmpty(), "diff should not be empty")
+ require.Greater(t, len(diff.Relations), 0, "should have relation diffs")
+}
+
+// TestClone_WalkThroughIntegration tests the full walk-through flow using Clone:
+// load catalog → clone → exec user DDL on clone → diff → apply.
+func TestClone_WalkThroughIntegration(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "postgres",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "name", Type: "text", Position: 2, Nullable: true},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {Name: "users_pkey", Expressions: []string{"id"}, Unique: true, Primary: true},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ catBefore := catalog.New()
+ err := loadWalkThroughCatalog(context.Background(), catBefore, meta)
+ require.NoError(t, err)
+
+ catAfter := catBefore.Clone()
+
+ userSQL := `
+ ALTER TABLE public.users ADD COLUMN email text NOT NULL DEFAULT 'unknown';
+ CREATE INDEX users_email_idx ON public.users (email);
+ CREATE TABLE public.posts (id serial PRIMARY KEY, user_id int REFERENCES public.users(id), title text);
+ `
+ results, err := catAfter.Exec(userSQL, &catalog.ExecOptions{ContinueOnError: true})
+ require.NoError(t, err)
+ for _, r := range results {
+ require.NoError(t, r.Error, "DDL should succeed: %s", r.SQL)
+ }
+
+ // Diff
+ diff := catalog.Diff(catBefore, catAfter)
+ require.False(t, diff.IsEmpty())
+
+ // Apply diff to original metadata
+ newProto := applyDiffToMetadata(meta, catBefore, catAfter, diff)
+ newMeta := model.NewDatabaseMetadata(newProto, nil, nil, storepb.Engine_POSTGRES, true)
+
+ // Verify: users table should have 3 columns
+ usersSchema := newMeta.GetSchemaMetadata("public")
+ require.NotNil(t, usersSchema)
+ usersTbl := usersSchema.GetTable("users")
+ require.NotNil(t, usersTbl)
+ require.Equal(t, 3, len(usersTbl.GetProto().Columns), "users should have id, name, email")
+
+ // Verify: users should have 2 indexes (pkey + email)
+ require.GreaterOrEqual(t, len(usersTbl.GetProto().Indexes), 2)
+
+ // Verify: posts table should exist
+ postsTbl := usersSchema.GetTable("posts")
+ require.NotNil(t, postsTbl, "posts table should exist")
+
+ // Verify: original metadata should NOT have posts or email column
+ origMeta := model.NewDatabaseMetadata(meta, nil, nil, storepb.Engine_POSTGRES, true)
+ origUsers := origMeta.GetSchemaMetadata("public").GetTable("users")
+ require.Equal(t, 2, len(origUsers.GetProto().Columns), "original should still have 2 columns")
+ require.Nil(t, origMeta.GetSchemaMetadata("public").GetTable("posts"), "original should not have posts")
+}
+
+// TestClone_SearchPath tests Clone preserves search path and session user.
+func TestClone_SearchPath(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "postgres",
+ Schemas: []*storepb.SchemaMetadata{
+ {Name: "public"},
+ {Name: "alice"},
+ },
+ }
+
+ catBefore := catalog.New()
+ catBefore.SetSessionUser("alice")
+ catBefore.SetSearchPath([]string{"$user", "public"})
+ err := loadWalkThroughCatalog(context.Background(), catBefore, meta)
+ require.NoError(t, err)
+
+ catAfter := catBefore.Clone()
+
+ // CREATE TABLE without schema should use search path ($user → alice)
+ _, err = catAfter.Exec(`CREATE TABLE my_table (id int);`, nil)
+ require.NoError(t, err)
+
+ // Table should be in alice schema on clone
+ require.NotNil(t, catAfter.GetRelation("alice", "my_table"))
+
+ // Original should NOT have it
+ require.Nil(t, catBefore.GetRelation("alice", "my_table"))
+
+ // Diff should show the new table
+ diff := catalog.Diff(catBefore, catAfter)
+ require.False(t, diff.IsEmpty())
+
+ foundAliceTable := false
+ for _, rel := range diff.Relations {
+ if rel.SchemaName == "alice" && rel.Name == "my_table" && rel.Action == catalog.DiffAdd {
+ foundAliceTable = true
+ }
+ }
+ require.True(t, foundAliceTable, "diff should show alice.my_table as added")
+}
+
+// TestClone_WalkThroughOmniFunction tests the actual WalkThroughOmni function
+// using Clone (if enabled) produces correct FinalMetadata.
+func TestClone_WalkThroughOmniFunction(t *testing.T) {
+ meta := &storepb.DatabaseSchemaMetadata{
+ Name: "postgres",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "test",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer", Position: 1},
+ {Name: "name", Type: "text", Position: 2, Nullable: true},
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {Name: "test_pkey", Expressions: []string{"id"}, Unique: true, Primary: true},
+ },
+ },
+ },
+ },
+ },
+ }
+
+ state := model.NewDatabaseMetadata(meta, nil, nil, storepb.Engine_POSTGRES, true)
+ ctx := schema.WalkThroughContext{
+ RawSQL: `CREATE TABLE public.new_table (id int PRIMARY KEY, val text);`,
+ }
+
+ advice := WalkThroughOmni(ctx, state, nil)
+ require.Nil(t, advice, "walk-through should succeed")
+
+ // Check FinalMetadata has the new table
+ newTbl := state.GetSchemaMetadata("public").GetTable("new_table")
+ require.NotNil(t, newTbl, "new_table should exist in FinalMetadata")
+
+ // Check original table is preserved
+ origTbl := state.GetSchemaMetadata("public").GetTable("test")
+ require.NotNil(t, origTbl, "test table should still exist")
+ require.Equal(t, 2, len(origTbl.GetProto().Columns))
+}
+
// compositeWalkThroughMetadata has an adversarial name pair (aa_nested sorts
// before its dependency zz_base) plus an enum-referencing composite and a
// table using one, so a successful load proves dependency-ordered install.
diff --git a/backend/plugin/schema/tidb/generate_migration_testcontainer_test.go b/backend/plugin/schema/tidb/generate_migration_test.go
similarity index 100%
rename from backend/plugin/schema/tidb/generate_migration_testcontainer_test.go
rename to backend/plugin/schema/tidb/generate_migration_test.go
diff --git a/backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go b/backend/plugin/schema/tidb/get_database_definition_test.go
similarity index 100%
rename from backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go
rename to backend/plugin/schema/tidb/get_database_definition_test.go
diff --git a/backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/tidb/get_database_metadata_test.go
similarity index 100%
rename from backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go
rename to backend/plugin/schema/tidb/get_database_metadata_test.go