Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: support pg column in sdl workflow (#17407)
rebeliced8b5a2070e75
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.goindex f9cc29b1ec..159bb3e234 100644--- a/backend/plugin/schema/differ.go+++ b/backend/plugin/schema/differ.go@@ -98,6 +98,10 @@ type ColumnDiff struct {Action MetadataDiffActionOldColumn *storepb.ColumnMetadataNewColumn *storepb.ColumnMetadata++ // AST nodes for DDL analysis and generation+ OldASTNode parser.IColumnDefContext // Previous column definition AST node+ NewASTNode parser.IColumnDefContext // Current column definition AST node}// IndexDiff represents changes to an index.diff --git a/backend/plugin/schema/pg/column_sdl_diff_test.go b/backend/plugin/schema/pg/column_sdl_diff_test.gonew file mode 100644index 0000000000..35fcb668f8--- /dev/null+++ b/backend/plugin/schema/pg/column_sdl_diff_test.go@@ -0,0 +1,596 @@+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)+ }{+ {+ 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")+ assert.NotNil(t, columnDiff.NewColumn, "CREATE should have NewColumn")+ assert.Equal(t, "email", columnDiff.NewColumn.Name, "Column name should match")+ assert.Contains(t, columnDiff.NewColumn.Type, "VARCHAR", "Should contain VARCHAR type")++ // Verify AST nodes+ assert.Nil(t, columnDiff.OldASTNode, "CREATE should have nil OldASTNode")+ assert.NotNil(t, columnDiff.NewASTNode, "CREATE should have NewASTNode")+ },+ },+ {+ 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.NotNil(t, columnDiff.OldColumn, "DROP should have OldColumn")+ assert.Nil(t, columnDiff.NewColumn, "DROP should have nil NewColumn")+ assert.Equal(t, "email", columnDiff.OldColumn.Name, "Column name should match")++ // Verify AST nodes+ assert.NotNil(t, columnDiff.OldASTNode, "DROP should have OldASTNode")+ assert.Nil(t, columnDiff.NewASTNode, "DROP should have nil NewASTNode")+ },+ },+ {+ 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.NotNil(t, columnDiff.OldColumn, "ALTER should have OldColumn")+ assert.NotNil(t, columnDiff.NewColumn, "ALTER should have NewColumn")+ assert.Equal(t, "name", columnDiff.OldColumn.Name, "Old column name should match")+ assert.Equal(t, "name", columnDiff.NewColumn.Name, "New column name should match")+ assert.Contains(t, columnDiff.OldColumn.Type, "VARCHAR(255)", "Old type should contain VARCHAR(255)")+ assert.Contains(t, columnDiff.NewColumn.Type, "TEXT", "New type should contain TEXT")++ // Verify AST nodes+ assert.NotNil(t, columnDiff.OldASTNode, "ALTER should have OldASTNode")+ assert.NotNil(t, columnDiff.NewASTNode, "ALTER should have NewASTNode")++ // 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+++ createColumns = append(createColumns, columnDiff.NewColumn.Name)+ assert.Nil(t, columnDiff.OldColumn, "CREATE should have nil OldColumn")+ assert.NotNil(t, columnDiff.NewColumn, "CREATE should have NewColumn")+ assert.Nil(t, columnDiff.OldASTNode, "CREATE should have nil OldASTNode")+ assert.NotNil(t, columnDiff.NewASTNode, "CREATE should have NewASTNode")+ case schema.MetadataDiffActionAlter:+ alterCount+++ alterColumns = append(alterColumns, columnDiff.NewColumn.Name)+ assert.NotNil(t, columnDiff.OldColumn, "ALTER should have OldColumn")+ assert.NotNil(t, columnDiff.NewColumn, "ALTER should have NewColumn")+ assert.NotNil(t, columnDiff.OldASTNode, "ALTER should have OldASTNode")+ assert.NotNil(t, columnDiff.NewASTNode, "ALTER should have NewASTNode")+ case schema.MetadataDiffActionDrop:+ dropCount+++ dropColumns = append(dropColumns, columnDiff.OldColumn.Name)+ assert.NotNil(t, columnDiff.OldColumn, "DROP should have OldColumn")+ 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.False(t, columnDiff.OldColumn.Nullable, "Old column should not be nullable (has NOT NULL)")+ assert.True(t, columnDiff.NewColumn.Nullable, "New column should be nullable (no 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.Equal(t, "'active'", columnDiff.OldColumn.Default, "Old default should match")+ assert.Equal(t, "'inactive'", columnDiff.NewColumn.Default, "New default should match")+ },+ },+ {+ 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.Equal(t, `"C"`, columnDiff.OldColumn.Collation, "Old collation should match")+ assert.Equal(t, `"en_US"`, columnDiff.NewColumn.Collation, "New collation should match")+ },+ },+ {+ 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")+ assert.Equal(t, "id", columnDiff.NewColumn.Name, "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")+ },+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ // Call GetSDLDiff+ diff, err := GetSDLDiff(tc.currentSDL, tc.previousSDL, nil, 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:+ createColumns = append(createColumns, columnChange.NewColumn.Name)+ case schema.MetadataDiffActionAlter:+ alterColumns = append(alterColumns, columnChange.NewColumn.Name)+ case schema.MetadataDiffActionDrop:+ dropColumns = append(dropColumns, columnChange.OldColumn.Name)+ 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)+ }+ }+ })+ }+}++// 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, "test")++ testChunk := chunks.Tables["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/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex e21a68543e..ca6351b9fa 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -203,28 +203,46 @@ func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currencurrentText := currentChunk.GetText(currentChunks.Tokens)previousText := previousChunk.GetText(previousChunks.Tokens)if currentText != previousText {- // Table was modified+ // 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)+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: previousChunk.ASTNode.(*parser.CreatestmtContext),- NewASTNode: currentChunk.ASTNode.(*parser.CreatestmtContext),+ 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,}diff.TableChanges = append(diff.TableChanges, tableDiff)}} else {// New table+ 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: currentChunk.ASTNode.(*parser.CreatestmtContext),+ 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)}@@ -235,14 +253,20 @@ func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currenif _, exists := currentChunks.Tables[identifier]; !exists {// Table was droppedschemaName, tableName := parseTableIdentifier(previousChunk.Identifier)+ 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: previousChunk.ASTNode.(*parser.CreatestmtContext),- NewASTNode: nil,+ 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)}@@ -251,6 +275,127 @@ func processTableChanges(currentChunks, previousChunks *schema.SDLChunks, currenreturn nil}+// processColumnChanges analyzes column changes between old and new table definitions+// Following the same pattern as processTableChanges: compare text first, then analyze differences+func processColumnChanges(oldTable, newTable *parser.CreatestmtContext) []*schema.ColumnDiff {+ if oldTable == nil || newTable == nil {+ return []*schema.ColumnDiff{}+ }++ // Step 1: Extract all column definitions with their AST nodes for text comparison+ oldColumnMap := extractColumnDefinitionsWithAST(oldTable)+ newColumnMap := extractColumnDefinitionsWithAST(newTable)++ var columnDiffs []*schema.ColumnDiff++ // Step 2: Process current columns to find created and modified columns+ for columnName, newColumnDef := range newColumnMap {+ if oldColumnDef, exists := oldColumnMap[columnName]; exists {+ // Column exists in both - check if modified by comparing text first+ currentText := getColumnText(newColumnDef.ASTNode)+ previousText := getColumnText(oldColumnDef.ASTNode)+ if currentText != previousText {+ // Column was modified - only now extract detailed metadata+ 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 for new columns+ 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+ for columnName, oldColumnDef := range oldColumnMap {+ if _, exists := newColumnMap[columnName]; !exists {+ // Column was dropped - extract metadata only for dropped columns+ 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+}++// extractColumnDefinitionsWithAST extracts only column name and AST node for text comparison+// This is more efficient than full metadata extraction when we only need text comparison+func extractColumnDefinitionsWithAST(createStmt *parser.CreatestmtContext) map[string]*ColumnDefWithAST {+ columns := make(map[string]*ColumnDefWithAST)++ if createStmt == nil {+ return columns+ }++ // 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())++ columns[columnName] = &ColumnDefWithAST{+ Name: columnName,+ ASTNode: columnDef,+ }+ }+ }+ }+ }++ return columns+}++// 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),+ }+}+// parseTableIdentifier parses a table identifier and returns schema name and table namefunc parseTableIdentifier(identifier string) (schemaName, tableName string) {parts := strings.Split(identifier, ".")@@ -259,3 +404,140 @@ func parseTableIdentifier(identifier string) (schemaName, tableName string) {}return "public", identifier}++// 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()+}