Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(pg): remove legacy ANTLR SDL diff/migration code [BYT-9010] (#19733)
rebelice8bc2e7b91c0c
diff --git a/backend/plugin/schema/pg/camelcase_index_test.go b/backend/plugin/schema/pg/camelcase_index_test.goindex 3b817e4d2c..6e9e9c51b3 100644--- a/backend/plugin/schema/pg/camelcase_index_test.go+++ b/backend/plugin/schema/pg/camelcase_index_test.go@@ -32,38 +32,6 @@ func TestQuoteIndexExpression(t *testing.T) {}}-// TestCamelCaseIndexColumnQuoting_SDL reproduces https://github.com/bytebase/bytebase/issues/19348-// via the SDL diff path (AST-based).-func TestCamelCaseIndexColumnQuoting_SDL(t *testing.T) {- previousSDL := ""-- currentSDL := `-CREATE TABLE "public"."ba_account" (- "userId" text NOT NULL,- "accountName" text-);--CREATE INDEX ba_account_userId_idx ON "public"."ba_account" ("userId");-`-- currentSchema := model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{},- }, nil, nil, storepb.Engine_POSTGRES, false)-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- ddl, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated DDL:\n%s", ddl)-- require.Contains(t, ddl, `("userId")`, "CamelCase column in CREATE INDEX must be quoted")-}-// TestCamelCaseIndexColumnQuoting_SyncSchema reproduces https://github.com/bytebase/bytebase/issues/19348// via the Sync Schema path: the source schema is exported (with proper quoting), then// parsed into metadata by GetDatabaseMetadata (which calls generateIndexDefinition).diff --git a/backend/plugin/schema/pg/chunk_extraction_test.go b/backend/plugin/schema/pg/chunk_extraction_test.godeleted file mode 100644index 620cf7aef8..0000000000--- a/backend/plugin/schema/pg/chunk_extraction_test.go+++ /dev/null@@ -1,357 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/antlr4-go/antlr/v4"- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- parser "github.com/bytebase/parser/postgresql"-- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestChunkSDLTextExtraction(t *testing.T) {- testCases := []struct {- name string- sdlText string- expectedResults map[string]string // chunk type:identifier -> expected text- }{- {- name: "Single table extraction",- sdlText: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL-);`,- expectedResults: map[string]string{- "TABLE:public.users": `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL-)`,- },- },- {- name: "Multiple statements extraction",- sdlText: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL-);--CREATE INDEX idx_users_name ON users(name);--CREATE SEQUENCE user_seq START 1;`,- expectedResults: map[string]string{- "TABLE:public.users": `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL-)`,- "INDEX:public.idx_users_name": `CREATE INDEX idx_users_name ON users(name)`,- "SEQUENCE:public.user_seq": `CREATE SEQUENCE user_seq START 1`,- },- },- {- name: "Function extraction",- sdlText: `CREATE FUNCTION get_user_count() RETURNS INTEGER AS $$-BEGIN- RETURN (SELECT COUNT(*) FROM users);-END;-$$ LANGUAGE plpgsql;`,- expectedResults: map[string]string{- "FUNCTION:public.get_user_count()": `CREATE FUNCTION get_user_count() RETURNS INTEGER AS $$-BEGIN- RETURN (SELECT COUNT(*) FROM users);-END;-$$ LANGUAGE plpgsql`,- },- },- {- name: "View with qualified name",- sdlText: `CREATE VIEW public.active_users AS-SELECT * FROM users WHERE active = true;`,- expectedResults: map[string]string{- "VIEW:public.active_users": `CREATE VIEW public.active_users AS-SELECT * FROM users WHERE active = true`,- },- },- {- name: "Complex mixed statements",- sdlText: `CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- price DECIMAL(10,2)-);--CREATE VIEW product_summary AS-SELECT id, name, price-FROM products-WHERE price > 0;--CREATE INDEX idx_products_price ON products(price);--CREATE FUNCTION calculate_discount(amount DECIMAL) RETURNS DECIMAL AS $$-BEGIN- RETURN amount * 0.9;-END;-$$ LANGUAGE plpgsql;--CREATE SEQUENCE product_seq START 100;`,- expectedResults: map[string]string{- "TABLE:public.products": `CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- price DECIMAL(10,2)-)`,- "VIEW:public.product_summary": `CREATE VIEW product_summary AS-SELECT id, name, price-FROM products-WHERE price > 0`,- "INDEX:public.idx_products_price": `CREATE INDEX idx_products_price ON products(price)`,- "FUNCTION:public.calculate_discount(amount numeric)": `CREATE FUNCTION calculate_discount(amount DECIMAL) RETURNS DECIMAL AS $$-BEGIN- RETURN amount * 0.9;-END;-$$ LANGUAGE plpgsql`,- "SEQUENCE:public.product_seq": `CREATE SEQUENCE product_seq START 100`,- },- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- // Extract chunks from SDL text- chunks, err := ChunkSDLText(tc.sdlText)- require.NoError(t, err, "Should successfully chunk SDL text")- require.NotNil(t, chunks, "Chunks should not be nil")- // Token stream is no longer stored in chunks, obtained from AST nodes directly-- // Build a map of all chunks with type:identifier keys- allChunks := make(map[string]*schema.SDLChunk)- for id, chunk := range chunks.Tables {- allChunks["TABLE:"+id] = chunk- }- for id, chunk := range chunks.Views {- allChunks["VIEW:"+id] = chunk- }- for id, chunk := range chunks.Functions {- allChunks["FUNCTION:"+id] = chunk- }- for id, chunk := range chunks.Indexes {- allChunks["INDEX:"+id] = chunk- }- for id, chunk := range chunks.Sequences {- allChunks["SEQUENCE:"+id] = chunk- }-- // Verify we have the expected number of chunks- assert.Equal(t, len(tc.expectedResults), len(allChunks),- "Expected %d chunks, got %d", len(tc.expectedResults), len(allChunks))-- // Verify each expected chunk- for expectedKey, expectedText := range tc.expectedResults {- chunk, exists := allChunks[expectedKey]- require.True(t, exists, "Expected chunk with key %s not found", expectedKey)-- // Verify chunk has required fields- assert.NotEmpty(t, chunk.Identifier, "Chunk identifier should not be empty")- assert.NotNil(t, chunk.ASTNode, "Chunk AST node should not be nil")-- // Extract text using GetText method and compare- actualText := chunk.GetText()- require.NotEmpty(t, actualText, "GetText should return non-empty text")-- // Normalize whitespace for comparison- actualTextNormalized := strings.TrimSpace(actualText)- expectedTextNormalized := strings.TrimSpace(expectedText)-- assert.Equal(t, expectedTextNormalized, actualTextNormalized,- "Text mismatch for chunk %s.\nExpected:\n%s\nActual:\n%s",- expectedKey, expectedTextNormalized, actualTextNormalized)-- // Verify AST node type matches chunk type- switch {- case strings.HasPrefix(expectedKey, "TABLE:"):- _, ok := chunk.ASTNode.(*parser.CreatestmtContext)- assert.True(t, ok, "TABLE chunk should have CreatestmtContext AST node")- case strings.HasPrefix(expectedKey, "INDEX:"):- _, ok := chunk.ASTNode.(*parser.IndexstmtContext)- assert.True(t, ok, "INDEX chunk should have IndexstmtContext AST node")- case strings.HasPrefix(expectedKey, "SEQUENCE:"):- _, ok := chunk.ASTNode.(*parser.CreateseqstmtContext)- assert.True(t, ok, "SEQUENCE chunk should have CreateseqstmtContext AST node")- case strings.HasPrefix(expectedKey, "FUNCTION:"):- _, ok := chunk.ASTNode.(*parser.CreatefunctionstmtContext)- assert.True(t, ok, "FUNCTION chunk should have CreatefunctionstmtContext AST node")- case strings.HasPrefix(expectedKey, "VIEW:"):- _, ok := chunk.ASTNode.(*parser.ViewstmtContext)- assert.True(t, ok, "VIEW chunk should have ViewstmtContext AST node")- default:- assert.Fail(t, "Unknown chunk type: %s", expectedKey)- }- }- })- }-}--func TestChunkSDLTextEdgeCases(t *testing.T) {- testCases := []struct {- name string- sdlText string- expectedChunks int- shouldError bool- }{- {- name: "Empty SDL text",- sdlText: "",- expectedChunks: 0,- shouldError: false,- },- {- name: "Whitespace only",- sdlText: " \n\t ",- expectedChunks: 0,- shouldError: false,- },- {- name: "CREATE INDEX without name (should be skipped)",- sdlText: `CREATE TABLE users (id SERIAL);-CREATE INDEX ON users(id);`,- expectedChunks: 1, // Only table, index without name is skipped- shouldError: false,- },- {- name: "Schema-qualified identifiers",- sdlText: `CREATE TABLE public.users (id SERIAL);-CREATE TABLE admin.settings (key VARCHAR);`,- expectedChunks: 2,- shouldError: false,- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- chunks, err := ChunkSDLText(tc.sdlText)-- if tc.shouldError {- assert.Error(t, err, "Should return error")- return- }-- require.NoError(t, err, "Should not return error")- require.NotNil(t, chunks, "Chunks should not be nil")-- // Count total chunks- totalChunks := len(chunks.Tables) + len(chunks.Views) + len(chunks.Functions) +- len(chunks.Indexes) + len(chunks.Sequences)-- assert.Equal(t, tc.expectedChunks, totalChunks,- "Expected %d chunks, got %d", tc.expectedChunks, totalChunks)- })- }-}--func TestChunkGetTextMethod(t *testing.T) {- sdlText := `CREATE TABLE test_table (- id INTEGER PRIMARY KEY,- data TEXT-);`-- chunks, err := ChunkSDLText(sdlText)- require.NoError(t, err)- require.Contains(t, chunks.Tables, "public.test_table")-- chunk := chunks.Tables["public.test_table"]-- // Test GetText method- text := chunk.GetText()- assert.NotEmpty(t, text, "GetText should return non-empty text")- assert.Contains(t, text, "CREATE TABLE test_table", "Text should contain table creation")- assert.Contains(t, text, "id INTEGER PRIMARY KEY", "Text should contain column definition")-- // Test that GetText works with the new token-less approach- // (This test is no longer relevant since we get tokens from AST directly)-}--func TestChunkKeyFormats(t *testing.T) {- sdlText := `CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);-CREATE TABLE admin.users (id SERIAL, role TEXT);-CREATE SEQUENCE user_seq;-CREATE SEQUENCE admin.admin_seq;-CREATE VIEW user_view AS SELECT * FROM users;-CREATE VIEW admin.admin_view AS SELECT * FROM admin.users;-CREATE INDEX idx_users_name ON users(name);`-- chunks, err := ChunkSDLText(sdlText)- require.NoError(t, err)- require.NotNil(t, chunks)-- // Check that all chunk keys use schema.objectName format- expectedTableKeys := []string{"public.users", "admin.users"}- expectedSequenceKeys := []string{"public.user_seq", "admin.admin_seq"}- expectedViewKeys := []string{"public.user_view", "admin.admin_view"}- expectedIndexKeys := []string{"public.idx_users_name"}-- // Verify table chunk keys- require.Equal(t, len(expectedTableKeys), len(chunks.Tables))- for _, key := range expectedTableKeys {- _, exists := chunks.Tables[key]- require.True(t, exists, "Expected table key %s not found", key)- }-- // Verify sequence chunk keys- require.Equal(t, len(expectedSequenceKeys), len(chunks.Sequences))- for _, key := range expectedSequenceKeys {- _, exists := chunks.Sequences[key]- require.True(t, exists, "Expected sequence key %s not found", key)- }-- // Verify view chunk keys- require.Equal(t, len(expectedViewKeys), len(chunks.Views))- for _, key := range expectedViewKeys {- _, exists := chunks.Views[key]- require.True(t, exists, "Expected view key %s not found", key)- }-- // Verify index chunk keys- require.Equal(t, len(expectedIndexKeys), len(chunks.Indexes))- for _, key := range expectedIndexKeys {- _, exists := chunks.Indexes[key]- require.True(t, exists, "Expected index key %s not found", key)- }-}--// TestNewTableASTParsing tests that new tables created during drift detection-// have proper AST nodes that can generate correct text-func TestNewTableASTParsing(t *testing.T) {- // Test the createTableExtractor functionality- tableSDL := `CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text NOT NULL-);`-- parseResults, err := pgparser.ParsePostgreSQL(tableSDL)- require.NoError(t, err)- require.Len(t, parseResults, 1, "Should parse single statement")-- // Extract the CREATE TABLE AST node- var createTableNode *parser.CreatestmtContext- antlr.ParseTreeWalkerDefault.Walk(&createTableExtractor{- result: &createTableNode,- }, parseResults[0].Tree)-- require.NotNil(t, createTableNode, "Should extract CREATE TABLE AST node")-- // Create chunk with the AST node- chunk := &schema.SDLChunk{- Identifier: "public.users",- ASTNode: createTableNode,- }-- // Verify we can extract text from the AST node- text := chunk.GetText()- require.NotEmpty(t, text)- require.Contains(t, strings.ToUpper(text), "CREATE TABLE")- require.Contains(t, text, "users")-}diff --git a/backend/plugin/schema/pg/column_migration_integration_test.go b/backend/plugin/schema/pg/column_migration_integration_test.godeleted file mode 100644index 8bdec7564b..0000000000--- a/backend/plugin/schema/pg/column_migration_integration_test.go+++ /dev/null@@ -1,463 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestColumnSDLDiffAndMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Add new column",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(320) UNIQUE- );- `,- expectedMigration: `ALTER TABLE "public"."users" ADD COLUMN email VARCHAR(320) UNIQUE;--`,- },- {- name: "Drop column",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- phone VARCHAR(20)- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."users" DROP COLUMN IF EXISTS "phone";--`,- },- {- name: "Modify column (ALTER TYPE)",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL- );- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(15,3) NOT NULL DEFAULT 0.00- );- `,- expectedMigration: `ALTER TABLE "public"."products" ALTER COLUMN "price" TYPE DECIMAL(15,3);-ALTER TABLE "public"."products" ALTER COLUMN "price" SET DEFAULT 0.00;--`,- },- {- name: "Modify column nullable",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(320)- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(320) NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."users" ALTER COLUMN "email" SET NOT NULL;--`,- },- {- name: "Drop column nullable constraint",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(320) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(320)- );- `,- expectedMigration: `ALTER TABLE "public"."users" ALTER COLUMN "email" DROP NOT NULL;--`,- },- {- name: "Add default value to existing column",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL- );- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL DEFAULT 9.99- );- `,- expectedMigration: `ALTER TABLE "public"."products" ALTER COLUMN "price" SET DEFAULT 9.99;--`,- },- {- name: "Drop default value from column",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL DEFAULT 9.99- );- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."products" ALTER COLUMN "price" DROP DEFAULT;--`,- },- {- name: "Change column type only",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(100) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."users" ALTER COLUMN "name" TYPE VARCHAR(255);--`,- },- {- name: "Multiple column changes in same table",- previousSDL: `- CREATE TABLE employees (- id SERIAL PRIMARY KEY,- name VARCHAR(100),- email VARCHAR(255) NOT NULL,- salary DECIMAL(8,2)- );- `,- currentSDL: `- CREATE TABLE employees (- id SERIAL PRIMARY KEY,- name VARCHAR(200) NOT NULL,- email VARCHAR(320) NOT NULL,- salary DECIMAL(10,2) DEFAULT 50000.00- );- `,- expectedMigration: `ALTER TABLE "public"."employees" ALTER COLUMN "name" TYPE VARCHAR(200);-ALTER TABLE "public"."employees" ALTER COLUMN "name" SET NOT NULL;-ALTER TABLE "public"."employees" ALTER COLUMN "email" TYPE VARCHAR(320);-ALTER TABLE "public"."employees" ALTER COLUMN "salary" TYPE DECIMAL(10,2);-ALTER TABLE "public"."employees" ALTER COLUMN "salary" SET DEFAULT 50000.00;--`,- },- {- name: "Add column with complex constraints",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL- );- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- status VARCHAR(20) NOT NULL DEFAULT 'pending'- );- `,- expectedMigration: `ALTER TABLE "public"."orders" ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'pending';--`,- },- {- name: "Change column from nullable to not null with default",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- category VARCHAR(50)- );- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- category VARCHAR(50) NOT NULL DEFAULT 'general'- );- `,- expectedMigration: `ALTER TABLE "public"."products" ALTER COLUMN "category" SET NOT NULL;-ALTER TABLE "public"."products" ALTER COLUMN "category" SET DEFAULT 'general';--`,- },- {- name: "Complex type change with precision",- previousSDL: `- CREATE TABLE measurements (- id SERIAL PRIMARY KEY,- value DECIMAL(5,2)- );- `,- currentSDL: `- CREATE TABLE measurements (- id SERIAL PRIMARY KEY,- value DECIMAL(10,4) NOT NULL DEFAULT 0.0000- );- `,- expectedMigration: `ALTER TABLE "public"."measurements" ALTER COLUMN "value" TYPE DECIMAL(10,4);-ALTER TABLE "public"."measurements" ALTER COLUMN "value" SET NOT NULL;-ALTER TABLE "public"."measurements" ALTER COLUMN "value" SET DEFAULT 0.0000;--`,- },- {- name: "Add multiple columns",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(320) UNIQUE,- created_at TIMESTAMP DEFAULT NOW()- );- `,- expectedMigration: `ALTER TABLE "public"."users" ADD COLUMN email VARCHAR(320) UNIQUE;-ALTER TABLE "public"."users" ADD COLUMN created_at TIMESTAMP DEFAULT NOW();--`,- },- {- name: "Drop multiple columns",- previousSDL: `- CREATE TABLE temp_data (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- temp_field1 VARCHAR(100),- temp_field2 INTEGER,- description TEXT- );- `,- currentSDL: `- CREATE TABLE temp_data (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- description TEXT- );- `,- expectedMigration: `ALTER TABLE "public"."temp_data" DROP COLUMN IF EXISTS "temp_field1";-ALTER TABLE "public"."temp_data" DROP COLUMN IF EXISTS "temp_field2";--`,- },- {- name: "Mixed operations: add, drop, modify",- previousSDL: `- CREATE TABLE inventory (- id SERIAL PRIMARY KEY,- product_name VARCHAR(100) NOT NULL,- old_field VARCHAR(50),- quantity INTEGER DEFAULT 0- );- `,- currentSDL: `- CREATE TABLE inventory (- id SERIAL PRIMARY KEY,- product_name VARCHAR(200) NOT NULL,- quantity INTEGER DEFAULT 1,- location VARCHAR(100) NOT NULL DEFAULT 'warehouse'- );- `,- expectedMigration: `ALTER TABLE "public"."inventory" DROP COLUMN IF EXISTS "old_field";--ALTER TABLE "public"."inventory" ADD COLUMN location VARCHAR(100) NOT NULL DEFAULT 'warehouse';-ALTER TABLE "public"."inventory" ALTER COLUMN "product_name" TYPE VARCHAR(200);-ALTER TABLE "public"."inventory" ALTER COLUMN "quantity" SET DEFAULT 1;--`,- },- {- name: "Integer type variations",- previousSDL: `- CREATE TABLE counters (- id SERIAL PRIMARY KEY,- small_count SMALLINT,- big_count BIGINT- );- `,- currentSDL: `- CREATE TABLE counters (- id SERIAL PRIMARY KEY,- small_count INTEGER NOT NULL DEFAULT 0,- big_count BIGINT NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."counters" ALTER COLUMN "small_count" TYPE INTEGER;-ALTER TABLE "public"."counters" ALTER COLUMN "small_count" SET NOT NULL;-ALTER TABLE "public"."counters" ALTER COLUMN "small_count" SET DEFAULT 0;-ALTER TABLE "public"."counters" ALTER COLUMN "big_count" SET NOT NULL;--`,- },- {- name: "Text and character types",- previousSDL: `- CREATE TABLE documents (- id SERIAL PRIMARY KEY,- title CHAR(50),- content TEXT- );- `,- currentSDL: `- CREATE TABLE documents (- id SERIAL PRIMARY KEY,- title VARCHAR(100) NOT NULL,- content TEXT DEFAULT ''- );- `,- expectedMigration: `ALTER TABLE "public"."documents" ALTER COLUMN "title" TYPE VARCHAR(100);-ALTER TABLE "public"."documents" ALTER COLUMN "title" SET NOT NULL;-ALTER TABLE "public"."documents" ALTER COLUMN "content" SET DEFAULT '';--`,- },- {- name: "Boolean column with default",- previousSDL: `- CREATE TABLE features (- id SERIAL PRIMARY KEY,- name VARCHAR(100) NOT NULL- );- `,- currentSDL: `- CREATE TABLE features (- id SERIAL PRIMARY KEY,- name VARCHAR(100) NOT NULL,- enabled BOOLEAN NOT NULL DEFAULT false- );- `,- expectedMigration: `ALTER TABLE "public"."features" ADD COLUMN enabled BOOLEAN NOT NULL DEFAULT false;--`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Step 1: Get SDL diff using AST-only mode (no metadata extraction)- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Step 2: Verify that diff contains column changes with AST nodes- if len(diff.TableChanges) > 0 {- for _, tableDiff := range diff.TableChanges {- for _, colDiff := range tableDiff.ColumnChanges {- switch colDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, colDiff.NewASTNode,- "Create action should have NewASTNode")- assert.Nil(t, colDiff.OldASTNode,- "Create action should not have OldASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, colDiff.NewColumn,- "AST-only mode should not have metadata")- case schema.MetadataDiffActionDrop:- assert.NotNil(t, colDiff.OldASTNode,- "Drop action should have OldASTNode")- assert.Nil(t, colDiff.NewASTNode,- "Drop action should not have NewASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, colDiff.OldColumn,- "AST-only mode should not have metadata")- case schema.MetadataDiffActionAlter:- assert.NotNil(t, colDiff.OldASTNode,- "Alter action should have OldASTNode")- assert.NotNil(t, colDiff.NewASTNode,- "Alter action should have NewASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, colDiff.OldColumn,- "AST-only mode should not have metadata")- assert.Nil(t, colDiff.NewColumn,- "AST-only mode should not have metadata")- default:- // Other actions- t.Logf("Encountered column action: %v", colDiff.Action)- }- }- }- }-- // Step 3: Generate migration SQL using AST nodes- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // Step 4: Verify the generated migration matches expectations- assert.Equal(t, tt.expectedMigration, migrationSQL,- "Generated migration SQL should match expected output")-- // Step 5: Verify the migration contains expected keywords for columns- if tt.expectedMigration != "" {- if containsColumnString(tt.expectedMigration, "ADD COLUMN") {- assert.Contains(t, migrationSQL, "ADD COLUMN",- "Migration should contain ADD COLUMN statement")- }- if containsColumnString(tt.expectedMigration, "DROP COLUMN") {- assert.Contains(t, migrationSQL, "DROP COLUMN",- "Migration should contain DROP COLUMN statement")- }- if containsColumnString(tt.expectedMigration, "ALTER COLUMN") {- assert.Contains(t, migrationSQL, "ALTER COLUMN",- "Migration should contain ALTER COLUMN statement")- }- }- })- }-}--// Helper function to check if a string contains a substring-func containsColumnString(s, substr string) bool {- return strings.Contains(s, substr)-}diff --git a/backend/plugin/schema/pg/column_sdl_diff_test.go b/backend/plugin/schema/pg/column_sdl_diff_test.godeleted file mode 100644index ac74ac8482..0000000000--- a/backend/plugin/schema/pg/column_sdl_diff_test.go+++ /dev/null@@ -1,733 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- parser "github.com/bytebase/parser/postgresql"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// TestColumnSDLDiff provides comprehensive testing for column-related SDL differences-func TestColumnSDLDiff(t *testing.T) {- testCases := []struct {- name string- currentSDL string- previousSDL string- expectedTableChanges int- expectedColumnChanges int- expectedCreateColumns []string- expectedAlterColumns []string- expectedDropColumns []string- validateColumnDiffs func(t *testing.T, columnDiffs []*schema.ColumnDiff)- validateTableDiffs func(t *testing.T, tableDiffs []*schema.TableDiff)- }{- {- name: "No column changes - identical tables",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- expectedTableChanges: 0,- expectedColumnChanges: 0,- expectedCreateColumns: []string{},- expectedAlterColumns: []string{},- expectedDropColumns: []string{},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- assert.Len(t, columnDiffs, 0, "Should have no column changes for identical tables")- },- },- {- name: "Add new column",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(100)- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- expectedTableChanges: 1,- expectedColumnChanges: 1,- expectedCreateColumns: []string{"email"},- expectedAlterColumns: []string{},- expectedDropColumns: []string{},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 1, "Should have exactly one column change")-- columnDiff := columnDiffs[0]- assert.Equal(t, schema.MetadataDiffActionCreate, columnDiff.Action, "Should be CREATE action")- assert.Nil(t, columnDiff.OldColumn, "CREATE should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "CREATE should have nil NewColumn in AST-only mode")-- // Verify AST nodes- assert.Nil(t, columnDiff.OldASTNode, "CREATE should have nil OldASTNode")- assert.NotNil(t, columnDiff.NewASTNode, "CREATE should have NewASTNode")-- // Extract column name from AST for verification- columnName := extractColumnNameFromAST(columnDiff.NewASTNode)- assert.Equal(t, "email", columnName, "Column name should match")- },- },- {- name: "Drop column",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(100)- );`,- expectedTableChanges: 1,- expectedColumnChanges: 1,- expectedCreateColumns: []string{},- expectedAlterColumns: []string{},- expectedDropColumns: []string{"email"},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 1, "Should have exactly one column change")-- columnDiff := columnDiffs[0]- assert.Equal(t, schema.MetadataDiffActionDrop, columnDiff.Action, "Should be DROP action")- assert.Nil(t, columnDiff.OldColumn, "DROP should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "DROP should have nil NewColumn")-- // Verify AST nodes- assert.NotNil(t, columnDiff.OldASTNode, "DROP should have OldASTNode")- assert.Nil(t, columnDiff.NewASTNode, "DROP should have nil NewASTNode")-- // Extract column name from AST for verification- columnName := extractColumnNameFromAST(columnDiff.OldASTNode)- assert.Equal(t, "email", columnName, "Column name should match")- },- },- {- name: "Alter column type",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name TEXT NOT NULL- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- expectedTableChanges: 1,- expectedColumnChanges: 1,- expectedCreateColumns: []string{},- expectedAlterColumns: []string{"name"},- expectedDropColumns: []string{},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 1, "Should have exactly one column change")-- columnDiff := columnDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, columnDiff.Action, "Should be ALTER action")- assert.Nil(t, columnDiff.OldColumn, "ALTER should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "ALTER should have nil NewColumn in AST-only mode")-- // Verify AST nodes- assert.NotNil(t, columnDiff.OldASTNode, "ALTER should have OldASTNode")- assert.NotNil(t, columnDiff.NewASTNode, "ALTER should have NewASTNode")-- // Extract column names from AST for verification- oldColumnName := extractColumnNameFromAST(columnDiff.OldASTNode)- newColumnName := extractColumnNameFromAST(columnDiff.NewASTNode)- assert.Equal(t, "name", oldColumnName, "Old column name should match")- assert.Equal(t, "name", newColumnName, "New column name should match")-- // Verify AST text extraction- oldText := getColumnText(columnDiff.OldASTNode)- newText := getColumnText(columnDiff.NewASTNode)- assert.Contains(t, oldText, "VARCHAR(255)", "Old AST should contain VARCHAR(255)")- assert.Contains(t, newText, "TEXT", "New AST should contain TEXT")- },- },- {- name: "Multiple column changes",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name TEXT NOT NULL,- email VARCHAR(200),- created_at TIMESTAMP- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- phone VARCHAR(20),- updated_at TIMESTAMP- );`,- expectedTableChanges: 1,- expectedColumnChanges: 5,- expectedCreateColumns: []string{"email", "created_at"},- expectedAlterColumns: []string{"name"},- expectedDropColumns: []string{"phone", "updated_at"},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 5, "Should have exactly 5 column changes")-- // Count each action type- createCount := 0- alterCount := 0- dropCount := 0-- createColumns := []string{}- alterColumns := []string{}- dropColumns := []string{}-- for _, columnDiff := range columnDiffs {- switch columnDiff.Action {- case schema.MetadataDiffActionCreate:- createCount++- columnName := extractColumnNameFromAST(columnDiff.NewASTNode)- createColumns = append(createColumns, columnName)- assert.Nil(t, columnDiff.OldColumn, "CREATE should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "CREATE should have nil NewColumn in AST-only mode")- assert.Nil(t, columnDiff.OldASTNode, "CREATE should have nil OldASTNode")- assert.NotNil(t, columnDiff.NewASTNode, "CREATE should have NewASTNode")- case schema.MetadataDiffActionAlter:- alterCount++- columnName := extractColumnNameFromAST(columnDiff.NewASTNode)- alterColumns = append(alterColumns, columnName)- assert.Nil(t, columnDiff.OldColumn, "ALTER should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "ALTER should have nil NewColumn in AST-only mode")- assert.NotNil(t, columnDiff.OldASTNode, "ALTER should have OldASTNode")- assert.NotNil(t, columnDiff.NewASTNode, "ALTER should have NewASTNode")- case schema.MetadataDiffActionDrop:- dropCount++- columnName := extractColumnNameFromAST(columnDiff.OldASTNode)- dropColumns = append(dropColumns, columnName)- assert.Nil(t, columnDiff.OldColumn, "DROP should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "DROP should have nil NewColumn")- assert.NotNil(t, columnDiff.OldASTNode, "DROP should have OldASTNode")- assert.Nil(t, columnDiff.NewASTNode, "DROP should have nil NewASTNode")- default:- t.Fatalf("Unexpected column diff action: %v", columnDiff.Action)- }- }-- assert.Equal(t, 2, createCount, "Should have 2 CREATE actions")- assert.Equal(t, 1, alterCount, "Should have 1 ALTER action")- assert.Equal(t, 2, dropCount, "Should have 2 DROP actions")-- assert.ElementsMatch(t, []string{"email", "created_at"}, createColumns, "CREATE columns should match")- assert.ElementsMatch(t, []string{"name"}, alterColumns, "ALTER columns should match")- assert.ElementsMatch(t, []string{"phone", "updated_at"}, dropColumns, "DROP columns should match")- },- },- {- name: "Column constraint changes",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- expectedTableChanges: 1,- expectedColumnChanges: 1,- expectedCreateColumns: []string{},- expectedAlterColumns: []string{"name"},- expectedDropColumns: []string{},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 1, "Should have exactly one column change")-- columnDiff := columnDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, columnDiff.Action, "Should be ALTER action")- assert.Nil(t, columnDiff.OldColumn, "Should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "Should have nil NewColumn in AST-only mode")-- // Verify AST nodes are available- assert.NotNil(t, columnDiff.OldASTNode, "Should have OldASTNode")- assert.NotNil(t, columnDiff.NewASTNode, "Should have NewASTNode")-- // Check constraint differences via AST text- oldText := getColumnText(columnDiff.OldASTNode)- newText := getColumnText(columnDiff.NewASTNode)- assert.Contains(t, oldText, "NOT NULL", "Old AST should contain NOT NULL")- assert.NotContains(t, newText, "NOT NULL", "New AST should not contain NOT NULL")- },- },- {- name: "Column default value changes",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- status VARCHAR(50) DEFAULT 'inactive'- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- status VARCHAR(50) DEFAULT 'active'- );`,- expectedTableChanges: 1,- expectedColumnChanges: 1,- expectedCreateColumns: []string{},- expectedAlterColumns: []string{"status"},- expectedDropColumns: []string{},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 1, "Should have exactly one column change")-- columnDiff := columnDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, columnDiff.Action, "Should be ALTER action")- assert.Nil(t, columnDiff.OldColumn, "Should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "Should have nil NewColumn in AST-only mode")-- // Verify AST nodes are available- assert.NotNil(t, columnDiff.OldASTNode, "Should have OldASTNode")- assert.NotNil(t, columnDiff.NewASTNode, "Should have NewASTNode")-- // Check default value differences via AST text- oldText := getColumnText(columnDiff.OldASTNode)- newText := getColumnText(columnDiff.NewASTNode)- assert.Contains(t, oldText, "'active'", "Old AST should contain 'active'")- assert.Contains(t, newText, "'inactive'", "New AST should contain 'inactive'")- },- },- {- name: "Column collation changes",- currentSDL: `CREATE TABLE users (- name VARCHAR(255) COLLATE "en_US"- );`,- previousSDL: `CREATE TABLE users (- name VARCHAR(255) COLLATE "C"- );`,- expectedTableChanges: 1,- expectedColumnChanges: 1,- expectedCreateColumns: []string{},- expectedAlterColumns: []string{"name"},- expectedDropColumns: []string{},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 1, "Should have exactly one column change")-- columnDiff := columnDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, columnDiff.Action, "Should be ALTER action")- assert.Nil(t, columnDiff.OldColumn, "Should have nil OldColumn in AST-only mode")- assert.Nil(t, columnDiff.NewColumn, "Should have nil NewColumn in AST-only mode")-- // Verify AST nodes are available- assert.NotNil(t, columnDiff.OldASTNode, "Should have OldASTNode")- assert.NotNil(t, columnDiff.NewASTNode, "Should have NewASTNode")-- // Check collation differences via AST text- oldText := getColumnText(columnDiff.OldASTNode)- newText := getColumnText(columnDiff.NewASTNode)- assert.Contains(t, oldText, `"C"`, "Old AST should contain 'C' collation")- assert.Contains(t, newText, `"en_US"`, "New AST should contain 'en_US' collation")- },- },- {- name: "Whitespace differences detected by text comparison",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- expectedTableChanges: 1,- expectedColumnChanges: 1,- expectedCreateColumns: []string{},- expectedAlterColumns: []string{"id"},- expectedDropColumns: []string{},- validateColumnDiffs: func(t *testing.T, columnDiffs []*schema.ColumnDiff) {- require.Len(t, columnDiffs, 1, "Should detect whitespace difference")-- columnDiff := columnDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, columnDiff.Action, "Should be ALTER action")- columnName := extractColumnNameFromAST(columnDiff.NewASTNode)- assert.Equal(t, "id", columnName, "Column name should match")-- // Verify that text comparison detects the difference- oldText := getColumnText(columnDiff.OldASTNode)- newText := getColumnText(columnDiff.NewASTNode)- assert.NotEqual(t, oldText, newText, "AST text should be different due to whitespace")- },- },- {- name: "Add foreign key constraint",- currentSDL: `CREATE TABLE orders (- id INTEGER PRIMARY KEY,- customer_id INTEGER,- CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers(id)- );`,- previousSDL: `CREATE TABLE orders (- id INTEGER PRIMARY KEY,- customer_id INTEGER- );`,- expectedTableChanges: 1,- validateTableDiffs: func(t *testing.T, tableDiffs []*schema.TableDiff) {- assert.Len(t, tableDiffs, 1, "Should have 1 table diff")- tableDiff := tableDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)- assert.Len(t, tableDiff.ForeignKeyChanges, 1, "Should have 1 FK change")- fkChange := tableDiff.ForeignKeyChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, fkChange.Action)- // AST node should be present for created FK- assert.NotNil(t, fkChange.NewASTNode)- assert.Nil(t, fkChange.OldASTNode)- },- },- {- name: "Add check constraint",- currentSDL: `CREATE TABLE products (- id INTEGER PRIMARY KEY,- price DECIMAL(10,2),- CONSTRAINT chk_positive_price CHECK (price > 0)- );`,- previousSDL: `CREATE TABLE products (- id INTEGER PRIMARY KEY,- price DECIMAL(10,2)- );`,- expectedTableChanges: 1,- validateTableDiffs: func(t *testing.T, tableDiffs []*schema.TableDiff) {- assert.Len(t, tableDiffs, 1, "Should have 1 table diff")- tableDiff := tableDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)- assert.Len(t, tableDiff.CheckConstraintChanges, 1, "Should have 1 check constraint change")- checkChange := tableDiff.CheckConstraintChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, checkChange.Action)- // AST node should be present for created check constraint- assert.NotNil(t, checkChange.NewASTNode)- assert.Nil(t, checkChange.OldASTNode)- },- },- {- name: "Add unique constraint",- currentSDL: `CREATE TABLE users (- id INTEGER PRIMARY KEY,- email VARCHAR(255),- CONSTRAINT uk_users_email UNIQUE (email)- );`,- previousSDL: `CREATE TABLE users (- id INTEGER PRIMARY KEY,- email VARCHAR(255)- );`,- expectedTableChanges: 1,- validateTableDiffs: func(t *testing.T, tableDiffs []*schema.TableDiff) {- assert.Len(t, tableDiffs, 1, "Should have 1 table diff")- tableDiff := tableDiffs[0]- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)- assert.Len(t, tableDiff.UniqueConstraintChanges, 1, "Should have 1 unique constraint change")- uniqueChange := tableDiff.UniqueConstraintChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, uniqueChange.Action)- // AST node should be present for created unique constraint- assert.NotNil(t, uniqueChange.NewASTNode)- assert.Nil(t, uniqueChange.OldASTNode)- },- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- // Call GetSDLDiff- diff, err := GetSDLDiff(tc.currentSDL, tc.previousSDL, nil)- require.NoError(t, err, "GetSDLDiff should not return error")-- // Validate table changes count- assert.Len(t, diff.TableChanges, tc.expectedTableChanges,- "Expected %d table changes, got %d", tc.expectedTableChanges, len(diff.TableChanges))-- if tc.expectedTableChanges == 0 {- return // No further validation needed- }-- // Should have exactly one table change- require.Len(t, diff.TableChanges, 1, "Should have exactly one table change")- tableDiff := diff.TableChanges[0]-- // Validate column changes count- assert.Len(t, tableDiff.ColumnChanges, tc.expectedColumnChanges,- "Expected %d column changes, got %d", tc.expectedColumnChanges, len(tableDiff.ColumnChanges))-- // Validate column changes by action type- if tc.expectedColumnChanges > 0 {- createColumns := []string{}- alterColumns := []string{}- dropColumns := []string{}-- for _, columnChange := range tableDiff.ColumnChanges {- switch columnChange.Action {- case schema.MetadataDiffActionCreate:- if columnChange.NewColumn != nil {- createColumns = append(createColumns, columnChange.NewColumn.Name)- } else if columnChange.NewASTNode != nil {- // AST-only mode: extract column name from AST- columnName := extractColumnNameFromAST(columnChange.NewASTNode)- createColumns = append(createColumns, columnName)- }- case schema.MetadataDiffActionAlter:- if columnChange.NewColumn != nil {- alterColumns = append(alterColumns, columnChange.NewColumn.Name)- } else if columnChange.NewASTNode != nil {- // AST-only mode: extract column name from AST- columnName := extractColumnNameFromAST(columnChange.NewASTNode)- alterColumns = append(alterColumns, columnName)- }- case schema.MetadataDiffActionDrop:- if columnChange.OldColumn != nil {- dropColumns = append(dropColumns, columnChange.OldColumn.Name)- } else if columnChange.OldASTNode != nil {- // AST-only mode: extract column name from AST- columnName := extractColumnNameFromAST(columnChange.OldASTNode)- dropColumns = append(dropColumns, columnName)- }- default:- t.Fatalf("Unexpected column change action: %v", columnChange.Action)- }- }-- // Verify expected columns- assert.ElementsMatch(t, tc.expectedCreateColumns, createColumns, "CREATE columns mismatch")- assert.ElementsMatch(t, tc.expectedAlterColumns, alterColumns, "ALTER columns mismatch")- assert.ElementsMatch(t, tc.expectedDropColumns, dropColumns, "DROP columns mismatch")-- // Run custom validation- if tc.validateColumnDiffs != nil {- tc.validateColumnDiffs(t, tableDiff.ColumnChanges)- }- }-- // Run table-level validation- if tc.validateTableDiffs != nil {- tc.validateTableDiffs(t, diff.TableChanges)- }- })- }-}--// TestColumnMetadataExtraction tests all column metadata fields extraction-func TestColumnMetadataExtraction(t *testing.T) {- testCases := []struct {- name string- sdlText string- expectedName string- expectedType string- expectedNullable bool- expectedDefault string- expectedCollation string- expectedComment string- }{- {- name: "Simple INTEGER column",- sdlText: `CREATE TABLE test (- id INTEGER- );`,- expectedName: "id",- expectedType: "INTEGER",- expectedNullable: true,- expectedDefault: "",- expectedCollation: "",- expectedComment: "",- },- {- name: "VARCHAR with length and nullable",- sdlText: `CREATE TABLE test (- name VARCHAR(255)- );`,- expectedName: "name",- expectedType: "VARCHAR(255)",- expectedNullable: true,- expectedDefault: "",- expectedCollation: "",- expectedComment: "",- },- {- name: "NOT NULL SERIAL column",- sdlText: `CREATE TABLE test (- id SERIAL NOT NULL- );`,- expectedName: "id",- expectedType: "SERIAL",- expectedNullable: false,- expectedDefault: "",- expectedCollation: "",- expectedComment: "",- },- {- name: "PRIMARY KEY column (implicitly NOT NULL)",- sdlText: `CREATE TABLE test (- id INTEGER PRIMARY KEY- );`,- expectedName: "id",- expectedType: "INTEGER",- expectedNullable: false,- expectedDefault: "",- expectedCollation: "",- expectedComment: "",- },- {- name: "DECIMAL with precision",- sdlText: `CREATE TABLE test (- price DECIMAL(10,2)- );`,- expectedName: "price",- expectedType: "DECIMAL(10,2)",- expectedNullable: true,- expectedDefault: "",- expectedCollation: "",- expectedComment: "",- },- {- name: "Column with string default value",- sdlText: `CREATE TABLE test (- status VARCHAR(50) DEFAULT 'active'- );`,- expectedName: "status",- expectedType: "VARCHAR(50)",- expectedNullable: true,- expectedDefault: "'active'",- expectedCollation: "",- expectedComment: "",- },- {- name: "Column with numeric default value",- sdlText: `CREATE TABLE test (- count INTEGER DEFAULT 0- );`,- expectedName: "count",- expectedType: "INTEGER",- expectedNullable: true,- expectedDefault: "0",- expectedCollation: "",- expectedComment: "",- },- {- name: "Column with function default value",- sdlText: `CREATE TABLE test (- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );`,- expectedName: "created_at",- expectedType: "TIMESTAMP",- expectedNullable: true,- expectedDefault: "CURRENT_TIMESTAMP",- expectedCollation: "",- expectedComment: "",- },- {- name: "NOT NULL column with default",- sdlText: `CREATE TABLE test (- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP- );`,- expectedName: "updated_at",- expectedType: "TIMESTAMP",- expectedNullable: false,- expectedDefault: "CURRENT_TIMESTAMP",- expectedCollation: "",- expectedComment: "",- },- {- name: "Column with collation",- sdlText: `CREATE TABLE test (- name VARCHAR(255) COLLATE "C"- );`,- expectedName: "name",- expectedType: "VARCHAR(255)",- expectedNullable: true,- expectedDefault: "",- expectedCollation: `"C"`,- expectedComment: "",- },- {- name: "Column with unquoted collation",- sdlText: `CREATE TABLE test (- name VARCHAR(255) COLLATE C- );`,- expectedName: "name",- expectedType: "VARCHAR(255)",- expectedNullable: true,- expectedDefault: "",- expectedCollation: "C",- expectedComment: "",- },- {- name: "Column with schema-qualified collation",- sdlText: `CREATE TABLE test (- name VARCHAR(255) COLLATE pg_catalog."POSIX"- );`,- expectedName: "name",- expectedType: "VARCHAR(255)",- expectedNullable: true,- expectedDefault: "",- expectedCollation: `pg_catalog."POSIX"`,- expectedComment: "",- },- {- name: "Complex column with multiple attributes",- sdlText: `CREATE TABLE test (- name VARCHAR(255) NOT NULL COLLATE "en_US" DEFAULT 'unknown'- );`,- expectedName: "name",- expectedType: "VARCHAR(255)",- expectedNullable: false,- expectedDefault: "'unknown'",- expectedCollation: `"en_US"`,- expectedComment: "",- },- {- name: "TEXT column with collation and default",- sdlText: `CREATE TABLE test (- description TEXT COLLATE "en_US.UTF-8" DEFAULT 'No description'- );`,- expectedName: "description",- expectedType: "TEXT",- expectedNullable: true,- expectedDefault: "'No description'",- expectedCollation: `"en_US.UTF-8"`,- expectedComment: "",- },- {- name: "BIGSERIAL PRIMARY KEY column",- sdlText: `CREATE TABLE test (- id BIGSERIAL PRIMARY KEY- );`,- expectedName: "id",- expectedType: "BIGSERIAL",- expectedNullable: false,- expectedDefault: "",- expectedCollation: "",- expectedComment: "",- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- chunks, err := ChunkSDLText(tc.sdlText)- require.NoError(t, err)- require.Contains(t, chunks.Tables, "public.test")-- testChunk := chunks.Tables["public.test"]- createStmt, ok := testChunk.ASTNode.(*parser.CreatestmtContext)- require.True(t, ok, "Should be a CreatestmtContext")-- // Extract the first column definition directly- require.NotNil(t, createStmt.Opttableelementlist(), "Should have table element list")- require.NotNil(t, createStmt.Opttableelementlist().Tableelementlist(), "Should have table element list")-- elements := createStmt.Opttableelementlist().Tableelementlist().AllTableelement()- require.Len(t, elements, 1, "Should have exactly one table element")-- element := elements[0]- require.NotNil(t, element.ColumnDef(), "Should have column definition")-- columnDef := element.ColumnDef()- column := extractColumnMetadata(columnDef)-- require.NotNil(t, column, "Should extract column metadata")-- // Verify all column fields- assert.Equal(t, tc.expectedName, column.Name, "Column name should match")- assert.Equal(t, tc.expectedType, column.Type, "Column type should match")- assert.Equal(t, tc.expectedNullable, column.Nullable, "Column nullable should match")- assert.Equal(t, tc.expectedDefault, column.Default, "Column default should match")- assert.Equal(t, tc.expectedCollation, column.Collation, "Column collation should match")- assert.Equal(t, tc.expectedComment, column.Comment, "Column comment should match")- })- }-}diff --git a/backend/plugin/schema/pg/comment_migration_test.go b/backend/plugin/schema/pg/comment_migration_test.godeleted file mode 100644index 1c01750dae..0000000000--- a/backend/plugin/schema/pg/comment_migration_test.go+++ /dev/null@@ -1,1062 +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/store/model"-)--func TestCommentMigration_TableComment(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- wantSQL string- }{- {- name: "add table comment",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'User information table';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`,- wantSQL: `COMMENT ON TABLE "public"."users" IS 'User information table';-`,- },- {- name: "modify table comment",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Updated user table';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'User information table';-`,- wantSQL: `COMMENT ON TABLE "public"."users" IS 'Updated user table';-`,- },- {- name: "remove table comment",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'User information table';-`,- wantSQL: `COMMENT ON TABLE "public"."users" IS NULL;-`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- require.Equal(t, tt.wantSQL, migrationSQL)- })- }-}--func TestCommentMigration_ColumnComment(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- wantSQL string- }{- {- name: "add column comment",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON COLUMN "public"."users"."id" IS 'Primary key';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`,- wantSQL: `COMMENT ON COLUMN "public"."users"."id" IS 'Primary key';-`,- },- {- name: "modify column comment",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON COLUMN "public"."users"."id" IS 'User unique identifier';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON COLUMN "public"."users"."id" IS 'Primary key';-`,- wantSQL: `COMMENT ON COLUMN "public"."users"."id" IS 'User unique identifier';-`,- },- {- name: "multiple column comments",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON COLUMN "public"."users"."id" IS 'Primary key';-COMMENT ON COLUMN "public"."users"."name" IS 'User full name';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`,- // Note: order may vary depending on map iteration- wantSQL: `COMMENT ON COLUMN "public"."users"."id" IS 'Primary key';-COMMENT ON COLUMN "public"."users"."name" IS 'User full name';-`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // For multiple column comments, check that both statements exist (order may vary)- if tt.name == "multiple column comments" {- require.Contains(t, migrationSQL, `COMMENT ON COLUMN "public"."users"."id" IS 'Primary key';`)- require.Contains(t, migrationSQL, `COMMENT ON COLUMN "public"."users"."name" IS 'User full name';`)- } else {- require.Equal(t, tt.wantSQL, migrationSQL)- }- })- }-}--func TestCommentMigration_ViewComment(t *testing.T) {- currentSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--CREATE VIEW "public"."active_users" AS SELECT * FROM users;--COMMENT ON VIEW "public"."active_users" IS 'View of active users';-`-- previousSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--CREATE VIEW "public"."active_users" AS SELECT * FROM users;-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- expectedSQL := `COMMENT ON VIEW "public"."active_users" IS 'View of active users';-`- require.Equal(t, expectedSQL, migrationSQL)-}--func TestCommentMigration_SequenceComment(t *testing.T) {- currentSDL := `-CREATE SEQUENCE "public"."user_id_seq" AS integer START WITH 1 INCREMENT BY 1;--COMMENT ON SEQUENCE "public"."user_id_seq" IS 'Sequence for user IDs';-`-- previousSDL := `-CREATE SEQUENCE "public"."user_id_seq" AS integer START WITH 1 INCREMENT BY 1;-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- expectedSQL := `COMMENT ON SEQUENCE "public"."user_id_seq" IS 'Sequence for user IDs';-`- require.Equal(t, expectedSQL, migrationSQL)-}--func TestCommentMigration_FunctionComment(t *testing.T) {- currentSDL := `-CREATE FUNCTION "public"."add_numbers"(a integer, b integer) RETURNS integer- LANGUAGE plpgsql- AS $$-BEGIN- RETURN a + b;-END;-$$;--COMMENT ON FUNCTION "public"."add_numbers"(integer, integer) IS 'Adds two numbers';-`-- previousSDL := `-CREATE FUNCTION "public"."add_numbers"(a integer, b integer) RETURNS integer- LANGUAGE plpgsql- AS $$-BEGIN- RETURN a + b;-END;-$$;-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // Function signature includes parameter names- expectedSQL := `COMMENT ON FUNCTION "public".add_numbers(a integer, b integer) IS 'Adds two numbers';-`- require.Equal(t, expectedSQL, migrationSQL)-}--func TestCommentMigration_SchemaComment(t *testing.T) {- currentSDL := `-CREATE SCHEMA "app";--COMMENT ON SCHEMA "app" IS 'Application schema';-`-- previousSDL := `-CREATE SCHEMA "app";-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- expectedSQL := `COMMENT ON SCHEMA "app" IS 'Application schema';-`- require.Equal(t, expectedSQL, migrationSQL)-}--func TestCommentMigration_SpecialCharacters(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- wantSQL string- }{- {- name: "comment with single quote",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL-);--COMMENT ON TABLE "public"."users" IS 'User''s table';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL-);-`,- // Single quotes are already escaped in SDL, and writeCommentOnTable escapes again- wantSQL: `COMMENT ON TABLE "public"."users" IS 'User''''s table';-`,- },- {- name: "comment with newlines",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL-);--COMMENT ON TABLE "public"."users" IS 'This is a-multi-line-comment';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL-);-`,- wantSQL: `COMMENT ON TABLE "public"."users" IS 'This is a-multi-line-comment';-`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- require.Equal(t, tt.wantSQL, migrationSQL)- })- }-}--func TestCommentMigration_NoObjectChangeWithCommentChange(t *testing.T) {- // Test that when only comment changes, no table ALTER is generated- currentSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Updated comment';-`-- previousSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Old comment';-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Should have no table changes- require.Len(t, diff.TableChanges, 0)-- // Should have comment change- require.Len(t, diff.CommentChanges, 1)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- expectedSQL := `COMMENT ON TABLE "public"."users" IS 'Updated comment';-`- require.Equal(t, expectedSQL, migrationSQL)-}--func TestCommentMigration_CombinedChanges(t *testing.T) {- // Test that when both table and comment change, we get both statements- currentSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "email" text-);--COMMENT ON TABLE "public"."users" IS 'User table with email';-`-- previousSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Basic user table';-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Should have table change- require.Len(t, diff.TableChanges, 1)- require.Equal(t, "ALTER", string(diff.TableChanges[0].Action))-- // Should have comment change- require.Len(t, diff.CommentChanges, 1)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // Should contain both ALTER TABLE and COMMENT ON- require.Contains(t, migrationSQL, "ALTER TABLE")- require.Contains(t, migrationSQL, "COMMENT ON TABLE")- require.Contains(t, migrationSQL, "email")- require.Contains(t, migrationSQL, "User table with email")-}--func TestFunctionMigration_NewFunctionWithComment(t *testing.T) {- currentSDL := `-CREATE FUNCTION "public"."calculate_tax"(amount numeric) RETURNS numeric- LANGUAGE plpgsql- AS $$-BEGIN- RETURN amount * 0.1;-END;-$$;--COMMENT ON FUNCTION "public"."calculate_tax"(numeric) IS 'Calculate 10% tax';-`-- previousSDL := ``-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both CREATE FUNCTION and COMMENT ON FUNCTION- require.Contains(t, migrationSQL, "CREATE FUNCTION")- require.Contains(t, migrationSQL, "COMMENT ON FUNCTION")- require.Contains(t, migrationSQL, "calculate_tax")- require.Contains(t, migrationSQL, "Calculate 10% tax")-}--func TestViewMigration_NewViewWithComment(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- }{- {- name: "new view with comment - table exists in both",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "active" boolean-);--CREATE VIEW "public"."active_users" AS SELECT * FROM users WHERE active = true;--COMMENT ON VIEW "public"."active_users" IS 'View of all active users';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "active" boolean-);-`,- },- {- name: "new view with comment - realistic scenario with JOIN",- currentSDL: `-CREATE TABLE "public"."test_table" (- "id" integer NOT NULL,- "name" text,- "created_at" timestamp,- "is_active" boolean,- "description" text-);--CREATE TABLE "public"."test_r_table" (- "id" integer NOT NULL,- "r_name" text,- "r_value" text-);--CREATE VIEW "public"."test_view" AS-SELECT- t.id,- t.name,- t.created_at,- t.is_active,- t.description,- r.r_name,- r.r_value-FROM- "public"."test_table" t-JOIN- "public"."test_r_table" r ON t.id = r.id;--COMMENT ON VIEW "public"."test_view" IS 'A view that combines test_table and test_r_table for easier';-`,- previousSDL: `-CREATE TABLE "public"."test_table" (- "id" integer NOT NULL,- "name" text,- "created_at" timestamp,- "is_active" boolean,- "description" text-);--CREATE TABLE "public"."test_r_table" (- "id" integer NOT NULL,- "r_name" text,- "r_value" text-);-`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both CREATE VIEW and COMMENT ON VIEW- require.Contains(t, migrationSQL, "CREATE VIEW", "Migration should contain CREATE VIEW")- require.Contains(t, migrationSQL, "COMMENT ON VIEW", "Migration should contain COMMENT ON VIEW")- })- }-}--func TestViewMigration_NewViewWithComment_DriftScenario(t *testing.T) {- // This test simulates the drift scenario where schemas are provided- // This matches what happens in real execution (schema_update_sdl_executor.go)-- previousSDL := `-CREATE TABLE "public"."test_table" (- "id" integer NOT NULL,- "name" text-);--CREATE TABLE "public"."test_r_table" (- "id" integer NOT NULL,- "r_name" text,- "r_value" text-);-`-- currentSDL := `-CREATE TABLE "public"."test_table" (- "id" integer NOT NULL,- "name" text-);--CREATE TABLE "public"."test_r_table" (- "id" integer NOT NULL,- "r_name" text,- "r_value" text-);--CREATE VIEW "public"."test_view" AS-SELECT- t.id,- t.name,- r.r_name,- r.r_value-FROM- "public"."test_table" t-JOIN- "public"."test_r_table" r ON t.id = r.id;--COMMENT ON VIEW "public"."test_view" IS 'A view that combines test_table and test_r_table';-`-- // Create mock schemas with view metadata- // Previous schema: no views-- // Current schema: includes view with comment- currentSchema := model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Views: []*storepb.ViewMetadata{- {- Name: "test_view",- Definition: `SELECT- t.id,- t.name,- r.r_name,- r.r_value-FROM- "public"."test_table" t-JOIN- "public"."test_r_table" r ON t.id = r.id;`,- Comment: "A view that combines test_table and test_r_table",- },- },- },- },- },- []byte{},- &storepb.DatabaseConfig{},- storepb.Engine_POSTGRES,- false,- )-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both CREATE VIEW and COMMENT ON VIEW- require.Contains(t, migrationSQL, "CREATE VIEW", "Migration should contain CREATE VIEW")- require.Contains(t, migrationSQL, "COMMENT ON VIEW", "Migration should contain COMMENT ON VIEW")- require.Contains(t, migrationSQL, "test_view", "Migration should reference test_view")- require.Contains(t, migrationSQL, "A view that combines test_table and test_r_table", "Migration should contain the comment text")-}--func TestIndexMigration_NewIndexWithComment(t *testing.T) {- currentSDL := `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text,- "price" numeric-);--CREATE INDEX "idx_products_name" ON "public"."products" (name);--COMMENT ON INDEX "public"."idx_products_name" IS 'Index for product name lookup';-`-- previousSDL := `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text,- "price" numeric-);-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both CREATE INDEX and COMMENT ON INDEX- require.Contains(t, migrationSQL, "CREATE INDEX")- require.Contains(t, migrationSQL, "COMMENT ON INDEX")- require.Contains(t, migrationSQL, "idx_products_name")- require.Contains(t, migrationSQL, "Index for product name lookup")-}--func TestTableMigration_NewTableWithComment(t *testing.T) {- currentSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "email" varchar(255)-);--COMMENT ON TABLE "public"."users" IS 'User information table';-`- previousSDL := ``-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both CREATE TABLE and COMMENT ON TABLE- require.Contains(t, migrationSQL, "CREATE TABLE")- require.Contains(t, migrationSQL, "COMMENT ON TABLE")- require.Contains(t, migrationSQL, "users")- require.Contains(t, migrationSQL, "User information table")-}--func TestColumnMigration_AddColumnWithComment(t *testing.T) {- currentSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "email" varchar(255)-);--COMMENT ON COLUMN "public"."users"."email" IS 'User email address';-`- previousSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both ADD COLUMN and COMMENT ON COLUMN- require.Contains(t, migrationSQL, "ADD COLUMN")- require.Contains(t, migrationSQL, "COMMENT ON COLUMN")- require.Contains(t, migrationSQL, "email")- require.Contains(t, migrationSQL, "User email address")-}--func TestColumnMigration_AddMultipleColumnsWithComments(t *testing.T) {- currentSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "email" varchar(255),- "phone" varchar(20),- "age" integer-);--COMMENT ON COLUMN "public"."users"."email" IS 'User email address';-COMMENT ON COLUMN "public"."users"."phone" IS 'User phone number';-`- previousSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain ADD COLUMN for all new columns- require.Contains(t, migrationSQL, "ADD COLUMN")- require.Contains(t, migrationSQL, "email")- require.Contains(t, migrationSQL, "phone")- require.Contains(t, migrationSQL, "age")-- // Should contain COMMENT ON COLUMN for columns with comments- require.Contains(t, migrationSQL, "COMMENT ON COLUMN")- require.Contains(t, migrationSQL, "User email address")- require.Contains(t, migrationSQL, "User phone number")-}--func TestColumnMigration_AddColumnAndModifyExistingComment(t *testing.T) {- currentSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "email" varchar(255)-);--COMMENT ON COLUMN "public"."users"."name" IS 'Updated: User full name';-COMMENT ON COLUMN "public"."users"."email" IS 'User email address';-`- previousSDL := `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON COLUMN "public"."users"."name" IS 'User name';-`-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain ADD COLUMN for the new column- require.Contains(t, migrationSQL, "ADD COLUMN")- require.Contains(t, migrationSQL, "email")-- // Should contain COMMENT ON COLUMN for both new and modified comments- require.Contains(t, migrationSQL, "COMMENT ON COLUMN")- require.Contains(t, migrationSQL, "Updated: User full name")- require.Contains(t, migrationSQL, "User email address")-}--func TestSequenceMigration_NewSequenceWithOwner(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- wantSQL string- }{- {- name: "add new sequence with owner",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--CREATE SEQUENCE "public"."user_id_seq" AS integer START WITH 1 INCREMENT BY 1;--ALTER SEQUENCE "public"."user_id_seq" OWNED BY "public"."users"."id";-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`,- wantSQL: `CREATE SEQUENCE "public"."user_id_seq" AS integer START WITH 1 INCREMENT BY 1;--ALTER SEQUENCE "public"."user_id_seq" OWNED BY "public"."users"."id";--`,- },- {- name: "add new sequence with owner and comment",- currentSDL: `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text-);--CREATE SEQUENCE "public"."product_id_seq" AS bigint START WITH 100 INCREMENT BY 1;--ALTER SEQUENCE "public"."product_id_seq" OWNED BY "public"."products"."id";--COMMENT ON SEQUENCE "public"."product_id_seq" IS 'Product ID sequence';-`,- previousSDL: `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text-);-`,- wantSQL: `CREATE SEQUENCE "public"."product_id_seq" AS bigint START WITH 100 INCREMENT BY 1;--ALTER SEQUENCE "public"."product_id_seq" OWNED BY "public"."products"."id";--COMMENT ON SEQUENCE "public"."product_id_seq" IS 'Product ID sequence';-`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)- t.Logf("Expected SQL:\n%s", tt.wantSQL)-- require.Equal(t, tt.wantSQL, migrationSQL)- })- }-}--func TestIndexMigration_NewIndexWithComment_DriftScenario(t *testing.T) {- // This test simulates the drift scenario where an index with comment- // is created from database metadata (not in previous SDL)-- previousSDL := `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text,- "price" numeric-);-`-- currentSDL := `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text,- "price" numeric-);--CREATE INDEX "idx_products_name" ON "public"."products" (name);--COMMENT ON INDEX "public"."idx_products_name" IS 'Index for product name lookup';-`-- // Previous schema: table with columns but no indexes-- // Current schema: includes index with comment- currentSchema := model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "products",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- {- Name: "name",- Type: "text",- },- {- Name: "price",- Type: "numeric",- },- },- Indexes: []*storepb.IndexMetadata{- {- Name: "idx_products_name",- Expressions: []string{"name"},- Unique: false,- Comment: "Index for product name lookup",- },- },- },- },- },- },- },- []byte{},- &storepb.DatabaseConfig{},- storepb.Engine_POSTGRES,- false,- )-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both CREATE INDEX and COMMENT ON INDEX- require.Contains(t, migrationSQL, "CREATE INDEX", "Migration should contain CREATE INDEX")- require.Contains(t, migrationSQL, "COMMENT ON INDEX", "Migration should contain COMMENT ON INDEX")- require.Contains(t, migrationSQL, "idx_products_name", "Migration should reference idx_products_name")- require.Contains(t, migrationSQL, "Index for product name lookup", "Migration should contain the comment text")-}--func TestSequenceMigration_NewSequenceWithComment_DriftScenario(t *testing.T) {- // This test simulates the drift scenario where a sequence with comment- // is created from database metadata (not in previous SDL)-- previousSDL := `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text-);-`-- currentSDL := `-CREATE TABLE "public"."products" (- "id" integer NOT NULL,- "name" text-);--CREATE SEQUENCE "public"."product_id_seq" AS bigint START WITH 100 INCREMENT BY 1;--COMMENT ON SEQUENCE "public"."product_id_seq" IS 'Product ID sequence';-`-- // Previous schema: table but no sequences-- // Current schema: table and sequence with comment- currentSchema := model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "products",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- {- Name: "name",- Type: "text",- },- },- },- },- Sequences: []*storepb.SequenceMetadata{- {- Name: "product_id_seq",- DataType: "bigint",- Start: "100",- Increment: "1",- Comment: "Product ID sequence",- },- },- },- },- },- []byte{},- &storepb.DatabaseConfig{},- storepb.Engine_POSTGRES,- false,- )-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should contain both CREATE SEQUENCE and COMMENT ON SEQUENCE- require.Contains(t, migrationSQL, "CREATE SEQUENCE", "Migration should contain CREATE SEQUENCE")- require.Contains(t, migrationSQL, "COMMENT ON SEQUENCE", "Migration should contain COMMENT ON SEQUENCE")- require.Contains(t, migrationSQL, "product_id_seq", "Migration should reference product_id_seq")- require.Contains(t, migrationSQL, "Product ID sequence", "Migration should contain the comment text")-}diff --git a/backend/plugin/schema/pg/commented_enum_test.go b/backend/plugin/schema/pg/commented_enum_test.godeleted file mode 100644index 730630c393..0000000000--- a/backend/plugin/schema/pg/commented_enum_test.go+++ /dev/null@@ -1,220 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/require"-)--// TestCommentedOutEnumType tests that commented-out enum type definitions are ignored-func TestCommentedOutEnumType(t *testing.T) {- // This reproduces the issue from the screenshot where commented SQL causes syntax error- sdl := `-- Enum type: Order status--- CREATE TYPE "public"."order_status_enum" AS ENUM (--- 'draft',--- 'pending',--- 'processing',--- 'shipped',--- 'delivered',--- 'cancelled',--- 'refunded'--- );------ COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';------ Enum type: User role-CREATE TYPE "public"."user_role" AS ENUM (- 'admin',- 'user'-);-`-- // Parse the SDL- chunks, err := ChunkSDLText(sdl)- require.NoError(t, err, "Should successfully parse SDL with commented-out enum type")- require.NotNil(t, chunks)-- // Should only have 1 enum type (user_role), not 2- // The commented-out order_status_enum should be ignored- require.Equal(t, 1, len(chunks.EnumTypes), "Should only parse non-commented enum types")-- // Verify we got the correct enum type- _, exists := chunks.EnumTypes["public.user_role"]- require.True(t, exists, "Should have parsed user_role enum type")-- // Verify the commented-out enum type is NOT parsed- _, exists = chunks.EnumTypes["public.order_status_enum"]- require.False(t, exists, "Should NOT parse commented-out order_status_enum")-}--// TestMultipleCommentedOutStatements tests various commented-out SQL patterns-func TestMultipleCommentedOutStatements(t *testing.T) {- sdl := `-- This is a comment--- CREATE TYPE "public"."status1" AS ENUM ('a', 'b');--CREATE TYPE "public"."status2" AS ENUM ('x', 'y');---- CREATE TYPE "public"."status3" AS ENUM (--- 'foo',--- 'bar'--- );--CREATE TABLE "public"."users" (- id INTEGER PRIMARY KEY,- status "public"."status2"-);---- CREATE TABLE "public"."orders" (--- id INTEGER PRIMARY KEY--- );-`-- chunks, err := ChunkSDLText(sdl)- require.NoError(t, err, "Should parse SDL with mixed commented and uncommented statements")- require.NotNil(t, chunks)-- // Should only have 1 enum type- require.Equal(t, 1, len(chunks.EnumTypes), "Should only parse uncommented enum type")-- // Should only have 1 table- require.Equal(t, 1, len(chunks.Tables), "Should only parse uncommented table")-- // Verify we got the correct objects- _, exists := chunks.EnumTypes["public.status2"]- require.True(t, exists, "Should have parsed status2")-- _, exists = chunks.Tables["public.users"]- require.True(t, exists, "Should have parsed users table")-}--// TestCommentedEnumFromFile simulates reading SDL from a file with commented-out enum-func TestCommentedEnumFromFile(t *testing.T) {- // Simulate exact content from user's screenshot- fileContent := `-- Enum type: Order status--- CREATE TYPE "public"."order_status_enum" AS ENUM (--- 'draft',--- 'pending',--- 'processing',--- 'shipped',--- 'delivered',--- 'cancelled',--- 'refunded'--- );------ COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';------ Enum type: User role-CREATE TYPE "public"."user_role_enum" AS ENUM (- 'admin',- 'moderator',- 'user',- 'guest'-);--COMMENT ON TYPE "public"."user_role_enum" IS 'User role types';-`-- // Test ChunkSDLText- chunks, err := ChunkSDLText(fileContent)- require.NoError(t, err, "Failed to chunk SDL: %v", err)- require.NotNil(t, chunks)-- t.Logf("Number of enum types parsed: %d", len(chunks.EnumTypes))- for k := range chunks.EnumTypes {- t.Logf(" - %s", k)- }-- // Should only have 1 enum type- require.Equal(t, 1, len(chunks.EnumTypes), "Should only parse non-commented enum types")-- // Test GetSDLDiff- previousSDL := ""- currentSDL := fileContent-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err, "GetSDLDiff should not fail with commented SQL: %v", err)- require.NotNil(t, diff)-- // Should have 1 enum type creation- require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")- require.Equal(t, "user_role_enum", diff.EnumTypeChanges[0].EnumTypeName)-}--// TestMultiFileScenarioWithComments simulates reading multiple files-func TestMultiFileScenarioWithComments(t *testing.T) {- // File 1: types.sql with commented and uncommented enums- typesFile := `-- Commented out enum--- CREATE TYPE "public"."old_status" AS ENUM ('a', 'b');--CREATE TYPE "public"."current_status" AS ENUM ('active', 'inactive');-`-- // File 2: tables.sql- tablesFile := `CREATE TABLE "public"."users" (- id SERIAL PRIMARY KEY,- status "public"."current_status" NOT NULL-);-`-- // Concatenate files (simulating what might happen in multi-file mode)- combinedSDL := typesFile + "\n" + tablesFile-- chunks, err := ChunkSDLText(combinedSDL)- require.NoError(t, err, "Should parse combined SDL from multiple files")- require.NotNil(t, chunks)-- // Should only have 1 enum type- require.Equal(t, 1, len(chunks.EnumTypes))-- // Should have 1 table- require.Equal(t, 1, len(chunks.Tables))-}--// TestExactContentFromScreenshot tests the exact content from user's screenshot-func TestExactContentFromScreenshot(t *testing.T) {- // Exact content from screenshot (lines 18188-18202)- sdl := `-- Enum type: Order status--- CREATE TYPE "public"."order_status_enum" AS ENUM (--- 'draft',--- 'pending',--- 'processing',--- 'shipped',--- 'delivered',--- 'cancelled',--- 'refunded'--- );------ COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';------ Enum type: User role-CREATE TYPE "public"."user_role_enum" AS ENUM (- 'admin',- 'user'-);-`-- // This should parse without errors- chunks, err := ChunkSDLText(sdl)- if err != nil {- t.Fatalf("Failed to parse SDL with commented enum: %v\nSDL content:\n%s", err, sdl)- }- require.NotNil(t, chunks)-- // Verify only the uncommented enum was parsed- require.Equal(t, 1, len(chunks.EnumTypes), "Should parse only user_role_enum")-- _, exists := chunks.EnumTypes["public.user_role_enum"]- require.True(t, exists, "Should have user_role_enum")-}--// TestCommentWithSpecialQuotes tests comments with different quote patterns-func TestCommentWithSpecialQuotes(t *testing.T) {- sdl := `-- COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';-CREATE TYPE "public"."status" AS ENUM ('active');-`-- chunks, err := ChunkSDLText(sdl)- require.NoError(t, err, "Should handle commented COMMENT ON TYPE")- require.NotNil(t, chunks)- require.Equal(t, 1, len(chunks.EnumTypes))-}diff --git a/backend/plugin/schema/pg/enum_special_chars_test.go b/backend/plugin/schema/pg/enum_special_chars_test.godeleted file mode 100644index f0c6dd6c4f..0000000000--- a/backend/plugin/schema/pg/enum_special_chars_test.go+++ /dev/null@@ -1,124 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/require"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"-)--// TestEnumTypeWithSingleQuotes tests that enum values containing single quotes are properly escaped-func TestEnumTypeWithSingleQuotes(t *testing.T) {- // Create database metadata with enum type that has single quotes in values- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- EnumTypes: []*storepb.EnumTypeMetadata{- {- Name: "status_with_quotes",- Values: []string{- "active",- "don't use", // Contains single quote- "can't access", // Contains single quote- "won't work", // Contains single quote- },- },- },- },- },- }-- // Generate single-file SDL- sdl, err := getSDLFormat(metadata)- require.NoError(t, err, "Failed to generate SDL")- require.Contains(t, sdl, "don''t use", "Single quotes should be escaped as double single quotes")- require.Contains(t, sdl, "can''t access", "Single quotes should be escaped as double single quotes")- require.Contains(t, sdl, "won''t work", "Single quotes should be escaped as double single quotes")-- // Parse the generated SDL - it should parse without errors- chunks, err := ChunkSDLText(sdl)- require.NoError(t, err, "Generated SDL with escaped quotes should parse correctly")- require.Equal(t, 1, len(chunks.EnumTypes), "Should have 1 enum type")-- // Verify the enum type was parsed- _, exists := chunks.EnumTypes["public.status_with_quotes"]- require.True(t, exists, "Should have parsed status_with_quotes enum type")-}--// TestEnumTypeWithOtherSpecialChars tests enum values with various special characters-func TestEnumTypeWithOtherSpecialChars(t *testing.T) {- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- EnumTypes: []*storepb.EnumTypeMetadata{- {- Name: "special_chars",- Values: []string{- "normal",- "with space",- "with\ttab",- "with\nnewline",- "with-dash",- "with_underscore",- "with.dot",- },- },- },- },- },- }-- // Generate SDL- sdl, err := getSDLFormat(metadata)- require.NoError(t, err, "Failed to generate SDL")-- // Parse the generated SDL - should work for all special characters- chunks, err := ChunkSDLText(sdl)- require.NoError(t, err, "Generated SDL with special characters should parse correctly")- require.Equal(t, 1, len(chunks.EnumTypes), "Should have 1 enum type")-}--// TestEnumTypeRoundTrip tests that enum values survive a full round-trip through SDL-func TestEnumTypeRoundTrip(t *testing.T) {- originalValues := []string{- "active",- "inactive",- "don't care",- "user's choice",- "it's working",- }-- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- EnumTypes: []*storepb.EnumTypeMetadata{- {- Name: "roundtrip_status",- Values: originalValues,- },- },- },- },- }-- // Generate SDL- sdl, err := getSDLFormat(metadata)- require.NoError(t, err, "Failed to generate SDL")-- // Parse it back- chunks, err := ChunkSDLText(sdl)- require.NoError(t, err, "Failed to parse generated SDL")- require.Equal(t, 1, len(chunks.EnumTypes), "Should have 1 enum type")-- // Verify the enum exists- _, exists := chunks.EnumTypes["public.roundtrip_status"]- require.True(t, exists, "Should have parsed roundtrip_status enum type")-- // Note: We can't easily verify the exact values from the parsed chunks- // because the parser doesn't expose the enum values directly.- // The important thing is that it parses without errors.-}diff --git a/backend/plugin/schema/pg/enum_type_test.go b/backend/plugin/schema/pg/enum_type_test.goindex 19ea7365bd..73d17202ae 100644--- a/backend/plugin/schema/pg/enum_type_test.go+++ b/backend/plugin/schema/pg/enum_type_test.go@@ -247,150 +247,6 @@ func TestEnumTypeDependencyOrdering(t *testing.T) {require.NotEqual(t, -1, typePos, "DROP TYPE not found in migration")require.Less(t, tablePos, typePos, "DROP TABLE should come before DROP TYPE")})-- t.Run("ADD Enum via SDL chunks", func(t *testing.T) {- // This tests adding an enum type via SDL diff-- // Previous SDL (no enum)- previousSDL := `CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);`-- // Current SDL (with new enum type)- currentSDL := `CREATE TYPE "public"."status_type" AS ENUM (- 'active',- 'inactive',- 'pending'-);--CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);`-- // Get SDL diff (simulates bb rollout behavior)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Debug: log all diffs- t.Logf("EnumTypeChanges: %d", len(diff.EnumTypeChanges))- for i, enumDiff := range diff.EnumTypeChanges {- t.Logf(" Enum[%d]: Action=%v, Schema=%s, Name=%s, HasNewAST=%v",- i, enumDiff.Action, enumDiff.SchemaName, enumDiff.EnumTypeName,- enumDiff.NewASTNode != nil)- }-- // Verify we have an EnumTypeDiff with CREATE action- require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")- enumDiff := diff.EnumTypeChanges[0]- require.Equal(t, schema.MetadataDiffActionCreate, enumDiff.Action)- require.Equal(t, "public", enumDiff.SchemaName)- require.Equal(t, "status_type", enumDiff.EnumTypeName)- require.NotNil(t, enumDiff.NewASTNode, "NewASTNode should be set for SDL mode")-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration creates the enum type- require.Contains(t, migration, "CREATE TYPE")- require.Contains(t, migration, "status_type")- require.Contains(t, migration, "'active'")- require.Contains(t, migration, "'inactive'")- require.Contains(t, migration, "'pending'")- })-- t.Run("REMOVE Enum via SDL chunks", func(t *testing.T) {- // This tests removing an enum type via SDL diff-- // Previous SDL (with enum)- previousSDL := `CREATE TYPE "public"."status_type" AS ENUM (- 'active',- 'inactive'-);--CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);`-- // Current SDL (enum removed)- currentSDL := `CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);`-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have an EnumTypeDiff with DROP action- require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")- enumDiff := diff.EnumTypeChanges[0]- require.Equal(t, schema.MetadataDiffActionDrop, enumDiff.Action)- require.Equal(t, "public", enumDiff.SchemaName)- require.Equal(t, "status_type", enumDiff.EnumTypeName)-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration drops the enum type- require.Contains(t, migration, "DROP TYPE")- require.Contains(t, migration, "status_type")- })-- t.Run("MODIFY Enum via SDL chunks (DROP + CREATE)", func(t *testing.T) {- // This tests modifying an enum type via SDL diff- // PostgreSQL doesn't support renaming enum values, so we use DROP + CREATE pattern-- // Previous SDL (with original values)- previousSDL := `CREATE TYPE "public"."status_type" AS ENUM (- 'active',- 'inactive'-);`-- // Current SDL (with modified values)- currentSDL := `CREATE TYPE "public"."status_type" AS ENUM (- 'active',- 'inactive',- 'pending'-);`-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have two EnumTypeDiff entries: DROP then CREATE- require.Equal(t, 2, len(diff.EnumTypeChanges), "Should have 2 enum type changes (DROP + CREATE)")- dropDiff := diff.EnumTypeChanges[0]- createDiff := diff.EnumTypeChanges[1]-- require.Equal(t, schema.MetadataDiffActionDrop, dropDiff.Action)- require.Equal(t, "public", dropDiff.SchemaName)- require.Equal(t, "status_type", dropDiff.EnumTypeName)-- require.Equal(t, schema.MetadataDiffActionCreate, createDiff.Action)- require.Equal(t, "public", createDiff.SchemaName)- require.Equal(t, "status_type", createDiff.EnumTypeName)- require.NotNil(t, createDiff.NewASTNode, "NewASTNode should be set for SDL mode")-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration has both DROP and CREATE- require.Contains(t, migration, "DROP TYPE")- require.Contains(t, migration, "CREATE TYPE")- require.Contains(t, migration, "'pending'")- })}// TestEnumTypeAlterAddValue tests that ALTER TYPE ADD VALUE is generated for new enum values@@ -646,46 +502,3 @@ func TestEnumTypeAlterAddValue(t *testing.T) {require.Contains(t, migration, "ADD VALUE 'normal'")})}--// TestEnumTypeCommentChanges tests that enum type comment changes are properly detected and generated-func TestEnumTypeCommentChanges(t *testing.T) {- // Previous SDL (enum without comment)- previousSDL := `CREATE TYPE "public"."status_type" AS ENUM (- 'active',- 'inactive'-);`-- // Current SDL (enum with comment)- currentSDL := `CREATE TYPE "public"."status_type" AS ENUM (- 'active',- 'inactive'-);--COMMENT ON TYPE "public"."status_type" IS 'User status enum';`-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have a comment change- hasCommentDiff := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeType &&- commentDiff.ObjectName == "status_type" {- hasCommentDiff = true- break- }- }- require.True(t, hasCommentDiff, "Should have comment diff for enum type")-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration includes comment- require.Contains(t, migration, "COMMENT ON TYPE")- require.Contains(t, migration, "status_type")- require.Contains(t, migration, "User status enum")-}diff --git a/backend/plugin/schema/pg/exclude_index_test.go b/backend/plugin/schema/pg/exclude_index_test.godeleted file mode 100644index e2e157cbb6..0000000000--- a/backend/plugin/schema/pg/exclude_index_test.go+++ /dev/null@@ -1,306 +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/store/model"-)--// TestExcludeConstraintWithRegularIndexes tests that regular CREATE INDEX statements-// are properly executed alongside EXCLUDE constraints-func TestExcludeConstraintWithRegularIndexes(t *testing.T) {- // Initial state: empty database- previousSDL := ""-- // Current SDL: Table with EXCLUDE constraint and regular indexes- currentSDL := `--- Table with EXCLUDE constraint for employee shift assignments-CREATE TABLE "public"."employee_shifts" (- "shift_id" bigserial,- "employee_id" integer NOT NULL,- "department_id" integer NOT NULL,- "shift_start" timestamp(6) with time zone NOT NULL,- "shift_end" timestamp(6) with time zone NOT NULL,- "shift_type" character varying(50) DEFAULT 'regular',- "created_at" timestamp(6) without time zone DEFAULT CURRENT_TIMESTAMP,- CONSTRAINT "pk_employee_shifts" PRIMARY KEY (shift_id),- CONSTRAINT "chk_employee_shifts_time" CHECK (shift_end > shift_start),- -- EXCLUDE constraint: same employee cannot have overlapping shifts- CONSTRAINT "excl_employee_no_overlap" EXCLUDE USING gist (- employee_id WITH =,- tstzrange(shift_start, shift_end) WITH &&- )-);--CREATE INDEX idx_employee_shifts_employee ON "public"."employee_shifts" (employee_id);-CREATE INDEX idx_employee_shifts_department ON "public"."employee_shifts" (department_id);-CREATE INDEX idx_employee_shifts_time ON "public"."employee_shifts" USING gist (tstzrange(shift_start, shift_end));-CREATE INDEX idx_employee_shifts_type ON "public"."employee_shifts" (shift_type);-`-- currentDBMetadata := &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{},- }-- currentSchema := model.NewDatabaseMetadata(currentDBMetadata, nil, nil, storepb.Engine_POSTGRES, false)-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // Debug: Check what's in the diff- t.Logf("Number of table changes: %d", len(diff.TableChanges))- for i, tc := range diff.TableChanges {- t.Logf("Table %d: %s.%s - IndexChanges: %d", i, tc.SchemaName, tc.TableName, len(tc.IndexChanges))- for j, ic := range tc.IndexChanges {- t.Logf(" Index change %d: Action=%v, HasNewAST=%v, HasOldAST=%v",- j, ic.Action, ic.NewASTNode != nil, ic.OldASTNode != nil)- }- }-- // Generate migration DDL- ddl, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated DDL:\n%s", ddl)-- // Verify the migration includes:- // 1. CREATE TABLE statement- require.Contains(t, ddl, "CREATE TABLE", "Should create the table")- require.Contains(t, ddl, "employee_shifts", "Should create employee_shifts table")-- // 2. EXCLUDE constraint in the CREATE TABLE- require.Contains(t, ddl, "EXCLUDE USING gist", "Should include EXCLUDE constraint")- require.Contains(t, ddl, "excl_employee_no_overlap", "Should include constraint name")-- // 3. Regular CREATE INDEX statements (THIS IS THE BUG - these might be missing)- require.Contains(t, ddl, "CREATE INDEX idx_employee_shifts_employee", "Should create idx_employee_shifts_employee index")- require.Contains(t, ddl, "CREATE INDEX idx_employee_shifts_department", "Should create idx_employee_shifts_department index")- require.Contains(t, ddl, "CREATE INDEX idx_employee_shifts_time", "Should create idx_employee_shifts_time index")- require.Contains(t, ddl, "CREATE INDEX idx_employee_shifts_type", "Should create idx_employee_shifts_type index")-- // 4. Should NOT create an index for the EXCLUDE constraint (it's auto-created)- // The EXCLUDE constraint will create an index, but we shouldn't output a separate CREATE INDEX for it-}--// TestExcludeConstraintIndexNotDuplicated tests that EXCLUDE constraint indexes-// are not output as separate CREATE INDEX statements-func TestExcludeConstraintIndexNotDuplicated(t *testing.T) {- previousSDL := ""-- currentSDL := `-CREATE TABLE "public"."meetings" (- "meeting_id" serial PRIMARY KEY,- "room_id" integer NOT NULL,- "meeting_start" timestamp NOT NULL,- "meeting_end" timestamp NOT NULL,- CONSTRAINT "excl_room_no_overlap" EXCLUDE USING gist (- room_id WITH =,- tsrange(meeting_start, meeting_end, '[)') WITH &&- )-);-`-- // Simulate database state where EXCLUDE constraint has created an index- currentDBMetadata := &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "meetings",- Columns: []*storepb.ColumnMetadata{- {Name: "meeting_id", Type: "integer"},- {Name: "room_id", Type: "integer"},- {Name: "meeting_start", Type: "timestamp without time zone"},- {Name: "meeting_end", Type: "timestamp without time zone"},- },- ExcludeConstraints: []*storepb.ExcludeConstraintMetadata{- {- Name: "excl_room_no_overlap",- Expression: "EXCLUDE USING gist (room_id WITH =, tsrange(meeting_start, meeting_end, '[)') WITH &&)",- },- },- Indexes: []*storepb.IndexMetadata{- {- Name: "excl_room_no_overlap",- Expressions: []string{- "room_id",- "tsrange(meeting_start, meeting_end, '[)')",- },- Type: "gist",- Unique: false,- Primary: false,- IsConstraint: true, // This index is created by EXCLUDE constraint- },- },- },- },- },- },- }-- currentSchema := model.NewDatabaseMetadata(currentDBMetadata, nil, nil, storepb.Engine_POSTGRES, false)-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)-- ddl, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated DDL:\n%s", ddl)-- // Should include EXCLUDE constraint- require.Contains(t, ddl, "EXCLUDE USING gist", "Should include EXCLUDE constraint")-- // Should NOT include a separate CREATE INDEX for the constraint index- // The index is automatically created by PostgreSQL when creating the EXCLUDE constraint- require.NotContains(t, ddl, "CREATE INDEX excl_room_no_overlap", "Should NOT create separate index for EXCLUDE constraint")-}--// TestMaterializedViewWithRegularIndexes tests that regular CREATE INDEX statements-// are properly executed alongside materialized view creation-func TestMaterializedViewWithRegularIndexes(t *testing.T) {- // Initial state: empty database- previousSDL := ""-- // Current SDL: Materialized view with regular indexes- currentSDL := `-CREATE TABLE "public"."orders" (- "order_id" serial PRIMARY KEY,- "customer_id" integer NOT NULL,- "order_date" date NOT NULL,- "total_amount" numeric(10,2) NOT NULL-);--CREATE MATERIALIZED VIEW "public"."order_summary" AS-SELECT- customer_id,- DATE_TRUNC('month', order_date) as order_month,- COUNT(*) as order_count,- SUM(total_amount) as total_sales-FROM "public"."orders"-GROUP BY customer_id, DATE_TRUNC('month', order_date);--CREATE INDEX idx_order_summary_customer ON "public"."order_summary" (customer_id);-CREATE INDEX idx_order_summary_month ON "public"."order_summary" (order_month);-CREATE UNIQUE INDEX idx_order_summary_unique ON "public"."order_summary" (customer_id, order_month);-`-- currentDBMetadata := &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{},- }-- currentSchema := model.NewDatabaseMetadata(currentDBMetadata, nil, nil, storepb.Engine_POSTGRES, false)-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // Debug: Check what's in the diff- t.Logf("Number of materialized view changes: %d", len(diff.MaterializedViewChanges))- for i, mvc := range diff.MaterializedViewChanges {- t.Logf("MV %d: %s.%s - Action=%v, IndexChanges: %d", i, mvc.SchemaName, mvc.MaterializedViewName, mvc.Action, len(mvc.IndexChanges))- for j, ic := range mvc.IndexChanges {- t.Logf(" Index change %d: Action=%v, HasNewAST=%v, HasOldAST=%v",- j, ic.Action, ic.NewASTNode != nil, ic.OldASTNode != nil)- }- }-- // Generate migration DDL- ddl, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated DDL:\n%s", ddl)-- // Verify the migration includes:- // 1. CREATE TABLE statement- require.Contains(t, ddl, "CREATE TABLE", "Should create the orders table")- require.Contains(t, ddl, "orders", "Should create orders table")-- // 2. CREATE MATERIALIZED VIEW statement- require.Contains(t, ddl, "CREATE MATERIALIZED VIEW", "Should create materialized view")- require.Contains(t, ddl, "order_summary", "Should create order_summary materialized view")-- // 3. Regular CREATE INDEX statements (THIS IS THE BUG - these might be missing)- require.Contains(t, ddl, "CREATE INDEX idx_order_summary_customer", "Should create idx_order_summary_customer index")- require.Contains(t, ddl, "CREATE INDEX idx_order_summary_month", "Should create idx_order_summary_month index")- require.Contains(t, ddl, "CREATE UNIQUE INDEX idx_order_summary_unique", "Should create idx_order_summary_unique unique index")-}--// TestIndexCreatedBeforeForeignKey tests that indexes are created before foreign keys that might reference them-// This is important because FK performance benefits from indexes on referenced columns-func TestIndexCreatedBeforeForeignKey(t *testing.T) {- // Initial state: empty database- previousSDL := ""-- // Current SDL: Two tables where orders.customer_id references customers.id- // customers.id has an index that should be created before the FK- currentSDL := `-CREATE TABLE "public"."customers" (- "id" serial PRIMARY KEY,- "email" varchar(255) NOT NULL,- "name" varchar(100) NOT NULL-);--CREATE INDEX idx_customers_id ON "public"."customers" (id);-CREATE INDEX idx_customers_email ON "public"."customers" (email);--CREATE TABLE "public"."orders" (- "id" serial PRIMARY KEY,- "customer_id" integer NOT NULL,- "order_date" date NOT NULL,- "total_amount" numeric(10,2) NOT NULL,- CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES "public"."customers" (id)-);--CREATE INDEX idx_orders_customer ON "public"."orders" (customer_id);-`-- currentDBMetadata := &storepb.DatabaseSchemaMetadata{- Name: "testdb",- Schemas: []*storepb.SchemaMetadata{},- }-- currentSchema := model.NewDatabaseMetadata(currentDBMetadata, nil, nil, storepb.Engine_POSTGRES, false)-- diff, err := GetSDLDiff(currentSDL, previousSDL, currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // Generate migration DDL- ddl, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated DDL:\n%s", ddl)-- // Verify the migration includes all necessary statements- require.Contains(t, ddl, "CREATE TABLE", "Should create tables")- require.Contains(t, ddl, "customers", "Should create customers table")- require.Contains(t, ddl, "orders", "Should create orders table")-- // Verify indexes are created- require.Contains(t, ddl, "CREATE INDEX idx_customers_id", "Should create idx_customers_id index")- require.Contains(t, ddl, "CREATE INDEX idx_customers_email", "Should create idx_customers_email index")- require.Contains(t, ddl, "CREATE INDEX idx_orders_customer", "Should create idx_orders_customer index")-- // Verify FK is created- require.Contains(t, ddl, "fk_orders_customer", "Should create foreign key")-- // CRITICAL: Verify that indexes on customers table are created BEFORE the foreign key- // This is important for FK performance- idxCustomersPos := strings.Index(ddl, "CREATE INDEX idx_customers_id")- fkPos := strings.Index(ddl, "fk_orders_customer")-- require.NotEqual(t, -1, idxCustomersPos, "idx_customers_id index should exist")- require.NotEqual(t, -1, fkPos, "fk_orders_customer should exist")- require.Less(t, idxCustomersPos, fkPos,- "Index on referenced column (customers.id) should be created BEFORE the foreign key that references it")-}diff --git a/backend/plugin/schema/pg/extension_test.go b/backend/plugin/schema/pg/extension_test.goindex 9476c61703..8b618edb2a 100644--- a/backend/plugin/schema/pg/extension_test.go+++ b/backend/plugin/schema/pg/extension_test.go@@ -1,7 +1,6 @@package pgimport (- "strings""testing""github.com/stretchr/testify/require"@@ -271,280 +270,6 @@ func TestExtensionDependencyOrdering(t *testing.T) {require.Less(t, tablePos, extensionPos, "DROP TABLE should come before DROP EXTENSION")require.Less(t, typePos, extensionPos, "DROP TYPE should come before DROP EXTENSION")})-- t.Run("INITIALIZATION: First-time SDL with Extension", func(t *testing.T) {- // This tests the initialization scenario: empty database, user writes first SDL- // This is a very common bb rollout scenario-- // Previous SDL (empty - new database)- previousSDL := ``-- // Current SDL (first-time setup with extension and other objects)- currentSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "public";--CREATE TYPE "public"."status_type" AS ENUM ('active', 'inactive');--CREATE TABLE "public"."users" (- "id" uuid NOT NULL,- "status" status_type NOT NULL-);`-- // Get SDL diff (simulates bb rollout initialization)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have extension, enum, and table changes- require.Equal(t, 1, len(diff.ExtensionChanges), "Should have 1 extension change")- require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")- require.Equal(t, 1, len(diff.TableChanges), "Should have 1 table change")-- extDiff := diff.ExtensionChanges[0]- require.Equal(t, schema.MetadataDiffActionCreate, extDiff.Action)- require.Equal(t, "uuid-ossp", extDiff.ExtensionName)-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify correct ordering: extension → enum → table- extensionPos := strings.Index(migration, "CREATE EXTENSION")- enumPos := strings.Index(migration, "CREATE TYPE")- tablePos := strings.Index(migration, "CREATE TABLE")-- require.NotEqual(t, -1, extensionPos, "CREATE EXTENSION should be present")- require.NotEqual(t, -1, enumPos, "CREATE TYPE should be present")- require.NotEqual(t, -1, tablePos, "CREATE TABLE should be present")- require.Less(t, extensionPos, enumPos, "CREATE EXTENSION should come before CREATE TYPE")- require.Less(t, extensionPos, tablePos, "CREATE EXTENSION should come before CREATE TABLE")- })-- t.Run("ADD Extension via SDL chunks", func(t *testing.T) {- // This tests adding an extension to existing database via SDL diff-- // Previous SDL (existing database with table, no extension)- previousSDL := `CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);`-- // Current SDL (with new extension)- currentSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "public";--CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);`-- // Get SDL diff (simulates bb rollout behavior)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Debug: log all diffs- t.Logf("ExtensionChanges: %d", len(diff.ExtensionChanges))- for i, extDiff := range diff.ExtensionChanges {- t.Logf(" Extension[%d]: Action=%v, Name=%s, HasNewAST=%v",- i, extDiff.Action, extDiff.ExtensionName,- extDiff.NewASTNode != nil)- }-- // Verify we have an ExtensionDiff with CREATE action- require.Equal(t, 1, len(diff.ExtensionChanges), "Should have 1 extension change")- extDiff := diff.ExtensionChanges[0]- require.Equal(t, schema.MetadataDiffActionCreate, extDiff.Action)- require.Equal(t, "uuid-ossp", extDiff.ExtensionName)- require.NotNil(t, extDiff.NewASTNode, "NewASTNode should be set for SDL mode")-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration creates the extension- require.Contains(t, migration, "CREATE EXTENSION")- require.Contains(t, migration, "uuid-ossp")- })-- t.Run("REMOVE Extension via SDL chunks", func(t *testing.T) {- // This tests removing an extension via SDL diff-- // Previous SDL (with extension)- previousSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp";--CREATE TABLE "public"."users" (- "id" uuid NOT NULL,- "name" text-);`-- // Current SDL (extension removed)- currentSDL := `CREATE TABLE "public"."users" (- "id" uuid NOT NULL,- "name" text-);`-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have an ExtensionDiff with DROP action- require.Equal(t, 1, len(diff.ExtensionChanges), "Should have 1 extension change")- extDiff := diff.ExtensionChanges[0]- require.Equal(t, schema.MetadataDiffActionDrop, extDiff.Action)- require.Equal(t, "uuid-ossp", extDiff.ExtensionName)-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration drops the extension- require.Contains(t, migration, "DROP EXTENSION")- require.Contains(t, migration, "uuid-ossp")- })-- t.Run("MODIFY Extension via SDL chunks (DROP + CREATE)", func(t *testing.T) {- // This tests modifying an extension via SDL diff- // PostgreSQL doesn't support modifying extensions easily, so we use DROP + CREATE pattern-- // Previous SDL (with original version)- previousSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp" VERSION '1.0';`-- // Current SDL (with modified version)- currentSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp" VERSION '1.1';`-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have two ExtensionDiff entries: DROP then CREATE- require.Equal(t, 2, len(diff.ExtensionChanges), "Should have 2 extension changes (DROP + CREATE)")- dropDiff := diff.ExtensionChanges[0]- createDiff := diff.ExtensionChanges[1]-- require.Equal(t, schema.MetadataDiffActionDrop, dropDiff.Action)- require.Equal(t, "uuid-ossp", dropDiff.ExtensionName)-- require.Equal(t, schema.MetadataDiffActionCreate, createDiff.Action)- require.Equal(t, "uuid-ossp", createDiff.ExtensionName)- require.NotNil(t, createDiff.NewASTNode, "NewASTNode should be set for SDL mode")-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration has both DROP and CREATE- require.Contains(t, migration, "DROP EXTENSION")- require.Contains(t, migration, "CREATE EXTENSION")- require.Contains(t, migration, "uuid-ossp")- })-}--// TestExtensionCommentChanges tests that extension comment changes are properly detected and generated-func TestExtensionCommentChanges(t *testing.T) {- // Previous SDL (extension without comment)- previousSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`-- // Current SDL (extension with comment)- currentSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp";--COMMENT ON EXTENSION "uuid-ossp" IS 'UUID generation functions';`-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have a comment change- hasCommentDiff := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeExtension &&- commentDiff.ObjectName == "uuid-ossp" {- hasCommentDiff = true- break- }- }- require.True(t, hasCommentDiff, "Should have comment diff for extension")-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration includes comment- require.Contains(t, migration, "COMMENT ON EXTENSION")- require.Contains(t, migration, "uuid-ossp")- require.Contains(t, migration, "UUID generation functions")-}--// TestExtensionWithCitextType tests the real-world scenario where citext extension-// is needed before creating tables with citext columns (bb rollout multi-file SDL)-func TestExtensionWithCitextType(t *testing.T) {- // Simulates multi-file SDL scenario where:- // - extensions.sql contains: CREATE EXTENSION "citext"- // - tables.sql contains: CREATE TABLE with citext column- // Previous SDL (empty database)- previousSDL := ``-- // Current SDL (multi-file combined: extension + table using citext type)- // This simulates the content after merging extensions.sql and tables.sql- currentSDL := `-- Extension: citext--- Provides case-insensitive text data type-CREATE EXTENSION IF NOT EXISTS "citext" WITH SCHEMA public;--COMMENT ON EXTENSION "citext" IS 'Case-insensitive character string type';---- Script to create all dependency example objects in correct order--- For testdb11 schema---- ============================================================================--- STEP 1: Create Base Tables (Layer 0)--- ============================================================================---- Layer 0 entities have no foreign key dependencies and can be created first--CREATE TABLE "public"."email_addresses" (- "email_id" serial,- "email" citext NOT NULL,- "user_id" integer,- "created_at" timestamp DEFAULT CURRENT_TIMESTAMP-);`-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify we have both extension and table changes- require.Equal(t, 1, len(diff.ExtensionChanges), "Should have 1 extension change")- require.GreaterOrEqual(t, len(diff.TableChanges), 1, "Should have at least 1 table change")-- extDiff := diff.ExtensionChanges[0]- require.Equal(t, schema.MetadataDiffActionCreate, extDiff.Action)- require.Equal(t, "citext", extDiff.ExtensionName)-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // CRITICAL: Verify correct ordering - extension MUST come before table- extensionPos := strings.Index(migration, "CREATE EXTENSION")- tablePos := strings.Index(migration, "CREATE TABLE")-- require.NotEqual(t, -1, extensionPos, "CREATE EXTENSION should be present")- require.NotEqual(t, -1, tablePos, "CREATE TABLE should be present")- require.Less(t, extensionPos, tablePos, "CREATE EXTENSION must come BEFORE CREATE TABLE (otherwise citext type won't exist)")-- // Verify the extension is citext- require.Contains(t, migration, `"citext"`)-- // Verify the table uses citext type- require.Contains(t, migration, "email_addresses")}// TestExtensionNotFilteredByArchiveSchemaFilter tests that extension changes are preserved@@ -709,119 +434,3 @@ $function$;`}})}--// TestExtensionRoundtripNoDiff tests that dumping SDL from database doesn't create false diffs-// This reproduces the issue where:-// 1. User creates extension with format A (e.g., manual SQL)-// 2. Bytebase dumps SDL from database-// 3. No changes were made, but extension is detected as changed and DROP+CREATE is generated-func TestExtensionRoundtripNoDiff(t *testing.T) {- t.Run("User creates extension then dump from database - should not generate diff", func(t *testing.T) {- // STEP 1: User's initial SDL (manual creation)- userSDL := `CREATE EXTENSION IF NOT EXISTS "citext" WITH SCHEMA public;`-- // Parse user SDL to get metadata (simulate database state)- chunks, err := ChunkSDLText(userSDL)- require.NoError(t, err)- require.Equal(t, 1, len(chunks.Extensions))-- // STEP 2: Simulate database dump - create metadata and dump back to SDL- // This simulates what happens when Bytebase dumps the current database state- metadata := &storepb.DatabaseSchemaMetadata{- Extensions: []*storepb.ExtensionMetadata{- {- Name: "citext",- Schema: "public",- // Version might or might not be specified by user- Version: "",- // Description might be empty- Description: "",- },- },- }-- // Create current database schema from metadata (this is the actual database state)- currentSchema := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)-- // Dump SDL from metadata (this is what getSDLFormat does)- dumpedSDL, err := getSDLFormat(metadata)- require.NoError(t, err)-- // STEP 3: Compare user SDL (previous) vs dumped SDL (current)- // This is what happens in bb rollout when user doesn't change anything- // Pass currentSchema so usability check can work- diff, err := GetSDLDiff(dumpedSDL, userSDL, currentSchema)- require.NoError(t, err)-- // CRITICAL: There should be NO extension changes!- // The extension is the same, just different text representation- if len(diff.ExtensionChanges) > 0 {- t.Error("FAILED: Extension changes detected when there should be none!")- t.Error("This means Bytebase will try to DROP+CREATE the extension even though nothing changed")-- // Generate migration to see what would happen- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)-- // Check if DROP is in the migration- if strings.Contains(migration, "DROP EXTENSION") {- t.Error("CRITICAL: Migration contains DROP EXTENSION - this will fail with '2BP01' error!")- }- }-- require.Equal(t, 0, len(diff.ExtensionChanges), "No changes should be detected for unchanged extension")- })-- t.Run("Extension with version - roundtrip test", func(t *testing.T) {- // User creates with explicit version- userSDL := `CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public VERSION '1.1';`-- // Database metadata after creation- metadata := &storepb.DatabaseSchemaMetadata{- Extensions: []*storepb.ExtensionMetadata{- {- Name: "uuid-ossp",- Schema: "public",- Version: "1.1",- },- },- }-- currentSchema := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)-- dumpedSDL, err := getSDLFormat(metadata)- require.NoError(t, err)-- diff, err := GetSDLDiff(dumpedSDL, userSDL, currentSchema)- require.NoError(t, err)-- require.Equal(t, 0, len(diff.ExtensionChanges), "No changes for extension with version")- })-- t.Run("Extension with comment - roundtrip test", func(t *testing.T) {- // User creates with comment- userSDL := `CREATE EXTENSION IF NOT EXISTS "citext" WITH SCHEMA public;--COMMENT ON EXTENSION "citext" IS 'Case-insensitive text type';`-- metadata := &storepb.DatabaseSchemaMetadata{- Extensions: []*storepb.ExtensionMetadata{- {- Name: "citext",- Schema: "public",- Description: "Case-insensitive text type",- },- },- }-- currentSchema := model.NewDatabaseMetadata(metadata, nil, nil, storepb.Engine_POSTGRES, false)-- dumpedSDL, err := getSDLFormat(metadata)- require.NoError(t, err)-- diff, err := GetSDLDiff(dumpedSDL, userSDL, currentSchema)- require.NoError(t, err)-- require.Equal(t, 0, len(diff.ExtensionChanges), "No changes for extension with comment")- })-}diff --git a/backend/plugin/schema/pg/function_comparer_test.go b/backend/plugin/schema/pg/function_comparer_test.godeleted file mode 100644index b34df5c94d..0000000000--- a/backend/plugin/schema/pg/function_comparer_test.go+++ /dev/null@@ -1,490 +0,0 @@-package pg--import (- "strings"- "testing"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestPostgreSQLFunctionComparer_Equal(t *testing.T) {- comparer := &PostgreSQLFunctionComparer{}-- tests := []struct {- name string- oldFunc *storepb.FunctionMetadata- newFunc *storepb.FunctionMetadata- wantEqual bool- }{- {- name: "identical functions",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- wantEqual: true,- },- {- name: "body only difference",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a * b; END; $$ LANGUAGE plpgsql;`,- },- wantEqual: false,- },- {- name: "signature difference",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer, c integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- wantEqual: false,- },- {- name: "nil functions",- oldFunc: nil,- newFunc: nil,- wantEqual: true,- },- {- name: "one nil function",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE FUNCTION test() RETURNS integer AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;`,- },- newFunc: nil,- wantEqual: false,- },- {- name: "identical procedures",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- },- wantEqual: true,- },- {- name: "procedure body only difference",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name, updated_at = NOW() WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- },- wantEqual: false,- },- {- name: "procedure with dollar quote differences",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE public.refresh_stats() LANGUAGE plpgsql AS $procedure$ BEGIN REFRESH MATERIALIZED VIEW stats; END; $procedure$`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE refresh_stats() LANGUAGE plpgsql AS $$ BEGIN REFRESH MATERIALIZED VIEW stats; END; $$`,- },- wantEqual: true, // Should be equal - only formatting differences- },- {- name: "function vs procedure - different types should not be equal",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION get_user_count() RETURNS integer AS $$ BEGIN RETURN (SELECT COUNT(*) FROM users); END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE get_user_count() AS $$ DECLARE count_val integer; BEGIN SELECT COUNT(*) INTO count_val FROM users; END; $$ LANGUAGE plpgsql;`,- },- wantEqual: false,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- equal := comparer.Equal(tt.oldFunc, tt.newFunc)- if equal != tt.wantEqual {- t.Errorf("Equal() = %v, want %v", equal, tt.wantEqual)- }- })- }-}--func TestPostgreSQLFunctionComparer_CompareDetailed(t *testing.T) {- comparer := &PostgreSQLFunctionComparer{}-- tests := []struct {- name string- oldFunc *storepb.FunctionMetadata- newFunc *storepb.FunctionMetadata- wantResult *schema.FunctionComparisonResult- wantSignatureChanged bool- wantBodyChanged bool- wantCanUseAlter bool- }{- {- name: "identical functions",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- wantResult: nil, // No changes, should return nil- },- {- name: "body only difference",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a * b; END; $$ LANGUAGE plpgsql;`,- },- wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil- wantSignatureChanged: false,- wantBodyChanged: true,- wantCanUseAlter: true,- },- {- name: "signature difference",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer, c integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- },- wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil- wantSignatureChanged: true,- wantBodyChanged: false,- wantCanUseAlter: false,- },- {- name: "nil functions",- oldFunc: nil,- newFunc: nil,- wantResult: nil,- },- {- name: "procedure body only difference",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_stats() AS $$ BEGIN UPDATE stats SET last_updated = NOW(); END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_stats() AS $$ BEGIN UPDATE stats SET last_updated = NOW(), count = count + 1; END; $$ LANGUAGE plpgsql;`,- },- wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil- wantSignatureChanged: false,- wantBodyChanged: true,- wantCanUseAlter: true,- },- {- name: "procedure signature difference",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer) AS $$ BEGIN UPDATE users SET updated_at = NOW() WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_status text) AS $$ BEGIN UPDATE users SET updated_at = NOW() WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- },- wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil- wantSignatureChanged: true,- wantBodyChanged: false,- wantCanUseAlter: false,- },- {- name: "procedure with dollar quote formatting differences",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE public.refresh_data() LANGUAGE plpgsql AS $procedure$ BEGIN REFRESH MATERIALIZED VIEW data_view; END; $procedure$`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE refresh_data() LANGUAGE plpgsql AS $$ BEGIN REFRESH MATERIALIZED VIEW data_view; END; $$`,- },- wantResult: nil, // Should be nil - functions are equivalent after normalization- },- {- name: "function to procedure change",- oldFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE FUNCTION calculate_total() RETURNS integer AS $$ BEGIN RETURN (SELECT SUM(amount) FROM orders); END; $$ LANGUAGE plpgsql;`,- },- newFunc: &storepb.FunctionMetadata{- Definition: `CREATE OR REPLACE PROCEDURE calculate_total() AS $$ BEGIN RETURN (SELECT SUM(amount) FROM orders); END; $$ LANGUAGE plpgsql;`,- },- wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil- wantSignatureChanged: true, // FUNCTION vs PROCEDURE is a signature change- wantBodyChanged: false,- wantCanUseAlter: false,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- result, err := comparer.CompareDetailed(tt.oldFunc, tt.newFunc)- if err != nil {- t.Errorf("CompareDetailed() error = %v", err)- return- }-- if tt.wantResult == nil {- if result != nil {- t.Errorf("CompareDetailed() = %v, want nil", result)- }- return- }-- if result == nil {- t.Error("CompareDetailed() = nil, want non-nil result")- return- }-- if result.SignatureChanged != tt.wantSignatureChanged {- t.Errorf("CompareDetailed().SignatureChanged = %v, want %v", result.SignatureChanged, tt.wantSignatureChanged)- }-- if result.BodyChanged != tt.wantBodyChanged {- t.Errorf("CompareDetailed().BodyChanged = %v, want %v", result.BodyChanged, tt.wantBodyChanged)- }-- if result.CanUseAlterFunction != tt.wantCanUseAlter {- t.Errorf("CompareDetailed().CanUseAlterFunction = %v, want %v", result.CanUseAlterFunction, tt.wantCanUseAlter)- }- })- }-}--func TestExtractFunctionBody(t *testing.T) {- tests := []struct {- name string- definition string- wantBody string- }{- {- name: "dollar quoted function",- definition: `CREATE OR REPLACE FUNCTION test() RETURNS integer AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;`,- wantBody: "BEGIN RETURN 1; END;",- },- {- name: "single quoted function",- definition: `CREATE FUNCTION test() RETURNS integer AS 'SELECT 1' LANGUAGE sql;`,- wantBody: "SELECT", // Note: Due to a bug in TrimQuotes function in postgresql parser, single quotes are incorrectly trimmed- },- {- name: "complex function with nested dollar quotes",- definition: `CREATE OR REPLACE FUNCTION complex_func() RETURNS text AS $func$ DECLARE result text; BEGIN result := 'test'; RETURN result; END; $func$ LANGUAGE plpgsql;`,- wantBody: "DECLARE result text; BEGIN result := 'test'; RETURN result; END;",- },- {- name: "simple procedure",- definition: `CREATE OR REPLACE PROCEDURE update_stats() AS $$ BEGIN UPDATE stats SET count = count + 1; END; $$ LANGUAGE plpgsql;`,- wantBody: "BEGIN UPDATE stats SET count = count + 1; END;",- },- {- name: "procedure with custom dollar quote",- definition: `CREATE PROCEDURE refresh_data() AS $proc$ BEGIN REFRESH MATERIALIZED VIEW data_view; END; $proc$ LANGUAGE plpgsql;`,- wantBody: "BEGIN REFRESH MATERIALIZED VIEW data_view; END;",- },- {- name: "procedure with parameters",- definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- wantBody: "BEGIN UPDATE users SET name = new_name WHERE id = user_id; END;",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- body := extractFunctionBody(tt.definition)- if body != tt.wantBody {- t.Errorf("extractFunctionBody() = %q, want %q", body, tt.wantBody)- }- })- }-}--func TestParseFunctionSignature(t *testing.T) {- tests := []struct {- name string- definition string- wantName string- wantParams int- wantReturn string- }{- {- name: "simple function",- definition: `CREATE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- wantName: "add_numbers",- wantParams: 2,- wantReturn: "integer",- },- {- name: "function with no parameters",- definition: `CREATE OR REPLACE FUNCTION get_current_time() RETURNS timestamp AS $$ BEGIN RETURN NOW(); END; $$ LANGUAGE plpgsql;`,- wantName: "get_current_time",- wantParams: 0,- wantReturn: "timestamp",- },- {- name: "function with complex return type",- definition: `CREATE FUNCTION get_user(user_id bigint) RETURNS TABLE(id bigint, name text) AS $$ BEGIN RETURN QUERY SELECT id, name FROM users WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- wantName: "get_user",- wantParams: 1,- wantReturn: "", // TABLE return types may not be fully parsed by current implementation- },- {- name: "simple procedure",- definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,- wantName: "update_user",- wantParams: 2,- wantReturn: "", // Procedures don't have return types- },- {- name: "procedure with no parameters",- definition: `CREATE PROCEDURE refresh_stats() AS $$ BEGIN REFRESH MATERIALIZED VIEW stats; END; $$ LANGUAGE plpgsql;`,- wantName: "refresh_stats",- wantParams: 0,- wantReturn: "", // Procedures don't have return types- },- {- name: "procedure with schema qualification",- definition: `CREATE OR REPLACE PROCEDURE public.cleanup_old_data(days_old integer) AS $$ BEGIN DELETE FROM logs WHERE created_at < NOW() - INTERVAL '%d days', days_old; END; $$ LANGUAGE plpgsql;`,- wantName: "cleanup_old_data", // Should extract unqualified name- wantParams: 1,- wantReturn: "", // Procedures don't have return types- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- signature, err := parseFunctionSignature(tt.definition)- if err != nil {- t.Errorf("parseFunctionSignature() error = %v", err)- return- }-- if signature.Name != tt.wantName {- t.Errorf("parseFunctionSignature().Name = %v, want %v", signature.Name, tt.wantName)- }-- if len(signature.Parameters) != tt.wantParams {- t.Errorf("parseFunctionSignature().Parameters count = %v, want %v", len(signature.Parameters), tt.wantParams)- }-- if signature.ReturnType != tt.wantReturn {- t.Errorf("parseFunctionSignature().ReturnType = %v, want %v", signature.ReturnType, tt.wantReturn)- }- })- }-}--func TestPostgreSQLFunctionComparer_ProcedureSupport(t *testing.T) {- comparer := &PostgreSQLFunctionComparer{}-- tests := []struct {- name string- def1 string- def2 string- wantDiff bool- wantMsg string- }{- {- name: "identical procedures should be equal",- def1: `CREATE OR REPLACE PROCEDURE test_proc(param1 integer) AS $$ BEGIN UPDATE table1 SET col1 = param1; END; $$ LANGUAGE plpgsql;`,- def2: `CREATE OR REPLACE PROCEDURE test_proc(param1 integer) AS $$ BEGIN UPDATE table1 SET col1 = param1; END; $$ LANGUAGE plpgsql;`,- wantDiff: false,- wantMsg: "identical procedures",- },- {- name: "procedures with dollar quote differences should be equal",- def1: `CREATE OR REPLACE PROCEDURE public.test_proc() LANGUAGE plpgsql AS $procedure$ BEGIN UPDATE stats SET count = 1; END; $procedure$`,- def2: `CREATE OR REPLACE PROCEDURE test_proc() LANGUAGE plpgsql AS $$ BEGIN UPDATE stats SET count = 1; END; $$`,- wantDiff: false,- wantMsg: "procedures with only formatting differences",- },- {- name: "procedures with body differences should not be equal",- def1: `CREATE OR REPLACE PROCEDURE test_proc() AS $$ BEGIN UPDATE table1 SET col1 = 1; END; $$ LANGUAGE plpgsql;`,- def2: `CREATE OR REPLACE PROCEDURE test_proc() AS $$ BEGIN UPDATE table1 SET col1 = 2; END; $$ LANGUAGE plpgsql;`,- wantDiff: true,- wantMsg: "procedures with different bodies",- },- {- name: "procedures with signature differences should not be equal",- def1: `CREATE OR REPLACE PROCEDURE test_proc(param1 integer) AS $$ BEGIN UPDATE table1 SET col1 = param1; END; $$ LANGUAGE plpgsql;`,- def2: `CREATE OR REPLACE PROCEDURE test_proc(param1 integer, param2 text) AS $$ BEGIN UPDATE table1 SET col1 = param1; END; $$ LANGUAGE plpgsql;`,- wantDiff: true,- wantMsg: "procedures with different signatures",- },- {- name: "function vs procedure should not be equal",- def1: `CREATE OR REPLACE FUNCTION test_func() RETURNS integer AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;`,- def2: `CREATE OR REPLACE PROCEDURE test_proc() AS $$ BEGIN UPDATE table1 SET col1 = 1; END; $$ LANGUAGE plpgsql;`,- wantDiff: true,- wantMsg: "function vs procedure",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- oldFunc := &storepb.FunctionMetadata{Definition: tt.def1}- newFunc := &storepb.FunctionMetadata{Definition: tt.def2}-- equal := comparer.Equal(oldFunc, newFunc)- if equal == tt.wantDiff {- t.Errorf("%s: Equal() = %v, want %v", tt.wantMsg, equal, !tt.wantDiff)- }-- // Also test detailed comparison for non-identical cases- if tt.wantDiff {- result, err := comparer.CompareDetailed(oldFunc, newFunc)- if err != nil {- t.Errorf("%s: CompareDetailed() error = %v", tt.wantMsg, err)- }- if result == nil {- t.Errorf("%s: CompareDetailed() returned nil, expected non-nil result", tt.wantMsg)- }- }- })- }-}--func TestFunctionBodyExtraction_WithANTLR(t *testing.T) {- tests := []struct {- name string- definition string- wantBody string- }{- {- name: "function with ANTLR-based extraction",- definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,- wantBody: " BEGIN RETURN a + b; END; ", // Expected content between dollar quotes- },- {- name: "procedure with ANTLR-based extraction",- definition: `CREATE OR REPLACE PROCEDURE update_counter() AS $$ BEGIN UPDATE counters SET count = count + 1; END; $$ LANGUAGE plpgsql;`,- wantBody: " BEGIN UPDATE counters SET count = count + 1; END; ", // Expected content between dollar quotes- },- {- name: "function with custom dollar tag",- definition: `CREATE OR REPLACE FUNCTION test_func() RETURNS text AS $body$ BEGIN RETURN 'test'; END; $body$ LANGUAGE plpgsql;`,- wantBody: " BEGIN RETURN 'test'; END; ", // Expected content between dollar quotes- },- {- name: "procedure with custom dollar tag",- definition: `CREATE OR REPLACE PROCEDURE test_proc() AS $proc$ BEGIN UPDATE stats SET last_run = NOW(); END; $proc$ LANGUAGE plpgsql;`,- wantBody: " BEGIN UPDATE stats SET last_run = NOW(); END; ", // Expected content between dollar quotes- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- body := extractFunctionBody(tt.definition)- // Normalize whitespace for comparison- body = strings.TrimSpace(body)- wantBody := strings.TrimSpace(tt.wantBody)-- if body != wantBody {- t.Errorf("extractFunctionBody() = %q, want %q", body, wantBody)- }- })- }-}diff --git a/backend/plugin/schema/pg/function_migration_integration_test.go b/backend/plugin/schema/pg/function_migration_integration_test.godeleted file mode 100644index 508dac5e79..0000000000--- a/backend/plugin/schema/pg/function_migration_integration_test.go+++ /dev/null@@ -1,638 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestFunctionSDLDiffAndMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Create new function",- previousSDL: ``,- currentSDL: `- CREATE FUNCTION calculate_area(radius DECIMAL) RETURNS DECIMAL AS $$- BEGIN- RETURN 3.14159 * radius * radius;- END;- $$ LANGUAGE plpgsql;- `,- expectedMigration: `CREATE FUNCTION calculate_area(radius DECIMAL) RETURNS DECIMAL AS $$- BEGIN- RETURN 3.14159 * radius * radius;- END;- $$ LANGUAGE plpgsql;--`,- },- {- name: "Drop function",- previousSDL: `- CREATE FUNCTION calculate_area(radius DECIMAL) RETURNS DECIMAL AS $$- BEGIN- RETURN 3.14159 * radius * radius;- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: ``,- expectedMigration: `DROP FUNCTION IF EXISTS "public"."calculate_area"(radius numeric);-`,- },- {- name: "Modify function (CREATE OR REPLACE)",- previousSDL: `- CREATE FUNCTION calculate_area(radius DECIMAL) RETURNS DECIMAL AS $$- BEGIN- RETURN 3.14159 * radius * radius;- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE FUNCTION calculate_area(radius DECIMAL) RETURNS DECIMAL AS $$- BEGIN- RETURN 3.14159265 * radius * radius; -- More precise PI- END;- $$ LANGUAGE plpgsql;- `,- expectedMigration: `CREATE OR REPLACE FUNCTION calculate_area(radius DECIMAL) RETURNS DECIMAL AS $$- BEGIN- RETURN 3.14159265 * radius * radius; -- More precise PI- END;- $$ LANGUAGE plpgsql;--`,- },- {- name: "Create new procedure (stored as function)",- previousSDL: ``,- currentSDL: `- CREATE PROCEDURE log_message(msg TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- -- Some procedure logic here- NULL;- END;- $$;- `,- expectedMigration: `CREATE PROCEDURE log_message(msg TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- -- Some procedure logic here- NULL;- END;- $$;--`,- },- {- name: "Overloaded functions with different signatures",- previousSDL: `- CREATE FUNCTION add_numbers(a INTEGER, b INTEGER)- RETURNS INTEGER AS $$- BEGIN- RETURN a + b;- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE FUNCTION add_numbers(a INTEGER, b INTEGER)- RETURNS INTEGER AS $$- BEGIN- RETURN a + b;- END;- $$ LANGUAGE plpgsql;-- CREATE FUNCTION add_numbers(a FLOAT, b FLOAT)- RETURNS FLOAT AS $$- BEGIN- RETURN a + b;- END;- $$ LANGUAGE plpgsql;- `,- expectedMigration: `CREATE FUNCTION add_numbers(a FLOAT, b FLOAT)- RETURNS FLOAT AS $$- BEGIN- RETURN a + b;- END;- $$ LANGUAGE plpgsql;--`,- },- {- name: "Drop one overloaded function",- previousSDL: `- CREATE FUNCTION add_numbers(a INTEGER, b INTEGER)- RETURNS INTEGER AS $$- BEGIN- RETURN a + b;- END;- $$ LANGUAGE plpgsql;-- CREATE FUNCTION add_numbers(a FLOAT, b FLOAT)- RETURNS FLOAT AS $$- BEGIN- RETURN a + b;- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE FUNCTION add_numbers(a INTEGER, b INTEGER)- RETURNS INTEGER AS $$- BEGIN- RETURN a + b;- END;- $$ LANGUAGE plpgsql;- `,- expectedMigration: `DROP FUNCTION IF EXISTS "public"."add_numbers"(a float, b float);-`,- },- {- name: "Modify one overloaded function",- previousSDL: `- CREATE FUNCTION calculate(x INTEGER)- RETURNS INTEGER AS $$- BEGIN- RETURN x * 2;- END;- $$ LANGUAGE plpgsql;-- CREATE FUNCTION calculate(x FLOAT)- RETURNS FLOAT AS $$- BEGIN- RETURN x * 2.0;- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE FUNCTION calculate(x INTEGER)- RETURNS INTEGER AS $$- BEGIN- RETURN x * 3; -- Changed logic- END;- $$ LANGUAGE plpgsql;-- CREATE FUNCTION calculate(x FLOAT)- RETURNS FLOAT AS $$- BEGIN- RETURN x * 2.0;- END;- $$ LANGUAGE plpgsql;- `,- expectedMigration: `CREATE OR REPLACE FUNCTION calculate(x INTEGER)- RETURNS INTEGER AS $$- BEGIN- RETURN x * 3; -- Changed logic- END;- $$ LANGUAGE plpgsql;--`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Step 1: Get SDL diff using AST-only mode (no metadata extraction)- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Step 2: Verify that diff contains function changes with AST nodes- if len(diff.FunctionChanges) > 0 {- for _, funcDiff := range diff.FunctionChanges {- switch funcDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, funcDiff.NewASTNode,- "Create action should have NewASTNode")- assert.Nil(t, funcDiff.OldASTNode,- "Create action should not have OldASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, funcDiff.NewFunction,- "AST-only mode should not have metadata")- case schema.MetadataDiffActionDrop:- assert.NotNil(t, funcDiff.OldASTNode,- "Drop action should have OldASTNode")- assert.Nil(t, funcDiff.NewASTNode,- "Drop action should not have NewASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, funcDiff.OldFunction,- "AST-only mode should not have metadata")- case schema.MetadataDiffActionAlter:- assert.NotNil(t, funcDiff.OldASTNode,- "Alter action should have OldASTNode")- assert.NotNil(t, funcDiff.NewASTNode,- "Alter action should have NewASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, funcDiff.OldFunction,- "AST-only mode should not have metadata")- assert.Nil(t, funcDiff.NewFunction,- "AST-only mode should not have metadata")- default:- // Other actions- t.Logf("Encountered function action: %v", funcDiff.Action)- }- }- }-- // Step 3: Generate migration SQL using AST nodes- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // Step 4: Verify the generated migration matches expectations- assert.Equal(t, tt.expectedMigration, migrationSQL,- "Generated migration SQL should match expected output")-- // Step 5: Verify the migration contains expected keywords for functions- if tt.expectedMigration != "" {- if containsString(tt.expectedMigration, "CREATE FUNCTION") {- assert.Contains(t, migrationSQL, "CREATE FUNCTION",- "Migration should contain CREATE FUNCTION statement")- }- if containsString(tt.expectedMigration, "CREATE PROCEDURE") {- assert.Contains(t, migrationSQL, "CREATE PROCEDURE",- "Migration should contain CREATE PROCEDURE statement")- }- if containsString(tt.expectedMigration, "DROP FUNCTION") {- assert.Contains(t, migrationSQL, "DROP FUNCTION",- "Migration should contain DROP FUNCTION statement")- }- }- })- }-}--func TestFunctionMigrationASTOnlyModeValidation(t *testing.T) {- // Test that ensures AST-only mode works correctly without any metadata extraction- previousSDL := ``- currentSDL := `- CREATE FUNCTION get_total(amount DECIMAL) RETURNS DECIMAL AS $$- BEGIN- RETURN amount * 1.1;- END;- $$ LANGUAGE plpgsql;- `-- // Get diff without metadata extraction- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly one function change- assert.Len(t, diff.FunctionChanges, 1)- funcDiff := diff.FunctionChanges[0]-- // Verify AST-only mode properties- assert.Equal(t, schema.MetadataDiffActionCreate, funcDiff.Action)- assert.Equal(t, "public", funcDiff.SchemaName)- assert.Equal(t, "get_total(amount numeric)", funcDiff.FunctionName)-- // Critical assertion: No metadata should be present (AST-only mode)- assert.Nil(t, funcDiff.NewFunction, "AST-only mode should not extract metadata")- assert.Nil(t, funcDiff.OldFunction, "AST-only mode should not extract metadata")-- // But AST nodes should be present- assert.NotNil(t, funcDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, funcDiff.OldASTNode, "No old AST node for CREATE action")-- // Generate migration should work with AST nodes only- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migrationSQL, "CREATE FUNCTION get_total(amount DECIMAL) RETURNS DECIMAL")-}--func TestOverloadedFunctionSignatureHandling(t *testing.T) {- // Test that verifies signature-based identification works correctly in full flow- previousSDL := ``- currentSDL := `- -- Create two functions with same name but different signatures- CREATE FUNCTION format_value(val INTEGER)- RETURNS TEXT AS $$- BEGIN- RETURN val::TEXT;- END;- $$ LANGUAGE plpgsql;-- CREATE FUNCTION format_value(val DECIMAL, precision_digits INTEGER)- RETURNS TEXT AS $$- BEGIN- RETURN ROUND(val, precision_digits)::TEXT;- END;- $$ LANGUAGE plpgsql;- `-- // Get diff without metadata extraction (AST-only mode)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly two function changes (CREATE actions)- assert.Len(t, diff.FunctionChanges, 2)-- // Verify both functions are CREATE actions with different signatures- createCount := 0- signatures := make(map[string]bool)- for _, funcDiff := range diff.FunctionChanges {- if funcDiff.Action == schema.MetadataDiffActionCreate {- createCount++- // Verify AST-only mode properties- assert.NotNil(t, funcDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, funcDiff.OldASTNode, "No old AST node for CREATE action")- assert.Nil(t, funcDiff.NewFunction, "AST-only mode should not extract metadata")-- // Collect signatures to verify they're different- signatures[funcDiff.FunctionName] = true- }- }- assert.Equal(t, 2, createCount, "Should have two CREATE actions")-- // Verify the signatures are different- assert.Len(t, signatures, 2, "Should have two different function signatures")-- // Expected signatures- expectedSignatures := map[string]bool{- "format_value(val integer)": true,- "format_value(val numeric, precision_digits integer)": true,- }- assert.Equal(t, expectedSignatures, signatures, "Should have correct function signatures")-- // Generate migration SQL - this should work with both functions- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- // Verify the migration contains both functions- assert.Contains(t, migrationSQL, "format_value(val INTEGER)")- assert.Contains(t, migrationSQL, "format_value(val DECIMAL, precision_digits INTEGER)")-- // Verify both functions are created- functionCount := strings.Count(migrationSQL, "CREATE FUNCTION format_value")- assert.Equal(t, 2, functionCount, "Should create both overloaded functions")-}--// Helper function to check if a string contains a substring-func containsString(s, substr string) bool {- return strings.Contains(s, substr)-}--func TestProcedureDropMigration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Drop procedure should use DROP PROCEDURE",- previousSDL: `- CREATE PROCEDURE 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;- $$;- `,- currentSDL: ``,- expectedMigration: `DROP PROCEDURE IF EXISTS "public"."update_user_name"(user_id integer, new_name character varying);`,- },- {- name: "Drop function should use DROP FUNCTION",- previousSDL: `- CREATE FUNCTION calculate_tax(amount numeric) RETURNS numeric- LANGUAGE plpgsql- AS $$- BEGIN- RETURN amount * 0.1;- END;- $$;- `,- currentSDL: ``,- expectedMigration: `DROP FUNCTION IF EXISTS "public"."calculate_tax"(amount numeric);`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migrationSQL, tt.expectedMigration)- })- }-}--func TestDropProcedureShouldNotAffectFunctionComment(t *testing.T) {- // This test reproduces the issue shown in the screenshot:- // When dropping a procedure, the system should not generate- // COMMENT ON FUNCTION statements that remove comments from other functions- tests := []struct {- name string- previousSDL string- currentSDL string- }{- {- name: "Drop procedure without comment should not affect function comment",- previousSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- -- Function logic goes here- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';-- CREATE PROCEDURE new_procedure()- LANGUAGE plpgsql- AS $$- BEGIN- -- Procedure logic- NULL;- END;- $$;- `,- currentSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- -- Function logic goes here- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';- `,- },- {- name: "Drop procedure with comment should not affect function comment",- previousSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';-- CREATE PROCEDURE new_procedure()- LANGUAGE plpgsql- AS $$- BEGIN- NULL;- END;- $$;-- COMMENT ON PROCEDURE "public"."new_procedure()" IS 'A test procedure';- `,- currentSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';- `,- },- {- name: "Drop procedure with quoted identifiers should not affect function comment",- // This tests the case where COMMENT statements use quoted identifiers (as generated by the database)- previousSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';-- CREATE PROCEDURE new_procedure()- LANGUAGE plpgsql- AS $$- BEGIN- NULL;- END;- $$;-- COMMENT ON PROCEDURE "public"."new_procedure()" IS 'A test procedure';- `,- currentSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';- `,- },- {- name: "Drop procedure with wrongly labeled FUNCTION comment",- // This simulates a bug where procedure comment was mistakenly written as COMMENT ON FUNCTION- previousSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';-- CREATE PROCEDURE new_procedure()- LANGUAGE plpgsql- AS $$- BEGIN- NULL;- END;- $$;-- COMMENT ON FUNCTION "public"."new_procedure()" IS 'Wrong: should be PROCEDURE not FUNCTION';- `,- currentSDL: `- CREATE OR REPLACE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $function$- BEGIN- RAISE NOTICE 'Test function executed';- END- $function$;-- COMMENT ON FUNCTION "public"."test_function()" IS 'A test function that raises a notice';- `,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Log all changes- t.Logf("Function changes: %d", len(diff.FunctionChanges))- for i, fc := range diff.FunctionChanges {- t.Logf(" [%d] Action=%v, Schema=%s, Function=%s", i, fc.Action, fc.SchemaName, fc.FunctionName)- }- t.Logf("Comment changes: %d", len(diff.CommentChanges))- for i, cc := range diff.CommentChanges {- t.Logf(" [%d] Action=%v, Type=%v, Schema=%s, Object=%s, OldComment=%q, NewComment=%q",- i, cc.Action, cc.ObjectType, cc.SchemaName, cc.ObjectName, cc.OldComment, cc.NewComment)- }-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Should drop the procedure- assert.Contains(t, migrationSQL, "DROP PROCEDURE IF EXISTS")- assert.Contains(t, migrationSQL, "new_procedure")-- // Should NOT touch the function's comment- // The migration should NOT contain any COMMENT statement about test_function- if strings.Contains(migrationSQL, "test_function") && strings.Contains(migrationSQL, "COMMENT") {- // If there's a COMMENT statement about test_function, it should not be setting it to NULL- assert.NotContains(t, migrationSQL, "COMMENT ON FUNCTION \"public\".test_function() IS NULL",- "Migration should not remove test_function's comment")- assert.NotContains(t, migrationSQL, "COMMENT ON FUNCTION \"public\".\"test_function()\" IS NULL",- "Migration should not remove test_function's comment")- t.Errorf("Found unexpected COMMENT statement about test_function in migration: %s", migrationSQL)- }- })- }-}diff --git a/backend/plugin/schema/pg/function_sdl_diff_test.go b/backend/plugin/schema/pg/function_sdl_diff_test.godeleted file mode 100644index 7af4fa29ab..0000000000--- a/backend/plugin/schema/pg/function_sdl_diff_test.go+++ /dev/null@@ -1,575 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestFunctionSDLDiff(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedFunctionChanges int- expectedActions []schema.MetadataDiffAction- }{- {- name: "Create new function",- previousSDL: ``,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE OR REPLACE FUNCTION get_user_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$ LANGUAGE plpgsql;- `,- expectedFunctionChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Drop function",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE OR REPLACE FUNCTION get_user_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- expectedFunctionChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},- },- {- name: "Modify function (CREATE OR REPLACE)",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE OR REPLACE FUNCTION get_user_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE OR REPLACE FUNCTION get_user_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users WHERE active = true);- END;- $$ LANGUAGE plpgsql;- `,- expectedFunctionChanges: 1, // ALTER only- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionAlter},- },- {- name: "No changes to function",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE OR REPLACE FUNCTION get_user_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE OR REPLACE FUNCTION get_user_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$ LANGUAGE plpgsql;- `,- expectedFunctionChanges: 0,- expectedActions: []schema.MetadataDiffAction{},- },- {- name: "Multiple functions with different changes",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE OR REPLACE FUNCTION get_all_users()- RETURNS SETOF users AS $$- BEGIN- RETURN QUERY SELECT * FROM users;- END;- $$ LANGUAGE plpgsql;-- CREATE OR REPLACE FUNCTION get_user_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$ LANGUAGE plpgsql;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE OR REPLACE FUNCTION get_all_users()- RETURNS SETOF users AS $$- BEGIN- RETURN QUERY SELECT * FROM users;- END;- $$ LANGUAGE plpgsql;-- CREATE OR REPLACE FUNCTION get_admin_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users WHERE role = 'admin');- END;- $$ LANGUAGE plpgsql;- `,- expectedFunctionChanges: 2, // Drop get_user_count + Create get_admin_count- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate, schema.MetadataDiffActionDrop},- },- {- name: "Schema-qualified function names",- previousSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );- `,- currentSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE OR REPLACE FUNCTION test_schema.get_product_count()- RETURNS INTEGER AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM test_schema.products);- END;- $$ LANGUAGE plpgsql;- `,- expectedFunctionChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Function with complex signature",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- amount DECIMAL(10,2),- created_at TIMESTAMP DEFAULT NOW()- );- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- amount DECIMAL(10,2),- created_at TIMESTAMP DEFAULT NOW()- );-- CREATE OR REPLACE FUNCTION calculate_total(- p_start_date DATE,- p_end_date DATE,- p_discount_rate DECIMAL DEFAULT 0.0- )- RETURNS DECIMAL(10,2) AS $$- BEGIN- RETURN (- SELECT COALESCE(SUM(amount * (1 - p_discount_rate)), 0)- FROM orders- WHERE created_at::DATE BETWEEN p_start_date AND p_end_date- );- END;- $$ LANGUAGE plpgsql;- `,- expectedFunctionChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- assert.Equal(t, tt.expectedFunctionChanges, len(diff.FunctionChanges),- "Expected %d function changes, got %d", tt.expectedFunctionChanges, len(diff.FunctionChanges))-- // Check that the actions match expectations- var actualActions []schema.MetadataDiffAction- for _, funcDiff := range diff.FunctionChanges {- actualActions = append(actualActions, funcDiff.Action)- }-- // Handle nil vs empty slice comparison- if len(tt.expectedActions) == 0 && len(actualActions) == 0 {- // Both are effectively empty - test passes- t.Log("Both expected and actual actions are empty")- } else {- assert.ElementsMatch(t, tt.expectedActions, actualActions,- "Expected actions %v, got %v", tt.expectedActions, actualActions)- }-- // Verify AST nodes are properly set- for i, funcDiff := range diff.FunctionChanges {- switch funcDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, funcDiff.NewASTNode,- "Function diff %d should have NewASTNode for CREATE action", i)- assert.Nil(t, funcDiff.OldASTNode,- "Function diff %d should not have OldASTNode for CREATE action", i)- case schema.MetadataDiffActionDrop:- assert.NotNil(t, funcDiff.OldASTNode,- "Function diff %d should have OldASTNode for DROP action", i)- assert.Nil(t, funcDiff.NewASTNode,- "Function diff %d should not have NewASTNode for DROP action", i)- case schema.MetadataDiffActionAlter:- assert.NotNil(t, funcDiff.OldASTNode,- "Function diff %d should have OldASTNode for ALTER action", i)- assert.NotNil(t, funcDiff.NewASTNode,- "Function diff %d should have NewASTNode for ALTER action", i)- default:- t.Errorf("Unexpected action %v for function diff %d", funcDiff.Action, i)- }- }- })- }-}--func TestFunctionIdentifierParsing(t *testing.T) {- tests := []struct {- name string- identifier string- expectedSchema string- expectedFunction string- }{- {- name: "Simple function name",- identifier: "my_function",- expectedSchema: "public",- expectedFunction: "my_function",- },- {- name: "Schema-qualified function name",- identifier: "test_schema.my_function",- expectedSchema: "test_schema",- expectedFunction: "my_function",- },- {- name: "Public schema explicit",- identifier: "public.user_summary_func",- expectedSchema: "public",- expectedFunction: "user_summary_func",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- schema, function := parseIdentifier(tt.identifier)- assert.Equal(t, tt.expectedSchema, schema)- assert.Equal(t, tt.expectedFunction, function)- })- }-}--// TestSameNameFunctionsWithDifferentSignatures tests BYT-7994-// PostgreSQL supports function overloading - same name with different arguments-// The SDL diff should recognize functions by (name + signature), not just name-func TestSameNameFunctionsWithDifferentSignatures(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedFunctionChanges int- expectedCreateCount int- expectedDropCount int- expectedAlterCount int- description string- }{- {- name: "Add second function with same name but different signature - BYT-7994",- previousSDL: `-CREATE FUNCTION "public"."my_procedure"(p_id integer) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Function with one parameter: %', p_id;-END;-$$;-`,- currentSDL: `-CREATE FUNCTION "public"."my_procedure"(p_id integer) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Function with one parameter: %', p_id;-END;-$$;--CREATE FUNCTION "public"."my_procedure"(p_id integer, p_name text) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Function with two parameters: % - %', p_id, p_name;-END;-$$;-`,- expectedFunctionChanges: 1,- expectedCreateCount: 1,- expectedDropCount: 0,- expectedAlterCount: 0,- description: "Should add the new function without dropping the old one",- },- {- name: "Three overloaded functions with same name",- previousSDL: `-CREATE FUNCTION "public"."calculate"(x integer) RETURNS integer- LANGUAGE sql- AS $$- SELECT x * 2;-$$;-`,- currentSDL: `-CREATE FUNCTION "public"."calculate"(x integer) RETURNS integer- LANGUAGE sql- AS $$- SELECT x * 2;-$$;--CREATE FUNCTION "public"."calculate"(x integer, y integer) RETURNS integer- LANGUAGE sql- AS $$- SELECT x + y;-$$;--CREATE FUNCTION "public"."calculate"(x numeric, y numeric, z numeric) RETURNS numeric- LANGUAGE sql- AS $$- SELECT x + y + z;-$$;-`,- expectedFunctionChanges: 2,- expectedCreateCount: 2,- expectedDropCount: 0,- expectedAlterCount: 0,- description: "Should add two new overloaded functions",- },- {- name: "Remove one overloaded function, keep others",- previousSDL: `-CREATE FUNCTION "public"."process"(p_id integer) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Process one param';-END;-$$;--CREATE FUNCTION "public"."process"(p_id integer, p_name text) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Process two params';-END;-$$;--CREATE FUNCTION "public"."process"(p_id integer, p_name text, p_email text) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Process three params';-END;-$$;-`,- currentSDL: `-CREATE FUNCTION "public"."process"(p_id integer) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Process one param';-END;-$$;--CREATE FUNCTION "public"."process"(p_id integer, p_name text, p_email text) RETURNS void- LANGUAGE plpgsql- AS $$-BEGIN- RAISE NOTICE 'Process three params';-END;-$$;-`,- expectedFunctionChanges: 1,- expectedCreateCount: 0,- expectedDropCount: 1,- expectedAlterCount: 0,- description: "Should drop only the middle function (2 params), keep others",- },- {- name: "Modify one overloaded function, keep others unchanged",- previousSDL: `-CREATE FUNCTION "public"."get_info"(p_id integer) RETURNS text- LANGUAGE sql- AS $$- SELECT 'User ID: ' || p_id::text;-$$;--CREATE FUNCTION "public"."get_info"(p_name text) RETURNS text- LANGUAGE sql- AS $$- SELECT 'User Name: ' || p_name;-$$;-`,- currentSDL: `-CREATE FUNCTION "public"."get_info"(p_id integer) RETURNS text- LANGUAGE sql- AS $$- SELECT 'Updated User ID: ' || p_id::text;-$$;--CREATE FUNCTION "public"."get_info"(p_name text) RETURNS text- LANGUAGE sql- AS $$- SELECT 'User Name: ' || p_name;-$$;-`,- expectedFunctionChanges: 1,- expectedCreateCount: 0,- expectedDropCount: 0,- expectedAlterCount: 1,- description: "Should modify only the first function, keep second unchanged",- },- {- name: "Procedures with same name but different signatures",- previousSDL: `-CREATE PROCEDURE "public"."update_status"(p_id integer)- LANGUAGE plpgsql- AS $$-BEGIN- UPDATE users SET status = 'active' WHERE id = p_id;-END;-$$;-`,- currentSDL: `-CREATE PROCEDURE "public"."update_status"(p_id integer)- LANGUAGE plpgsql- AS $$-BEGIN- UPDATE users SET status = 'active' WHERE id = p_id;-END;-$$;--CREATE PROCEDURE "public"."update_status"(p_id integer, p_new_status text)- LANGUAGE plpgsql- AS $$-BEGIN- UPDATE users SET status = p_new_status WHERE id = p_id;-END;-$$;-`,- expectedFunctionChanges: 1,- expectedCreateCount: 1,- expectedDropCount: 0,- expectedAlterCount: 0,- description: "Should add the new procedure without dropping the old one",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)-- // Count different action types- createCount := 0- dropCount := 0- alterCount := 0-- for _, change := range diff.FunctionChanges {- signature := ""- if change.NewFunction != nil {- signature = change.NewFunction.Signature- } else if change.OldFunction != nil {- signature = change.OldFunction.Signature- }- t.Logf("Function: %s, Signature: %s, Action: %s",- change.FunctionName, signature, change.Action)-- switch change.Action {- case schema.MetadataDiffActionCreate:- createCount++- case schema.MetadataDiffActionDrop:- dropCount++- case schema.MetadataDiffActionAlter:- alterCount++- default:- t.Errorf("Unexpected action: %v", change.Action)- }- }-- t.Logf("Description: %s", tt.description)- t.Logf("Total changes: %d (CREATE: %d, DROP: %d, ALTER: %d)",- len(diff.FunctionChanges), createCount, dropCount, alterCount)-- assert.Equal(t, tt.expectedFunctionChanges, len(diff.FunctionChanges),- "Expected %d function changes, got %d", tt.expectedFunctionChanges, len(diff.FunctionChanges))- assert.Equal(t, tt.expectedCreateCount, createCount,- "Expected %d CREATE actions, got %d", tt.expectedCreateCount, createCount)- assert.Equal(t, tt.expectedDropCount, dropCount,- "Expected %d DROP actions, got %d", tt.expectedDropCount, dropCount)- assert.Equal(t, tt.expectedAlterCount, alterCount,- "Expected %d ALTER actions, got %d", tt.expectedAlterCount, alterCount)-- // If we see unexpected DROP actions for overloaded functions, that's the bug- if dropCount > tt.expectedDropCount {- t.Errorf("BUG DETECTED (BYT-7994): Found %d DROP action(s), expected %d. "+- "Functions with same name but different signatures should be identified separately.",- dropCount, tt.expectedDropCount)- }- })- }-}diff --git a/backend/plugin/schema/pg/generate_migration_testcontainer_test.go b/backend/plugin/schema/pg/generate_migration_testcontainer_test.goindex 90a1ad7b4f..04f10c2a8e 100644--- a/backend/plugin/schema/pg/generate_migration_testcontainer_test.go+++ b/backend/plugin/schema/pg/generate_migration_testcontainer_test.go@@ -2339,176 +2339,3 @@ func sortExtensionsByName(extensions []*storepb.ExtensionMetadata) {return strings.Compare(a.Name, b.Name)})}--// TestSDLForeignKeyDependencyOrder tests that SDL migration correctly handles FK dependencies-// when tables are provided in alphabetical order (table1 before table2) but table1 references table2-func TestSDLForeignKeyDependencyOrder(t *testing.T) {- ctx := context.Background()-- container := testcontainer.GetTestPgContainer(ctx, t)- t.Cleanup(func() {- container.Close(ctx)- })-- dbName := fmt.Sprintf("test_fk_dep_order_%d", time.Now().Unix())- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))- require.NoError(t, err)-- driver, err := createPgDriver(ctx, container.GetHost(), container.GetPort(), dbName)- require.NoError(t, err)- defer driver.Close(ctx)-- // This SDL text simulates multi-file SDL where files are merged in alphabetical order- // table1.sql comes before table2.sql, but table1 has FK to table2- currentSDL := `-CREATE TABLE table1 (- id serial,- name varchar(100) not null,- created_at timestamp default current_timestamp,- description text,- constraint pk_table1_id primary key (id),- constraint fk_table1_name foreign key (name) references table2(name)-);--CREATE TABLE table2 (- id int,- name varchar(100),- constraint pk_table2_id primary key (id),- constraint uq_table2_name unique (name)-);-`-- previousSDL := ``-- // Get current schema (empty database)- currentSchema, err := driver.SyncDBSchema(ctx)- require.NoError(t, err)- dbMetadata := model.NewDatabaseMetadata(currentSchema, nil, nil, storepb.Engine_POSTGRES, false)-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, dbMetadata)- require.NoError(t, err)-- // Generate migration SQL with topological sorting- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Execute the generated migration SQL- // This should work because generateMigration should have sorted tables by FK dependencies- _, err = driver.GetDB().ExecContext(ctx, migrationSQL)- require.NoError(t, err, "Migration SQL should execute successfully with correct FK dependency order")-- // Connect to the test database for verification queries- verifyDriver, err := createPgDriver(ctx, container.GetHost(), container.GetPort(), dbName)- require.NoError(t, err)- defer verifyDriver.Close(ctx)-- // Verify both tables were created- verifyDB := verifyDriver.GetDB()- var table1Exists, table2Exists bool- err = verifyDB.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'table1')").Scan(&table1Exists)- require.NoError(t, err)- err = verifyDB.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'table2')").Scan(&table2Exists)- require.NoError(t, err)-- require.True(t, table1Exists, "table1 should be created")- require.True(t, table2Exists, "table2 should be created")-- // Verify FK constraint exists- var fkExists bool- err = verifyDB.QueryRowContext(ctx, `- SELECT EXISTS (- SELECT 1 FROM information_schema.table_constraints- WHERE constraint_name = 'fk_table1_name' AND table_name = 'table1'- )- `).Scan(&fkExists)- require.NoError(t, err)- require.True(t, fkExists, "FK constraint fk_table1_name should exist")-}--// TestSDLViewDependencyChain tests that SDL migration correctly handles view dependencies-// when a table is depended on by view1, and view1 is depended on by view2-func TestSDLViewDependencyChain(t *testing.T) {- ctx := context.Background()-- container := testcontainer.GetTestPgContainer(ctx, t)- t.Cleanup(func() {- container.Close(ctx)- })-- dbName := fmt.Sprintf("test_view_dep_chain_%d", time.Now().Unix())- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))- require.NoError(t, err)-- driver, err := createPgDriver(ctx, container.GetHost(), container.GetPort(), dbName)- require.NoError(t, err)- defer driver.Close(ctx)-- // This SDL text simulates the REAL multi-file scenario where files are merged alphabetically:- // Files: table_base.sql, test_view_0.sql, test_view2.sql- // Alphabetical order: table_base < test_view_0 < test_view2- // Dependency chain: table_base -> test_view2 -> test_view_0- // So SDL has: table_base, test_view_0 (depends on test_view2 which comes AFTER), test_view2- currentSDL := `-CREATE TABLE table_base (- id serial,- info text not null,- created_at timestamp default current_timestamp,- updated_at timestamp default current_timestamp,- constraint pk_table_base_id primary key (id)-);--CREATE VIEW test_view_0 AS-SELECT * FROM test_view2;--CREATE VIEW test_view2 AS-SELECT id, info, created_at, updated_at FROM table_base;-`-- previousSDL := ``-- // Get current schema (empty database)- currentSchema, err := driver.SyncDBSchema(ctx)- require.NoError(t, err)- dbMetadata := model.NewDatabaseMetadata(currentSchema, nil, nil, storepb.Engine_POSTGRES, false)-- // Get SDL diff- diff, err := GetSDLDiff(currentSDL, previousSDL, dbMetadata)- require.NoError(t, err)-- // Generate migration SQL with topological sorting- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // Execute the generated migration SQL- // This should work because generateMigration should have sorted objects by dependencies- _, err = driver.GetDB().ExecContext(ctx, migrationSQL)- require.NoError(t, err, "Migration SQL should execute successfully with correct view dependency order")-- // Connect to the test database for verification queries- verifyDriver, err := createPgDriver(ctx, container.GetHost(), container.GetPort(), dbName)- require.NoError(t, err)- defer verifyDriver.Close(ctx)-- verifyDB := verifyDriver.GetDB()-- // Verify table was created- var tableExists bool- err = verifyDB.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'table_base')").Scan(&tableExists)- require.NoError(t, err)- require.True(t, tableExists, "table_base should be created")-- // Verify test_view2 was created (depends on table_base)- var view2Exists bool- err = verifyDB.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM information_schema.views WHERE table_name = 'test_view2')").Scan(&view2Exists)- require.NoError(t, err)- require.True(t, view2Exists, "test_view2 should be created")-- // Verify test_view_0 was created (depends on test_view2)- var view0Exists bool- err = verifyDB.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM information_schema.views WHERE table_name = 'test_view_0')").Scan(&view0Exists)- require.NoError(t, err)- require.True(t, view0Exists, "test_view_0 should be created")-}diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.goindex e46c832454..dee00c5cd0 100644--- a/backend/plugin/schema/pg/get_database_definition.go+++ b/backend/plugin/schema/pg/get_database_definition.go@@ -2240,6 +2240,19 @@ func writeFunction(out io.Writer, schema string, function *storepb.FunctionMetadreturn nil}+// functionExtractor walks a PostgreSQL parse tree and captures the first+// CREATE FUNCTION/PROCEDURE statement it encounters.+type functionExtractor struct {+ parser.BasePostgreSQLParserListener+ result **parser.CreatefunctionstmtContext+}++func (e *functionExtractor) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {+ if e.result != nil && *e.result == nil {+ *e.result = ctx+ }+}+// isDefinitionProcedure checks if the definition string represents a PROCEDURE (not a FUNCTION)// Returns true if it's a PROCEDURE, false if it's a FUNCTION// This function uses AST-based parsing for robust detectiondiff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.godeleted file mode 100644index 6c27a225a4..0000000000--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ /dev/null@@ -1,519 +0,0 @@-package pg--import (- "fmt"- "strings"-- "github.com/pkg/errors"-- parser "github.com/bytebase/parser/postgresql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/schema"- "github.com/bytebase/bytebase/backend/store/model"-)--func init() {- schema.RegisterGetSDLDiff(storepb.Engine_POSTGRES, GetSDLDiff)- schema.RegisterGetSDLDiff(storepb.Engine_COCKROACHDB, GetSDLDiff)-}--func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema *model.DatabaseMetadata) (*schema.MetadataDiff, error) {- generatedSDL, err := convertDatabaseSchemaToSDL(currentSchema)- if err != nil {- return nil, errors.Wrap(err, "failed to convert current schema to SDL format for initialization")- }-- // Check for initialization scenario: previousUserSDLText is empty- if strings.TrimSpace(previousUserSDLText) == "" && currentSchema != nil {- // Initialization scenario: convert currentSchema to SDL format as baseline- previousUserSDLText = generatedSDL- }-- // Only skip processing if both current SDL and generated SDL match- // AND there is actually a current schema to compare against.- // If currentSchema is nil, we must process the diff to detect drops from previous SDL.- if currentSchema != nil && strings.TrimSpace(currentSDLText) == strings.TrimSpace(generatedSDL) {- // No changes detected between current SDL and database schema- return &schema.MetadataDiff{}, nil- }-- currentChunks, err := ChunkSDLText(currentSDLText)- if err != nil {- return nil, errors.Wrap(err, "failed to chunk current SDL text")- }-- previousChunks, err := ChunkSDLText(previousUserSDLText)- if err != nil {- return nil, errors.Wrap(err, "failed to chunk previous SDL text")- }-- // Pre-compute SDL chunks from current database metadata for performance optimization- // This avoids repeated calls to convertDatabaseSchemaToSDL and ChunkSDLText during usability checks- currentDBSDLChunks, err := buildCurrentDatabaseSDLChunks(currentSchema)- if err != nil {- return nil, errors.Wrap(err, "failed to build current database SDL chunks")- }-- // Initialize MetadataDiff- diff := &schema.MetadataDiff{- DatabaseName: "",- SchemaChanges: []*schema.SchemaDiff{},- TableChanges: []*schema.TableDiff{},- ViewChanges: []*schema.ViewDiff{},- MaterializedViewChanges: []*schema.MaterializedViewDiff{},- FunctionChanges: []*schema.FunctionDiff{},- ProcedureChanges: []*schema.ProcedureDiff{},- SequenceChanges: []*schema.SequenceDiff{},- EnumTypeChanges: []*schema.EnumTypeDiff{},- CommentChanges: []*schema.CommentDiff{},- }-- // Process table changes- err = processTableChanges(currentChunks, previousChunks, currentSchema, currentDBSDLChunks, diff)- if err != nil {- return nil, errors.Wrap(err, "failed to process table changes")- }-- // Process index changes (standalone CREATE INDEX statements)- processStandaloneIndexChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process trigger changes (standalone CREATE TRIGGER statements)- processStandaloneTriggerChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process view changes- processViewChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process materialized view changes- processMaterializedViewChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process function changes- processFunctionChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process sequence changes- processSequenceChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process enum type changes- processEnumTypeChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process extension changes- processExtensionChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process event trigger changes- processEventTriggerChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Process explicit schema changes (CREATE SCHEMA statements)- processSchemaChanges(currentChunks, previousChunks, currentSchema, diff)-- // Process comment changes (must be after all object changes are processed)- processCommentChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)-- // Add implicit schema creation for schemas referenced by new objects- // This handles the case where users define objects in a new schema without explicit CREATE SCHEMA- addImplicitSchemaCreation(diff, currentSchema)-- return diff, nil-}--// currentDatabaseSDLChunks stores pre-computed SDL chunks from current database metadata-// for performance optimization during usability checks-type currentDatabaseSDLChunks struct {- chunks map[string]string // maps chunk identifier to normalized SDL text (without comments) from current database metadata- comments map[string][]string // maps chunk identifier to normalized comment texts- columns map[string]string // maps "schema.table.column" to normalized column SDL text- constraints map[string]string // maps "schema.table.constraint" to normalized constraint SDL text-}--// buildCurrentDatabaseSDLChunks pre-computes SDL chunks from the current database schema-// for usability checks. This avoids repeated expensive calls to convertDatabaseSchemaToSDL-// and ChunkSDLText during diff processing by storing normalized SDL text from current database metadata.-func buildCurrentDatabaseSDLChunks(currentSchema *model.DatabaseMetadata) (*currentDatabaseSDLChunks, error) {- sdlChunks := ¤tDatabaseSDLChunks{- chunks: make(map[string]string),- comments: make(map[string][]string),- columns: make(map[string]string),- constraints: make(map[string]string),- }-- // Only build SDL chunks if current schema is provided- if currentSchema == nil {- return sdlChunks, nil- }-- // Generate SDL from current database metadata- currentSDL, err := convertDatabaseSchemaToSDL(currentSchema)- if err != nil {- return nil, errors.Wrap(err, "failed to convert current schema to SDL")- }-- // Parse the generated SDL to get chunks- currentSDLChunks, err := ChunkSDLText(currentSDL)- if err != nil {- return nil, errors.Wrap(err, "failed to chunk current schema SDL")- }-- // Populate SDL chunks with normalized chunk texts from current database metadata- // Use GetTextWithoutComments() to focus on structural changes only, not comment formatting differences- for identifier, chunk := range currentSDLChunks.Tables {- sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[identifier] = []string{commentText}- }-- // Extract column and constraint SDL texts for fine-grained usability checks- if err := extractColumnAndConstraintSDLTexts(chunk, identifier, sdlChunks); err != nil {- // Log error but don't fail the entire operation- // Fine-grained usability checks will fall back to table-level checks- continue- }- }- for identifier, chunk := range currentSDLChunks.Views {- sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[identifier] = []string{commentText}- }- }- for identifier, chunk := range currentSDLChunks.MaterializedViews {- sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[identifier] = []string{commentText}- }- }- for identifier, chunk := range currentSDLChunks.Functions {- sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[identifier] = []string{commentText}- }- }- for identifier, chunk := range currentSDLChunks.Sequences {- sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[identifier] = []string{commentText}- }- }- for identifier, chunk := range currentSDLChunks.Indexes {- sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[identifier] = []string{commentText}- }- }- for identifier, chunk := range currentSDLChunks.EnumTypes {- sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[identifier] = []string{commentText}- }- }- for extensionName, chunk := range currentSDLChunks.Extensions {- sdlChunks.chunks[extensionName] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[extensionName] = []string{commentText}- }- }- for eventTriggerName, chunk := range currentSDLChunks.EventTriggers {- sdlChunks.chunks[eventTriggerName] = strings.TrimSpace(chunk.GetTextWithoutComments())- // Store comment text for usability check- commentText := extractCommentTextFromChunk(chunk)- if commentText != "" {- sdlChunks.comments[eventTriggerName] = []string{commentText}- }- }-- return sdlChunks, nil-}--// extractColumnAndConstraintSDLTexts extracts individual column and constraint SDL texts-// from a table chunk for fine-grained usability checks-func extractColumnAndConstraintSDLTexts(chunk *schema.SDLChunk, tableIdentifier string, sdlChunks *currentDatabaseSDLChunks) error {- if chunk == nil || chunk.ASTNode == nil {- return nil- }-- createStmt, ok := chunk.ASTNode.(*parser.CreatestmtContext)- if !ok {- return errors.New("chunk AST node is not a CREATE TABLE statement")- }-- schemaName, tableName := parseIdentifier(tableIdentifier)-- // Extract column definitions with their SDL texts- columnDefs := extractColumnDefinitionsWithAST(createStmt)- for columnName, columnDef := range columnDefs.Map {- columnSDLText := strings.TrimSpace(getColumnText(columnDef.ASTNode))- columnKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, columnName)- sdlChunks.columns[columnKey] = columnSDLText- }-- // Extract constraint definitions with their SDL texts- // Primary Key constraints- pkDefs := extractPrimaryKeyDefinitionsWithAST(createStmt)- for constraintName, pkDef := range pkDefs {- constraintSDLText := strings.TrimSpace(getIndexText(pkDef.ASTNode))- constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, constraintName)- sdlChunks.constraints[constraintKey] = constraintSDLText- }-- // Unique constraints- uniqueDefs := extractUniqueConstraintDefinitionsInOrder(createStmt)- for _, uniqueDef := range uniqueDefs {- constraintSDLText := strings.TrimSpace(getIndexText(uniqueDef.ASTNode))- constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, uniqueDef.Name)- sdlChunks.constraints[constraintKey] = constraintSDLText- }-- // Check constraints- checkDefs := extractCheckConstraintDefinitionsWithAST(createStmt)- for _, checkDef := range checkDefs {- constraintSDLText := strings.TrimSpace(getCheckConstraintText(checkDef.ASTNode))- constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, checkDef.Name)- sdlChunks.constraints[constraintKey] = constraintSDLText- }-- // Foreign Key constraints- fkDefs := extractForeignKeyDefinitionsWithAST(createStmt)- for _, fkDef := range fkDefs {- constraintSDLText := strings.TrimSpace(getForeignKeyText(fkDef.ASTNode))- constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, fkDef.Name)- sdlChunks.constraints[constraintKey] = constraintSDLText- }-- return nil-}--// shouldSkipChunkDiffForUsability checks if a chunk should skip diff comparison-// by comparing against the pre-computed SDL chunks from current database metadata-func (sdlChunks *currentDatabaseSDLChunks) shouldSkipChunkDiffForUsability(chunkText string, chunkIdentifier string) bool {- if sdlChunks == nil || len(sdlChunks.chunks) == 0 {- return false- }-- // Get the corresponding SDL text from current database metadata- currentDatabaseSDLText, exists := sdlChunks.chunks[chunkIdentifier]- if !exists {- return false- }-- // Normalize current chunk text for comparison- normalizedChunkText := strings.TrimSpace(chunkText)-- // If chunk text matches current database metadata SDL, skip the diff (no actual change needed)- return normalizedChunkText == currentDatabaseSDLText-}--// shouldSkipCommentDiff checks if a comment should skip diff comparison-// by comparing against the pre-computed comment texts from current database metadata-func shouldSkipCommentDiff(commentText string, objectIdentifier string, sdlChunks *currentDatabaseSDLChunks) bool {- if sdlChunks == nil || len(sdlChunks.comments) == 0 {- return false- }-- // Get the corresponding comment text from current database metadata- currentDatabaseComments, exists := sdlChunks.comments[objectIdentifier]- if !exists || len(currentDatabaseComments) == 0 {- return false- }-- // Normalize current comment text for comparison- normalizedCommentText := strings.TrimSpace(commentText)-- // Check if comment matches any of the database comments (typically just one)- for _, dbComment := range currentDatabaseComments {- if normalizedCommentText == strings.TrimSpace(dbComment) {- return true- }- }-- return false-}--// shouldSkipColumnDiffForUsability checks if a column should skip diff comparison-// by comparing against the pre-computed column SDL texts from current database metadata-func (sdlChunks *currentDatabaseSDLChunks) shouldSkipColumnDiffForUsability(columnText string, schemaName, tableName, columnName string) bool {- if sdlChunks == nil || len(sdlChunks.columns) == 0 {- return false- }-- // Build column key- columnKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, columnName)-- // Get the corresponding SDL text from current database metadata- currentDatabaseColumnSDL, exists := sdlChunks.columns[columnKey]- if !exists {- return false- }-- // Normalize current column text for comparison- normalizedColumnText := strings.TrimSpace(columnText)-- // If column text matches current database metadata SDL, skip the diff (no actual change needed)- return normalizedColumnText == currentDatabaseColumnSDL-}--// shouldSkipConstraintDiffForUsability checks if a constraint should skip diff comparison-// by comparing against the pre-computed constraint SDL texts from current database metadata-func (sdlChunks *currentDatabaseSDLChunks) shouldSkipConstraintDiffForUsability(constraintText string, schemaName, tableName, constraintName string) bool {- if sdlChunks == nil || len(sdlChunks.constraints) == 0 {- return false- }-- // Build constraint key- constraintKey := fmt.Sprintf("%s.%s.%s", schemaName, tableName, constraintName)-- // Get the corresponding SDL text from current database metadata- currentDatabaseConstraintSDL, exists := sdlChunks.constraints[constraintKey]- if !exists {- return false- }-- // Normalize current constraint text for comparison- normalizedConstraintText := strings.TrimSpace(constraintText)-- // If constraint text matches current database metadata SDL, skip the diff (no actual change needed)- return normalizedConstraintText == currentDatabaseConstraintSDL-}--// convertDatabaseSchemaToSDL converts a model.DatabaseMetadata to SDL format string-// This is used in initialization scenarios where previousUserSDLText is empty-func convertDatabaseSchemaToSDL(dbMetadata *model.DatabaseMetadata) (string, error) {- if dbMetadata == nil {- return "", nil- }-- metadata := dbMetadata.GetProto()- if metadata == nil {- return "", nil- }-- // Use the existing getSDLFormat function from get_database_definition.go- return getSDLFormat(metadata)-}--// extractCheckConstraintDefinitionsWithAST extracts check constraint definitions with their AST nodes-// Note: This is a wrapper around the existing function with a different name for clarity-func extractCheckConstraintDefinitionsWithAST(createStmt *parser.CreatestmtContext) []*CheckConstraintDefWithAST {- return extractCheckConstraintDefinitionsInOrder(createStmt)-}--// extractForeignKeyDefinitionsWithAST extracts foreign key constraint definitions with their AST nodes-// Note: This is a wrapper around the existing function with a different name for clarity-func extractForeignKeyDefinitionsWithAST(createStmt *parser.CreatestmtContext) []*ForeignKeyDefWithAST {- return extractForeignKeyDefinitionsInOrder(createStmt)-}--// PrimaryKeyDefWithAST represents a primary key constraint definition with its AST node-type PrimaryKeyDefWithAST struct {- Name string- ASTNode parser.ITableconstraintContext-}--// UniqueKeyDefWithAST represents a unique key constraint definition with its AST node-type UniqueKeyDefWithAST struct {- Name string- ASTNode parser.ITableconstraintContext-}--// extractFunctionSignatureFromAST extracts function signature from CREATE FUNCTION AST node-// This is the unified function used by both chunk extraction and function comparison-func extractFunctionSignatureFromAST(ctx *parser.CreatefunctionstmtContext) string {- if ctx == nil {- return ""- }-- funcNameCtx := ctx.Func_name()- if funcNameCtx == nil {- return ""- }-- // Extract function name using proper normalization- funcNameParts := pgparser.NormalizePostgreSQLFuncName(funcNameCtx)- if len(funcNameParts) == 0 {- return ""- }-- // Get the function name (without schema)- var functionName string- if len(funcNameParts) == 2 {- // Schema qualified: schema.function_name- functionName = funcNameParts[1]- } else if len(funcNameParts) == 1 {- // Unqualified: function_name- functionName = funcNameParts[0]- } else {- // Unexpected format- return ""- }-- if functionName == "" {- return ""- }-- // Use the existing ExtractFunctionSignature function- return ExtractFunctionSignature(ctx, functionName)-}--// functionExtractor is a walker to extract CREATE FUNCTION AST nodes-type functionExtractor struct {- parser.BasePostgreSQLParserListener- result **parser.CreatefunctionstmtContext-}--func (e *functionExtractor) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {- if e.result != nil && *e.result == nil {- *e.result = ctx- }-}--// processCommentChanges processes comment changes for all database objects-// It must be called after all object changes have been processed to determine-// which objects were created or dropped (those should not generate comment diffs)--// processEnumTypeChanges analyzes enum type changes between current and previous chunks-// Enum types use DROP + CREATE pattern for modifications (PostgreSQL doesn't support ALTER TYPE ... RENAME VALUE)--// processExtensionChanges processes extension changes between current and previous chunks--// processEventTriggerChanges processes event trigger changes between current and previous chunks--// extractTableNameFromTrigger extracts the fully qualified table name from CREATE TRIGGER ... ON table-// Returns schema.table format, defaulting to public schema if not specified-func extractTableNameFromTrigger(ctx *parser.CreatetrigstmtContext) string {- if ctx == nil {- return ""- }-- // Find ON clause - the table name comes after ON keyword- // In PostgreSQL grammar: CREATE TRIGGER name ... ON qualified_name- if ctx.Qualified_name() == nil {- return ""- }-- // Extract table name- qualifiedNameParts := pgparser.NormalizePostgreSQLQualifiedName(ctx.Qualified_name())- if len(qualifiedNameParts) == 0 {- return ""- }-- // Return fully qualified name (schema.table)- if len(qualifiedNameParts) == 1 {- // No schema specified, default to public- return "public." + qualifiedNameParts[0]- }- // Schema is specified- return strings.Join(qualifiedNameParts, ".")-}diff --git a/backend/plugin/schema/pg/get_sdl_diff_comment_test.go b/backend/plugin/schema/pg/get_sdl_diff_comment_test.godeleted file mode 100644index 7600e7cd19..0000000000--- a/backend/plugin/schema/pg/get_sdl_diff_comment_test.go+++ /dev/null@@ -1,663 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// TestCommentDiff_TableComment tests table-level comment changes-func TestCommentDiff_TableComment(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantCommentDiffCount int- wantAction schema.MetadataDiffAction- wantObjectType schema.CommentObjectType- wantOldComment string- wantNewComment string- }{- {- name: "add table comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'User information table';`,- wantCommentDiffCount: 1,- wantAction: schema.MetadataDiffActionCreate,- wantObjectType: schema.CommentObjectTypeTable,- wantOldComment: "",- wantNewComment: "User information table",- },- {- name: "modify table comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'Old comment';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'New comment';`,- wantCommentDiffCount: 1,- wantAction: schema.MetadataDiffActionAlter,- wantObjectType: schema.CommentObjectTypeTable,- wantOldComment: "Old comment",- wantNewComment: "New comment",- },- {- name: "remove table comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'User table';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS NULL;`,- wantCommentDiffCount: 1,- wantAction: schema.MetadataDiffActionAlter,- wantObjectType: schema.CommentObjectTypeTable,- wantOldComment: "User table",- wantNewComment: "",- },- {- name: "no comment change",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'Same comment';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'Same comment';`,- wantCommentDiffCount: 0,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")-- if tt.wantCommentDiffCount > 0 {- commentDiff := diff.CommentChanges[0]- require.Equal(t, tt.wantAction, commentDiff.Action, "unexpected action")- require.Equal(t, tt.wantObjectType, commentDiff.ObjectType, "unexpected object type")- require.Equal(t, tt.wantOldComment, commentDiff.OldComment, "unexpected old comment")- require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")- require.Equal(t, "public", commentDiff.SchemaName, "unexpected schema name")- require.Equal(t, "users", commentDiff.ObjectName, "unexpected object name")- }- })- }-}--// TestCommentDiff_ColumnComment tests column-level comment changes-func TestCommentDiff_ColumnComment(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantCommentDiffCount int- wantAction schema.MetadataDiffAction- wantColumnName string- wantOldComment string- wantNewComment string- }{- {- name: "add column comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN users.name IS 'User full name';`,- wantCommentDiffCount: 1,- wantAction: schema.MetadataDiffActionCreate,- wantColumnName: "name",- wantOldComment: "",- wantNewComment: "User full name",- },- {- name: "modify column comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN users.name IS 'Old name';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN users.name IS 'New name';`,- wantCommentDiffCount: 1,- wantAction: schema.MetadataDiffActionAlter,- wantColumnName: "name",- wantOldComment: "Old name",- wantNewComment: "New name",- },- {- name: "multiple column comments",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT,- email TEXT-);--COMMENT ON COLUMN users.name IS 'User name';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT,- email TEXT-);--COMMENT ON COLUMN users.name IS 'Full name';-COMMENT ON COLUMN users.email IS 'Email address';`,- wantCommentDiffCount: 2,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")-- if tt.wantCommentDiffCount == 1 {- commentDiff := diff.CommentChanges[0]- require.Equal(t, tt.wantAction, commentDiff.Action, "unexpected action")- require.Equal(t, schema.CommentObjectTypeColumn, commentDiff.ObjectType, "unexpected object type")- require.Equal(t, tt.wantColumnName, commentDiff.ColumnName, "unexpected column name")- require.Equal(t, tt.wantOldComment, commentDiff.OldComment, "unexpected old comment")- require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")- }- })- }-}--// TestCommentDiff_ViewComment tests view-level comment changes-func TestCommentDiff_ViewComment(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantCommentDiffCount int- wantNewComment string- }{- {- name: "add view comment",- previousSDL: `-CREATE VIEW user_view AS SELECT * FROM users;`,- currentSDL: `-CREATE VIEW user_view AS SELECT * FROM users;--COMMENT ON VIEW user_view IS 'User view description';`,- wantCommentDiffCount: 1,- wantNewComment: "User view description",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")-- if tt.wantCommentDiffCount > 0 {- commentDiff := diff.CommentChanges[0]- require.Equal(t, schema.CommentObjectTypeView, commentDiff.ObjectType, "unexpected object type")- require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")- }- })- }-}--// TestCommentDiff_SequenceComment tests sequence-level comment changes-func TestCommentDiff_SequenceComment(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantCommentDiffCount int- wantNewComment string- }{- {- name: "add sequence comment",- previousSDL: `-CREATE SEQUENCE user_id_seq;`,- currentSDL: `-CREATE SEQUENCE user_id_seq;--COMMENT ON SEQUENCE user_id_seq IS 'User ID sequence';`,- wantCommentDiffCount: 1,- wantNewComment: "User ID sequence",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")-- if tt.wantCommentDiffCount > 0 {- commentDiff := diff.CommentChanges[0]- require.Equal(t, schema.CommentObjectTypeSequence, commentDiff.ObjectType, "unexpected object type")- require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")- }- })- }-}--// TestCommentDiff_FunctionComment tests function-level comment changes-func TestCommentDiff_FunctionComment(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantCommentDiffCount int- wantNewComment string- }{- {- name: "add function comment",- previousSDL: `-CREATE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$-BEGIN- RETURN a + b;-END;-$$ LANGUAGE plpgsql;`,- currentSDL: `-CREATE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$-BEGIN- RETURN a + b;-END;-$$ LANGUAGE plpgsql;--COMMENT ON FUNCTION add_numbers(INT, INT) IS 'Adds two numbers';`,- wantCommentDiffCount: 1,- wantNewComment: "Adds two numbers",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")-- if tt.wantCommentDiffCount > 0 {- commentDiff := diff.CommentChanges[0]- require.Equal(t, schema.CommentObjectTypeFunction, commentDiff.ObjectType, "unexpected object type")- require.Equal(t, tt.wantNewComment, commentDiff.NewComment, "unexpected new comment")- }- })- }-}--// TestCommentDiff_ObjectCreateWithComment tests that creating an object with comment generates CommentDiff-func TestCommentDiff_ObjectCreateWithComment(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantTableDiffCount int- wantCommentDiffCount int- }{- {- name: "create table with comment",- previousSDL: ``,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'User table';`,- wantTableDiffCount: 1,- wantCommentDiffCount: 1, // CommentDiff is generated for the table comment- },- {- name: "create table with column comment",- previousSDL: ``,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN users.name IS 'User name';`,- wantTableDiffCount: 1,- wantCommentDiffCount: 1, // CommentDiff is generated for the column comment- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")-- if tt.wantTableDiffCount > 0 {- require.Equal(t, schema.MetadataDiffActionCreate, diff.TableChanges[0].Action, "expected CREATE action")- }- })- }-}--// TestCommentDiff_ObjectDropWithComment tests that dropping an object with comment doesn't generate CommentDiff-func TestCommentDiff_ObjectDropWithComment(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantTableDiffCount int- wantCommentDiffCount int- }{- {- name: "drop table with comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'User table';`,- currentSDL: ``,- wantTableDiffCount: 1,- wantCommentDiffCount: 0, // No comment diff because table is dropped- },- {- name: "drop table with column comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN users.name IS 'User name';`,- currentSDL: ``,- wantTableDiffCount: 1,- wantCommentDiffCount: 0, // No comment diff because table is dropped- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")-- if tt.wantTableDiffCount > 0 {- require.Equal(t, schema.MetadataDiffActionDrop, diff.TableChanges[0].Action, "expected DROP action")- }- })- }-}--// TestCommentDiff_CommentOnlyChange tests that comment-only changes don't trigger object diffs-func TestCommentDiff_CommentOnlyChange(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantTableDiffCount int- wantCommentDiffCount int- }{- {- name: "only table comment changes",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'Old comment';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'New comment';`,- wantTableDiffCount: 0, // No table diff, only comment changed- wantCommentDiffCount: 1,- },- {- name: "only column comment changes",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN users.name IS 'Old name';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN users.name IS 'New name';`,- wantTableDiffCount: 0, // No table diff, only comment changed- wantCommentDiffCount: 1,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")- })- }-}--// TestCommentDiff_TableChangeWithCommentChange tests table changes with comment changes-func TestCommentDiff_TableChangeWithCommentChange(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantTableDiffCount int- wantCommentDiffCount int- }{- {- name: "add column and change comment",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY-);--COMMENT ON TABLE users IS 'Old comment';`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON TABLE users IS 'New comment';`,- wantTableDiffCount: 1, // Table changed (column added)- wantCommentDiffCount: 1, // Comment also changed- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, tt.wantTableDiffCount, len(diff.TableChanges), "unexpected number of table diffs")- require.Equal(t, tt.wantCommentDiffCount, len(diff.CommentChanges), "unexpected number of comment diffs")- })- }-}--// TestCommentDiff_SpecialCharacters tests comment handling with special characters-func TestCommentDiff_SpecialCharacters(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantNewComment string- }{- {- name: "comment with single quote",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY-);`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY-);--COMMENT ON TABLE users IS 'User''s table';`,- wantNewComment: "User''s table", // Escaped single quote- },- {- name: "comment with newlines and special chars",- previousSDL: `-CREATE TABLE users (- id INT PRIMARY KEY-);`,- currentSDL: `-CREATE TABLE users (- id INT PRIMARY KEY-);--COMMENT ON TABLE users IS 'Line 1-Line 2';`,- wantNewComment: "Line 1\nLine 2",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, 1, len(diff.CommentChanges), "unexpected number of comment diffs")- require.Equal(t, tt.wantNewComment, diff.CommentChanges[0].NewComment, "unexpected new comment")- })- }-}--// TestCommentDiff_SchemaQualified tests schema-qualified object names-func TestCommentDiff_SchemaQualified(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantSchemaName string- wantObjectName string- }{- {- name: "schema qualified table",- previousSDL: `-CREATE TABLE myschema.users (- id INT PRIMARY KEY-);`,- currentSDL: `-CREATE TABLE myschema.users (- id INT PRIMARY KEY-);--COMMENT ON TABLE myschema.users IS 'Users table';`,- wantSchemaName: "myschema",- wantObjectName: "users",- },- {- name: "schema qualified column",- previousSDL: `-CREATE TABLE myschema.users (- id INT PRIMARY KEY,- name TEXT-);`,- currentSDL: `-CREATE TABLE myschema.users (- id INT PRIMARY KEY,- name TEXT-);--COMMENT ON COLUMN myschema.users.name IS 'User name';`,- wantSchemaName: "myschema",- wantObjectName: "users",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- require.Equal(t, 1, len(diff.CommentChanges), "unexpected number of comment diffs")- require.Equal(t, tt.wantSchemaName, diff.CommentChanges[0].SchemaName, "unexpected schema name")- require.Equal(t, tt.wantObjectName, diff.CommentChanges[0].ObjectName, "unexpected object name")- })- }-}diff --git a/backend/plugin/schema/pg/get_sdl_diff_test.go b/backend/plugin/schema/pg/get_sdl_diff_test.godeleted file mode 100644index de7d967feb..0000000000--- a/backend/plugin/schema/pg/get_sdl_diff_test.go+++ /dev/null@@ -1,352 +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/store/model"-)--func TestGetSDLDiff_InitializationScenario(t *testing.T) {- tests := []struct {- name string- currentSDLText string- previousUserSDLText string- currentSchema *model.DatabaseMetadata- previousSchema *model.DatabaseMetadata- expectedDiffEmpty bool- expectedTableChanges int- expectedViewChanges int- expectedFunctionChanges int- expectedSequenceChanges int- }{- {- name: "initialization_with_empty_previous_SDL",- currentSDLText: "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL);",- previousUserSDLText: "", // Empty - initialization scenario- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- 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,- },- },- },- },- Sequences: []*storepb.SequenceMetadata{- {- Name: "users_id_seq",- OwnerTable: "users",- OwnerColumn: "id",- },- },- },- },- },- nil, // schema bytes- nil, // config- storepb.Engine_POSTGRES,- false, // isObjectCaseSensitive- ),- previousSchema: nil,- expectedDiffEmpty: false, // Diff may be generated due to format differences- expectedTableChanges: 1, // One table creation- expectedViewChanges: 0,- expectedFunctionChanges: 0,- expectedSequenceChanges: 0,- },- {- name: "initialization_with_complex_schema",- currentSDLText: "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL); CREATE VIEW user_view AS SELECT * FROM users;",- previousUserSDLText: "", // Empty - initialization scenario- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- 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,- },- },- },- },- Views: []*storepb.ViewMetadata{- {- Name: "user_view",- Definition: "SELECT * FROM users",- },- },- Sequences: []*storepb.SequenceMetadata{- {- Name: "users_id_seq",- OwnerTable: "users",- OwnerColumn: "id",- },- },- },- },- },- nil, // schema bytes- nil, // config- storepb.Engine_POSTGRES,- false, // isObjectCaseSensitive- ),- previousSchema: nil,- expectedDiffEmpty: false, // Diff may be generated due to format differences- expectedTableChanges: 1, // One table creation- expectedViewChanges: 0,- expectedFunctionChanges: 0,- expectedSequenceChanges: 0,- },- {- name: "non_initialization_normal_diff",- currentSDLText: "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT);",- previousUserSDLText: "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL);", // Non-empty - normal diff- currentSchema: nil, // Not used in non-initialization scenario- previousSchema: nil, // Not used in non-initialization scenario- expectedDiffEmpty: false,- expectedTableChanges: 1, // One table changed (column added)- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDLText, tt.previousUserSDLText, tt.currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- if tt.expectedDiffEmpty {- // In initialization scenario, we expect minimal or no changes- // since we're comparing against a generated baseline- require.LessOrEqual(t, len(diff.TableChanges), tt.expectedTableChanges)- require.LessOrEqual(t, len(diff.ViewChanges), tt.expectedViewChanges)- require.LessOrEqual(t, len(diff.FunctionChanges), tt.expectedFunctionChanges)- require.LessOrEqual(t, len(diff.SequenceChanges), tt.expectedSequenceChanges)- } else {- // In normal diff scenario, we expect specific changes- require.Equal(t, tt.expectedTableChanges, len(diff.TableChanges))- }- })- }-}--func TestConvertDatabaseSchemaToSDL(t *testing.T) {- tests := []struct {- name string- dbMetadata *model.DatabaseMetadata- expectedSDL string- expectedError bool- }{- {- name: "nil_schema",- dbMetadata: nil,- expectedSDL: "",- expectedError: false,- },- {- name: "empty_metadata",- dbMetadata: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{}, // empty but not nil metadata- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSDL: "",- expectedError: false,- },- {- name: "simple_table_schema",- dbMetadata: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- {- Name: "name",- Type: "text",- Nullable: false,- },- },- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSDL: "CREATE TABLE users (\n id integer NOT NULL,\n name text NOT NULL\n);",- expectedError: false,- },- {- name: "schema_with_materialized_view",- dbMetadata: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "products",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- {- Name: "name",- Type: "text",- Nullable: false,- },- {- Name: "price",- Type: "numeric",- Nullable: false,- },- },- },- },- MaterializedViews: []*storepb.MaterializedViewMetadata{- {- Name: "product_summary_mv",- Definition: "SELECT id, name, price FROM products",- Comment: "Summary of products",- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSDL: "CREATE MATERIALIZED VIEW",- expectedError: false,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- result, err := convertDatabaseSchemaToSDL(tt.dbMetadata)-- if tt.expectedError {- require.Error(t, err)- } else {- require.NoError(t, err)- switch tt.expectedSDL {- case "":- require.Equal(t, tt.expectedSDL, result)- case "CREATE TABLE users (\n id integer NOT NULL,\n name text NOT NULL\n);":- require.Contains(t, result, "CREATE TABLE")- require.Contains(t, result, "users")- case "CREATE MATERIALIZED VIEW":- require.Contains(t, result, "CREATE MATERIALIZED VIEW")- require.Contains(t, result, "product_summary_mv")- require.Contains(t, result, "COMMENT ON MATERIALIZED VIEW")- require.Contains(t, result, "Summary of products")- default:- require.Equal(t, tt.expectedSDL, result)- }- }- })- }-}--// TestGetSDLDiff_MinimalChangesScenario tests the minimal changes functionality-// This is a basic test to verify that the drift detection logic is invoked-func TestGetSDLDiff_MinimalChangesScenario(t *testing.T) {- currentSDLText := "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL);"- previousUserSDLText := `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name TEXT NOT NULL- );`-- currentSchema := model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: false},- },- },- {- Name: "posts", // This table exists in current schema but not in previous- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "title", Type: "text", Nullable: false},- },- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- )-- // Test that the function runs without error when schemas are provided- diff, err := GetSDLDiff(currentSDLText, previousUserSDLText, currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // We don't assert specific outcomes here because the main goal is to test- // that the minimal changes logic is invoked and doesn't crash- t.Logf("Generated %d table changes", len(diff.TableChanges))- for i, change := range diff.TableChanges {- t.Logf("Change %d: %s %s", i+1, change.Action, change.TableName)- }-}diff --git a/backend/plugin/schema/pg/index_comparer_test.go b/backend/plugin/schema/pg/index_comparer_test.godeleted file mode 100644index 8b240462e4..0000000000--- a/backend/plugin/schema/pg/index_comparer_test.go+++ /dev/null@@ -1,210 +0,0 @@-package pg--import (- "testing"-)--func TestPostgreSQLIndexComparer_ExtractWhereClauseFromIndexDef(t *testing.T) {- comparer := &PostgreSQLIndexComparer{}-- tests := []struct {- name string- indexDef string- expectedWhere string- }{- {- name: "Index with simple WHERE clause",- indexDef: "CREATE INDEX idx_users_active ON users (email) WHERE active = true",- expectedWhere: "active = true",- },- {- name: "Index with complex WHERE clause",- indexDef: "CREATE INDEX idx_orders_status ON orders (customer_id) WHERE status IN ('pending', 'processing')",- expectedWhere: "status IN ('pending', 'processing')",- },- {- name: "Index with function in WHERE clause",- indexDef: `CREATE INDEX idx_text_search ON articles (title) WHERE "length"(content) > 100`,- expectedWhere: `"length"(content) > 100`,- },- {- name: "Index without WHERE clause",- indexDef: "CREATE INDEX idx_users_email ON users (email)",- expectedWhere: "",- },- {- name: "UNIQUE index with WHERE clause",- indexDef: "CREATE UNIQUE INDEX idx_users_email_active ON users (email) WHERE active = true",- expectedWhere: "active = true",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- result := comparer.ExtractWhereClauseFromIndexDef(tt.indexDef)- if result != tt.expectedWhere {- t.Errorf("ExtractWhereClauseFromIndexDef() = %q, want %q", result, tt.expectedWhere)- }- })- }-}--func TestPostgreSQLIndexComparer_CompareIndexWhereConditions(t *testing.T) {- comparer := &PostgreSQLIndexComparer{}-- tests := []struct {- name string- def1 string- def2 string- expected bool- }{- {- name: "Same WHERE conditions",- def1: "CREATE INDEX idx1 ON table1 (col1) WHERE active = true",- def2: "CREATE INDEX idx2 ON table2 (col2) WHERE active = true",- expected: true,- },- {- name: "Different WHERE conditions",- def1: "CREATE INDEX idx1 ON table1 (col1) WHERE active = true",- def2: "CREATE INDEX idx2 ON table2 (col2) WHERE status = 'pending'",- expected: false,- },- {- name: "Both without WHERE clause",- def1: "CREATE INDEX idx1 ON table1 (col1)",- def2: "CREATE INDEX idx2 ON table2 (col2)",- expected: true,- },- {- name: "One with WHERE, one without",- def1: "CREATE INDEX idx1 ON table1 (col1) WHERE active = true",- def2: "CREATE INDEX idx2 ON table2 (col2)",- expected: false,- },- {- name: "Function names with different quoting should be equal",- def1: `CREATE INDEX idx1 ON table1 (LEFT(text_col, 50)) WHERE active = true`,- def2: `CREATE INDEX idx2 ON table2 ("left"(text_col, 50)) WHERE active = true`,- expected: true,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- result := comparer.CompareIndexWhereConditions(tt.def1, tt.def2)- if result != tt.expected {- t.Errorf("CompareIndexWhereConditions() = %v, want %v", result, tt.expected)- }- })- }-}--func TestPostgreSQLIndexComparer_ExtractIncludeClauseFromIndexDef(t *testing.T) {- comparer := &PostgreSQLIndexComparer{}-- tests := []struct {- name string- indexDef string- expectedInclude string- }{- {- name: "Index with INCLUDE clause",- indexDef: "CREATE INDEX idx_covering ON products (price) INCLUDE (name, description)",- expectedInclude: "INCLUDE (name, description)",- },- {- name: "Index with INCLUDE and WHERE clause",- indexDef: "CREATE INDEX idx_covering ON orders (status) INCLUDE (total, customer_name) WHERE (status = 'active'::text)",- expectedInclude: "INCLUDE (total, customer_name)",- },- {- name: "Index without INCLUDE clause",- indexDef: "CREATE INDEX idx_simple ON users (email)",- expectedInclude: "",- },- {- name: "UNIQUE index with INCLUDE",- indexDef: "CREATE UNIQUE INDEX idx_unique_covering ON products (id) INCLUDE (name)",- expectedInclude: "INCLUDE (name)",- },- {- name: "Empty definition",- indexDef: "",- expectedInclude: "",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- result := comparer.ExtractIncludeClauseFromIndexDef(tt.indexDef)- if result != tt.expectedInclude {- t.Errorf("ExtractIncludeClauseFromIndexDef() = %q, want %q", result, tt.expectedInclude)- }- })- }-}--func TestPostgreSQLIndexComparer_ParseIndexDefinition(t *testing.T) {- comparer := &PostgreSQLIndexComparer{}-- tests := []struct {- name string- indexDef string- expectError bool- expected *IndexDefinition- }{- {- name: "Simple index",- indexDef: "CREATE INDEX idx_users_email ON users (email)",- expectError: false,- expected: &IndexDefinition{- IndexName: "idx_users_email",- TableName: "users",- Unique: false,- WhereClause: "",- },- },- {- name: "UNIQUE index with WHERE clause",- indexDef: "CREATE UNIQUE INDEX idx_users_email_active ON users (email) WHERE active = true",- expectError: false,- expected: &IndexDefinition{- IndexName: "idx_users_email_active",- TableName: "users",- Unique: true,- WhereClause: "active = true",- },- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- result, err := comparer.ParseIndexDefinition(tt.indexDef)-- if tt.expectError && err == nil {- t.Error("ParseIndexDefinition() expected error, but got none")- return- }- if !tt.expectError && err != nil {- t.Errorf("ParseIndexDefinition() unexpected error: %v", err)- return- }-- if tt.expected != nil {- if result.IndexName != tt.expected.IndexName {- t.Errorf("ParseIndexDefinition() IndexName = %q, want %q", result.IndexName, tt.expected.IndexName)- }- if result.TableName != tt.expected.TableName {- t.Errorf("ParseIndexDefinition() TableName = %q, want %q", result.TableName, tt.expected.TableName)- }- if result.Unique != tt.expected.Unique {- t.Errorf("ParseIndexDefinition() Unique = %v, want %v", result.Unique, tt.expected.Unique)- }- if result.WhereClause != tt.expected.WhereClause {- t.Errorf("ParseIndexDefinition() WhereClause = %q, want %q", result.WhereClause, tt.expected.WhereClause)- }- }- })- }-}diff --git a/backend/plugin/schema/pg/materialized_view_index_test.go b/backend/plugin/schema/pg/materialized_view_index_test.goindex dcd7a35c5b..d17347f363 100644--- a/backend/plugin/schema/pg/materialized_view_index_test.go+++ b/backend/plugin/schema/pg/materialized_view_index_test.go@@ -245,57 +245,4 @@ func TestMaterializedViewIndexDependencyOrdering(t *testing.T) {// so we don't need (and shouldn't have) separate DROP INDEX statementsrequire.NotContains(t, migration, "DROP INDEX")})-- t.Run("ADD Index to existing MV via SDL chunks", func(t *testing.T) {- // This tests the exact scenario from the user's screenshot:- // Adding an index to an existing materialized view should generate- // CREATE INDEX statement without trying to recreate the MV-- // Previous SDL (MV without index)- previousSDL := `CREATE MATERIALIZED VIEW "public"."customer_segmentation_mv" AS- SELECT customer_id, SUM(amount) as total_spending- FROM orders- GROUP BY customer_id;`-- // Current SDL (MV with new index)- currentSDL := `CREATE MATERIALIZED VIEW "public"."customer_segmentation_mv" AS- SELECT customer_id, SUM(amount) as total_spending- FROM orders- GROUP BY customer_id;-- CREATE INDEX "idx_customer_seg_spending" ON "public"."customer_segmentation_mv" (total_spending);`-- // Get SDL diff (simulates bb rollout behavior)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Debug: log all diffs- t.Logf("MaterializedViewChanges: %d", len(diff.MaterializedViewChanges))- for i, mvDiff := range diff.MaterializedViewChanges {- t.Logf(" MV[%d]: Action=%v, Schema=%s, Name=%s, IndexChanges=%d, HasNewMV=%v, HasNewAST=%v",- i, mvDiff.Action, mvDiff.SchemaName, mvDiff.MaterializedViewName,- len(mvDiff.IndexChanges), mvDiff.NewMaterializedView != nil, mvDiff.NewASTNode != nil)- }-- // Verify we have a MaterializedViewDiff with IndexChanges- require.Equal(t, 1, len(diff.MaterializedViewChanges), "Should have 1 MV change")- mvDiff := diff.MaterializedViewChanges[0]- require.Equal(t, schema.MetadataDiffActionAlter, mvDiff.Action)- require.Equal(t, "public", mvDiff.SchemaName)- require.Equal(t, "customer_segmentation_mv", mvDiff.MaterializedViewName)- require.Equal(t, 1, len(mvDiff.IndexChanges), "Should have 1 index change")- require.Equal(t, schema.MetadataDiffActionCreate, mvDiff.IndexChanges[0].Action)-- // Generate migration- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- t.Logf("Generated migration:\n%s", migration)-- // Verify migration only creates the index, doesn't recreate the MV- require.Contains(t, migration, "CREATE INDEX")- require.Contains(t, migration, "idx_customer_seg_spending")- require.NotContains(t, migration, "DROP MATERIALIZED VIEW", "Should not drop MV for index-only changes")- require.NotContains(t, migration, "CREATE MATERIALIZED VIEW", "Should not recreate MV for index-only changes")- })}diff --git a/backend/plugin/schema/pg/materialized_view_sdl_diff_test.go b/backend/plugin/schema/pg/materialized_view_sdl_diff_test.godeleted file mode 100644index 9c47d97de8..0000000000--- a/backend/plugin/schema/pg/materialized_view_sdl_diff_test.go+++ /dev/null@@ -1,706 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestMaterializedViewSDLDiff(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMaterializedViewChanges int- expectedActions []schema.MetadataDiffAction- }{- {- name: "Create new materialized view",- previousSDL: ``,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW active_users_mv AS- SELECT id, name- FROM users- WHERE active = true;- `,- expectedMaterializedViewChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Drop materialized view",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW active_users_mv AS- SELECT id, name- FROM users- WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );- `,- expectedMaterializedViewChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},- },- {- name: "Modify materialized view (drop and recreate)",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW active_users_mv AS- SELECT id, name- FROM users- WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW active_users_mv AS- SELECT id, name, 'active' as status- FROM users- WHERE active = true;- `,- expectedMaterializedViewChanges: 2, // Drop + Create- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop, schema.MetadataDiffActionCreate},- },- {- name: "No changes to materialized view",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW active_users_mv AS- SELECT id, name- FROM users- WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW active_users_mv AS- SELECT id, name- FROM users- WHERE active = true;- `,- expectedMaterializedViewChanges: 0,- expectedActions: []schema.MetadataDiffAction{},- },- {- name: "Multiple materialized views with different changes",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW all_users_mv AS- SELECT * FROM users;-- CREATE MATERIALIZED VIEW active_users_mv AS- SELECT id, name FROM users WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE MATERIALIZED VIEW all_users_mv AS- SELECT * FROM users;-- CREATE MATERIALIZED VIEW admin_users_mv AS- SELECT id, name FROM users WHERE role = 'admin';- `,- expectedMaterializedViewChanges: 2, // Drop active_users_mv + Create admin_users_mv- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate, schema.MetadataDiffActionDrop},- },- {- name: "Schema-qualified materialized view names",- previousSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );- `,- currentSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW test_schema.product_summary_mv AS- SELECT id, name FROM test_schema.products;- `,- expectedMaterializedViewChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Materialized view with comment",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE MATERIALIZED VIEW user_summary_mv AS- SELECT id, name FROM users;-- COMMENT ON MATERIALIZED VIEW user_summary_mv IS 'Summary of all users';- `,- expectedMaterializedViewChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- assert.Equal(t, tt.expectedMaterializedViewChanges, len(diff.MaterializedViewChanges),- "Expected %d materialized view changes, got %d", tt.expectedMaterializedViewChanges, len(diff.MaterializedViewChanges))-- // Check that the actions match expectations- var actualActions []schema.MetadataDiffAction- for _, mvDiff := range diff.MaterializedViewChanges {- actualActions = append(actualActions, mvDiff.Action)- }-- // Handle nil vs empty slice comparison- if len(tt.expectedActions) == 0 && len(actualActions) == 0 {- // Both are effectively empty - test passes- t.Log("Both expected and actual actions are empty")- } else {- assert.ElementsMatch(t, tt.expectedActions, actualActions,- "Expected actions %v, got %v", tt.expectedActions, actualActions)- }-- // Verify AST nodes are properly set- for i, mvDiff := range diff.MaterializedViewChanges {- switch mvDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, mvDiff.NewASTNode,- "Materialized view diff %d should have NewASTNode for CREATE action", i)- assert.Nil(t, mvDiff.OldASTNode,- "Materialized view diff %d should not have OldASTNode for CREATE action", i)- case schema.MetadataDiffActionDrop:- assert.NotNil(t, mvDiff.OldASTNode,- "Materialized view diff %d should have OldASTNode for DROP action", i)- assert.Nil(t, mvDiff.NewASTNode,- "Materialized view diff %d should not have NewASTNode for DROP action", i)- default:- t.Errorf("Unexpected action %v for materialized view diff %d", mvDiff.Action, i)- }- }- })- }-}--func TestMaterializedViewWithCommentParsing(t *testing.T) {- tests := []struct {- name string- sdl string- expectedMVCount int- expectedComment string- expectCommentInSDL bool- }{- {- name: "Create materialized view with comment",- sdl: `- CREATE TABLE public.users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE MATERIALIZED VIEW public.user_summary_mv AS- SELECT id, name FROM users;-- COMMENT ON MATERIALIZED VIEW public.user_summary_mv IS 'Summary of all users';- `,- expectedMVCount: 1,- expectedComment: "Summary of all users",- expectCommentInSDL: true,- },- {- name: "Create materialized view with schema-qualified name and comment",- sdl: `- CREATE TABLE public.table1 (- id INTEGER PRIMARY KEY,- name TEXT- );-- CREATE MATERIALIZED VIEW "public"."table1_summary" AS- SELECT t2.id AS table2_id, t2.name AS table2_name- FROM "public"."table1" t2;-- COMMENT ON MATERIALIZED VIEW "public"."table1_summary" IS 'Summarizes table1 records grouped by table2 categories. Refresh periodically to keep data current.';- `,- expectedMVCount: 1,- expectedComment: "Summarizes table1 records grouped by table2 categories. Refresh periodically to keep data current.",- expectCommentInSDL: true,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Parse the SDL to get chunks- chunks, err := ChunkSDLText(tt.sdl)- require.NoError(t, err)- require.NotNil(t, chunks)-- // Debug: Print what we got- t.Logf("Found %d materialized views", len(chunks.MaterializedViews))- for id, chunk := range chunks.MaterializedViews {- t.Logf(" MV ID: %s", id)- t.Logf(" Has ASTNode: %v", chunk.ASTNode != nil)- t.Logf(" CommentStatements: %d", len(chunk.CommentStatements))- }-- // Verify we parsed the correct number of materialized views- assert.Equal(t, tt.expectedMVCount, len(chunks.MaterializedViews),- "Expected %d materialized view(s), got %d", tt.expectedMVCount, len(chunks.MaterializedViews))-- // Get the materialized view chunk- var mvChunk *schema.SDLChunk- for _, chunk := range chunks.MaterializedViews {- mvChunk = chunk- break- }- require.NotNil(t, mvChunk, "Materialized view chunk should exist")-- // Get the full text including comments- fullText := mvChunk.GetText()- t.Logf("Full materialized view text:\n%s", fullText)-- // Verify comment is included in the SDL- if tt.expectCommentInSDL {- assert.Contains(t, fullText, "COMMENT ON MATERIALIZED VIEW",- "Full text should contain COMMENT ON MATERIALIZED VIEW statement")- assert.Contains(t, fullText, tt.expectedComment,- "Comment should contain the expected text: %s", tt.expectedComment)-- // Verify comment statements count- assert.Greater(t, len(mvChunk.CommentStatements), 0,- "Materialized view should have comment statements")- }-- // Verify CREATE MATERIALIZED VIEW is present- assert.Contains(t, fullText, "CREATE MATERIALIZED VIEW",- "Full text should contain CREATE MATERIALIZED VIEW statement")- })- }-}--func TestMaterializedViewMigrationGeneration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantCreate bool- wantDrop bool- }{- {- name: "Create materialized view generates CREATE statement",- previousSDL: ``,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;- `,- wantCreate: true,- wantDrop: false,- },- {- name: "Drop materialized view generates DROP statement",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );- `,- wantCreate: false,- wantDrop: true,- },- {- name: "Modify materialized view generates DROP and CREATE",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255),- price DECIMAL- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name, price FROM products;- `,- wantCreate: true,- wantDrop: true,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- migration, err := generateMigration(diff)- require.NoError(t, err)-- if tt.wantCreate {- assert.Contains(t, migration, "CREATE MATERIALIZED VIEW",- "Expected migration to contain CREATE MATERIALIZED VIEW statement")- }- if tt.wantDrop {- assert.Contains(t, migration, "DROP MATERIALIZED VIEW",- "Expected migration to contain DROP MATERIALIZED VIEW statement")- }- })- }-}--func TestMaterializedViewCommentMigrationGeneration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- wantComment bool- }{- {- name: "Create materialized view with comment generates COMMENT statement",- previousSDL: ``,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;-- COMMENT ON MATERIALIZED VIEW product_mv IS 'Product summary view';- `,- wantComment: true,- },- {- name: "Add comment to existing materialized view",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;-- COMMENT ON MATERIALIZED VIEW product_mv IS 'Product summary view';- `,- wantComment: true,- },- {- name: "Remove comment from materialized view",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;-- COMMENT ON MATERIALIZED VIEW product_mv IS 'Product summary view';- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;- `,- wantComment: true, // Should generate COMMENT statement with empty string- },- {- name: "Update comment on materialized view",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;-- COMMENT ON MATERIALIZED VIEW product_mv IS 'Old comment';- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE MATERIALIZED VIEW product_mv AS- SELECT id, name FROM products;-- COMMENT ON MATERIALIZED VIEW product_mv IS 'New comment';- `,- wantComment: true,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- migration, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration:\n%s", migration)-- if tt.wantComment {- assert.Contains(t, migration, "COMMENT ON MATERIALIZED VIEW",- "Expected migration to contain COMMENT ON MATERIALIZED VIEW statement")- }- })- }-}--func TestMaterializedViewDependencyOrder(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- description string- }{- {- name: "Create table, view, and materialized view with dependencies",- previousSDL: ``,- currentSDL: `- CREATE TABLE customers (- customer_id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(255),- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE TABLE orders (- order_id SERIAL PRIMARY KEY,- customer_id INTEGER REFERENCES customers(customer_id),- amount DECIMAL(10,2),- order_date DATE- );-- -- View depends on tables- CREATE VIEW customer_stats_view AS- SELECT- c.customer_id,- c.name,- c.email,- COUNT(o.order_id) as order_count,- SUM(o.amount) as total_spent- FROM customers c- LEFT JOIN orders o ON c.customer_id = o.customer_id- GROUP BY c.customer_id, c.name, c.email;-- -- Materialized view depends on the view above- CREATE MATERIALIZED VIEW customer_segmentation_mv AS- SELECT- csv.customer_id,- csv.name,- csv.total_spent,- CASE- WHEN csv.total_spent >= 1000 THEN 'Premium'- WHEN csv.total_spent >= 500 THEN 'Standard'- ELSE 'Basic'- END as segment- FROM customer_stats_view csv;- `,- description: "Tests that objects are created in correct dependency order: tables -> view -> materialized view",- },- {- name: "Drop table, view, and materialized view with dependencies",- previousSDL: `- CREATE TABLE customers (- customer_id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(255),- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE TABLE orders (- order_id SERIAL PRIMARY KEY,- customer_id INTEGER REFERENCES customers(customer_id),- amount DECIMAL(10,2),- order_date DATE- );-- -- View depends on tables- CREATE VIEW customer_stats_view AS- SELECT- c.customer_id,- c.name,- c.email,- COUNT(o.order_id) as order_count,- SUM(o.amount) as total_spent- FROM customers c- LEFT JOIN orders o ON c.customer_id = o.customer_id- GROUP BY c.customer_id, c.name, c.email;-- -- Materialized view depends on the view above- CREATE MATERIALIZED VIEW customer_segmentation_mv AS- SELECT- csv.customer_id,- csv.name,- csv.total_spent,- CASE- WHEN csv.total_spent >= 1000 THEN 'Premium'- WHEN csv.total_spent >= 500 THEN 'Standard'- ELSE 'Basic'- END as segment- FROM customer_stats_view csv;- `,- currentSDL: ``,- description: "Tests that objects are dropped in correct dependency order: materialized view -> view -> tables",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- migration, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration:\n%s", migration)-- // Check if this is a CREATE or DROP test- if tt.currentSDL != "" {- // CREATE test: verify correct order for creation- // Tables should come before views, and views should come before materialized views- customersIdx := strings.Index(migration, "CREATE TABLE customers")- ordersIdx := strings.Index(migration, "CREATE TABLE orders")- viewIdx := strings.Index(migration, "CREATE VIEW customer_stats_view")- mvIdx := strings.Index(migration, "CREATE MATERIALIZED VIEW customer_segmentation_mv")-- assert.NotEqual(t, -1, customersIdx, "customers table should be created")- assert.NotEqual(t, -1, ordersIdx, "orders table should be created")- assert.NotEqual(t, -1, viewIdx, "customer_stats_view should be created")- assert.NotEqual(t, -1, mvIdx, "customer_segmentation_mv should be created")-- // Verify correct order for CREATE- if customersIdx != -1 && viewIdx != -1 {- assert.Less(t, customersIdx, viewIdx,- "customers table must be created before customer_stats_view")- }- if ordersIdx != -1 && viewIdx != -1 {- assert.Less(t, ordersIdx, viewIdx,- "orders table must be created before customer_stats_view")- }- if viewIdx != -1 && mvIdx != -1 {- assert.Less(t, viewIdx, mvIdx,- "customer_stats_view must be created before customer_segmentation_mv")- }- } else {- // DROP test: verify correct order for dropping- // Materialized views should be dropped before views, and views before tables- mvIdx := strings.Index(migration, "DROP MATERIALIZED VIEW")- viewIdx := strings.Index(migration, "DROP VIEW")- customersIdx := strings.Index(migration, "DROP TABLE")-- assert.NotEqual(t, -1, mvIdx, "customer_segmentation_mv should be dropped")- assert.NotEqual(t, -1, viewIdx, "customer_stats_view should be dropped")- assert.NotEqual(t, -1, customersIdx, "tables should be dropped")-- // Verify correct order for DROP (reverse of CREATE)- if mvIdx != -1 && viewIdx != -1 {- assert.Less(t, mvIdx, viewIdx,- "customer_segmentation_mv must be dropped before customer_stats_view")- }- if viewIdx != -1 && customersIdx != -1 {- assert.Less(t, viewIdx, customersIdx,- "customer_stats_view must be dropped before tables")- }- }- })- }-}diff --git a/backend/plugin/schema/pg/procedure_comment_roundtrip_test.go b/backend/plugin/schema/pg/procedure_comment_roundtrip_test.godeleted file mode 100644index 05c9e84f6e..0000000000--- a/backend/plugin/schema/pg/procedure_comment_roundtrip_test.go+++ /dev/null@@ -1,139 +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"-)--// TestProcedureCommentRoundtrip tests the full cycle:-// 1. Database metadata with function + procedure (both with comments)-// 2. Generate SDL from metadata (this is what "bytebase sdl dump" does)-// 3. Use that SDL as "previous"-// 4. Create "current" SDL with procedure removed-// 5. Generate migration-// 6. Verify function comment is NOT touched-func TestProcedureCommentRoundtrip(t *testing.T) {- ctx := schema.GetDefinitionContext{- SDLFormat: true,- }-- // Step 1: Create database metadata with both function and procedure (with comments)- metadata := &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Functions: []*storepb.FunctionMetadata{- {- Name: "test_function",- Signature: "test_function()",- Definition: `CREATE OR REPLACE FUNCTION test_function()-RETURNS void-LANGUAGE plpgsql-AS $function$-BEGIN- RAISE NOTICE 'Test function executed';-END-$function$;`,- Comment: "A test function that raises a notice",- },- {- Name: "new_procedure",- Signature: "new_procedure()",- Definition: `CREATE PROCEDURE new_procedure()-LANGUAGE plpgsql-AS $$-BEGIN- NULL;-END;-$$;`,- Comment: "A test procedure",- },- },- },- },- }-- // Step 2: Generate SDL from metadata (simulating "bytebase sdl dump")- previousSDL, err := GetDatabaseDefinition(ctx, metadata)- require.NoError(t, err, "Failed to generate SDL from metadata")-- t.Logf("Generated SDL from metadata:\n%s", previousSDL)-- // Verify the generated SDL contains both objects with their comments- assert.Contains(t, previousSDL, "CREATE OR REPLACE FUNCTION", "Should contain function")- assert.Contains(t, previousSDL, "CREATE PROCEDURE", "Should contain procedure")- assert.Contains(t, previousSDL, "COMMENT ON FUNCTION", "Should contain function comment")- assert.Contains(t, previousSDL, "COMMENT ON PROCEDURE", "Should contain procedure comment")-- // Step 3: Create current SDL with procedure removed (only function remains)- metadataCurrent := &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Functions: []*storepb.FunctionMetadata{- {- Name: "test_function",- Signature: "test_function()",- Definition: `CREATE OR REPLACE FUNCTION test_function()-RETURNS void-LANGUAGE plpgsql-AS $function$-BEGIN- RAISE NOTICE 'Test function executed';-END-$function$;`,- Comment: "A test function that raises a notice",- },- },- },- },- }-- currentSDL, err := GetDatabaseDefinition(ctx, metadataCurrent)- require.NoError(t, err, "Failed to generate current SDL")-- t.Logf("Current SDL (procedure removed):\n%s", currentSDL)-- // Step 4: Generate diff and migration- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err, "Failed to generate SDL diff")-- // Log the diff for debugging- t.Logf("Function changes: %d", len(diff.FunctionChanges))- for i, fc := range diff.FunctionChanges {- t.Logf(" [%d] Action=%v, Schema=%s, Function=%s", i, fc.Action, fc.SchemaName, fc.FunctionName)- }-- t.Logf("Comment changes: %d", len(diff.CommentChanges))- for i, cc := range diff.CommentChanges {- t.Logf(" [%d] Action=%v, Type=%v, Schema=%s, Object=%s, OldComment=%q, NewComment=%q",- i, cc.Action, cc.ObjectType, cc.SchemaName, cc.ObjectName, cc.OldComment, cc.NewComment)- }-- migrationSQL, err := generateMigration(diff)- require.NoError(t, err, "Failed to generate migration")-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Step 5: Verify the migration is correct- // Should drop the procedure- assert.Contains(t, migrationSQL, "DROP PROCEDURE IF EXISTS", "Should drop procedure")- assert.Contains(t, migrationSQL, "new_procedure", "Should mention new_procedure")-- // Should NOT touch the function comment- // This is the key assertion - we should NOT see any COMMENT statement about test_function- if strings.Contains(migrationSQL, "test_function") && strings.Contains(migrationSQL, "COMMENT") {- t.Errorf("Migration incorrectly contains COMMENT statement about test_function:\n%s", migrationSQL)- }-- // Should NOT contain "COMMENT ON FUNCTION ... IS NULL"- assert.NotContains(t, migrationSQL, "IS NULL", "Should not try to remove any comments")-}diff --git a/backend/plugin/schema/pg/procedure_sdl_diff_test.go b/backend/plugin/schema/pg/procedure_sdl_diff_test.godeleted file mode 100644index 0558fdee05..0000000000--- a/backend/plugin/schema/pg/procedure_sdl_diff_test.go+++ /dev/null@@ -1,589 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestProcedureSDLDiff(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedFunctionChanges int // Note: procedures are stored as functions in PostgreSQL- expectedActions []schema.MetadataDiffAction- }{- {- name: "Create new procedure",- previousSDL: ``,- currentSDL: `- CREATE TABLE logs (- id SERIAL PRIMARY KEY,- message TEXT,- created_at TIMESTAMP DEFAULT NOW()- );-- CREATE PROCEDURE log_message(msg TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- INSERT INTO logs (message) VALUES (msg);- END;- $$;- `,- expectedFunctionChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Drop procedure",- previousSDL: `- CREATE TABLE logs (- id SERIAL PRIMARY KEY,- message TEXT,- created_at TIMESTAMP DEFAULT NOW()- );-- CREATE PROCEDURE log_message(msg TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- INSERT INTO logs (message) VALUES (msg);- END;- $$;- `,- currentSDL: `- CREATE TABLE logs (- id SERIAL PRIMARY KEY,- message TEXT,- created_at TIMESTAMP DEFAULT NOW()- );- `,- expectedFunctionChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},- },- {- name: "Modify procedure (CREATE OR REPLACE)",- previousSDL: `- CREATE TABLE logs (- id SERIAL PRIMARY KEY,- message TEXT,- created_at TIMESTAMP DEFAULT NOW()- );-- CREATE PROCEDURE log_message(msg TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- INSERT INTO logs (message) VALUES (msg);- END;- $$;- `,- currentSDL: `- CREATE TABLE logs (- id SERIAL PRIMARY KEY,- message TEXT,- created_at TIMESTAMP DEFAULT NOW()- );-- CREATE PROCEDURE log_message(msg TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- INSERT INTO logs (message, created_at) VALUES (msg, NOW());- END;- $$;- `,- expectedFunctionChanges: 1, // ALTER only- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionAlter},- },- {- name: "Mixed functions and procedures",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255),- email VARCHAR(255)- );-- CREATE FUNCTION get_user_count() RETURNS INTEGER- LANGUAGE plpgsql- AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$;-- CREATE PROCEDURE add_user(user_name TEXT, user_email TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- INSERT INTO users (name, email) VALUES (user_name, user_email);- END;- $$;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255),- email VARCHAR(255)- );-- CREATE FUNCTION get_user_count() RETURNS INTEGER- LANGUAGE plpgsql- AS $$- BEGIN- RETURN (SELECT COUNT(*) FROM users);- END;- $$;-- CREATE PROCEDURE update_user_email(user_id INTEGER, new_email TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- UPDATE users SET email = new_email WHERE id = user_id;- END;- $$;- `,- expectedFunctionChanges: 2, // Drop add_user + Create update_user_email- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate, schema.MetadataDiffActionDrop},- },- {- name: "Schema-qualified procedure",- previousSDL: `- CREATE SCHEMA admin;- CREATE TABLE admin.settings (- key VARCHAR(255) PRIMARY KEY,- value TEXT- );- `,- currentSDL: `- CREATE SCHEMA admin;- CREATE TABLE admin.settings (- key VARCHAR(255) PRIMARY KEY,- value TEXT- );-- CREATE PROCEDURE admin.update_setting(setting_key TEXT, setting_value TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- INSERT INTO admin.settings (key, value) VALUES (setting_key, setting_value)- ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;- END;- $$;- `,- expectedFunctionChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Note: In PostgreSQL, procedures are stored as functions- assert.Equal(t, tt.expectedFunctionChanges, len(diff.FunctionChanges),- "Expected %d function changes (including procedures), got %d", tt.expectedFunctionChanges, len(diff.FunctionChanges))-- // Check that the actions match expectations- var actualActions []schema.MetadataDiffAction- for _, funcDiff := range diff.FunctionChanges {- actualActions = append(actualActions, funcDiff.Action)- }-- // Handle nil vs empty slice comparison- if len(tt.expectedActions) == 0 && len(actualActions) == 0 {- // Both are effectively empty - test passes- t.Log("Both expected and actual actions are empty")- } else {- assert.ElementsMatch(t, tt.expectedActions, actualActions,- "Expected actions %v, got %v", tt.expectedActions, actualActions)- }-- // Verify AST nodes are properly set- for i, funcDiff := range diff.FunctionChanges {- switch funcDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, funcDiff.NewASTNode,- "Function diff %d should have NewASTNode for CREATE action", i)- assert.Nil(t, funcDiff.OldASTNode,- "Function diff %d should not have OldASTNode for CREATE action", i)- case schema.MetadataDiffActionDrop:- assert.NotNil(t, funcDiff.OldASTNode,- "Function diff %d should have OldASTNode for DROP action", i)- assert.Nil(t, funcDiff.NewASTNode,- "Function diff %d should not have NewASTNode for DROP action", i)- case schema.MetadataDiffActionAlter:- assert.NotNil(t, funcDiff.OldASTNode,- "Function diff %d should have OldASTNode for ALTER action", i)- assert.NotNil(t, funcDiff.NewASTNode,- "Function diff %d should have NewASTNode for ALTER action", i)- default:- t.Errorf("Unexpected action %v for function diff %d", funcDiff.Action, i)- }- }- })- }-}--func TestProcedureSDLDiff_CommentOnProcedure(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedSQL string- shouldContain []string- shouldNotContain []string- expectedCommentChange int- }{- {- name: "Create procedure with COMMENT ON PROCEDURE",- previousSDL: ``,- currentSDL: `- CREATE PROCEDURE new_procedure()- LANGUAGE plpgsql- AS $$- BEGIN- RAISE NOTICE 'New procedure executed';- END;- $$;-- COMMENT ON PROCEDURE "public"."new_procedure"() IS 'A new procedure that raises a notice';- `,- shouldContain: []string{- "CREATE PROCEDURE",- "COMMENT ON PROCEDURE",- `"public".new_procedure()`,- },- shouldNotContain: []string{- "COMMENT ON FUNCTION",- },- expectedCommentChange: 1,- },- {- name: "Create procedure with COMMENT ON PROCEDURE - ensure FUNCTION is not used",- previousSDL: `- CREATE TABLE logs (- id SERIAL PRIMARY KEY,- message TEXT- );- `,- currentSDL: `- CREATE TABLE logs (- id SERIAL PRIMARY KEY,- message TEXT- );-- CREATE PROCEDURE log_message(msg TEXT)- LANGUAGE plpgsql- AS $$- BEGIN- INSERT INTO logs (message) VALUES (msg);- END;- $$;-- COMMENT ON PROCEDURE "public"."log_message"(msg text) IS 'Logs a message to the logs table';- `,- shouldContain: []string{- "CREATE PROCEDURE",- "COMMENT ON PROCEDURE",- `log_message`,- },- shouldNotContain: []string{- "COMMENT ON FUNCTION",- },- expectedCommentChange: 1,- },- {- name: "Mixed - procedure with comment and function with comment",- previousSDL: ``,- currentSDL: `- CREATE FUNCTION test_function()- RETURNS void- LANGUAGE plpgsql- AS $$- BEGIN- RAISE NOTICE 'Test function executed';- END;- $$;-- COMMENT ON FUNCTION "public".test_function() IS 'A test function that raises a notice';-- CREATE PROCEDURE test_procedure()- LANGUAGE plpgsql- AS $$- BEGIN- RAISE NOTICE 'Test procedure executed';- END;- $$;-- COMMENT ON PROCEDURE "public"."test_procedure"() IS 'A test procedure that raises a notice';- `,- shouldContain: []string{- "COMMENT ON FUNCTION",- "test_function",- "COMMENT ON PROCEDURE",- "test_procedure",- },- shouldNotContain: []string{},- expectedCommentChange: 2,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Verify comment changes- assert.Equal(t, tt.expectedCommentChange, len(diff.CommentChanges),- "Expected %d comment changes, got %d", tt.expectedCommentChange, len(diff.CommentChanges))-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Verify expected strings are present- for _, expected := range tt.shouldContain {- assert.Contains(t, migrationSQL, expected,- "Migration SQL should contain %q", expected)- }-- // Verify unwanted strings are not present- for _, unwanted := range tt.shouldNotContain {- assert.NotContains(t, migrationSQL, unwanted,- "Migration SQL should NOT contain %q", unwanted)- }- })- }-}--func TestDropColumnWithComment_NoCommentGeneration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- shouldContain []string- shouldNotContain []string- }{- {- name: "Drop column with comment - should not generate COMMENT ON COLUMN",- previousSDL: `- CREATE TABLE "public"."test_table" (- "id" serial,- "name" character varying(100) NOT NULL,- "test_column" integer,- CONSTRAINT "test_table_pkey" PRIMARY KEY (id)- );-- COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';- COMMENT ON COLUMN "public"."test_table"."test_column" IS 'A test column for various uses';- `,- currentSDL: `- CREATE TABLE "public"."test_table" (- "id" serial,- "name" character varying(100) NOT NULL,- CONSTRAINT "test_table_pkey" PRIMARY KEY (id)- );-- COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';- `,- shouldContain: []string{- `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "test_column"`,- },- shouldNotContain: []string{- `COMMENT ON COLUMN "public"."test_table"."test_column"`,- },- },- {- name: "Drop table with comment - should not generate COMMENT ON TABLE",- previousSDL: `- CREATE TABLE "public"."test_table" (- "id" serial,- "name" character varying(100) NOT NULL,- CONSTRAINT "test_table_pkey" PRIMARY KEY (id)- );-- COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';- `,- currentSDL: ``,- shouldContain: []string{- `DROP TABLE IF EXISTS "public"."test_table"`,- },- shouldNotContain: []string{- `COMMENT ON TABLE "public"."test_table"`,- },- },- {- name: "Drop multiple columns with comments",- previousSDL: `- CREATE TABLE "public"."test_table" (- "id" serial,- "name" character varying(100) NOT NULL,- "created_at" timestamp(6) without time zone DEFAULT CURRENT_TIMESTAMP,- "is_active" boolean DEFAULT true,- "description" text,- "test_column" integer,- CONSTRAINT "test_table_pkey" PRIMARY KEY (id)- );-- COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';- COMMENT ON COLUMN "public"."test_table"."test_column" IS 'A test column for various uses';- COMMENT ON COLUMN "public"."test_table"."description" IS 'Description field';- `,- currentSDL: `- CREATE TABLE "public"."test_table" (- "id" serial,- "name" character varying(100) NOT NULL,- "is_active" boolean DEFAULT true,- CONSTRAINT "test_table_pkey" PRIMARY KEY (id)- );-- COMMENT ON TABLE "public"."test_table" IS 'A table for testing purposes';- `,- shouldContain: []string{- `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "test_column"`,- `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "created_at"`,- `ALTER TABLE "public"."test_table" DROP COLUMN IF EXISTS "description"`,- },- shouldNotContain: []string{- `COMMENT ON COLUMN "public"."test_table"."test_column"`,- `COMMENT ON COLUMN "public"."test_table"."description"`,- `COMMENT ON COLUMN "public"."test_table"."created_at"`,- },- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Verify expected strings are present- for _, expected := range tt.shouldContain {- assert.Contains(t, migrationSQL, expected,- "Migration SQL should contain %q", expected)- }-- // Verify unwanted strings are not present- for _, unwanted := range tt.shouldNotContain {- assert.NotContains(t, migrationSQL, unwanted,- "Migration SQL should NOT contain %q", unwanted)- }- })- }-}-func TestRemoveProcedureComment_ShouldUsePROCEDURE(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- shouldContain []string- shouldNotContain []string- }{- {- name: "Remove comment from procedure - should use COMMENT ON PROCEDURE, not FUNCTION",- previousSDL: `-CREATE PROCEDURE "public"."new_procedure"()-LANGUAGE plpgsql-AS $$-BEGIN- RAISE NOTICE 'New procedure executed';-END;-$$;--COMMENT ON PROCEDURE "public"."new_procedure"() IS 'A new procedure that raises a notice';-`,- currentSDL: `-CREATE PROCEDURE "public"."new_procedure"()-LANGUAGE plpgsql-AS $$-BEGIN- RAISE NOTICE 'New procedure executed';-END;-$$;-`,- shouldContain: []string{- "COMMENT ON PROCEDURE",- `"public".new_procedure() IS NULL`,- },- shouldNotContain: []string{- "COMMENT ON FUNCTION",- },- },- {- name: "Update procedure comment - should use COMMENT ON PROCEDURE",- previousSDL: `-CREATE PROCEDURE "public"."log_message"(msg text)-LANGUAGE plpgsql-AS $$-BEGIN- INSERT INTO logs (message) VALUES (msg);-END;-$$;--COMMENT ON PROCEDURE "public"."log_message"(msg text) IS 'Old comment';-`,- currentSDL: `-CREATE PROCEDURE "public"."log_message"(msg text)-LANGUAGE plpgsql-AS $$-BEGIN- INSERT INTO logs (message) VALUES (msg);-END;-$$;--COMMENT ON PROCEDURE "public"."log_message"(msg text) IS 'New comment';-`,- shouldContain: []string{- "COMMENT ON PROCEDURE",- `"public".log_message(msg text) IS 'New comment'`,- },- shouldNotContain: []string{- "COMMENT ON FUNCTION",- },- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Verify expected strings are present- for _, expected := range tt.shouldContain {- assert.Contains(t, migrationSQL, expected,- "Migration SQL should contain %q", expected)- }-- // Verify unwanted strings are not present- for _, unwanted := range tt.shouldNotContain {- assert.NotContains(t, migrationSQL, unwanted,- "Migration SQL should NOT contain %q", unwanted)- }- })- }-}diff --git a/backend/plugin/schema/pg/schema_implicit_creation_test.go b/backend/plugin/schema/pg/schema_implicit_creation_test.godeleted file mode 100644index 3371246034..0000000000--- a/backend/plugin/schema/pg/schema_implicit_creation_test.go+++ /dev/null@@ -1,370 +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"-)--// TestGetSDLDiff_ImplicitSchemaCreation tests that when users add tables in a new schema-// without explicit CREATE SCHEMA statement, the diff should include schema creation.-// It also tests explicit CREATE SCHEMA handling and mixed scenarios.-func TestGetSDLDiff_ImplicitSchemaCreation(t *testing.T) {- tests := []struct {- name string- currentSDLText string- previousUserSDLText string- currentSchema *model.DatabaseMetadata- expectedSchemaChanges int- expectedTableChanges int- expectSchemaCreation bool- expectedSchemaName string- expectedSchemaNames []string // For cases with multiple schemas- }{- {- name: "add_table_in_new_schema_without_create_schema",- // Previous SDL: only has a table in public schema- previousUserSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY,- name VARCHAR(100)-);-`,- // Current SDL: user adds a table in new_schema WITHOUT CREATE SCHEMA statement- currentSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY,- name VARCHAR(100)-);--CREATE TABLE new_schema.t(- id INT PRIMARY KEY,- value VARCHAR(50)-);-`,- // Current database state: only has public schema with existing_table- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "existing_table",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- {- Name: "name",- Type: "character varying(100)",- Nullable: true,- },- },- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSchemaChanges: 1, // Should detect new_schema creation- expectedTableChanges: 1, // new_schema.t creation- expectSchemaCreation: true,- expectedSchemaName: "new_schema",- },- {- name: "add_multiple_tables_in_new_schema_without_create_schema",- previousUserSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY-);-`,- currentSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY-);--CREATE TABLE new_schema.users(- id INT PRIMARY KEY,- name VARCHAR(100)-);--CREATE TABLE new_schema.orders(- id INT PRIMARY KEY,- user_id INT REFERENCES new_schema.users(id)-);-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "existing_table",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- },- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSchemaChanges: 1, // Should detect new_schema creation- expectedTableChanges: 2, // users and orders creation- expectSchemaCreation: true,- expectedSchemaName: "new_schema",- },- {- name: "add_table_in_new_schema_with_explicit_create_schema",- // Previous SDL: only has a table in public schema- previousUserSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY,- name VARCHAR(100)-);-`,- // Current SDL: user adds EXPLICIT CREATE SCHEMA statement and table- currentSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY,- name VARCHAR(100)-);--CREATE SCHEMA new_schema;--CREATE TABLE new_schema.t(- id INT PRIMARY KEY,- value VARCHAR(50)-);-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "existing_table",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- {- Name: "name",- Type: "character varying(100)",- Nullable: true,- },- },- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSchemaChanges: 1, // Should detect new_schema creation- expectedTableChanges: 1, // new_schema.t creation- expectSchemaCreation: true,- expectedSchemaName: "new_schema",- },- {- name: "explicit_create_schema_without_objects",- // Test that explicit CREATE SCHEMA alone is handled correctly- previousUserSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY-);-`,- currentSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY-);--CREATE SCHEMA new_schema;-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "existing_table",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- },- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSchemaChanges: 1, // Should detect new_schema creation- expectedTableChanges: 0, // No table changes- expectSchemaCreation: true,- expectedSchemaName: "new_schema",- },- {- name: "mixed_explicit_and_implicit_schemas",- // Test: one schema created explicitly, another implicitly via table reference- previousUserSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY-);-`,- currentSDLText: `-CREATE TABLE public.existing_table(- id INT PRIMARY KEY-);--CREATE SCHEMA schema_a;--CREATE TABLE schema_a.table_a(- id INT PRIMARY KEY-);--CREATE TABLE schema_b.table_b(- id INT PRIMARY KEY-);-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "existing_table",- Columns: []*storepb.ColumnMetadata{- {- Name: "id",- Type: "integer",- Nullable: false,- },- },- },- },- },- },- },- nil,- nil,- storepb.Engine_POSTGRES,- false,- ),- expectedSchemaChanges: 2, // Should detect both schema_a (explicit) and schema_b (implicit) creation- expectedTableChanges: 2, // table_a and table_b creation- expectSchemaCreation: true, // For backward compatibility- expectedSchemaName: "schema_a", // For backward compatibility- expectedSchemaNames: []string{"schema_a", "schema_b"}, // Both schemas should be created- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Get SDL diff- diff, err := GetSDLDiff(tt.currentSDLText, tt.previousUserSDLText, tt.currentSchema)- require.NoError(t, err, "GetSDLDiff should not return error")- require.NotNil(t, diff, "Diff should not be nil")-- // Debug: print what we got- t.Logf("Schema changes count: %d", len(diff.SchemaChanges))- for i, sc := range diff.SchemaChanges {- t.Logf(" SchemaChange[%d]: Action=%v, SchemaName=%s", i, sc.Action, sc.SchemaName)- }- t.Logf("Table changes count: %d", len(diff.TableChanges))- for i, tc := range diff.TableChanges {- t.Logf(" TableChange[%d]: Action=%v, Schema=%s, Table=%s", i, tc.Action, tc.SchemaName, tc.TableName)- }-- // Verify schema changes- require.Len(t, diff.SchemaChanges, tt.expectedSchemaChanges,- "Should have %d schema change(s)", tt.expectedSchemaChanges)-- if tt.expectSchemaCreation {- // If expectedSchemaNames is provided, check all of them- if len(tt.expectedSchemaNames) > 0 {- for _, expectedSchema := range tt.expectedSchemaNames {- foundSchemaCreation := false- for _, schemaChange := range diff.SchemaChanges {- if schemaChange.Action == schema.MetadataDiffActionCreate &&- schemaChange.SchemaName == expectedSchema {- foundSchemaCreation = true- break- }- }- require.True(t, foundSchemaCreation,- "Should detect CREATE action for schema %s", expectedSchema)- }- } else {- // Backward compatibility: check for single expectedSchemaName- foundSchemaCreation := false- for _, schemaChange := range diff.SchemaChanges {- if schemaChange.Action == schema.MetadataDiffActionCreate &&- schemaChange.SchemaName == tt.expectedSchemaName {- foundSchemaCreation = true- break- }- }- require.True(t, foundSchemaCreation,- "Should detect CREATE action for schema %s", tt.expectedSchemaName)- }- }-- // Verify table changes- require.Len(t, diff.TableChanges, tt.expectedTableChanges,- "Should have %d table change(s)", tt.expectedTableChanges)-- // Generate migration to verify it includes CREATE SCHEMA- migration, err := generateMigration(diff)- require.NoError(t, err, "Migration generation should not fail")-- if tt.expectSchemaCreation {- require.Contains(t, migration, "CREATE SCHEMA",- "Migration should include CREATE SCHEMA statement")- require.Contains(t, migration, tt.expectedSchemaName,- "Migration should reference schema %s", tt.expectedSchemaName)-- t.Logf("Generated migration:\n%s", migration)- }- })- }-}diff --git a/backend/plugin/schema/pg/sdl_chunk.go b/backend/plugin/schema/pg/sdl_chunk.godeleted file mode 100644index 91afcf93cd..0000000000--- a/backend/plugin/schema/pg/sdl_chunk.go+++ /dev/null@@ -1,741 +0,0 @@-package pg--import (- "strings"-- "github.com/antlr4-go/antlr/v4"- "github.com/pkg/errors"-- parser "github.com/bytebase/parser/postgresql"-- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// ChunkSDLText parses SDL text and extracts chunks for each database object.-func ChunkSDLText(sdlText string) (*schema.SDLChunks, error) {- if strings.TrimSpace(sdlText) == "" {- return &schema.SDLChunks{- Tables: make(map[string]*schema.SDLChunk),- Views: make(map[string]*schema.SDLChunk),- MaterializedViews: make(map[string]*schema.SDLChunk),- Functions: make(map[string]*schema.SDLChunk),- Triggers: make(map[string]*schema.SDLChunk),- Indexes: make(map[string]*schema.SDLChunk),- Sequences: make(map[string]*schema.SDLChunk),- Schemas: make(map[string]*schema.SDLChunk),- EnumTypes: make(map[string]*schema.SDLChunk),- Extensions: make(map[string]*schema.SDLChunk),- EventTriggers: make(map[string]*schema.SDLChunk),- ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),- IndexComments: make(map[string]map[string]antlr.ParserRuleContext),- }, nil- }-- parseResults, err := pgparser.ParsePostgreSQL(sdlText)- if err != nil {- return nil, errors.Wrap(err, "failed to parse SDL text")- }-- extractor := &sdlChunkExtractor{- sdlText: sdlText,- chunks: &schema.SDLChunks{- Tables: make(map[string]*schema.SDLChunk),- Views: make(map[string]*schema.SDLChunk),- MaterializedViews: make(map[string]*schema.SDLChunk),- Functions: make(map[string]*schema.SDLChunk),- Triggers: make(map[string]*schema.SDLChunk),- Indexes: make(map[string]*schema.SDLChunk),- Sequences: make(map[string]*schema.SDLChunk),- Schemas: make(map[string]*schema.SDLChunk),- EnumTypes: make(map[string]*schema.SDLChunk),- Extensions: make(map[string]*schema.SDLChunk),- EventTriggers: make(map[string]*schema.SDLChunk),- ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),- IndexComments: make(map[string]map[string]antlr.ParserRuleContext),- },- }-- // Walk all parsed statements- for _, parseResult := range parseResults {- extractor.tokens = parseResult.Tokens- antlr.ParseTreeWalkerDefault.Walk(extractor, parseResult.Tree)- }-- return extractor.chunks, nil-}--type sdlChunkExtractor struct {- *parser.BasePostgreSQLParserListener- sdlText string- chunks *schema.SDLChunks- tokens *antlr.CommonTokenStream-}--func (l *sdlChunkExtractor) EnterCreatestmt(ctx *parser.CreatestmtContext) {- if ctx.Qualified_name(0) == nil {- return- }-- identifier := pgparser.NormalizePostgreSQLQualifiedName(ctx.Qualified_name(0))- identifierStr := strings.Join(identifier, ".")-- // Ensure schema.tableName format- var schemaQualifiedName string- if strings.Contains(identifierStr, ".") {- schemaQualifiedName = identifierStr- } else {- schemaQualifiedName = "public." + identifierStr- }-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: ctx,- }-- l.chunks.Tables[schemaQualifiedName] = chunk-}--func (l *sdlChunkExtractor) EnterCreateseqstmt(ctx *parser.CreateseqstmtContext) {- if ctx.Qualified_name() == nil {- return- }-- identifier := pgparser.NormalizePostgreSQLQualifiedName(ctx.Qualified_name())- identifierStr := strings.Join(identifier, ".")-- // Ensure schema.sequenceName format- var schemaQualifiedName string- if strings.Contains(identifierStr, ".") {- schemaQualifiedName = identifierStr- } else {- schemaQualifiedName = "public." + identifierStr- }-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: ctx,- AlterStatements: []antlr.ParserRuleContext{},- }-- l.chunks.Sequences[schemaQualifiedName] = chunk-}--func (l *sdlChunkExtractor) EnterAlterseqstmt(ctx *parser.AlterseqstmtContext) {- // Extract the sequence name from ALTER SEQUENCE statement- if ctx.Qualified_name() == nil {- return- }-- identifier := pgparser.NormalizePostgreSQLQualifiedName(ctx.Qualified_name())- identifierStr := strings.Join(identifier, ".")-- // Ensure schema.sequenceName format- var schemaQualifiedName string- if strings.Contains(identifierStr, ".") {- schemaQualifiedName = identifierStr- } else {- schemaQualifiedName = "public." + identifierStr- }-- // Find the corresponding CREATE SEQUENCE chunk and append this ALTER statement- if chunk, exists := l.chunks.Sequences[schemaQualifiedName]; exists {- chunk.AlterStatements = append(chunk.AlterStatements, ctx)- } else {- // If CREATE SEQUENCE doesn't exist yet, create a placeholder chunk- // This handles cases where ALTER appears before CREATE in the SDL text- // (though this is unusual, we handle it for robustness)- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: nil, // No CREATE statement yet- AlterStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Sequences[schemaQualifiedName] = chunk- }-}--// EnterDefinestmt handles CREATE TYPE AS ENUM statements-func (l *sdlChunkExtractor) EnterDefinestmt(ctx *parser.DefinestmtContext) {- // Check if this is CREATE TYPE AS ENUM- if ctx.CREATE() == nil || ctx.TYPE_P() == nil || ctx.AS() == nil || ctx.ENUM_P() == nil {- return- }-- // Extract type name- typeNames := ctx.AllAny_name()- if len(typeNames) == 0 {- return- }-- // Get the enum type name (first Any_name)- typeName := typeNames[0]- identifier := pgparser.NormalizePostgreSQLAnyName(typeName)- identifierStr := strings.Join(identifier, ".")-- // Ensure schema.enumName format (default to "public" if no schema specified)- var schemaQualifiedName string- if strings.Contains(identifierStr, ".") {- schemaQualifiedName = identifierStr- } else {- schemaQualifiedName = "public." + identifierStr- }-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: ctx,- }-- l.chunks.EnumTypes[schemaQualifiedName] = chunk-}--func (l *sdlChunkExtractor) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {- funcNameCtx := ctx.Func_name()- if funcNameCtx == nil {- return- }-- // Extract function name using proper normalization- funcNameParts := pgparser.NormalizePostgreSQLFuncName(funcNameCtx)- if len(funcNameParts) == 0 {- return- }-- // Parse schema and function name directly from parts- var schemaName string- if len(funcNameParts) == 2 {- // Schema qualified: schema.function_name- schemaName = funcNameParts[0]- } else if len(funcNameParts) == 1 {- // Unqualified: function_name (assume public schema)- schemaName = "public"- } else {- // Unexpected format- return- }-- // Use the unified function signature extraction- signature := extractFunctionSignatureFromAST(ctx)- if signature == "" {- return- }-- schemaQualifiedSignature := schemaName + "." + signature-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedSignature,- ASTNode: ctx,- }-- l.chunks.Functions[schemaQualifiedSignature] = chunk-}--func (l *sdlChunkExtractor) EnterIndexstmt(ctx *parser.IndexstmtContext) {- // Check if this is CREATE INDEX- if ctx.CREATE() == nil || ctx.INDEX() == nil {- return- }-- // Extract index name- var indexName string- if name := ctx.Name(); name != nil {- indexName = pgparser.NormalizePostgreSQLName(name)- }-- // If no explicit name, we skip it as PostgreSQL will generate one- if indexName == "" {- return- }-- // Ensure schema.indexName format- var schemaQualifiedName string- if strings.Contains(indexName, ".") {- schemaQualifiedName = indexName- } else {- schemaQualifiedName = "public." + indexName- }-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: ctx,- }-- l.chunks.Indexes[schemaQualifiedName] = chunk-}--// EnterCreatetrigstmt handles CREATE TRIGGER statements-func (l *sdlChunkExtractor) EnterCreatetrigstmt(ctx *parser.CreatetrigstmtContext) {- // Check if this is CREATE TRIGGER- if ctx.CREATE() == nil || ctx.TRIGGER() == nil {- return- }-- // Extract trigger name- if ctx.Name() == nil {- return- }- triggerName := pgparser.NormalizePostgreSQLName(ctx.Name())-- // Extract table name from ON clause- tableName := extractTableNameFromTrigger(ctx)- if tableName == "" {- return- }-- // Parse schema and table from table name (format: schema.table)- parts := strings.Split(tableName, ".")- var schemaName string- var unqualifiedTableName string- if len(parts) == 2 {- schemaName = parts[0]- unqualifiedTableName = parts[1]- } else {- schemaName = "public"- unqualifiedTableName = tableName- }-- // Create identifier: schema.table.trigger_name (table-scoped)- schemaQualifiedName := schemaName + "." + unqualifiedTableName + "." + triggerName-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: ctx,- }-- l.chunks.Triggers[schemaQualifiedName] = chunk-}--func (l *sdlChunkExtractor) EnterViewstmt(ctx *parser.ViewstmtContext) {- if ctx.Qualified_name() == nil {- return- }-- identifier := pgparser.NormalizePostgreSQLQualifiedName(ctx.Qualified_name())- identifierStr := strings.Join(identifier, ".")-- // Ensure schema.viewName format- var schemaQualifiedName string- if strings.Contains(identifierStr, ".") {- schemaQualifiedName = identifierStr- } else {- schemaQualifiedName = "public." + identifierStr- }-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: ctx,- }-- l.chunks.Views[schemaQualifiedName] = chunk-}--// EnterCreatematviewstmt handles CREATE MATERIALIZED VIEW statements-func (l *sdlChunkExtractor) EnterCreatematviewstmt(ctx *parser.CreatematviewstmtContext) {- // Extract materialized view name from create_mv_target- if ctx.Create_mv_target() == nil || ctx.Create_mv_target().Qualified_name() == nil {- return- }-- identifier := pgparser.NormalizePostgreSQLQualifiedName(ctx.Create_mv_target().Qualified_name())- identifierStr := strings.Join(identifier, ".")-- // Ensure schema.materializedViewName format- var schemaQualifiedName string- if strings.Contains(identifierStr, ".") {- schemaQualifiedName = identifierStr- } else {- schemaQualifiedName = "public." + identifierStr- }-- chunk := &schema.SDLChunk{- Identifier: schemaQualifiedName,- ASTNode: ctx,- }-- l.chunks.MaterializedViews[schemaQualifiedName] = chunk-}--func (l *sdlChunkExtractor) EnterCreateschemastmt(ctx *parser.CreateschemastmtContext) {- // Extract schema name- var schemaName string-- // Schema name can be either from optschemaname or colid- if ctx.Colid() != nil {- schemaName = pgparser.NormalizePostgreSQLColid(ctx.Colid())- } else if ctx.Optschemaname() != nil && ctx.Optschemaname().Colid() != nil {- schemaName = pgparser.NormalizePostgreSQLColid(ctx.Optschemaname().Colid())- } else {- // Skip if we can't determine schema name- return- }-- // Skip pg_catalog and information_schema- if schemaName == "pg_catalog" || schemaName == "information_schema" {- return- }-- // Create chunk for this schema- l.chunks.Schemas[schemaName] = &schema.SDLChunk{- Identifier: schemaName,- ASTNode: ctx,- }-}--func (l *sdlChunkExtractor) EnterCreateextensionstmt(ctx *parser.CreateextensionstmtContext) {- // Extract extension name from AST- if ctx.Name() == nil {- return- }-- extensionName := pgparser.NormalizePostgreSQLName(ctx.Name())-- // Extension is database-level, no schema prefix needed- chunk := &schema.SDLChunk{- Identifier: extensionName, // Just the extension name- ASTNode: ctx,- }-- l.chunks.Extensions[extensionName] = chunk-}--func (l *sdlChunkExtractor) EnterCreateeventtrigstmt(ctx *parser.CreateeventtrigstmtContext) {- // Extract event trigger name from AST- if ctx.Name() == nil {- return- }-- eventTriggerName := pgparser.NormalizePostgreSQLName(ctx.Name())-- // Event trigger is database-level, no schema prefix needed- chunk := &schema.SDLChunk{- Identifier: eventTriggerName, // Just the event trigger name- ASTNode: ctx,- }-- l.chunks.EventTriggers[eventTriggerName] = chunk-}--func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {- // Extract the comment text (can be sconst or NULL)- // We store the entire AST node, not just the comment text-- // Check for COLUMN comment: COMMENT ON COLUMN any_name IS comment_text- if ctx.COLUMN() != nil {- if ctx.Any_name() != nil {- // Extract schema.table.column from any_name- anyName := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())- if len(anyName) >= 3 {- // Format: schema.table.column- schemaName := anyName[0]- tableName := anyName[1]- columnName := anyName[2]- tableIdentifier := schemaName + "." + tableName-- // Ensure the map for this table exists- if l.chunks.ColumnComments[tableIdentifier] == nil {- l.chunks.ColumnComments[tableIdentifier] = make(map[string]antlr.ParserRuleContext)- }- l.chunks.ColumnComments[tableIdentifier][columnName] = ctx- } else if len(anyName) == 2 {- // Format: table.column (assume public schema)- tableName := anyName[0]- columnName := anyName[1]- tableIdentifier := "public." + tableName-- if l.chunks.ColumnComments[tableIdentifier] == nil {- l.chunks.ColumnComments[tableIdentifier] = make(map[string]antlr.ParserRuleContext)- }- l.chunks.ColumnComments[tableIdentifier][columnName] = ctx- }- }- return- }-- // Check for FUNCTION/PROCEDURE comment- // Note: ctx.FUNCTION() is true for both COMMENT ON FUNCTION and COMMENT ON PROCEDURE- // We need to check ctx.PROCEDURE() to distinguish between them- if ctx.FUNCTION() != nil || ctx.PROCEDURE() != nil {- if ctx.Function_with_argtypes() != nil {- funcWithArgsCtx := ctx.Function_with_argtypes()-- // Extract function name from func_name- var funcNameParts []string- if funcWithArgsCtx.Func_name() != nil {- funcNameParts = pgparser.NormalizePostgreSQLFuncName(funcWithArgsCtx.Func_name())- } else if funcWithArgsCtx.Colid() != nil {- // Single identifier case- funcNameParts = []string{pgparser.NormalizePostgreSQLColid(funcWithArgsCtx.Colid())}- } else {- // Fallback: use the whole text- funcNameParts = []string{funcWithArgsCtx.GetText()}- }-- // Determine schema name- var schemaName string- var funcName string- if len(funcNameParts) >= 2 {- schemaName = funcNameParts[0]- funcName = funcNameParts[1]- } else if len(funcNameParts) == 1 {- schemaName = "public"- funcName = funcNameParts[0]- } else {- return- }-- // Try to find matching function by name prefix (schema.funcname)- // This is necessary because CREATE FUNCTION uses parameter names and normalized types,- // while COMMENT ON FUNCTION may use different type representations- funcNamePrefix := schemaName + "." + funcName- var matchedChunk *schema.SDLChunk- for identifier, chunk := range l.chunks.Functions {- if strings.HasPrefix(identifier, funcNamePrefix+"(") || identifier == funcNamePrefix {- matchedChunk = chunk- break- }- }-- if matchedChunk != nil {- // Add comment to the existing function chunk- matchedChunk.CommentStatements = append(matchedChunk.CommentStatements, ctx)- }- // If no match found, we don't create a new chunk because the function- // should already exist from CREATE FUNCTION statement- }- return- }-- // Check for TYPE comment: COMMENT ON TYPE typename IS comment_text- if ctx.TYPE_P() != nil {- // Extract typename from the third child (TYPE_P is second, typename is third)- if ctx.GetChildCount() >= 4 {- child := ctx.GetChild(3)- if typenameCtx, ok := child.(*parser.TypenameContext); ok {- // Extract schema.type from typename- identifier := extractSchemaAndTypeFromTypename(typenameCtx)- if identifier != "" {- if chunk, exists := l.chunks.EnumTypes[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- // Create a new chunk for the comment (type may not have CREATE TYPE statement)- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.EnumTypes[identifier] = chunk- }- }- }- }- return- }-- // Check for object_type_any_name: TABLE, SEQUENCE, VIEW, INDEX, etc.- if ctx.Object_type_any_name() != nil && ctx.Any_name() != nil {- objectType := ctx.Object_type_any_name().GetText()- anyName := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())-- // Debug: log the object type to understand what we're getting- // This will help us identify the issue with MATERIALIZED VIEW comments- _ = objectType // Prevent unused variable error in production-- var identifier string- if len(anyName) >= 2 {- // Format: schema.object- identifier = strings.Join(anyName, ".")- } else if len(anyName) == 1 {- // Format: object (assume public schema)- identifier = "public." + anyName[0]- } else {- return- }-- // Route to appropriate chunk map based on object type- // Note: For "MATERIALIZED VIEW", the parser may return "MATERIALIZEDVIEW" (no space)- objectTypeUpper := strings.ToUpper(objectType)- switch objectTypeUpper {- case "TABLE":- if chunk, exists := l.chunks.Tables[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Tables[identifier] = chunk- }- case "VIEW":- if chunk, exists := l.chunks.Views[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Views[identifier] = chunk- }- case "MATERIALIZED VIEW", "MATERIALIZEDVIEW":- // Handle both "MATERIALIZED VIEW" (with space) and "MATERIALIZEDVIEW" (no space)- // The parser may return either depending on the grammar definition- if chunk, exists := l.chunks.MaterializedViews[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.MaterializedViews[identifier] = chunk- }- case "SEQUENCE":- if chunk, exists := l.chunks.Sequences[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Sequences[identifier] = chunk- }- case "INDEX":- if chunk, exists := l.chunks.Indexes[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Indexes[identifier] = chunk- }- case "TYPE":- // Handle COMMENT ON TYPE (for enum types)- if chunk, exists := l.chunks.EnumTypes[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.EnumTypes[identifier] = chunk- }- default:- // Unsupported object type for comment tracking (e.g., FOREIGN TABLE)- // We skip these for now- }- return- }-- // Check for object_type_name: SCHEMA, EXTENSION, DATABASE, etc.- if ctx.Object_type_name() != nil && ctx.Name() != nil {- objectType := ctx.Object_type_name().GetText()- name := pgparser.NormalizePostgreSQLName(ctx.Name())-- objectTypeUpper := strings.ToUpper(objectType)- switch objectTypeUpper {- case "SCHEMA":- if chunk, exists := l.chunks.Schemas[name]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: name,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Schemas[name] = chunk- }- case "EXTENSION":- // Extensions are database-level, no schema prefix- if chunk, exists := l.chunks.Extensions[name]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: name,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Extensions[name] = chunk- }- case "EVENTTRIGGER", "EVENT TRIGGER":- // Event triggers are database-level, no schema prefix- if chunk, exists := l.chunks.EventTriggers[name]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: name,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.EventTriggers[name] = chunk- }- default:- // Other object types are handled elsewhere- }- return- }-- // Handle: COMMENT ON TRIGGER trigger_name ON table_name IS 'comment'- if ctx.Object_type_name_on_any_name() != nil {- objectType := ctx.Object_type_name_on_any_name().GetText()- if strings.ToUpper(objectType) == "TRIGGER" {- // Extract trigger name- if ctx.Name() == nil {- return- }- triggerName := pgparser.NormalizePostgreSQLName(ctx.Name())-- // Extract table name from Any_name- if ctx.Any_name() == nil {- return- }- anyName := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())- if len(anyName) == 0 {- return- }-- // Build fully qualified table name- var tableName string- var unqualifiedTableName string- if len(anyName) >= 2 {- // schema.table- tableName = strings.Join(anyName, ".")- unqualifiedTableName = anyName[1]- } else {- // table (default to public schema)- tableName = "public." + anyName[0]- unqualifiedTableName = anyName[0]- }-- // Parse schema from table name- parts := strings.Split(tableName, ".")- var schemaName string- if len(parts) == 2 {- schemaName = parts[0]- } else {- schemaName = "public"- }-- // Use table-scoped identifier: schema.table.trigger_name- // This matches the identifier format in EnterCreatetrigstmt- identifier := schemaName + "." + unqualifiedTableName + "." + triggerName-- if chunk, exists := l.chunks.Triggers[identifier]; exists {- chunk.CommentStatements = append(chunk.CommentStatements, ctx)- } else {- chunk := &schema.SDLChunk{- Identifier: identifier,- ASTNode: nil,- CommentStatements: []antlr.ParserRuleContext{ctx},- }- l.chunks.Triggers[identifier] = chunk- }- }- }-- // Check for object_type_name_on_any_name: TRIGGER, RULE, POLICY (we don't track these currently)- // These are table-level objects that we may want to support in the future-}diff --git a/backend/plugin/schema/pg/sdl_comment.go b/backend/plugin/schema/pg/sdl_comment.godeleted file mode 100644index 5f550f2c17..0000000000--- a/backend/plugin/schema/pg/sdl_comment.go+++ /dev/null@@ -1,333 +0,0 @@-package pg--import (- "github.com/antlr4-go/antlr/v4"-- parser "github.com/bytebase/parser/postgresql"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processCommentChanges processes COMMENT ON statement changes between current and previous chunks-func processCommentChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Build sets of created and dropped objects to avoid generating comment diffs for them- createdObjects := buildCreatedObjectsSet(diff)- droppedObjects := buildDroppedObjectsSet(diff)-- // Process object-level comments- processObjectComments(currentChunks.Tables, previousChunks.Tables, schema.CommentObjectTypeTable, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.Views, previousChunks.Views, schema.CommentObjectTypeView, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.MaterializedViews, previousChunks.MaterializedViews, schema.CommentObjectTypeMaterializedView, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.Functions, previousChunks.Functions, schema.CommentObjectTypeFunction, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.Sequences, previousChunks.Sequences, schema.CommentObjectTypeSequence, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.Extensions, previousChunks.Extensions, schema.CommentObjectTypeExtension, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.EnumTypes, previousChunks.EnumTypes, schema.CommentObjectTypeType, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.Indexes, previousChunks.Indexes, schema.CommentObjectTypeIndex, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.Schemas, previousChunks.Schemas, schema.CommentObjectTypeSchema, createdObjects, droppedObjects, currentDBSDLChunks, diff)- processObjectComments(currentChunks.EventTriggers, previousChunks.EventTriggers, schema.CommentObjectTypeEventTrigger, createdObjects, droppedObjects, currentDBSDLChunks, diff)-- // Process column comments- processColumnComments(currentChunks, previousChunks, createdObjects, droppedObjects, diff)-}--// buildCreatedObjectsSet builds a set of object identifiers that were created-func buildCreatedObjectsSet(diff *schema.MetadataDiff) map[string]bool {- created := make(map[string]bool)-- for _, tableDiff := range diff.TableChanges {- if tableDiff.Action == schema.MetadataDiffActionCreate {- identifier := tableDiff.SchemaName + "." + tableDiff.TableName- created[identifier] = true- }- }-- for _, viewDiff := range diff.ViewChanges {- if viewDiff.Action == schema.MetadataDiffActionCreate {- identifier := viewDiff.SchemaName + "." + viewDiff.ViewName- created[identifier] = true- }- }-- for _, mvDiff := range diff.MaterializedViewChanges {- if mvDiff.Action == schema.MetadataDiffActionCreate {- identifier := mvDiff.SchemaName + "." + mvDiff.MaterializedViewName- created[identifier] = true- }- }-- for _, funcDiff := range diff.FunctionChanges {- if funcDiff.Action == schema.MetadataDiffActionCreate {- identifier := funcDiff.SchemaName + "." + funcDiff.FunctionName- created[identifier] = true- }- }-- for _, seqDiff := range diff.SequenceChanges {- if seqDiff.Action == schema.MetadataDiffActionCreate {- identifier := seqDiff.SchemaName + "." + seqDiff.SequenceName- created[identifier] = true- }- }-- for _, enumDiff := range diff.EnumTypeChanges {- if enumDiff.Action == schema.MetadataDiffActionCreate {- identifier := enumDiff.SchemaName + "." + enumDiff.EnumTypeName- created[identifier] = true- }- }-- for _, extDiff := range diff.ExtensionChanges {- if extDiff.Action == schema.MetadataDiffActionCreate {- // Extensions are database-level, no schema prefix- created[extDiff.ExtensionName] = true- }- }-- for _, etDiff := range diff.EventTriggerChanges {- if etDiff.Action == schema.MetadataDiffActionCreate {- // Event triggers are database-level, no schema prefix- created[etDiff.EventTriggerName] = true- }- }-- return created-}--// buildDroppedObjectsSet builds a set of object identifiers that were dropped-func buildDroppedObjectsSet(diff *schema.MetadataDiff) map[string]bool {- dropped := make(map[string]bool)-- for _, tableDiff := range diff.TableChanges {- if tableDiff.Action == schema.MetadataDiffActionDrop {- identifier := tableDiff.SchemaName + "." + tableDiff.TableName- dropped[identifier] = true- }- }-- for _, viewDiff := range diff.ViewChanges {- if viewDiff.Action == schema.MetadataDiffActionDrop {- identifier := viewDiff.SchemaName + "." + viewDiff.ViewName- dropped[identifier] = true- }- }-- for _, mvDiff := range diff.MaterializedViewChanges {- if mvDiff.Action == schema.MetadataDiffActionDrop {- identifier := mvDiff.SchemaName + "." + mvDiff.MaterializedViewName- dropped[identifier] = true- }- }-- for _, funcDiff := range diff.FunctionChanges {- if funcDiff.Action == schema.MetadataDiffActionDrop {- identifier := funcDiff.SchemaName + "." + funcDiff.FunctionName- dropped[identifier] = true- }- }-- for _, seqDiff := range diff.SequenceChanges {- if seqDiff.Action == schema.MetadataDiffActionDrop {- identifier := seqDiff.SchemaName + "." + seqDiff.SequenceName- dropped[identifier] = true- }- }-- for _, enumDiff := range diff.EnumTypeChanges {- if enumDiff.Action == schema.MetadataDiffActionDrop {- identifier := enumDiff.SchemaName + "." + enumDiff.EnumTypeName- dropped[identifier] = true- }- }-- for _, extDiff := range diff.ExtensionChanges {- if extDiff.Action == schema.MetadataDiffActionDrop {- // Extensions are database-level, no schema prefix- dropped[extDiff.ExtensionName] = true- }- }-- for _, etDiff := range diff.EventTriggerChanges {- if etDiff.Action == schema.MetadataDiffActionDrop {- // Event triggers are database-level, no schema prefix- dropped[etDiff.EventTriggerName] = true- }- }-- return dropped-}--// processObjectComments processes comment changes for a specific object type-// droppedObjects is intentionally unused because we only process objects in currentMap,-// and dropped objects won't appear there.-func processObjectComments(currentMap, previousMap map[string]*schema.SDLChunk, objectType schema.CommentObjectType, createdObjects, _ map[string]bool, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process all objects in current chunks- for identifier, currentChunk := range currentMap {- // Skip if object was created (comment will be in CREATE statement)- if createdObjects[identifier] {- continue- }-- previousChunk := previousMap[identifier]- if previousChunk == nil {- // Object doesn't exist in previous - this shouldn't happen as it should be in createdObjects- continue- }-- // Extract comment text from both chunks- currentCommentText := extractCommentTextFromChunk(currentChunk)- previousCommentText := extractCommentTextFromChunk(previousChunk)-- // If comments are different, check usability before generating a CommentDiff- if currentCommentText != previousCommentText {- // Apply usability check: skip comment diff if current comment matches database metadata- if currentDBSDLChunks != nil && shouldSkipCommentDiff(currentCommentText, identifier, currentDBSDLChunks) {- continue- }- var schemaName, objectName string-- // For SCHEMA, EXTENSION, and EVENT TRIGGER objects, identifier is just the object name (database-level)- // For other objects, identifier is "schema.object"- switch objectType {- case schema.CommentObjectTypeSchema:- schemaName = identifier- objectName = identifier // For schemas, objectName is also the schema name- case schema.CommentObjectTypeExtension, schema.CommentObjectTypeEventTrigger:- schemaName = "" // Extensions and event triggers are database-level, no schema- objectName = identifier- default:- schemaName, objectName = parseIdentifier(identifier)- }-- action := schema.MetadataDiffActionAlter- if previousCommentText == "" {- action = schema.MetadataDiffActionCreate- }-- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: action,- ObjectType: objectType,- SchemaName: schemaName,- ObjectName: objectName,- OldComment: previousCommentText,- NewComment: currentCommentText,- OldASTNode: getFirstCommentNode(previousChunk.CommentStatements),- NewASTNode: getFirstCommentNode(currentChunk.CommentStatements),- })- }- }-}--// processColumnComments processes column comment changes-func processColumnComments(currentChunks, previousChunks *schema.SDLChunks, createdObjects, droppedObjects map[string]bool, diff *schema.MetadataDiff) {- // Process all tables in current chunks- allTableIdentifiers := make(map[string]bool)- for identifier := range currentChunks.ColumnComments {- allTableIdentifiers[identifier] = true- }- for identifier := range previousChunks.ColumnComments {- allTableIdentifiers[identifier] = true- }-- for tableIdentifier := range allTableIdentifiers {- // Skip if table was created or dropped- if createdObjects[tableIdentifier] || droppedObjects[tableIdentifier] {- continue- }-- currentColumns := currentChunks.ColumnComments[tableIdentifier]- previousColumns := previousChunks.ColumnComments[tableIdentifier]-- // Find all column names- allColumnNames := make(map[string]bool)- for columnName := range currentColumns {- allColumnNames[columnName] = true- }- for columnName := range previousColumns {- allColumnNames[columnName] = true- }-- // Compare each column's comment- for columnName := range allColumnNames {- currentNode := currentColumns[columnName]- previousNode := previousColumns[columnName]-- currentCommentText := extractCommentTextFromNode(currentNode)- previousCommentText := extractCommentTextFromNode(previousNode)-- if currentCommentText != previousCommentText {- schemaName, tableName := parseIdentifier(tableIdentifier)- action := schema.MetadataDiffActionAlter- if previousCommentText == "" {- action = schema.MetadataDiffActionCreate- }-- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: action,- ObjectType: schema.CommentObjectTypeColumn,- SchemaName: schemaName,- ObjectName: tableName,- ColumnName: columnName,- OldComment: previousCommentText,- NewComment: currentCommentText,- OldASTNode: previousNode,- NewASTNode: currentNode,- })- }- }- }-}--// extractCommentTextFromChunk extracts the comment text from a chunk's comment statements-func extractCommentTextFromChunk(chunk *schema.SDLChunk) string {- if chunk == nil || len(chunk.CommentStatements) == 0 {- return ""- }-- // Get the first comment statement (there should typically only be one)- return extractCommentTextFromNode(chunk.CommentStatements[0])-}--// extractCommentTextFromNode extracts comment text from a COMMENT ON statement AST node-func extractCommentTextFromNode(node antlr.ParserRuleContext) string {- if node == nil {- return ""- }-- // Try to cast to CommentstmtContext- commentStmt, ok := node.(*parser.CommentstmtContext)- if !ok {- return ""- }-- // Get the comment_text- if commentStmt.Comment_text() == nil {- return ""- }-- commentTextCtx := commentStmt.Comment_text()-- // Check if it's NULL_P- if commentTextCtx.NULL_P() != nil {- return "" // NULL comment means no comment- }-- // Get the sconst (string constant)- if commentTextCtx.Sconst() != nil {- text := commentTextCtx.Sconst().GetText()- // Remove surrounding quotes- if len(text) >= 2 && text[0] == '\'' && text[len(text)-1] == '\'' {- return text[1 : len(text)-1]- }- return text- }-- return ""-}--// getFirstCommentNode returns the first comment AST node from a list, or nil if empty-func getFirstCommentNode(nodes []antlr.ParserRuleContext) antlr.ParserRuleContext {- if len(nodes) == 0 {- return nil- }- return nodes[0]-}diff --git a/backend/plugin/schema/pg/sdl_common.go b/backend/plugin/schema/pg/sdl_common.godeleted file mode 100644index 046b9cc26e..0000000000--- a/backend/plugin/schema/pg/sdl_common.go+++ /dev/null@@ -1,121 +0,0 @@-package pg--import (- "strings"-- "github.com/antlr4-go/antlr/v4"-- parser "github.com/bytebase/parser/postgresql"-)--// parseIdentifier parses a table identifier and returns schema name and object name-func parseIdentifier(identifier string) (schemaName, objectName string) {- parts := strings.Split(identifier, ".")- if len(parts) == 2 {- return parts[0], parts[1]- }- return "public", identifier-}--// extractAlterTexts extracts and concatenates text from a list of ALTER statement nodes-func extractAlterTexts(alterNodes []antlr.ParserRuleContext) string {- if len(alterNodes) == 0 {- return ""- }-- var parts []string- for _, node := range alterNodes {- text := extractTextFromNode(node)- if text != "" {- parts = append(parts, text)- }- }- return strings.Join(parts, "\n\n")-}--// extractTextFromNode extracts text from a parser rule context node-func extractTextFromNode(node antlr.ParserRuleContext) string {- if node == nil {- return ""- }-- // Check for interfaces that have the required methods- type parserContext interface {- GetParser() antlr.Parser- GetStart() antlr.Token- GetStop() antlr.Token- }-- if ruleContext, ok := node.(parserContext); ok {- if parser := ruleContext.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := ruleContext.GetStart()- stop := ruleContext.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }- }-- // Fallback to node's GetText method- return node.GetText()-}--// extractSchemaAndTypeFromTypename extracts schema.type identifier from a Typename context-// Used for parsing COMMENT ON TYPE statements-func extractSchemaAndTypeFromTypename(typenameCtx *parser.TypenameContext) string {- if typenameCtx == nil {- return ""- }-- // Helper function to normalize a PostgreSQL identifier string- normalizeIdentifier := func(text string) string {- if len(text) >= 2 && text[0] == '"' && text[len(text)-1] == '"' {- // Quoted identifier - preserve case but remove quotes- return text[1 : len(text)-1]- }- // Unquoted identifier - convert to lowercase- return strings.ToLower(text)- }-- // Navigate to Simpletypename -> Generictype- if typenameCtx.GetChildCount() >= 1 {- if simpleTypeCtx, ok := typenameCtx.GetChild(0).(*parser.SimpletypenameContext); ok {- if simpleTypeCtx.GetChildCount() >= 1 {- if genericTypeCtx, ok := simpleTypeCtx.GetChild(0).(*parser.GenerictypeContext); ok {- // Generictype contains Type_function_name (schema) and Attrs (.typename)- var schemaName string- var typeName string-- if genericTypeCtx.GetChildCount() >= 1 {- if tfnCtx, ok := genericTypeCtx.GetChild(0).(*parser.Type_function_nameContext); ok {- schemaName = normalizeIdentifier(tfnCtx.GetText())- }- }-- if genericTypeCtx.GetChildCount() >= 2 {- if attrsCtx, ok := genericTypeCtx.GetChild(1).(*parser.AttrsContext); ok {- // Attrs contains ".typename", need to extract the typename- attrsText := attrsCtx.GetText()- // Remove leading dot and normalize- if len(attrsText) > 1 && attrsText[0] == '.' {- typeName = normalizeIdentifier(attrsText[1:])- }- }- }-- if schemaName != "" && typeName != "" {- return schemaName + "." + typeName- }- // If only Type_function_name is present (no attrs), treat it as the type name with public schema- if schemaName != "" && typeName == "" {- return "public." + schemaName- }- }- }- }- }-- return ""-}diff --git a/backend/plugin/schema/pg/sdl_constraint.go b/backend/plugin/schema/pg/sdl_constraint.godeleted file mode 100644index 08ba6426f4..0000000000--- a/backend/plugin/schema/pg/sdl_constraint.go+++ /dev/null@@ -1,690 +0,0 @@-package pg--import (- parser "github.com/bytebase/parser/postgresql"-- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// ForeignKeyDefWithAST holds foreign key constraint definition with its AST node for text comparison-type ForeignKeyDefWithAST struct {- Name string- ASTNode parser.ITableconstraintContext-}--// CheckConstraintDefWithAST holds check constraint definition with its AST node for text comparison-type CheckConstraintDefWithAST struct {- Name string- ASTNode parser.ITableconstraintContext-}--// ExcludeConstraintDefWithAST holds EXCLUDE constraint definition with its AST node for text comparison-type ExcludeConstraintDefWithAST struct {- Name string- ASTNode parser.ITableconstraintContext-}--// IndexDefWithAST holds index/unique constraint definition with its AST node for text comparison-type IndexDefWithAST struct {- Name string- ASTNode parser.ITableconstraintContext-}--// processForeignKeyChanges analyzes foreign key constraint changes between old and new table definitions-// Following the text-first comparison pattern for performance optimization-func processForeignKeyChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.ForeignKeyDiff {- if oldTable == nil || newTable == nil {- return []*schema.ForeignKeyDiff{}- }-- // Step 1: Extract all foreign key definitions with their AST nodes for text comparison- oldFKList := extractForeignKeyDefinitionsInOrder(oldTable)- newFKList := extractForeignKeyDefinitionsInOrder(newTable)-- // Create maps for quick lookup- oldFKMap := make(map[string]*ForeignKeyDefWithAST)- for _, def := range oldFKList {- oldFKMap[def.Name] = def- }- newFKMap := make(map[string]*ForeignKeyDefWithAST)- for _, def := range newFKList {- newFKMap[def.Name] = def- }-- var fkDiffs []*schema.ForeignKeyDiff-- // Step 2: Process current foreign keys to find created and modified foreign keys- for _, newFKDef := range newFKList {- if oldFKDef, exists := oldFKMap[newFKDef.Name]; exists {- // FK exists in both - check if modified by comparing text first- currentText := getForeignKeyText(newFKDef.ASTNode)- previousText := getForeignKeyText(oldFKDef.ASTNode)- if currentText != previousText {- // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL- schemaName, tableName := parseIdentifier(tableIdentifier)- if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, newFKDef.Name) {- continue- }- // FK was modified - drop and recreate (PostgreSQL pattern)- fkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldFKDef.ASTNode,- })- fkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newFKDef.ASTNode,- })- }- } else {- // New foreign key - store AST node only- fkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newFKDef.ASTNode,- })- }- }-- // Step 3: Process old foreign keys to find dropped ones- for _, oldFKDef := range oldFKList {- if _, exists := newFKMap[oldFKDef.Name]; !exists {- // Foreign key was dropped - store AST node only- fkDiffs = append(fkDiffs, &schema.ForeignKeyDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldFKDef.ASTNode,- })- }- }-- return fkDiffs-}--// processCheckConstraintChanges analyzes check constraint changes between old and new table definitions-// Following the text-first comparison pattern for performance optimization-func processCheckConstraintChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.CheckConstraintDiff {- if oldTable == nil || newTable == nil {- return []*schema.CheckConstraintDiff{}- }-- // Step 1: Extract all check constraint definitions with their AST nodes for text comparison- oldCheckList := extractCheckConstraintDefinitionsInOrder(oldTable)- newCheckList := extractCheckConstraintDefinitionsInOrder(newTable)-- // Create maps for quick lookup- oldCheckMap := make(map[string]*CheckConstraintDefWithAST)- for _, def := range oldCheckList {- oldCheckMap[def.Name] = def- }- newCheckMap := make(map[string]*CheckConstraintDefWithAST)- for _, def := range newCheckList {- newCheckMap[def.Name] = def- }-- var checkDiffs []*schema.CheckConstraintDiff-- // Step 2: Process current check constraints to find created and modified check constraints- for _, newCheckDef := range newCheckList {- if oldCheckDef, exists := oldCheckMap[newCheckDef.Name]; exists {- // Check constraint exists in both - check if modified by comparing text first- currentText := getCheckConstraintText(newCheckDef.ASTNode)- previousText := getCheckConstraintText(oldCheckDef.ASTNode)- if currentText != previousText {- // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL- schemaName, tableName := parseIdentifier(tableIdentifier)- if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, newCheckDef.Name) {- continue- }- // Check constraint was modified - drop and recreate (PostgreSQL pattern)- checkDiffs = append(checkDiffs, &schema.CheckConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldCheckDef.ASTNode,- })- checkDiffs = append(checkDiffs, &schema.CheckConstraintDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newCheckDef.ASTNode,- })- }- } else {- // New check constraint - store AST node only- checkDiffs = append(checkDiffs, &schema.CheckConstraintDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newCheckDef.ASTNode,- })- }- }-- // Step 3: Process old check constraints to find dropped ones- for _, oldCheckDef := range oldCheckList {- if _, exists := newCheckMap[oldCheckDef.Name]; !exists {- // Check constraint was dropped - store AST node only- checkDiffs = append(checkDiffs, &schema.CheckConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldCheckDef.ASTNode,- })- }- }-- return checkDiffs-}--// processExcludeConstraintChanges analyzes EXCLUDE constraint changes between old and new table definitions-// Following the text-first comparison pattern for performance optimization-func processExcludeConstraintChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.ExcludeConstraintDiff {- if oldTable == nil || newTable == nil {- return []*schema.ExcludeConstraintDiff{}- }-- // Step 1: Extract all EXCLUDE constraint definitions with their AST nodes for text comparison- oldExcludeList := extractExcludeConstraintDefinitionsInOrder(oldTable)- newExcludeList := extractExcludeConstraintDefinitionsInOrder(newTable)-- // Create maps for quick lookup- oldExcludeMap := make(map[string]*ExcludeConstraintDefWithAST)- for _, def := range oldExcludeList {- oldExcludeMap[def.Name] = def- }- newExcludeMap := make(map[string]*ExcludeConstraintDefWithAST)- for _, def := range newExcludeList {- newExcludeMap[def.Name] = def- }-- var excludeDiffs []*schema.ExcludeConstraintDiff-- // Step 2: Process current EXCLUDE constraints to find created and modified EXCLUDE constraints- for _, newExcludeDef := range newExcludeList {- if oldExcludeDef, exists := oldExcludeMap[newExcludeDef.Name]; exists {- // EXCLUDE constraint exists in both - check if modified by comparing text first- currentText := getExcludeConstraintText(newExcludeDef.ASTNode)- previousText := getExcludeConstraintText(oldExcludeDef.ASTNode)- if currentText != previousText {- // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL- schemaName, tableName := parseIdentifier(tableIdentifier)- if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, newExcludeDef.Name) {- continue- }- // EXCLUDE constraint was modified - drop and recreate (PostgreSQL pattern)- excludeDiffs = append(excludeDiffs, &schema.ExcludeConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldExcludeDef.ASTNode,- })- excludeDiffs = append(excludeDiffs, &schema.ExcludeConstraintDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newExcludeDef.ASTNode,- })- }- } else {- // New EXCLUDE constraint - store AST node only- excludeDiffs = append(excludeDiffs, &schema.ExcludeConstraintDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newExcludeDef.ASTNode,- })- }- }-- // Step 3: Process old EXCLUDE constraints to find dropped ones- for _, oldExcludeDef := range oldExcludeList {- if _, exists := newExcludeMap[oldExcludeDef.Name]; !exists {- // EXCLUDE constraint was dropped - store AST node only- excludeDiffs = append(excludeDiffs, &schema.ExcludeConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldExcludeDef.ASTNode,- })- }- }-- return excludeDiffs-}--// processPrimaryKeyChanges analyzes primary key constraint changes between old and new table definitions-func processPrimaryKeyChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.PrimaryKeyDiff {- if oldTable == nil || newTable == nil {- return []*schema.PrimaryKeyDiff{}- }-- // Step 1: Extract all primary key constraint definitions with their AST nodes for text comparison- oldPKMap := extractPrimaryKeyDefinitionsWithAST(oldTable)- newPKMap := extractPrimaryKeyDefinitionsWithAST(newTable)-- var pkDiffs []*schema.PrimaryKeyDiff-- // Step 2: Process current primary keys to find created and modified primary keys- for pkName, newPKDef := range newPKMap {- if oldPKDef, exists := oldPKMap[pkName]; exists {- // PK exists in both - check if modified by comparing text first- currentText := getIndexText(newPKDef.ASTNode)- previousText := getIndexText(oldPKDef.ASTNode)- if currentText != previousText {- // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL- schemaName, tableName := parseIdentifier(tableIdentifier)- if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, pkName) {- continue- }- // PK was modified - store AST nodes only- pkDiffs = append(pkDiffs, &schema.PrimaryKeyDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldPKDef.ASTNode,- })- pkDiffs = append(pkDiffs, &schema.PrimaryKeyDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newPKDef.ASTNode,- })- }- } else {- // New PK - store AST node only- pkDiffs = append(pkDiffs, &schema.PrimaryKeyDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newPKDef.ASTNode,- })- }- }-- // Step 3: Process old primary keys to find dropped ones- for pkName, oldPKDef := range oldPKMap {- if _, exists := newPKMap[pkName]; !exists {- // PK was dropped - store AST node only- pkDiffs = append(pkDiffs, &schema.PrimaryKeyDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldPKDef.ASTNode,- })- }- }-- return pkDiffs-}--// processUniqueConstraintChanges analyzes unique constraint changes between old and new table definitions-func processUniqueConstraintChanges(oldTable, newTable *parser.CreatestmtContext, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.UniqueConstraintDiff {- if oldTable == nil || newTable == nil {- return []*schema.UniqueConstraintDiff{}- }-- // Step 1: Extract all unique constraint definitions with their AST nodes for text comparison- oldUKList := extractUniqueConstraintDefinitionsInOrder(oldTable)- newUKList := extractUniqueConstraintDefinitionsInOrder(newTable)-- // Create maps for quick lookup- oldUKMap := make(map[string]*IndexDefWithAST)- for _, def := range oldUKList {- oldUKMap[def.Name] = def- }- newUKMap := make(map[string]*IndexDefWithAST)- for _, def := range newUKList {- newUKMap[def.Name] = def- }-- var ukDiffs []*schema.UniqueConstraintDiff-- // Step 2: Process current unique constraints to find created and modified unique constraints- for _, newUKDef := range newUKList {- if oldUKDef, exists := oldUKMap[newUKDef.Name]; exists {- // UK exists in both - check if modified by comparing text first- currentText := getIndexText(newUKDef.ASTNode)- previousText := getIndexText(oldUKDef.ASTNode)- if currentText != previousText {- // Apply constraint-level usability check: skip diff if current constraint matches database metadata SDL- schemaName, tableName := parseIdentifier(tableIdentifier)- if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipConstraintDiffForUsability(currentText, schemaName, tableName, newUKDef.Name) {- continue- }- // UK was modified - drop and recreate (PostgreSQL pattern)- ukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldUKDef.ASTNode,- })- ukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newUKDef.ASTNode,- })- }- } else {- // New UK - store AST node only- ukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: newUKDef.ASTNode,- })- }- }-- // Step 3: Process old unique constraints to find dropped ones- for _, oldUKDef := range oldUKList {- if _, exists := newUKMap[oldUKDef.Name]; !exists {- // UK was dropped - store AST node only- ukDiffs = append(ukDiffs, &schema.UniqueConstraintDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: oldUKDef.ASTNode,- })- }- }-- return ukDiffs-}--// extractUniqueConstraintDefinitionsInOrder extracts unique constraints with their AST nodes in SQL order-func extractUniqueConstraintDefinitionsInOrder(createStmt *parser.CreatestmtContext) []*IndexDefWithAST {- var ukList []*IndexDefWithAST-- if createStmt == nil || createStmt.Opttableelementlist() == nil {- return ukList- }-- tableElementList := createStmt.Opttableelementlist().Tableelementlist()- if tableElementList == nil {- return ukList- }-- for _, element := range tableElementList.AllTableelement() {- if element.Tableconstraint() != nil {- constraint := element.Tableconstraint()- if constraint.Constraintelem() != nil {- elem := constraint.Constraintelem()- // Check for UNIQUE constraints (but not PRIMARY KEY)- isUnique := elem.UNIQUE() != nil && (elem.PRIMARY() == nil || elem.KEY() == nil)-- if isUnique {- // This is a unique constraint- name := ""- if constraint.Name() != nil {- name = pgparser.NormalizePostgreSQLName(constraint.Name())- }- // Use constraint definition text as fallback key if name is empty- if name == "" {- // Get the full original text from tokens- if parser := constraint.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- name = tokenStream.GetTextFromRuleContext(constraint)- }- }- if name == "" {- name = constraint.GetText() // Final fallback- }- }- ukList = append(ukList, &IndexDefWithAST{- Name: name,- ASTNode: constraint,- })- }- }- }- }-- return ukList-}--// extractForeignKeyDefinitionsInOrder extracts foreign key constraints with their AST nodes in SQL order-func extractForeignKeyDefinitionsInOrder(createStmt *parser.CreatestmtContext) []*ForeignKeyDefWithAST {- var fkList []*ForeignKeyDefWithAST-- if createStmt == nil || createStmt.Opttableelementlist() == nil {- return fkList- }-- tableElementList := createStmt.Opttableelementlist().Tableelementlist()- if tableElementList == nil {- return fkList- }-- for _, element := range tableElementList.AllTableelement() {- if element.Tableconstraint() != nil {- constraint := element.Tableconstraint()- if constraint.Constraintelem() != nil {- elem := constraint.Constraintelem()- if elem.FOREIGN() != nil && elem.KEY() != nil {- // This is a foreign key constraint- name := ""- if constraint.Name() != nil {- name = pgparser.NormalizePostgreSQLName(constraint.Name())- }- // Use constraint definition text as fallback key if name is empty- if name == "" {- // Get the full original text from tokens- if parser := constraint.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- name = tokenStream.GetTextFromRuleContext(constraint)- }- }- if name == "" {- name = constraint.GetText() // Final fallback- }- }- fkList = append(fkList, &ForeignKeyDefWithAST{- Name: name,- ASTNode: constraint,- })- }- }- }- }-- return fkList-}--// extractCheckConstraintDefinitionsInOrder extracts check constraints with their AST nodes in SQL order-func extractCheckConstraintDefinitionsInOrder(createStmt *parser.CreatestmtContext) []*CheckConstraintDefWithAST {- var checkList []*CheckConstraintDefWithAST-- if createStmt == nil || createStmt.Opttableelementlist() == nil {- return checkList- }-- tableElementList := createStmt.Opttableelementlist().Tableelementlist()- if tableElementList == nil {- return checkList- }-- for _, element := range tableElementList.AllTableelement() {- if element.Tableconstraint() != nil {- constraint := element.Tableconstraint()- if constraint.Constraintelem() != nil {- elem := constraint.Constraintelem()- if elem.CHECK() != nil {- // This is a check constraint- name := ""- if constraint.Name() != nil {- name = pgparser.NormalizePostgreSQLName(constraint.Name())- }- // Use constraint definition text as fallback key if name is empty- if name == "" {- // Get the full original text from tokens- if parser := constraint.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- name = tokenStream.GetTextFromRuleContext(constraint)- }- }- if name == "" {- name = constraint.GetText() // Final fallback- }- }- checkList = append(checkList, &CheckConstraintDefWithAST{- Name: name,- ASTNode: constraint,- })- }- }- }- }-- return checkList-}--// extractExcludeConstraintDefinitionsInOrder extracts EXCLUDE constraints in their original order with AST nodes-func extractExcludeConstraintDefinitionsInOrder(createStmt *parser.CreatestmtContext) []*ExcludeConstraintDefWithAST {- var excludeList []*ExcludeConstraintDefWithAST-- if createStmt == nil || createStmt.Opttableelementlist() == nil {- return excludeList- }-- tableElementList := createStmt.Opttableelementlist().Tableelementlist()- if tableElementList == nil {- return excludeList- }-- for _, element := range tableElementList.AllTableelement() {- if element.Tableconstraint() != nil {- constraint := element.Tableconstraint()- if constraint.Constraintelem() != nil {- elem := constraint.Constraintelem()- if elem.EXCLUDE() != nil {- // This is an EXCLUDE constraint- name := ""- if constraint.Name() != nil {- name = pgparser.NormalizePostgreSQLName(constraint.Name())- }- // Use constraint definition text as fallback key if name is empty- if name == "" {- // Get the full original text from tokens- if parser := constraint.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- name = tokenStream.GetTextFromRuleContext(constraint)- }- }- if name == "" {- name = constraint.GetText() // Final fallback- }- }- excludeList = append(excludeList, &ExcludeConstraintDefWithAST{- Name: name,- ASTNode: constraint,- })- }- }- }- }-- return excludeList-}--// extractPrimaryKeyDefinitionsWithAST extracts primary key constraints with their AST nodes-func extractPrimaryKeyDefinitionsWithAST(createStmt *parser.CreatestmtContext) map[string]*IndexDefWithAST {- pkMap := make(map[string]*IndexDefWithAST)-- if createStmt == nil || createStmt.Opttableelementlist() == nil {- return pkMap- }-- tableElementList := createStmt.Opttableelementlist().Tableelementlist()- if tableElementList == nil {- return pkMap- }-- for _, element := range tableElementList.AllTableelement() {- if element.Tableconstraint() != nil {- constraint := element.Tableconstraint()- if constraint.Constraintelem() != nil {- elem := constraint.Constraintelem()- // Check for PRIMARY KEY constraints- isPrimary := elem.PRIMARY() != nil && elem.KEY() != nil-- if isPrimary {- // This is a primary key constraint- name := ""- if constraint.Name() != nil {- name = pgparser.NormalizePostgreSQLName(constraint.Name())- }- // Use constraint definition text as fallback key if name is empty- if name == "" {- // Get the full original text from tokens- if parser := constraint.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- name = tokenStream.GetTextFromRuleContext(constraint)- }- }- if name == "" {- name = constraint.GetText() // Final fallback- }- }- pkMap[name] = &IndexDefWithAST{- Name: name,- ASTNode: constraint,- }- }- }- }- }-- return pkMap-}--// getForeignKeyText returns the text representation of a foreign key constraint for comparison-func getForeignKeyText(constraintAST parser.ITableconstraintContext) string {- if constraintAST == nil {- return ""- }-- // Get tokens from the parser for precise text extraction- if parser := constraintAST.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := constraintAST.GetStart()- stop := constraintAST.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return constraintAST.GetText()-}--// getCheckConstraintText returns the text representation of a check constraint for comparison-func getCheckConstraintText(constraintAST parser.ITableconstraintContext) string {- if constraintAST == nil {- return ""- }-- // Get tokens from the parser for precise text extraction- if parser := constraintAST.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := constraintAST.GetStart()- stop := constraintAST.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return constraintAST.GetText()-}--// getExcludeConstraintText returns the text representation of an EXCLUDE constraint for comparison-func getExcludeConstraintText(constraintAST parser.ITableconstraintContext) string {- if constraintAST == nil {- return ""- }-- // Get tokens from the parser for precise text extraction- if parser := constraintAST.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := constraintAST.GetStart()- stop := constraintAST.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return constraintAST.GetText()-}--// getIndexText returns the text representation of an index/unique constraint for comparison-func getIndexText(constraintAST parser.ITableconstraintContext) string {- if constraintAST == nil {- return ""- }-- // Get tokens from the parser for precise text extraction- if parser := constraintAST.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := constraintAST.GetStart()- stop := constraintAST.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return constraintAST.GetText()-}diff --git a/backend/plugin/schema/pg/sdl_diff_table_test.go b/backend/plugin/schema/pg/sdl_diff_table_test.godeleted file mode 100644index 2fae4d16a1..0000000000--- a/backend/plugin/schema/pg/sdl_diff_table_test.go+++ /dev/null@@ -1,167 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestTableDiffScenarios(t *testing.T) {- testCases := []struct {- name string- currentSDL string- previousSDL string- expectedCreateCount int- expectedAlterCount int- expectedDropCount int- expectedTableNames []string- }{- {- name: "Create new table",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- previousSDL: "",- expectedCreateCount: 1,- expectedAlterCount: 0,- expectedDropCount: 0,- expectedTableNames: []string{"users"},- },- {- name: "Drop existing table",- currentSDL: "",- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- expectedCreateCount: 0,- expectedAlterCount: 0,- expectedDropCount: 1,- expectedTableNames: []string{"users"},- },- {- name: "Modify existing table",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(255)- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- expectedCreateCount: 0,- expectedAlterCount: 1,- expectedDropCount: 0,- expectedTableNames: []string{"users"},- },- {- name: "Mixed operations",- currentSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(255)- );-- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );`,- previousSDL: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE TABLE old_table (- id SERIAL PRIMARY KEY- );`,- expectedCreateCount: 1, // products- expectedAlterCount: 1, // users- expectedDropCount: 1, // old_table- expectedTableNames: []string{"users", "products", "old_table"},- },- {- name: "Schema qualified tables",- currentSDL: `CREATE TABLE public.users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE TABLE admin.settings (- key VARCHAR(255) PRIMARY KEY,- value TEXT- );`,- previousSDL: `CREATE TABLE public.users (- id SERIAL PRIMARY KEY- );`,- expectedCreateCount: 1, // admin.settings- expectedAlterCount: 1, // public.users- expectedDropCount: 0,- expectedTableNames: []string{"users", "settings"},- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- diff, err := GetSDLDiff(tc.currentSDL, tc.previousSDL, nil)- require.NoError(t, err)-- // Count different action types- createCount := 0- alterCount := 0- dropCount := 0-- for _, change := range diff.TableChanges {- switch change.Action {- case schema.MetadataDiffActionCreate:- createCount++- case schema.MetadataDiffActionAlter:- alterCount++- case schema.MetadataDiffActionDrop:- dropCount++- default:- // Should not reach here with valid actions- }- }-- assert.Equal(t, tc.expectedCreateCount, createCount, "CREATE count mismatch")- assert.Equal(t, tc.expectedAlterCount, alterCount, "ALTER count mismatch")- assert.Equal(t, tc.expectedDropCount, dropCount, "DROP count mismatch")-- // Verify table names- actualTableNames := make([]string, len(diff.TableChanges))- for i, change := range diff.TableChanges {- actualTableNames[i] = change.TableName- }-- assert.ElementsMatch(t, tc.expectedTableNames, actualTableNames, "Table names mismatch")-- // Verify schema names are set correctly- for _, change := range diff.TableChanges {- assert.NotEmpty(t, change.SchemaName, "Schema name should not be empty")- }-- // Verify AST nodes are populated correctly- for _, change := range diff.TableChanges {- switch change.Action {- case schema.MetadataDiffActionCreate:- assert.Nil(t, change.OldASTNode, "CREATE action should have nil OldASTNode")- assert.NotNil(t, change.NewASTNode, "CREATE action should have NewASTNode")- case schema.MetadataDiffActionAlter:- assert.NotNil(t, change.OldASTNode, "ALTER action should have OldASTNode")- assert.NotNil(t, change.NewASTNode, "ALTER action should have NewASTNode")- assert.NotEqual(t, change.OldASTNode, change.NewASTNode, "ALTER action should have different OldASTNode and NewASTNode")- case schema.MetadataDiffActionDrop:- assert.NotNil(t, change.OldASTNode, "DROP action should have OldASTNode")- assert.Nil(t, change.NewASTNode, "DROP action should have nil NewASTNode")- default:- // Should not reach here with valid actions- }- }- })- }-}diff --git a/backend/plugin/schema/pg/sdl_enum.go b/backend/plugin/schema/pg/sdl_enum.godeleted file mode 100644index 0b0b749815..0000000000--- a/backend/plugin/schema/pg/sdl_enum.go+++ /dev/null@@ -1,108 +0,0 @@-package pg--import (- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processEnumTypeChanges analyzes enum type changes between current and previous chunks-// Enum types use DROP + CREATE pattern for modifications (PostgreSQL doesn't support ALTER TYPE ... RENAME VALUE)-func processEnumTypeChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process current enum types to find created and modified ones- for _, currentChunk := range currentChunks.EnumTypes {- if previousChunk, exists := previousChunks.EnumTypes[currentChunk.Identifier]; exists {- // Enum type exists in both - check if modified by comparing text (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }- // Enum type was modified - use DROP + CREATE pattern- schemaName, enumName := parseIdentifier(currentChunk.Identifier)- // Add DROP diff- diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- EnumTypeName: enumName,- OldEnumType: nil,- NewEnumType: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- // Add CREATE diff- diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- EnumTypeName: enumName,- OldEnumType: nil,- NewEnumType: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON TYPE diffs if they exist in the new version- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeType,- SchemaName: schemaName,- ObjectName: enumName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- // If text is identical, skip - no changes detected- } else {- // New enum type- schemaName, enumName := parseIdentifier(currentChunk.Identifier)- diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- EnumTypeName: enumName,- OldEnumType: nil,- NewEnumType: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON TYPE diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeType,- SchemaName: schemaName,- ObjectName: enumName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Process previous enum types to find dropped ones- for identifier, previousChunk := range previousChunks.EnumTypes {- if _, exists := currentChunks.EnumTypes[identifier]; !exists {- // Enum type was dropped- schemaName, enumName := parseIdentifier(identifier)- diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- EnumTypeName: enumName,- OldEnumType: nil,- NewEnumType: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- }- }-}diff --git a/backend/plugin/schema/pg/sdl_event_trigger.go b/backend/plugin/schema/pg/sdl_event_trigger.godeleted file mode 100644index fe3dffdf66..0000000000--- a/backend/plugin/schema/pg/sdl_event_trigger.go+++ /dev/null@@ -1,84 +0,0 @@-package pg--import (- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processEventTriggerChanges processes event trigger changes between current and previous chunks-func processEventTriggerChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process current event triggers to find created and modified ones- for eventTriggerName, currentChunk := range currentChunks.EventTriggers {- if previousChunk, exists := previousChunks.EventTriggers[eventTriggerName]; exists {- // Event trigger exists in both - check if modified by comparing text (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, eventTriggerName) {- continue- }- // Event trigger was modified - use DROP + CREATE pattern- // (PostgreSQL doesn't support CREATE OR REPLACE for event triggers)- diff.EventTriggerChanges = append(diff.EventTriggerChanges, &schema.EventTriggerDiff{- Action: schema.MetadataDiffActionDrop,- EventTriggerName: eventTriggerName,- OldEventTrigger: nil,- NewEventTrigger: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- diff.EventTriggerChanges = append(diff.EventTriggerChanges, &schema.EventTriggerDiff{- Action: schema.MetadataDiffActionCreate,- EventTriggerName: eventTriggerName,- OldEventTrigger: nil,- NewEventTrigger: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- }- // Note: Comment-only changes are handled by processCommentChanges- } else {- // Event trigger is new in current SDL- currentText := currentChunk.GetTextWithoutComments()- // Apply usability check- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, eventTriggerName) {- continue- }- diff.EventTriggerChanges = append(diff.EventTriggerChanges, &schema.EventTriggerDiff{- Action: schema.MetadataDiffActionCreate,- EventTriggerName: eventTriggerName,- OldEventTrigger: nil,- NewEventTrigger: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Handle comments for new event triggers- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeEventTrigger,- ObjectName: eventTriggerName,- NewComment: extractTextFromNode(commentNode),- NewASTNode: commentNode,- })- }- }- }- }-- // Process previous event triggers to find dropped ones- for eventTriggerName, previousChunk := range previousChunks.EventTriggers {- if _, exists := currentChunks.EventTriggers[eventTriggerName]; !exists {- // Event trigger was dropped- diff.EventTriggerChanges = append(diff.EventTriggerChanges, &schema.EventTriggerDiff{- Action: schema.MetadataDiffActionDrop,- EventTriggerName: eventTriggerName,- OldEventTrigger: nil,- NewEventTrigger: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- }- }-}diff --git a/backend/plugin/schema/pg/sdl_extension.go b/backend/plugin/schema/pg/sdl_extension.godeleted file mode 100644index f810e5c8bc..0000000000--- a/backend/plugin/schema/pg/sdl_extension.go+++ /dev/null@@ -1,97 +0,0 @@-package pg--import (- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processExtensionChanges processes extension changes between current and previous chunks-func processExtensionChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process current extensions to find created and modified ones- for extensionName, currentChunk := range currentChunks.Extensions {- if previousChunk, exists := previousChunks.Extensions[extensionName]; exists {- // Extension exists in both - check if modified by comparing text (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, extensionName) {- continue- }- // Extension was modified - use DROP + CREATE pattern- // (PostgreSQL doesn't support ALTER EXTENSION for most changes)- diff.ExtensionChanges = append(diff.ExtensionChanges, &schema.ExtensionDiff{- Action: schema.MetadataDiffActionDrop,- ExtensionName: extensionName,- OldExtension: nil,- NewExtension: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- diff.ExtensionChanges = append(diff.ExtensionChanges, &schema.ExtensionDiff{- Action: schema.MetadataDiffActionCreate,- ExtensionName: extensionName,- OldExtension: nil,- NewExtension: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON EXTENSION diffs if they exist in the new version- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeExtension,- ObjectName: extensionName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- // If text is identical, skip - no changes detected- } else {- // New extension- diff.ExtensionChanges = append(diff.ExtensionChanges, &schema.ExtensionDiff{- Action: schema.MetadataDiffActionCreate,- ExtensionName: extensionName,- OldExtension: nil,- NewExtension: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON EXTENSION diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeExtension,- ObjectName: extensionName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Process previous extensions to find dropped ones- for extensionName, previousChunk := range previousChunks.Extensions {- if _, exists := currentChunks.Extensions[extensionName]; !exists {- // Extension was dropped- diff.ExtensionChanges = append(diff.ExtensionChanges, &schema.ExtensionDiff{- Action: schema.MetadataDiffActionDrop,- ExtensionName: extensionName,- OldExtension: nil,- NewExtension: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- }- }-}diff --git a/backend/plugin/schema/pg/sdl_function.go b/backend/plugin/schema/pg/sdl_function.godeleted file mode 100644index f79d0e9694..0000000000--- a/backend/plugin/schema/pg/sdl_function.go+++ /dev/null@@ -1,81 +0,0 @@-package pg--import (- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processFunctionChanges analyzes function changes between current and previous chunks-// Following the text-first comparison pattern for performance optimization-func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process current functions to find created and modified functions- for _, currentChunk := range currentChunks.Functions {- if previousChunk, exists := previousChunks.Functions[currentChunk.Identifier]; exists {- // Function exists in both - check if modified by comparing text first (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check: skip diff if current chunk matches database metadata SDL- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }- // Function was modified - use CREATE OR REPLACE (AST-only mode)- schemaName, functionName := parseIdentifier(currentChunk.Identifier)- diff.FunctionChanges = append(diff.FunctionChanges, &schema.FunctionDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- FunctionName: functionName,- OldFunction: nil, // Will be populated when SDL drift detection is implemented- NewFunction: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: previousChunk.ASTNode,- NewASTNode: currentChunk.ASTNode,- })- }- // If text is identical, skip - no changes detected- } else {- // New function- schemaName, functionName := parseIdentifier(currentChunk.Identifier)- diff.FunctionChanges = append(diff.FunctionChanges, &schema.FunctionDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- FunctionName: functionName,- OldFunction: nil,- NewFunction: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON FUNCTION diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeFunction,- SchemaName: schemaName,- ObjectName: functionName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Process previous functions to find dropped ones- for identifier, previousChunk := range previousChunks.Functions {- if _, exists := currentChunks.Functions[identifier]; !exists {- // Function was dropped- schemaName, functionName := parseIdentifier(identifier)- diff.FunctionChanges = append(diff.FunctionChanges, &schema.FunctionDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- FunctionName: functionName,- OldFunction: nil, // Will be populated when SDL drift detection is implemented- NewFunction: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- }- }-}diff --git a/backend/plugin/schema/pg/sdl_index.go b/backend/plugin/schema/pg/sdl_index.godeleted file mode 100644index 2b769f3ac1..0000000000--- a/backend/plugin/schema/pg/sdl_index.go+++ /dev/null@@ -1,291 +0,0 @@-package pg--import (- "strings"-- parser "github.com/bytebase/parser/postgresql"-- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processStandaloneIndexChanges analyzes standalone CREATE INDEX statement changes-// and adds them to the appropriate table's or materialized view's IndexChanges-func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- if currentChunks == nil || previousChunks == nil {- return- }-- // Initialize map with all existing table diffs for efficient lookups- // Use schema.table format as key for consistency with extractTableNameFromIndex- affectedTables := make(map[string]*schema.TableDiff, len(diff.TableChanges))- for _, tableDiff := range diff.TableChanges {- qualifiedTableName := tableDiff.SchemaName + "." + tableDiff.TableName- affectedTables[qualifiedTableName] = tableDiff- }-- // Initialize map with all existing materialized view diffs- affectedMaterializedViews := make(map[string]*schema.MaterializedViewDiff, len(diff.MaterializedViewChanges))- for _, mvDiff := range diff.MaterializedViewChanges {- qualifiedMVName := mvDiff.SchemaName + "." + mvDiff.MaterializedViewName- affectedMaterializedViews[qualifiedMVName] = mvDiff- }-- // Step 1: Process current indexes to find created and modified indexes- for _, currentChunk := range currentChunks.Indexes {- targetObjectName := extractTableNameFromIndex(currentChunk.ASTNode)- if targetObjectName == "" {- continue // Skip if we can't determine the target object name- }-- // Determine if this index is on a table or materialized view- isOnMaterializedView := isIndexOnMaterializedView(targetObjectName, currentChunks, previousChunks)-- if previousChunk, exists := previousChunks.Indexes[currentChunk.Identifier]; exists {- // Index exists in both - check if modified by comparing text first- currentText := getStandaloneIndexText(currentChunk.ASTNode)- previousText := getStandaloneIndexText(previousChunk.ASTNode)- if currentText != previousText {- // Apply usability check: skip diff if current chunk matches database metadata- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }- // Index was modified - use drop and recreate pattern (PostgreSQL standard)- if isOnMaterializedView {- // Index is on materialized view- mvDiff := getOrCreateMaterializedViewDiff(diff, targetObjectName, affectedMaterializedViews)- mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: previousChunk.ASTNode,- })- mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: currentChunk.ASTNode,- })- } else {- // Index is on table- tableDiff := getOrCreateTableDiff(diff, targetObjectName, affectedTables)- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: previousChunk.ASTNode,- })- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: currentChunk.ASTNode,- })- }- // Add COMMENT ON INDEX diffs if they exist in the new version- if len(currentChunk.CommentStatements) > 0 {- schemaName, indexName := parseIdentifier(currentChunk.Identifier)- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeIndex,- SchemaName: schemaName,- ObjectName: indexName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- // If text is identical, skip - no changes detected- } else {- // New index - store AST node only- if isOnMaterializedView {- mvDiff := getOrCreateMaterializedViewDiff(diff, targetObjectName, affectedMaterializedViews)- mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: currentChunk.ASTNode,- })- } else {- tableDiff := getOrCreateTableDiff(diff, targetObjectName, affectedTables)- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: currentChunk.ASTNode,- })- }- // Add COMMENT ON INDEX diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- schemaName, indexName := parseIdentifier(currentChunk.Identifier)- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeIndex,- SchemaName: schemaName,- ObjectName: indexName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Step 2: Process previous indexes to find dropped ones- for indexName, previousChunk := range previousChunks.Indexes {- if _, exists := currentChunks.Indexes[indexName]; !exists {- // Index was dropped - store AST node only- targetObjectName := extractTableNameFromIndex(previousChunk.ASTNode)- if targetObjectName == "" {- continue // Skip if we can't determine the target object name- }-- // Determine if this index was on a table or materialized view- isOnMaterializedView := isIndexOnMaterializedView(targetObjectName, currentChunks, previousChunks)-- if isOnMaterializedView {- mvDiff := getOrCreateMaterializedViewDiff(diff, targetObjectName, affectedMaterializedViews)- mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: previousChunk.ASTNode,- })- } else {- tableDiff := getOrCreateTableDiff(diff, targetObjectName, affectedTables)- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: previousChunk.ASTNode,- })- }- }- }-}--// getStandaloneIndexText returns the text representation of a standalone CREATE INDEX statement for comparison-func getStandaloneIndexText(astNode any) string {- indexStmt, ok := astNode.(*parser.IndexstmtContext)- if !ok || indexStmt == nil {- return ""- }-- // Get tokens from the parser for precise text extraction with original spacing- if parser := indexStmt.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := indexStmt.GetStart()- stop := indexStmt.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return indexStmt.GetText()-}--// extractTableNameFromIndex extracts the fully qualified table name from a CREATE INDEX statement-// Returns schema.table format, defaulting to public schema if not specified-func extractTableNameFromIndex(astNode any) string {- indexStmt, ok := astNode.(*parser.IndexstmtContext)- if !ok || indexStmt == nil {- return ""- }-- // Extract table name from relation_expr in CREATE INDEX ... ON table_name- if relationExpr := indexStmt.Relation_expr(); relationExpr != nil {- if qualifiedName := relationExpr.Qualified_name(); qualifiedName != nil {- // Extract qualified name parts- qualifiedNameParts := pgparser.NormalizePostgreSQLQualifiedName(qualifiedName)- if len(qualifiedNameParts) == 0 {- return ""- }-- // Return fully qualified name (schema.table)- if len(qualifiedNameParts) == 1 {- // No schema specified, default to public- return "public." + qualifiedNameParts[0]- }- // Schema is specified- return strings.Join(qualifiedNameParts, ".")- }- }-- return ""-}--// getOrCreateTableDiff finds an existing table diff or creates a new one for the given table-// tableName should be in schema.table format-func getOrCreateTableDiff(diff *schema.MetadataDiff, tableName string, affectedTables map[string]*schema.TableDiff) *schema.TableDiff {- // Check if we already have this table in our map- if tableDiff, exists := affectedTables[tableName]; exists {- return tableDiff- }-- // Parse schema and table name from tableName (format: schema.table)- schemaName, tableNameOnly := parseIdentifier(tableName)-- // Create a new table diff for standalone index changes- // We set Action to ALTER since we're modifying an existing table by adding/removing indexes- newTableDiff := &schema.TableDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- TableName: tableNameOnly,- OldTable: nil, // Will be populated when SDL drift detection is implemented- NewTable: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: nil, // No table-level AST changes for standalone indexes- NewASTNode: nil, // No table-level AST changes for standalone indexes- ColumnChanges: []*schema.ColumnDiff{},- IndexChanges: []*schema.IndexDiff{},- PrimaryKeyChanges: []*schema.PrimaryKeyDiff{},- UniqueConstraintChanges: []*schema.UniqueConstraintDiff{},- ForeignKeyChanges: []*schema.ForeignKeyDiff{},- CheckConstraintChanges: []*schema.CheckConstraintDiff{},- ExcludeConstraintChanges: []*schema.ExcludeConstraintDiff{},- }-- diff.TableChanges = append(diff.TableChanges, newTableDiff)- affectedTables[tableName] = newTableDiff- return newTableDiff-}--// getOrCreateMaterializedViewDiff finds an existing materialized view diff or creates a new one-// mvName should be in schema.mv_name format-func getOrCreateMaterializedViewDiff(diff *schema.MetadataDiff, mvName string, affectedMaterializedViews map[string]*schema.MaterializedViewDiff) *schema.MaterializedViewDiff {- // Check if we already have this materialized view in our map- if mvDiff, exists := affectedMaterializedViews[mvName]; exists {- return mvDiff- }-- // Parse schema and MV name from mvName (format: schema.mv_name)- schemaName, mvNameOnly := parseIdentifier(mvName)-- // Create a new materialized view diff for standalone index changes- // We set Action to ALTER since we're modifying an existing MV by adding/removing indexes- newMVDiff := &schema.MaterializedViewDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- MaterializedViewName: mvNameOnly,- OldMaterializedView: nil,- NewMaterializedView: nil,- OldASTNode: nil,- NewASTNode: nil,- IndexChanges: []*schema.IndexDiff{},- }-- diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, newMVDiff)- affectedMaterializedViews[mvName] = newMVDiff- return newMVDiff-}--// isIndexOnMaterializedView checks if the given object name refers to a materialized view-// rather than a table, by checking if it exists in the materialized view chunks-func isIndexOnMaterializedView(objectName string, currentChunks, previousChunks *schema.SDLChunks) bool {- // Check if the object exists as a materialized view in current or previous chunks- if currentChunks != nil && currentChunks.MaterializedViews != nil {- if _, exists := currentChunks.MaterializedViews[objectName]; exists {- return true- }- }- if previousChunks != nil && previousChunks.MaterializedViews != nil {- if _, exists := previousChunks.MaterializedViews[objectName]; exists {- return true- }- }- return false-}diff --git a/backend/plugin/schema/pg/sdl_materialized_view.go b/backend/plugin/schema/pg/sdl_materialized_view.godeleted file mode 100644index 9d02e5c965..0000000000--- a/backend/plugin/schema/pg/sdl_materialized_view.go+++ /dev/null@@ -1,107 +0,0 @@-package pg--import (- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processMaterializedViewChanges analyzes materialized view changes between current and previous chunks-// Following the same pattern as processViewChanges-// Note: Materialized views use DROP + CREATE pattern (no ALTER support in PostgreSQL)-func processMaterializedViewChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process current materialized views to find created and modified materialized views- for _, currentChunk := range currentChunks.MaterializedViews {- if previousChunk, exists := previousChunks.MaterializedViews[currentChunk.Identifier]; exists {- // Materialized view exists in both - check if modified by comparing text first (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check: skip diff if current chunk matches database metadata SDL- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }- // Materialized view was modified - use drop and recreate pattern (PostgreSQL doesn't support ALTER MATERIALIZED VIEW definition)- schemaName, mvName := parseIdentifier(currentChunk.Identifier)- diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- MaterializedViewName: mvName,- OldMaterializedView: nil, // Will be populated when SDL drift detection is implemented- NewMaterializedView: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- MaterializedViewName: mvName,- OldMaterializedView: nil, // Will be populated when SDL drift detection is implemented- NewMaterializedView: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON MATERIALIZED VIEW diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeMaterializedView,- SchemaName: schemaName,- ObjectName: mvName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- // If text is identical, skip - no changes detected- } else {- // New materialized view- schemaName, mvName := parseIdentifier(currentChunk.Identifier)- diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- MaterializedViewName: mvName,- OldMaterializedView: nil,- NewMaterializedView: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON MATERIALIZED VIEW diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeMaterializedView,- SchemaName: schemaName,- ObjectName: mvName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Process previous materialized views to find dropped ones- for identifier, previousChunk := range previousChunks.MaterializedViews {- if _, exists := currentChunks.MaterializedViews[identifier]; !exists {- // Materialized view was dropped- schemaName, mvName := parseIdentifier(identifier)- diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- MaterializedViewName: mvName,- OldMaterializedView: nil, // Will be populated when SDL drift detection is implemented- NewMaterializedView: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- }- }-}diff --git a/backend/plugin/schema/pg/sdl_migration_omni.go b/backend/plugin/schema/pg/sdl_migration_omni.goindex c5fdad9dd8..c547fa2bf0 100644--- a/backend/plugin/schema/pg/sdl_migration_omni.go+++ b/backend/plugin/schema/pg/sdl_migration_omni.go@@ -25,7 +25,14 @@ func init() {// catalogFromMetadata builds an omni Catalog from database metadata.func catalogFromMetadata(meta *model.DatabaseMetadata) (*catalog.Catalog, error) {- ddl, err := convertDatabaseSchemaToSDL(meta)+ if meta == nil {+ return catalog.LoadSDL("")+ }+ proto := meta.GetProto()+ if proto == nil {+ return catalog.LoadSDL("")+ }+ ddl, err := getSDLFormat(proto)if err != nil {return nil, errors.Wrap(err, "failed to convert metadata to DDL")}@@ -52,9 +59,16 @@ func pgSchemaDiffMigration(oldSchema, newSchema *model.DatabaseMetadata) (string// buildSDLCatalogs builds the from/to catalogs for an SDL migration.func buildSDLCatalogs(userSDLText string, currentSchema *model.DatabaseMetadata) (*catalog.Catalog, *catalog.Catalog, error) {- fromDDL, err := convertDatabaseSchemaToSDL(currentSchema)- if err != nil {- return nil, nil, errors.Wrap(err, "failed to convert current schema to DDL")+ var fromDDL string+ if currentSchema != nil {+ proto := currentSchema.GetProto()+ if proto != nil {+ var ddlErr error+ fromDDL, ddlErr = getSDLFormat(proto)+ if ddlErr != nil {+ return nil, nil, errors.Wrap(ddlErr, "failed to convert current schema to DDL")+ }+ }}from, err := catalog.LoadSDL(fromDDL)diff --git a/backend/plugin/schema/pg/sdl_schema.go b/backend/plugin/schema/pg/sdl_schema.godeleted file mode 100644index 6fc73f5906..0000000000--- a/backend/plugin/schema/pg/sdl_schema.go+++ /dev/null@@ -1,144 +0,0 @@-package pg--import (- storepb "github.com/bytebase/bytebase/backend/generated-go/store"-- "github.com/bytebase/bytebase/backend/plugin/schema"- "github.com/bytebase/bytebase/backend/store/model"-)--// processSchemaChanges processes explicit CREATE SCHEMA statements in the SDL-func processSchemaChanges(currentChunks, previousChunks *schema.SDLChunks, currentMeta *model.DatabaseMetadata, diff *schema.MetadataDiff) {- // Build set of existing schemas in current database- existingSchemas := make(map[string]bool)- if currentMeta != nil {- for _, schemaName := range currentMeta.ListSchemaNames() {- existingSchemas[schemaName] = true- }- }-- // Process current schemas to find created schemas- for schemaName := range currentChunks.Schemas {- if _, existsInPrevious := previousChunks.Schemas[schemaName]; !existsInPrevious {- // Schema is new in current SDL- // Only add if it doesn't exist in current database- if !existingSchemas[schemaName] {- diff.SchemaChanges = append(diff.SchemaChanges, &schema.SchemaDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- NewSchema: &storepb.SchemaMetadata{- Name: schemaName,- },- })- }- }- // Note: We don't handle ALTER SCHEMA or schema modification here as it's rarely used- // If needed in the future, we can add that logic- }-- // Process previous schemas to find dropped schemas- for schemaName := range previousChunks.Schemas {- if _, existsInCurrent := currentChunks.Schemas[schemaName]; !existsInCurrent {- // Schema was removed from SDL- diff.SchemaChanges = append(diff.SchemaChanges, &schema.SchemaDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- OldSchema: &storepb.SchemaMetadata{- Name: schemaName,- },- })- }- }-}--// addImplicitSchemaCreation adds schema creation diffs for schemas that are referenced-// by new objects but don't exist in the current database and aren't explicitly created in the SDL.-// This handles the case where users write "CREATE TABLE new_schema.t(...)" without "CREATE SCHEMA new_schema".-func addImplicitSchemaCreation(diff *schema.MetadataDiff, currentMeta *model.DatabaseMetadata) {- if currentMeta == nil {- return- }-- // Collect all schemas that are referenced by objects being created- referencedSchemas := make(map[string]bool)-- // Check tables- for _, tableDiff := range diff.TableChanges {- if tableDiff.Action == schema.MetadataDiffActionCreate {- referencedSchemas[tableDiff.SchemaName] = true- }- }-- // Check views- for _, viewDiff := range diff.ViewChanges {- if viewDiff.Action == schema.MetadataDiffActionCreate {- referencedSchemas[viewDiff.SchemaName] = true- }- }-- // Check materialized views- for _, mvDiff := range diff.MaterializedViewChanges {- if mvDiff.Action == schema.MetadataDiffActionCreate {- referencedSchemas[mvDiff.SchemaName] = true- }- }-- // Check functions- for _, funcDiff := range diff.FunctionChanges {- if funcDiff.Action == schema.MetadataDiffActionCreate {- referencedSchemas[funcDiff.SchemaName] = true- }- }-- // Check procedures- for _, procDiff := range diff.ProcedureChanges {- if procDiff.Action == schema.MetadataDiffActionCreate {- referencedSchemas[procDiff.SchemaName] = true- }- }-- // Check sequences- for _, seqDiff := range diff.SequenceChanges {- if seqDiff.Action == schema.MetadataDiffActionCreate {- referencedSchemas[seqDiff.SchemaName] = true- }- }-- // Check enum types- for _, enumDiff := range diff.EnumTypeChanges {- if enumDiff.Action == schema.MetadataDiffActionCreate {- referencedSchemas[enumDiff.SchemaName] = true- }- }-- // Get existing schemas from current database- existingSchemas := make(map[string]bool)- for _, schemaName := range currentMeta.ListSchemaNames() {- existingSchemas[schemaName] = true- }-- // Check which schemas are already in SchemaChanges with CREATE action- schemasBeingCreated := make(map[string]bool)- for _, schemaDiff := range diff.SchemaChanges {- if schemaDiff.Action == schema.MetadataDiffActionCreate {- schemasBeingCreated[schemaDiff.SchemaName] = true- }- }-- // Add schema creation for schemas that:- // 1. Are referenced by new objects- // 2. Don't exist in current database- // 3. Aren't already being created- for schemaName := range referencedSchemas {- if !existingSchemas[schemaName] && !schemasBeingCreated[schemaName] {- // Add schema creation diff- diff.SchemaChanges = append(diff.SchemaChanges, &schema.SchemaDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- NewSchema: &storepb.SchemaMetadata{- Name: schemaName,- },- })- }- }-}diff --git a/backend/plugin/schema/pg/sdl_sequence.go b/backend/plugin/schema/pg/sdl_sequence.godeleted file mode 100644index 15029baad8..0000000000--- a/backend/plugin/schema/pg/sdl_sequence.go+++ /dev/null@@ -1,230 +0,0 @@-package pg--import (- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processSequenceChanges analyzes sequence changes between current and previous chunks-// Following the text-first comparison pattern for performance optimization-// Supports fine-grained diff: if only ALTER SEQUENCE OWNED BY changed, generate ALTER instead of DROP+CREATE-func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process current sequences to find created and modified sequences- for _, currentChunk := range currentChunks.Sequences {- if previousChunk, exists := previousChunks.Sequences[currentChunk.Identifier]; exists {- // Sequence exists in both - check if modified (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check: skip diff if current chunk matches database metadata SDL- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }-- // Fine-grained comparison: check if only ALTER statements changed- currentCreateText := extractTextFromNode(currentChunk.ASTNode)- previousCreateText := extractTextFromNode(previousChunk.ASTNode)- currentAlterTexts := extractAlterTexts(currentChunk.AlterStatements)- previousAlterTexts := extractAlterTexts(previousChunk.AlterStatements)-- createChanged := currentCreateText != previousCreateText- alterChanged := currentAlterTexts != previousAlterTexts-- schemaName, sequenceName := parseIdentifier(currentChunk.Identifier)-- if createChanged && alterChanged {- // Both CREATE and ALTER changed - use drop and recreate- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Also need to add ALTER if current has ALTER statements- if len(currentChunk.AlterStatements) > 0 {- for _, alterNode := range currentChunk.AlterStatements {- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: nil,- NewASTNode: alterNode,- })- }- }- // Add COMMENT ON SEQUENCE diffs if they exist in the new version- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeSequence,- SchemaName: schemaName,- ObjectName: sequenceName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- } else if createChanged {- // Only CREATE changed - use drop and recreate- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Preserve ALTER if it exists in current- if len(currentChunk.AlterStatements) > 0 {- for _, alterNode := range currentChunk.AlterStatements {- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: nil,- NewASTNode: alterNode,- })- }- }- // Add COMMENT ON SEQUENCE diffs if they exist in the new version- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeSequence,- SchemaName: schemaName,- ObjectName: sequenceName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- } else if alterChanged {- // Only ALTER changed - generate ALTER statements- // This handles ownership changes without recreating the sequence- if len(currentChunk.AlterStatements) > 0 {- // Adding or modifying ALTER statements- for _, alterNode := range currentChunk.AlterStatements {- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: nil,- NewASTNode: alterNode,- })- }- } else if len(previousChunk.AlterStatements) > 0 {- // Removing ALTER statements - use the previous ALTER node to represent the removal- // The migration generator should interpret this as removing the ownership- for _, alterNode := range previousChunk.AlterStatements {- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: alterNode,- NewASTNode: nil,- })- }- }- }- }- // If text is identical, skip - no changes detected- } else {- // New sequence- schemaName, sequenceName := parseIdentifier(currentChunk.Identifier)- // Add CREATE SEQUENCE diff- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add ALTER SEQUENCE OWNED BY diffs if they exist- if len(currentChunk.AlterStatements) > 0 {- for _, alterNode := range currentChunk.AlterStatements {- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil,- NewSequence: nil,- OldASTNode: nil,- NewASTNode: alterNode,- })- }- }- // Add COMMENT ON SEQUENCE diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeSequence,- SchemaName: schemaName,- ObjectName: sequenceName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Process previous sequences to find dropped ones- for identifier, previousChunk := range previousChunks.Sequences {- if _, exists := currentChunks.Sequences[identifier]; !exists {- // Sequence was dropped- schemaName, sequenceName := parseIdentifier(identifier)- diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- SequenceName: sequenceName,- OldSequence: nil, // Will be populated when SDL drift detection is implemented- NewSequence: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- }- }-}diff --git a/backend/plugin/schema/pg/sdl_table.go b/backend/plugin/schema/pg/sdl_table.godeleted file mode 100644index 62ef3ca52d..0000000000--- a/backend/plugin/schema/pg/sdl_table.go+++ /dev/null@@ -1,468 +0,0 @@-package pg--import (- parser "github.com/bytebase/parser/postgresql"- "github.com/pkg/errors"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/schema"- "github.com/bytebase/bytebase/backend/store/model"-)--func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currentSchema *model.DatabaseMetadata, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) error {- // Process current table chunks to find created and modified tables- for _, currentChunk := range currentChunks.Tables {- schemaName, tableName := parseIdentifier(currentChunk.Identifier)-- if previousChunk, exists := previousChunks.Tables[currentChunk.Identifier]; exists {- // Table exists in both - check if modified (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check: skip diff if current chunk matches database metadata SDL- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }- // Table was modified - process column changes- oldASTNode, ok := previousChunk.ASTNode.(*parser.CreatestmtContext)- if !ok {- return errors.Errorf("expected CreatestmtContext for previous table %s", previousChunk.Identifier)- }- newASTNode, ok := currentChunk.ASTNode.(*parser.CreatestmtContext)- if !ok {- return errors.Errorf("expected CreatestmtContext for current table %s", currentChunk.Identifier)- }-- columnChanges := processColumnChanges(oldASTNode, newASTNode, currentSchema, currentDBSDLChunks, currentChunk.Identifier)- foreignKeyChanges := processForeignKeyChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)- checkConstraintChanges := processCheckConstraintChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)- excludeConstraintChanges := processExcludeConstraintChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)- primaryKeyChanges := processPrimaryKeyChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)- uniqueConstraintChanges := processUniqueConstraintChanges(oldASTNode, newASTNode, currentDBSDLChunks, currentChunk.Identifier)-- tableDiff := &schema.TableDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- TableName: tableName,- OldTable: nil, // Will be populated when SDL drift detection is implemented- NewTable: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: oldASTNode,- NewASTNode: newASTNode,- ColumnChanges: columnChanges,- ForeignKeyChanges: foreignKeyChanges,- CheckConstraintChanges: checkConstraintChanges,- ExcludeConstraintChanges: excludeConstraintChanges,- PrimaryKeyChanges: primaryKeyChanges,- UniqueConstraintChanges: uniqueConstraintChanges,- }- diff.TableChanges = append(diff.TableChanges, tableDiff)- }- } else {- // New table- // Handle generated chunks (from drift detection) that don't have AST nodes- if currentChunk.ASTNode == nil {- // This is a generated chunk, create a simplified diff entry- tableDiff := &schema.TableDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- TableName: tableName,- OldTable: nil,- NewTable: nil,- OldASTNode: nil,- NewASTNode: nil, // No AST node for generated content- ColumnChanges: []*schema.ColumnDiff{}, // Empty - table creation automatically creates all columns- }- diff.TableChanges = append(diff.TableChanges, tableDiff)- } else {- newASTNode, ok := currentChunk.ASTNode.(*parser.CreatestmtContext)- if !ok {- return errors.Errorf("expected CreatestmtContext for new table %s", currentChunk.Identifier)- }-- tableDiff := &schema.TableDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- TableName: tableName,- OldTable: nil,- NewTable: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: nil,- NewASTNode: newASTNode,- ColumnChanges: []*schema.ColumnDiff{}, // Empty - table creation automatically creates all columns- }- diff.TableChanges = append(diff.TableChanges, tableDiff)- // Add COMMENT ON TABLE diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeTable,- SchemaName: schemaName,- ObjectName: tableName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- // Add COMMENT ON COLUMN diffs if they exist- tableIdentifier := currentChunk.Identifier- if columnComments := currentChunks.ColumnComments[tableIdentifier]; len(columnComments) > 0 {- for columnName, commentNode := range columnComments {- commentText := extractCommentTextFromNode(commentNode)- if commentText != "" {- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeColumn,- SchemaName: schemaName,- ObjectName: tableName,- ColumnName: columnName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }- }- }-- // Process previous table chunks to find dropped tables- for identifier, previousChunk := range previousChunks.Tables {- if _, exists := currentChunks.Tables[identifier]; !exists {- // Table was dropped- schemaName, tableName := parseIdentifier(previousChunk.Identifier)-- // Handle generated chunks (from drift detection) that don't have AST nodes- if previousChunk.ASTNode == nil {- // This is a generated chunk that was removed, skip it in the diff- // It means this table was added during drift detection but doesn't exist in current SDL- continue- }-- oldASTNode, ok := previousChunk.ASTNode.(*parser.CreatestmtContext)- if !ok {- return errors.Errorf("expected CreatestmtContext for dropped table %s", previousChunk.Identifier)- }-- tableDiff := &schema.TableDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- TableName: tableName,- OldTable: nil, // Will be populated when SDL drift detection is implemented- NewTable: nil,- OldASTNode: oldASTNode,- NewASTNode: nil,- ColumnChanges: []*schema.ColumnDiff{}, // Empty - table drop doesn't need column analysis- }- diff.TableChanges = append(diff.TableChanges, tableDiff)- }- }-- return nil-}--// processColumnChanges analyzes column changes between old and new table definitions-// Following the same pattern as processTableChanges: compare text first, then analyze differences-// If schemas are nil, operates in AST-only mode without metadata extraction-func processColumnChanges(oldTable, newTable *parser.CreatestmtContext, currentSchema *model.DatabaseMetadata, currentDBSDLChunks *currentDatabaseSDLChunks, tableIdentifier string) []*schema.ColumnDiff {- if oldTable == nil || newTable == nil {- return []*schema.ColumnDiff{}- }-- // Detect AST-only mode: when both schemas are nil, operate without metadata extraction- astOnlyMode := currentSchema == nil-- // Step 1: Extract all column definitions with their AST nodes for text comparison- oldColumns := extractColumnDefinitionsWithAST(oldTable)- newColumns := extractColumnDefinitionsWithAST(newTable)-- var columnDiffs []*schema.ColumnDiff-- // Step 2: Process current columns to find created and modified columns- // Use the order from the new table's AST to maintain original column order- for _, columnName := range newColumns.Order {- newColumnDef := newColumns.Map[columnName]- if oldColumnDef, exists := oldColumns.Map[columnName]; exists {- // Column exists in both - check if modified by comparing text first- currentText := getColumnText(newColumnDef.ASTNode)- previousText := getColumnText(oldColumnDef.ASTNode)- if currentText != previousText {- // Apply column-level usability check: skip diff if current column matches database metadata SDL- schemaName, tableName := parseIdentifier(tableIdentifier)- if currentDBSDLChunks != nil && currentDBSDLChunks.shouldSkipColumnDiffForUsability(currentText, schemaName, tableName, columnName) {- continue- }- // Column was modified - extract metadata only if not in AST-only mode- var oldColumn, newColumn *storepb.ColumnMetadata- if !astOnlyMode {- oldColumn = extractColumnMetadata(oldColumnDef.ASTNode)- newColumn = extractColumnMetadata(newColumnDef.ASTNode)- }-- columnDiffs = append(columnDiffs, &schema.ColumnDiff{- Action: schema.MetadataDiffActionAlter,- OldColumn: oldColumn,- NewColumn: newColumn,- OldASTNode: oldColumnDef.ASTNode,- NewASTNode: newColumnDef.ASTNode,- })- }- // If text is identical, skip - no changes detected- } else {- // New column - extract metadata only if not in AST-only mode- var newColumn *storepb.ColumnMetadata- if !astOnlyMode {- newColumn = extractColumnMetadata(newColumnDef.ASTNode)- }- columnDiffs = append(columnDiffs, &schema.ColumnDiff{- Action: schema.MetadataDiffActionCreate,- OldColumn: nil,- NewColumn: newColumn,- OldASTNode: nil,- NewASTNode: newColumnDef.ASTNode,- })- }- }-- // Step 3: Process previous columns to find dropped columns- // Use the order from the old table's AST to maintain original column order- for _, columnName := range oldColumns.Order {- oldColumnDef := oldColumns.Map[columnName]- if _, exists := newColumns.Map[columnName]; !exists {- // Column was dropped - extract metadata only if not in AST-only mode- var oldColumn *storepb.ColumnMetadata- if !astOnlyMode {- oldColumn = extractColumnMetadata(oldColumnDef.ASTNode)- }- columnDiffs = append(columnDiffs, &schema.ColumnDiff{- Action: schema.MetadataDiffActionDrop,- OldColumn: oldColumn,- NewColumn: nil,- OldASTNode: oldColumnDef.ASTNode,- NewASTNode: nil,- })- }- }-- return columnDiffs-}--// ColumnDefWithAST holds a column definition AST node with its name for efficient lookup-type ColumnDefWithAST struct {- Name string- ASTNode parser.IColumnDefContext-}--// ColumnDefWithASTOrdered holds column definitions in AST order for deterministic processing-type ColumnDefWithASTOrdered struct {- Map map[string]*ColumnDefWithAST- Order []string // Column names in the order they appear in the AST-}--// extractColumnDefinitionsWithAST extracts column definitions and preserves their AST order-// This ensures deterministic processing while maintaining the original column order-func extractColumnDefinitionsWithAST(createStmt *parser.CreatestmtContext) *ColumnDefWithASTOrdered {- result := &ColumnDefWithASTOrdered{- Map: make(map[string]*ColumnDefWithAST),- Order: []string{},- }-- if createStmt == nil {- return result- }-- // Get the optTableElementList which contains column definitions- if createStmt.Opttableelementlist() != nil && createStmt.Opttableelementlist().Tableelementlist() != nil {- elementList := createStmt.Opttableelementlist().Tableelementlist()-- for _, element := range elementList.AllTableelement() {- // Check if this is a columnDef (column definition)- if element.ColumnDef() != nil {- columnDef := element.ColumnDef()- if columnDef.Colid() != nil {- columnName := pgparser.NormalizePostgreSQLColid(columnDef.Colid())-- result.Map[columnName] = &ColumnDefWithAST{- Name: columnName,- ASTNode: columnDef,- }- // Preserve the order columns appear in the AST- result.Order = append(result.Order, columnName)- }- }- }- }-- return result-}--// extractColumnMetadata extracts full column metadata from a single column AST node-// This is called only when we actually need the detailed information-func extractColumnMetadata(columnDef parser.IColumnDefContext) *storepb.ColumnMetadata {- if columnDef == nil {- return nil- }-- columnName := pgparser.NormalizePostgreSQLColid(columnDef.Colid())-- return &storepb.ColumnMetadata{- Name: columnName,- Type: extractColumnType(columnDef.Typename()),- Nullable: extractColumnNullable(columnDef),- Default: extractColumnDefault(columnDef),- Comment: extractColumnComment(columnDef),- Collation: extractColumnCollation(columnDef),- }-}--// createTableExtractor extracts CREATE TABLE AST node from parse tree-type createTableExtractor struct {- *parser.BasePostgreSQLParserListener- result **parser.CreatestmtContext-}--func (e *createTableExtractor) EnterCreatestmt(ctx *parser.CreatestmtContext) {- *e.result = ctx-}--// getColumnText extracts the text representation of a column definition-func getColumnText(columnAST parser.IColumnDefContext) string {- if columnAST == nil {- return ""- }-- // Get tokens from the parser- if parser := columnAST.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := columnAST.GetStart()- stop := columnAST.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return columnAST.GetText()-}--// extractColumnType extracts column type information from the typename AST node-func extractColumnType(typename parser.ITypenameContext) string {- if typename == nil {- return "unknown"- }-- // Get tokens from the parser- if parser := typename.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := typename.GetStart()- stop := typename.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return typename.GetText()-}--// extractColumnNullable extracts whether a column is nullable from column constraints-func extractColumnNullable(columnDef parser.IColumnDefContext) bool {- if columnDef == nil || columnDef.Colquallist() == nil {- return true // Default is nullable- }-- // Check all column constraints- for _, constraint := range columnDef.Colquallist().AllColconstraint() {- if constraint.Colconstraintelem() != nil {- elem := constraint.Colconstraintelem()- if elem.NOT() != nil && elem.NULL_P() != nil {- return false // NOT NULL- }- if elem.PRIMARY() != nil && elem.KEY() != nil {- return false // PRIMARY KEY implies NOT NULL- }- }- }-- return true // Default is nullable-}--// extractColumnDefault extracts the default value from column constraints-func extractColumnDefault(columnDef parser.IColumnDefContext) string {- if columnDef == nil || columnDef.Colquallist() == nil {- return ""- }-- // Check all column constraints for DEFAULT- for _, constraint := range columnDef.Colquallist().AllColconstraint() {- if constraint.Colconstraintelem() != nil {- elem := constraint.Colconstraintelem()- if elem.DEFAULT() != nil && elem.B_expr() != nil {- // Extract default value text- if parser := elem.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := elem.B_expr().GetStart()- stop := elem.B_expr().GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }- }- }- }-- return ""-}--// extractColumnComment extracts column comment (PostgreSQL doesn't store comments in CREATE TABLE syntax)-func extractColumnComment(_ parser.IColumnDefContext) string {- // PostgreSQL column comments are set using COMMENT ON COLUMN, not in CREATE TABLE- // This information is not available in the CREATE TABLE AST- return ""-}--// extractColumnCollation extracts collation from column constraints-func extractColumnCollation(columnDef parser.IColumnDefContext) string {- if columnDef == nil || columnDef.Colquallist() == nil {- return ""- }-- // Check all column constraints for COLLATE- for _, constraint := range columnDef.Colquallist().AllColconstraint() {- if constraint.COLLATE() != nil && constraint.Any_name() != nil {- // Extract collation name- return extractAnyName(constraint.Any_name())- }- }-- return ""-}--// extractAnyName extracts the string representation from an Any_name context-func extractAnyName(anyName parser.IAny_nameContext) string {- if anyName == nil {- return ""- }-- // Get tokens from the parser- if parser := anyName.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := anyName.GetStart()- stop := anyName.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- // Fallback to GetText() if tokens are not available- return anyName.GetText()-}diff --git a/backend/plugin/schema/pg/sdl_view.go b/backend/plugin/schema/pg/sdl_view.godeleted file mode 100644index fe782f3773..0000000000--- a/backend/plugin/schema/pg/sdl_view.go+++ /dev/null@@ -1,104 +0,0 @@-package pg--import (- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- // Process current views to find created and modified views- for _, currentChunk := range currentChunks.Views {- if previousChunk, exists := previousChunks.Views[currentChunk.Identifier]; exists {- // View exists in both - check if modified by comparing text first (excluding comments)- currentText := currentChunk.GetTextWithoutComments()- previousText := previousChunk.GetTextWithoutComments()- if currentText != previousText {- // Apply usability check: skip diff if current chunk matches database metadata SDL- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }- // View was modified - use drop and recreate pattern (PostgreSQL standard)- schemaName, viewName := parseIdentifier(currentChunk.Identifier)- diff.ViewChanges = append(diff.ViewChanges, &schema.ViewDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- ViewName: viewName,- OldView: nil, // Will be populated when SDL drift detection is implemented- NewView: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- diff.ViewChanges = append(diff.ViewChanges, &schema.ViewDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- ViewName: viewName,- OldView: nil, // Will be populated when SDL drift detection is implemented- NewView: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON VIEW diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeView,- SchemaName: schemaName,- ObjectName: viewName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- // If text is identical, skip - no changes detected- } else {- // New view- schemaName, viewName := parseIdentifier(currentChunk.Identifier)- diff.ViewChanges = append(diff.ViewChanges, &schema.ViewDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- ViewName: viewName,- OldView: nil,- NewView: nil, // Will be populated when SDL drift detection is implemented- OldASTNode: nil,- NewASTNode: currentChunk.ASTNode,- })- // Add COMMENT ON VIEW diffs if they exist- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeView,- SchemaName: schemaName,- ObjectName: viewName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Process previous views to find dropped ones- for identifier, previousChunk := range previousChunks.Views {- if _, exists := currentChunks.Views[identifier]; !exists {- // View was dropped- schemaName, viewName := parseIdentifier(identifier)- diff.ViewChanges = append(diff.ViewChanges, &schema.ViewDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- ViewName: viewName,- OldView: nil, // Will be populated when SDL drift detection is implemented- NewView: nil,- OldASTNode: previousChunk.ASTNode,- NewASTNode: nil,- })- }- }-}diff --git a/backend/plugin/schema/pg/sequence_migration_integration_test.go b/backend/plugin/schema/pg/sequence_migration_integration_test.godeleted file mode 100644index f7df426841..0000000000--- a/backend/plugin/schema/pg/sequence_migration_integration_test.go+++ /dev/null@@ -1,395 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestSequenceSDLDiffAndMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Create new sequence",- previousSDL: ``,- currentSDL: `- CREATE SEQUENCE my_sequence- START WITH 1- INCREMENT BY 1- MINVALUE 1- MAXVALUE 9223372036854775807- CACHE 1;- `,- expectedMigration: `CREATE SEQUENCE my_sequence- START WITH 1- INCREMENT BY 1- MINVALUE 1- MAXVALUE 9223372036854775807- CACHE 1;--`,- },- {- name: "Drop sequence",- previousSDL: `- CREATE SEQUENCE user_id_seq- START WITH 1- INCREMENT BY 1- MINVALUE 1- MAXVALUE 2147483647- CACHE 1;- `,- currentSDL: ``,- expectedMigration: `DROP SEQUENCE IF EXISTS "public"."user_id_seq";-`,- },- {- name: "Modify sequence (drop and recreate)",- previousSDL: `- CREATE SEQUENCE order_seq- START WITH 1- INCREMENT BY 1- CACHE 1;- `,- currentSDL: `- CREATE SEQUENCE order_seq- START WITH 100- INCREMENT BY 5- CACHE 10;- `,- expectedMigration: `DROP SEQUENCE IF EXISTS "public"."order_seq";--CREATE SEQUENCE order_seq- START WITH 100- INCREMENT BY 5- CACHE 10;--`,- },- {- name: "Create sequence with data type",- previousSDL: ``,- currentSDL: `- CREATE SEQUENCE bigint_sequence AS bigint- START WITH 1- INCREMENT BY 1;- `,- expectedMigration: `CREATE SEQUENCE bigint_sequence AS bigint- START WITH 1- INCREMENT BY 1;--`,- },- {- name: "Create schema-qualified sequence",- previousSDL: ``,- currentSDL: `- CREATE SEQUENCE test_schema.seq_test- START WITH 10- INCREMENT BY 2;- `,- expectedMigration: `CREATE SEQUENCE test_schema.seq_test- START WITH 10- INCREMENT BY 2;--`,- },- {- name: "Multiple sequences with different operations",- previousSDL: `- CREATE SEQUENCE seq_a START WITH 1;- CREATE SEQUENCE seq_b START WITH 1;- `,- currentSDL: `- CREATE SEQUENCE seq_a START WITH 1;- CREATE SEQUENCE seq_c START WITH 1;- `,- expectedMigration: `DROP SEQUENCE IF EXISTS "public"."seq_b";--CREATE SEQUENCE seq_c START WITH 1;--`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Step 1: Get SDL diff using AST-only mode (no metadata extraction)- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Step 2: Verify that diff contains sequence changes with AST nodes- if len(diff.SequenceChanges) > 0 {- for _, seqDiff := range diff.SequenceChanges {- switch seqDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, seqDiff.NewASTNode,- "Create action should have NewASTNode")- assert.Nil(t, seqDiff.OldASTNode,- "Create action should not have OldASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, seqDiff.NewSequence,- "AST-only mode should not have metadata")- case schema.MetadataDiffActionDrop:- assert.NotNil(t, seqDiff.OldASTNode,- "Drop action should have OldASTNode")- assert.Nil(t, seqDiff.NewASTNode,- "Drop action should not have NewASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, seqDiff.OldSequence,- "AST-only mode should not have metadata")- default:- // Other actions- t.Logf("Encountered sequence action: %v", seqDiff.Action)- }- }- }-- // Step 3: Generate migration SQL using AST nodes- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // Step 4: Verify the generated migration matches expectations- assert.Equal(t, tt.expectedMigration, migrationSQL,- "Generated migration SQL should match expected output")-- // Step 5: Verify the migration contains expected keywords for sequences- if tt.expectedMigration != "" {- if containsSequenceString(tt.expectedMigration, "CREATE SEQUENCE") {- assert.Contains(t, migrationSQL, "CREATE SEQUENCE",- "Migration should contain CREATE SEQUENCE statement")- }- if containsSequenceString(tt.expectedMigration, "DROP SEQUENCE") {- assert.Contains(t, migrationSQL, "DROP SEQUENCE",- "Migration should contain DROP SEQUENCE statement")- }- }- })- }-}--func TestSequenceMigrationASTOnlyModeValidation(t *testing.T) {- // Test that ensures AST-only mode works correctly without any metadata extraction- previousSDL := ``- currentSDL := `- CREATE SEQUENCE test_sequence- START WITH 100- INCREMENT BY 2- CACHE 5;- `-- // Get diff without metadata extraction- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly one sequence change- assert.Len(t, diff.SequenceChanges, 1)- seqDiff := diff.SequenceChanges[0]-- // Verify AST-only mode properties- assert.Equal(t, schema.MetadataDiffActionCreate, seqDiff.Action)- assert.Equal(t, "public", seqDiff.SchemaName)- assert.Equal(t, "test_sequence", seqDiff.SequenceName)-- // Critical assertion: No metadata should be present (AST-only mode)- assert.Nil(t, seqDiff.NewSequence, "AST-only mode should not extract metadata")- assert.Nil(t, seqDiff.OldSequence, "AST-only mode should not extract metadata")-- // But AST nodes should be present- assert.NotNil(t, seqDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, seqDiff.OldASTNode, "No old AST node for CREATE action")-- // Generate migration should work with AST nodes only- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migrationSQL, "CREATE SEQUENCE test_sequence")- assert.Contains(t, migrationSQL, "START WITH 100")- assert.Contains(t, migrationSQL, "INCREMENT BY 2")- assert.Contains(t, migrationSQL, "CACHE 5")-}--func TestMultipleSequencesHandling(t *testing.T) {- // Test that verifies multiple sequences are handled correctly- previousSDL := `- CREATE SEQUENCE seq1 START WITH 1;- `- currentSDL := `- CREATE SEQUENCE seq1 START WITH 1;- CREATE SEQUENCE seq2 START WITH 10 INCREMENT BY 2;- CREATE SEQUENCE seq3 AS bigint START WITH 1000;- `-- // Get diff without metadata extraction (AST-only mode)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly two sequence changes (CREATE actions for seq2 and seq3)- assert.Len(t, diff.SequenceChanges, 2)-- // Verify both sequences are CREATE actions- createCount := 0- sequenceNames := make(map[string]bool)- for _, seqDiff := range diff.SequenceChanges {- if seqDiff.Action == schema.MetadataDiffActionCreate {- createCount++- // Verify AST-only mode properties- assert.NotNil(t, seqDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, seqDiff.OldASTNode, "No old AST node for CREATE action")- assert.Nil(t, seqDiff.NewSequence, "AST-only mode should not extract metadata")-- // Collect sequence names to verify they're different- sequenceNames[seqDiff.SequenceName] = true- }- }- assert.Equal(t, 2, createCount, "Should have two CREATE actions")-- // Verify the sequence names are correct- expectedSequences := map[string]bool{- "seq2": true,- "seq3": true,- }- assert.Equal(t, expectedSequences, sequenceNames, "Should have correct sequence names")-- // Generate migration SQL - this should work with both sequences- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- // Verify the migration contains both sequences- assert.Contains(t, migrationSQL, "CREATE SEQUENCE seq2")- assert.Contains(t, migrationSQL, "CREATE SEQUENCE seq3")-- // Verify both sequences are created- sequenceCount := strings.Count(migrationSQL, "CREATE SEQUENCE")- assert.Equal(t, 2, sequenceCount, "Should create both sequences")-}--func TestSequenceOwnershipMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- notExpected string- }{- {- name: "Add ALTER SEQUENCE OWNED BY to existing sequence",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 MINVALUE 100 MAXVALUE 9223372036854775807 NO CYCLE CACHE 10;- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 MINVALUE 100 MAXVALUE 9223372036854775807 NO CYCLE CACHE 10;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- expectedMigration: "ALTER SEQUENCE custom_seq OWNED BY orders.order_number;",- notExpected: "DROP SEQUENCE",- },- {- name: "Modify ALTER SEQUENCE OWNED BY (change owner)",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );- CREATE TABLE products (- id BIGINT PRIMARY KEY,- product_code BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );- CREATE TABLE products (- id BIGINT PRIMARY KEY,- product_code BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY products.product_code;- `,- expectedMigration: "ALTER SEQUENCE custom_seq OWNED BY products.product_code;",- notExpected: "DROP SEQUENCE",- },- {- name: "Remove ALTER SEQUENCE OWNED BY",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;- `,- expectedMigration: `ALTER SEQUENCE "public"."custom_seq" OWNED BY NONE;`,- notExpected: "DROP SEQUENCE",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Get SDL diff- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- // Verify expected ALTER SEQUENCE is present- assert.Contains(t, migrationSQL, tt.expectedMigration,- "Migration should contain expected ALTER SEQUENCE statement")-- // Verify DROP SEQUENCE is not present (we're using ALTER, not DROP+CREATE)- if tt.notExpected != "" {- assert.NotContains(t, migrationSQL, tt.notExpected,- "Migration should not contain DROP SEQUENCE")- }- })- }-}--// Helper function to check if a string contains a substring (sequence tests)-func containsSequenceString(s, substr string) bool {- return strings.Contains(s, substr)-}diff --git a/backend/plugin/schema/pg/sequence_sdl_diff_test.go b/backend/plugin/schema/pg/sequence_sdl_diff_test.godeleted file mode 100644index ed3637a5de..0000000000--- a/backend/plugin/schema/pg/sequence_sdl_diff_test.go+++ /dev/null@@ -1,417 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestSequenceSDLDiff(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedSequenceChanges int- expectedActions []schema.MetadataDiffAction- }{- {- name: "Create new sequence",- previousSDL: ``,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 1- INCREMENT BY 1- NO MINVALUE- NO MAXVALUE- CACHE 1;- `,- expectedSequenceChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Drop sequence",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 1- INCREMENT BY 1- NO MINVALUE- NO MAXVALUE- CACHE 1;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- expectedSequenceChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},- },- {- name: "Modify sequence (drop and recreate)",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 1- INCREMENT BY 1- NO MINVALUE- NO MAXVALUE- CACHE 1;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 10- INCREMENT BY 2- NO MINVALUE- NO MAXVALUE- CACHE 5;- `,- expectedSequenceChanges: 2, // Drop + Create- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop, schema.MetadataDiffActionCreate},- },- {- name: "No changes to sequence",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 1- INCREMENT BY 1- NO MINVALUE- NO MAXVALUE- CACHE 1;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 1- INCREMENT BY 1- NO MINVALUE- NO MAXVALUE- CACHE 1;- `,- expectedSequenceChanges: 0,- expectedActions: []schema.MetadataDiffAction{},- },- {- name: "Multiple sequences with different changes",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 1- INCREMENT BY 1;-- CREATE SEQUENCE order_seq- START WITH 100- INCREMENT BY 1;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE SEQUENCE user_seq- START WITH 1- INCREMENT BY 1;-- CREATE SEQUENCE product_seq- START WITH 1000- INCREMENT BY 10;- `,- expectedSequenceChanges: 2, // Drop order_seq + Create product_seq- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate, schema.MetadataDiffActionDrop},- },- {- name: "Schema-qualified sequence names",- previousSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );- `,- currentSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE SEQUENCE test_schema.product_id_seq- START WITH 1- INCREMENT BY 1- OWNED BY test_schema.products.id;- `,- expectedSequenceChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Sequence with complex options",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- amount DECIMAL(10,2)- );- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- amount DECIMAL(10,2)- );-- CREATE SEQUENCE order_id_seq- AS BIGINT- START WITH 1000000- INCREMENT BY 1- MINVALUE 1- MAXVALUE 9223372036854775807- CACHE 50- NO CYCLE- OWNED BY orders.id;- `,- expectedSequenceChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "SERIAL column changes (implicit sequence handling)",- previousSDL: `- CREATE TABLE users (- name VARCHAR(255) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- expectedSequenceChanges: 0, // SERIAL sequences are handled at table level, not explicit sequence level- expectedActions: []schema.MetadataDiffAction{},- },- {- name: "Add ALTER SEQUENCE OWNED BY to existing sequence",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 MINVALUE 100 MAXVALUE 9223372036854775807 NO CYCLE CACHE 10;- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 MINVALUE 100 MAXVALUE 9223372036854775807 NO CYCLE CACHE 10;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- expectedSequenceChanges: 1, // Only ALTER (ownership changed)- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionAlter},- },- {- name: "Modify ALTER SEQUENCE OWNED BY (change owner)",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );- CREATE TABLE products (- id BIGINT PRIMARY KEY,- product_code BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );- CREATE TABLE products (- id BIGINT PRIMARY KEY,- product_code BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY products.product_code;- `,- expectedSequenceChanges: 1, // Only ALTER (ownership changed)- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionAlter},- },- {- name: "Remove ALTER SEQUENCE OWNED BY",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;- `,- expectedSequenceChanges: 1, // Only ALTER (ownership removed)- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionAlter},- },- {- name: "No change when ALTER SEQUENCE OWNED BY is identical",- previousSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- currentSDL: `- CREATE TABLE orders (- id BIGINT PRIMARY KEY,- order_number BIGINT NOT NULL- );-- CREATE SEQUENCE custom_seq AS bigint START WITH 100 INCREMENT BY 5 NO CYCLE;-- ALTER SEQUENCE custom_seq OWNED BY orders.order_number;- `,- expectedSequenceChanges: 0, // No changes- expectedActions: []schema.MetadataDiffAction{},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- assert.Equal(t, tt.expectedSequenceChanges, len(diff.SequenceChanges),- "Expected %d sequence changes, got %d", tt.expectedSequenceChanges, len(diff.SequenceChanges))-- // Check that the actions match expectations- var actualActions []schema.MetadataDiffAction- for _, seqDiff := range diff.SequenceChanges {- actualActions = append(actualActions, seqDiff.Action)- }-- // Handle nil vs empty slice comparison- if len(tt.expectedActions) == 0 && len(actualActions) == 0 {- // Both are effectively empty - test passes- t.Log("Both expected and actual actions are empty")- } else {- assert.ElementsMatch(t, tt.expectedActions, actualActions,- "Expected actions %v, got %v", tt.expectedActions, actualActions)- }-- // Verify AST nodes are properly set- for i, seqDiff := range diff.SequenceChanges {- switch seqDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, seqDiff.NewASTNode,- "Sequence diff %d should have NewASTNode for CREATE action", i)- assert.Nil(t, seqDiff.OldASTNode,- "Sequence diff %d should not have OldASTNode for CREATE action", i)- case schema.MetadataDiffActionDrop:- assert.NotNil(t, seqDiff.OldASTNode,- "Sequence diff %d should have OldASTNode for DROP action", i)- assert.Nil(t, seqDiff.NewASTNode,- "Sequence diff %d should not have NewASTNode for DROP action", i)- case schema.MetadataDiffActionAlter:- // ALTER action can have either NewASTNode (adding/modifying) or OldASTNode (removing)- // but not both and not neither- hasNew := seqDiff.NewASTNode != nil- hasOld := seqDiff.OldASTNode != nil- assert.True(t, hasNew != hasOld,- "Sequence diff %d should have exactly one of NewASTNode or OldASTNode for ALTER action", i)- default:- t.Errorf("Unexpected action %v for sequence diff %d", seqDiff.Action, i)- }- }- })- }-}--func TestSequenceIdentifierParsing(t *testing.T) {- tests := []struct {- name string- identifier string- expectedSchema string- expectedSequence string- }{- {- name: "Simple sequence name",- identifier: "my_seq",- expectedSchema: "public",- expectedSequence: "my_seq",- },- {- name: "Schema-qualified sequence name",- identifier: "test_schema.my_seq",- expectedSchema: "test_schema",- expectedSequence: "my_seq",- },- {- name: "Public schema explicit",- identifier: "public.user_id_seq",- expectedSchema: "public",- expectedSequence: "user_id_seq",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- schema, sequence := parseIdentifier(tt.identifier)- assert.Equal(t, tt.expectedSchema, schema)- assert.Equal(t, tt.expectedSequence, sequence)- })- }-}diff --git a/backend/plugin/schema/pg/standalone_index_test.go b/backend/plugin/schema/pg/standalone_index_test.godeleted file mode 100644index e2c2d84649..0000000000--- a/backend/plugin/schema/pg/standalone_index_test.go+++ /dev/null@@ -1,716 +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 TestStandaloneCreateIndexSupport(t *testing.T) {- tests := []struct {- name string- previousUserSDL string- currentSDL string- expectedIndexDiffs int- expectedActions []schema.MetadataDiffAction- }{- {- name: "Create new index",- previousUserSDL: ``,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) UNIQUE- );-- CREATE INDEX idx_users_email ON users(email);- `,- expectedIndexDiffs: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Drop index",- previousUserSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) UNIQUE- );-- CREATE INDEX idx_users_email ON users(email);- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) UNIQUE- );- `,- expectedIndexDiffs: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},- },- {- name: "Modify index (drop and recreate)",- previousUserSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) UNIQUE,- name VARCHAR(100)- );-- CREATE INDEX idx_users_email ON users(email);- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) UNIQUE,- name VARCHAR(100)- );-- CREATE INDEX idx_users_email ON users(email, name);- `,- expectedIndexDiffs: 2, // Drop + Create- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop, schema.MetadataDiffActionCreate},- },- {- name: "No changes to index",- previousUserSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) UNIQUE- );-- CREATE INDEX idx_users_email ON users(email);- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) UNIQUE- );-- CREATE INDEX idx_users_email ON users(email);- `,- expectedIndexDiffs: 0,- expectedActions: []schema.MetadataDiffAction{},- },- {- name: "Complex index with WHERE clause",- previousUserSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- status VARCHAR(50),- customer_id INTEGER- );- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- status VARCHAR(50),- customer_id INTEGER- );-- CREATE INDEX idx_orders_active ON orders(customer_id) WHERE status = 'active';- `,- expectedIndexDiffs: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousUserSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Count total index diffs across all table changes- var allIndexDiffs []*schema.IndexDiff- for _, tableChange := range diff.TableChanges {- allIndexDiffs = append(allIndexDiffs, tableChange.IndexChanges...)- }-- assert.Equal(t, tt.expectedIndexDiffs, len(allIndexDiffs), "Expected %d index diffs, got %d", tt.expectedIndexDiffs, len(allIndexDiffs))-- // Check that the actions match expectations- var actualActions []schema.MetadataDiffAction- for _, indexDiff := range allIndexDiffs {- actualActions = append(actualActions, indexDiff.Action)- }-- // Handle nil vs empty slice comparison- if len(tt.expectedActions) == 0 && len(actualActions) == 0 {- // Both are effectively empty - test passes- t.Log("Both expected and actual actions are empty")- } else {- assert.Equal(t, tt.expectedActions, actualActions, "Expected actions %v, got %v", tt.expectedActions, actualActions)- }-- // Verify AST nodes are properly set- for i, indexDiff := range allIndexDiffs {- switch indexDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, indexDiff.NewASTNode, "Index diff %d should have NewASTNode for CREATE action", i)- assert.Nil(t, indexDiff.OldASTNode, "Index diff %d should not have OldASTNode for CREATE action", i)- case schema.MetadataDiffActionDrop:- assert.NotNil(t, indexDiff.OldASTNode, "Index diff %d should have OldASTNode for DROP action", i)- assert.Nil(t, indexDiff.NewASTNode, "Index diff %d should not have NewASTNode for DROP action", i)- default:- t.Errorf("Unexpected action %v for index diff %d", indexDiff.Action, i)- }- }- })- }-}--func TestGetStandaloneIndexText(t *testing.T) {- // This test validates that the text extraction works correctly- sdlText := `CREATE INDEX idx_users_email ON users(email);`-- chunks, err := ChunkSDLText(sdlText)- require.NoError(t, err)- require.NotNil(t, chunks)-- // Should have exactly one index chunk- assert.Equal(t, 1, len(chunks.Indexes))-- // Get the index chunk- var indexChunk *schema.SDLChunk- for _, chunk := range chunks.Indexes {- indexChunk = chunk- break- }- require.NotNil(t, indexChunk)-- // Test text extraction- text := getStandaloneIndexText(indexChunk.ASTNode)- assert.Contains(t, text, "CREATE INDEX")- assert.Contains(t, text, "idx_users_email")- assert.Contains(t, text, "users(email)")-}--// TestStandaloneIndexIntegrationWithTableChanges tests integration with table changes-func TestStandaloneIndexIntegrationWithTableChanges(t *testing.T) {- tests := []struct {- name string- previousUserSDL string- currentSDL string- expectedTableChanges int- expectedTableName string- expectedTableAction schema.MetadataDiffAction- expectedIndexChanges int- expectedIndexActions []schema.MetadataDiffAction- }{- {- name: "Standalone index on existing table (no table changes)",- previousUserSDL: `CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255));`,- currentSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255));- CREATE INDEX idx_users_email ON users(email);- `,- expectedTableChanges: 1, // One table affected by index change- expectedTableName: "users",- expectedTableAction: schema.MetadataDiffActionAlter,- expectedIndexChanges: 1,- expectedIndexActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Table change and index change combined",- previousUserSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255));- CREATE INDEX idx_users_email ON users(email);- `,- currentSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255), name VARCHAR(100));- CREATE INDEX idx_users_email_name ON users(email, name);- `,- expectedTableChanges: 1, // Same table with both column and index changes- expectedTableName: "users",- expectedTableAction: schema.MetadataDiffActionAlter,- expectedIndexChanges: 2, // Create new index + drop old index (different names)- expectedIndexActions: []schema.MetadataDiffAction{- schema.MetadataDiffActionCreate, // New index idx_users_email_name- schema.MetadataDiffActionDrop, // Old index idx_users_email- },- },- {- name: "Multiple tables with index changes",- previousUserSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255));- CREATE TABLE orders (id SERIAL PRIMARY KEY, user_id INTEGER);- CREATE INDEX idx_users_email ON users(email);- `,- currentSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255));- CREATE TABLE orders (id SERIAL PRIMARY KEY, user_id INTEGER);- CREATE INDEX idx_users_email ON users(email);- CREATE INDEX idx_orders_user_id ON orders(user_id);- `,- expectedTableChanges: 1, // Only orders table affected by new index- expectedTableName: "orders",- expectedTableAction: schema.MetadataDiffActionAlter,- expectedIndexChanges: 1,- expectedIndexActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Index on non-primary schema table", // Testing schema extraction- previousUserSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (id SERIAL PRIMARY KEY, name VARCHAR(255));- `,- currentSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (id SERIAL PRIMARY KEY, name VARCHAR(255));- CREATE INDEX idx_products_name ON test_schema.products(name);- `,- expectedTableChanges: 1,- expectedTableName: "products", // Table name without schema prefix- expectedTableAction: schema.MetadataDiffActionAlter,- expectedIndexChanges: 1,- expectedIndexActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousUserSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- assert.Equal(t, tt.expectedTableChanges, len(diff.TableChanges),- "Expected %d table changes, got %d", tt.expectedTableChanges, len(diff.TableChanges))-- if tt.expectedTableChanges > 0 {- // Find the table change we're interested in- var targetTableChange *schema.TableDiff- for _, tableChange := range diff.TableChanges {- if tableChange.TableName == tt.expectedTableName {- targetTableChange = tableChange- break- }- }-- require.NotNil(t, targetTableChange, "Expected to find table change for %s", tt.expectedTableName)- assert.Equal(t, tt.expectedTableAction, targetTableChange.Action,- "Expected table action %v, got %v", tt.expectedTableAction, targetTableChange.Action)-- // Check index changes within this table- assert.Equal(t, tt.expectedIndexChanges, len(targetTableChange.IndexChanges),- "Expected %d index changes in table %s, got %d",- tt.expectedIndexChanges, tt.expectedTableName, len(targetTableChange.IndexChanges))-- // Verify index actions- var actualIndexActions []schema.MetadataDiffAction- for _, indexChange := range targetTableChange.IndexChanges {- actualIndexActions = append(actualIndexActions, indexChange.Action)- }- assert.Equal(t, tt.expectedIndexActions, actualIndexActions,- "Expected index actions %v, got %v", tt.expectedIndexActions, actualIndexActions)-- // Verify AST nodes are properly set- for i, indexChange := range targetTableChange.IndexChanges {- switch indexChange.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, indexChange.NewASTNode,- "Index change %d should have NewASTNode for CREATE", i)- assert.Nil(t, indexChange.OldASTNode,- "Index change %d should not have OldASTNode for CREATE", i)- case schema.MetadataDiffActionDrop:- assert.NotNil(t, indexChange.OldASTNode,- "Index change %d should have OldASTNode for DROP", i)- assert.Nil(t, indexChange.NewASTNode,- "Index change %d should not have NewASTNode for DROP", i)- default:- t.Errorf("Unexpected index action %v for change %d", indexChange.Action, i)- }- }- }- })- }-}--// TestIndexTableNameExtraction specifically tests the table name extraction logic-func TestIndexTableNameExtraction(t *testing.T) {- tests := []struct {- name string- indexSQL string- expectedName string- }{- {- name: "Simple table name",- indexSQL: "CREATE INDEX idx_test ON users(email);",- expectedName: "public.users", // Defaults to public schema- },- {- name: "Qualified table name",- indexSQL: "CREATE INDEX idx_test ON public.users(email);",- expectedName: "public.users",- },- {- name: "Custom schema",- indexSQL: "CREATE INDEX idx_test ON myschema.products(name);",- expectedName: "myschema.products",- },- {- name: "Complex index with WHERE clause",- indexSQL: "CREATE INDEX idx_active_users ON public.users(email) WHERE active = true;",- expectedName: "public.users",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Parse the index statement- chunks, err := ChunkSDLText(tt.indexSQL)- require.NoError(t, err)- require.Equal(t, 1, len(chunks.Indexes), "Should have exactly one index")-- // Get the index chunk- var indexChunk *schema.SDLChunk- for _, chunk := range chunks.Indexes {- indexChunk = chunk- break- }- require.NotNil(t, indexChunk)-- // Test table name extraction- tableName := extractTableNameFromIndex(indexChunk.ASTNode)- assert.Equal(t, tt.expectedName, tableName,- "Expected table name '%s', got '%s'", tt.expectedName, tableName)- })- }-}--// TestIndexSchemaNameExtraction tests that index schema names are correctly extracted-// This is a regression test for the bug where indexes in non-public schemas-// were incorrectly identified as belonging to the public schema-func TestIndexSchemaNameExtraction(t *testing.T) {- tests := []struct {- name string- previousUserSDL string- currentSDL string- expectedSchemaName string- expectedTableName string- expectedIndexCount int- }{- {- name: "Index in company schema",- previousUserSDL: "",- currentSDL: `-CREATE SCHEMA company;--CREATE TABLE "company"."employees" (- "id" integer NOT NULL,- "first_name" character varying(50) NOT NULL,- "email" character varying(100) NOT NULL-);--CREATE INDEX "idx_employees_email" ON "company"."employees" (email);- `,- expectedSchemaName: "company",- expectedTableName: "employees",- expectedIndexCount: 1,- },- {- name: "Index in custom schema with multiple indexes",- previousUserSDL: `-CREATE SCHEMA myapp;--CREATE TABLE "myapp"."products" (- "id" integer NOT NULL,- "name" text,- "price" numeric-);- `,- currentSDL: `-CREATE SCHEMA myapp;--CREATE TABLE "myapp"."products" (- "id" integer NOT NULL,- "name" text,- "price" numeric-);--CREATE INDEX "idx_products_name" ON "myapp"."products" (name);-CREATE INDEX "idx_products_price" ON "myapp"."products" (price);- `,- expectedSchemaName: "myapp",- expectedTableName: "products",- expectedIndexCount: 2,- },- {- name: "Mixed schemas - should only affect correct schema",- previousUserSDL: `-CREATE SCHEMA company;-CREATE SCHEMA sales;--CREATE TABLE "company"."employees" (- "id" integer NOT NULL,- "name" text-);--CREATE TABLE "sales"."orders" (- "id" integer NOT NULL,- "amount" numeric-);- `,- currentSDL: `-CREATE SCHEMA company;-CREATE SCHEMA sales;--CREATE TABLE "company"."employees" (- "id" integer NOT NULL,- "name" text-);--CREATE TABLE "sales"."orders" (- "id" integer NOT NULL,- "amount" numeric-);--CREATE INDEX "idx_employees_name" ON "company"."employees" (name);- `,- expectedSchemaName: "company",- expectedTableName: "employees",- expectedIndexCount: 1,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousUserSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Find the table change for our expected table- var targetTableDiff *schema.TableDiff- for _, tableDiff := range diff.TableChanges {- if tableDiff.SchemaName == tt.expectedSchemaName && tableDiff.TableName == tt.expectedTableName {- targetTableDiff = tableDiff- break- }- }-- require.NotNil(t, targetTableDiff,- "Expected to find table change for %s.%s", tt.expectedSchemaName, tt.expectedTableName)-- // Verify schema name is correct (not defaulting to "public")- assert.Equal(t, tt.expectedSchemaName, targetTableDiff.SchemaName,- "Expected schema name '%s', got '%s'", tt.expectedSchemaName, targetTableDiff.SchemaName)-- // Verify table name is correct- assert.Equal(t, tt.expectedTableName, targetTableDiff.TableName,- "Expected table name '%s', got '%s'", tt.expectedTableName, targetTableDiff.TableName)-- // Verify index count- assert.Equal(t, tt.expectedIndexCount, len(targetTableDiff.IndexChanges),- "Expected %d index changes, got %d", tt.expectedIndexCount, len(targetTableDiff.IndexChanges))-- // Verify all index changes have CREATE action- for _, indexChange := range targetTableDiff.IndexChanges {- assert.Equal(t, schema.MetadataDiffActionCreate, indexChange.Action,- "Expected CREATE action for index")- }- })- }-}--// TestPartialIndexWhereClauseInSDLOutput tests that partial index WHERE clauses-// are preserved in SDL output (regression test for WHERE clause being dropped).-func TestPartialIndexWhereClauseInSDLOutput(t *testing.T) {- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "channels",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Position: 1},- {Name: "slug", Type: "character varying(255)", Position: 2},- {Name: "social_region", Type: "character varying(50)", Position: 3},- },- Indexes: []*storepb.IndexMetadata{- {- Name: "udx_slug_social_region_on_channels",- Expressions: []string{"slug", "social_region"},- Type: "btree",- Unique: true,- Definition: "CREATE UNIQUE INDEX udx_slug_social_region_on_channels ON public.channels USING btree (slug, social_region) WHERE ((slug)::text <> ''::text);",- },- },- },- },- },- },- }-- sdl, err := getSDLFormat(metadata)- require.NoError(t, err)-- // WHERE clause must be present- require.Contains(t, sdl, "WHERE")- require.Contains(t, sdl, "(slug)::text")-- // Non-partitioned table: must NOT have ON ONLY- require.NotContains(t, sdl, "ON ONLY")- require.Contains(t, sdl, "ON \"public\".\"channels\"")-}--// TestRegularIndexNoOnOnly tests that regular (non-partitioned) table indexes-// do not include ON ONLY in SDL output.-func TestRegularIndexNoOnOnly(t *testing.T) {- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Position: 1},- {Name: "email", Type: "character varying(255)", Position: 2},- },- Indexes: []*storepb.IndexMetadata{- {- Name: "idx_users_email",- Expressions: []string{"email"},- Type: "btree",- },- },- },- },- },- },- }-- sdl, err := getSDLFormat(metadata)- require.NoError(t, err)-- require.Contains(t, sdl, `CREATE INDEX "idx_users_email"`)- require.Contains(t, sdl, `ON "public"."users"`)- require.NotContains(t, sdl, "ON ONLY")-}--// TestPartitionedTableIndexUsesOnOnly tests that partitioned parent table indexes-// use ON ONLY in SDL output to prevent recursive child index creation.-func TestPartitionedTableIndexUsesOnOnly(t *testing.T) {- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "orders",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Position: 1},- {Name: "order_date", Type: "date", Position: 2},- {Name: "customer_id", Type: "integer", Position: 3},- },- Indexes: []*storepb.IndexMetadata{- {- Name: "idx_orders_customer_id",- Expressions: []string{"customer_id"},- Type: "btree",- },- },- Partitions: []*storepb.TablePartitionMetadata{- {- Name: "orders_2024",- Type: storepb.TablePartitionMetadata_RANGE,- },- },- },- },- },- },- }-- sdl, err := getSDLFormat(metadata)- require.NoError(t, err)-- require.Contains(t, sdl, `CREATE INDEX "idx_orders_customer_id"`)- require.Contains(t, sdl, `ON ONLY "public"."orders"`)-}--// TestCoveringIndexIncludeClauseInSDLOutput tests that covering index INCLUDE clauses-// are preserved in SDL output (regression test for INCLUDE clause being dropped).-func TestCoveringIndexIncludeClauseInSDLOutput(t *testing.T) {- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "products",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Position: 1},- {Name: "name", Type: "text", Position: 2},- {Name: "description", Type: "text", Position: 3},- {Name: "price", Type: "numeric(10,2)", Position: 4},- },- Indexes: []*storepb.IndexMetadata{- {- Name: "idx_products_price_covering",- Expressions: []string{"price"},- Type: "btree",- Definition: "CREATE INDEX idx_products_price_covering ON public.products USING btree (price) INCLUDE (name, description);",- },- },- },- },- },- },- }-- sdl, err := getSDLFormat(metadata)- require.NoError(t, err)-- require.Contains(t, sdl, "INCLUDE (name, description)")- require.Contains(t, sdl, `ON "public"."products"`)- require.NotContains(t, sdl, "ON ONLY")-}--// TestCoveringIndexWithWhereClause tests that an index with both INCLUDE and WHERE-// clauses preserves both in the correct SQL order.-func TestCoveringIndexWithWhereClause(t *testing.T) {- metadata := &storepb.DatabaseSchemaMetadata{- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "orders",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Position: 1},- {Name: "status", Type: "text", Position: 2},- {Name: "total", Type: "numeric", Position: 3},- {Name: "customer_name", Type: "text", Position: 4},- },- Indexes: []*storepb.IndexMetadata{- {- Name: "idx_orders_active_covering",- Expressions: []string{"status"},- Type: "btree",- Definition: "CREATE INDEX idx_orders_active_covering ON public.orders USING btree (status) INCLUDE (total, customer_name) WHERE (status = 'active'::text);",- },- },- },- },- },- },- }-- sdl, err := getSDLFormat(metadata)- require.NoError(t, err)-- require.Contains(t, sdl, "INCLUDE (total, customer_name)")- require.Contains(t, sdl, "WHERE")- require.Contains(t, sdl, "active")-- // Verify correct order: INCLUDE before WHERE- includePos := strings.Index(sdl, "INCLUDE")- wherePos := strings.Index(sdl, "WHERE")- require.Greater(t, wherePos, includePos, "INCLUDE should come before WHERE in SQL syntax")-}diff --git a/backend/plugin/schema/pg/table_constraints_test.go b/backend/plugin/schema/pg/table_constraints_test.godeleted file mode 100644index d3a94cf821..0000000000--- a/backend/plugin/schema/pg/table_constraints_test.go+++ /dev/null@@ -1,212 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// TestTableConstraintsSDLDiff tests all table constraint types (PK, UK, FK, Check)-func TestTableConstraintsSDLDiff(t *testing.T) {- testCases := []struct {- name string- currentSDL string- previousSDL string- validate func(t *testing.T, diff *schema.MetadataDiff)- }{- {- name: "Add primary key constraint",- currentSDL: `CREATE TABLE users (- id INTEGER,- name VARCHAR(255),- CONSTRAINT pk_users PRIMARY KEY (id)- );`,- previousSDL: `CREATE TABLE users (- id INTEGER,- name VARCHAR(255)- );`,- validate: func(t *testing.T, diff *schema.MetadataDiff) {- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)- assert.Len(t, tableDiff.PrimaryKeyChanges, 1)- pkChange := tableDiff.PrimaryKeyChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, pkChange.Action)- // AST node should be present for created primary key- assert.NotNil(t, pkChange.NewASTNode)- assert.Nil(t, pkChange.OldASTNode)- },- },- {- name: "Drop foreign key constraint",- currentSDL: `CREATE TABLE orders (- id INTEGER PRIMARY KEY,- customer_id INTEGER- );`,- previousSDL: `CREATE TABLE orders (- id INTEGER PRIMARY KEY,- customer_id INTEGER,- CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers(id)- );`,- validate: func(t *testing.T, diff *schema.MetadataDiff) {- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)- assert.Len(t, tableDiff.ForeignKeyChanges, 1)- fkChange := tableDiff.ForeignKeyChanges[0]- assert.Equal(t, schema.MetadataDiffActionDrop, fkChange.Action)- // AST node should be present for dropped FK- assert.NotNil(t, fkChange.OldASTNode)- assert.Nil(t, fkChange.NewASTNode)- },- },- {- name: "Modify check constraint",- currentSDL: `CREATE TABLE products (- id INTEGER PRIMARY KEY,- price DECIMAL(10,2),- CONSTRAINT chk_price CHECK (price >= 0)- );`,- previousSDL: `CREATE TABLE products (- id INTEGER PRIMARY KEY,- price DECIMAL(10,2),- CONSTRAINT chk_price CHECK (price > 0)- );`,- validate: func(t *testing.T, diff *schema.MetadataDiff) {- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)- // Should drop old and create new for modifications- assert.Len(t, tableDiff.CheckConstraintChanges, 2)-- // Find drop and create actions- var dropChange, createChange *schema.CheckConstraintDiff- for _, change := range tableDiff.CheckConstraintChanges {- switch change.Action {- case schema.MetadataDiffActionDrop:- dropChange = change- case schema.MetadataDiffActionCreate:- createChange = change- default:- // Other actions not expected in this test- }- }-- assert.NotNil(t, dropChange)- assert.NotNil(t, createChange)- // AST nodes should be present for check constraint changes- assert.NotNil(t, dropChange.OldASTNode)- assert.Nil(t, dropChange.NewASTNode)- assert.NotNil(t, createChange.NewASTNode)- assert.Nil(t, createChange.OldASTNode)- },- },- {- name: "Multiple constraint types in one table",- currentSDL: `CREATE TABLE orders (- id INTEGER,- customer_id INTEGER,- order_date DATE,- amount DECIMAL(10,2),- status VARCHAR(20),- CONSTRAINT pk_orders PRIMARY KEY (id),- CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers(id),- CONSTRAINT uk_orders_date_customer UNIQUE (order_date, customer_id),- CONSTRAINT chk_positive_amount CHECK (amount > 0),- CONSTRAINT chk_valid_status CHECK (status IN ('pending', 'completed', 'cancelled'))- );`,- previousSDL: `CREATE TABLE orders (- id INTEGER,- customer_id INTEGER,- order_date DATE,- amount DECIMAL(10,2),- status VARCHAR(20)- );`,- validate: func(t *testing.T, diff *schema.MetadataDiff) {- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)-- // Should have 1 primary key change- assert.Len(t, tableDiff.PrimaryKeyChanges, 1)-- // Should have 1 unique constraint change- assert.Len(t, tableDiff.UniqueConstraintChanges, 1)-- // Should have 1 foreign key change- assert.Len(t, tableDiff.ForeignKeyChanges, 1)- fkChange := tableDiff.ForeignKeyChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, fkChange.Action)- // AST node should be present for created FK- assert.NotNil(t, fkChange.NewASTNode)- assert.Nil(t, fkChange.OldASTNode)-- // Should have 2 check constraint changes- assert.Len(t, tableDiff.CheckConstraintChanges, 2)- for _, checkChange := range tableDiff.CheckConstraintChanges {- assert.Equal(t, schema.MetadataDiffActionCreate, checkChange.Action)- // AST node should be present for created check constraints- assert.NotNil(t, checkChange.NewASTNode)- assert.Nil(t, checkChange.OldASTNode)- }- },- },- {- name: "Complex foreign key with schema qualification",- currentSDL: `CREATE TABLE orders (- id INTEGER PRIMARY KEY,- customer_id INTEGER,- CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES public.customers(id)- );`,- previousSDL: `CREATE TABLE orders (- id INTEGER PRIMARY KEY,- customer_id INTEGER- );`,- validate: func(t *testing.T, diff *schema.MetadataDiff) {- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]- assert.Len(t, tableDiff.ForeignKeyChanges, 1)- fkChange := tableDiff.ForeignKeyChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, fkChange.Action)- // AST node should be present for created FK with schema qualification- assert.NotNil(t, fkChange.NewASTNode)- assert.Nil(t, fkChange.OldASTNode)- },- },- {- name: "Composite unique constraint",- currentSDL: `CREATE TABLE user_sessions (- user_id INTEGER,- session_token VARCHAR(255),- created_at TIMESTAMP,- CONSTRAINT uk_user_sessions UNIQUE (user_id, session_token)- );`,- previousSDL: `CREATE TABLE user_sessions (- user_id INTEGER,- session_token VARCHAR(255),- created_at TIMESTAMP- );`,- validate: func(t *testing.T, diff *schema.MetadataDiff) {- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]- assert.Len(t, tableDiff.UniqueConstraintChanges, 1)- ukChange := tableDiff.UniqueConstraintChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, ukChange.Action)- // AST node should be present for created unique constraint- assert.NotNil(t, ukChange.NewASTNode)- assert.Nil(t, ukChange.OldASTNode)- },- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- diff, err := GetSDLDiff(tc.currentSDL, tc.previousSDL, nil)- require.NoError(t, err, "GetSDLDiff should not return error")- tc.validate(t, diff)- })- }-}diff --git a/backend/plugin/schema/pg/table_migration_integration_test.go b/backend/plugin/schema/pg/table_migration_integration_test.godeleted file mode 100644index 03c10bd085..0000000000--- a/backend/plugin/schema/pg/table_migration_integration_test.go+++ /dev/null@@ -1,1105 +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 TestTableSDLDiffAndMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Create new table",- previousSDL: ``,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(255) UNIQUE,- created_at TIMESTAMP DEFAULT NOW()- );- `,- expectedMigration: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- email VARCHAR(255) UNIQUE,- created_at TIMESTAMP DEFAULT NOW()- );--`,- },- {- name: "Drop table",- previousSDL: `- CREATE TABLE old_table (- id INTEGER PRIMARY KEY,- data TEXT- );- `,- currentSDL: ``,- expectedMigration: `DROP TABLE IF EXISTS "public"."old_table";-`,- },- {- name: "Create table with constraints",- previousSDL: ``,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- price DECIMAL(10,2) CHECK (price > 0),- category_id INTEGER,- CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(id)- );- `,- expectedMigration: `CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- price DECIMAL(10,2) CHECK (price > 0),- category_id INTEGER,- CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(id)- );--`,- },- {- name: "Create schema-qualified table",- previousSDL: ``,- currentSDL: `- CREATE TABLE test_schema.items (- id BIGSERIAL PRIMARY KEY,- description TEXT- );- `,- expectedMigration: `CREATE TABLE test_schema.items (- id BIGSERIAL PRIMARY KEY,- description TEXT- );--`,- },- {- name: "Multiple tables with different operations",- previousSDL: `- CREATE TABLE table_a (id INTEGER PRIMARY KEY);- CREATE TABLE table_b (id INTEGER PRIMARY KEY);- `,- currentSDL: `- CREATE TABLE table_a (id INTEGER PRIMARY KEY);- CREATE TABLE table_c (id INTEGER PRIMARY KEY);- `,- expectedMigration: `DROP TABLE IF EXISTS "public"."table_b";--CREATE TABLE table_c (id INTEGER PRIMARY KEY);--`,- },- {- name: "Create table with various data types",- previousSDL: ``,- currentSDL: `- CREATE TABLE data_types_test (- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),- title VARCHAR(100),- content TEXT,- count INTEGER,- amount DECIMAL(15,2),- is_active BOOLEAN DEFAULT false,- created_at TIMESTAMPTZ DEFAULT NOW(),- metadata JSONB- );- `,- expectedMigration: `CREATE TABLE data_types_test (- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),- title VARCHAR(100),- content TEXT,- count INTEGER,- amount DECIMAL(15,2),- is_active BOOLEAN DEFAULT false,- created_at TIMESTAMPTZ DEFAULT NOW(),- metadata JSONB- );--`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Step 1: Get SDL diff using AST-only mode (no metadata extraction)- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Step 2: Verify that diff contains table changes with AST nodes- if len(diff.TableChanges) > 0 {- for _, tableDiff := range diff.TableChanges {- switch tableDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, tableDiff.NewASTNode,- "Create action should have NewASTNode")- assert.Nil(t, tableDiff.OldASTNode,- "Create action should not have OldASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, tableDiff.NewTable,- "AST-only mode should not have metadata")- case schema.MetadataDiffActionDrop:- assert.NotNil(t, tableDiff.OldASTNode,- "Drop action should have OldASTNode")- assert.Nil(t, tableDiff.NewASTNode,- "Drop action should not have NewASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, tableDiff.OldTable,- "AST-only mode should not have metadata")- default:- // Other actions like ALTER- t.Logf("Encountered table action: %v", tableDiff.Action)- }- }- }-- // Step 3: Generate migration SQL using AST nodes- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)-- // Step 4: Verify the generated migration matches expectations- assert.Equal(t, tt.expectedMigration, migrationSQL,- "Generated migration SQL should match expected output")-- // Step 5: Verify the migration contains expected keywords for tables- if tt.expectedMigration != "" {- if containsTableString(tt.expectedMigration, "CREATE TABLE") {- assert.Contains(t, migrationSQL, "CREATE TABLE",- "Migration should contain CREATE TABLE statement")- }- if containsTableString(tt.expectedMigration, "DROP TABLE") {- assert.Contains(t, migrationSQL, "DROP TABLE",- "Migration should contain DROP TABLE statement")- }- }- })- }-}--func TestTableMigrationASTOnlyModeValidation(t *testing.T) {- // Test that ensures AST-only mode works correctly without any metadata extraction- previousSDL := ``- currentSDL := `- CREATE TABLE test_table (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- status VARCHAR(50) DEFAULT 'active'- );- `-- // Get diff without metadata extraction- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly one table change- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]-- // Verify AST-only mode properties- assert.Equal(t, schema.MetadataDiffActionCreate, tableDiff.Action)- assert.Equal(t, "public", tableDiff.SchemaName)- assert.Equal(t, "test_table", tableDiff.TableName)-- // Critical assertion: No metadata should be present (AST-only mode)- assert.Nil(t, tableDiff.NewTable, "AST-only mode should not extract metadata")- assert.Nil(t, tableDiff.OldTable, "AST-only mode should not extract metadata")-- // But AST nodes should be present- assert.NotNil(t, tableDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, tableDiff.OldASTNode, "No old AST node for CREATE action")-- // Generate migration should work with AST nodes only- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- assert.Contains(t, migrationSQL, "CREATE TABLE test_table")- assert.Contains(t, migrationSQL, "id SERIAL PRIMARY KEY")- assert.Contains(t, migrationSQL, "name VARCHAR(255) NOT NULL")- assert.Contains(t, migrationSQL, "status VARCHAR(50) DEFAULT 'active'")-}--func TestMultipleTablesHandling(t *testing.T) {- // Test that verifies multiple tables are handled correctly- previousSDL := `- CREATE TABLE table1 (id INTEGER PRIMARY KEY);- `- currentSDL := `- CREATE TABLE table1 (id INTEGER PRIMARY KEY);- CREATE TABLE table2 (id SERIAL PRIMARY KEY, name TEXT);- CREATE TABLE table3 (id UUID PRIMARY KEY, data JSONB);- `-- // Get diff without metadata extraction (AST-only mode)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly two table changes (CREATE actions for table2 and table3)- assert.Len(t, diff.TableChanges, 2)-- // Verify both tables are CREATE actions- createCount := 0- tableNames := make(map[string]bool)- for _, tableDiff := range diff.TableChanges {- if tableDiff.Action == schema.MetadataDiffActionCreate {- createCount++- // Verify AST-only mode properties- assert.NotNil(t, tableDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, tableDiff.OldASTNode, "No old AST node for CREATE action")- assert.Nil(t, tableDiff.NewTable, "AST-only mode should not extract metadata")-- // Collect table names to verify they're different- tableNames[tableDiff.TableName] = true- }- }- assert.Equal(t, 2, createCount, "Should have two CREATE actions")-- // Verify the table names are correct- expectedTables := map[string]bool{- "table2": true,- "table3": true,- }- assert.Equal(t, expectedTables, tableNames, "Should have correct table names")-- // Generate migration SQL - this should work with both tables- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- // Verify the migration contains both tables- assert.Contains(t, migrationSQL, "CREATE TABLE table2")- assert.Contains(t, migrationSQL, "CREATE TABLE table3")-- // Verify both tables are created- tableCount := strings.Count(migrationSQL, "CREATE TABLE")- assert.Equal(t, 2, tableCount, "Should create both tables")-}--func TestComplexTableWithConstraintsAndIndexes(t *testing.T) {- // Test with complex table definition including constraints and indexes- previousSDL := ``- currentSDL := `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- order_date DATE DEFAULT CURRENT_DATE,- total_amount DECIMAL(12,2) NOT NULL CHECK (total_amount >= 0),- status VARCHAR(20) DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'shipped', 'delivered')),- created_at TIMESTAMP DEFAULT NOW(),- updated_at TIMESTAMP DEFAULT NOW(),- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,- CONSTRAINT unique_customer_date UNIQUE (customer_id, order_date)- );- `-- // Get diff in AST-only mode- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have one table creation- assert.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]-- // Verify properties- assert.Equal(t, schema.MetadataDiffActionCreate, tableDiff.Action)- assert.Equal(t, "public", tableDiff.SchemaName)- assert.Equal(t, "orders", tableDiff.TableName)- assert.Nil(t, tableDiff.NewTable, "AST-only mode should not extract metadata")- assert.NotNil(t, tableDiff.NewASTNode, "AST node should be present")-- // Generate migration- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)-- // Verify the migration contains the complex table definition- assert.Contains(t, migrationSQL, "CREATE TABLE orders")- assert.Contains(t, migrationSQL, "id SERIAL PRIMARY KEY")- assert.Contains(t, migrationSQL, "total_amount DECIMAL(12,2) NOT NULL CHECK (total_amount >= 0)")- assert.Contains(t, migrationSQL, "CONSTRAINT fk_customer FOREIGN KEY")- assert.Contains(t, migrationSQL, "CONSTRAINT unique_customer_date UNIQUE")-}--func TestTableConstraintSDLDiffAndMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Add CHECK constraint",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL- );- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_price CHECK (price > 0)- );- `,- expectedMigration: `ALTER TABLE "public"."products" ADD CONSTRAINT "chk_price" CHECK (price > 0);`,- },- {- name: "Add FOREIGN KEY constraint",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL- );- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id)- );- `,- expectedMigration: `ALTER TABLE "public"."orders" ADD CONSTRAINT "fk_customer" FOREIGN KEY (customer_id) REFERENCES customers(id);`,- },- {- name: "Add UNIQUE constraint",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) NOT NULL,- CONSTRAINT unique_email UNIQUE (email)- );- `,- expectedMigration: `ALTER TABLE "public"."users" ADD CONSTRAINT "unique_email" UNIQUE (email);`,- },- {- name: "Drop CHECK constraint",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_price CHECK (price > 0)- );- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."products" DROP CONSTRAINT IF EXISTS "chk_price";`,- },- {- name: "Drop FOREIGN KEY constraint",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id)- );- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."orders" DROP CONSTRAINT IF EXISTS "fk_customer";`,- },- {- name: "Modify CHECK constraint",- previousSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_price CHECK (price > 0)- );- `,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_price CHECK (price >= 0)- );- `,- expectedMigration: `ALTER TABLE "public"."products" DROP CONSTRAINT IF EXISTS "chk_price";--ALTER TABLE "public"."products" ADD CONSTRAINT "chk_price" CHECK (price >= 0);`,- },- {- name: "Add PRIMARY KEY constraint",- previousSDL: `- CREATE TABLE users (- id INTEGER NOT NULL,- name VARCHAR(255) NOT NULL- );- `,- currentSDL: `- CREATE TABLE users (- id INTEGER NOT NULL,- name VARCHAR(255) NOT NULL,- CONSTRAINT pk_users PRIMARY KEY (id)- );- `,- expectedMigration: `ALTER TABLE "public"."users" ADD CONSTRAINT "pk_users" PRIMARY KEY (id);`,- },- {- name: "Drop PRIMARY KEY constraint",- previousSDL: `- CREATE TABLE users (- id INTEGER NOT NULL,- name VARCHAR(255) NOT NULL,- CONSTRAINT pk_users PRIMARY KEY (id)- );- `,- currentSDL: `- CREATE TABLE users (- id INTEGER NOT NULL,- name VARCHAR(255) NOT NULL- );- `,- expectedMigration: `ALTER TABLE "public"."users" DROP CONSTRAINT IF EXISTS "pk_users";`,- },- {- name: "Add multiple constraints",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending',- CONSTRAINT chk_total_amount CHECK (total_amount >= 0),- CONSTRAINT chk_status CHECK (status IN ('pending', 'confirmed', 'shipped')),- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id),- CONSTRAINT unique_customer_status UNIQUE (customer_id, status)- );- `,- expectedMigration: `ALTER TABLE "public"."orders" ADD CONSTRAINT "chk_total_amount" CHECK (total_amount >= 0);-ALTER TABLE "public"."orders" ADD CONSTRAINT "chk_status" CHECK (status IN ('pending', 'confirmed', 'shipped'));-ALTER TABLE "public"."orders" ADD CONSTRAINT "unique_customer_status" UNIQUE (customer_id, status);--ALTER TABLE "public"."orders" ADD CONSTRAINT "fk_customer" FOREIGN KEY (customer_id) REFERENCES customers(id);`,- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- // Get diff in AST-only mode (nil schemas for AST-only)- diff, err := GetSDLDiff(test.currentSDL, test.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have one table change- require.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]-- // Verify it's an ALTER action for constraint changes- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)-- // Verify AST-only mode properties- assert.Nil(t, tableDiff.NewTable, "AST-only mode should not extract metadata")- assert.Nil(t, tableDiff.OldTable, "AST-only mode should not extract metadata")- assert.NotNil(t, tableDiff.NewASTNode, "New AST node should be present")- assert.NotNil(t, tableDiff.OldASTNode, "Old AST node should be present")-- // Verify constraint changes are detected- hasConstraintChanges := len(tableDiff.CheckConstraintChanges) > 0 ||- len(tableDiff.ForeignKeyChanges) > 0 ||- len(tableDiff.UniqueConstraintChanges) > 0 ||- len(tableDiff.PrimaryKeyChanges) > 0-- assert.True(t, hasConstraintChanges, "Should detect constraint changes")-- // Generate migration SQL- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)-- // Normalize whitespace for comparison- normalizedMigration := strings.TrimSpace(migrationSQL)- normalizedExpected := strings.TrimSpace(test.expectedMigration)-- assert.Equal(t, normalizedExpected, normalizedMigration,- "Migration SQL should match expected output")- })- }-}--func TestTableConstraintComplexChanges(t *testing.T) {- // Test a complex scenario with constraint additions, modifications, and drops- previousSDL := `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) NOT NULL UNIQUE,- age INTEGER CHECK (age >= 18),- created_at TIMESTAMP DEFAULT NOW()- );-- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- user_id INTEGER NOT NULL,- amount DECIMAL(10,2) NOT NULL,- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id)- );- `-- currentSDL := `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(255) NOT NULL,- age INTEGER CHECK (age >= 16),- phone VARCHAR(20) UNIQUE,- created_at TIMESTAMP DEFAULT NOW()- );-- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- user_id INTEGER NOT NULL,- amount DECIMAL(10,2) NOT NULL CHECK (amount > 0),- status VARCHAR(20) DEFAULT 'pending',- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,- CONSTRAINT unique_user_amount UNIQUE (user_id, amount)- );- `-- // Get diff in AST-only mode- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have changes for both tables- assert.Len(t, diff.TableChanges, 2)-- // Check that each table has the expected constraint changes- for _, tableDiff := range diff.TableChanges {- assert.Equal(t, schema.MetadataDiffActionAlter, tableDiff.Action)-- // Verify AST-only mode properties- assert.Nil(t, tableDiff.NewTable, "AST-only mode should not extract metadata")- assert.Nil(t, tableDiff.OldTable, "AST-only mode should not extract metadata")- assert.NotNil(t, tableDiff.NewASTNode, "New AST node should be present")- assert.NotNil(t, tableDiff.OldASTNode, "Old AST node should be present")-- // Verify constraint or column changes are detected- hasConstraintChanges := len(tableDiff.CheckConstraintChanges) > 0 ||- len(tableDiff.ForeignKeyChanges) > 0 ||- len(tableDiff.UniqueConstraintChanges) > 0 ||- len(tableDiff.PrimaryKeyChanges) > 0 ||- len(tableDiff.ColumnChanges) > 0-- if tableDiff.TableName == "users" || tableDiff.TableName == "orders" {- assert.True(t, hasConstraintChanges, "Should detect constraint or column changes for %s", tableDiff.TableName)- }- }-- // Generate migration SQL- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- // Verify the migration handles both tables- assert.Contains(t, migrationSQL, "users")- assert.Contains(t, migrationSQL, "orders")-}--func TestCreateTableWithTableConstraintsIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Create table with CHECK constraint",- previousSDL: ``,- currentSDL: `- CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_price CHECK (price > 0)- );- `,- expectedMigration: `CREATE TABLE products (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_price CHECK (price > 0)- );--`,- },- {- name: "Create table with FOREIGN KEY constraint",- previousSDL: ``,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- order_date DATE DEFAULT CURRENT_DATE,- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE- );- `,- expectedMigration: `CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- order_date DATE DEFAULT CURRENT_DATE,- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE- );--`,- },- {- name: "Create table with UNIQUE constraint",- previousSDL: ``,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(320) NOT NULL,- username VARCHAR(50) NOT NULL,- CONSTRAINT unique_email UNIQUE (email),- CONSTRAINT unique_username UNIQUE (username)- );- `,- expectedMigration: `CREATE TABLE users (- id SERIAL PRIMARY KEY,- email VARCHAR(320) NOT NULL,- username VARCHAR(50) NOT NULL,- CONSTRAINT unique_email UNIQUE (email),- CONSTRAINT unique_username UNIQUE (username)- );--`,- },- {- name: "Create table with PRIMARY KEY constraint",- previousSDL: ``,- currentSDL: `- CREATE TABLE composite_key_table (- tenant_id INTEGER NOT NULL,- user_id INTEGER NOT NULL,- data TEXT,- CONSTRAINT pk_composite PRIMARY KEY (tenant_id, user_id)- );- `,- expectedMigration: `CREATE TABLE composite_key_table (- tenant_id INTEGER NOT NULL,- user_id INTEGER NOT NULL,- data TEXT,- CONSTRAINT pk_composite PRIMARY KEY (tenant_id, user_id)- );--`,- },- {- name: "Create table with multiple constraints",- previousSDL: ``,- currentSDL: `- CREATE TABLE orders (- id SERIAL,- customer_id INTEGER NOT NULL,- order_date DATE DEFAULT CURRENT_DATE,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending',- created_at TIMESTAMP DEFAULT NOW(),- CONSTRAINT pk_orders PRIMARY KEY (id),- CONSTRAINT chk_total_amount CHECK (total_amount >= 0),- CONSTRAINT chk_status CHECK (status IN ('pending', 'confirmed', 'shipped', 'delivered')),- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,- CONSTRAINT unique_customer_date UNIQUE (customer_id, order_date)- );- `,- expectedMigration: `CREATE TABLE orders (- id SERIAL,- customer_id INTEGER NOT NULL,- order_date DATE DEFAULT CURRENT_DATE,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending',- created_at TIMESTAMP DEFAULT NOW(),- CONSTRAINT pk_orders PRIMARY KEY (id),- CONSTRAINT chk_total_amount CHECK (total_amount >= 0),- CONSTRAINT chk_status CHECK (status IN ('pending', 'confirmed', 'shipped', 'delivered')),- CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,- CONSTRAINT unique_customer_date UNIQUE (customer_id, order_date)- );--`,- },- {- name: "Create table with complex CHECK constraints",- previousSDL: ``,- currentSDL: `- CREATE TABLE employees (- id SERIAL PRIMARY KEY,- first_name VARCHAR(50) NOT NULL,- last_name VARCHAR(50) NOT NULL,- email VARCHAR(320) NOT NULL,- salary DECIMAL(10,2),- hire_date DATE DEFAULT CURRENT_DATE,- department_id INTEGER,- CONSTRAINT chk_salary CHECK (salary IS NULL OR salary > 0),- CONSTRAINT chk_email_format CHECK (email ~* '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'),- CONSTRAINT chk_hire_date CHECK (hire_date >= '2000-01-01'),- CONSTRAINT chk_name_not_empty CHECK (LENGTH(TRIM(first_name)) > 0 AND LENGTH(TRIM(last_name)) > 0),- CONSTRAINT unique_email UNIQUE (email),- CONSTRAINT fk_department FOREIGN KEY (department_id) REFERENCES departments(id)- );- `,- expectedMigration: `CREATE TABLE employees (- id SERIAL PRIMARY KEY,- first_name VARCHAR(50) NOT NULL,- last_name VARCHAR(50) NOT NULL,- email VARCHAR(320) NOT NULL,- salary DECIMAL(10,2),- hire_date DATE DEFAULT CURRENT_DATE,- department_id INTEGER,- CONSTRAINT chk_salary CHECK (salary IS NULL OR salary > 0),- CONSTRAINT chk_email_format CHECK (email ~* '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'),- CONSTRAINT chk_hire_date CHECK (hire_date >= '2000-01-01'),- CONSTRAINT chk_name_not_empty CHECK (LENGTH(TRIM(first_name)) > 0 AND LENGTH(TRIM(last_name)) > 0),- CONSTRAINT unique_email UNIQUE (email),- CONSTRAINT fk_department FOREIGN KEY (department_id) REFERENCES departments(id)- );--`,- },- {- name: "Create table with FOREIGN KEY and referential actions",- previousSDL: ``,- currentSDL: `- CREATE TABLE order_items (- id SERIAL PRIMARY KEY,- order_id INTEGER NOT NULL,- product_id INTEGER NOT NULL,- quantity INTEGER NOT NULL DEFAULT 1,- unit_price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_quantity CHECK (quantity > 0),- CONSTRAINT chk_unit_price CHECK (unit_price > 0),- CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE ON UPDATE CASCADE,- CONSTRAINT fk_product FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE RESTRICT ON UPDATE CASCADE,- CONSTRAINT unique_order_product UNIQUE (order_id, product_id)- );- `,- expectedMigration: `CREATE TABLE order_items (- id SERIAL PRIMARY KEY,- order_id INTEGER NOT NULL,- product_id INTEGER NOT NULL,- quantity INTEGER NOT NULL DEFAULT 1,- unit_price DECIMAL(10,2) NOT NULL,- CONSTRAINT chk_quantity CHECK (quantity > 0),- CONSTRAINT chk_unit_price CHECK (unit_price > 0),- CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE ON UPDATE CASCADE,- CONSTRAINT fk_product FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE RESTRICT ON UPDATE CASCADE,- CONSTRAINT unique_order_product UNIQUE (order_id, product_id)- );--`,- },- {- name: "Create table with schema-qualified constraint references",- previousSDL: ``,- currentSDL: `- CREATE TABLE audit.user_actions (- id BIGSERIAL PRIMARY KEY,- user_id INTEGER NOT NULL,- action_type VARCHAR(50) NOT NULL,- table_name VARCHAR(100) NOT NULL,- record_id INTEGER,- action_timestamp TIMESTAMP DEFAULT NOW(),- CONSTRAINT chk_action_type CHECK (action_type IN ('INSERT', 'UPDATE', 'DELETE')),- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE,- CONSTRAINT unique_user_action_time UNIQUE (user_id, action_timestamp)- );- `,- expectedMigration: `CREATE TABLE audit.user_actions (- id BIGSERIAL PRIMARY KEY,- user_id INTEGER NOT NULL,- action_type VARCHAR(50) NOT NULL,- table_name VARCHAR(100) NOT NULL,- record_id INTEGER,- action_timestamp TIMESTAMP DEFAULT NOW(),- CONSTRAINT chk_action_type CHECK (action_type IN ('INSERT', 'UPDATE', 'DELETE')),- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE,- CONSTRAINT unique_user_action_time UNIQUE (user_id, action_timestamp)- );--`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Step 1: Get SDL diff using AST-only mode (no metadata extraction)- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Step 2: Verify that diff contains table creation with AST nodes- require.Len(t, diff.TableChanges, 1)- tableDiff := diff.TableChanges[0]-- assert.Equal(t, schema.MetadataDiffActionCreate, tableDiff.Action,- "Should be a CREATE action")- assert.NotNil(t, tableDiff.NewASTNode,- "Create action should have NewASTNode")- assert.Nil(t, tableDiff.OldASTNode,- "Create action should not have OldASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, tableDiff.NewTable,- "AST-only mode should not have metadata")-- // Step 3: Generate migration SQL using AST nodes- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err)-- // Step 4: Verify the generated migration matches expectations- assert.Equal(t, tt.expectedMigration, migrationSQL,- "Generated migration SQL should match expected output")-- // Step 5: Verify the migration contains expected constraint keywords- if tt.expectedMigration != "" {- if containsTableString(tt.expectedMigration, "CHECK") {- assert.Contains(t, migrationSQL, "CHECK",- "Migration should contain CHECK constraint")- }- if containsTableString(tt.expectedMigration, "FOREIGN KEY") {- assert.Contains(t, migrationSQL, "FOREIGN KEY",- "Migration should contain FOREIGN KEY constraint")- }- if containsTableString(tt.expectedMigration, "UNIQUE") {- assert.Contains(t, migrationSQL, "UNIQUE",- "Migration should contain UNIQUE constraint")- }- if containsTableString(tt.expectedMigration, "PRIMARY KEY") {- assert.Contains(t, migrationSQL, "PRIMARY KEY",- "Migration should contain PRIMARY KEY constraint")- }- if containsTableString(tt.expectedMigration, "CONSTRAINT") {- assert.Contains(t, migrationSQL, "CONSTRAINT",- "Migration should contain CONSTRAINT keyword")- }- }- })- }-}--// Helper function to check if a string contains a substring (table tests)-func containsTableString(s, substr string) bool {- return strings.Contains(s, substr)-}--func TestIndexSDLDiffAndMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Create standalone indexes",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );-- CREATE INDEX idx_orders_customer_id ON orders (customer_id);- CREATE UNIQUE INDEX idx_orders_status_unique ON orders (status);- CREATE INDEX idx_orders_complex ON orders (customer_id, total_amount DESC);- `,- expectedMigration: `CREATE INDEX idx_orders_customer_id ON orders (customer_id);-CREATE UNIQUE INDEX idx_orders_status_unique ON orders (status);-CREATE INDEX idx_orders_complex ON orders (customer_id, total_amount DESC);`,- },- {- name: "Drop standalone indexes",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );-- CREATE INDEX idx_orders_customer_id ON orders (customer_id);- CREATE UNIQUE INDEX idx_orders_status_unique ON orders (status);- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );- `,- expectedMigration: `DROP INDEX IF EXISTS "public"."idx_orders_customer_id";-DROP INDEX IF EXISTS "public"."idx_orders_status_unique";`,- },- {- name: "Replace indexes (drop old, create new)",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );-- CREATE INDEX idx_orders_old ON orders (customer_id);- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );-- CREATE INDEX idx_orders_new ON orders (customer_id, status);- `,- expectedMigration: `DROP INDEX IF EXISTS "public"."idx_orders_old";--CREATE INDEX idx_orders_new ON orders (customer_id, status);`,- },- {- name: "Mixed index operations",- previousSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );-- CREATE INDEX idx_orders_customer ON orders (customer_id);- CREATE INDEX idx_orders_amount ON orders (total_amount);- `,- currentSDL: `- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- customer_id INTEGER NOT NULL,- total_amount DECIMAL(12,2) NOT NULL,- status VARCHAR(20) DEFAULT 'pending'- );-- CREATE INDEX idx_orders_customer ON orders (customer_id);- CREATE UNIQUE INDEX idx_orders_status ON orders (status);- CREATE INDEX idx_orders_complex ON orders (customer_id, status, total_amount DESC);- `,- expectedMigration: `DROP INDEX IF EXISTS "public"."idx_orders_amount";--CREATE UNIQUE INDEX idx_orders_status ON orders (status);-CREATE INDEX idx_orders_complex ON orders (customer_id, status, total_amount DESC);`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Get diff in AST-only mode (no metadata extraction)- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err, "GetSDLDiff should not return error")- require.NotNil(t, diff, "diff should not be nil")-- // Generate migration using AST-only mode- migrationSQL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)- require.NoError(t, err, "GenerateMigration should not return error")-- // Normalize whitespace for comparison- normalizedExpected := strings.TrimSpace(tt.expectedMigration)- normalizedActual := strings.TrimSpace(migrationSQL)-- if normalizedExpected == "" {- assert.Empty(t, normalizedActual, "Migration should be empty")- } else {- // For index tests, check that essential parts are present- if strings.Contains(tt.expectedMigration, "CREATE INDEX") {- assert.Contains(t, migrationSQL, "CREATE INDEX",- "Migration should contain CREATE INDEX statements")- }- if strings.Contains(tt.expectedMigration, "CREATE UNIQUE INDEX") {- assert.Contains(t, migrationSQL, "CREATE UNIQUE INDEX",- "Migration should contain CREATE UNIQUE INDEX statements")- }- if strings.Contains(tt.expectedMigration, "DROP INDEX") {- assert.Contains(t, migrationSQL, "DROP INDEX IF EXISTS",- "Migration should contain DROP INDEX statements")- }-- // Check that specific index names are mentioned- lines := strings.Split(normalizedExpected, "\n")- for _, line := range lines {- line = strings.TrimSpace(line)- if strings.Contains(line, "idx_") {- // Extract index name and check it exists in migration- if strings.Contains(line, "CREATE") {- // For CREATE statements, check the full statement structure is preserved- assert.Contains(t, migrationSQL, line,- "Migration should contain the expected CREATE INDEX statement")- } else if strings.Contains(line, "DROP") {- // For DROP statements, just check the index name is dropped- indexName := extractIndexNameFromLine(line)- if indexName != "" {- assert.Contains(t, migrationSQL, indexName,- "Migration should drop the expected index")- }- }- }- }- }- })- }-}--// Helper function to extract index name from DROP INDEX statement-func extractIndexNameFromLine(line string) string {- if strings.Contains(line, "DROP INDEX") && strings.Contains(line, `"."`) {- // Extract from DROP INDEX IF EXISTS "schema"."index_name";- parts := strings.Split(line, `"."`)- if len(parts) >= 2 {- indexPart := parts[1]- // Remove trailing quote and semicolon- if strings.Contains(indexPart, `"`) {- return strings.Split(indexPart, `"`)[0]- }- }- }- return ""-}diff --git a/backend/plugin/schema/pg/topological_order_test.go b/backend/plugin/schema/pg/topological_order_test.godeleted file mode 100644index e25de410a8..0000000000--- a/backend/plugin/schema/pg/topological_order_test.go+++ /dev/null@@ -1,543 +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"-)--// TestTopologicalOrderCreateObjects tests that objects are created in the correct dependency order-func TestTopologicalOrderCreateObjects(t *testing.T) {- tests := []struct {- name string- diff *schema.MetadataDiff- expectedSQL string- 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: "public",- TableName: "orders",- NewTable: &storepb.TableMetadata{- Name: "orders",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "customer_id", Type: "INT", Nullable: false},- },- ForeignKeys: []*storepb.ForeignKeyMetadata{- {- Name: "fk_orders_customer",- Columns: []string{"customer_id"},- ReferencedSchema: "public",- ReferencedTable: "customers",- ReferencedColumns: []string{"id"},- },- },- },- },- // Create customers table (should be created first)- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- TableName: "customers",- NewTable: &storepb.TableMetadata{- Name: "customers",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: false},- },- },- },- },- },- description: "Customers table should be created before orders table due to FK dependency",- },- {- name: "table1_references_table2_name_column",- diff: &schema.MetadataDiff{- TableChanges: []*schema.TableDiff{- // Create table1 that references table2(name) - this comes FIRST in the list- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- TableName: "table1",- NewTable: &storepb.TableMetadata{- Name: "table1",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: false},- {Name: "created_at", Type: "TIMESTAMP", Default: "CURRENT_TIMESTAMP", Nullable: false},- {Name: "description", Type: "TEXT", Nullable: true},- },- ForeignKeys: []*storepb.ForeignKeyMetadata{- {- Name: "fk_table1_name",- Columns: []string{"name"},- ReferencedSchema: "public",- ReferencedTable: "table2",- ReferencedColumns: []string{"name"},- },- },- },- },- // Create table2 with unique constraint on name - this comes SECOND in the list- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- TableName: "table2",- NewTable: &storepb.TableMetadata{- Name: "table2",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "INT", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: true},- },- Indexes: []*storepb.IndexMetadata{- {- Name: "uq_table2_name",- Expressions: []string{"name"},- Unique: true,- IsConstraint: true,- },- },- },- },- },- },- description: "table2 should be created before table1 even though table1 appears first in the diff, because table1 has FK to table2",- },- {- name: "view_depends_on_tables",- diff: &schema.MetadataDiff{- TableChanges: []*schema.TableDiff{- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- TableName: "users",- NewTable: &storepb.TableMetadata{- Name: "users",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: false},- },- },- },- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- TableName: "orders",- NewTable: &storepb.TableMetadata{- Name: "orders",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "user_id", Type: "INT", Nullable: false},- },- },- },- },- ViewChanges: []*schema.ViewDiff{- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- 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: "public", Table: "users", Column: "id"},- {Schema: "public", Table: "users", Column: "name"},- {Schema: "public", Table: "orders", Column: "id"},- {Schema: "public", 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 that references customers- {- Action: schema.MetadataDiffActionAlter,- SchemaName: "public",- TableName: "orders",- ColumnChanges: []*schema.ColumnDiff{- {- Action: schema.MetadataDiffActionCreate,- NewColumn: &storepb.ColumnMetadata{- Name: "customer_ref",- Type: "INT",- Nullable: true,- },- },- },- },- // Add column to customers table (should be added first)- {- Action: schema.MetadataDiffActionAlter,- SchemaName: "public",- TableName: "customers",- ColumnChanges: []*schema.ColumnDiff{- {- Action: schema.MetadataDiffActionCreate,- NewColumn: &storepb.ColumnMetadata{- Name: "status",- Type: "VARCHAR(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: "public",- TableName: "products",- NewTable: &storepb.TableMetadata{- Name: "products",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: false},- {Name: "price", Type: "DECIMAL(10,2)", Nullable: false},- },- },- },- },- ViewChanges: []*schema.ViewDiff{- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- ViewName: "expensive_products",- NewView: &storepb.ViewMetadata{- Name: "expensive_products",- Definition: "SELECT * FROM products WHERE price > 100",- DependencyColumns: []*storepb.DependencyColumn{- {Schema: "public", Table: "products", Column: "id"},- {Schema: "public", Table: "products", Column: "name"},- {Schema: "public", Table: "products", Column: "price"},- },- },- },- },- MaterializedViewChanges: []*schema.MaterializedViewDiff{- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- MaterializedViewName: "expensive_products_mv",- NewMaterializedView: &storepb.MaterializedViewMetadata{- Name: "expensive_products_mv",- Definition: "SELECT name FROM expensive_products",- DependencyColumns: []*storepb.DependencyColumn{- {Schema: "public", Table: "expensive_products", Column: "name"},- },- },- },- },- },- description: "Materialized view should be created after view 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 "table1_references_table2_name_column":- // table2 should be created before table1 because table1 has FK to table2- table1Index := findStatementIndex(statements, "CREATE TABLE", "table1")- table2Index := findStatementIndex(statements, "CREATE TABLE", "table2")- assert.True(t, table2Index < table1Index,- "table2 should be created before table1 because table1 has FK to table2(name). 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 right order- // This is more complex as we need to check ALTER TABLE statements- customersAlterIndex := findStatementIndex(statements, "ALTER TABLE", "customers")- ordersAlterIndex := findStatementIndex(statements, "ALTER TABLE", "orders")- // Both should exist, actual order depends on topological sorting- 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)- 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: "public",- TableName: "table_a",- NewTable: &storepb.TableMetadata{- Name: "table_a",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "b_id", Type: "INT", Nullable: true},- },- ForeignKeys: []*storepb.ForeignKeyMetadata{- {- Name: "fk_a_to_b",- Columns: []string{"b_id"},- ReferencedSchema: "public",- ReferencedTable: "table_b",- ReferencedColumns: []string{"id"},- },- },- },- },- {- Action: schema.MetadataDiffActionCreate,- SchemaName: "public",- TableName: "table_b",- NewTable: &storepb.TableMetadata{- Name: "table_b",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "a_id", Type: "INT", Nullable: true},- },- ForeignKeys: []*storepb.ForeignKeyMetadata{- {- Name: "fk_b_to_a",- Columns: []string{"a_id"},- ReferencedSchema: "public",- 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- fkCount := 0- for _, stmt := range statements {- if strings.Contains(stmt, "ADD CONSTRAINT") && strings.Contains(stmt, "FOREIGN KEY") {- fkCount++- }- }- assert.Equal(t, 2, fkCount, "Both foreign keys should be added separately")-}--// 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-}--// TestTopologicalOrderDropObjects tests that objects are dropped in the correct dependency order-func TestTopologicalOrderDropObjects(t *testing.T) {- tests := []struct {- name string- diff *schema.MetadataDiff- description string- }{- {- name: "drop_table1_with_fk_before_table2",- diff: &schema.MetadataDiff{- TableChanges: []*schema.TableDiff{- // Drop table1 that has FK to table2 - comes FIRST in list- {- Action: schema.MetadataDiffActionDrop,- SchemaName: "public",- TableName: "table1",- OldTable: &storepb.TableMetadata{- Name: "table1",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: false},- },- ForeignKeys: []*storepb.ForeignKeyMetadata{- {- Name: "fk_table1_name",- Columns: []string{"name"},- ReferencedSchema: "public",- ReferencedTable: "table2",- ReferencedColumns: []string{"name"},- },- },- },- },- // Drop table2 - comes SECOND in list but should be dropped AFTER table1- {- Action: schema.MetadataDiffActionDrop,- SchemaName: "public",- TableName: "table2",- OldTable: &storepb.TableMetadata{- Name: "table2",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "INT", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: false},- },- },- },- },- },- description: "table1 (with FK) should be dropped before table2 (referenced)",- },- {- name: "drop_orders_before_customers",- diff: &schema.MetadataDiff{- TableChanges: []*schema.TableDiff{- // Drop customers table - comes FIRST in list- {- Action: schema.MetadataDiffActionDrop,- SchemaName: "public",- TableName: "customers",- OldTable: &storepb.TableMetadata{- Name: "customers",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "name", Type: "VARCHAR(100)", Nullable: false},- },- },- },- // Drop orders table that has FK to customers - comes SECOND in list- {- Action: schema.MetadataDiffActionDrop,- SchemaName: "public",- TableName: "orders",- OldTable: &storepb.TableMetadata{- Name: "orders",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "SERIAL", Nullable: false},- {Name: "customer_id", Type: "INT", Nullable: false},- },- ForeignKeys: []*storepb.ForeignKeyMetadata{- {- Name: "fk_orders_customer",- Columns: []string{"customer_id"},- ReferencedSchema: "public",- ReferencedTable: "customers",- ReferencedColumns: []string{"id"},- },- },- },- },- },- },- description: "orders (with FK) should be dropped before customers (referenced)",- },- }-- for _, tc := range tests {- t.Run(tc.name, func(t *testing.T) {- migrationSQL, err := generateMigration(tc.diff)- require.NoError(t, err)-- // Parse the SQL into statements- statements := parseStatements(migrationSQL)-- // For DROP operations, find the DROP statements- switch tc.name {- case "drop_table1_with_fk_before_table2":- table1DropIdx := findStatementIndex(statements, "DROP TABLE", "table1")- table2DropIdx := findStatementIndex(statements, "DROP TABLE", "table2")-- assert.NotEqual(t, -1, table1DropIdx, "table1 DROP statement should exist")- assert.NotEqual(t, -1, table2DropIdx, "table2 DROP statement should exist")- assert.Less(t, table1DropIdx, table2DropIdx, "table1 (with FK) should be dropped before table2 (referenced)")- case "drop_orders_before_customers":- ordersDropIdx := findStatementIndex(statements, "DROP TABLE", "orders")- customersDropIdx := findStatementIndex(statements, "DROP TABLE", "customers")-- assert.NotEqual(t, -1, ordersDropIdx, "orders DROP statement should exist")- assert.NotEqual(t, -1, customersDropIdx, "customers DROP statement should exist")- assert.Less(t, ordersDropIdx, customersDropIdx, "orders (with FK) should be dropped before customers (referenced)")- default:- t.Fatalf("Unknown test case: %s", tc.name)- }- })- }-}diff --git a/backend/plugin/schema/pg/trigger_differ.go b/backend/plugin/schema/pg/trigger_differ.godeleted file mode 100644index c85e3b4832..0000000000--- a/backend/plugin/schema/pg/trigger_differ.go+++ /dev/null@@ -1,252 +0,0 @@-package pg--import (- "strings"-- parser "github.com/bytebase/parser/postgresql"-- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/schema"-)--// processStandaloneTriggerChanges analyzes standalone CREATE TRIGGER statement changes-// and adds them to the appropriate table's TriggerChanges-func processStandaloneTriggerChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {- if currentChunks == nil || previousChunks == nil {- return- }-- // Build map of affected tables for efficient lookups- affectedTables := make(map[string]*schema.TableDiff, len(diff.TableChanges))- for _, tableDiff := range diff.TableChanges {- qualifiedTableName := tableDiff.SchemaName + "." + tableDiff.TableName- affectedTables[qualifiedTableName] = tableDiff- }-- // Step 1: Process current triggers (CREATE and MODIFY)- for _, currentChunk := range currentChunks.Triggers {- targetTableName := extractTableNameFromTriggerChunk(currentChunk.ASTNode)- if targetTableName == "" {- // No CREATE TRIGGER statement, might be comment-only chunk- // Handle comment-only chunks separately- if len(currentChunk.CommentStatements) > 0 {- // Extract info from chunk identifier: schema.table.trigger_name- parts := strings.Split(currentChunk.Identifier, ".")- if len(parts) == 3 {- schemaName := parts[0]- tableName := parts[1]- triggerName := parts[2]- targetTableName = schemaName + "." + tableName-- // Add COMMENT changes- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeTrigger,- SchemaName: schemaName,- TableName: targetTableName,- ObjectName: triggerName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- continue- }-- if previousChunk, exists := previousChunks.Triggers[currentChunk.Identifier]; exists {- // Trigger exists in both - check if modified- currentText := getStandaloneTriggerText(currentChunk.ASTNode)- previousText := getStandaloneTriggerText(previousChunk.ASTNode)-- // Extract trigger info once for both trigger and comment changes- schemaName, tableName := parseSchemaAndTableFromQualifiedName(targetTableName)- triggerName := extractTriggerNameFromAST(currentChunk.ASTNode)-- if currentText != previousText {- // Usability check- if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {- continue- }-- // Trigger was modified - use ALTER action (will be converted to CREATE OR REPLACE)- tableDiff := getOrCreateTableDiff(diff, targetTableName, affectedTables)-- tableDiff.TriggerChanges = append(tableDiff.TriggerChanges, &schema.TriggerDiff{- Action: schema.MetadataDiffActionAlter,- SchemaName: schemaName,- TableName: tableName,- TriggerName: triggerName,- OldASTNode: previousChunk.ASTNode,- NewASTNode: currentChunk.ASTNode,- })- }-- // Check for comment changes independently of trigger definition changes- currentCommentText := ""- if len(currentChunk.CommentStatements) > 0 {- currentCommentText = extractCommentTextFromNode(currentChunk.CommentStatements[0])- }- previousCommentText := ""- if len(previousChunk.CommentStatements) > 0 {- previousCommentText = extractCommentTextFromNode(previousChunk.CommentStatements[0])- }-- if currentCommentText != previousCommentText {- if currentCommentText != "" && previousCommentText == "" {- // Comment added- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeTrigger,- SchemaName: schemaName,- TableName: targetTableName,- ObjectName: triggerName,- OldComment: "",- NewComment: currentCommentText,- OldASTNode: nil,- NewASTNode: currentChunk.CommentStatements[0],- })- } else if currentCommentText == "" && previousCommentText != "" {- // Comment removed- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionDrop,- ObjectType: schema.CommentObjectTypeTrigger,- SchemaName: schemaName,- TableName: targetTableName,- ObjectName: triggerName,- OldComment: previousCommentText,- NewComment: "",- OldASTNode: previousChunk.CommentStatements[0],- NewASTNode: nil,- })- } else if currentCommentText != "" && previousCommentText != "" {- // Comment modified- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionAlter,- ObjectType: schema.CommentObjectTypeTrigger,- SchemaName: schemaName,- TableName: targetTableName,- ObjectName: triggerName,- OldComment: previousCommentText,- NewComment: currentCommentText,- OldASTNode: previousChunk.CommentStatements[0],- NewASTNode: currentChunk.CommentStatements[0],- })- }- }- } else {- // New trigger- tableDiff := getOrCreateTableDiff(diff, targetTableName, affectedTables)-- schemaName, tableName := parseSchemaAndTableFromQualifiedName(targetTableName)- triggerName := extractTriggerNameFromAST(currentChunk.ASTNode)-- tableDiff.TriggerChanges = append(tableDiff.TriggerChanges, &schema.TriggerDiff{- Action: schema.MetadataDiffActionCreate,- SchemaName: schemaName,- TableName: tableName,- TriggerName: triggerName,- NewASTNode: currentChunk.ASTNode,- })-- // Add comments- if len(currentChunk.CommentStatements) > 0 {- for _, commentNode := range currentChunk.CommentStatements {- commentText := extractCommentTextFromNode(commentNode)- diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{- Action: schema.MetadataDiffActionCreate,- ObjectType: schema.CommentObjectTypeTrigger,- SchemaName: schemaName,- TableName: targetTableName,- ObjectName: triggerName,- OldComment: "",- NewComment: commentText,- OldASTNode: nil,- NewASTNode: commentNode,- })- }- }- }- }-- // Step 2: Process previous triggers (DROP)- for triggerName, previousChunk := range previousChunks.Triggers {- if _, exists := currentChunks.Triggers[triggerName]; !exists {- // Trigger was dropped- targetTableName := extractTableNameFromTriggerChunk(previousChunk.ASTNode)- if targetTableName == "" {- // No CREATE TRIGGER statement, skip (comment-only chunks don't need DROP)- continue- }-- tableDiff := getOrCreateTableDiff(diff, targetTableName, affectedTables)-- schemaName, tableName := parseSchemaAndTableFromQualifiedName(targetTableName)- triggerNameStr := extractTriggerNameFromAST(previousChunk.ASTNode)-- tableDiff.TriggerChanges = append(tableDiff.TriggerChanges, &schema.TriggerDiff{- Action: schema.MetadataDiffActionDrop,- SchemaName: schemaName,- TableName: tableName,- TriggerName: triggerNameStr,- OldASTNode: previousChunk.ASTNode,- })- }- }-}--// extractTableNameFromTriggerChunk extracts the table name from a trigger chunk's AST node-// Returns empty string if the AST node is not a CREATE TRIGGER statement or if extraction fails-func extractTableNameFromTriggerChunk(astNode any) string {- if astNode == nil {- return ""- }- triggerCtx, ok := astNode.(*parser.CreatetrigstmtContext)- if !ok {- return ""- }- return extractTableNameFromTrigger(triggerCtx)-}--// getStandaloneTriggerText returns the text representation of a CREATE TRIGGER statement-func getStandaloneTriggerText(astNode any) string {- triggerStmt, ok := astNode.(*parser.CreatetrigstmtContext)- if !ok || triggerStmt == nil {- return ""- }-- // Get text from token stream- if parser := triggerStmt.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := triggerStmt.GetStart()- stop := triggerStmt.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }-- return triggerStmt.GetText()-}--// extractTriggerNameFromAST extracts trigger name from CREATE TRIGGER AST-func extractTriggerNameFromAST(astNode any) string {- triggerStmt, ok := astNode.(*parser.CreatetrigstmtContext)- if !ok || triggerStmt == nil || triggerStmt.Name() == nil {- return ""- }- return pgparser.NormalizePostgreSQLName(triggerStmt.Name())-}--// parseSchemaAndTableFromQualifiedName parses "schema.table" into separate parts-func parseSchemaAndTableFromQualifiedName(qualifiedName string) (schemaName, tableName string) {- parts := strings.Split(qualifiedName, ".")- if len(parts) == 2 {- return parts[0], parts[1]- }- return "public", qualifiedName-}diff --git a/backend/plugin/schema/pg/trigger_drop_dependency_test.go b/backend/plugin/schema/pg/trigger_drop_dependency_test.godeleted file mode 100644index 21b941a38c..0000000000--- a/backend/plugin/schema/pg/trigger_drop_dependency_test.go+++ /dev/null@@ -1,326 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-)--func TestDropTriggerDependencyOrder(t *testing.T) {- // Previous SDL with tables, functions, and triggers- previousSDL := `- CREATE TABLE test_products (- id SERIAL PRIMARY KEY,- name VARCHAR(100),- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE TABLE test_orders (- id SERIAL PRIMARY KEY,- product_id INTEGER,- quantity INTEGER,- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE TABLE audit_log (- id SERIAL PRIMARY KEY,- table_name VARCHAR(50),- operation VARCHAR(10),- changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE FUNCTION update_timestamp_trigger_func()- RETURNS TRIGGER AS $$- BEGIN- NEW.updated_at = CURRENT_TIMESTAMP;- RETURN NEW;- END;- $$ LANGUAGE plpgsql;-- CREATE FUNCTION audit_trigger_func()- RETURNS TRIGGER AS $$- BEGIN- INSERT INTO audit_log (table_name, operation)- VALUES (TG_TABLE_NAME, TG_OP);- RETURN NEW;- END;- $$ LANGUAGE plpgsql;-- CREATE TRIGGER update_timestamp_trigger- BEFORE UPDATE ON test_products- FOR EACH ROW EXECUTE FUNCTION update_timestamp_trigger_func();-- CREATE TRIGGER audit_insert_trigger- AFTER INSERT ON test_products- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER audit_update_trigger- AFTER UPDATE ON test_products- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER audit_delete_trigger- AFTER DELETE ON test_products- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER update_timestamp_trigger- BEFORE UPDATE ON test_orders- FOR EACH ROW EXECUTE FUNCTION update_timestamp_trigger_func();-- CREATE TRIGGER audit_insert_trigger- AFTER INSERT ON test_orders- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER audit_delete_trigger- AFTER DELETE ON test_orders- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();- `-- // Current SDL is empty - drop everything- currentSDL := ``-- // Generate diff and migration (AST-only mode)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Debug: print diff information- t.Logf("Number of table changes: %d", len(diff.TableChanges))- for i, tableDiff := range diff.TableChanges {- t.Logf("Table %d: %s.%s, Action=%s, TriggerChanges=%d",- i, tableDiff.SchemaName, tableDiff.TableName, tableDiff.Action, len(tableDiff.TriggerChanges))- for j, triggerDiff := range tableDiff.TriggerChanges {- t.Logf(" Trigger %d: %s, Action=%s", j, triggerDiff.TriggerName, triggerDiff.Action)- }- }-- migration, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Migration:\n%s", migration)-- // Find positions of DROP statements- dropTriggerIdx := strings.Index(migration, "DROP TRIGGER")- dropFunctionIdx := strings.Index(migration, "DROP FUNCTION")- dropTableIdx := strings.Index(migration, "DROP TABLE")-- t.Logf("Indices: dropTrigger=%d, dropFunction=%d, dropTable=%d", dropTriggerIdx, dropFunctionIdx, dropTableIdx)-- // Verify DROP order: triggers must be dropped before functions- // Functions must be dropped before tables (or at least before tables that don't depend on them)- assert.NotEqual(t, -1, dropTriggerIdx, "Should have DROP TRIGGER statements")- assert.NotEqual(t, -1, dropFunctionIdx, "Should have DROP FUNCTION statements")- assert.NotEqual(t, -1, dropTableIdx, "Should have DROP TABLE statements")-- // Critical: triggers must be dropped before functions they depend on- assert.Less(t, dropTriggerIdx, dropFunctionIdx, "DROP TRIGGER must come before DROP FUNCTION to avoid dependency errors")-- // Functions should be dropped before or after tables (depends on implementation)- // But triggers MUST be before functions-}--func TestCreateTriggerDependencyOrder(t *testing.T) {- // Previous SDL is empty- previousSDL := ``-- // Current SDL with tables, functions, and triggers- currentSDL := `- CREATE TABLE test_products (- id SERIAL PRIMARY KEY,- name VARCHAR(100),- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE TABLE test_orders (- id SERIAL PRIMARY KEY,- product_id INTEGER,- quantity INTEGER,- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE TABLE audit_log (- id SERIAL PRIMARY KEY,- table_name VARCHAR(50),- operation VARCHAR(10),- changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE FUNCTION update_timestamp_trigger_func()- RETURNS TRIGGER AS $$- BEGIN- NEW.updated_at = CURRENT_TIMESTAMP;- RETURN NEW;- END;- $$ LANGUAGE plpgsql;-- CREATE FUNCTION audit_trigger_func()- RETURNS TRIGGER AS $$- BEGIN- INSERT INTO audit_log (table_name, operation)- VALUES (TG_TABLE_NAME, TG_OP);- RETURN NEW;- END;- $$ LANGUAGE plpgsql;-- CREATE TRIGGER update_timestamp_trigger- BEFORE UPDATE ON test_products- FOR EACH ROW EXECUTE FUNCTION update_timestamp_trigger_func();-- CREATE TRIGGER audit_insert_trigger- AFTER INSERT ON test_products- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER audit_update_trigger- AFTER UPDATE ON test_products- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER audit_delete_trigger- AFTER DELETE ON test_products- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER update_timestamp_trigger- BEFORE UPDATE ON test_orders- FOR EACH ROW EXECUTE FUNCTION update_timestamp_trigger_func();-- CREATE TRIGGER audit_insert_trigger- AFTER INSERT ON test_orders- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();-- CREATE TRIGGER audit_delete_trigger- AFTER DELETE ON test_orders- FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();- `-- // Generate diff and migration (AST-only mode)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Debug: print diff information- t.Logf("Number of table changes: %d", len(diff.TableChanges))- for i, tableDiff := range diff.TableChanges {- t.Logf("Table %d: %s.%s, Action=%s, TriggerChanges=%d",- i, tableDiff.SchemaName, tableDiff.TableName, tableDiff.Action, len(tableDiff.TriggerChanges))- for j, triggerDiff := range tableDiff.TriggerChanges {- t.Logf(" Trigger %d: %s, Action=%s", j, triggerDiff.TriggerName, triggerDiff.Action)- }- }-- migration, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Migration:\n%s", migration)-- // Find positions of CREATE statements- createTableIdx := strings.Index(migration, "CREATE TABLE")- createFunctionIdx := strings.Index(migration, "CREATE FUNCTION") // Note: Not "CREATE OR REPLACE" for new functions- createTriggerIdx := strings.Index(migration, "CREATE OR REPLACE TRIGGER")-- t.Logf("Indices: createTable=%d, createFunction=%d, createTrigger=%d",- createTableIdx, createFunctionIdx, createTriggerIdx)-- // Verify CREATE order: tables must be created before functions and triggers- assert.NotEqual(t, -1, createTableIdx, "Should have CREATE TABLE statements")- assert.NotEqual(t, -1, createFunctionIdx, "Should have CREATE FUNCTION statements")- assert.NotEqual(t, -1, createTriggerIdx, "Should have CREATE TRIGGER statements")-- // Critical: tables must be created before triggers (triggers reference tables)- assert.Less(t, createTableIdx, createTriggerIdx, "CREATE TABLE must come before CREATE TRIGGER")-- // Functions must be created before triggers (triggers call functions)- assert.Less(t, createFunctionIdx, createTriggerIdx, "CREATE FUNCTION must come before CREATE TRIGGER to avoid dependency errors")-- // Verify no duplicate triggers- firstTrigger := strings.Index(migration, "CREATE OR REPLACE TRIGGER")- assert.NotEqual(t, -1, firstTrigger, "Should have at least one CREATE TRIGGER")-- // Count total triggers (should match the number we expect)- triggerCount := strings.Count(migration, "CREATE OR REPLACE TRIGGER")- t.Logf("Total triggers in migration: %d", triggerCount)- assert.Equal(t, 7, triggerCount, "Should have exactly 7 triggers (4 from test_products, 3 from test_orders)")-}--func TestSameTriggerNameOnDifferentTables(t *testing.T) {- // Test that triggers with the same name on different tables are handled correctly- // In PostgreSQL, trigger names are table-scoped, not schema-scoped- // So "public.table1.trigger_name" and "public.table2.trigger_name" should be different-- currentSDL := `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE TABLE orders (- id SERIAL PRIMARY KEY,- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- );-- CREATE FUNCTION update_timestamp()- RETURNS TRIGGER AS $$- BEGIN- NEW.updated_at = CURRENT_TIMESTAMP;- RETURN NEW;- END;- $$ LANGUAGE plpgsql;-- -- Same trigger name on different tables- CREATE TRIGGER update_timestamp_trigger- BEFORE UPDATE ON users- FOR EACH ROW EXECUTE FUNCTION update_timestamp();-- CREATE TRIGGER update_timestamp_trigger- BEFORE UPDATE ON orders- FOR EACH ROW EXECUTE FUNCTION update_timestamp();- `-- previousSDL := ``-- // Generate diff and migration (AST-only mode)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Debug: print diff information- t.Logf("Number of table changes: %d", len(diff.TableChanges))-- totalTriggers := 0- for _, tableDiff := range diff.TableChanges {- t.Logf("Table: %s.%s, Action=%s, TriggerChanges=%d",- tableDiff.SchemaName, tableDiff.TableName, tableDiff.Action, len(tableDiff.TriggerChanges))- for _, triggerDiff := range tableDiff.TriggerChanges {- t.Logf(" Trigger: %s, Action=%s", triggerDiff.TriggerName, triggerDiff.Action)- totalTriggers++- }- }-- // Should have 2 triggers (one for each table)- assert.Equal(t, 2, totalTriggers, "Should have 2 triggers (one for each table)")-- migration, err := generateMigration(diff)- require.NoError(t, err)-- t.Logf("Migration:\n%s", migration)-- // Verify both triggers are in the migration- assert.Contains(t, migration, "ON users", "Should have trigger on users table")- assert.Contains(t, migration, "ON orders", "Should have trigger on orders table")-- // Count how many times the trigger is created- // Should be 2 (once for users, once for orders)- triggerCount := 0- if assert.Contains(t, migration, "CREATE OR REPLACE TRIGGER update_timestamp_trigger") {- // Count occurrences manually by checking both table references- hasUsersTrigger := assert.Contains(t, migration, "update_timestamp_trigger") &&- assert.Contains(t, migration, "ON users")- hasOrdersTrigger := assert.Contains(t, migration, "update_timestamp_trigger") &&- assert.Contains(t, migration, "ON orders")-- if hasUsersTrigger {- triggerCount++- }- if hasOrdersTrigger {- triggerCount++- }- }-- assert.Equal(t, 2, triggerCount, "Should have 2 CREATE TRIGGER statements (one for each table)")-}diff --git a/backend/plugin/schema/pg/trigger_sdl_diff_test.go b/backend/plugin/schema/pg/trigger_sdl_diff_test.godeleted file mode 100644index 620ae1e0fe..0000000000--- a/backend/plugin/schema/pg/trigger_sdl_diff_test.go+++ /dev/null@@ -1,739 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestTriggerDiffStructure(t *testing.T) {- // Test that TriggerDiff has all required fields for SDL processing- currentSDL := `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(100)- );-- CREATE FUNCTION audit_log() RETURNS TRIGGER AS $$- BEGIN- RAISE NOTICE 'Audit';- RETURN NEW;- END;- $$ LANGUAGE plpgsql;-- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit_log();- `-- previousSDL := ""-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.Len(t, diff.TableChanges, 1, "Should have one table change")-- tableDiff := diff.TableChanges[0]- require.Len(t, tableDiff.TriggerChanges, 1, "Should have one trigger change")-- triggerDiff := tableDiff.TriggerChanges[0]- assert.Equal(t, schema.MetadataDiffActionCreate, triggerDiff.Action)- assert.NotNil(t, triggerDiff.NewASTNode, "Should have AST node")- // These fields should exist after we update the structure- // assert.Equal(t, "public", triggerDiff.SchemaName)- // assert.Equal(t, "users", triggerDiff.TableName)- // assert.Equal(t, "audit_trigger", triggerDiff.TriggerName)-}--func TestCreateTriggerExtraction(t *testing.T) {- tests := []struct {- name string- sql string- expectedTriggers int- }{- {- name: "Simple trigger",- sql: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- expectedTriggers: 1,- },- {- name: "Multiple triggers on same table",- sql: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- CREATE TRIGGER t2 BEFORE UPDATE ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- expectedTriggers: 2,- },- {- name: "Trigger with schema-qualified table",- sql: `- CREATE SCHEMA app;- CREATE TABLE app.users (id SERIAL PRIMARY KEY);- CREATE FUNCTION app.f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON app.users FOR EACH ROW EXECUTE FUNCTION app.f();- `,- expectedTriggers: 1,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.sql, "", nil)- require.NoError(t, err)-- totalTriggers := 0- for _, tableDiff := range diff.TableChanges {- totalTriggers += len(tableDiff.TriggerChanges)- }-- assert.Equal(t, tt.expectedTriggers, totalTriggers)- })- }-}--func TestTriggerCommentExtraction(t *testing.T) {- sql := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- COMMENT ON TRIGGER audit_trigger ON users IS 'Audit log trigger';- `-- diff, err := GetSDLDiff(sql, "", nil)- require.NoError(t, err)-- // Check that comment change exists- found := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeTrigger &&- commentDiff.ObjectName == "audit_trigger" &&- commentDiff.NewComment == "Audit log trigger" {- found = true- break- }- }- assert.True(t, found, "Should have trigger comment")-}--func TestDropTriggerMigration(t *testing.T) {- previousSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- `- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- `-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)-- assert.Contains(t, migration, "DROP TRIGGER")- assert.Contains(t, migration, "audit_trigger")- assert.Contains(t, migration, "ON")- assert.Contains(t, migration, "users")-}--func TestCreateTriggerMigrationWithDependencyOrder(t *testing.T) {- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION audit_log() RETURNS TRIGGER AS $$- BEGIN- RAISE NOTICE 'Audit';- RETURN NEW;- END;- $$ LANGUAGE plpgsql;-- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit_log();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)-- // Print migration for debugging- t.Logf("Migration:\n%s", migration)-- // Verify CREATE OR REPLACE TRIGGER is in migration- assert.Contains(t, migration, "CREATE OR REPLACE TRIGGER")- assert.Contains(t, migration, "audit_trigger")-- // Verify dependency order: Both TABLE and FUNCTION must be created before TRIGGER- // The order between TABLE and FUNCTION doesn't matter - either is valid in PostgreSQL- // since trigger functions don't reference the table until the trigger is created- tableIdx := strings.Index(migration, "CREATE TABLE")- functionIdx := strings.Index(migration, "CREATE FUNCTION audit_log")- triggerIdx := strings.Index(migration, "CREATE OR REPLACE TRIGGER audit_trigger")-- t.Logf("Indices: table=%d, function=%d, trigger=%d", tableIdx, functionIdx, triggerIdx)-- // Both table and function must exist before trigger- assert.Less(t, tableIdx, triggerIdx, "Table must be created before trigger")- assert.Less(t, functionIdx, triggerIdx, "Function must be created before trigger")-}--func TestTriggerCommentMigration(t *testing.T) {- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- COMMENT ON TRIGGER audit_trigger ON users IS 'Audit log trigger';- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)-- assert.Contains(t, migration, "COMMENT ON TRIGGER")- assert.Contains(t, migration, "audit_trigger")- assert.Contains(t, migration, "Audit log trigger")-}--func TestStandaloneTriggerDiff(t *testing.T) {- tests := []struct {- name string- previousUserSDL string- currentSDL string- expectedTriggerDiff int- expectedActions []schema.MetadataDiffAction- }{- {- name: "Create new trigger",- previousUserSDL: "",- currentSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- expectedTriggerDiff: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Drop trigger",- previousUserSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- currentSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- `,- expectedTriggerDiff: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},- },- {- name: "Modify trigger (CREATE OR REPLACE)",- previousUserSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- currentSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT OR UPDATE ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- expectedTriggerDiff: 1, // ALTER (CREATE OR REPLACE)- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionAlter},- },- {- name: "No changes to trigger",- previousUserSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- currentSDL: `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- `,- expectedTriggerDiff: 0,- expectedActions: []schema.MetadataDiffAction{},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousUserSDL, nil)- require.NoError(t, err)-- totalTriggers := 0- var actions []schema.MetadataDiffAction- for _, tableDiff := range diff.TableChanges {- for _, triggerDiff := range tableDiff.TriggerChanges {- totalTriggers++- actions = append(actions, triggerDiff.Action)- }- }-- assert.Equal(t, tt.expectedTriggerDiff, totalTriggers)- if len(tt.expectedActions) == 0 {- assert.Empty(t, actions)- } else {- assert.Equal(t, tt.expectedActions, actions)- }- })- }-}--func TestTriggerSDLIntegration(t *testing.T) {- t.Run("Multiple triggers on same table", func(t *testing.T) {- currentSDL := `- CREATE TABLE orders (id SERIAL PRIMARY KEY, status VARCHAR(20));- CREATE FUNCTION log_insert() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE FUNCTION log_update() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;-- CREATE TRIGGER insert_trigger AFTER INSERT ON orders FOR EACH ROW EXECUTE FUNCTION log_insert();- CREATE TRIGGER update_trigger BEFORE UPDATE ON orders FOR EACH ROW EXECUTE FUNCTION log_update();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- totalTriggers := 0- for _, tableDiff := range diff.TableChanges {- totalTriggers += len(tableDiff.TriggerChanges)- }- assert.Equal(t, 2, totalTriggers, "Should have 2 triggers")-- migration, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migration, "insert_trigger")- assert.Contains(t, migration, "update_trigger")- })-- t.Run("Trigger with schema-qualified table", func(t *testing.T) {- currentSDL := `- CREATE SCHEMA app;- CREATE TABLE app.events (id SERIAL PRIMARY KEY);- CREATE FUNCTION app.notify() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER event_trigger AFTER INSERT ON app.events FOR EACH ROW EXECUTE FUNCTION app.notify();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- found := false- for _, tableDiff := range diff.TableChanges {- if tableDiff.SchemaName == "app" && tableDiff.TableName == "events" {- assert.Len(t, tableDiff.TriggerChanges, 1)- found = true- }- }- assert.True(t, found, "Should find trigger on app.events")- })-- t.Run("Trigger with WHEN condition", func(t *testing.T) {- currentSDL := `- CREATE TABLE products (id SERIAL PRIMARY KEY, price NUMERIC);- CREATE FUNCTION check_price() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER price_check- BEFORE UPDATE ON products- FOR EACH ROW- WHEN (NEW.price > 1000)- EXECUTE FUNCTION check_price();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migration, "WHEN")- assert.Contains(t, migration, "NEW.price > 1000")- })-- t.Run("Trigger modification preserves other triggers", func(t *testing.T) {- previousSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- CREATE TRIGGER t2 AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION f();- `- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT OR UPDATE ON users FOR EACH ROW EXECUTE FUNCTION f();- CREATE TRIGGER t2 AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION f();- `-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Should have 1 change for t1 (ALTER using CREATE OR REPLACE), none for t2- totalChanges := 0- alterChanges := 0- for _, tableDiff := range diff.TableChanges {- for _, triggerDiff := range tableDiff.TriggerChanges {- totalChanges++- if triggerDiff.Action == schema.MetadataDiffActionAlter {- alterChanges++- }- }- }- assert.Equal(t, 1, totalChanges, "Should only modify t1 (ALTER)")- assert.Equal(t, 1, alterChanges, "Should use ALTER action (CREATE OR REPLACE)")- })-- t.Run("Trigger with multiple events", func(t *testing.T) {- currentSDL := `- CREATE TABLE audit_log (id SERIAL PRIMARY KEY);- CREATE FUNCTION log_changes() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER multi_event_trigger- AFTER INSERT OR UPDATE OR DELETE ON audit_log- FOR EACH ROW EXECUTE FUNCTION log_changes();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migration, "CREATE OR REPLACE TRIGGER")- assert.Contains(t, migration, "INSERT OR UPDATE OR DELETE")- })-- t.Run("Trigger with FOR EACH STATEMENT", func(t *testing.T) {- currentSDL := `- CREATE TABLE statements (id SERIAL PRIMARY KEY);- CREATE FUNCTION stmt_trigger() RETURNS TRIGGER AS $$ BEGIN RETURN NULL; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER statement_level_trigger- AFTER INSERT ON statements- FOR EACH STATEMENT EXECUTE FUNCTION stmt_trigger();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migration, "FOR EACH STATEMENT")- })-- t.Run("Multiple comments on triggers", func(t *testing.T) {- currentSDL := `- CREATE TABLE events (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON events FOR EACH ROW EXECUTE FUNCTION f();- CREATE TRIGGER t2 AFTER UPDATE ON events FOR EACH ROW EXECUTE FUNCTION f();- COMMENT ON TRIGGER t1 ON events IS 'Insert trigger';- COMMENT ON TRIGGER t2 ON events IS 'Update trigger';- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- // Check both comments are extracted- triggerCommentCount := 0- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeTrigger {- triggerCommentCount++- }- }- assert.Equal(t, 2, triggerCommentCount, "Should have 2 trigger comments")- })-- t.Run("Drop trigger removes comment", func(t *testing.T) {- previousSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER t1 AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION f();- COMMENT ON TRIGGER t1 ON users IS 'My trigger';- `- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION f() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- `-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Should have trigger drop- found := false- for _, tableDiff := range diff.TableChanges {- for _, triggerDiff := range tableDiff.TriggerChanges {- if triggerDiff.Action == schema.MetadataDiffActionDrop {- found = true- }- }- }- assert.True(t, found, "Should have trigger drop action")- })-- t.Run("Trigger referencing columns", func(t *testing.T) {- currentSDL := `- CREATE TABLE inventory (id SERIAL PRIMARY KEY, stock INT);- CREATE FUNCTION check_stock() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER stock_check- BEFORE UPDATE OF stock ON inventory- FOR EACH ROW EXECUTE FUNCTION check_stock();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migration, "UPDATE OF stock")- })-- t.Run("INSTEAD OF trigger on view", func(t *testing.T) {- currentSDL := `- CREATE TABLE base_table (id SERIAL PRIMARY KEY);- CREATE VIEW my_view AS SELECT * FROM base_table;- CREATE FUNCTION instead_trigger() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER view_trigger- INSTEAD OF INSERT ON my_view- FOR EACH ROW EXECUTE FUNCTION instead_trigger();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migration, "INSTEAD OF INSERT")- })-- t.Run("CREATE OR REPLACE conversion test", func(t *testing.T) {- // Test that CREATE TRIGGER is converted to CREATE OR REPLACE TRIGGER- currentSDL := `- CREATE TABLE test_table (id SERIAL PRIMARY KEY);- CREATE FUNCTION test_func() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER test_trigger- AFTER INSERT ON test_table- FOR EACH ROW EXECUTE FUNCTION test_func();- `-- diff, err := GetSDLDiff(currentSDL, "", nil)- require.NoError(t, err)-- migration, err := generateMigration(diff)- require.NoError(t, err)-- // Should contain CREATE OR REPLACE TRIGGER, not just CREATE TRIGGER- assert.Contains(t, migration, "CREATE OR REPLACE TRIGGER")- assert.NotContains(t, migration, "CREATE TRIGGER test_trigger\n")- })-- t.Run("Trigger modification uses CREATE OR REPLACE", func(t *testing.T) {- // Test that modifying a trigger uses CREATE OR REPLACE instead of DROP + CREATE- previousSDL := `- CREATE TABLE test_table (id SERIAL PRIMARY KEY);- CREATE FUNCTION test_func() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER test_trigger- AFTER INSERT ON test_table- FOR EACH ROW EXECUTE FUNCTION test_func();- `-- currentSDL := `- CREATE TABLE test_table (id SERIAL PRIMARY KEY);- CREATE FUNCTION test_func() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER test_trigger- AFTER INSERT OR UPDATE ON test_table- FOR EACH ROW EXECUTE FUNCTION test_func();- `-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Should have 1 ALTER action (not DROP + CREATE)- var alterCount int- for _, tableDiff := range diff.TableChanges {- for _, triggerDiff := range tableDiff.TriggerChanges {- if triggerDiff.Action == schema.MetadataDiffActionAlter {- alterCount++- }- }- }- assert.Equal(t, 1, alterCount, "Should use ALTER action")-- migration, err := generateMigration(diff)- require.NoError(t, err)-- // Should use CREATE OR REPLACE- assert.Contains(t, migration, "CREATE OR REPLACE TRIGGER")- // Should NOT contain DROP TRIGGER- assert.NotContains(t, migration, "DROP TRIGGER")- })-- t.Run("Modify trigger comment only", func(t *testing.T) {- // Test that modifying only the trigger comment doesn't trigger ALTER on the trigger itself- previousSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION audit() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit();- COMMENT ON TRIGGER audit_trigger ON users IS 'Original comment';- `-- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION audit() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit();- COMMENT ON TRIGGER audit_trigger ON users IS 'Updated comment';- `-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Trigger itself should NOT have ALTER action (definition unchanged)- triggerAlterCount := 0- for _, tableDiff := range diff.TableChanges {- for _, triggerDiff := range tableDiff.TriggerChanges {- if triggerDiff.Action == schema.MetadataDiffActionAlter {- triggerAlterCount++- }- }- }- assert.Equal(t, 0, triggerAlterCount, "Trigger should not have ALTER action when only comment changes")-- // Should have comment change- commentChanged := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeTrigger &&- commentDiff.ObjectName == "audit_trigger" &&- commentDiff.OldComment == "Original comment" &&- commentDiff.NewComment == "Updated comment" {- commentChanged = true- }- }- assert.True(t, commentChanged, "Should have trigger comment change")-- // Migration should only contain COMMENT statement, not CREATE OR REPLACE TRIGGER- migration, err := generateMigration(diff)- require.NoError(t, err)-- assert.Contains(t, migration, "COMMENT ON TRIGGER", "Should have COMMENT ON TRIGGER")- assert.Contains(t, migration, "Updated comment", "Should have new comment text")- assert.NotContains(t, migration, "CREATE OR REPLACE TRIGGER", "Should NOT recreate trigger when only comment changes")- })-- t.Run("Add comment to trigger without comment", func(t *testing.T) {- // Test adding a comment to a trigger that previously had no comment- previousSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION audit() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit();- `-- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION audit() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit();- COMMENT ON TRIGGER audit_trigger ON users IS 'New comment';- `-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Trigger should NOT have ALTER action- triggerAlterCount := 0- for _, tableDiff := range diff.TableChanges {- for _, triggerDiff := range tableDiff.TriggerChanges {- if triggerDiff.Action == schema.MetadataDiffActionAlter {- triggerAlterCount++- }- }- }- assert.Equal(t, 0, triggerAlterCount, "Trigger should not have ALTER action when adding comment")-- // Should have comment addition- commentAdded := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeTrigger &&- commentDiff.ObjectName == "audit_trigger" &&- commentDiff.Action == schema.MetadataDiffActionCreate &&- commentDiff.NewComment == "New comment" {- commentAdded = true- }- }- assert.True(t, commentAdded, "Should have comment addition")-- migration, err := generateMigration(diff)- require.NoError(t, err)-- assert.Contains(t, migration, "COMMENT ON TRIGGER", "Should have COMMENT ON TRIGGER")- assert.NotContains(t, migration, "CREATE OR REPLACE TRIGGER", "Should NOT recreate trigger")- })-- t.Run("Remove trigger comment", func(t *testing.T) {- // Test removing a comment from a trigger- previousSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION audit() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit();- COMMENT ON TRIGGER audit_trigger ON users IS 'Old comment';- `-- currentSDL := `- CREATE TABLE users (id SERIAL PRIMARY KEY);- CREATE FUNCTION audit() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;- CREATE TRIGGER audit_trigger- AFTER INSERT ON users- FOR EACH ROW EXECUTE FUNCTION audit();- `-- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)-- // Trigger should NOT have ALTER action- triggerAlterCount := 0- for _, tableDiff := range diff.TableChanges {- for _, triggerDiff := range tableDiff.TriggerChanges {- if triggerDiff.Action == schema.MetadataDiffActionAlter {- triggerAlterCount++- }- }- }- assert.Equal(t, 0, triggerAlterCount, "Trigger should not have ALTER action when removing comment")-- // Should have comment drop- commentDropped := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeTrigger &&- commentDiff.ObjectName == "audit_trigger" &&- commentDiff.Action == schema.MetadataDiffActionDrop {- commentDropped = true- }- }- assert.True(t, commentDropped, "Should have comment drop action")-- migration, err := generateMigration(diff)- require.NoError(t, err)-- assert.Contains(t, migration, "COMMENT ON TRIGGER", "Should have COMMENT ON TRIGGER")- assert.Contains(t, migration, "NULL", "Should set comment to NULL")- assert.NotContains(t, migration, "CREATE OR REPLACE TRIGGER", "Should NOT recreate trigger")- })-}diff --git a/backend/plugin/schema/pg/usability_check_test.go b/backend/plugin/schema/pg/usability_check_test.godeleted file mode 100644index 66378b0a05..0000000000--- a/backend/plugin/schema/pg/usability_check_test.go+++ /dev/null@@ -1,646 +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"-)--// TestUsabilityCheck_TableScenarios tests all usability check scenarios for tables-func TestUsabilityCheck_TableScenarios(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- currentSchema *model.DatabaseMetadata- expectTableDiff bool- expectCommentDiff bool- description string- }{- {- name: "scenario_1_same_structure_same_comment_should_skip_both",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'User information table';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text NOT NULL-);--COMMENT ON TABLE "public"."users" IS 'User table description';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Comment: "User information table",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectTableDiff: false,- expectCommentDiff: false,- description: "Structure matches database, comment matches database → skip both diffs",- },- {- name: "scenario_2_same_structure_different_comment_should_skip_structure_only",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Updated user table description';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text NOT NULL-);--COMMENT ON TABLE "public"."users" IS 'Old user table description';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Comment: "User information table",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectTableDiff: false,- expectCommentDiff: true,- description: "Structure matches database → skip structure diff, comment differs → generate comment diff",- },- {- name: "scenario_3_different_structure_same_comment_should_skip_comment_only",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "email" varchar(255)-);--COMMENT ON TABLE "public"."users" IS 'User information table';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Old user table';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Comment: "User information table",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectTableDiff: true,- expectCommentDiff: false,- description: "Structure differs → generate structure diff, comment matches database → skip comment diff",- },- {- name: "scenario_4_different_structure_different_comment_should_generate_both",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text,- "email" varchar(255)-);--COMMENT ON TABLE "public"."users" IS 'Updated user information';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Old user table';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Comment: "User information table",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectTableDiff: true,- expectCommentDiff: true,- description: "Structure differs → generate structure diff, comment differs → generate comment diff",- },- {- name: "add_comment_to_existing_table",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'Newly added comment';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Comment: "",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectTableDiff: false,- expectCommentDiff: true,- description: "Adding comment to table without comment in database → skip structure, generate comment diff",- },- {- name: "remove_comment_from_table",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--COMMENT ON TABLE "public"."users" IS 'To be removed';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Comment: "User table",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectTableDiff: false,- expectCommentDiff: true,- description: "Removing comment while structure matches database → skip structure, generate comment diff",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- t.Logf("Description: %s", tt.description)-- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, tt.currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // Check table diff- hasTableDiff := len(diff.TableChanges) > 0- require.Equal(t, tt.expectTableDiff, hasTableDiff,- "Expected table diff=%v but got=%v. TableChanges count: %d",- tt.expectTableDiff, hasTableDiff, len(diff.TableChanges))-- // Check comment diff- hasCommentDiff := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeTable &&- commentDiff.ObjectName == "users" {- hasCommentDiff = true- break- }- }- require.Equal(t, tt.expectCommentDiff, hasCommentDiff,- "Expected comment diff=%v but got=%v. CommentChanges count: %d",- tt.expectCommentDiff, hasCommentDiff, len(diff.CommentChanges))-- if t.Failed() {- t.Log("Full diff result:")- t.Logf(" TableChanges: %d", len(diff.TableChanges))- for i, tc := range diff.TableChanges {- t.Logf(" [%d] %s.%s (Action: %s)", i, tc.SchemaName, tc.TableName, tc.Action)- }- t.Logf(" CommentChanges: %d", len(diff.CommentChanges))- for i, cc := range diff.CommentChanges {- t.Logf(" [%d] %s.%s (Type: %s, Action: %s)",- i, cc.SchemaName, cc.ObjectName, cc.ObjectType, cc.Action)- }- }- })- }-}--// TestUsabilityCheck_ViewScenarios tests usability check for views-func TestUsabilityCheck_ViewScenarios(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- currentSchema *model.DatabaseMetadata- expectViewDiff bool- expectCommentDiff bool- description string- }{- {- name: "view_same_definition_same_comment_skip_both",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--CREATE VIEW "public"."active_users" AS SELECT * FROM users WHERE id > 0;--COMMENT ON VIEW "public"."active_users" IS 'View of active users';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--CREATE VIEW "public"."active_users" AS SELECT * FROM users WHERE id < 0;--COMMENT ON VIEW "public"."active_users" IS 'View of all users';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- Views: []*storepb.ViewMetadata{- {- Name: "active_users",- Definition: "SELECT * FROM users WHERE id > 0",- Comment: "View of active users",- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectViewDiff: false,- expectCommentDiff: false,- description: "View definition and comment match database → skip both",- },- {- name: "view_same_definition_different_comment_skip_view_only",- currentSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--CREATE VIEW "public"."active_users" AS SELECT * FROM users WHERE id > 0;--COMMENT ON VIEW "public"."active_users" IS 'Updated view comment';-`,- previousSDL: `-CREATE TABLE "public"."users" (- "id" integer NOT NULL,- "name" text-);--CREATE VIEW "public"."active_users" AS SELECT * FROM users;--COMMENT ON VIEW "public"."active_users" IS 'Old view comment';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Tables: []*storepb.TableMetadata{- {- Name: "users",- Columns: []*storepb.ColumnMetadata{- {Name: "id", Type: "integer", Nullable: false},- {Name: "name", Type: "text", Nullable: true},- },- },- },- Views: []*storepb.ViewMetadata{- {- Name: "active_users",- Definition: "SELECT * FROM users WHERE id > 0",- Comment: "View of active users",- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectViewDiff: false,- expectCommentDiff: true,- description: "View definition matches database, comment differs → skip view diff, generate comment diff",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- t.Logf("Description: %s", tt.description)-- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, tt.currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // Check view diff- hasViewDiff := len(diff.ViewChanges) > 0- require.Equal(t, tt.expectViewDiff, hasViewDiff,- "Expected view diff=%v but got=%v. ViewChanges count: %d",- tt.expectViewDiff, hasViewDiff, len(diff.ViewChanges))-- // Check comment diff- hasCommentDiff := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeView &&- commentDiff.ObjectName == "active_users" {- hasCommentDiff = true- break- }- }- require.Equal(t, tt.expectCommentDiff, hasCommentDiff,- "Expected comment diff=%v but got=%v. CommentChanges count: %d",- tt.expectCommentDiff, hasCommentDiff, len(diff.CommentChanges))- })- }-}--// TestUsabilityCheck_FunctionScenarios tests usability check for functions-func TestUsabilityCheck_FunctionScenarios(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- currentSchema *model.DatabaseMetadata- expectFunctionDiff bool- expectCommentDiff bool- description string- }{- {- name: "function_same_body_same_comment_skip_both",- currentSDL: `-CREATE FUNCTION "public"."add_numbers"(a integer, b integer) RETURNS integer- LANGUAGE plpgsql- AS $$-BEGIN- RETURN a + b;-END;-$$;--COMMENT ON FUNCTION "public"."add_numbers"(a integer, b integer) IS 'Adds two numbers';-`,- previousSDL: `-CREATE FUNCTION "public"."add_numbers"(a integer, b integer) RETURNS integer- LANGUAGE plpgsql- AS $$-BEGIN- RETURN a - b;-END;-$$;--COMMENT ON FUNCTION "public"."add_numbers"(a integer, b integer) IS 'Old comment';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Functions: []*storepb.FunctionMetadata{- {- Name: "add_numbers",- Signature: `add_numbers(a integer, b integer)`,- Definition: `CREATE FUNCTION "public"."add_numbers"(a integer, b integer) RETURNS integer- LANGUAGE plpgsql- AS $$-BEGIN- RETURN a + b;-END;-$$`,- Comment: "Adds two numbers",- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectFunctionDiff: false,- expectCommentDiff: false,- description: "Function body and comment match database → skip both",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- t.Logf("Description: %s", tt.description)-- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, tt.currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // Check function diff- hasFunctionDiff := len(diff.FunctionChanges) > 0- require.Equal(t, tt.expectFunctionDiff, hasFunctionDiff,- "Expected function diff=%v but got=%v. FunctionChanges count: %d",- tt.expectFunctionDiff, hasFunctionDiff, len(diff.FunctionChanges))-- // Check comment diff- hasCommentDiff := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeFunction &&- commentDiff.ObjectName == "add_numbers" {- hasCommentDiff = true- break- }- }- require.Equal(t, tt.expectCommentDiff, hasCommentDiff,- "Expected comment diff=%v but got=%v",- tt.expectCommentDiff, hasCommentDiff)- })- }-}--// TestUsabilityCheck_SequenceScenarios tests usability check for sequences-func TestUsabilityCheck_SequenceScenarios(t *testing.T) {- tests := []struct {- name string- currentSDL string- previousSDL string- currentSchema *model.DatabaseMetadata- expectSequenceDiff bool- expectCommentDiff bool- description string- }{- {- name: "sequence_same_config_same_comment_skip_both",- currentSDL: `-CREATE SEQUENCE "public"."user_id_seq" START WITH 1 INCREMENT BY 1 NO CYCLE;--COMMENT ON SEQUENCE "public"."user_id_seq" IS 'User ID sequence';-`,- previousSDL: `-CREATE SEQUENCE "public"."user_id_seq" START WITH 100 INCREMENT BY 2 NO CYCLE;--COMMENT ON SEQUENCE "public"."user_id_seq" IS 'Old sequence comment';-`,- currentSchema: model.NewDatabaseMetadata(- &storepb.DatabaseSchemaMetadata{- Name: "test_db",- Schemas: []*storepb.SchemaMetadata{- {- Name: "public",- Sequences: []*storepb.SequenceMetadata{- {- Name: "user_id_seq",- Start: "1",- Increment: "1",- Comment: "User ID sequence",- },- },- },- },- },- nil, nil, storepb.Engine_POSTGRES, false,- ),- expectSequenceDiff: false,- expectCommentDiff: false,- description: "Sequence config and comment match database → skip both",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- t.Logf("Description: %s", tt.description)-- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, tt.currentSchema)- require.NoError(t, err)- require.NotNil(t, diff)-- // Check sequence diff- hasSequenceDiff := len(diff.SequenceChanges) > 0- require.Equal(t, tt.expectSequenceDiff, hasSequenceDiff,- "Expected sequence diff=%v but got=%v",- tt.expectSequenceDiff, hasSequenceDiff)-- // Check comment diff- hasCommentDiff := false- for _, commentDiff := range diff.CommentChanges {- if commentDiff.ObjectType == schema.CommentObjectTypeSequence &&- commentDiff.ObjectName == "user_id_seq" {- hasCommentDiff = true- break- }- }- require.Equal(t, tt.expectCommentDiff, hasCommentDiff,- "Expected comment diff=%v but got=%v",- tt.expectCommentDiff, hasCommentDiff)- })- }-}diff --git a/backend/plugin/schema/pg/view_migration_integration_test.go b/backend/plugin/schema/pg/view_migration_integration_test.godeleted file mode 100644index 74d0050a68..0000000000--- a/backend/plugin/schema/pg/view_migration_integration_test.go+++ /dev/null@@ -1,500 +0,0 @@-package pg--import (- "strings"- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestViewSDLDiffAndMigrationIntegration(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedMigration string- }{- {- name: "Create new view",- previousSDL: ``,- currentSDL: `- CREATE VIEW active_users AS- SELECT 1 as id, 'test' as name;- `,- expectedMigration: `CREATE VIEW active_users AS- SELECT 1 as id, 'test' as name;--`,- },- {- name: "Drop view",- previousSDL: `- CREATE VIEW active_users AS- SELECT 1 as id, 'test' as name;- `,- currentSDL: ``,- expectedMigration: `DROP VIEW IF EXISTS "public"."active_users";-`,- },- {- name: "Modify view (drop and recreate)",- previousSDL: `- CREATE VIEW active_users AS- SELECT 1 as id, 'test' as name;- `,- currentSDL: `- CREATE VIEW active_users AS- SELECT 2 as id, 'updated' as name, 'extra' as email;- `,- expectedMigration: `DROP VIEW IF EXISTS "public"."active_users";--CREATE VIEW active_users AS- SELECT 2 as id, 'updated' as name, 'extra' as email;--`,- },- {- name: "Schema-qualified view",- previousSDL: ``,- currentSDL: `- CREATE VIEW test_schema.expensive_products AS- SELECT 1 as id, 'product' as name, 150.00 as price;- `,- expectedMigration: `CREATE VIEW test_schema.expensive_products AS- SELECT 1 as id, 'product' as name, 150.00 as price;--`,- },- {- name: "Multiple view changes",- previousSDL: `- CREATE VIEW user_summary AS- SELECT 1 as id, 'user' as name;-- CREATE VIEW order_summary AS- SELECT 1 as id, 100.00 as amount;- `,- currentSDL: `- CREATE VIEW user_summary AS- SELECT 2 as id, 'updated_user' as name, 'active' as status;-- CREATE VIEW order_analytics AS- SELECT 1 as user_id, 500.00 as total_amount;- `,- expectedMigration: `DROP VIEW IF EXISTS "public"."order_summary";-DROP VIEW IF EXISTS "public"."user_summary";--CREATE VIEW order_analytics AS- SELECT 1 as user_id, 500.00 as total_amount;--CREATE VIEW user_summary AS- SELECT 2 as id, 'updated_user' as name, 'active' as status;--`,- },- {- name: "View with dependencies (AST-only mode)",- previousSDL: ``,- currentSDL: `- CREATE VIEW dependent_view AS- SELECT id, name, 'derived' as type FROM base_view;-- CREATE VIEW base_view AS- SELECT 1 as id, 'base' as name;- `,- expectedMigration: `CREATE VIEW base_view AS- SELECT 1 as id, 'base' as name;--CREATE VIEW dependent_view AS- SELECT id, name, 'derived' as type FROM base_view;--`,- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- // Step 1: Get SDL diff using AST-only mode (no metadata extraction)- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Step 2: Verify that diff contains view changes with AST nodes- if len(diff.ViewChanges) > 0 {- for _, viewDiff := range diff.ViewChanges {- switch viewDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, viewDiff.NewASTNode,- "Create action should have NewASTNode")- assert.Nil(t, viewDiff.OldASTNode,- "Create action should not have OldASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, viewDiff.NewView,- "AST-only mode should not have metadata")- case schema.MetadataDiffActionDrop:- assert.NotNil(t, viewDiff.OldASTNode,- "Drop action should have OldASTNode")- assert.Nil(t, viewDiff.NewASTNode,- "Drop action should not have NewASTNode")- // Verify that no metadata was extracted (AST-only mode)- assert.Nil(t, viewDiff.OldView,- "AST-only mode should not have metadata")- default:- // Other actions like MetadataDiffActionAlter- t.Logf("Encountered view action: %v", viewDiff.Action)- }- }- }-- // Step 3: Generate migration SQL using AST nodes- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)-- // Step 4: Verify the generated migration matches expectations- assert.Equal(t, tt.expectedMigration, migrationSQL,- "Generated migration SQL should match expected output")-- // Step 5: Verify the migration is valid PostgreSQL SQL- // We can do a basic check to ensure it contains expected keywords- if tt.expectedMigration != "" {- if contains(tt.expectedMigration, "CREATE VIEW") {- assert.Contains(t, migrationSQL, "CREATE VIEW",- "Migration should contain CREATE VIEW statement")- }- if contains(tt.expectedMigration, "DROP VIEW") {- assert.Contains(t, migrationSQL, "DROP VIEW",- "Migration should contain DROP VIEW statement")- }- }- })- }-}--func TestViewMigrationASTOnlyModeValidation(t *testing.T) {- // Test that ensures AST-only mode works correctly without any metadata extraction- previousSDL := ``- currentSDL := `- CREATE VIEW product_view AS SELECT 1 as id, 'product' as name;- `-- // Get diff without metadata extraction- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly one view change- assert.Len(t, diff.ViewChanges, 1)- viewDiff := diff.ViewChanges[0]-- // Verify AST-only mode properties- assert.Equal(t, schema.MetadataDiffActionCreate, viewDiff.Action)- assert.Equal(t, "public", viewDiff.SchemaName)- assert.Equal(t, "product_view", viewDiff.ViewName)-- // Critical assertion: No metadata should be present (AST-only mode)- assert.Nil(t, viewDiff.NewView, "AST-only mode should not extract metadata")- assert.Nil(t, viewDiff.OldView, "AST-only mode should not extract metadata")-- // But AST nodes should be present- assert.NotNil(t, viewDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, viewDiff.OldASTNode, "No old AST node for CREATE action")-- // Generate migration should work with AST nodes only- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.Contains(t, migrationSQL, "CREATE VIEW product_view AS SELECT 1 as id, 'product' as name")-}--func TestViewDependencyHandlingInAST(t *testing.T) {- // Test that verifies dependency extraction works in AST-only mode- // This test creates two views where one depends on the other- previousSDL := ``- currentSDL := `- -- Create a base view first- CREATE VIEW base_data AS- SELECT 1 as id, 'item1' as name, 100 as value;-- -- Create a dependent view that references the base view- CREATE VIEW summary_report AS- SELECT b.name, b.value, b.value * 2 as doubled_value- FROM base_data b- WHERE b.value > 50;- `-- // Get diff without metadata extraction (AST-only mode)- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have exactly two view changes (CREATE actions)- assert.Len(t, diff.ViewChanges, 2)-- // Verify both views are CREATE actions with AST nodes- createCount := 0- for _, viewDiff := range diff.ViewChanges {- if viewDiff.Action == schema.MetadataDiffActionCreate {- createCount++- // Verify AST-only mode properties- assert.NotNil(t, viewDiff.NewASTNode, "AST node should be present for CREATE action")- assert.Nil(t, viewDiff.OldASTNode, "No old AST node for CREATE action")- assert.Nil(t, viewDiff.NewView, "AST-only mode should not extract metadata")- }- }- assert.Equal(t, 2, createCount, "Should have two CREATE actions")-- // Generate migration SQL - this should work with dependency resolution- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- // Verify the migration contains both views- assert.Contains(t, migrationSQL, "CREATE VIEW base_data AS")- assert.Contains(t, migrationSQL, "CREATE VIEW summary_report AS")-- // The important test: base_data should be created before summary_report- // due to dependency resolution- baseIndex := strings.Index(migrationSQL, "CREATE VIEW base_data AS")- summaryIndex := strings.Index(migrationSQL, "CREATE VIEW summary_report AS")- assert.True(t, baseIndex < summaryIndex && baseIndex >= 0 && summaryIndex >= 0,- "base_data should be created before summary_report due to dependency ordering. Migration SQL:\n%s", migrationSQL)-}--// Helper function to check if a string contains a substring-func contains(s, substr string) bool {- return strings.Contains(s, substr)-}--func TestDropTableAndDependentView_CorrectOrder(t *testing.T) {- // This test verifies that when dropping both a table and a view that depends on it,- // the DROP statements are generated in the correct order:- // 1. DROP VIEW first (dependent object)- // 2. DROP TABLE second (base object)- //- // This is critical because PostgreSQL will fail if we try to drop the table first- // while the view still depends on it.-- previousSDL := `- CREATE TABLE users (- id INTEGER PRIMARY KEY,- name TEXT NOT NULL,- email TEXT NOT NULL- );-- CREATE VIEW active_users AS- SELECT id, name, email- FROM users- WHERE email IS NOT NULL;- `-- // Both table and view are removed in current SDL- currentSDL := ``-- // Get the diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have both table and view changes- assert.Len(t, diff.TableChanges, 1, "Should have one table change")- assert.Len(t, diff.ViewChanges, 1, "Should have one view change")-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Verify both DROP statements are present- assert.Contains(t, migrationSQL, "DROP VIEW", "Should contain DROP VIEW statement")- assert.Contains(t, migrationSQL, "DROP TABLE", "Should contain DROP TABLE statement")-- // The critical test: VIEW must be dropped BEFORE TABLE- // Find the positions of each DROP statement- viewDropIndex := strings.Index(migrationSQL, "DROP VIEW")- tableDropIndex := strings.Index(migrationSQL, "DROP TABLE")-- assert.True(t, viewDropIndex >= 0, "DROP VIEW statement should be present")- assert.True(t, tableDropIndex >= 0, "DROP TABLE statement should be present")- assert.True(t, viewDropIndex < tableDropIndex,- "DROP VIEW must come before DROP TABLE because view depends on table.\n"+- "Current order is incorrect: VIEW at position %d, TABLE at position %d\n"+- "Migration SQL:\n%s",- viewDropIndex, tableDropIndex, migrationSQL)-}--func TestDropMultipleTablesAndViews_CorrectOrder(t *testing.T) {- // Test with multiple tables and views to ensure proper dependency ordering-- previousSDL := `- CREATE TABLE categories (- id INTEGER PRIMARY KEY,- name TEXT NOT NULL- );-- CREATE TABLE products (- id INTEGER PRIMARY KEY,- name TEXT NOT NULL,- category_id INTEGER REFERENCES categories(id)- );-- CREATE VIEW product_summary AS- SELECT p.id, p.name, c.name as category_name- FROM products p- JOIN categories c ON p.category_id = c.id;- `-- // Remove all objects- currentSDL := ``-- // Get the diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Verify the view is dropped before both tables- viewDropIndex := strings.Index(migrationSQL, "DROP VIEW")- productsTableDropIndex := strings.Index(migrationSQL, `DROP TABLE IF EXISTS "public"."products"`)- categoriesTableDropIndex := strings.Index(migrationSQL, `DROP TABLE IF EXISTS "public"."categories"`)-- assert.True(t, viewDropIndex >= 0, "DROP VIEW should be present")- assert.True(t, productsTableDropIndex >= 0, "DROP TABLE products should be present")- assert.True(t, categoriesTableDropIndex >= 0, "DROP TABLE categories should be present")-- // View must be dropped first (this is the critical fix)- assert.True(t, viewDropIndex < productsTableDropIndex,- "DROP VIEW must come before DROP TABLE products. Migration SQL:\n%s", migrationSQL)- assert.True(t, viewDropIndex < categoriesTableDropIndex,- "DROP VIEW must come before DROP TABLE categories. Migration SQL:\n%s", migrationSQL)-- // Note: In AST-only mode, we cannot reliably extract foreign key dependencies,- // so table DROP order may not respect FK constraints. The FKs are dropped before tables anyway,- // so the migration will succeed even if the table order is not optimal.- t.Log("Note: Products should be dropped before categories (FK dependency), but AST-only mode may not enforce this")-}--func TestCreateTableAndDependentView_CorrectOrder(t *testing.T) {- // This test verifies that when creating both a table and a view that depends on it,- // the CREATE statements are generated in the correct order:- // 1. CREATE TABLE first (base object)- // 2. CREATE VIEW second (dependent object)- //- // This is critical because PostgreSQL will fail if we try to create the view first- // before the table exists.-- previousSDL := ``-- // Both table and view are added in current SDL- currentSDL := `- CREATE TABLE users (- id INTEGER PRIMARY KEY,- name TEXT NOT NULL,- email TEXT NOT NULL- );-- CREATE VIEW active_users AS- SELECT id, name, email- FROM users- WHERE email IS NOT NULL;- `-- // Get the diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Should have both table and view changes- assert.Len(t, diff.TableChanges, 1, "Should have one table change")- assert.Len(t, diff.ViewChanges, 1, "Should have one view change")-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Verify both CREATE statements are present- assert.Contains(t, migrationSQL, "CREATE TABLE", "Should contain CREATE TABLE statement")- assert.Contains(t, migrationSQL, "CREATE VIEW", "Should contain CREATE VIEW statement")-- // The critical test: TABLE must be created BEFORE VIEW- // Find the positions of each CREATE statement- tableCreateIndex := strings.Index(migrationSQL, "CREATE TABLE")- viewCreateIndex := strings.Index(migrationSQL, "CREATE VIEW")-- assert.True(t, tableCreateIndex >= 0, "CREATE TABLE statement should be present")- assert.True(t, viewCreateIndex >= 0, "CREATE VIEW statement should be present")- assert.True(t, tableCreateIndex < viewCreateIndex,- "CREATE TABLE must come before CREATE VIEW because view depends on table.\n"+- "Current order is incorrect: TABLE at position %d, VIEW at position %d\n"+- "Migration SQL:\n%s",- tableCreateIndex, viewCreateIndex, migrationSQL)-}--func TestCreateMultipleTablesAndViews_CorrectOrder(t *testing.T) {- // Test with multiple tables and views to ensure proper dependency ordering-- previousSDL := ``-- // Create tables and a view that depends on them- currentSDL := `- CREATE TABLE categories (- id INTEGER PRIMARY KEY,- name TEXT NOT NULL- );-- CREATE TABLE products (- id INTEGER PRIMARY KEY,- name TEXT NOT NULL,- category_id INTEGER REFERENCES categories(id)- );-- CREATE VIEW product_summary AS- SELECT p.id, p.name, c.name as category_name- FROM products p- JOIN categories c ON p.category_id = c.id;- `-- // Get the diff- diff, err := GetSDLDiff(currentSDL, previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- // Generate migration SQL- migrationSQL, err := generateMigration(diff)- require.NoError(t, err)- assert.NotEmpty(t, migrationSQL, "Migration SQL should not be empty")-- t.Logf("Generated migration SQL:\n%s", migrationSQL)-- // Verify both tables are created before the view- categoriesTableCreateIndex := strings.Index(migrationSQL, `CREATE TABLE categories`)- productsTableCreateIndex := strings.Index(migrationSQL, `CREATE TABLE products`)- viewCreateIndex := strings.Index(migrationSQL, "CREATE VIEW")-- assert.True(t, categoriesTableCreateIndex >= 0, "CREATE TABLE categories should be present")- assert.True(t, productsTableCreateIndex >= 0, "CREATE TABLE products should be present")- assert.True(t, viewCreateIndex >= 0, "CREATE VIEW should be present")-- // Both tables must be created before the view (this is the critical fix)- assert.True(t, categoriesTableCreateIndex < viewCreateIndex,- "CREATE TABLE categories must come before CREATE VIEW. Migration SQL:\n%s", migrationSQL)- assert.True(t, productsTableCreateIndex < viewCreateIndex,- "CREATE TABLE products must come before CREATE VIEW. Migration SQL:\n%s", migrationSQL)-- // Note: The FK constraint means categories should be created before products,- // and the topological sort should handle this correctly- assert.True(t, categoriesTableCreateIndex < productsTableCreateIndex,- "CREATE TABLE categories should come before CREATE TABLE products (FK dependency). Migration SQL:\n%s", migrationSQL)-}diff --git a/backend/plugin/schema/pg/view_sdl_diff_test.go b/backend/plugin/schema/pg/view_sdl_diff_test.godeleted file mode 100644index 0cf56b1ca7..0000000000--- a/backend/plugin/schema/pg/view_sdl_diff_test.go+++ /dev/null@@ -1,247 +0,0 @@-package pg--import (- "testing"-- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/schema"-)--func TestViewSDLDiff(t *testing.T) {- tests := []struct {- name string- previousSDL string- currentSDL string- expectedViewChanges int- expectedActions []schema.MetadataDiffAction- }{- {- name: "Create new view",- previousSDL: ``,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE VIEW active_users AS- SELECT id, name- FROM users- WHERE active = true;- `,- expectedViewChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- {- name: "Drop view",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE VIEW active_users AS- SELECT id, name- FROM users- WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );- `,- expectedViewChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},- },- {- name: "Modify view (drop and recreate)",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE VIEW active_users AS- SELECT id, name- FROM users- WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL,- active BOOLEAN DEFAULT true- );-- CREATE VIEW active_users AS- SELECT id, name, 'active' as status- FROM users- WHERE active = true;- `,- expectedViewChanges: 2, // Drop + Create- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop, schema.MetadataDiffActionCreate},- },- {- name: "No changes to view",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE VIEW active_users AS- SELECT id, name- FROM users- WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE VIEW active_users AS- SELECT id, name- FROM users- WHERE active = true;- `,- expectedViewChanges: 0,- expectedActions: []schema.MetadataDiffAction{},- },- {- name: "Multiple views with different changes",- previousSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE VIEW all_users AS- SELECT * FROM users;-- CREATE VIEW active_users AS- SELECT id, name FROM users WHERE active = true;- `,- currentSDL: `- CREATE TABLE users (- id SERIAL PRIMARY KEY,- name VARCHAR(255) NOT NULL- );-- CREATE VIEW all_users AS- SELECT * FROM users;-- CREATE VIEW admin_users AS- SELECT id, name FROM users WHERE role = 'admin';- `,- expectedViewChanges: 2, // Drop active_users + Create admin_users- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate, schema.MetadataDiffActionDrop},- },- {- name: "Schema-qualified view names",- previousSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );- `,- currentSDL: `- CREATE SCHEMA test_schema;- CREATE TABLE test_schema.products (- id SERIAL PRIMARY KEY,- name VARCHAR(255)- );-- CREATE VIEW test_schema.product_summary AS- SELECT id, name FROM test_schema.products;- `,- expectedViewChanges: 1,- expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil)- require.NoError(t, err)- require.NotNil(t, diff)-- assert.Equal(t, tt.expectedViewChanges, len(diff.ViewChanges),- "Expected %d view changes, got %d", tt.expectedViewChanges, len(diff.ViewChanges))-- // Check that the actions match expectations- var actualActions []schema.MetadataDiffAction- for _, viewDiff := range diff.ViewChanges {- actualActions = append(actualActions, viewDiff.Action)- }-- // Handle nil vs empty slice comparison- if len(tt.expectedActions) == 0 && len(actualActions) == 0 {- // Both are effectively empty - test passes- t.Log("Both expected and actual actions are empty")- } else {- assert.ElementsMatch(t, tt.expectedActions, actualActions,- "Expected actions %v, got %v", tt.expectedActions, actualActions)- }-- // Verify AST nodes are properly set- for i, viewDiff := range diff.ViewChanges {- switch viewDiff.Action {- case schema.MetadataDiffActionCreate:- assert.NotNil(t, viewDiff.NewASTNode,- "View diff %d should have NewASTNode for CREATE action", i)- assert.Nil(t, viewDiff.OldASTNode,- "View diff %d should not have OldASTNode for CREATE action", i)- case schema.MetadataDiffActionDrop:- assert.NotNil(t, viewDiff.OldASTNode,- "View diff %d should have OldASTNode for DROP action", i)- assert.Nil(t, viewDiff.NewASTNode,- "View diff %d should not have NewASTNode for DROP action", i)- default:- t.Errorf("Unexpected action %v for view diff %d", viewDiff.Action, i)- }- }- })- }-}--func TestIdentifierParsing(t *testing.T) {- tests := []struct {- name string- identifier string- expectedSchema string- expectedView string- }{- {- name: "Simple view name",- identifier: "my_view",- expectedSchema: "public",- expectedView: "my_view",- },- {- name: "Schema-qualified view name",- identifier: "test_schema.my_view",- expectedSchema: "test_schema",- expectedView: "my_view",- },- {- name: "Public schema explicit",- identifier: "public.user_summary",- expectedSchema: "public",- expectedView: "user_summary",- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- schema, view := parseIdentifier(tt.identifier)- assert.Equal(t, tt.expectedSchema, schema)- assert.Equal(t, tt.expectedView, view)- })- }-}