Skip to content

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

refactor(pg): remove legacy differ and migration generation code (#19754)

rebelice5abefb3f9c6b
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.go
index 349d5e6177..cf6961befa 100644
--- a/backend/plugin/schema/differ.go
+++ b/backend/plugin/schema/differ.go
@@ -4,9 +4,6 @@ import (
"slices"
"strings"
- "github.com/antlr4-go/antlr/v4"
- parser "github.com/bytebase/parser/postgresql"
-
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/store/model"
@@ -80,10 +77,6 @@ type TableDiff struct {
OldTable *storepb.TableMetadata
NewTable *storepb.TableMetadata
- // AST nodes for DDL analysis and generation
- OldASTNode *parser.CreatestmtContext // Previous user CREATE TABLE AST node
- NewASTNode *parser.CreatestmtContext // Current user CREATE TABLE AST node
-
// Column changes
ColumnChanges []*ColumnDiff
@@ -117,19 +110,13 @@ type ColumnDiff struct {
Action MetadataDiffAction
OldColumn *storepb.ColumnMetadata
NewColumn *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.
type IndexDiff struct {
- Action MetadataDiffAction
- OldIndex *storepb.IndexMetadata
- NewIndex *storepb.IndexMetadata
- OldASTNode any // AST node for old index constraint
- NewASTNode any // AST node for new index constraint
+ Action MetadataDiffAction
+ OldIndex *storepb.IndexMetadata
+ NewIndex *storepb.IndexMetadata
}
// ForeignKeyDiff represents changes to a foreign key.
@@ -137,8 +124,6 @@ type ForeignKeyDiff struct {
Action MetadataDiffAction
OldForeignKey *storepb.ForeignKeyMetadata
NewForeignKey *storepb.ForeignKeyMetadata
- OldASTNode any // AST node for old foreign key constraint
- NewASTNode any // AST node for new foreign key constraint
}
// CheckConstraintDiff represents changes to a check constraint.
@@ -146,8 +131,6 @@ type CheckConstraintDiff struct {
Action MetadataDiffAction
OldCheckConstraint *storepb.CheckConstraintMetadata
NewCheckConstraint *storepb.CheckConstraintMetadata
- OldASTNode any // AST node for old check constraint
- NewASTNode any // AST node for new check constraint
}
// ExcludeConstraintDiff represents changes to an EXCLUDE constraint (PostgreSQL specific).
@@ -155,8 +138,6 @@ type ExcludeConstraintDiff struct {
Action MetadataDiffAction
OldExcludeConstraint *storepb.ExcludeConstraintMetadata
NewExcludeConstraint *storepb.ExcludeConstraintMetadata
- OldASTNode any // AST node for old EXCLUDE constraint
- NewASTNode any // AST node for new EXCLUDE constraint
}
// PrimaryKeyDiff represents changes to a primary key constraint.
@@ -164,8 +145,6 @@ type PrimaryKeyDiff struct {
Action MetadataDiffAction
OldPrimaryKey *storepb.IndexMetadata
NewPrimaryKey *storepb.IndexMetadata
- OldASTNode any // AST node for old primary key constraint
- NewASTNode any // AST node for new primary key constraint
}
// UniqueConstraintDiff represents changes to a unique constraint.
@@ -173,8 +152,6 @@ type UniqueConstraintDiff struct {
Action MetadataDiffAction
OldUniqueConstraint *storepb.IndexMetadata
NewUniqueConstraint *storepb.IndexMetadata
- OldASTNode any // AST node for old unique constraint
- NewASTNode any // AST node for new unique constraint
}
// TriggerDiff represents changes to a trigger.
@@ -185,8 +162,6 @@ type TriggerDiff struct {
TriggerName string // Trigger name
OldTrigger *storepb.TriggerMetadata
NewTrigger *storepb.TriggerMetadata
- OldASTNode any // AST node for old trigger (*parser.CreatetrigstmtContext)
- NewASTNode any // AST node for new trigger (*parser.CreatetrigstmtContext)
}
// PartitionDiff represents changes to table partitions.
@@ -203,8 +178,6 @@ type ViewDiff struct {
ViewName string
OldView *storepb.ViewMetadata
NewView *storepb.ViewMetadata
- OldASTNode any // AST node for old view
- NewASTNode any // AST node for new view
}
// MaterializedViewDiff represents changes to a materialized view.
@@ -214,8 +187,6 @@ type MaterializedViewDiff struct {
MaterializedViewName string
OldMaterializedView *storepb.MaterializedViewMetadata
NewMaterializedView *storepb.MaterializedViewMetadata
- OldASTNode any // AST node for old materialized view
- NewASTNode any // AST node for new materialized view
IndexChanges []*IndexDiff // Index changes on materialized view
}
@@ -226,8 +197,6 @@ type FunctionDiff struct {
FunctionName string
OldFunction *storepb.FunctionMetadata
NewFunction *storepb.FunctionMetadata
- OldASTNode any // AST node for old function
- NewASTNode any // AST node for new function
// Detailed change information for advanced engines
SignatureChanged bool
@@ -244,8 +213,6 @@ type ProcedureDiff struct {
ProcedureName string
OldProcedure *storepb.ProcedureMetadata
NewProcedure *storepb.ProcedureMetadata
- OldASTNode any // AST node for old procedure
- NewASTNode any // AST node for new procedure
}
// SequenceDiff represents changes to a sequence.
@@ -255,8 +222,6 @@ type SequenceDiff struct {
SequenceName string
OldSequence *storepb.SequenceMetadata
NewSequence *storepb.SequenceMetadata
- OldASTNode any // AST node for old sequence
- NewASTNode any // AST node for new sequence
}
// EnumTypeDiff represents changes to an enum type.
@@ -266,8 +231,6 @@ type EnumTypeDiff struct {
EnumTypeName string
OldEnumType *storepb.EnumTypeMetadata
NewEnumType *storepb.EnumTypeMetadata
- OldASTNode any // For SDL/AST-only mode
- NewASTNode any // For SDL/AST-only mode
}
// ExtensionDiff represents changes to an extension.
@@ -276,8 +239,6 @@ type ExtensionDiff struct {
ExtensionName string
OldExtension *storepb.ExtensionMetadata
NewExtension *storepb.ExtensionMetadata
- OldASTNode any // For SDL/AST-only mode
- NewASTNode any // For SDL/AST-only mode
}
// EventTriggerDiff represents changes to an event trigger.
@@ -286,8 +247,6 @@ type EventTriggerDiff struct {
EventTriggerName string
OldEventTrigger *storepb.EventTriggerMetadata
NewEventTrigger *storepb.EventTriggerMetadata
- OldASTNode any // For SDL/AST-only mode
- NewASTNode any // For SDL/AST-only mode
}
// EventDiff represents changes to an event.
@@ -328,8 +287,6 @@ type CommentDiff struct {
IndexName string // only used for table-level INDEX comments
OldComment string
NewComment string
- OldASTNode antlr.ParserRuleContext
- NewASTNode antlr.ParserRuleContext
}
// GetDatabaseSchemaDiff compares two model.DatabaseMetadata instances and returns the differences.
@@ -425,8 +382,6 @@ func addNewSchemaObjects(diff *MetadataDiff, schemaName string, schema *model.Sc
SchemaName: schemaName,
ViewName: viewName,
NewView: view,
- OldASTNode: nil,
- NewASTNode: nil,
})
}
}
@@ -1555,8 +1510,6 @@ func compareViews(engine storepb.Engine, diff *MetadataDiff, schemaName string,
SchemaName: schemaName,
ViewName: viewName,
OldView: oldView,
- OldASTNode: nil,
- NewASTNode: nil,
})
}
}
@@ -1576,8 +1529,6 @@ func compareViews(engine storepb.Engine, diff *MetadataDiff, schemaName string,
SchemaName: schemaName,
ViewName: viewName,
NewView: newView,
- OldASTNode: nil,
- NewASTNode: nil,
})
} else if !oldView.SkipDump {
// Use engine-specific comparison
@@ -1591,8 +1542,6 @@ func compareViews(engine storepb.Engine, diff *MetadataDiff, schemaName string,
ViewName: viewName,
OldView: oldView,
NewView: newView,
- OldASTNode: nil,
- NewASTNode: nil,
})
}
} else if len(changes) > 0 {
@@ -1612,8 +1561,6 @@ func compareViews(engine storepb.Engine, diff *MetadataDiff, schemaName string,
ViewName: viewName,
OldView: oldView,
NewView: newView,
- OldASTNode: nil,
- NewASTNode: nil,
})
}
// TODO: Handle non-recreating changes like comment updates
diff --git a/backend/plugin/schema/pg/camelcase_index_test.go b/backend/plugin/schema/pg/camelcase_index_test.go
deleted file mode 100644
index a96f70717a..0000000000
--- a/backend/plugin/schema/pg/camelcase_index_test.go
+++ /dev/null
@@ -1,161 +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"
-)
-
-func TestQuoteIndexExpression(t *testing.T) {
- tests := []struct {
- input string
- expected string
- }{
- {"userId", `"userId"`},
- {"accountName", `"accountName"`},
- {"user_id", "user_id"},
- {"id", "id"},
- {`"userId"`, `"userId"`},
- {"lower(name)", "lower(name)"},
- {"(a + b)", "(a + b)"},
- {"col::text", "col::text"},
- {"", ""},
- }
- for _, tt := range tests {
- t.Run(tt.input, func(t *testing.T) {
- require.Equal(t, tt.expected, quoteIndexExpression(tt.input))
- })
- }
-}
-
-// TestCamelCaseIndexColumnQuoting_SyncSchema reproduces https://github.com/bytebase/bytebase/issues/19348
-// TestCamelCaseConstraintQuoting_PrimaryKey verifies that PRIMARY KEY constraints
-// on CamelCase columns are properly quoted in generated ALTER TABLE statements.
-func TestCamelCaseConstraintQuoting_PrimaryKey(t *testing.T) {
- // Old state: table without primary key
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "ba_account",
- Columns: []*storepb.ColumnMetadata{
- {Name: "userId", Type: "text"},
- },
- },
- },
- },
- },
- }
-
- // New state: table with primary key on CamelCase column
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "ba_account",
- Columns: []*storepb.ColumnMetadata{
- {Name: "userId", Type: "text"},
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "ba_account_pkey",
- Expressions: []string{"userId"},
- Type: "btree",
- Primary: true,
- Unique: true,
- IsConstraint: true,
- },
- },
- },
- },
- },
- },
- }
-
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- 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, `PRIMARY KEY ("userId")`, "CamelCase column in PRIMARY KEY must be quoted")
-}
-
-// TestCamelCaseConstraintQuoting_Unique verifies that UNIQUE constraints
-// on CamelCase columns are properly quoted in generated ALTER TABLE statements.
-func TestCamelCaseConstraintQuoting_Unique(t *testing.T) {
- // Old state: table without unique constraint
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "ba_account",
- Columns: []*storepb.ColumnMetadata{
- {Name: "userId", Type: "text"},
- },
- },
- },
- },
- },
- }
-
- // New state: table with unique constraint on CamelCase column
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "ba_account",
- Columns: []*storepb.ColumnMetadata{
- {Name: "userId", Type: "text"},
- },
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "ba_account_userId_key",
- Expressions: []string{"userId"},
- Type: "btree",
- Unique: true,
- IsConstraint: true,
- },
- },
- },
- },
- },
- },
- }
-
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- 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, `UNIQUE ("userId")`, "CamelCase column in UNIQUE constraint must be quoted")
-}
diff --git a/backend/plugin/schema/pg/enum_type_test.go b/backend/plugin/schema/pg/enum_type_test.go
deleted file mode 100644
index 73d17202ae..0000000000
--- a/backend/plugin/schema/pg/enum_type_test.go
+++ /dev/null
@@ -1,504 +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"
-)
-
-// TestEnumTypeInSDLOutput tests that enum types are included in SDL output
-func TestEnumTypeInSDLOutput(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_type",
- Values: []string{"active", "inactive", "pending"},
- Comment: "User status enum",
- },
- {
- Name: "priority_type",
- Values: []string{"low", "medium", "high", "urgent"},
- },
- },
- },
- },
- }
-
- // Test single-file SDL format
- sdl, err := getSDLFormat(metadata)
- require.NoError(t, err)
-
- // Verify enum types are present
- require.Contains(t, sdl, "CREATE TYPE")
- require.Contains(t, sdl, "status_type")
- require.Contains(t, sdl, "priority_type")
- require.Contains(t, sdl, "'active'")
- require.Contains(t, sdl, "'inactive'")
- require.Contains(t, sdl, "'pending'")
- require.Contains(t, sdl, "'low'")
- require.Contains(t, sdl, "'medium'")
- require.Contains(t, sdl, "'high'")
- require.Contains(t, sdl, "'urgent'")
-
- // Verify comment
- require.Contains(t, sdl, "COMMENT ON TYPE")
- require.Contains(t, sdl, "User status enum")
-}
-
-// TestEnumTypeInMultiFileSDL tests that enum types are included in multi-file SDL
-func TestEnumTypeInMultiFileSDL(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "color_type",
- Values: []string{"red", "green", "blue"},
- Comment: "RGB color enum",
- },
- },
- },
- },
- }
-
- // Test multi-file SDL format
- result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{}, metadata)
- require.NoError(t, err)
- require.NotNil(t, result)
-
- // Find the enum type file
- var enumFile *schema.File
- for i := range result.Files {
- if result.Files[i].Name == "schemas/public/types.sql" {
- enumFile = &result.Files[i]
- break
- }
- }
- require.NotNil(t, enumFile, "Enum type file not found")
-
- // Verify content includes enum type
- require.Contains(t, enumFile.Content, "CREATE TYPE")
- require.Contains(t, enumFile.Content, "color_type")
- require.Contains(t, enumFile.Content, "'red'")
- require.Contains(t, enumFile.Content, "'green'")
- require.Contains(t, enumFile.Content, "'blue'")
- require.Contains(t, enumFile.Content, "COMMENT ON TYPE")
- require.Contains(t, enumFile.Content, "RGB color enum")
-}
-
-// TestEnumTypeDependencyOrdering tests that enum types have correct dependency ordering in migrations
-func TestEnumTypeDependencyOrdering(t *testing.T) {
- t.Run("CREATE: Enum before Table", func(t *testing.T) {
- // Test that when creating an enum type used by a table,
- // CREATE TYPE comes before CREATE TABLE
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_type",
- Values: []string{"active", "inactive"},
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- },
- {
- Name: "status",
- Type: "status_type",
- },
- },
- },
- },
- },
- },
- }
-
- // Convert to model.DatabaseSchema
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- // Get diff
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Generate migration
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
- require.NotEmpty(t, migration)
-
- // Find positions of CREATE TYPE and CREATE TABLE
- typePos := -1
- tablePos := -1
- for i := 0; i < len(migration); i++ {
- if typePos == -1 && i+len("CREATE TYPE") <= len(migration) {
- if migration[i:i+len("CREATE TYPE")] == "CREATE TYPE" {
- typePos = i
- }
- }
- if tablePos == -1 && i+len("CREATE TABLE") <= len(migration) {
- if migration[i:i+len("CREATE TABLE")] == "CREATE TABLE" {
- tablePos = i
- }
- }
- }
-
- require.NotEqual(t, -1, typePos, "CREATE TYPE not found in migration")
- require.NotEqual(t, -1, tablePos, "CREATE TABLE not found in migration")
- require.Less(t, typePos, tablePos, "CREATE TYPE should come before CREATE TABLE")
- })
-
- t.Run("DROP: Table before Enum", func(t *testing.T) {
- // Test that when dropping an enum type used by a table,
- // DROP TABLE comes before DROP TYPE
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_type",
- Values: []string{"active", "inactive"},
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "integer",
- },
- {
- Name: "status",
- Type: "status_type",
- },
- },
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- // Convert to model.DatabaseSchema
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- // Get diff
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Generate migration
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
- require.NotEmpty(t, migration)
-
- // Find positions of DROP TABLE and DROP TYPE
- tablePos := -1
- typePos := -1
- for i := 0; i < len(migration); i++ {
- if tablePos == -1 && i+len("DROP TABLE") <= len(migration) {
- if migration[i:i+len("DROP TABLE")] == "DROP TABLE" {
- tablePos = i
- }
- }
- if typePos == -1 && i+len("DROP TYPE") <= len(migration) {
- if migration[i:i+len("DROP TYPE")] == "DROP TYPE" {
- typePos = i
- }
- }
- }
-
- require.NotEqual(t, -1, tablePos, "DROP TABLE not found in migration")
- require.NotEqual(t, -1, typePos, "DROP TYPE not found in migration")
- require.Less(t, tablePos, typePos, "DROP TABLE should come before DROP TYPE")
- })
-}
-
-// TestEnumTypeAlterAddValue tests that ALTER TYPE ADD VALUE is generated for new enum values
-func TestEnumTypeAlterAddValue(t *testing.T) {
- t.Run("Add single value at end", func(t *testing.T) {
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "orderstatus_enum",
- Values: []string{"active", "inactive"},
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "orderstatus_enum",
- Values: []string{"active", "inactive", "archived"},
- },
- },
- },
- },
- }
-
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Should have one ALTER enum change
- require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")
- enumDiff := diff.EnumTypeChanges[0]
- require.Equal(t, schema.MetadataDiffActionAlter, enumDiff.Action)
- require.Equal(t, "public", enumDiff.SchemaName)
- require.Equal(t, "orderstatus_enum", enumDiff.EnumTypeName)
-
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
-
- require.Contains(t, migration, "ALTER TYPE")
- require.Contains(t, migration, "ADD VALUE")
- require.Contains(t, migration, "'archived'")
- // Should not contain DROP TYPE since we're using ALTER
- require.NotContains(t, migration, "DROP TYPE")
- })
-
- t.Run("Add multiple values", func(t *testing.T) {
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "priority_enum",
- Values: []string{"low", "high"},
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "priority_enum",
- Values: []string{"low", "medium", "high", "urgent"},
- },
- },
- },
- },
- }
-
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
-
- // Should have ALTER TYPE ADD VALUE for 'medium' and 'urgent'
- require.Contains(t, migration, "ALTER TYPE")
- require.Contains(t, migration, "'medium'")
- require.Contains(t, migration, "'urgent'")
- // 'medium' should be added BEFORE 'high'
- require.Contains(t, migration, "BEFORE 'high'")
- })
-
- t.Run("Add value in the middle with BEFORE clause", func(t *testing.T) {
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_enum",
- Values: []string{"draft", "published"},
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_enum",
- Values: []string{"draft", "pending", "published"},
- },
- },
- },
- },
- }
-
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
-
- // 'pending' should be added BEFORE 'published'
- require.Contains(t, migration, "'pending'")
- require.Contains(t, migration, "BEFORE 'published'")
- })
-
- t.Run("Remove enum value - generates warning comment", func(t *testing.T) {
- // PostgreSQL does NOT support ALTER TYPE ... DROP VALUE
- // When enum values are removed, we generate a warning comment with instructions
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_enum",
- Values: []string{"active", "inactive", "archived"},
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_enum",
- Values: []string{"active", "inactive"}, // 'archived' removed
- },
- },
- },
- },
- }
-
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Should detect the change
- require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")
- enumDiff := diff.EnumTypeChanges[0]
- require.Equal(t, schema.MetadataDiffActionAlter, enumDiff.Action)
-
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration for enum value removal:\n%s", migration)
-
- // Should generate warning comment about removed values
- require.Contains(t, migration, "WARNING")
- require.Contains(t, migration, "does not support removing enum values")
- require.Contains(t, migration, "'archived'")
- require.Contains(t, migration, "RENAME TO")
- })
-
- t.Run("Rename enum value - generates warning and adds new value", func(t *testing.T) {
- // When enum values appear to be renamed (one removed, one added at same position),
- // we generate a warning for the removed value and ADD VALUE for the new one
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "priority_enum",
- Values: []string{"low", "medium", "high"},
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "priority_enum",
- Values: []string{"low", "normal", "high"}, // 'medium' renamed to 'normal'
- },
- },
- },
- },
- }
-
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Should detect the change
- require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")
- enumDiff := diff.EnumTypeChanges[0]
- require.Equal(t, schema.MetadataDiffActionAlter, enumDiff.Action)
-
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration for enum value rename:\n%s", migration)
-
- // Should generate warning for removed 'medium' and ADD VALUE for 'normal'
- require.Contains(t, migration, "WARNING")
- require.Contains(t, migration, "'medium'")
- require.Contains(t, migration, "ADD VALUE 'normal'")
- })
-}
diff --git a/backend/plugin/schema/pg/extension_test.go b/backend/plugin/schema/pg/extension_test.go
deleted file mode 100644
index c3a007438e..0000000000
--- a/backend/plugin/schema/pg/extension_test.go
+++ /dev/null
@@ -1,321 +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"
-)
-
-// TestExtensionInSDLOutput tests that extensions are included in SDL output
-func TestExtensionInSDLOutput(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Extensions: []*storepb.ExtensionMetadata{
- {
- Name: "uuid-ossp",
- Schema: "public",
- Version: "1.1",
- Description: "UUID generation extension",
- },
- {
- Name: "pg_trgm",
- Version: "1.6",
- },
- },
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- // Test single-file SDL format
- sdl, err := getSDLFormat(metadata)
- require.NoError(t, err)
-
- // Verify extensions are present
- require.Contains(t, sdl, "CREATE EXTENSION")
- require.Contains(t, sdl, "uuid-ossp")
- require.Contains(t, sdl, "pg_trgm")
- require.Contains(t, sdl, `WITH SCHEMA "public"`)
- require.Contains(t, sdl, `VERSION '1.1'`)
- require.Contains(t, sdl, `VERSION '1.6'`)
-
- // Verify comment
- require.Contains(t, sdl, "COMMENT ON EXTENSION")
- require.Contains(t, sdl, "UUID generation extension")
-}
-
-// TestExtensionInMultiFileSDL tests that extensions are included in multi-file SDL
-func TestExtensionInMultiFileSDL(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Extensions: []*storepb.ExtensionMetadata{
- {
- Name: "postgis",
- Schema: "public",
- Version: "3.3.2",
- Description: "PostGIS geometry and geography types",
- },
- },
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- // Test multi-file SDL format
- result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{}, metadata)
- require.NoError(t, err)
- require.NotNil(t, result)
-
- // Find the extension file
- var extensionFile *schema.File
- for i := range result.Files {
- if result.Files[i].Name == "extensions.sql" {
- extensionFile = &result.Files[i]
- break
- }
- }
- require.NotNil(t, extensionFile, "Extension file not found")
-
- // Verify content includes extension
- require.Contains(t, extensionFile.Content, "CREATE EXTENSION")
- require.Contains(t, extensionFile.Content, "postgis")
- require.Contains(t, extensionFile.Content, `WITH SCHEMA "public"`)
- require.Contains(t, extensionFile.Content, `VERSION '3.3.2'`)
- require.Contains(t, extensionFile.Content, "COMMENT ON EXTENSION")
- require.Contains(t, extensionFile.Content, "PostGIS geometry and geography types")
-}
-
-// TestExtensionDependencyOrdering tests that extensions have correct dependency ordering in migrations
-func TestExtensionDependencyOrdering(t *testing.T) {
- t.Run("CREATE: Extension before Enum Types and Tables", func(t *testing.T) {
- // Test that when creating an extension,
- // CREATE EXTENSION comes before CREATE TYPE and CREATE TABLE
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Extensions: []*storepb.ExtensionMetadata{
- {
- Name: "uuid-ossp",
- },
- },
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_type",
- Values: []string{"active", "inactive"},
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "uuid",
- },
- {
- Name: "status",
- Type: "status_type",
- },
- },
- },
- },
- },
- },
- }
-
- // Convert to model.DatabaseSchema
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- // Get diff
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Generate migration
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
- require.NotEmpty(t, migration)
-
- // Find positions of CREATE EXTENSION, CREATE TYPE, and CREATE TABLE
- extensionPos := -1
- typePos := -1
- tablePos := -1
- for i := 0; i < len(migration); i++ {
- if extensionPos == -1 && i+len("CREATE EXTENSION") <= len(migration) {
- if migration[i:i+len("CREATE EXTENSION")] == "CREATE EXTENSION" {
- extensionPos = i
- }
- }
- if typePos == -1 && i+len("CREATE TYPE") <= len(migration) {
- if migration[i:i+len("CREATE TYPE")] == "CREATE TYPE" {
- typePos = i
- }
- }
- if tablePos == -1 && i+len("CREATE TABLE") <= len(migration) {
- if migration[i:i+len("CREATE TABLE")] == "CREATE TABLE" {
- tablePos = i
- }
- }
- }
-
- require.NotEqual(t, -1, extensionPos, "CREATE EXTENSION not found in migration")
- require.NotEqual(t, -1, typePos, "CREATE TYPE not found in migration")
- require.NotEqual(t, -1, tablePos, "CREATE TABLE not found in migration")
- require.Less(t, extensionPos, typePos, "CREATE EXTENSION should come before CREATE TYPE")
- require.Less(t, extensionPos, tablePos, "CREATE EXTENSION should come before CREATE TABLE")
- })
-
- t.Run("DROP: Tables and Enum Types before Extension", func(t *testing.T) {
- // Test that when dropping an extension,
- // DROP TABLE and DROP TYPE come before DROP EXTENSION
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Extensions: []*storepb.ExtensionMetadata{
- {
- Name: "uuid-ossp",
- },
- },
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- EnumTypes: []*storepb.EnumTypeMetadata{
- {
- Name: "status_type",
- Values: []string{"active", "inactive"},
- },
- },
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {
- Name: "id",
- Type: "uuid",
- },
- {
- Name: "status",
- Type: "status_type",
- },
- },
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- // Convert to model.DatabaseSchema
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- // Get diff
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Generate migration
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
- require.NotEmpty(t, migration)
-
- // Find positions of DROP TABLE, DROP TYPE, and DROP EXTENSION
- tablePos := -1
- typePos := -1
- extensionPos := -1
- for i := 0; i < len(migration); i++ {
- if tablePos == -1 && i+len("DROP TABLE") <= len(migration) {
- if migration[i:i+len("DROP TABLE")] == "DROP TABLE" {
- tablePos = i
- }
- }
- if typePos == -1 && i+len("DROP TYPE") <= len(migration) {
- if migration[i:i+len("DROP TYPE")] == "DROP TYPE" {
- typePos = i
- }
- }
- if extensionPos == -1 && i+len("DROP EXTENSION") <= len(migration) {
- if migration[i:i+len("DROP EXTENSION")] == "DROP EXTENSION" {
- extensionPos = i
- }
- }
- }
-
- require.NotEqual(t, -1, tablePos, "DROP TABLE not found in migration")
- require.NotEqual(t, -1, typePos, "DROP TYPE not found in migration")
- require.NotEqual(t, -1, extensionPos, "DROP EXTENSION not found in migration")
- require.Less(t, tablePos, extensionPos, "DROP TABLE should come before DROP EXTENSION")
- require.Less(t, typePos, extensionPos, "DROP TYPE should come before DROP EXTENSION")
- })
-}
-
-// TestExtensionNotFilteredByArchiveSchemaFilter tests that extension changes are preserved
-// when FilterPostgresArchiveSchema is applied (bb rollout workflow uses this filter)
-func TestExtensionNotFilteredByArchiveSchemaFilter(t *testing.T) {
- // Create a diff with extension changes
- diff := &schema.MetadataDiff{
- DatabaseName: "testdb",
- ExtensionChanges: []*schema.ExtensionDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- ExtensionName: "citext",
- NewExtension: &storepb.ExtensionMetadata{
- Name: "citext",
- Schema: "public",
- },
- },
- },
- TableChanges: []*schema.TableDiff{
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "public",
- TableName: "test_table",
- },
- {
- Action: schema.MetadataDiffActionCreate,
- SchemaName: "bbdataarchive", // This should be filtered
- TableName: "archive_table",
- },
- },
- }
-
- // Apply the archive schema filter (this is what bb rollout does)
- filtered := schema.FilterPostgresArchiveSchema(diff)
-
- // Verify extension changes are preserved (database-level objects)
- require.Equal(t, 1, len(filtered.ExtensionChanges), "Extension changes should be preserved")
- require.Equal(t, "citext", filtered.ExtensionChanges[0].ExtensionName)
-
- // Verify archive schema tables are filtered out
- require.Equal(t, 1, len(filtered.TableChanges), "Only non-archive tables should remain")
- require.Equal(t, "public", filtered.TableChanges[0].SchemaName)
- require.Equal(t, "test_table", filtered.TableChanges[0].TableName)
-}
-
-// TestExtensionSchemaSyncBug reproduces the customer-reported bug where Schema Sync:
-// 1. Incorrectly generates DROP EXTENSION even though the extension is identical in source and target
-// 2. Generates CREATE SCHEMA for a phantom schema named after the extension
-//
diff --git a/backend/plugin/schema/pg/function_comparer.go b/backend/plugin/schema/pg/function_comparer.go
deleted file mode 100644
index affb7e7547..0000000000
--- a/backend/plugin/schema/pg/function_comparer.go
+++ /dev/null
@@ -1,723 +0,0 @@
-package pg
-
-import (
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- pgparser "github.com/bytebase/parser/postgresql"
- "github.com/pkg/errors"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/plugin/schema/pg/ast"
-)
-
-func init() {
- // Register PostgreSQL-specific function comparer
- schema.RegisterFunctionComparer(storepb.Engine_POSTGRES, &PostgreSQLFunctionComparer{})
-}
-
-// PostgreSQLFunctionComparer provides PostgreSQL-specific function comparison logic.
-type PostgreSQLFunctionComparer struct {
- schema.DefaultFunctionComparer
-}
-
-// FunctionChangeType represents the type of change detected in a function.
-type FunctionChangeType int
-
-const (
- FunctionChangeNone FunctionChangeType = iota
- FunctionChangeBodyOnly
- FunctionChangeSignature
- FunctionChangeAttributes
- FunctionChangeBoth // Both signature and body changed
-)
-
-// CompareDetailed compares two functions with PostgreSQL-specific logic using ANTLR parsing.
-func (*PostgreSQLFunctionComparer) CompareDetailed(oldFunc, newFunc *storepb.FunctionMetadata) (*schema.FunctionComparisonResult, error) {
- if oldFunc == nil || newFunc == nil {
- if oldFunc == newFunc {
- return nil, nil
- }
- return nil, errors.New("cannot compare nil functions")
- }
-
- result := &schema.FunctionComparisonResult{}
-
- result.SignatureChanged = compareFunctionSignature(oldFunc, newFunc)
-
- // Compare function attributes (comment, volatility, etc.)
- attributesChanged, changedAttrs := compareFunctionAttributes(oldFunc, newFunc)
- result.AttributesChanged = attributesChanged
- result.ChangedAttributes = changedAttrs
-
- // Compare function body using strict string comparison
- bodyChanged := compareFunctionBody(oldFunc.Definition, newFunc.Definition)
- result.BodyChanged = bodyChanged
-
- // Determine change type and migration strategy
- changeType := determineChangeType(result.SignatureChanged, bodyChanged, attributesChanged)
- requiresRecreation, canUseAlterFunction := determineMigrationStrategy(changeType, changedAttrs)
- result.RequiresRecreation = requiresRecreation
- result.CanUseAlterFunction = canUseAlterFunction
-
- // If there are no changes, return nil to indicate equality
- if !result.SignatureChanged && !bodyChanged && !attributesChanged {
- return nil, nil
- }
-
- return result, nil
-}
-
-// Equal compares two functions for equality (implements the interface expected by the schema differ).
-func (c *PostgreSQLFunctionComparer) Equal(oldFunc, newFunc *storepb.FunctionMetadata) bool {
- if oldFunc == nil || newFunc == nil {
- return oldFunc == newFunc
- }
-
- result, err := c.CompareDetailed(oldFunc, newFunc)
- if err != nil {
- // Fallback to simple definition comparison on error
- return oldFunc.Definition == newFunc.Definition
- }
-
- // If result is nil, functions are equal
- if result == nil {
- return true
- }
-
- // Functions are not equal if there are any changes
- return false
-}
-
-// compareFunctionSignature compares function signatures using ANTLR parsing.
-func compareFunctionSignature(oldFunc, newFunc *storepb.FunctionMetadata) bool {
- // Extract signature components using ANTLR parsing
- oldSig, err := parseFunctionSignature(oldFunc.Definition)
- if err != nil {
- // Fallback to simple string comparison if parsing fails
- return oldFunc.Signature != newFunc.Signature
- }
-
- newSig, err := parseFunctionSignature(newFunc.Definition)
- if err != nil {
- // Fallback to simple string comparison if parsing fails
- return oldFunc.Signature != newFunc.Signature
- }
-
- return !signatureEqual(oldSig, newSig)
-}
-
-// parseFunctionSignature parses a function definition using ANTLR and extracts signature components.
-func parseFunctionSignature(definition string) (*FunctionSignature, error) {
- // Use ANTLR PostgreSQL parser for proper parsing
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := pgparser.NewPostgreSQLParser(stream)
-
- // Parse the SQL statement
- tree := parser.Root()
- if tree == nil {
- return nil, errors.New("failed to parse function definition")
- }
-
- // Create a visitor to extract function signature information
- visitor := &FunctionSignatureVisitor{}
- result := visitor.Visit(tree)
-
- if signature, ok := result.(*FunctionSignature); ok {
- return signature, nil
- }
-
- return nil, errors.New("no function signature found in definition")
-}
-
-// FunctionSignature represents the parsed signature of a PostgreSQL function.
-type FunctionSignature struct {
- Name string
- Parameters []FunctionParameter
- ReturnType string
- Language string
- Volatility string // VOLATILE, STABLE, IMMUTABLE
- Security string // DEFINER, INVOKER
- Parallel string // UNSAFE, RESTRICTED, SAFE
- Cost string
- Rows string
- Leakproof bool
- Strict bool
-}
-
-// FunctionParameter represents a function parameter.
-type FunctionParameter struct {
- Mode string // IN, OUT, INOUT, VARIADIC
- Name string
- DataType string
- Default string
-}
-
-// FunctionSignatureVisitor extracts function signature information from ANTLR parse tree.
-type FunctionSignatureVisitor struct {
- *pgparser.BasePostgreSQLParserVisitor
-}
-
-// Visit implements the visitor pattern for extracting function signature from ANTLR parse tree.
-func (v *FunctionSignatureVisitor) Visit(tree antlr.ParseTree) any {
- switch t := tree.(type) {
- case *pgparser.CreatefunctionstmtContext:
- return v.visitCreateFunctionStmt(t)
- default:
- // Continue visiting children
- for i := 0; i < t.GetChildCount(); i++ {
- child := t.GetChild(i)
- if parseTree, ok := child.(antlr.ParseTree); ok {
- if result := v.Visit(parseTree); result != nil {
- return result
- }
- }
- }
- }
- return nil
-}
-
-// visitCreateFunctionStmt extracts signature information from CREATE FUNCTION statement.
-func (v *FunctionSignatureVisitor) visitCreateFunctionStmt(ctx *pgparser.CreatefunctionstmtContext) any {
- // In PostgreSQL, CREATE FUNCTION and CREATE PROCEDURE use the same grammar rule
- // We support both FUNCTION and PROCEDURE
- if ctx.FUNCTION() == nil && ctx.PROCEDURE() == nil {
- // This is neither CREATE FUNCTION nor CREATE PROCEDURE - skip it
- return nil
- }
-
- signature := &FunctionSignature{
- Volatility: "VOLATILE", // Default volatility
- Security: "INVOKER", // Default security
- }
-
- // Extract function name
- if ctx.Func_name() != nil {
- signature.Name = v.extractFunctionName(ctx.Func_name())
- }
-
- // Extract parameters
- if ctx.Func_args_with_defaults() != nil {
- signature.Parameters = v.extractParameters(ctx.Func_args_with_defaults())
- }
-
- // Extract return type
- if ctx.Func_return() != nil {
- signature.ReturnType = v.normalizeDataType(ctx.Func_return().GetText())
- }
-
- // Extract function options
- if ctx.Createfunc_opt_list() != nil {
- v.extractFunctionOptions(ctx.Createfunc_opt_list(), signature)
- }
-
- return signature
-}
-
-// extractFunctionName extracts the function name from func_name context.
-func (*FunctionSignatureVisitor) extractFunctionName(ctx pgparser.IFunc_nameContext) string {
- if ctx == nil {
- return ""
- }
- // Get the last part of the qualified name (schema.function -> function)
- text := ctx.GetText()
- parts := strings.Split(text, ".")
- if len(parts) > 0 {
- return parts[len(parts)-1]
- }
- return text
-}
-
-// extractParameters extracts function parameters from func_args_with_defaults context.
-func (v *FunctionSignatureVisitor) extractParameters(ctx pgparser.IFunc_args_with_defaultsContext) []FunctionParameter {
- var parameters []FunctionParameter
-
- if ctx == nil {
- return parameters
- }
-
- // Get the parameter list
- if argsList := ctx.Func_args_with_defaults_list(); argsList != nil {
- // Visit each parameter
- for _, argCtx := range argsList.AllFunc_arg_with_default() {
- if param := v.extractParameter(argCtx); param != nil {
- parameters = append(parameters, *param)
- }
- }
- }
-
- return parameters
-}
-
-// extractParameter extracts a single function parameter.
-func (v *FunctionSignatureVisitor) extractParameter(ctx pgparser.IFunc_arg_with_defaultContext) *FunctionParameter {
- if ctx == nil || ctx.Func_arg() == nil {
- return nil
- }
-
- funcArg := ctx.Func_arg()
- param := &FunctionParameter{}
-
- // Extract parameter mode (IN, OUT, INOUT, VARIADIC)
- if funcArg.Arg_class() != nil {
- param.Mode = strings.ToUpper(funcArg.Arg_class().GetText())
- }
-
- // Extract parameter name
- if funcArg.Param_name() != nil {
- param.Name = funcArg.Param_name().GetText()
- }
-
- // Extract parameter type
- if funcArg.Func_type() != nil {
- param.DataType = v.normalizeDataType(funcArg.Func_type().GetText())
- }
-
- // Extract default value
- if ctx.DEFAULT() != nil && ctx.A_expr() != nil {
- param.Default = ctx.A_expr().GetText()
- } else if ctx.EQUAL() != nil && ctx.A_expr() != nil {
- param.Default = ctx.A_expr().GetText()
- }
-
- return param
-}
-
-// extractFunctionOptions extracts function options like LANGUAGE, VOLATILE, etc.
-func (v *FunctionSignatureVisitor) extractFunctionOptions(ctx pgparser.ICreatefunc_opt_listContext, signature *FunctionSignature) {
- if ctx == nil {
- return
- }
-
- // Visit each option item
- for _, optCtx := range ctx.AllCreatefunc_opt_item() {
- v.extractFunctionOption(optCtx, signature)
- }
-}
-
-// extractFunctionOption extracts a single function option.
-func (*FunctionSignatureVisitor) extractFunctionOption(ctx pgparser.ICreatefunc_opt_itemContext, signature *FunctionSignature) {
- if ctx == nil {
- return
- }
-
- // Extract LANGUAGE
- if ctx.LANGUAGE() != nil && ctx.Nonreservedword_or_sconst() != nil {
- signature.Language = strings.ToLower(ctx.Nonreservedword_or_sconst().GetText())
- }
-
- // Extract volatility
- if ctx.Common_func_opt_item() != nil {
- commonOpt := ctx.Common_func_opt_item()
- if commonOpt.VOLATILE() != nil {
- signature.Volatility = "VOLATILE"
- } else if commonOpt.STABLE() != nil {
- signature.Volatility = "STABLE"
- } else if commonOpt.IMMUTABLE() != nil {
- signature.Volatility = "IMMUTABLE"
- }
-
- // Extract security
- if commonOpt.SECURITY() != nil {
- if commonOpt.DEFINER() != nil {
- signature.Security = "DEFINER"
- } else if commonOpt.INVOKER() != nil {
- signature.Security = "INVOKER"
- }
- }
-
- // Extract parallel safety
- if commonOpt.PARALLEL() != nil && commonOpt.Colid() != nil {
- signature.Parallel = strings.ToUpper(commonOpt.Colid().GetText())
- }
-
- // Extract LEAKPROOF
- if commonOpt.LEAKPROOF() != nil {
- signature.Leakproof = true
- }
-
- // Extract STRICT
- if commonOpt.STRICT_P() != nil || commonOpt.RETURNS() != nil {
- signature.Strict = true
- }
-
- // Extract COST
- if commonOpt.COST() != nil && commonOpt.Numericonly() != nil {
- signature.Cost = commonOpt.Numericonly().GetText()
- }
-
- // Extract ROWS
- if commonOpt.ROWS() != nil && commonOpt.Numericonly() != nil {
- signature.Rows = commonOpt.Numericonly().GetText()
- }
- }
-}
-
-// normalizeDataType normalizes PostgreSQL data types for comparison.
-func (*FunctionSignatureVisitor) normalizeDataType(dataType string) string {
- if dataType == "" {
- return ""
- }
-
- // Normalize common PostgreSQL data type aliases
- dataType = strings.ToLower(strings.TrimSpace(dataType))
-
- // Handle common aliases
- switch dataType {
- case "int4":
- return "integer"
- case "int8":
- return "bigint"
- case "int2":
- return "smallint"
- case "float8":
- return "double precision"
- case "float4":
- return "real"
- case "bool":
- return "boolean"
- case "varchar":
- return "character varying"
- case "char":
- return "character"
- default:
- return dataType
- }
-}
-
-// signatureEqual compares two function signatures for equality.
-func signatureEqual(sig1, sig2 *FunctionSignature) bool {
- // Compare function name (case-insensitive, ignoring schema qualification)
- if !normalizedFunctionNamesEqual(sig1.Name, sig2.Name) {
- return false
- }
-
- // Compare parameters
- if len(sig1.Parameters) != len(sig2.Parameters) {
- return false
- }
-
- for i, param1 := range sig1.Parameters {
- param2 := sig2.Parameters[i]
- if !parameterEqual(param1, param2) {
- return false
- }
- }
-
- // Compare return type using PostgreSQL-aware type normalization
- if !postgreSQLTypeStringsEqual(sig1.ReturnType, sig2.ReturnType) {
- return false
- }
-
- return true
-}
-
-// parameterEqual compares two function parameters for equality.
-func parameterEqual(param1, param2 FunctionParameter) bool {
- // Compare parameter mode
- if !strings.EqualFold(param1.Mode, param2.Mode) {
- return false
- }
-
- // Compare parameter name (case-insensitive for PostgreSQL)
- if !strings.EqualFold(param1.Name, param2.Name) {
- return false
- }
-
- // Compare data type using PostgreSQL-aware type normalization
- if !postgreSQLTypeStringsEqual(param1.DataType, param2.DataType) {
- return false
- }
-
- // Compare default value using semantic comparison
- if !ast.CompareExpressionsSemantically(param1.Default, param2.Default) {
- return false
- }
-
- return true
-}
-
-// compareFunctionAttributes compares function attributes like comment, volatility, etc.
-func compareFunctionAttributes(oldFunc, newFunc *storepb.FunctionMetadata) (bool, []string) {
- var changedAttrs []string
-
- // Compare comment
- if oldFunc.Comment != newFunc.Comment {
- changedAttrs = append(changedAttrs, "comment")
- }
-
- // Compare other PostgreSQL-specific attributes if they exist in the metadata
- // Note: Most PostgreSQL function attributes are part of the definition,
- // so they would be captured by signature or body comparison
-
- return len(changedAttrs) > 0, changedAttrs
-}
-
-// compareFunctionBody compares function bodies using strict string comparison.
-func compareFunctionBody(oldDef, newDef string) bool {
- // Extract the function body from the complete definition
- // Dollar quotes are already removed during extraction
- oldBody := extractFunctionBody(oldDef)
- newBody := extractFunctionBody(newDef)
-
- // Use strict string comparison for the extracted body content
- return strings.TrimSpace(oldBody) != strings.TrimSpace(newBody)
-}
-
-// extractFunctionBody extracts the function body from a complete function definition using ANTLR.
-func extractFunctionBody(definition string) string {
- // Use ANTLR to parse and extract the function body
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := pgparser.NewPostgreSQLParser(stream)
-
- tree := parser.Root()
- if tree == nil {
- // Fallback to returning the entire definition if parsing fails
- return strings.TrimSpace(definition)
- }
-
- // Create a visitor to extract the function body
- visitor := &FunctionBodyVisitor{}
- if body := visitor.Visit(tree); body != nil {
- if bodyStr, ok := body.(string); ok {
- return strings.TrimSpace(bodyStr)
- }
- }
-
- // Fallback to returning the entire definition
- return strings.TrimSpace(definition)
-}
-
-// FunctionBodyVisitor extracts function body from ANTLR parse tree.
-type FunctionBodyVisitor struct {
- *pgparser.BasePostgreSQLParserVisitor
-}
-
-// Visit implements the visitor pattern for extracting function body.
-func (v *FunctionBodyVisitor) Visit(tree antlr.ParseTree) any {
- switch t := tree.(type) {
- case *pgparser.CreatefunctionstmtContext:
- return v.visitCreateFunctionStmt(t)
- default:
- // Continue visiting children
- for i := 0; i < t.GetChildCount(); i++ {
- child := t.GetChild(i)
- if parseTree, ok := child.(antlr.ParseTree); ok {
- if result := v.Visit(parseTree); result != nil {
- return result
- }
- }
- }
- }
- return nil
-}
-
-// visitCreateFunctionStmt extracts the function body from CREATE FUNCTION/PROCEDURE statement.
-// Note: In PostgreSQL, CREATE FUNCTION and CREATE PROCEDURE use the same grammar rule.
-// We support both FUNCTION and PROCEDURE as they have similar body extraction logic.
-func (v *FunctionBodyVisitor) visitCreateFunctionStmt(ctx *pgparser.CreatefunctionstmtContext) any {
- // In PostgreSQL, CREATE FUNCTION and CREATE PROCEDURE use the same grammar rule
- // We support both FUNCTION and PROCEDURE
- if ctx.FUNCTION() == nil && ctx.PROCEDURE() == nil {
- // This is neither CREATE FUNCTION nor CREATE PROCEDURE - skip it
- return nil
- }
-
- if ctx.Createfunc_opt_list() == nil {
- return nil
- }
-
- // Look for AS clause in function options
- for _, optCtx := range ctx.Createfunc_opt_list().AllCreatefunc_opt_item() {
- if optCtx.AS() != nil && optCtx.Func_as() != nil {
- // Extract the function body from func_as
- return v.extractBodyFromFuncAs(optCtx.Func_as())
- }
- }
-
- return nil
-}
-
-// extractBodyFromFuncAs extracts the actual function body from func_as context using ANTLR-based parsing.
-func (*FunctionBodyVisitor) extractBodyFromFuncAs(ctx pgparser.IFunc_asContext) string {
- if ctx == nil {
- return ""
- }
-
- // Use ANTLR parser to properly handle all types of string constants including dollar quotes
- sconstList := ctx.AllSconst()
- if len(sconstList) > 0 {
- // Use the postgresql parser's standard function to extract routine body
- // This properly handles dollar quotes, regular quotes, and escape sequences
- if sconstCtx, ok := sconstList[0].(*pgparser.SconstContext); ok {
- return pgparser.GetRoutineBodyString(sconstCtx)
- }
- }
-
- return ""
-}
-
-// determineChangeType determines the overall type of change in the function.
-func determineChangeType(signatureChanged, bodyChanged, attributesChanged bool) FunctionChangeType {
- if !signatureChanged && !bodyChanged && !attributesChanged {
- return FunctionChangeNone
- }
-
- if signatureChanged && (bodyChanged || attributesChanged) {
- return FunctionChangeBoth
- }
-
- if signatureChanged {
- return FunctionChangeSignature
- }
-
- if bodyChanged {
- return FunctionChangeBodyOnly
- }
-
- if attributesChanged {
- return FunctionChangeAttributes
- }
-
- return FunctionChangeNone
-}
-
-// determineMigrationStrategy determines whether to use ALTER FUNCTION or DROP/CREATE.
-func determineMigrationStrategy(changeType FunctionChangeType, changedAttrs []string) (requiresRecreation, canUseAlterFunction bool) {
- switch changeType {
- case FunctionChangeNone:
- return false, false
-
- case FunctionChangeBodyOnly:
- // Body changes can use CREATE OR REPLACE (ALTER strategy)
- return false, true
-
- case FunctionChangeSignature:
- // Signature changes require DROP/CREATE
- return true, false
-
- case FunctionChangeAttributes:
- // Some attributes can be changed with ALTER FUNCTION
- for _, attr := range changedAttrs {
- switch attr {
- case "comment":
- // Comments can be changed separately with COMMENT ON FUNCTION
- canUseAlterFunction = true
- default:
- // Most other attributes require DROP/CREATE
- requiresRecreation = true
- }
- }
- return requiresRecreation, canUseAlterFunction
-
- case FunctionChangeBoth:
- // Both signature and body/attributes changed - requires DROP/CREATE
- return true, false
-
- default:
- return true, false
- }
-}
-
-// postgreSQLTypeStringsEqual compares two PostgreSQL type strings, handling type aliases and format differences.
-func postgreSQLTypeStringsEqual(type1, type2 string) bool {
- // Normalize both types and compare
- normalized1 := normalizePostgreSQLTypeAlias(type1)
- normalized2 := normalizePostgreSQLTypeAlias(type2)
-
- return strings.EqualFold(normalized1, normalized2)
-}
-
-// normalizePostgreSQLTypeAlias normalizes PostgreSQL type names to handle aliases and format differences.
-func normalizePostgreSQLTypeAlias(typeName string) string {
- // Trim leading/trailing whitespace and convert to lowercase for comparison
- typeName = strings.TrimSpace(strings.ToLower(typeName))
-
- // Handle PostgreSQL type aliases
- typeAliases := map[string]string{
- "varchar": "character varying",
- "charactervarying": "character varying", // Handle space-removed version
- "char": "character",
- "int": "integer",
- "int4": "integer",
- "int8": "bigint",
- "int2": "smallint",
- "float4": "real",
- "float8": "double precision",
- "doubleprecision": "double precision", // Handle space-removed version
- "bool": "boolean",
- "decimal": "numeric",
- "text": "text",
- "bytea": "bytea",
- "timestamp": "timestamp without time zone",
- "timestampwithouttime zone": "timestamp without time zone", // Handle space-removed version
- "timestampwithtimezone": "timestamp with time zone", // Handle space-removed version
- "timestamptz": "timestamp with time zone",
- "time": "time without time zone",
- "timewithouttime zone": "time without time zone", // Handle space-removed version
- "timewithtimezone": "time with time zone", // Handle space-removed version
- "timetz": "time with time zone",
- "date": "date",
- "interval": "interval",
- "uuid": "uuid",
- "json": "json",
- "jsonb": "jsonb",
- "xml": "xml",
- "money": "money",
- }
-
- // Check if it's an alias
- if canonical, exists := typeAliases[typeName]; exists {
- return canonical
- }
-
- // Handle parameterized types like VARCHAR(255) -> character varying
- if strings.HasPrefix(typeName, "varchar(") {
- return "character varying"
- }
- if strings.HasPrefix(typeName, "char(") {
- return "character"
- }
- if strings.HasPrefix(typeName, "decimal(") || strings.HasPrefix(typeName, "numeric(") {
- return "numeric"
- }
- if strings.HasPrefix(typeName, "timestamp(") {
- if strings.Contains(typeName, "time zone") {
- return "timestamp with time zone"
- }
- return "timestamp without time zone"
- }
- if strings.HasPrefix(typeName, "time(") {
- if strings.Contains(typeName, "time zone") {
- return "time with time zone"
- }
- return "time without time zone"
- }
-
- return typeName
-}
-
-// normalizedFunctionNamesEqual compares function names ignoring schema qualification.
-func normalizedFunctionNamesEqual(name1, name2 string) bool {
- // Extract unqualified names (remove schema prefix)
- unqualified1 := extractUnqualifiedName(name1)
- unqualified2 := extractUnqualifiedName(name2)
-
- // Compare case-insensitively
- return strings.EqualFold(unqualified1, unqualified2)
-}
-
-// extractUnqualifiedName removes schema qualification from a function name.
-func extractUnqualifiedName(qualifiedName string) string {
- // Handle schema.function_name -> function_name
- parts := strings.Split(qualifiedName, ".")
- if len(parts) > 1 {
- return parts[len(parts)-1] // Return the last part (function name)
- }
- return qualifiedName
-}
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.go
deleted file mode 100644
index ffaacaed3a..0000000000
--- a/backend/plugin/schema/pg/generate_migration.go
+++ /dev/null
@@ -1,5192 +0,0 @@
-package pg
-
-import (
- "fmt"
- "slices"
- "strconv"
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- pgparser "github.com/bytebase/parser/postgresql"
- "github.com/pkg/errors"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- pgpluginparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/plugin/schema/pg/ast"
-)
-
-// quoteIndexExpression wraps a PostgreSQL identifier in double quotes when it
-// contains uppercase letters (to preserve case sensitivity). Complex expressions
-// (function calls, operators) and already-quoted identifiers are unchanged.
-func quoteIndexExpression(expr string) string {
- if expr == "" || strings.HasPrefix(expr, `"`) {
- return expr
- }
- if strings.ContainsAny(expr, "() +-*/,:`'") {
- return expr
- }
- // Only quote if the identifier contains uppercase letters,
- // which require quoting to preserve case in PostgreSQL.
- for _, r := range expr {
- if r >= 'A' && r <= 'Z' {
- return `"` + expr + `"`
- }
- }
- return expr
-}
-
-func init() {
- schema.RegisterGenerateMigration(storepb.Engine_POSTGRES, generateMigration)
-}
-
-func generateMigration(diff *schema.MetadataDiff) (string, error) {
- var buf strings.Builder
-
- // Safe order for migrations:
- // 1. Drop dependent objects first (in reverse dependency order)
- // - Use topological sort to drop in safe order
- // 2. Create/Alter objects (in dependency order)
- // - Use topological sort to create in safe order
-
- // Phase 1: Drop dependent objects using topological sort
- dropObjectsInOrder(diff, &buf)
-
- // Only add blank line if we have drops AND we're about to create something
- dropPhaseHasContent := buf.Len() > 0
- createPhaseWillHaveContent := hasCreateOrAlterObjects(diff)
-
- if dropPhaseHasContent && createPhaseWillHaveContent {
- _, _ = buf.WriteString("\n")
- }
-
- // Phase 2: Create/Alter objects using topological sort
- if err := createObjectsInOrder(diff, &buf); err != nil {
- return "", err
- }
-
- return buf.String(), nil
-}
-
-// dropObjectsInOrder drops all objects in reverse topological order (most dependent first)
-func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {
- // Drop event triggers first (before everything else)
- // Event triggers are database-level objects that depend on functions
- for _, etDiff := range diff.EventTriggerChanges {
- if etDiff.Action == schema.MetadataDiffActionDrop {
- writeDropEventTrigger(buf, etDiff.EventTriggerName)
- }
- }
-
- // Next, drop all triggers that might depend on functions we're about to drop
- // This is necessary because PostgreSQL doesn't allow dropping functions that are used by triggers
- functionsBeingDropped := make(map[string]bool)
- for _, funcDiff := range diff.FunctionChanges {
- if funcDiff.Action == schema.MetadataDiffActionDrop {
- funcName := strings.ToLower(funcDiff.FunctionName)
- functionsBeingDropped[funcName] = true
- }
- }
-
- // Drop all triggers first (before dropping functions or tables they depend on)
- // This avoids dependency errors when dropping functions or tables
- for _, tableDiff := range diff.TableChanges {
- // Use TriggerChanges for AST-only mode, OldTable.Triggers for metadata mode
- if len(tableDiff.TriggerChanges) > 0 {
- // AST-only mode: use TriggerChanges
- for _, triggerDiff := range tableDiff.TriggerChanges {
- if triggerDiff.Action == schema.MetadataDiffActionDrop {
- writeDropTrigger(buf, tableDiff.SchemaName, tableDiff.TableName, triggerDiff.TriggerName)
- }
- }
- } else if tableDiff.OldTable != nil && tableDiff.Action == schema.MetadataDiffActionDrop {
- // Metadata mode: use OldTable.Triggers
- // Only drop triggers for tables being dropped
- for _, trigger := range tableDiff.OldTable.Triggers {
- writeDropTrigger(buf, tableDiff.SchemaName, tableDiff.TableName, trigger.Name)
- }
- }
- }
-
- // Also drop triggers for ALTER table operations
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- for _, triggerDiff := range tableDiff.TriggerChanges {
- if triggerDiff.Action == schema.MetadataDiffActionDrop {
- writeDropTrigger(buf, tableDiff.SchemaName, tableDiff.TableName, triggerDiff.TriggerName)
- }
- }
- }
- }
-
- // Build dependency graph for all objects being dropped or altered
- graph := base.NewGraph()
-
- // Maps to store different object types
- viewMap := make(map[string]*schema.ViewDiff)
- materializedViewMap := make(map[string]*schema.MaterializedViewDiff)
- tableMap := make(map[string]*schema.TableDiff)
- functionMap := make(map[string]*schema.FunctionDiff)
-
- // Track all object IDs for dependency resolution
- allObjects := make(map[string]bool)
-
- // Collect schemas being dropped to ensure their objects are dropped first
- schemasBeingDropped := make(map[string]bool)
- for _, schemaDiff := range diff.SchemaChanges {
- if schemaDiff.Action == schema.MetadataDiffActionDrop {
- schemasBeingDropped[schemaDiff.SchemaName] = true
- }
- }
-
- // Build temporary metadata for AST-only mode dependency extraction
- // This metadata only contains tables and views that are being dropped,
- // allowing GetQuerySpan to find them when extracting view dependencies
- tempMetadata := buildTempMetadataForDrop(diff)
-
- // Add views to graph
- for _, viewDiff := range diff.ViewChanges {
- if viewDiff.Action == schema.MetadataDiffActionDrop || viewDiff.Action == schema.MetadataDiffActionAlter {
- viewID := getMigrationObjectID(viewDiff.SchemaName, viewDiff.ViewName)
- graph.AddNode(viewID)
- viewMap[viewID] = viewDiff
- allObjects[viewID] = true
- }
- }
-
- // Add materialized views to graph
- for _, mvDiff := range diff.MaterializedViewChanges {
- if mvDiff.Action == schema.MetadataDiffActionDrop || mvDiff.Action == schema.MetadataDiffActionAlter {
- mvID := getMigrationObjectID(mvDiff.SchemaName, mvDiff.MaterializedViewName)
- graph.AddNode(mvID)
- materializedViewMap[mvID] = mvDiff
- allObjects[mvID] = true
- }
- }
-
- // Add functions to graph
- for _, funcDiff := range diff.FunctionChanges {
- if funcDiff.Action == schema.MetadataDiffActionDrop {
- funcID := getMigrationObjectID(funcDiff.SchemaName, funcDiff.FunctionName)
- graph.AddNode(funcID)
- functionMap[funcID] = funcDiff
- allObjects[funcID] = true
- }
- }
-
- // Add tables to graph
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionDrop {
- tableID := getMigrationObjectID(tableDiff.SchemaName, tableDiff.TableName)
- graph.AddNode(tableID)
- tableMap[tableID] = tableDiff
- allObjects[tableID] = true
- }
- }
-
- // Add dependency edges
- // For views depending on tables/views
- for viewID, viewDiff := range viewMap {
- var dependencies []*storepb.DependencyColumn
-
- if viewDiff.OldView != nil {
- // Use metadata if available
- dependencies = viewDiff.OldView.DependencyColumns
- } else if viewDiff.OldASTNode != nil {
- // Extract dependencies from AST node for AST-only mode
- // Use the temporary metadata containing objects being dropped
- dependencies = getViewDependenciesFromAST(viewDiff.OldASTNode, viewDiff.SchemaName, tempMetadata)
- }
-
- for _, dep := range dependencies {
- depID := getMigrationObjectID(dep.Schema, dep.Table)
- if allObjects[depID] {
- // Edge from dependent to dependency (view depends on table/view)
- graph.AddEdge(viewID, depID)
- }
- }
- }
-
- // For materialized views depending on tables/views
- for mvID, mvDiff := range materializedViewMap {
- var dependencies []*storepb.DependencyColumn
-
- if mvDiff.OldMaterializedView != nil {
- // Use metadata if available
- dependencies = mvDiff.OldMaterializedView.DependencyColumns
- } else if mvDiff.OldASTNode != nil {
- // Extract dependencies from AST node for AST-only mode
- // Use the temporary metadata containing objects being dropped
- dependencies = getMaterializedViewDependenciesFromAST(mvDiff.OldASTNode, mvDiff.SchemaName, tempMetadata)
- }
-
- for _, dep := range dependencies {
- depID := getMigrationObjectID(dep.Schema, dep.Table)
- if allObjects[depID] {
- // Edge from dependent to dependency (materialized view depends on table/view)
- // For DROP: mv -> view/table means mv should be dropped before view/table
- graph.AddEdge(mvID, depID)
- }
- }
- }
-
- // For functions depending on tables
- for funcID, funcDiff := range functionMap {
- if funcDiff.OldFunction != nil {
- for _, dep := range funcDiff.OldFunction.DependencyTables {
- depID := getMigrationObjectID(dep.Schema, dep.Table)
- if allObjects[depID] {
- // Edge from function to table
- graph.AddEdge(funcID, depID)
- }
- }
- }
- }
-
- // For tables with foreign keys depending on other tables (DROP operations)
- for tableID, tableDiff := range tableMap {
- var foreignKeys []*storepb.ForeignKeyMetadata
-
- if tableDiff.OldTable != nil {
- // Metadata mode: use ForeignKeys from metadata
- foreignKeys = tableDiff.OldTable.ForeignKeys
- } else if tableDiff.OldASTNode != nil {
- // AST-only mode: extract foreign keys from AST node
- foreignKeys = extractForeignKeysFromAST(tableDiff.OldASTNode, tableDiff.SchemaName)
- }
-
- for _, fk := range foreignKeys {
- depID := getMigrationObjectID(fk.ReferencedSchema, fk.ReferencedTable)
- if allObjects[depID] && depID != tableID {
- // Edge from table with FK to referenced table
- // For DROP: table1 (with FK) -> table2 (referenced)
- // This ensures table1 is dropped before table2
- graph.AddEdge(tableID, depID)
- }
- }
- }
-
- // Get topological order
- orderedList, err := graph.TopologicalSort()
- if err != nil {
- // If there's a cycle, fall back to a safe order
- // First drop foreign keys from ALTER operations that might reference tables being dropped
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- for _, fkDiff := range tableDiff.ForeignKeyChanges {
- if fkDiff.Action == schema.MetadataDiffActionDrop {
- if fkDiff.OldForeignKey != nil {
- // Metadata mode: use constraint metadata
- writeDropForeignKey(buf, tableDiff.SchemaName, tableDiff.TableName, fkDiff.OldForeignKey.Name)
- } else if fkDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := fkDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropForeignKeyFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
- }
- }
-
- // Triggers have already been dropped at the beginning of generateDrops function
- // to avoid dependency errors with functions and tables
-
- // Drop foreign keys from tables being dropped
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionDrop && tableDiff.OldTable != nil {
- for _, fk := range tableDiff.OldTable.ForeignKeys {
- writeDropForeignKey(buf, tableDiff.SchemaName, tableDiff.TableName, fk.Name)
- }
- }
- }
-
- // Drop views
- for _, viewDiff := range viewMap {
- if viewDiff.Action == schema.MetadataDiffActionDrop {
- writeDropView(buf, viewDiff.SchemaName, viewDiff.ViewName)
- }
- }
-
- // Drop materialized views
- for _, mvDiff := range materializedViewMap {
- if mvDiff.Action == schema.MetadataDiffActionDrop {
- writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)
- }
- }
-
- // Drop functions
- for _, funcDiff := range functionMap {
- definition := getFunctionDefinitionForDrop(funcDiff)
- writeDropFunction(buf, funcDiff.SchemaName, funcDiff.FunctionName, funcDiff.OldASTNode, definition)
- }
-
- // Drop tables
- for _, tableDiff := range tableMap {
- writeDropTable(buf, tableDiff.SchemaName, tableDiff.TableName)
- }
-
- // Handle remaining ALTER table operations (constraints, indexes, columns)
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- // Drop triggers
- for _, triggerDiff := range tableDiff.TriggerChanges {
- if triggerDiff.Action == schema.MetadataDiffActionDrop {
- // Skip triggers with empty names (defensive check)
- if triggerDiff.TriggerName == "" {
- continue
- }
- writeDropTrigger(buf, tableDiff.SchemaName, tableDiff.TableName, triggerDiff.TriggerName)
- }
- }
-
- // Drop check constraints
- for _, checkDiff := range tableDiff.CheckConstraintChanges {
- if checkDiff.Action == schema.MetadataDiffActionDrop {
- if checkDiff.OldCheckConstraint != nil {
- // Metadata mode: use constraint metadata
- writeDropCheckConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, checkDiff.OldCheckConstraint.Name)
- } else if checkDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := checkDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropCheckConstraintFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
- // Drop EXCLUDE constraints
- for _, excludeDiff := range tableDiff.ExcludeConstraintChanges {
- if excludeDiff.Action == schema.MetadataDiffActionDrop {
- if excludeDiff.OldExcludeConstraint != nil {
- // Metadata mode: use constraint metadata
- writeDropConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, excludeDiff.OldExcludeConstraint.Name)
- } else if excludeDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := excludeDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropExcludeConstraintFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
- // Drop primary key constraints
- for _, pkDiff := range tableDiff.PrimaryKeyChanges {
- if pkDiff.Action == schema.MetadataDiffActionDrop {
- if pkDiff.OldPrimaryKey != nil {
- // Metadata mode: use constraint metadata
- writeDropConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, pkDiff.OldPrimaryKey.Name)
- } else if pkDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := pkDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropPrimaryKeyFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
-
- // Drop indexes
- for _, indexDiff := range tableDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionDrop {
- if indexDiff.OldIndex != nil {
- // Metadata mode: use index metadata
- if indexDiff.OldIndex.IsConstraint {
- writeDropConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, indexDiff.OldIndex.Name)
- } else {
- writeDropIndex(buf, tableDiff.SchemaName, indexDiff.OldIndex.Name)
- }
- } else if indexDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if indexAST, ok := indexDiff.OldASTNode.(*pgparser.IndexstmtContext); ok {
- writeDropIndexFromAST(buf, tableDiff.SchemaName, indexAST)
- }
- }
- }
- }
-
- // Drop columns
- for _, colDiff := range tableDiff.ColumnChanges {
- if colDiff.Action == schema.MetadataDiffActionDrop {
- if colDiff.OldColumn != nil {
- // Metadata mode - use column metadata
- writeDropColumn(buf, tableDiff.SchemaName, tableDiff.TableName, colDiff.OldColumn.Name)
- } else if colDiff.OldASTNode != nil {
- // AST-only mode - use AST functions
- _ = writeDropColumnFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, colDiff.OldASTNode)
- }
- }
- }
- }
- }
- } else {
- // First, handle ALTER table drops (foreign keys that might reference tables being dropped)
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- // Drop foreign keys first
- for _, fkDiff := range tableDiff.ForeignKeyChanges {
- if fkDiff.Action == schema.MetadataDiffActionDrop {
- if fkDiff.OldForeignKey != nil {
- // Metadata mode: use constraint metadata
- writeDropForeignKey(buf, tableDiff.SchemaName, tableDiff.TableName, fkDiff.OldForeignKey.Name)
- } else if fkDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := fkDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropForeignKeyFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
- }
- }
-
- // Drop in topological order (most dependent first)
- // Note: Triggers have already been dropped at the beginning of generateDrops
- for _, objID := range orderedList {
- // Drop the object itself
- if viewDiff, ok := viewMap[objID]; ok {
- if viewDiff.Action == schema.MetadataDiffActionDrop {
- writeDropView(buf, viewDiff.SchemaName, viewDiff.ViewName)
- }
- } else if mvDiff, ok := materializedViewMap[objID]; ok {
- if mvDiff.Action == schema.MetadataDiffActionDrop {
- writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)
- }
- } else if funcDiff, ok := functionMap[objID]; ok {
- definition := getFunctionDefinitionForDrop(funcDiff)
- writeDropFunction(buf, funcDiff.SchemaName, funcDiff.FunctionName, funcDiff.OldASTNode, definition)
- } else if tableDiff, ok := tableMap[objID]; ok {
- // Drop foreign keys before table
- if tableDiff.OldTable != nil {
- for _, fk := range tableDiff.OldTable.ForeignKeys {
- writeDropForeignKey(buf, tableDiff.SchemaName, tableDiff.TableName, fk.Name)
- }
- }
- writeDropTable(buf, tableDiff.SchemaName, tableDiff.TableName)
- }
- }
-
- // Handle remaining ALTER table drops (constraints, indexes, columns)
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- // Drop triggers
- for _, triggerDiff := range tableDiff.TriggerChanges {
- if triggerDiff.Action == schema.MetadataDiffActionDrop {
- // Skip triggers with empty names (defensive check)
- if triggerDiff.TriggerName == "" {
- continue
- }
- writeDropTrigger(buf, tableDiff.SchemaName, tableDiff.TableName, triggerDiff.TriggerName)
- }
- }
-
- // Drop check constraints
- for _, checkDiff := range tableDiff.CheckConstraintChanges {
- if checkDiff.Action == schema.MetadataDiffActionDrop {
- if checkDiff.OldCheckConstraint != nil {
- // Metadata mode: use constraint metadata
- writeDropCheckConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, checkDiff.OldCheckConstraint.Name)
- } else if checkDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := checkDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropCheckConstraintFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
- // Drop EXCLUDE constraints
- for _, excludeDiff := range tableDiff.ExcludeConstraintChanges {
- if excludeDiff.Action == schema.MetadataDiffActionDrop {
- if excludeDiff.OldExcludeConstraint != nil {
- // Metadata mode: use constraint metadata
- writeDropConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, excludeDiff.OldExcludeConstraint.Name)
- } else if excludeDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := excludeDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropExcludeConstraintFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
- // Drop primary key constraints
- for _, pkDiff := range tableDiff.PrimaryKeyChanges {
- if pkDiff.Action == schema.MetadataDiffActionDrop {
- if pkDiff.OldPrimaryKey != nil {
- // Metadata mode: use constraint metadata
- writeDropConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, pkDiff.OldPrimaryKey.Name)
- } else if pkDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := pkDiff.OldASTNode.(pgparser.ITableconstraintContext); ok {
- writeDropPrimaryKeyFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST)
- }
- }
- }
- }
-
- // Drop indexes
- for _, indexDiff := range tableDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionDrop {
- if indexDiff.OldIndex != nil {
- // Metadata mode: use index metadata
- if indexDiff.OldIndex.IsConstraint {
- writeDropConstraint(buf, tableDiff.SchemaName, tableDiff.TableName, indexDiff.OldIndex.Name)
- } else {
- writeDropIndex(buf, tableDiff.SchemaName, indexDiff.OldIndex.Name)
- }
- } else if indexDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if indexAST, ok := indexDiff.OldASTNode.(*pgparser.IndexstmtContext); ok {
- writeDropIndexFromAST(buf, tableDiff.SchemaName, indexAST)
- }
- }
- }
- }
-
- // Drop columns
- for _, colDiff := range tableDiff.ColumnChanges {
- if colDiff.Action == schema.MetadataDiffActionDrop {
- if colDiff.OldColumn != nil {
- // Metadata mode - use column metadata
- writeDropColumn(buf, tableDiff.SchemaName, tableDiff.TableName, colDiff.OldColumn.Name)
- } else if colDiff.OldASTNode != nil {
- // AST-only mode - use AST functions
- _ = writeDropColumnFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, colDiff.OldASTNode)
- }
- }
- }
- }
- }
-
- // Handle remaining ALTER materialized view drops (indexes)
- for _, mvDiff := range diff.MaterializedViewChanges {
- if mvDiff.Action == schema.MetadataDiffActionAlter {
- // Drop indexes
- for _, indexDiff := range mvDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionDrop {
- if indexDiff.OldIndex != nil {
- // Metadata mode: use index metadata
- if indexDiff.OldIndex.IsConstraint {
- writeDropConstraint(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName, indexDiff.OldIndex.Name)
- } else {
- writeDropIndex(buf, mvDiff.SchemaName, indexDiff.OldIndex.Name)
- }
- } else if indexDiff.OldASTNode != nil {
- // AST-only mode: extract from AST node
- if indexAST, ok := indexDiff.OldASTNode.(*pgparser.IndexstmtContext); ok {
- writeDropIndexFromAST(buf, mvDiff.SchemaName, indexAST)
- }
- }
- }
- }
- }
- }
- }
-
- // Drop enum types
- for _, enumDiff := range diff.EnumTypeChanges {
- if enumDiff.Action == schema.MetadataDiffActionDrop {
- writeDropType(buf, enumDiff.SchemaName, enumDiff.EnumTypeName)
- }
- }
-
- // Drop extensions (after enum types and tables)
- for _, extDiff := range diff.ExtensionChanges {
- if extDiff.Action == schema.MetadataDiffActionDrop {
- writeDropExtension(buf, extDiff.ExtensionName)
- }
- }
-
- // Drop sequences - prioritize sequences in schemas being dropped
- var sequencesInDroppedSchemas []string
- var otherSequences []string
-
- for _, seqDiff := range diff.SequenceChanges {
- if seqDiff.Action == schema.MetadataDiffActionDrop {
- seqInfo := fmt.Sprintf("%s.%s", seqDiff.SchemaName, seqDiff.SequenceName)
- if schemasBeingDropped[seqDiff.SchemaName] {
- sequencesInDroppedSchemas = append(sequencesInDroppedSchemas, seqInfo)
- } else {
- otherSequences = append(otherSequences, seqInfo)
- }
- }
- }
-
- // Drop sequences in schemas being dropped first
- for _, seqInfo := range sequencesInDroppedSchemas {
- parts := strings.Split(seqInfo, ".")
- writeDropSequence(buf, parts[0], parts[1])
- }
-
- // Then drop other sequences
- for _, seqInfo := range otherSequences {
- parts := strings.Split(seqInfo, ".")
- writeDropSequence(buf, parts[0], parts[1])
- }
-
- // Drop schemas (must be empty)
- for _, schemaDiff := range diff.SchemaChanges {
- if schemaDiff.Action == schema.MetadataDiffActionDrop {
- // Skip dropping pg_catalog and public schemas as they are system schemas
- if schemaDiff.SchemaName == "pg_catalog" || schemaDiff.SchemaName == "public" {
- continue
- }
-
- // Before dropping a schema, we need to drop all objects inside it
- // This handles cases where the diff doesn't include objects within a schema being dropped
- if schemaDiff.OldSchema != nil {
- // Drop all objects in the schema in the correct order
-
- // Drop views first (they might depend on tables)
- for _, view := range schemaDiff.OldSchema.Views {
- writeDropView(buf, schemaDiff.SchemaName, view.Name)
- }
-
- // Drop materialized views
- for _, mv := range schemaDiff.OldSchema.MaterializedViews {
- writeDropMaterializedView(buf, schemaDiff.SchemaName, mv.Name)
- }
-
- // Drop functions
- for _, fn := range schemaDiff.OldSchema.Functions {
- // In metadata mode, no AST node is available, so pass nil
- writeDropFunction(buf, schemaDiff.SchemaName, fn.Name, nil, fn.Definition)
- }
-
- // Drop tables (this will handle foreign keys internally)
- for _, table := range schemaDiff.OldSchema.Tables {
- // Drop triggers first
- for _, trigger := range table.Triggers {
- writeDropTrigger(buf, schemaDiff.SchemaName, table.Name, trigger.Name)
- }
-
- // Drop the table
- writeDropTable(buf, schemaDiff.SchemaName, table.Name)
- }
-
- // Drop sequences
- for _, seq := range schemaDiff.OldSchema.Sequences {
- writeDropSequence(buf, schemaDiff.SchemaName, seq.Name)
- }
-
- // Drop types (enums)
- for _, enum := range schemaDiff.OldSchema.EnumTypes {
- writeDropType(buf, schemaDiff.SchemaName, enum.Name)
- }
- }
-
- writeDropSchema(buf, schemaDiff.SchemaName)
- } else if schemaDiff.Action == schema.MetadataDiffActionAlter {
- // Handle ALTER schema - check for enum types that need to be dropped
- // This handles cases where enum types are added to an existing schema
- if schemaDiff.OldSchema != nil && schemaDiff.NewSchema != nil {
- // Build a map of new enum types
- newEnumMap := make(map[string]bool)
- for _, enum := range schemaDiff.NewSchema.EnumTypes {
- newEnumMap[enum.Name] = true
- }
-
- // Drop enum types that exist in old but not in new
- for _, enum := range schemaDiff.OldSchema.EnumTypes {
- if !newEnumMap[enum.Name] {
- writeDropType(buf, schemaDiff.SchemaName, enum.Name)
- }
- }
- }
- }
- }
-}
-
-// createSchemas creates all new schemas.
-func createSchemas(diff *schema.MetadataDiff, buf *strings.Builder) error {
- var schemasToCreate []string
- for _, schemaDiff := range diff.SchemaChanges {
- if schemaDiff.Action == schema.MetadataDiffActionCreate {
- if schemaDiff.SchemaName != "pg_catalog" && schemaDiff.SchemaName != "public" {
- schemasToCreate = append(schemasToCreate, schemaDiff.SchemaName)
- }
- }
- }
- slices.Sort(schemasToCreate)
- for _, schemaName := range schemasToCreate {
- if err := writeCreateSchema(buf, schemaName); err != nil {
- return err
- }
- }
- if len(schemasToCreate) > 0 && (hasCreateOrAlterTables(diff) || hasCreateViewsOrFunctions(diff)) {
- _, _ = buf.WriteString("\n")
- }
- return nil
-}
-
-// createExtensions creates all new extensions.
-func createExtensions(diff *schema.MetadataDiff, buf *strings.Builder) error {
- for _, extDiff := range diff.ExtensionChanges {
- if extDiff.Action == schema.MetadataDiffActionCreate {
- if extDiff.NewExtension != nil {
- if err := writeCreateExtension(buf, extDiff.NewExtension); err != nil {
- return err
- }
- } else if extDiff.NewASTNode != nil {
- if err := writeMigrationExtensionFromAST(buf, extDiff.NewASTNode); err != nil {
- return err
- }
- }
- }
- }
- return nil
-}
-
-// createEnumTypes creates all new enum types.
-func createEnumTypes(diff *schema.MetadataDiff, buf *strings.Builder) error {
- for _, enumDiff := range diff.EnumTypeChanges {
- if enumDiff.Action == schema.MetadataDiffActionCreate {
- if enumDiff.NewEnumType != nil {
- if err := writeCreateEnumType(buf, enumDiff.SchemaName, enumDiff.NewEnumType); err != nil {
- return err
- }
- } else if enumDiff.NewASTNode != nil {
- if err := writeMigrationEnumTypeFromAST(buf, enumDiff.NewASTNode); err != nil {
- return err
- }
- }
- }
- }
- return nil
-}
-
-// alterEnumTypes generates ALTER TYPE ADD VALUE statements for enum value additions.
-func alterEnumTypes(diff *schema.MetadataDiff, buf *strings.Builder) error {
- for _, enumDiff := range diff.EnumTypeChanges {
- if enumDiff.Action == schema.MetadataDiffActionAlter {
- if enumDiff.OldEnumType != nil && enumDiff.NewEnumType != nil {
- if err := writeAlterEnumTypeAddValues(buf, enumDiff.SchemaName, enumDiff.OldEnumType, enumDiff.NewEnumType); err != nil {
- return err
- }
- }
- }
- }
- return nil
-}
-
-// writeAlterEnumTypeAddValues generates ALTER TYPE ADD VALUE statements for new enum values.
-// PostgreSQL supports adding enum values with ALTER TYPE ... ADD VALUE.
-// New values can be added at the end, before an existing value, or after an existing value.
-// For removed values, a warning comment is generated since PostgreSQL doesn't support DROP VALUE.
-func writeAlterEnumTypeAddValues(out *strings.Builder, schemaName string, oldEnum, newEnum *storepb.EnumTypeMetadata) error {
- // Build sets for comparison
- oldValueSet := make(map[string]bool)
- for _, v := range oldEnum.Values {
- oldValueSet[v] = true
- }
-
- newValueSet := make(map[string]bool)
- for _, v := range newEnum.Values {
- newValueSet[v] = true
- }
-
- // Find removed values (exist in old but not in new)
- var removedValues []string
- for _, v := range oldEnum.Values {
- if !newValueSet[v] {
- removedValues = append(removedValues, v)
- }
- }
-
- // Generate warning comment for removed values
- if len(removedValues) > 0 {
- _, _ = out.WriteString(`-- WARNING: PostgreSQL does not support removing enum values.`)
- _, _ = out.WriteString("\n")
- _, _ = out.WriteString(`-- The following enum values were removed from "`)
- _, _ = out.WriteString(schemaName)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(newEnum.Name)
- _, _ = out.WriteString(`": `)
- for i, v := range removedValues {
- if i > 0 {
- _, _ = out.WriteString(", ")
- }
- _, _ = out.WriteString("'")
- _, _ = out.WriteString(v)
- _, _ = out.WriteString("'")
- }
- _, _ = out.WriteString("\n")
- _, _ = out.WriteString(`-- To remove enum values, you must recreate the type:`)
- _, _ = out.WriteString("\n")
- _, _ = out.WriteString(`-- 1. Update existing data that uses the removed values`)
- _, _ = out.WriteString("\n")
- _, _ = out.WriteString(`-- 2. ALTER TYPE "`)
- _, _ = out.WriteString(schemaName)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(newEnum.Name)
- _, _ = out.WriteString(`" RENAME TO "`)
- _, _ = out.WriteString(newEnum.Name)
- _, _ = out.WriteString(`_old";`)
- _, _ = out.WriteString("\n")
- _, _ = out.WriteString(`-- 3. CREATE TYPE "`)
- _, _ = out.WriteString(schemaName)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(newEnum.Name)
- _, _ = out.WriteString(`" AS ENUM (...);`)
- _, _ = out.WriteString("\n")
- _, _ = out.WriteString(`-- 4. ALTER TABLE ... ALTER COLUMN ... TYPE "`)
- _, _ = out.WriteString(schemaName)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(newEnum.Name)
- _, _ = out.WriteString(`" USING ...;`)
- _, _ = out.WriteString("\n")
- _, _ = out.WriteString(`-- 5. DROP TYPE "`)
- _, _ = out.WriteString(schemaName)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(newEnum.Name)
- _, _ = out.WriteString(`_old";`)
- _, _ = out.WriteString("\n")
- }
-
- // Find new values and their positions
- for i, newValue := range newEnum.Values {
- if !oldValueSet[newValue] {
- // This is a new value, generate ALTER TYPE ADD VALUE
- _, _ = out.WriteString(`ALTER TYPE "`)
- _, _ = out.WriteString(schemaName)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(newEnum.Name)
- _, _ = out.WriteString(`" ADD VALUE '`)
- _, _ = out.WriteString(newValue)
- _, _ = out.WriteString(`'`)
-
- // Determine position: if not the last value, add BEFORE clause
- if i < len(newEnum.Values)-1 {
- // Find the next value that exists in oldEnum to use as reference
- for j := i + 1; j < len(newEnum.Values); j++ {
- if oldValueSet[newEnum.Values[j]] {
- _, _ = out.WriteString(` BEFORE '`)
- _, _ = out.WriteString(newEnum.Values[j])
- _, _ = out.WriteString(`'`)
- break
- }
- }
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
- }
- }
- return nil
-}
-
-// createSequences creates all new sequences.
-func createSequences(diff *schema.MetadataDiff, buf *strings.Builder) error {
- for _, seqDiff := range diff.SequenceChanges {
- if seqDiff.Action == schema.MetadataDiffActionCreate {
- if seqDiff.NewSequence != nil {
- if err := writeMigrationCreateSequence(buf, seqDiff.SchemaName, seqDiff.NewSequence); err != nil {
- return err
- }
- } else if seqDiff.NewASTNode != nil {
- if err := writeMigrationSequenceFromAST(buf, seqDiff.NewASTNode); err != nil {
- return err
- }
- } else {
- return errors.Errorf("sequence diff for %s.%s has neither metadata nor AST node", seqDiff.SchemaName, seqDiff.SequenceName)
- }
- }
- }
- return nil
-}
-
-// objectMaps holds maps of different object types for migration generation.
-type objectMaps struct {
- tables map[string]*schema.TableDiff
- views map[string]*schema.ViewDiff
- materializedViews map[string]*schema.MaterializedViewDiff
- functions map[string]*schema.FunctionDiff
-}
-
-// buildObjectMaps creates maps for all CREATE/ALTER objects.
-func buildObjectMaps(diff *schema.MetadataDiff) *objectMaps {
- m := &objectMaps{
- tables: make(map[string]*schema.TableDiff),
- views: make(map[string]*schema.ViewDiff),
- materializedViews: make(map[string]*schema.MaterializedViewDiff),
- functions: make(map[string]*schema.FunctionDiff),
- }
- for _, d := range diff.TableChanges {
- if d.Action == schema.MetadataDiffActionCreate || d.Action == schema.MetadataDiffActionAlter {
- m.tables[getMigrationObjectID(d.SchemaName, d.TableName)] = d
- }
- }
- for _, d := range diff.ViewChanges {
- if d.Action == schema.MetadataDiffActionCreate || d.Action == schema.MetadataDiffActionAlter {
- m.views[getMigrationObjectID(d.SchemaName, d.ViewName)] = d
- }
- }
- for _, d := range diff.MaterializedViewChanges {
- if d.Action == schema.MetadataDiffActionCreate || d.Action == schema.MetadataDiffActionAlter {
- m.materializedViews[getMigrationObjectID(d.SchemaName, d.MaterializedViewName)] = d
- }
- }
- for _, d := range diff.FunctionChanges {
- if d.Action == schema.MetadataDiffActionCreate || d.Action == schema.MetadataDiffActionAlter {
- m.functions[getMigrationObjectID(d.SchemaName, d.FunctionName)] = d
- }
- }
- return m
-}
-
-// createTables creates tables, detecting FK cycles and handling them appropriately.
-// Returns true if there's a FK cycle.
-func createTables(buf *strings.Builder, maps *objectMaps) (bool, error) {
- // Detect FK cycle in CREATE tables
- tableGraph := base.NewGraph()
- for tableID, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionCreate {
- tableGraph.AddNode(tableID)
- var foreignKeys []*storepb.ForeignKeyMetadata
- if tableDiff.NewTable != nil {
- foreignKeys = tableDiff.NewTable.ForeignKeys
- } else if tableDiff.NewASTNode != nil {
- foreignKeys = extractForeignKeysFromAST(tableDiff.NewASTNode, tableDiff.SchemaName)
- }
- for _, fk := range foreignKeys {
- depID := getMigrationObjectID(fk.ReferencedSchema, fk.ReferencedTable)
- if depID != tableID {
- tableGraph.AddEdge(depID, tableID)
- }
- }
- }
- }
- orderedList, err := tableGraph.TopologicalSort()
- hasCycle := err != nil
-
- if hasCycle {
- for _, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionCreate {
- if err := writeCreateTableDiff(buf, tableDiff, false); err != nil {
- return hasCycle, err
- }
- writeTableIndexesForCreate(buf, tableDiff)
- }
- }
- } else {
- for _, tableID := range orderedList {
- tableDiff := maps.tables[tableID]
- if tableDiff == nil {
- // Skip tables not in the map (e.g., existing referenced tables added via AddEdge)
- continue
- }
- if tableDiff.Action == schema.MetadataDiffActionCreate {
- if err := writeCreateTableDiff(buf, tableDiff, true); err != nil {
- return hasCycle, err
- }
- writeTableIndexesForCreate(buf, tableDiff)
- }
- }
- }
- return hasCycle, nil
-}
-
-// alterTables handles ALTER table operations (without FK).
-func alterTables(buf *strings.Builder, maps *objectMaps) error {
- for _, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- sql, err := generateAlterTableWithOptions(tableDiff, true)
- if err != nil {
- return err
- }
- if sql != "" {
- _, _ = buf.WriteString(sql)
- _, _ = buf.WriteString("\n")
- }
- }
- }
- return nil
-}
-
-// createViewsMVsFunctions creates views, materialized views, and functions in dependency order.
-func createViewsMVsFunctions(buf *strings.Builder, maps *objectMaps, tempMetadata *storepb.DatabaseSchemaMetadata) error {
- graph := base.NewGraph()
- nonTableObjects := make(map[string]bool)
-
- for id := range maps.views {
- graph.AddNode(id)
- nonTableObjects[id] = true
- }
- for id := range maps.materializedViews {
- graph.AddNode(id)
- nonTableObjects[id] = true
- }
- for id := range maps.functions {
- graph.AddNode(id)
- nonTableObjects[id] = true
- }
-
- // Add dependency edges for views
- for viewID, viewDiff := range maps.views {
- var deps []*storepb.DependencyColumn
- if viewDiff.NewView != nil {
- deps = viewDiff.NewView.DependencyColumns
- } else if viewDiff.NewASTNode != nil {
- deps = getViewDependenciesFromAST(viewDiff.NewASTNode, viewDiff.SchemaName, tempMetadata)
- }
- for _, dep := range deps {
- depID := getMigrationObjectID(dep.Schema, dep.Table)
- if nonTableObjects[depID] {
- graph.AddEdge(depID, viewID)
- }
- }
- }
- // Add dependency edges for materialized views
- for mvID, mvDiff := range maps.materializedViews {
- var deps []*storepb.DependencyColumn
- if mvDiff.NewMaterializedView != nil {
- deps = mvDiff.NewMaterializedView.DependencyColumns
- } else if mvDiff.NewASTNode != nil {
- deps = getMaterializedViewDependenciesFromAST(mvDiff.NewASTNode, mvDiff.SchemaName, tempMetadata)
- }
- for _, dep := range deps {
- depID := getMigrationObjectID(dep.Schema, dep.Table)
- if nonTableObjects[depID] {
- graph.AddEdge(depID, mvID)
- }
- }
- }
-
- orderedList, err := graph.TopologicalSort()
- if err != nil {
- // Fallback: create in map iteration order
- for _, d := range maps.views {
- if err := writeViewDiff(buf, d); err != nil {
- return err
- }
- }
- for _, d := range maps.materializedViews {
- if err := writeMaterializedViewDiff(buf, d); err != nil {
- return err
- }
- }
- for _, d := range maps.functions {
- if err := writeFunctionDiff(buf, d); err != nil {
- return err
- }
- if d.Action == schema.MetadataDiffActionCreate && d.NewFunction != nil && d.NewFunction.Comment != "" {
- writeCommentOnFunction(buf, d.SchemaName, d.NewFunction.Signature, d.NewFunction.Comment, d.NewASTNode, d.NewFunction.Definition)
- }
- }
- return nil
- }
-
- for _, objID := range orderedList {
- if d, ok := maps.views[objID]; ok {
- if err := writeViewDiff(buf, d); err != nil {
- return err
- }
- } else if d, ok := maps.materializedViews[objID]; ok {
- if err := writeMaterializedViewDiff(buf, d); err != nil {
- return err
- }
- } else if d, ok := maps.functions[objID]; ok {
- if err := writeFunctionDiff(buf, d); err != nil {
- return err
- }
- if d.Action == schema.MetadataDiffActionCreate && d.NewFunction != nil && d.NewFunction.Comment != "" {
- writeCommentOnFunction(buf, d.SchemaName, d.NewFunction.Signature, d.NewFunction.Comment, d.NewASTNode, d.NewFunction.Definition)
- }
- }
- }
- return nil
-}
-
-// createTriggers creates triggers for new tables.
-func createTriggers(buf *strings.Builder, maps *objectMaps) error {
- for _, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionCreate {
- for _, triggerDiff := range tableDiff.TriggerChanges {
- if triggerDiff.Action == schema.MetadataDiffActionCreate {
- if err := writeCreateTrigger(buf, triggerDiff); err != nil {
- return err
- }
- }
- }
- }
- }
- return nil
-}
-
-// addForeignKeys adds FK constraints after tables are created.
-func addForeignKeys(buf *strings.Builder, maps *objectMaps, hasFKCycle bool) error {
- if hasFKCycle {
- for _, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionCreate {
- if err := writeTableForeignKeysForCreate(buf, tableDiff); err != nil {
- return err
- }
- }
- }
- }
- for _, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- sql, err := generateAlterTableForeignKeys(tableDiff)
- if err != nil {
- return err
- }
- if sql != "" {
- _, _ = buf.WriteString(sql)
- _, _ = buf.WriteString("\n")
- }
- }
- }
- return nil
-}
-
-// createPostProcessing handles triggers, indexes, comments, and event triggers.
-func createPostProcessing(buf *strings.Builder, diff *schema.MetadataDiff, maps *objectMaps) error {
- // ALTER triggers
- for _, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- sql := generateAlterTableTriggers(tableDiff)
- if sql != "" {
- _, _ = buf.WriteString(sql)
- _, _ = buf.WriteString("\n")
- }
- }
- }
-
- // CREATE triggers not in TriggerChanges
- for _, tableDiff := range maps.tables {
- if tableDiff.Action == schema.MetadataDiffActionCreate && tableDiff.NewTable != nil {
- triggersInChanges := make(map[string]bool)
- for _, triggerDiff := range tableDiff.TriggerChanges {
- if triggerDiff.Action == schema.MetadataDiffActionCreate {
- triggersInChanges[triggerDiff.TriggerName] = true
- }
- }
- for _, trigger := range tableDiff.NewTable.Triggers {
- if !triggersInChanges[trigger.Name] {
- writeMigrationTrigger(buf, trigger)
- }
- }
- }
- }
-
- // MV indexes
- for _, mvDiff := range diff.MaterializedViewChanges {
- if mvDiff.Action == schema.MetadataDiffActionCreate && mvDiff.NewMaterializedView != nil {
- for _, index := range mvDiff.NewMaterializedView.Indexes {
- writeMigrationMaterializedViewIndex(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView.Name, index)
- }
- }
- if mvDiff.Action == schema.MetadataDiffActionAlter {
- for _, indexDiff := range mvDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionCreate {
- if indexDiff.NewIndex != nil {
- writeMigrationMaterializedViewIndex(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName, indexDiff.NewIndex)
- } else if indexDiff.NewASTNode != nil {
- if indexAST, ok := indexDiff.NewASTNode.(*pgparser.IndexstmtContext); ok {
- if err := writeCreateIndexFromAST(buf, indexAST); err != nil {
- _, _ = fmt.Fprintf(buf, "-- Error creating index: %v\n", err)
- }
- }
- }
- }
- }
- }
- }
-
- // Comments
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- if err := generateTableCommentChanges(buf, tableDiff); err != nil {
- return err
- }
- if err := generateColumnCommentChanges(buf, tableDiff); err != nil {
- return err
- }
- if err := generateIndexCommentChanges(buf, tableDiff); err != nil {
- return err
- }
- }
- }
- if err := generateSchemaCommentChanges(buf, diff); err != nil {
- return err
- }
- if err := generateViewCommentChanges(buf, diff); err != nil {
- return err
- }
- if err := generateMaterializedViewCommentChanges(buf, diff); err != nil {
- return err
- }
- for _, mvDiff := range diff.MaterializedViewChanges {
- if mvDiff.Action == schema.MetadataDiffActionAlter {
- if err := generateMaterializedViewIndexCommentChanges(buf, mvDiff); err != nil {
- return err
- }
- }
- }
- if err := generateFunctionCommentChanges(buf, diff); err != nil {
- return err
- }
- if err := generateSequenceCommentChanges(buf, diff); err != nil {
- return err
- }
- if err := generateSequenceOwnershipChanges(buf, diff); err != nil {
- return err
- }
-
- // Event triggers
- for _, etDiff := range diff.EventTriggerChanges {
- if etDiff.Action == schema.MetadataDiffActionCreate {
- if etDiff.NewEventTrigger != nil {
- if err := writeCreateEventTrigger(buf, etDiff.NewEventTrigger); err != nil {
- return err
- }
- } else if etDiff.NewASTNode != nil {
- if err := writeMigrationEventTriggerFromAST(buf, etDiff.NewASTNode); err != nil {
- return err
- }
- }
- }
- }
-
- if err := generateEnumTypeCommentChanges(buf, diff); err != nil {
- return err
- }
- return generateCommentChangesFromSDL(buf, diff)
-}
-
-// createObjectsInOrder creates all objects in topological order (dependencies first).
-func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
- if err := createSchemas(diff, buf); err != nil {
- return err
- }
- if err := createExtensions(diff, buf); err != nil {
- return err
- }
- if err := createEnumTypes(diff, buf); err != nil {
- return err
- }
- if err := alterEnumTypes(diff, buf); err != nil {
- return err
- }
- if err := createSequences(diff, buf); err != nil {
- return err
- }
-
- maps := buildObjectMaps(diff)
- tempMetadata := buildTempMetadataForCreate(diff)
-
- hasFKCycle, err := createTables(buf, maps)
- if err != nil {
- return err
- }
- if err := alterTables(buf, maps); err != nil {
- return err
- }
- if err := createViewsMVsFunctions(buf, maps, tempMetadata); err != nil {
- return err
- }
- if err := createTriggers(buf, maps); err != nil {
- return err
- }
-
- // Set sequence ownership
- for _, seqDiff := range diff.SequenceChanges {
- if seqDiff.Action == schema.MetadataDiffActionCreate && seqDiff.NewSequence != nil && seqDiff.NewSequence.OwnerTable != "" && seqDiff.NewSequence.OwnerColumn != "" {
- if err := writeMigrationSequenceOwnership(buf, seqDiff.SchemaName, seqDiff.NewSequence); err != nil {
- return err
- }
- }
- }
-
- if err := addForeignKeys(buf, maps, hasFKCycle); err != nil {
- return err
- }
- return createPostProcessing(buf, diff, maps)
-}
-
-func generateCreateTable(schemaName, tableName string, table *storepb.TableMetadata, includeForeignKeys bool) (string, error) {
- var buf strings.Builder
-
- writeMigrationCreateTable(&buf, schemaName, tableName, table.Columns, table.CheckConstraints)
-
- // Add partitioning clause if needed
- if len(table.Partitions) > 0 {
- writeMigrationPartitionClause(&buf, table.Partitions[0])
- }
-
- if _, err := buf.WriteString(";\n"); err != nil {
- return "", err
- }
-
- // Add constraints (primary key, unique)
- for _, index := range table.Indexes {
- if index.Primary {
- writeMigrationPrimaryKey(&buf, schemaName, tableName, index)
- } else if index.Unique && index.IsConstraint {
- writeMigrationUniqueKey(&buf, schemaName, tableName, index)
- }
- }
-
- // Add non-constraint indexes
- for _, index := range table.Indexes {
- if !index.IsConstraint {
- writeMigrationIndex(&buf, schemaName, tableName, index)
- }
- }
-
- // Optionally add foreign keys
- if includeForeignKeys && table != nil {
- for _, fk := range table.ForeignKeys {
- if err := writeMigrationForeignKey(&buf, schemaName, tableName, fk); err != nil {
- return "", err
- }
- }
- }
-
- return buf.String(), nil
-}
-
-func generateAlterTableWithOptions(tableDiff *schema.TableDiff, includeColumnAdditions bool) (string, error) {
- var buf strings.Builder
-
- // Add columns first (other operations might depend on them)
- if includeColumnAdditions {
- for _, colDiff := range tableDiff.ColumnChanges {
- if colDiff.Action == schema.MetadataDiffActionCreate {
- // Support both metadata mode and AST-only mode
- if colDiff.NewColumn != nil {
- // Metadata mode: use existing writeAddColumn
- writeAddColumn(&buf, tableDiff.SchemaName, tableDiff.TableName, colDiff.NewColumn)
- } else if colDiff.NewASTNode != nil {
- // AST-only mode: extract SQL from AST node
- if err := writeAddColumnFromAST(&buf, tableDiff.SchemaName, tableDiff.TableName, colDiff.NewASTNode); err != nil {
- return "", err
- }
- }
- }
- }
- }
-
- // Alter columns
- for _, colDiff := range tableDiff.ColumnChanges {
- if colDiff.Action == schema.MetadataDiffActionAlter {
- // Support both metadata mode and AST-only mode
- if colDiff.OldColumn != nil && colDiff.NewColumn != nil {
- // Metadata mode: use existing generateAlterColumn
- alterColSQL := generateAlterColumn(tableDiff.SchemaName, tableDiff.TableName, colDiff)
- _, _ = buf.WriteString(alterColSQL)
- } else if colDiff.OldASTNode != nil && colDiff.NewASTNode != nil {
- // AST-only mode: use ALTER COLUMN approach
- if err := writeAlterColumnFromAST(&buf, tableDiff.SchemaName, tableDiff.TableName, colDiff.OldASTNode, colDiff.NewASTNode); err != nil {
- return "", err
- }
- }
- }
- }
-
- // Add indexes
- for _, indexDiff := range tableDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionCreate {
- if indexDiff.NewIndex != nil {
- // Metadata mode: use index metadata
- if indexDiff.NewIndex.IsConstraint {
- // Add constraint (primary key or unique constraint)
- if indexDiff.NewIndex.Primary {
- writeMigrationPrimaryKey(&buf, tableDiff.SchemaName, tableDiff.TableName, indexDiff.NewIndex)
- } else if indexDiff.NewIndex.Unique {
- writeMigrationUniqueKey(&buf, tableDiff.SchemaName, tableDiff.TableName, indexDiff.NewIndex)
- }
- } else {
- writeMigrationIndex(&buf, tableDiff.SchemaName, tableDiff.TableName, indexDiff.NewIndex)
- }
- } else if indexDiff.NewASTNode != nil {
- // AST-only mode: extract from AST node
- if indexAST, ok := indexDiff.NewASTNode.(*pgparser.IndexstmtContext); ok {
- if err := writeCreateIndexFromAST(&buf, indexAST); err != nil {
- // If AST extraction fails, log error but continue (non-fatal)
- _, _ = fmt.Fprintf(&buf, "-- Error creating index: %v\n", err)
- }
- }
- }
- }
- }
-
- // Add check constraints
- for _, checkDiff := range tableDiff.CheckConstraintChanges {
- if checkDiff.Action == schema.MetadataDiffActionCreate {
- if checkDiff.NewCheckConstraint != nil {
- // Metadata mode: use constraint metadata
- writeAddCheckConstraint(&buf, tableDiff.SchemaName, tableDiff.TableName, checkDiff.NewCheckConstraint)
- } else if checkDiff.NewASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := checkDiff.NewASTNode.(pgparser.ITableconstraintContext); ok {
- if err := writeAddCheckConstraintFromAST(&buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST); err != nil {
- // If AST extraction fails, log error but continue (non-fatal)
- _, _ = fmt.Fprintf(&buf, "-- Error adding check constraint: %v\n", err)
- }
- }
- }
- }
- }
-
- // Add EXCLUDE constraints
- for _, excludeDiff := range tableDiff.ExcludeConstraintChanges {
- if excludeDiff.Action == schema.MetadataDiffActionCreate {
- if excludeDiff.NewExcludeConstraint != nil {
- // Metadata mode: use constraint metadata
- writeAddExcludeConstraint(&buf, tableDiff.SchemaName, tableDiff.TableName, excludeDiff.NewExcludeConstraint)
- } else if excludeDiff.NewASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := excludeDiff.NewASTNode.(pgparser.ITableconstraintContext); ok {
- if err := writeAddExcludeConstraintFromAST(&buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST); err != nil {
- // If AST extraction fails, log error but continue (non-fatal)
- _, _ = fmt.Fprintf(&buf, "-- Error adding EXCLUDE constraint: %v\n", err)
- }
- }
- }
- }
- }
-
- // Add unique constraints
- for _, uniqueDiff := range tableDiff.UniqueConstraintChanges {
- if uniqueDiff.Action == schema.MetadataDiffActionCreate {
- if uniqueDiff.NewUniqueConstraint != nil {
- // Metadata mode: use constraint metadata
- writeMigrationUniqueKey(&buf, tableDiff.SchemaName, tableDiff.TableName, uniqueDiff.NewUniqueConstraint)
- } else if uniqueDiff.NewASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := uniqueDiff.NewASTNode.(pgparser.ITableconstraintContext); ok {
- if err := writeAddUniqueConstraintFromAST(&buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST); err != nil {
- // If AST extraction fails, log error but continue (non-fatal)
- _, _ = fmt.Fprintf(&buf, "-- Error adding unique constraint: %v\n", err)
- }
- }
- }
- }
- }
-
- // Add primary key constraints
- for _, pkDiff := range tableDiff.PrimaryKeyChanges {
- if pkDiff.Action == schema.MetadataDiffActionCreate {
- if pkDiff.NewPrimaryKey != nil {
- // Metadata mode: use constraint metadata
- writeMigrationPrimaryKey(&buf, tableDiff.SchemaName, tableDiff.TableName, pkDiff.NewPrimaryKey)
- } else if pkDiff.NewASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := pkDiff.NewASTNode.(pgparser.ITableconstraintContext); ok {
- if err := writeAddPrimaryKeyFromAST(&buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST); err != nil {
- // If AST extraction fails, log error but continue (non-fatal)
- _, _ = fmt.Fprintf(&buf, "-- Error adding primary key constraint: %v\n", err)
- }
- }
- }
- }
- }
-
- return buf.String(), nil
-}
-
-// generateAlterTableForeignKeys generates foreign key statements for a table diff
-func generateAlterTableForeignKeys(tableDiff *schema.TableDiff) (string, error) {
- var buf strings.Builder
-
- for _, fkDiff := range tableDiff.ForeignKeyChanges {
- if fkDiff.Action == schema.MetadataDiffActionCreate {
- if fkDiff.NewForeignKey != nil {
- // Metadata mode: use constraint metadata
- if err := writeMigrationForeignKey(&buf, tableDiff.SchemaName, tableDiff.TableName, fkDiff.NewForeignKey); err != nil {
- return "", err
- }
- } else if fkDiff.NewASTNode != nil {
- // AST-only mode: extract from AST node
- if constraintAST, ok := fkDiff.NewASTNode.(pgparser.ITableconstraintContext); ok {
- if err := writeAddForeignKeyFromAST(&buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST); err != nil {
- // If AST extraction fails, log error but continue (non-fatal)
- _, _ = fmt.Fprintf(&buf, "-- Error adding foreign key constraint: %v\n", err)
- }
- }
- }
- }
- }
-
- return buf.String(), nil
-}
-
-// generateAlterTableTriggers generates trigger statements for a table diff
-func generateAlterTableTriggers(tableDiff *schema.TableDiff) string {
- var buf strings.Builder
-
- for _, triggerDiff := range tableDiff.TriggerChanges {
- if triggerDiff.Action == schema.MetadataDiffActionCreate || triggerDiff.Action == schema.MetadataDiffActionAlter {
- // Use writeCreateTrigger which supports both AST and metadata modes
- // Both CREATE and ALTER use CREATE OR REPLACE
- if err := writeCreateTrigger(&buf, triggerDiff); err != nil {
- // Log error but continue - this is a best-effort operation
- // The error will be caught in the caller if needed
- continue
- }
- }
- }
-
- return buf.String()
-}
-
-func generateAlterColumn(schemaName, tableName string, colDiff *schema.ColumnDiff) string {
- var buf strings.Builder
-
- // In PostgreSQL, we need to handle different aspects of column changes separately
-
- // If type changed, alter the column type
- if colDiff.OldColumn.Type != colDiff.NewColumn.Type {
- writeAlterColumnType(&buf, schemaName, tableName, colDiff.NewColumn.Name, colDiff.OldColumn.Type, colDiff.NewColumn.Type)
- }
-
- // If nullability changed
- if colDiff.OldColumn.Nullable != colDiff.NewColumn.Nullable {
- if colDiff.NewColumn.Nullable {
- writeAlterColumnDropNotNull(&buf, schemaName, tableName, colDiff.NewColumn.Name)
- } else {
- writeAlterColumnSetNotNull(&buf, schemaName, tableName, colDiff.NewColumn.Name)
- }
- }
-
- // Handle default value changes
- oldHasDefault := hasDefaultValue(colDiff.OldColumn)
- newHasDefault := hasDefaultValue(colDiff.NewColumn)
- if oldHasDefault || newHasDefault {
- if !defaultValuesEqual(colDiff.OldColumn, colDiff.NewColumn) {
- // First drop the old default if it exists
- if oldHasDefault {
- writeAlterColumnDropDefault(&buf, schemaName, tableName, colDiff.OldColumn.Name)
- }
-
- // Add new default if needed
- if newHasDefault {
- defaultExpr := getDefaultExpression(colDiff.NewColumn)
- writeAlterColumnSetDefault(&buf, schemaName, tableName, colDiff.NewColumn.Name, defaultExpr)
- }
- }
- }
-
- return buf.String()
-}
-
-// hasDefaultValue checks if a column has any default value
-func hasDefaultValue(column *storepb.ColumnMetadata) bool {
- if column == nil {
- return false
- }
- return column.GetDefault() != ""
-}
-
-// defaultValuesEqual checks if two columns have the same default value
-func defaultValuesEqual(col1, col2 *storepb.ColumnMetadata) bool {
- if col1 == nil || col2 == nil {
- return col1 == col2
- }
-
- // Check default value
- def1 := col1.GetDefault()
- def2 := col2.GetDefault()
-
- // Use semantic comparison for default values
- // This handles cases like 'ARRAY[]::TEXT[]' vs 'ARRAY[]::text[]'
- if def1 == def2 {
- return true
- }
-
- // If they're not identical strings, try semantic comparison
- if def1 != "" || def2 != "" {
- // Use the expression comparer from the ast package
- comparer := ast.NewPostgreSQLExpressionComparer()
- equal, err := comparer.CompareExpressions(def1, def2)
- if err != nil {
- // If comparison fails, fall back to string comparison
- return def1 == def2
- }
- return equal
- }
-
- return def1 == def2
-}
-
-// getDefaultExpression returns the SQL expression for a column's default value
-func getDefaultExpression(column *storepb.ColumnMetadata) string {
- if column == nil {
- return ""
- }
-
- if column.Default != "" {
- return column.Default
- }
-
- return ""
-}
-
-// Write functions for various DDL statements
-
-func writeDropTrigger(out *strings.Builder, schema, table, trigger string) {
- _, _ = out.WriteString(`DROP TRIGGER IF EXISTS "`)
- _, _ = out.WriteString(trigger)
- _, _ = out.WriteString(`" ON "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropForeignKey(out *strings.Builder, schema, table, constraint string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" DROP CONSTRAINT IF EXISTS "`)
- _, _ = out.WriteString(constraint)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-// isFunctionProcedure checks if the AST node or definition represents a PROCEDURE (not a FUNCTION)
-// Returns true if it's a PROCEDURE, false if it's a FUNCTION or cannot be determined
-func isFunctionProcedure(astNode any, definition string) bool {
- // First, try to determine from AST node (most reliable)
- if astNode != nil {
- // Check if it's a CreatefunctionstmtContext with PROCEDURE keyword
- if ctx, ok := astNode.(*pgparser.CreatefunctionstmtContext); ok {
- // If PROCEDURE() returns non-nil, it's a PROCEDURE
- // If FUNCTION() returns non-nil, it's a FUNCTION
- return ctx.PROCEDURE() != nil
- }
- // Check if it's a CommentstmtContext (COMMENT ON FUNCTION/PROCEDURE)
- if ctx, ok := astNode.(*pgparser.CommentstmtContext); ok {
- // If PROCEDURE() returns non-nil, it's a COMMENT ON PROCEDURE
- // If FUNCTION() returns non-nil (and PROCEDURE is nil), it's a COMMENT ON FUNCTION
- return ctx.PROCEDURE() != nil
- }
- }
-
- // Fall back to checking definition string (for metadata mode or when AST is not available)
- if definition != "" {
- upperDef := strings.ToUpper(definition)
- // Check for PROCEDURE keyword in CREATE statement
- return strings.Contains(upperDef, " PROCEDURE ") ||
- strings.HasPrefix(upperDef, "CREATE PROCEDURE") ||
- strings.HasPrefix(upperDef, "CREATE OR REPLACE PROCEDURE")
- }
-
- return false
-}
-
-// getFunctionDefinitionForDrop extracts the function definition from either metadata or AST node
-func getFunctionDefinitionForDrop(funcDiff *schema.FunctionDiff) string {
- // Try metadata first (if available)
- if funcDiff.OldFunction != nil {
- return funcDiff.OldFunction.Definition
- }
-
- // Fall back to extracting from AST node (AST-only mode)
- if funcDiff.OldASTNode != nil {
- // Use generic interface to extract text from any parser rule context
- type parserContext interface {
- GetParser() antlr.Parser
- GetStart() antlr.Token
- GetStop() antlr.Token
- GetText() string
- }
-
- if ruleContext, ok := funcDiff.OldASTNode.(parserContext); ok {
- // Try to use token stream first (preserves formatting)
- if parser := ruleContext.GetParser(); parser != nil {
- if tokenStream := parser.GetTokenStream(); tokenStream != nil {
- start := ruleContext.GetStart()
- stop := ruleContext.GetStop()
- if start != nil && stop != nil {
- text := tokenStream.GetTextFromTokens(start, stop)
- if text != "" {
- return text
- }
- }
- }
- }
- // Fallback to GetText() method
- return ruleContext.GetText()
- }
- }
-
- // No definition available
- return ""
-}
-
-func writeDropFunction(out *strings.Builder, schema, function string, astNode any, definition string) {
- // Determine if this is a PROCEDURE or FUNCTION using AST-based detection
- objectType := "FUNCTION"
- if isFunctionProcedure(astNode, definition) {
- objectType = "PROCEDURE"
- }
-
- _, _ = out.WriteString(`DROP `)
- _, _ = out.WriteString(objectType)
- _, _ = out.WriteString(` IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
-
- // Parse function signature to extract function name and parameters
- // Format: function_name(param1 type1, param2 type2)
- parenIndex := strings.Index(function, "(")
- if parenIndex > 0 {
- // Extract function name and parameters separately
- funcName := function[:parenIndex]
- params := function[parenIndex:] // includes parentheses
- _, _ = out.WriteString(funcName)
- _, _ = out.WriteString(`"`)
- _, _ = out.WriteString(params)
- } else {
- // Fallback for functions without parameters
- _, _ = out.WriteString(function)
- _, _ = out.WriteString(`"()`)
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeMigrationSequenceFromAST writes CREATE SEQUENCE statement from AST node
-func writeMigrationSequenceFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var sequenceSQL string
-
- // Try to cast to PostgreSQL CreateseqstmtContext
- if ctx, ok := astNode.(*pgparser.CreateseqstmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- sequenceSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if sequenceSQL == "" {
- sequenceSQL = ctx.GetText()
- }
- } else {
- return errors.Errorf("unsupported AST node type for sequence: %T", astNode)
- }
-
- if sequenceSQL == "" {
- return errors.New("failed to extract sequence SQL from AST node")
- }
-
- // Write the sequence SQL
- _, _ = out.WriteString(sequenceSQL)
- // Ensure statement ends with semicolon
- if !strings.HasSuffix(strings.TrimSpace(sequenceSQL), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
-
- return nil
-}
-
-// writeMigrationEnumTypeFromAST writes CREATE TYPE AS ENUM statement from AST node
-func writeMigrationEnumTypeFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var enumSQL string
-
- // Try to cast to PostgreSQL DefinestmtContext (CREATE TYPE AS ENUM)
- if ctx, ok := astNode.(*pgparser.DefinestmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- enumSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if enumSQL == "" {
- enumSQL = ctx.GetText()
- }
- } else {
- return errors.Errorf("unsupported AST node type for enum type: %T", astNode)
- }
-
- if enumSQL == "" {
- return errors.New("failed to extract enum type SQL from AST node")
- }
-
- // Write the enum type SQL
- _, _ = out.WriteString(enumSQL)
- // Ensure statement ends with semicolon
- if !strings.HasSuffix(strings.TrimSpace(enumSQL), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
-
- return nil
-}
-
-// writeMigrationTableFromAST writes CREATE TABLE statement from AST node
-func writeMigrationTableFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var tableSQL string
-
- // Try to cast to PostgreSQL CreatestmtContext
- if ctx, ok := astNode.(*pgparser.CreatestmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- tableSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // If token stream failed, fall back to GetText() method
- if tableSQL == "" {
- tableSQL = ctx.GetText()
- }
- }
-
- // If we couldn't extract SQL, return an error
- if tableSQL == "" {
- return errors.New("could not extract table SQL from AST node")
- }
-
- // Write the table SQL
- _, _ = out.WriteString(tableSQL)
- // Ensure statement ends with semicolon
- if !strings.HasSuffix(strings.TrimSpace(tableSQL), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
-
- return nil
-}
-
-// writeMigrationTableFromASTWithoutFK writes CREATE TABLE statement from AST node,
-// but excludes foreign key constraints. This is used when there are dependency cycles
-// and FK constraints need to be added separately after all tables are created.
-func writeMigrationTableFromASTWithoutFK(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- ctx, ok := astNode.(*pgparser.CreatestmtContext)
- if !ok {
- return errors.New("AST node is not a CreatestmtContext")
- }
-
- tokenStream := ctx.GetParser().GetTokenStream()
- if tokenStream == nil {
- return errors.New("could not get token stream from AST")
- }
-
- // Check if table has any elements
- if ctx.Opttableelementlist() == nil {
- // No elements, just write the full statement
- return writeMigrationTableFromAST(out, astNode)
- }
-
- tableElementList := ctx.Opttableelementlist().Tableelementlist()
- if tableElementList == nil {
- return writeMigrationTableFromAST(out, astNode)
- }
-
- // Collect non-FK table elements
- var nonFKElements []string
- for _, element := range tableElementList.AllTableelement() {
- // Check if this is a FK constraint
- 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, skip it
- continue
- }
- }
- }
- // Not a FK constraint, include it
- elementText := tokenStream.GetTextFromRuleContext(element)
- nonFKElements = append(nonFKElements, elementText)
- }
-
- // If all elements were FK constraints, we still need to create the table
- // with at least the column definitions (which should always exist)
- if len(nonFKElements) == 0 {
- // This shouldn't happen in practice, but fall back to full statement
- return writeMigrationTableFromAST(out, astNode)
- }
-
- // Build the CREATE TABLE statement without FK constraints
- // Get the table name part (everything before the first '(')
- fullText := tokenStream.GetTextFromTokens(ctx.GetStart(), ctx.GetStop())
- parenIdx := strings.Index(fullText, "(")
- if parenIdx == -1 {
- return errors.New("could not find opening parenthesis in CREATE TABLE statement")
- }
-
- tablePrefix := fullText[:parenIdx+1]
- _, _ = out.WriteString(tablePrefix)
- _, _ = out.WriteString("\n")
-
- // Write non-FK elements with proper formatting
- for i, elem := range nonFKElements {
- _, _ = out.WriteString(" ")
- _, _ = out.WriteString(strings.TrimSpace(elem))
- if i < len(nonFKElements)-1 {
- _, _ = out.WriteString(",")
- }
- _, _ = out.WriteString("\n")
- }
-
- _, _ = out.WriteString(");")
- _, _ = out.WriteString("\n\n")
-
- return nil
-}
-
-func writeDropView(out *strings.Builder, schema, view string) {
- _, _ = out.WriteString(`DROP VIEW IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(view)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropMaterializedView(out *strings.Builder, schema, view string) {
- _, _ = out.WriteString(`DROP MATERIALIZED VIEW IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(view)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropCheckConstraint(out *strings.Builder, schema, table, constraint string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" DROP CONSTRAINT IF EXISTS "`)
- _, _ = out.WriteString(constraint)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropConstraint(out *strings.Builder, schema, table, constraint string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" DROP CONSTRAINT IF EXISTS "`)
- _, _ = out.WriteString(constraint)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropPrimaryKey(out *strings.Builder, schema, table, constraint string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" DROP CONSTRAINT IF EXISTS "`)
- _, _ = out.WriteString(constraint)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropIndex(out *strings.Builder, schema, index string) {
- _, _ = out.WriteString(`DROP INDEX IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(index)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropColumn(out *strings.Builder, schema, table, column string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" DROP COLUMN IF EXISTS "`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropTable(out *strings.Builder, schema, table string) {
- _, _ = out.WriteString(`DROP TABLE IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropSequence(out *strings.Builder, schema, sequence string) {
- _, _ = out.WriteString(`DROP SEQUENCE IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(sequence)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropSchema(out *strings.Builder, schema string) {
- _, _ = out.WriteString(`DROP SCHEMA IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeDropType(out *strings.Builder, schema, typeName string) {
- _, _ = out.WriteString(`DROP TYPE IF EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(typeName)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeCreateEnumType(out *strings.Builder, schema string, enum *storepb.EnumTypeMetadata) error {
- _, _ = out.WriteString(`CREATE TYPE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(enum.Name)
- _, _ = out.WriteString(`" AS ENUM (`)
-
- for i, value := range enum.Values {
- if i > 0 {
- _, _ = out.WriteString(", ")
- }
- _, _ = out.WriteString("'")
- _, _ = out.WriteString(value)
- _, _ = out.WriteString("'")
- }
-
- _, _ = out.WriteString(");")
- _, _ = out.WriteString("\n")
- return nil
-}
-
-func writeDropExtension(out *strings.Builder, extensionName string) {
- _, _ = out.WriteString(`DROP EXTENSION IF EXISTS "`)
- _, _ = out.WriteString(extensionName)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeCreateExtension(out *strings.Builder, extension *storepb.ExtensionMetadata) error {
- _, _ = out.WriteString(`CREATE EXTENSION IF NOT EXISTS "`)
- _, _ = out.WriteString(extension.Name)
- _, _ = out.WriteString(`"`)
-
- // Add WITH SCHEMA clause if schema is specified
- if extension.Schema != "" {
- _, _ = out.WriteString(` WITH SCHEMA "`)
- _, _ = out.WriteString(extension.Schema)
- _, _ = out.WriteString(`"`)
- }
-
- // Add VERSION clause if version is specified
- if extension.Version != "" {
- _, _ = out.WriteString(` VERSION '`)
- _, _ = out.WriteString(extension.Version)
- _, _ = out.WriteString(`'`)
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-
- // Write description (comment) if present
- if extension.Description != "" {
- writeCommentOnExtension(out, extension.Name, extension.Description)
- }
-
- return nil
-}
-
-func writeMigrationExtensionFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Try to cast to PostgreSQL CreateextensionstmtContext
- if ctx, ok := astNode.(*pgparser.CreateextensionstmtContext); ok {
- // Get text using token stream
- if stream := ctx.GetParser().GetTokenStream(); stream != nil {
- text := stream.GetTextFromInterval(antlr.Interval{
- Start: ctx.GetStart().GetTokenIndex(),
- Stop: ctx.GetStop().GetTokenIndex(),
- })
- _, _ = out.WriteString(text)
- _, _ = out.WriteString(";")
- _, _ = out.WriteString("\n")
- return nil
- }
- return errors.New("token stream not available for extension AST node")
- }
-
- return errors.Errorf("unexpected AST node type for extension: %T", astNode)
-}
-
-func writeDropEventTrigger(out *strings.Builder, eventTriggerName string) {
- _, _ = out.WriteString(`DROP EVENT TRIGGER IF EXISTS "`)
- _, _ = out.WriteString(eventTriggerName)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-}
-
-func writeCreateEventTrigger(out *strings.Builder, eventTrigger *storepb.EventTriggerMetadata) error {
- // Use the stored definition if available
- if eventTrigger.Definition != "" {
- _, _ = out.WriteString(eventTrigger.Definition)
- _, _ = out.WriteString(";")
- _, _ = out.WriteString("\n")
- return nil
- }
-
- // Otherwise, build the CREATE EVENT TRIGGER statement
- _, _ = out.WriteString(`CREATE EVENT TRIGGER "`)
- _, _ = out.WriteString(eventTrigger.Name)
- _, _ = out.WriteString(`" ON `)
- _, _ = out.WriteString(eventTrigger.Event)
-
- // Add WHEN TAG IN clause if tags are specified
- if len(eventTrigger.Tags) > 0 {
- _, _ = out.WriteString("\n WHEN TAG IN (")
- for i, tag := range eventTrigger.Tags {
- if i > 0 {
- _, _ = out.WriteString(", ")
- }
- _, _ = out.WriteString("'")
- _, _ = out.WriteString(tag)
- _, _ = out.WriteString("'")
- }
- _, _ = out.WriteString(")")
- }
-
- // Add EXECUTE FUNCTION clause
- _, _ = out.WriteString("\n EXECUTE FUNCTION ")
- if eventTrigger.FunctionSchema != "" {
- _, _ = out.WriteString(`"`)
- _, _ = out.WriteString(eventTrigger.FunctionSchema)
- _, _ = out.WriteString(`".`)
- }
- _, _ = out.WriteString(`"`)
- _, _ = out.WriteString(eventTrigger.FunctionName)
- _, _ = out.WriteString(`"()`)
- _, _ = out.WriteString(";")
- _, _ = out.WriteString("\n")
-
- return nil
-}
-
-func writeMigrationEventTriggerFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Try to cast to PostgreSQL CreateeventtrigstmtContext
- if ctx, ok := astNode.(*pgparser.CreateeventtrigstmtContext); ok {
- // Get text using token stream
- if stream := ctx.GetParser().GetTokenStream(); stream != nil {
- text := stream.GetTextFromInterval(antlr.Interval{
- Start: ctx.GetStart().GetTokenIndex(),
- Stop: ctx.GetStop().GetTokenIndex(),
- })
- _, _ = out.WriteString(text)
- _, _ = out.WriteString(";")
- _, _ = out.WriteString("\n")
- return nil
- }
- return errors.New("token stream not available for event trigger AST node")
- }
-
- return errors.Errorf("unexpected AST node type for event trigger: %T", astNode)
-}
-
-func writeCreateSchema(out *strings.Builder, schema string) error {
- _, _ = out.WriteString(`CREATE SCHEMA IF NOT EXISTS "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
- return nil
-}
-
-func writeAddColumn(out *strings.Builder, schema, table string, column *storepb.ColumnMetadata) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD COLUMN "`)
- _, _ = out.WriteString(column.Name)
- _, _ = out.WriteString(`" `)
- _, _ = out.WriteString(column.Type)
-
- // Handle default values
- defaultExpr := getDefaultExpression(column)
- if defaultExpr != "" {
- _, _ = out.WriteString(` DEFAULT `)
- _, _ = out.WriteString(defaultExpr)
- }
-
- if !column.Nullable {
- _, _ = out.WriteString(` NOT NULL`)
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// requiresExplicitCasting determines if a PostgreSQL type conversion requires a USING clause
-func requiresExplicitCasting(oldType, newType string) bool {
- // Types are already normalized by get_database_metadata.go, no need to normalize again
-
- // Extract base types without parameters
- oldBaseType := extractBaseType(oldType)
- newBaseType := extractBaseType(newType)
-
- // If same base type, usually no USING clause needed (except for precision reductions)
- if oldBaseType == newBaseType {
- return requiresUsingForSameType(oldType, newType)
- }
-
- // Define incompatible type conversions that require USING clause
- incompatibleConversions := map[string][]string{
- "text": {"integer", "numeric", "real", "double precision", "smallint", "bigint", "boolean"},
- "character varying": {"integer", "numeric", "real", "double precision", "smallint", "bigint", "boolean"},
- "character": {"integer", "numeric", "real", "double precision", "smallint", "bigint", "boolean"},
- "jsonb": {"text", "character varying", "character"},
- "json": {"text", "character varying", "character"},
- "integer[]": {"text", "character varying", "character"},
- "text[]": {"text", "character varying", "character"},
- "bigint": {"smallint", "integer"}, // Potential overflow
- "double precision": {"real"}, // Precision loss
- "timestamp with time zone": {"date"}, // Time component loss
- "timestamp without time zone": {"date"}, // Time component loss
- "numeric": {"integer", "smallint", "bigint", "real", "double precision"}, // When precision specified
- }
-
- if targets, exists := incompatibleConversions[oldBaseType]; exists {
- for _, target := range targets {
- if target == newBaseType {
- return true
- }
- }
- }
-
- return false
-}
-
-// extractBaseType removes precision/scale parameters from PostgreSQL types
-func extractBaseType(typeName string) string {
- // Handle common patterns: numeric(10,2) -> numeric, varchar(100) -> character varying
- if idx := strings.Index(typeName, "("); idx != -1 {
- return strings.TrimSpace(typeName[:idx])
- }
- return typeName
-}
-
-// requiresUsingForSameType checks if same base type requires USING (e.g., precision reductions)
-func requiresUsingForSameType(oldType, newType string) bool {
- oldBase := extractBaseType(oldType)
- newBase := extractBaseType(newType)
-
- if oldBase != newBase {
- return false
- }
-
- // For character varying, check if reducing length significantly
- if strings.Contains(oldType, "character varying") {
- oldLen := extractLength(oldType)
- newLen := extractLength(newType)
- if oldLen > 0 && newLen > 0 && oldLen > newLen*2 {
- return true // Significant reduction might truncate data
- }
- }
-
- return false
-}
-
-// extractLength extracts length parameter from varchar(n) or char(n)
-func extractLength(typeName string) int {
- start := strings.Index(typeName, "(")
- end := strings.Index(typeName, ")")
- if start == -1 || end == -1 || end <= start {
- return 0
- }
-
- lengthStr := strings.TrimSpace(typeName[start+1 : end])
- if length, err := strconv.Atoi(lengthStr); err == nil {
- return length
- }
- return 0
-}
-
-func writeAlterColumnType(out *strings.Builder, schema, table, column, oldType, newType string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`" TYPE `)
- _, _ = out.WriteString(newType)
-
- // Add USING clause for incompatible type conversions
- if requiresExplicitCasting(oldType, newType) {
- _, _ = out.WriteString(` USING "`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`"::`)
- _, _ = out.WriteString(newType)
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-func writeAlterColumnSetNotNull(out *strings.Builder, schema, table, column string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`" SET NOT NULL;`)
- _, _ = out.WriteString("\n")
-}
-
-func writeAlterColumnDropNotNull(out *strings.Builder, schema, table, column string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`" DROP NOT NULL;`)
- _, _ = out.WriteString("\n")
-}
-
-func writeAlterColumnSetDefault(out *strings.Builder, schema, table, column, defaultExpr string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`" SET DEFAULT `)
- _, _ = out.WriteString(defaultExpr)
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-func writeAlterColumnDropDefault(out *strings.Builder, schema, table, column string) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`" DROP DEFAULT;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeAddColumnFromAST writes ALTER TABLE ADD COLUMN statement from AST node
-func writeAddColumnFromAST(out *strings.Builder, schema, table string, columnASTNode pgparser.IColumnDefContext) error {
- if columnASTNode == nil {
- return errors.New("Column AST node is nil")
- }
-
- columnDefinition := extractColumnDefinitionFromAST(columnASTNode)
- if columnDefinition == "" {
- return errors.New("could not extract column definition from AST node")
- }
-
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD COLUMN `)
- _, _ = out.WriteString(columnDefinition)
- _, _ = out.WriteString(";\n")
-
- return nil
-}
-
-// writeDropColumnFromAST writes ALTER TABLE DROP COLUMN statement from AST node
-func writeDropColumnFromAST(out *strings.Builder, schema, table string, columnASTNode pgparser.IColumnDefContext) error {
- if columnASTNode == nil {
- return errors.New("Column AST node is nil")
- }
-
- columnName := extractColumnNameFromAST(columnASTNode)
- if columnName == "" {
- return errors.New("could not extract column name from AST node")
- }
-
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" DROP COLUMN IF EXISTS "`)
- _, _ = out.WriteString(columnName)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
-
- return nil
-}
-
-// writeAlterColumnFromAST writes ALTER TABLE ALTER COLUMN statement from AST nodes
-func writeAlterColumnFromAST(out *strings.Builder, schema, table string, oldColumnASTNode, newColumnASTNode pgparser.IColumnDefContext) error {
- if oldColumnASTNode == nil || newColumnASTNode == nil {
- return errors.New("Column AST nodes are nil")
- }
-
- // Extract column metadata from both AST nodes (without normalization except for column name)
- oldMetadata := extractColumnMetadataFromAST(oldColumnASTNode)
- newMetadata := extractColumnMetadataFromAST(newColumnASTNode)
-
- if oldMetadata == nil || newMetadata == nil {
- return errors.New("could not extract column metadata from AST nodes")
- }
-
- if oldMetadata.Name == "" {
- return errors.New("could not extract column name from AST node")
- }
-
- // Check for type changes
- if oldMetadata.Type != newMetadata.Type {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(oldMetadata.Name)
- _, _ = out.WriteString(`" TYPE `)
- _, _ = out.WriteString(newMetadata.Type)
- _, _ = out.WriteString(";\n")
- }
-
- // Check for nullable changes
- if oldMetadata.Nullable != newMetadata.Nullable {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(oldMetadata.Name)
- if newMetadata.Nullable {
- _, _ = out.WriteString(`" DROP NOT NULL`)
- } else {
- _, _ = out.WriteString(`" SET NOT NULL`)
- }
- _, _ = out.WriteString(";\n")
- }
-
- // Check for default value changes
- if oldMetadata.Default != newMetadata.Default {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ALTER COLUMN "`)
- _, _ = out.WriteString(oldMetadata.Name)
- if newMetadata.Default == "" {
- _, _ = out.WriteString(`" DROP DEFAULT`)
- } else {
- _, _ = out.WriteString(`" SET DEFAULT `)
- _, _ = out.WriteString(newMetadata.Default)
- }
- _, _ = out.WriteString(";\n")
- }
-
- return nil
-}
-
-func writeAddCheckConstraint(out *strings.Builder, schema, table string, check *storepb.CheckConstraintMetadata) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(check.Name)
- _, _ = out.WriteString(`" CHECK (`)
- _, _ = out.WriteString(check.Expression)
- _, _ = out.WriteString(`);`)
- _, _ = out.WriteString("\n")
-}
-
-func writeFunctionDiff(out *strings.Builder, funcDiff *schema.FunctionDiff) error {
- switch funcDiff.Action {
- case schema.MetadataDiffActionCreate:
- // CREATE new function - support both metadata and AST-only modes
- if funcDiff.NewFunction != nil {
- // Metadata mode: use function definition
- _, _ = out.WriteString(funcDiff.NewFunction.Definition)
- if !strings.HasSuffix(strings.TrimSpace(funcDiff.NewFunction.Definition), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
- } else if funcDiff.NewASTNode != nil {
- // AST-only mode: extract SQL from AST node
- if err := writeMigrationFunctionFromAST(out, funcDiff.NewASTNode); err != nil {
- return err
- }
- } else {
- return errors.Errorf("function diff for %s.%s has neither metadata nor AST node", funcDiff.SchemaName, funcDiff.FunctionName)
- }
-
- case schema.MetadataDiffActionAlter:
- // ALTER function using CREATE OR REPLACE
- // The decision to use ALTER vs DROP/CREATE was already made in differ.go
- // If we reach here, it means we can safely use CREATE OR REPLACE
- if funcDiff.NewFunction != nil {
- // Metadata mode: use ANTLR parser to safely convert CREATE FUNCTION to CREATE OR REPLACE FUNCTION
- definition := convertToCreateOrReplace(funcDiff.NewFunction.Definition)
-
- _, _ = out.WriteString(definition)
- if !strings.HasSuffix(strings.TrimSpace(definition), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
- } else if funcDiff.NewASTNode != nil {
- // AST-only mode: extract SQL and convert to CREATE OR REPLACE
- if err := writeMigrationFunctionFromASTWithReplace(out, funcDiff.NewASTNode); err != nil {
- return err
- }
- } else {
- return errors.Errorf("function diff for %s.%s has neither metadata nor AST node", funcDiff.SchemaName, funcDiff.FunctionName)
- }
-
- default:
- // Ignore other actions like DROP (handled elsewhere)
- }
- return nil
-}
-
-// Helper functions to check if diff contains certain types of changes
-func hasCreateOrAlterObjects(diff *schema.MetadataDiff) bool {
- return hasCreateOrAlterTables(diff) || hasCreateViewsOrFunctions(diff)
-}
-
-func hasCreateOrAlterTables(diff *schema.MetadataDiff) bool {
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionCreate || tableDiff.Action == schema.MetadataDiffActionAlter {
- return true
- }
- }
- for _, seqDiff := range diff.SequenceChanges {
- if seqDiff.Action == schema.MetadataDiffActionCreate {
- return true
- }
- }
- return false
-}
-
-func hasCreateViewsOrFunctions(diff *schema.MetadataDiff) bool {
- for _, viewDiff := range diff.ViewChanges {
- if viewDiff.Action == schema.MetadataDiffActionCreate || viewDiff.Action == schema.MetadataDiffActionAlter {
- return true
- }
- }
- for _, mvDiff := range diff.MaterializedViewChanges {
- if mvDiff.Action == schema.MetadataDiffActionCreate || mvDiff.Action == schema.MetadataDiffActionAlter {
- return true
- }
- }
- for _, funcDiff := range diff.FunctionChanges {
- if funcDiff.Action == schema.MetadataDiffActionCreate || funcDiff.Action == schema.MetadataDiffActionAlter {
- return true
- }
- }
- return false
-}
-
-// buildTempMetadataForCreate builds a temporary DatabaseSchemaMetadata containing
-// the tables and views that are being created or altered. This allows GetQuerySpan to
-// find these objects when extracting view dependencies in AST-only mode.
-func buildTempMetadataForCreate(diff *schema.MetadataDiff) *storepb.DatabaseSchemaMetadata {
- // Group objects by schema
- schemaMap := make(map[string]*storepb.SchemaMetadata)
-
- // Collect tables being created or altered
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionCreate || tableDiff.Action == schema.MetadataDiffActionAlter {
- schemaName := tableDiff.SchemaName
- if schemaMap[schemaName] == nil {
- schemaMap[schemaName] = &storepb.SchemaMetadata{
- Name: schemaName,
- }
- }
- // Add table with just the name - that's all GetQuerySpan needs
- schemaMap[schemaName].Tables = append(schemaMap[schemaName].Tables, &storepb.TableMetadata{
- Name: tableDiff.TableName,
- })
- }
- }
-
- // Collect views being created or altered
- for _, viewDiff := range diff.ViewChanges {
- if viewDiff.Action == schema.MetadataDiffActionCreate || viewDiff.Action == schema.MetadataDiffActionAlter {
- schemaName := viewDiff.SchemaName
- if schemaMap[schemaName] == nil {
- schemaMap[schemaName] = &storepb.SchemaMetadata{
- Name: schemaName,
- }
- }
- // Add view with just the name
- schemaMap[schemaName].Views = append(schemaMap[schemaName].Views, &storepb.ViewMetadata{
- Name: viewDiff.ViewName,
- })
- }
- }
-
- // Convert map to slice
- var schemas []*storepb.SchemaMetadata
- for _, schemaMeta := range schemaMap {
- schemas = append(schemas, schemaMeta)
- }
-
- return &storepb.DatabaseSchemaMetadata{
- Schemas: schemas,
- }
-}
-
-// buildTempMetadataForDrop builds a temporary DatabaseSchemaMetadata containing only
-// the tables and views that are being dropped. This allows GetQuerySpan to find these
-// objects when extracting view dependencies in AST-only mode.
-func buildTempMetadataForDrop(diff *schema.MetadataDiff) *storepb.DatabaseSchemaMetadata {
- // Group objects by schema
- schemaMap := make(map[string]*storepb.SchemaMetadata)
-
- // Collect tables being dropped
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionDrop {
- schemaName := tableDiff.SchemaName
- if schemaMap[schemaName] == nil {
- schemaMap[schemaName] = &storepb.SchemaMetadata{
- Name: schemaName,
- }
- }
- // Add table with just the name - that's all GetQuerySpan needs
- schemaMap[schemaName].Tables = append(schemaMap[schemaName].Tables, &storepb.TableMetadata{
- Name: tableDiff.TableName,
- })
- }
- }
-
- // Collect views being dropped
- for _, viewDiff := range diff.ViewChanges {
- if viewDiff.Action == schema.MetadataDiffActionDrop || viewDiff.Action == schema.MetadataDiffActionAlter {
- schemaName := viewDiff.SchemaName
- if schemaMap[schemaName] == nil {
- schemaMap[schemaName] = &storepb.SchemaMetadata{
- Name: schemaName,
- }
- }
- // Add view with just the name
- schemaMap[schemaName].Views = append(schemaMap[schemaName].Views, &storepb.ViewMetadata{
- Name: viewDiff.ViewName,
- })
- }
- }
-
- // Convert map to slice
- var schemas []*storepb.SchemaMetadata
- for _, schemaMeta := range schemaMap {
- schemas = append(schemas, schemaMeta)
- }
-
- return &storepb.DatabaseSchemaMetadata{
- Schemas: schemas,
- }
-}
-
-// getObjectID generates a unique identifier for database objects
-func getMigrationObjectID(schema, name string) string {
- return fmt.Sprintf("%s.%s", schema, name)
-}
-
-// writeCreateSequence writes a CREATE SEQUENCE statement
-func writeMigrationCreateSequence(out *strings.Builder, schema string, seq *storepb.SequenceMetadata) error {
- _, _ = out.WriteString(`CREATE SEQUENCE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(seq.Name)
- _, _ = out.WriteString(`"`)
-
- if seq.DataType != "" {
- _, _ = out.WriteString(` AS `)
- _, _ = out.WriteString(seq.DataType)
- }
- if seq.Start != "" {
- _, _ = out.WriteString(` START WITH `)
- _, _ = out.WriteString(seq.Start)
- }
- if seq.Increment != "" {
- _, _ = out.WriteString(` INCREMENT BY `)
- _, _ = out.WriteString(seq.Increment)
- }
- if seq.MinValue != "" {
- _, _ = out.WriteString(` MINVALUE `)
- _, _ = out.WriteString(seq.MinValue)
- }
- if seq.MaxValue != "" {
- _, _ = out.WriteString(` MAXVALUE `)
- _, _ = out.WriteString(seq.MaxValue)
- }
- if seq.CacheSize != "" {
- _, _ = out.WriteString(` CACHE `)
- _, _ = out.WriteString(seq.CacheSize)
- }
- if seq.Cycle {
- _, _ = out.WriteString(` CYCLE`)
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
- return nil
-}
-
-// writeMigrationSequenceOwnership writes an ALTER SEQUENCE OWNED BY statement
-func writeMigrationSequenceOwnership(out *strings.Builder, schema string, seq *storepb.SequenceMetadata) error {
- _, _ = out.WriteString(`ALTER SEQUENCE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(seq.Name)
- _, _ = out.WriteString(`" OWNED BY "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(seq.OwnerTable)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(seq.OwnerColumn)
- _, _ = out.WriteString(`";`)
- _, _ = out.WriteString("\n")
- return nil
-}
-
-// writeView writes a CREATE VIEW statement from AST node or metadata
-func writeMigrationViewFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var viewSQL string
-
- // Try to cast to PostgreSQL ViewstmtContext first
- if ctx, ok := astNode.(*pgparser.ViewstmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- viewSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if viewSQL == "" {
- viewSQL = ctx.GetText()
- }
- } else {
- // Generic fallback - try to get text using token approach first
- if tree, ok := astNode.(antlr.ParseTree); ok {
- viewSQL = getTextFromAST(tree)
- }
- }
-
- if viewSQL != "" {
- _, _ = out.WriteString(viewSQL)
- if !strings.HasSuffix(strings.TrimSpace(viewSQL), ";") {
- _, _ = out.WriteString(`;`)
- }
- _, _ = out.WriteString("\n\n")
- return nil
- }
-
- return errors.New("failed to extract SQL from AST node")
-}
-
-// writeView writes a CREATE VIEW statement
-func writeMigrationView(out *strings.Builder, schema string, view *storepb.ViewMetadata) error {
- _, _ = out.WriteString(`CREATE VIEW "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(view.Name)
- _, _ = out.WriteString(`" AS `)
- _, _ = out.WriteString(view.Definition)
- if !strings.HasSuffix(strings.TrimSpace(view.Definition), ";") {
- _, _ = out.WriteString(`;`)
- }
- _, _ = out.WriteString("\n")
- return nil
-}
-
-// writeMigrationFunctionFromAST writes a CREATE FUNCTION statement from AST node
-func writeMigrationFunctionFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var functionSQL string
-
- // Try to cast to PostgreSQL CreatefunctionstmtContext first
- if ctx, ok := astNode.(*pgparser.CreatefunctionstmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- functionSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if functionSQL == "" {
- functionSQL = ctx.GetText()
- }
- } else {
- return errors.Errorf("unsupported AST node type for function: %T", astNode)
- }
-
- if functionSQL == "" {
- return errors.New("could not extract function SQL from AST node")
- }
-
- // Write the function SQL
- _, _ = out.WriteString(functionSQL)
- if !strings.HasSuffix(strings.TrimSpace(functionSQL), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
-
- return nil
-}
-
-// writeMigrationFunctionFromASTWithReplace writes a CREATE OR REPLACE FUNCTION statement from AST node
-func writeMigrationFunctionFromASTWithReplace(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var functionSQL string
-
- // Try to cast to PostgreSQL CreatefunctionstmtContext first
- if ctx, ok := astNode.(*pgparser.CreatefunctionstmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- functionSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if functionSQL == "" {
- functionSQL = ctx.GetText()
- }
- } else {
- return errors.Errorf("unsupported AST node type for function: %T", astNode)
- }
-
- if functionSQL == "" {
- return errors.New("could not extract function SQL from AST node")
- }
-
- // Convert CREATE FUNCTION to CREATE OR REPLACE FUNCTION
- functionSQL = convertToCreateOrReplace(functionSQL)
-
- // Write the function SQL
- _, _ = out.WriteString(functionSQL)
- if !strings.HasSuffix(strings.TrimSpace(functionSQL), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
-
- return nil
-}
-
-// writeMigrationMaterializedViewFromAST writes a CREATE MATERIALIZED VIEW statement from AST node
-func writeMigrationMaterializedViewFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var mvSQL string
-
- // Try to cast to PostgreSQL CreatematviewstmtContext first
- if ctx, ok := astNode.(*pgparser.CreatematviewstmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- mvSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if mvSQL == "" {
- mvSQL = ctx.GetText()
- }
- } else {
- // Generic fallback - try to get text using token approach first
- if tree, ok := astNode.(antlr.ParseTree); ok {
- mvSQL = getTextFromAST(tree)
- }
- }
-
- if mvSQL != "" {
- _, _ = out.WriteString(mvSQL)
- if !strings.HasSuffix(strings.TrimSpace(mvSQL), ";") {
- _, _ = out.WriteString(`;`)
- }
- _, _ = out.WriteString("\n\n")
- return nil
- }
-
- return errors.New("failed to extract SQL from materialized view AST node")
-}
-
-// writeMaterializedView writes a CREATE MATERIALIZED VIEW statement
-func writeMigrationMaterializedView(out *strings.Builder, schema string, view *storepb.MaterializedViewMetadata) error {
- _, _ = out.WriteString(`CREATE MATERIALIZED VIEW "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(view.Name)
- _, _ = out.WriteString(`" AS `)
- _, _ = out.WriteString(view.Definition)
- if !strings.HasSuffix(strings.TrimSpace(view.Definition), ";") {
- _, _ = out.WriteString(`;`)
- }
- _, _ = out.WriteString("\n")
- return nil
-}
-
-// writeForeignKey writes an ALTER TABLE ADD CONSTRAINT statement for a foreign key
-func writeMigrationForeignKey(out *strings.Builder, schema, table string, fk *storepb.ForeignKeyMetadata) error {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(fk.Name)
- _, _ = out.WriteString(`" FOREIGN KEY (`)
-
- for i, col := range fk.Columns {
- if i > 0 {
- _, _ = out.WriteString(`, `)
- }
- _, _ = out.WriteString(`"`)
- _, _ = out.WriteString(col)
- _, _ = out.WriteString(`"`)
- }
-
- _, _ = out.WriteString(`) REFERENCES "`)
- _, _ = out.WriteString(fk.ReferencedSchema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(fk.ReferencedTable)
- _, _ = out.WriteString(`" (`)
-
- for i, col := range fk.ReferencedColumns {
- if i > 0 {
- _, _ = out.WriteString(`, `)
- }
- _, _ = out.WriteString(`"`)
- _, _ = out.WriteString(col)
- _, _ = out.WriteString(`"`)
- }
-
- _, _ = out.WriteString(`)`)
-
- if fk.OnUpdate != "" {
- _, _ = out.WriteString(` ON UPDATE `)
- _, _ = out.WriteString(fk.OnUpdate)
- }
- if fk.OnDelete != "" {
- _, _ = out.WriteString(` ON DELETE `)
- _, _ = out.WriteString(fk.OnDelete)
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
- return nil
-}
-
-// writeMigrationTrigger writes a trigger creation statement
-func writeMigrationTrigger(out *strings.Builder, trigger *storepb.TriggerMetadata) {
- if trigger == nil || trigger.Body == "" {
- return
- }
- _, _ = out.WriteString(trigger.Body)
- if !strings.HasSuffix(strings.TrimSpace(trigger.Body), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n")
-}
-
-// writeMigrationMaterializedViewIndex writes an index creation statement for a materialized view
-func writeMigrationMaterializedViewIndex(out *strings.Builder, schema, mvName string, index *storepb.IndexMetadata) {
- if index == nil {
- return
- }
-
- // If index has a full definition, use it directly
- if index.Definition != "" {
- _, _ = out.WriteString(index.Definition)
- if !strings.HasSuffix(strings.TrimSpace(index.Definition), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n")
- return
- }
-
- // Otherwise, construct the CREATE INDEX statement
- _, _ = out.WriteString(`CREATE `)
- if index.Unique {
- _, _ = out.WriteString(`UNIQUE `)
- }
- _, _ = out.WriteString(`INDEX "`)
- _, _ = out.WriteString(index.Name)
- _, _ = out.WriteString(`" ON "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(mvName)
- _, _ = out.WriteString(`" `)
-
- if index.Type != "" && index.Type != "BTREE" && index.Type != "btree" {
- _, _ = out.WriteString(`USING `)
- _, _ = out.WriteString(strings.ToUpper(index.Type))
- _, _ = out.WriteString(` `)
- }
-
- _, _ = out.WriteString(`(`)
- for i, expr := range index.Expressions {
- if i > 0 {
- _, _ = out.WriteString(`, `)
- }
- _, _ = out.WriteString(expr)
-
- // Handle descending order if specified
- if i < len(index.Descending) && index.Descending[i] {
- _, _ = out.WriteString(` DESC`)
- }
- }
- _, _ = out.WriteString(`)`)
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeCreateTable writes a CREATE TABLE statement
-func writeMigrationCreateTable(out *strings.Builder, schema, table string, columns []*storepb.ColumnMetadata, checks []*storepb.CheckConstraintMetadata) {
- _, _ = out.WriteString(`CREATE TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" (`)
- _, _ = out.WriteString("\n")
-
- // Write columns
- for i, col := range columns {
- if i > 0 {
- _, _ = out.WriteString(",\n")
- }
- _, _ = out.WriteString(" \"")
- _, _ = out.WriteString(col.Name)
- _, _ = out.WriteString("\" ")
- _, _ = out.WriteString(col.Type)
-
- if !col.Nullable {
- _, _ = out.WriteString(" NOT NULL")
- }
-
- defaultExpr := getDefaultExpression(col)
- if defaultExpr != "" {
- _, _ = out.WriteString(" DEFAULT ")
- _, _ = out.WriteString(defaultExpr)
- }
- }
-
- // Write check constraints
- for _, check := range checks {
- _, _ = out.WriteString(",\n")
- _, _ = out.WriteString(" CONSTRAINT \"")
- _, _ = out.WriteString(check.Name)
- _, _ = out.WriteString("\" CHECK (")
- _, _ = out.WriteString(check.Expression)
- _, _ = out.WriteString(")")
- }
-
- _, _ = out.WriteString("\n)")
-}
-
-// writePartitionClause writes the partition clause for a table
-func writeMigrationPartitionClause(out *strings.Builder, partition *storepb.TablePartitionMetadata) {
- _, _ = out.WriteString(" PARTITION BY ")
- _, _ = out.WriteString(partition.Expression)
-}
-
-// writePrimaryKey writes an ALTER TABLE ADD PRIMARY KEY statement
-func writeMigrationPrimaryKey(out *strings.Builder, schema, table string, index *storepb.IndexMetadata) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(index.Name)
- _, _ = out.WriteString(`" PRIMARY KEY (`)
-
- for i, expr := range index.Expressions {
- if i > 0 {
- _, _ = out.WriteString(`, `)
- }
- _, _ = out.WriteString(quoteIndexExpression(expr))
- }
-
- _, _ = out.WriteString(`);`)
- _, _ = out.WriteString("\n")
-}
-
-// writeUniqueKey writes an ALTER TABLE ADD UNIQUE statement
-func writeMigrationUniqueKey(out *strings.Builder, schema, table string, index *storepb.IndexMetadata) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(index.Name)
- _, _ = out.WriteString(`" UNIQUE (`)
-
- for i, expr := range index.Expressions {
- if i > 0 {
- _, _ = out.WriteString(`, `)
- }
- _, _ = out.WriteString(quoteIndexExpression(expr))
- }
-
- _, _ = out.WriteString(`);`)
- _, _ = out.WriteString("\n")
-}
-
-// writeIndex writes a CREATE INDEX statement
-func writeMigrationIndex(out *strings.Builder, schema, table string, index *storepb.IndexMetadata) {
- // If index has a full definition, use it directly
- if index.Definition != "" {
- _, _ = out.WriteString(index.Definition)
- if !strings.HasSuffix(strings.TrimSpace(index.Definition), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n")
- return
- }
-
- // Otherwise, construct the CREATE INDEX statement
- _, _ = out.WriteString(`CREATE `)
- if index.Unique {
- _, _ = out.WriteString(`UNIQUE `)
- }
- _, _ = out.WriteString(`INDEX "`)
- _, _ = out.WriteString(index.Name)
- _, _ = out.WriteString(`" ON "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" `)
-
- if index.Type != "" && index.Type != "BTREE" {
- _, _ = out.WriteString(`USING `)
- _, _ = out.WriteString(index.Type)
- _, _ = out.WriteString(` `)
- }
-
- _, _ = out.WriteString(`(`)
- for i, expr := range index.Expressions {
- if i > 0 {
- _, _ = out.WriteString(`, `)
- }
- _, _ = out.WriteString(expr)
-
- // Handle descending order if specified
- if i < len(index.Descending) && index.Descending[i] {
- _, _ = out.WriteString(` DESC`)
- }
- }
- _, _ = out.WriteString(`)`)
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// generateTableCommentChanges generates COMMENT ON TABLE statements for table comment changes
-func generateTableCommentChanges(buf *strings.Builder, tableDiff *schema.TableDiff) error {
- if tableDiff.OldTable == nil || tableDiff.NewTable == nil {
- return nil
- }
-
- oldComment := tableDiff.OldTable.Comment
- newComment := tableDiff.NewTable.Comment
-
- // If comments are different, generate COMMENT ON TABLE statement
- if oldComment != newComment {
- writeCommentOnTable(buf, tableDiff.SchemaName, tableDiff.TableName, newComment)
- }
-
- return nil
-}
-
-// generateColumnCommentChanges generates COMMENT ON COLUMN statements for column comment changes
-func generateColumnCommentChanges(buf *strings.Builder, tableDiff *schema.TableDiff) error {
- if tableDiff.OldTable == nil || tableDiff.NewTable == nil {
- return nil
- }
-
- // Build maps for efficient lookup
- oldColumnMap := make(map[string]*storepb.ColumnMetadata)
- for _, col := range tableDiff.OldTable.Columns {
- oldColumnMap[col.Name] = col
- }
-
- newColumnMap := make(map[string]*storepb.ColumnMetadata)
- for _, col := range tableDiff.NewTable.Columns {
- newColumnMap[col.Name] = col
- }
-
- // Check for comment changes in existing columns
- for _, newCol := range tableDiff.NewTable.Columns {
- if oldCol, exists := oldColumnMap[newCol.Name]; exists {
- // Column exists in both old and new - check if comment changed
- if oldCol.Comment != newCol.Comment {
- writeCommentOnColumn(buf, tableDiff.SchemaName, tableDiff.TableName, newCol.Name, newCol.Comment)
- }
- } else {
- // New column - if it has a comment, add it
- if newCol.Comment != "" {
- writeCommentOnColumn(buf, tableDiff.SchemaName, tableDiff.TableName, newCol.Name, newCol.Comment)
- }
- }
- }
-
- // Check for columns that were removed (comments need to be handled by column drop which is handled elsewhere)
-
- return nil
-}
-
-// writeCommentOnTable writes a COMMENT ON TABLE statement
-func writeCommentOnTable(out *strings.Builder, schema, table, comment string) {
- _, _ = out.WriteString(`COMMENT ON TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeCommentOnColumn writes a COMMENT ON COLUMN statement
-func writeCommentOnColumn(out *strings.Builder, schema, table, column, comment string) {
- _, _ = out.WriteString(`COMMENT ON COLUMN "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(column)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// generateSchemaCommentChanges generates COMMENT ON SCHEMA statements for schema comment changes
-func generateSchemaCommentChanges(buf *strings.Builder, diff *schema.MetadataDiff) error {
- for _, schemaDiff := range diff.SchemaChanges {
- if schemaDiff.Action == schema.MetadataDiffActionAlter {
- if schemaDiff.OldSchema == nil || schemaDiff.NewSchema == nil {
- continue
- }
-
- oldComment := schemaDiff.OldSchema.Comment
- newComment := schemaDiff.NewSchema.Comment
-
- // If comments are different, generate COMMENT ON SCHEMA statement
- if oldComment != newComment {
- writeCommentOnSchema(buf, schemaDiff.SchemaName, newComment)
- }
- }
- }
- return nil
-}
-
-// generateViewCommentChanges generates COMMENT ON VIEW statements for view comment changes
-func generateViewCommentChanges(buf *strings.Builder, diff *schema.MetadataDiff) error {
- for _, viewDiff := range diff.ViewChanges {
- if viewDiff.Action == schema.MetadataDiffActionAlter {
- if viewDiff.OldView == nil || viewDiff.NewView == nil {
- continue
- }
-
- oldComment := viewDiff.OldView.Comment
- newComment := viewDiff.NewView.Comment
-
- // If comments are different, generate COMMENT ON VIEW statement
- if oldComment != newComment {
- writeCommentOnView(buf, viewDiff.SchemaName, viewDiff.ViewName, newComment)
- }
- }
- }
- return nil
-}
-
-// generateMaterializedViewCommentChanges generates COMMENT ON MATERIALIZED VIEW statements
-func generateMaterializedViewCommentChanges(buf *strings.Builder, diff *schema.MetadataDiff) error {
- for _, mvDiff := range diff.MaterializedViewChanges {
- if mvDiff.Action == schema.MetadataDiffActionAlter {
- if mvDiff.OldMaterializedView == nil || mvDiff.NewMaterializedView == nil {
- continue
- }
-
- oldComment := mvDiff.OldMaterializedView.Comment
- newComment := mvDiff.NewMaterializedView.Comment
-
- // If comments are different, generate COMMENT ON MATERIALIZED VIEW statement
- if oldComment != newComment {
- writeCommentOnMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName, newComment)
- }
- }
- }
- return nil
-}
-
-// generateFunctionCommentChanges generates COMMENT ON FUNCTION statements for function comment changes
-func generateFunctionCommentChanges(buf *strings.Builder, diff *schema.MetadataDiff) error {
- for _, funcDiff := range diff.FunctionChanges {
- if funcDiff.Action == schema.MetadataDiffActionAlter {
- if funcDiff.OldFunction == nil || funcDiff.NewFunction == nil {
- continue
- }
-
- oldComment := funcDiff.OldFunction.Comment
- newComment := funcDiff.NewFunction.Comment
-
- // If comments are different, generate COMMENT ON FUNCTION statement
- if oldComment != newComment {
- writeCommentOnFunction(buf, funcDiff.SchemaName, funcDiff.NewFunction.Signature, newComment, funcDiff.NewASTNode, funcDiff.NewFunction.Definition)
- }
- }
- }
- return nil
-}
-
-// generateSequenceCommentChanges generates COMMENT ON SEQUENCE statements for sequence comment changes
-func generateSequenceCommentChanges(buf *strings.Builder, diff *schema.MetadataDiff) error {
- for _, seqDiff := range diff.SequenceChanges {
- if seqDiff.Action == schema.MetadataDiffActionAlter {
- if seqDiff.OldSequence == nil || seqDiff.NewSequence == nil {
- continue
- }
-
- oldComment := seqDiff.OldSequence.Comment
- newComment := seqDiff.NewSequence.Comment
-
- // If comments are different, generate COMMENT ON SEQUENCE statement
- if oldComment != newComment {
- writeCommentOnSequence(buf, seqDiff.SchemaName, seqDiff.SequenceName, newComment)
- }
- }
- }
- return nil
-}
-
-// generateSequenceOwnershipChanges generates ALTER SEQUENCE OWNED BY statements for sequence ownership changes
-func generateSequenceOwnershipChanges(buf *strings.Builder, diff *schema.MetadataDiff) error {
- for _, seqDiff := range diff.SequenceChanges {
- if seqDiff.Action == schema.MetadataDiffActionAlter {
- // Handle ownership changes using AST nodes
- if seqDiff.NewASTNode != nil {
- // Adding or modifying ownership - extract SQL from NewASTNode
- if err := writeMigrationAlterSequenceFromAST(buf, seqDiff.NewASTNode); err != nil {
- return errors.Wrapf(err, "failed to generate ALTER SEQUENCE for %s.%s",
- seqDiff.SchemaName, seqDiff.SequenceName)
- }
- } else if seqDiff.OldASTNode != nil {
- // Removing ownership - generate ALTER SEQUENCE ... OWNED BY NONE
- if err := writeMigrationRemoveSequenceOwnership(buf, seqDiff.SchemaName, seqDiff.SequenceName); err != nil {
- return errors.Wrapf(err, "failed to remove ownership for %s.%s",
- seqDiff.SchemaName, seqDiff.SequenceName)
- }
- }
- }
- }
- return nil
-}
-
-// writeMigrationAlterSequenceFromAST writes ALTER SEQUENCE statement from AST node
-func writeMigrationAlterSequenceFromAST(out *strings.Builder, astNode any) error {
- if astNode == nil {
- return errors.New("AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var alterSQL string
-
- // Try to cast to PostgreSQL AlterseqstmtContext
- if ctx, ok := astNode.(*pgparser.AlterseqstmtContext); ok {
- // First try to get text using token stream
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start != nil && stop != nil {
- alterSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if alterSQL == "" {
- alterSQL = ctx.GetText()
- }
- } else {
- return errors.Errorf("unsupported AST node type for ALTER SEQUENCE: %T", astNode)
- }
-
- if alterSQL == "" {
- return errors.New("failed to extract ALTER SEQUENCE SQL from AST node")
- }
-
- // Write the ALTER SQL
- _, _ = out.WriteString(alterSQL)
- // Ensure statement ends with semicolon
- if !strings.HasSuffix(strings.TrimSpace(alterSQL), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n\n")
-
- return nil
-}
-
-// writeMigrationRemoveSequenceOwnership writes ALTER SEQUENCE OWNED BY NONE statement
-//
-//nolint:unparam
-func writeMigrationRemoveSequenceOwnership(out *strings.Builder, schema, sequence string) error {
- _, _ = out.WriteString(`ALTER SEQUENCE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(sequence)
- _, _ = out.WriteString(`" OWNED BY NONE;`)
- _, _ = out.WriteString("\n\n")
- return nil
-}
-
-// generateIndexCommentChanges generates COMMENT ON INDEX statements for index comment changes within table diffs
-func generateIndexCommentChanges(buf *strings.Builder, tableDiff *schema.TableDiff) error {
- for _, indexDiff := range tableDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionAlter {
- if indexDiff.OldIndex == nil || indexDiff.NewIndex == nil {
- continue
- }
-
- oldComment := indexDiff.OldIndex.Comment
- newComment := indexDiff.NewIndex.Comment
-
- // If comments are different, generate COMMENT ON INDEX statement
- if oldComment != newComment {
- writeCommentOnIndex(buf, tableDiff.SchemaName, indexDiff.NewIndex.Name, newComment)
- }
- }
- }
- return nil
-}
-
-// generateMaterializedViewIndexCommentChanges generates COMMENT ON INDEX statements for index comment changes within materialized view diffs
-func generateMaterializedViewIndexCommentChanges(buf *strings.Builder, mvDiff *schema.MaterializedViewDiff) error {
- for _, indexDiff := range mvDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionAlter {
- if indexDiff.OldIndex == nil || indexDiff.NewIndex == nil {
- continue
- }
-
- oldComment := indexDiff.OldIndex.Comment
- newComment := indexDiff.NewIndex.Comment
-
- // If comments are different, generate COMMENT ON INDEX statement
- if oldComment != newComment {
- writeCommentOnIndex(buf, mvDiff.SchemaName, indexDiff.NewIndex.Name, newComment)
- }
- }
- }
- return nil
-}
-
-// Helper functions to write comment statements for different object types
-
-// writeCommentOnSchema writes a COMMENT ON SCHEMA statement
-func writeCommentOnSchema(out *strings.Builder, schema, comment string) {
- _, _ = out.WriteString(`COMMENT ON SCHEMA "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeCommentOnView writes a COMMENT ON VIEW statement
-func writeCommentOnView(out *strings.Builder, schema, view, comment string) {
- _, _ = out.WriteString(`COMMENT ON VIEW "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(view)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeCommentOnMaterializedView writes a COMMENT ON MATERIALIZED VIEW statement
-func writeCommentOnMaterializedView(out *strings.Builder, schema, view, comment string) {
- _, _ = out.WriteString(`COMMENT ON MATERIALIZED VIEW "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(view)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeCommentOnFunction writes a COMMENT ON FUNCTION/PROCEDURE statement
-func writeCommentOnFunction(out *strings.Builder, schema, signature, comment string, astNode any, definition string) {
- // Determine if this is a PROCEDURE or FUNCTION using AST-based detection
- objectType := "FUNCTION"
- if isFunctionProcedure(astNode, definition) {
- objectType = "PROCEDURE"
- }
-
- _, _ = out.WriteString(`COMMENT ON `)
- _, _ = out.WriteString(objectType)
- _, _ = out.WriteString(` "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`".`)
- _, _ = out.WriteString(signature)
- _, _ = out.WriteString(` IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeCommentOnSequence writes a COMMENT ON SEQUENCE statement
-func writeCommentOnSequence(out *strings.Builder, schema, sequence, comment string) {
- _, _ = out.WriteString(`COMMENT ON SEQUENCE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(sequence)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeCommentOnIndex writes a COMMENT ON INDEX statement
-func writeCommentOnIndex(out *strings.Builder, schema, index, comment string) {
- _, _ = out.WriteString(`COMMENT ON INDEX "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(index)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// generateEnumTypeCommentChanges generates COMMENT ON TYPE statements for enum type comment changes
-func generateEnumTypeCommentChanges(buf *strings.Builder, diff *schema.MetadataDiff) error {
- for _, enumDiff := range diff.EnumTypeChanges {
- if enumDiff.Action == schema.MetadataDiffActionAlter {
- if enumDiff.OldEnumType == nil || enumDiff.NewEnumType == nil {
- continue
- }
-
- oldComment := enumDiff.OldEnumType.Comment
- newComment := enumDiff.NewEnumType.Comment
-
- // If comments are different, generate COMMENT ON TYPE statement
- if oldComment != newComment {
- writeCommentOnType(buf, enumDiff.SchemaName, enumDiff.EnumTypeName, newComment)
- }
- }
- }
- return nil
-}
-
-// writeCommentOnType writes a COMMENT ON TYPE statement
-func writeCommentOnType(out *strings.Builder, schema, typeName, comment string) {
- _, _ = out.WriteString(`COMMENT ON TYPE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(typeName)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-func writeCommentOnExtension(out *strings.Builder, extensionName, comment string) {
- _, _ = out.WriteString(`COMMENT ON EXTENSION "`)
- _, _ = out.WriteString(extensionName)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-func writeCommentOnEventTrigger(out *strings.Builder, eventTriggerName, comment string) {
- _, _ = out.WriteString(`COMMENT ON EVENT TRIGGER "`)
- _, _ = out.WriteString(eventTriggerName)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-func writeCommentOnTrigger(out *strings.Builder, schemaName, tableName, triggerName, comment string) {
- // Parse table name to get schema and table separately
- parts := strings.Split(tableName, ".")
- var tableSchemaName, tableNameOnly string
- if len(parts) == 2 {
- tableSchemaName, tableNameOnly = parts[0], parts[1]
- } else {
- tableSchemaName, tableNameOnly = schemaName, tableName
- }
-
- _, _ = out.WriteString(`COMMENT ON TRIGGER "`)
- _, _ = out.WriteString(triggerName)
- _, _ = out.WriteString(`" ON "`)
- _, _ = out.WriteString(tableSchemaName)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(tableNameOnly)
- _, _ = out.WriteString(`" IS `)
- if comment == "" {
- _, _ = out.WriteString(`NULL`)
- } else {
- _, _ = out.WriteString(`'`)
- // Escape single quotes in the comment
- escapedComment := strings.ReplaceAll(comment, "'", "''")
- _, _ = out.WriteString(escapedComment)
- _, _ = out.WriteString(`'`)
- }
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// generateCommentChangesFromSDL generates COMMENT ON statements from SDL-based comment diffs
-// This handles comment changes detected from SDL diff mode (AST-based)
-func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDiff) error {
- if len(diff.CommentChanges) == 0 {
- return nil
- }
-
- // Build a set of tables being dropped
- // When a table is dropped, its comments are automatically removed
- tablesBeingDropped := make(map[string]bool)
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionDrop {
- tableKey := tableDiff.SchemaName + "." + tableDiff.TableName
- tablesBeingDropped[tableKey] = true
- }
- }
-
- // Build a set of columns being dropped
- // When a column is dropped, its comment is automatically removed
- columnsBeingDropped := make(map[string]bool)
- for _, tableDiff := range diff.TableChanges {
- if tableDiff.Action == schema.MetadataDiffActionAlter {
- for _, colDiff := range tableDiff.ColumnChanges {
- if colDiff.Action == schema.MetadataDiffActionDrop {
- var columnName string
- if colDiff.OldColumn != nil {
- columnName = colDiff.OldColumn.Name
- } else if colDiff.OldASTNode != nil {
- columnName = extractColumnNameFromAST(colDiff.OldASTNode)
- }
- if columnName != "" {
- columnKey := tableDiff.SchemaName + "." + tableDiff.TableName + "." + columnName
- columnsBeingDropped[columnKey] = true
- }
- }
- }
- }
- }
-
- for _, commentDiff := range diff.CommentChanges {
- // Extract the new comment text from the AST node
- newComment := extractCommentFromDiff(commentDiff)
-
- switch commentDiff.ObjectType {
- case schema.CommentObjectTypeSchema:
- writeCommentOnSchema(buf, commentDiff.SchemaName, newComment)
-
- case schema.CommentObjectTypeTable:
- // Skip comment generation for tables being dropped
- tableKey := commentDiff.SchemaName + "." + commentDiff.ObjectName
- if tablesBeingDropped[tableKey] {
- continue
- }
- writeCommentOnTable(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
-
- case schema.CommentObjectTypeColumn:
- // Skip comment generation for columns being dropped
- columnKey := commentDiff.SchemaName + "." + commentDiff.ObjectName + "." + commentDiff.ColumnName
- if columnsBeingDropped[columnKey] {
- continue
- }
- writeCommentOnColumn(buf, commentDiff.SchemaName, commentDiff.ObjectName, commentDiff.ColumnName, newComment)
-
- case schema.CommentObjectTypeView:
- writeCommentOnView(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
-
- case schema.CommentObjectTypeMaterializedView:
- writeCommentOnMaterializedView(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
-
- case schema.CommentObjectTypeFunction:
- // For functions, ObjectName contains the function signature
- // Try to find the function definition and AST node to determine if it's a FUNCTION or PROCEDURE
- var functionDefinition string
- var functionASTNode any
- functionKey := commentDiff.SchemaName + "." + commentDiff.ObjectName
- for _, funcDiff := range diff.FunctionChanges {
- // Check both NewFunction and OldFunction to handle comment removal cases
- if funcDiff.NewFunction != nil {
- funcKey := funcDiff.SchemaName + "." + funcDiff.NewFunction.Signature
- if funcKey == functionKey {
- functionDefinition = funcDiff.NewFunction.Definition
- functionASTNode = funcDiff.NewASTNode
- break
- }
- }
- if funcDiff.OldFunction != nil {
- funcKey := funcDiff.SchemaName + "." + funcDiff.OldFunction.Signature
- if funcKey == functionKey {
- // Only use OldFunction if we haven't found NewFunction
- if functionDefinition == "" {
- functionDefinition = funcDiff.OldFunction.Definition
- functionASTNode = funcDiff.OldASTNode
- }
- break
- }
- }
- }
- // If we didn't find the function AST node from FunctionChanges, use the comment AST node
- // to determine if it's a FUNCTION or PROCEDURE
- // Check both NewASTNode (for adding comments) and OldASTNode (for removing comments)
- if functionASTNode == nil {
- if commentDiff.NewASTNode != nil {
- functionASTNode = commentDiff.NewASTNode
- } else if commentDiff.OldASTNode != nil {
- functionASTNode = commentDiff.OldASTNode
- }
- }
- writeCommentOnFunction(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment, functionASTNode, functionDefinition)
-
- case schema.CommentObjectTypeSequence:
- writeCommentOnSequence(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
-
- case schema.CommentObjectTypeIndex:
- // Index name is stored in IndexName field
- indexName := commentDiff.IndexName
- if indexName == "" {
- // Fallback to ObjectName if IndexName is not set
- indexName = commentDiff.ObjectName
- }
- writeCommentOnIndex(buf, commentDiff.SchemaName, indexName, newComment)
-
- case schema.CommentObjectTypeType:
- // COMMENT ON TYPE (for enum types)
- writeCommentOnType(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
-
- case schema.CommentObjectTypeExtension:
- // COMMENT ON EXTENSION
- writeCommentOnExtension(buf, commentDiff.ObjectName, newComment)
-
- case schema.CommentObjectTypeEventTrigger:
- // COMMENT ON EVENT TRIGGER
- writeCommentOnEventTrigger(buf, commentDiff.ObjectName, newComment)
-
- case schema.CommentObjectTypeTrigger:
- // COMMENT ON TRIGGER trigger_name ON table_name
- writeCommentOnTrigger(buf, commentDiff.SchemaName, commentDiff.TableName, commentDiff.ObjectName, newComment)
-
- default:
- // Unknown object type, skip
- continue
- }
- }
-
- return nil
-}
-
-// extractCommentFromDiff extracts the comment text from a CommentDiff
-// It uses the NewComment field which contains the extracted comment text
-func extractCommentFromDiff(commentDiff *schema.CommentDiff) string {
- return commentDiff.NewComment
-}
-
-// convertToCreateOrReplace uses ANTLR parser to safely convert CREATE FUNCTION to CREATE OR REPLACE FUNCTION
-func convertToCreateOrReplace(definition string) string {
- // Parse the SQL statement using ANTLR
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := pgparser.NewPostgreSQLParser(stream)
-
- // Parse the root
- tree := parser.Root()
- if tree == nil {
- // If parsing fails, return original definition
- return definition
- }
-
- // Create a visitor to find and modify CREATE FUNCTION statements
- visitor := &CreateOrReplaceVisitor{
- tokens: stream,
- rewriter: antlr.NewTokenStreamRewriter(stream),
- definition: definition,
- }
-
- // Visit the tree
- visitor.Visit(tree)
-
- // Get the modified text
- interval := antlr.NewInterval(0, len(definition)-1)
- result := visitor.rewriter.GetText("", interval)
- if result == "" {
- // If rewriting fails, return original definition
- return definition
- }
-
- return result
-}
-
-// CreateOrReplaceVisitor visits the parse tree and modifies CREATE FUNCTION to CREATE OR REPLACE FUNCTION
-type CreateOrReplaceVisitor struct {
- *pgparser.BasePostgreSQLParserVisitor
- tokens *antlr.CommonTokenStream
- rewriter *antlr.TokenStreamRewriter
- definition string
-}
-
-// Visit implements the visitor pattern
-func (v *CreateOrReplaceVisitor) Visit(tree antlr.ParseTree) any {
- switch t := tree.(type) {
- case *pgparser.CreatefunctionstmtContext:
- return v.visitCreateFunctionStmt(t)
- default:
- // Continue visiting children
- return v.visitChildren(tree)
- }
-}
-
-// visitChildren visits all children of a node
-func (v *CreateOrReplaceVisitor) visitChildren(node antlr.ParseTree) any {
- for i := 0; i < node.GetChildCount(); i++ {
- child := node.GetChild(i)
- if parseTree, ok := child.(antlr.ParseTree); ok {
- v.Visit(parseTree)
- }
- }
- return nil
-}
-
-// visitCreateFunctionStmt handles CREATE FUNCTION statements
-func (v *CreateOrReplaceVisitor) visitCreateFunctionStmt(ctx *pgparser.CreatefunctionstmtContext) any {
- if ctx == nil {
- return nil
- }
-
- // Check if "OR REPLACE" already exists
- if v.hasOrReplace(ctx) {
- // Already has "OR REPLACE", no need to modify
- return nil
- }
-
- // Find the CREATE token
- createToken := ctx.GetStart()
- if createToken == nil {
- return nil
- }
-
- // Look for the FUNCTION/PROCEDURE keyword after CREATE
- functionToken := v.findFunctionOrProcedureToken(ctx)
- if functionToken == nil {
- return nil
- }
-
- // Insert "OR REPLACE" between CREATE and FUNCTION/PROCEDURE
- // We insert it right before the FUNCTION token
- v.rewriter.InsertBefore("", functionToken.GetTokenIndex(), "OR REPLACE ")
-
- return nil
-}
-
-// extractColumnNameFromAST extracts column name from column definition AST node
-func extractColumnNameFromAST(columnASTNode pgparser.IColumnDefContext) string {
- if columnASTNode == nil {
- return ""
- }
-
- // Use the proper normalization function for PostgreSQL column identifiers
- if columnASTNode.Colid() != nil {
- return pgpluginparser.NormalizePostgreSQLColid(columnASTNode.Colid())
- }
-
- return ""
-}
-
-// extractColumnDefinitionFromAST extracts the full column definition from AST node
-func extractColumnDefinitionFromAST(columnASTNode pgparser.IColumnDefContext) string {
- if columnASTNode == nil {
- return ""
- }
-
- // Use the getTextFromAST helper to extract text properly
- return getTextFromAST(columnASTNode)
-}
-
-// findFunctionOrProcedureToken finds the FUNCTION or PROCEDURE token in the CREATE FUNCTION/PROCEDURE statement
-func (v *CreateOrReplaceVisitor) findFunctionOrProcedureToken(ctx *pgparser.CreatefunctionstmtContext) antlr.Token {
- // Get all tokens in the context range
- start := ctx.GetStart().GetTokenIndex()
- stop := ctx.GetStop().GetTokenIndex()
-
- for i := start; i <= stop; i++ {
- token := v.tokens.Get(i)
- // Check for both FUNCTION and PROCEDURE tokens since they share the same grammar rule
- if token.GetTokenType() == pgparser.PostgreSQLParserFUNCTION ||
- token.GetTokenType() == pgparser.PostgreSQLParserPROCEDURE {
- return token
- }
- }
-
- return nil
-}
-
-// hasOrReplace checks if the CREATE FUNCTION/PROCEDURE statement already contains "OR REPLACE"
-func (v *CreateOrReplaceVisitor) hasOrReplace(ctx *pgparser.CreatefunctionstmtContext) bool {
- // Get all tokens in the context range between CREATE and FUNCTION/PROCEDURE
- start := ctx.GetStart().GetTokenIndex()
- functionToken := v.findFunctionOrProcedureToken(ctx)
- if functionToken == nil {
- return false
- }
- stop := functionToken.GetTokenIndex()
-
- // Look for "OR" followed by "REPLACE" tokens, skipping whitespace
- for i := start; i < stop; i++ {
- token := v.tokens.Get(i)
- if token.GetTokenType() == pgparser.PostgreSQLParserOR {
- // Found OR, now look for REPLACE after it (skipping whitespace)
- for j := i + 1; j < stop; j++ {
- nextToken := v.tokens.Get(j)
- // Skip whitespace tokens (channel 1 is hidden channel based on debug output)
- if nextToken.GetChannel() == 1 {
- continue
- }
- // Check if the next non-whitespace token is REPLACE
- if nextToken.GetTokenType() == pgparser.PostgreSQLParserREPLACE {
- return true
- }
- // If it's not REPLACE, this OR is not our OR REPLACE pattern
- break
- }
- }
- }
-
- return false
-}
-
-// getViewDependenciesFromAST extracts view dependencies from AST node using lightweight access table extraction.
-// This avoids the complexity and potential panics of GetQuerySpan by only extracting table/view references.
-// The caller is responsible for filtering dependencies against the set of objects being migrated.
-func getViewDependenciesFromAST(astNode any, schemaName string, _ *storepb.DatabaseSchemaMetadata) []*storepb.DependencyColumn {
- if astNode == nil {
- return []*storepb.DependencyColumn{}
- }
-
- var selectStatement string
-
- if ctx, ok := astNode.(*pgparser.ViewstmtContext); ok {
- if ctx.Selectstmt() != nil {
- // Try to get text using token stream first
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.Selectstmt().GetStart()
- stop := ctx.Selectstmt().GetStop()
- if start != nil && stop != nil {
- selectStatement = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to token-based approach if failed
- if selectStatement == "" {
- selectStatement = getTextFromAST(ctx.Selectstmt())
- }
- }
- }
-
- if selectStatement == "" {
- return []*storepb.DependencyColumn{}
- }
-
- queryStatement := strings.TrimSpace(selectStatement)
-
- accessTables, err := pgpluginparser.ExtractAccessTables(queryStatement, pgpluginparser.ExtractAccessTablesOption{
- DefaultDatabase: "",
- DefaultSchema: schemaName,
- SkipMetadataValidation: true,
- })
- if err != nil {
- return []*storepb.DependencyColumn{}
- }
-
- // The caller will filter against allObjects
- dependencyMap := make(map[string]*storepb.DependencyColumn)
- for _, resource := range accessTables {
- if resource.Schema == "pg_catalog" || resource.Schema == "information_schema" {
- continue
- }
-
- resourceSchema := resource.Schema
- if resourceSchema == "" {
- resourceSchema = schemaName
- }
-
- key := fmt.Sprintf("%s.%s", resourceSchema, resource.Table)
- if _, exists := dependencyMap[key]; !exists {
- dependencyMap[key] = &storepb.DependencyColumn{
- Schema: resourceSchema,
- Table: resource.Table,
- Column: "*", // Table-level dependencies
- }
- }
- }
-
- var dependencies []*storepb.DependencyColumn
- for _, dep := range dependencyMap {
- dependencies = append(dependencies, dep)
- }
-
- return dependencies
-}
-
-// getMaterializedViewDependenciesFromAST extracts table/view dependencies from a materialized view's AST node
-func getMaterializedViewDependenciesFromAST(astNode any, schemaName string, _ *storepb.DatabaseSchemaMetadata) []*storepb.DependencyColumn {
- if astNode == nil {
- return []*storepb.DependencyColumn{}
- }
-
- var selectStatement string
-
- if ctx, ok := astNode.(*pgparser.CreatematviewstmtContext); ok {
- if ctx.Selectstmt() != nil {
- // Try to get text using token stream first
- if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
- start := ctx.Selectstmt().GetStart()
- stop := ctx.Selectstmt().GetStop()
- if start != nil && stop != nil {
- selectStatement = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to token-based approach if failed
- if selectStatement == "" {
- selectStatement = getTextFromAST(ctx.Selectstmt())
- }
- }
- }
-
- if selectStatement == "" {
- return []*storepb.DependencyColumn{}
- }
-
- queryStatement := strings.TrimSpace(selectStatement)
-
- accessTables, err := pgpluginparser.ExtractAccessTables(queryStatement, pgpluginparser.ExtractAccessTablesOption{
- DefaultDatabase: "",
- DefaultSchema: schemaName,
- SkipMetadataValidation: true,
- })
- if err != nil {
- return []*storepb.DependencyColumn{}
- }
-
- // The caller will filter against allObjects
- dependencyMap := make(map[string]*storepb.DependencyColumn)
- for _, resource := range accessTables {
- if resource.Schema == "pg_catalog" || resource.Schema == "information_schema" {
- continue
- }
-
- resourceSchema := resource.Schema
- if resourceSchema == "" {
- resourceSchema = schemaName
- }
-
- key := fmt.Sprintf("%s.%s", resourceSchema, resource.Table)
- if _, exists := dependencyMap[key]; !exists {
- dependencyMap[key] = &storepb.DependencyColumn{
- Schema: resourceSchema,
- Table: resource.Table,
- Column: "*", // Table-level dependencies
- }
- }
- }
-
- var dependencies []*storepb.DependencyColumn
- for _, dep := range dependencyMap {
- dependencies = append(dependencies, dep)
- }
-
- return dependencies
-}
-
-// extractColumnMetadataFromAST extracts column metadata from AST node without normalization (except column name)
-// This is used in AST-only mode where we need the raw values for comparison
-func extractColumnMetadataFromAST(columnDef pgparser.IColumnDefContext) *columnMetadataAST {
- if columnDef == nil {
- return nil
- }
-
- // Only normalize the column name - everything else stays as raw text
- columnName := ""
- if columnDef.Colid() != nil {
- columnName = pgpluginparser.NormalizePostgreSQLColid(columnDef.Colid())
- }
-
- metadata := &columnMetadataAST{
- Name: columnName,
- }
-
- // Extract type as raw text
- if columnDef.Typename() != nil {
- metadata.Type = getTextFromAST(columnDef.Typename())
- }
-
- // Extract nullable - check AST directly for NOT NULL constraint
- metadata.Nullable = true // Default is nullable
- if columnDef.Colquallist() != nil {
- for _, qualifier := range columnDef.Colquallist().AllColconstraint() {
- if qualifier != nil && qualifier.Colconstraintelem() != nil {
- elem := qualifier.Colconstraintelem()
- // Check for NOT NULL constraint using AST inspection
- if elem.NOT() != nil && elem.NULL_P() != nil {
- metadata.Nullable = false
- break
- }
- // Check for explicit NULL constraint
- if elem.NULL_P() != nil && elem.NOT() == nil {
- metadata.Nullable = true
- }
- }
- }
- }
-
- // Extract default value using AST inspection
- if columnDef.Colquallist() != nil {
- for _, qualifier := range columnDef.Colquallist().AllColconstraint() {
- if qualifier != nil && qualifier.Colconstraintelem() != nil {
- elem := qualifier.Colconstraintelem()
- // Check for DEFAULT constraint using AST inspection
- if elem.DEFAULT() != nil && elem.B_expr() != nil {
- metadata.Default = getTextFromAST(elem.B_expr())
- break
- }
- }
- }
- }
-
- return metadata
-}
-
-// columnMetadataAST holds column metadata extracted from AST without normalization
-type columnMetadataAST struct {
- Name string
- Type string
- Nullable bool
- Default string
-}
-
-// getTextFromAST extracts text from AST node using tokens when available, fallback to GetText()
-func getTextFromAST(ctx antlr.ParseTree) string {
- if ctx == nil {
- return ""
- }
-
- // Try to get text from tokens first (preferred method)
- // Check for interfaces that have the required methods
- type parserContext interface {
- GetParser() antlr.Parser
- GetStart() antlr.Token
- GetStop() antlr.Token
- }
-
- if ruleContext, ok := ctx.(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 GetText() if tokens are not available
- return ctx.GetText()
-}
-
-// extractConstraintNameFromAST extracts constraint name from table constraint AST node
-func extractConstraintNameFromAST(constraintAST pgparser.ITableconstraintContext) string {
- if constraintAST == nil {
- return ""
- }
-
- if constraintAST.Name() != nil {
- return pgpluginparser.NormalizePostgreSQLName(constraintAST.Name())
- }
-
- // If no explicit name, generate one based on constraint type and content
- if constraintAST.Constraintelem() != nil {
- elem := constraintAST.Constraintelem()
-
- // For CHECK constraints, try to generate a meaningful name
- if elem.CHECK() != nil {
- // Extract table name from context if possible, otherwise use a generic name
- return "check_constraint"
- }
-
- // For FOREIGN KEY constraints
- if elem.FOREIGN() != nil && elem.KEY() != nil {
- return "foreign_key_constraint"
- }
-
- // For UNIQUE constraints
- if elem.UNIQUE() != nil {
- return "unique_constraint"
- }
-
- // For PRIMARY KEY constraints
- if elem.PRIMARY() != nil && elem.KEY() != nil {
- return "primary_key_constraint"
- }
- }
-
- // Fallback: use the full constraint text as identifier
- return getTextFromAST(constraintAST)
-}
-
-// writeDropCheckConstraintFromAST drops a check constraint using AST node
-func writeDropCheckConstraintFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) {
- constraintName := extractConstraintNameFromAST(constraintAST)
- if constraintName == "" {
- // If we can't extract a name, use the constraint text as fallback
- constraintName = getTextFromAST(constraintAST)
- }
- writeDropCheckConstraint(out, schema, table, constraintName)
-}
-
-// writeDropForeignKeyFromAST drops a foreign key constraint using AST node
-func writeDropForeignKeyFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) {
- constraintName := extractConstraintNameFromAST(constraintAST)
- if constraintName == "" {
- // If we can't extract a name, use the constraint text as fallback
- constraintName = getTextFromAST(constraintAST)
- }
- writeDropForeignKey(out, schema, table, constraintName)
-}
-
-// writeDropPrimaryKeyFromAST drops a primary key constraint using AST node
-func writeDropPrimaryKeyFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) {
- constraintName := extractConstraintNameFromAST(constraintAST)
- if constraintName == "" {
- // If we can't extract a name, generate a default primary key name
- constraintName = fmt.Sprintf("%s_pkey", table)
- }
- writeDropPrimaryKey(out, schema, table, constraintName)
-}
-
-// writeAddCheckConstraintFromAST adds a check constraint using AST node
-func writeAddCheckConstraintFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) error {
- if constraintAST == nil {
- return errors.New("constraint AST node is nil")
- }
-
- constraintName := extractConstraintNameFromAST(constraintAST)
-
- // Extract CHECK expression
- if constraintAST.Constraintelem() != nil {
- elem := constraintAST.Constraintelem()
- if elem.CHECK() != nil && elem.A_expr() != nil {
- checkExpr := getTextFromAST(elem.A_expr())
-
- // Generate constraint name if not provided
- if constraintName == "" || constraintName == "check_constraint" {
- constraintName = fmt.Sprintf("%s_check", table)
- }
-
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(constraintName)
- _, _ = out.WriteString(`" CHECK (`)
- _, _ = out.WriteString(checkExpr)
- _, _ = out.WriteString(`);`)
- _, _ = out.WriteString("\n")
- return nil
- }
- }
-
- return errors.New("could not extract CHECK constraint from AST node")
-}
-
-// writeAddExcludeConstraint adds an EXCLUDE constraint using constraint metadata
-func writeAddExcludeConstraint(out *strings.Builder, schema, table string, exclude *storepb.ExcludeConstraintMetadata) {
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(exclude.Name)
- _, _ = out.WriteString(`" `)
- _, _ = out.WriteString(exclude.Expression) // Already includes "EXCLUDE USING ..."
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
-}
-
-// writeAddExcludeConstraintFromAST adds an EXCLUDE constraint using AST node
-func writeAddExcludeConstraintFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) error {
- if constraintAST == nil {
- return errors.New("constraint AST node is nil")
- }
-
- constraintName := extractConstraintNameFromAST(constraintAST)
-
- // Extract EXCLUDE constraint definition
- if constraintAST.Constraintelem() != nil {
- elem := constraintAST.Constraintelem()
- if elem.EXCLUDE() != nil {
- // Get the full EXCLUDE expression (EXCLUDE USING ...)
- excludeExpr := getTextFromAST(elem)
-
- // Generate constraint name if not provided
- if constraintName == "" || constraintName == "exclude_constraint" {
- constraintName = fmt.Sprintf("%s_exclude", table)
- }
-
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(constraintName)
- _, _ = out.WriteString(`" `)
- _, _ = out.WriteString(excludeExpr) // Full expression including "EXCLUDE USING ..."
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
- return nil
- }
- }
-
- return errors.New("could not extract EXCLUDE constraint from AST node")
-}
-
-// writeDropExcludeConstraintFromAST drops an EXCLUDE constraint using AST node
-func writeDropExcludeConstraintFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) {
- constraintName := extractConstraintNameFromAST(constraintAST)
- if constraintName == "" {
- // If we can't extract a name, use the constraint text as fallback
- constraintName = getTextFromAST(constraintAST)
- }
- writeDropConstraint(out, schema, table, constraintName)
-}
-
-// writeAddForeignKeyFromAST adds a foreign key constraint using AST node
-func writeAddForeignKeyFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) error {
- if constraintAST == nil {
- return errors.New("constraint AST node is nil")
- }
-
- constraintName := extractConstraintNameFromAST(constraintAST)
-
- // Extract FOREIGN KEY definition
- if constraintAST.Constraintelem() != nil {
- elem := constraintAST.Constraintelem()
- if elem.FOREIGN() != nil && elem.KEY() != nil {
- // Generate constraint name if not provided
- if constraintName == "" || constraintName == "foreign_key_constraint" {
- constraintName = fmt.Sprintf("%s_fkey", table)
- }
-
- // Extract the FOREIGN KEY part from the full constraint
- // This is a simplified approach - we'll use the original constraint text
- // but replace the constraint name if needed
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(constraintName)
- _, _ = out.WriteString(`" `)
-
- // Extract just the FOREIGN KEY part (after constraint name)
- if constraintAST.Constraintelem() != nil {
- fkElem := constraintAST.Constraintelem()
- fkText := getTextFromAST(fkElem)
- _, _ = out.WriteString(fkText)
- }
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
- return nil
- }
- }
-
- return errors.New("could not extract FOREIGN KEY constraint from AST node")
-}
-
-// writeAllForeignKeysFromTableAST extracts all FK constraints from a CREATE TABLE AST node
-// and writes ALTER TABLE ADD CONSTRAINT statements for each one.
-// This is used when tables are created without FK constraints due to cycles,
-// and the FK constraints need to be added separately afterwards.
-func writeAllForeignKeysFromTableAST(out *strings.Builder, schema, table string, astNode any) {
- if astNode == nil {
- return
- }
-
- ctx, ok := astNode.(*pgparser.CreatestmtContext)
- if !ok {
- return
- }
-
- // Check if table has any elements
- if ctx.Opttableelementlist() == nil {
- return
- }
-
- tableElementList := ctx.Opttableelementlist().Tableelementlist()
- if tableElementList == nil {
- return
- }
-
- // Find and process FK constraints
- 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 - add it
- if err := writeAddForeignKeyFromAST(out, schema, table, constraint); err != nil {
- // Log error but continue with other FK constraints
- _, _ = fmt.Fprintf(out, "-- Error adding foreign key constraint: %v\n", err)
- }
- }
- }
- }
- }
-}
-
-// writeViewDiff writes a view creation/alteration based on the diff.
-func writeViewDiff(buf *strings.Builder, viewDiff *schema.ViewDiff) error {
- switch viewDiff.Action {
- case schema.MetadataDiffActionCreate, schema.MetadataDiffActionAlter:
- if viewDiff.NewView != nil {
- if err := writeMigrationView(buf, viewDiff.SchemaName, viewDiff.NewView); err != nil {
- return err
- }
- } else if viewDiff.NewASTNode != nil {
- if err := writeMigrationViewFromAST(buf, viewDiff.NewASTNode); err != nil {
- return err
- }
- } else {
- return errors.Errorf("view diff for %s.%s has neither metadata nor AST node", viewDiff.SchemaName, viewDiff.ViewName)
- }
- default:
- // Other actions (like Delete) are handled elsewhere
- }
- // Add view comment for newly created views
- if viewDiff.NewView != nil && viewDiff.NewView.Comment != "" {
- writeCommentOnView(buf, viewDiff.SchemaName, viewDiff.ViewName, viewDiff.NewView.Comment)
- }
- return nil
-}
-
-// writeMaterializedViewDiff writes a materialized view creation/alteration based on the diff.
-func writeMaterializedViewDiff(buf *strings.Builder, mvDiff *schema.MaterializedViewDiff) error {
- switch mvDiff.Action {
- case schema.MetadataDiffActionCreate:
- // Support AST-only mode: if metadata is nil but AST node exists, use AST
- if mvDiff.NewMaterializedView != nil {
- if err := writeMigrationMaterializedView(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView); err != nil {
- return err
- }
- } else if mvDiff.NewASTNode != nil {
- if err := writeMigrationMaterializedViewFromAST(buf, mvDiff.NewASTNode); err != nil {
- return err
- }
- } else {
- return errors.Errorf("materialized view diff for %s.%s has neither metadata nor AST node", mvDiff.SchemaName, mvDiff.MaterializedViewName)
- }
- case schema.MetadataDiffActionAlter:
- // Check if this is an index-only change (no MV definition change)
- hasIndexOnlyChanges := len(mvDiff.IndexChanges) > 0 && mvDiff.NewMaterializedView == nil && mvDiff.NewASTNode == nil
-
- // For index-only changes, don't alter the MV itself
- // Index changes are processed separately later
- if !hasIndexOnlyChanges {
- // For PostgreSQL materialized views, we need to drop and recreate
- // since ALTER MATERIALIZED VIEW doesn't support changing the definition
- writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)
- // Support AST-only mode
- if mvDiff.NewMaterializedView != nil {
- if err := writeMigrationMaterializedView(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView); err != nil {
- return err
- }
- } else if mvDiff.NewASTNode != nil {
- if err := writeMigrationMaterializedViewFromAST(buf, mvDiff.NewASTNode); err != nil {
- return err
- }
- } else {
- return errors.Errorf("materialized view ALTER for %s.%s has neither metadata nor AST node", mvDiff.SchemaName, mvDiff.MaterializedViewName)
- }
- }
- default:
- // Other actions (like Delete) are handled elsewhere
- }
- // Add materialized view comment for newly created or altered materialized views
- if mvDiff.NewMaterializedView != nil && mvDiff.NewMaterializedView.Comment != "" {
- writeCommentOnMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName, mvDiff.NewMaterializedView.Comment)
- }
- return nil
-}
-
-// writeCreateTableDiff creates a table based on the diff, optionally including FK.
-func writeCreateTableDiff(buf *strings.Builder, tableDiff *schema.TableDiff, includeFK bool) error {
- if tableDiff.NewTable != nil {
- // Metadata mode: use generateCreateTable
- createTableSQL, err := generateCreateTable(tableDiff.SchemaName, tableDiff.TableName, tableDiff.NewTable, includeFK)
- if err != nil {
- return err
- }
- _, _ = buf.WriteString(createTableSQL)
- if createTableSQL != "" {
- _, _ = buf.WriteString("\n")
- }
-
- // Add table and column comments for newly created tables
- if tableDiff.NewTable.Comment != "" {
- writeCommentOnTable(buf, tableDiff.SchemaName, tableDiff.TableName, tableDiff.NewTable.Comment)
- }
- for _, col := range tableDiff.NewTable.Columns {
- if col.Comment != "" {
- writeCommentOnColumn(buf, tableDiff.SchemaName, tableDiff.TableName, col.Name, col.Comment)
- }
- }
- } else if tableDiff.NewASTNode != nil {
- // AST-only mode
- if includeFK {
- if err := writeMigrationTableFromAST(buf, tableDiff.NewASTNode); err != nil {
- return err
- }
- } else {
- if err := writeMigrationTableFromASTWithoutFK(buf, tableDiff.NewASTNode); err != nil {
- return err
- }
- }
- } else {
- return errors.Errorf("table CREATE action requires either NewTable metadata or NewASTNode for table %s.%s", tableDiff.SchemaName, tableDiff.TableName)
- }
- return nil
-}
-
-// writeTableIndexesForCreate writes indexes for a newly created table.
-func writeTableIndexesForCreate(buf *strings.Builder, tableDiff *schema.TableDiff) {
- for _, indexDiff := range tableDiff.IndexChanges {
- if indexDiff.Action == schema.MetadataDiffActionCreate {
- if indexDiff.NewIndex != nil {
- // Metadata mode: use index metadata
- if indexDiff.NewIndex.IsConstraint {
- // Skip constraint-based indexes (primary key, unique, exclude)
- // They are already included in CREATE TABLE statement
- continue
- }
- writeMigrationIndex(buf, tableDiff.SchemaName, tableDiff.TableName, indexDiff.NewIndex)
- } else if indexDiff.NewASTNode != nil {
- // AST-only mode: extract from AST node
- if indexAST, ok := indexDiff.NewASTNode.(*pgparser.IndexstmtContext); ok {
- if err := writeCreateIndexFromAST(buf, indexAST); err != nil {
- _, _ = fmt.Fprintf(buf, "-- Error creating index: %v\n", err)
- }
- }
- }
- }
- }
-}
-
-// writeTableForeignKeysForCreate writes foreign keys for a CREATE table operation.
-func writeTableForeignKeysForCreate(buf *strings.Builder, tableDiff *schema.TableDiff) error {
- if tableDiff.NewTable != nil {
- // Metadata mode
- for _, fk := range tableDiff.NewTable.ForeignKeys {
- if err := writeMigrationForeignKey(buf, tableDiff.SchemaName, tableDiff.TableName, fk); err != nil {
- return err
- }
- }
- } else if tableDiff.NewASTNode != nil {
- // AST mode
- if len(tableDiff.ForeignKeyChanges) > 0 {
- // FKs are in ForeignKeyChanges
- for _, fkDiff := range tableDiff.ForeignKeyChanges {
- if fkDiff.Action == schema.MetadataDiffActionCreate {
- if fkDiff.NewForeignKey != nil {
- if err := writeMigrationForeignKey(buf, tableDiff.SchemaName, tableDiff.TableName, fkDiff.NewForeignKey); err != nil {
- return err
- }
- } else if fkDiff.NewASTNode != nil {
- if constraintAST, ok := fkDiff.NewASTNode.(pgparser.ITableconstraintContext); ok {
- if err := writeAddForeignKeyFromAST(buf, tableDiff.SchemaName, tableDiff.TableName, constraintAST); err != nil {
- _, _ = fmt.Fprintf(buf, "-- Error adding foreign key constraint: %v\n", err)
- }
- }
- }
- }
- }
- } else {
- // Pure AST mode - FKs are embedded in the AST node itself
- writeAllForeignKeysFromTableAST(buf, tableDiff.SchemaName, tableDiff.TableName, tableDiff.NewASTNode)
- }
- }
- return nil
-}
-
-// writeAddUniqueConstraintFromAST adds a unique constraint using AST node
-func writeAddUniqueConstraintFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) error {
- if constraintAST == nil {
- return errors.New("constraint AST node is nil")
- }
-
- constraintName := extractConstraintNameFromAST(constraintAST)
-
- // Extract UNIQUE definition
- if constraintAST.Constraintelem() != nil {
- elem := constraintAST.Constraintelem()
- if elem.UNIQUE() != nil {
- // Generate constraint name if not provided
- if constraintName == "" || constraintName == "unique_constraint" {
- constraintName = fmt.Sprintf("%s_unique", table)
- }
-
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(constraintName)
- _, _ = out.WriteString(`" `)
-
- // Extract the UNIQUE part
- uniqueText := getTextFromAST(elem)
- _, _ = out.WriteString(uniqueText)
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
- return nil
- }
- }
-
- return errors.New("could not extract UNIQUE constraint from AST node")
-}
-
-// writeAddPrimaryKeyFromAST adds a primary key constraint using AST node
-func writeAddPrimaryKeyFromAST(out *strings.Builder, schema, table string, constraintAST pgparser.ITableconstraintContext) error {
- if constraintAST == nil {
- return errors.New("constraint AST node is nil")
- }
-
- constraintName := extractConstraintNameFromAST(constraintAST)
-
- // Extract PRIMARY KEY definition
- if constraintAST.Constraintelem() != nil {
- elem := constraintAST.Constraintelem()
- if elem.PRIMARY() != nil && elem.KEY() != nil {
- // Generate constraint name if not provided
- if constraintName == "" || constraintName == "primary_key_constraint" {
- constraintName = fmt.Sprintf("%s_pkey", table)
- }
-
- _, _ = out.WriteString(`ALTER TABLE "`)
- _, _ = out.WriteString(schema)
- _, _ = out.WriteString(`"."`)
- _, _ = out.WriteString(table)
- _, _ = out.WriteString(`" ADD CONSTRAINT "`)
- _, _ = out.WriteString(constraintName)
- _, _ = out.WriteString(`" `)
-
- // Extract the PRIMARY KEY part
- pkText := getTextFromAST(elem)
- _, _ = out.WriteString(pkText)
-
- _, _ = out.WriteString(`;`)
- _, _ = out.WriteString("\n")
- return nil
- }
- }
-
- return errors.New("could not extract PRIMARY KEY constraint from AST node")
-}
-
-// Index helper functions for AST-only mode
-
-// extractIndexNameFromAST extracts index name from standalone CREATE INDEX AST node
-func extractIndexNameFromAST(indexAST *pgparser.IndexstmtContext) string {
- if indexAST == nil {
- return ""
- }
-
- if indexAST.Name() != nil {
- return pgpluginparser.NormalizePostgreSQLName(indexAST.Name())
- }
-
- return ""
-}
-
-// writeDropIndexFromAST drops an index using AST node
-func writeDropIndexFromAST(out *strings.Builder, schema string, indexAST *pgparser.IndexstmtContext) {
- indexName := extractIndexNameFromAST(indexAST)
- if indexName == "" {
- // If we can't extract a name, use a fallback
- indexName = "unknown_index"
- }
- writeDropIndex(out, schema, indexName)
-}
-
-// writeCreateIndexFromAST creates an index using AST node
-func writeCreateIndexFromAST(out *strings.Builder, indexAST *pgparser.IndexstmtContext) error {
- if indexAST == nil {
- return errors.New("index AST node is nil")
- }
-
- // Extract the original SQL text from the AST node
- var indexSQL string
-
- // Try to get text using token stream first
- if tokenStream := indexAST.GetParser().GetTokenStream(); tokenStream != nil {
- start := indexAST.GetStart()
- stop := indexAST.GetStop()
- if start != nil && stop != nil {
- indexSQL = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- // Fallback to GetText() if token stream approach failed
- if indexSQL == "" {
- indexSQL = indexAST.GetText()
- }
-
- if indexSQL == "" {
- return errors.New("could not extract index SQL from AST node")
- }
-
- // Write the index SQL
- _, _ = out.WriteString(indexSQL)
- if !strings.HasSuffix(strings.TrimSpace(indexSQL), ";") {
- _, _ = out.WriteString(";")
- }
- _, _ = out.WriteString("\n")
-
- return nil
-}
-
-// extractForeignKeysFromAST extracts foreign key constraints from a CREATE TABLE AST node
-// and returns foreign key metadata for dependency graph construction
-func extractForeignKeysFromAST(createStmt *pgparser.CreatestmtContext, defaultSchema string) []*storepb.ForeignKeyMetadata {
- if createStmt == nil || createStmt.Opttableelementlist() == nil {
- return nil
- }
-
- tableElementList := createStmt.Opttableelementlist().Tableelementlist()
- if tableElementList == nil {
- return nil
- }
-
- var foreignKeys []*storepb.ForeignKeyMetadata
-
- for _, element := range tableElementList.AllTableelement() {
- if element.Tableconstraint() == nil {
- continue
- }
-
- constraint := element.Tableconstraint()
- if constraint.Constraintelem() == nil {
- continue
- }
-
- elem := constraint.Constraintelem()
- if elem.FOREIGN() == nil || elem.KEY() == nil {
- continue
- }
-
- // This is a foreign key constraint
- // Extract referenced table from qualified_name using AST and normalize functions
- // Grammar: FOREIGN KEY (...) REFERENCES qualified_name opt_column_list? ...
- qualifiedName := elem.Qualified_name()
- if qualifiedName == nil {
- continue
- }
-
- // Use NormalizePostgreSQLQualifiedName to get schema and table name
- // Returns []string: [table] or [schema, table]
- nameParts := pgpluginparser.NormalizePostgreSQLQualifiedName(qualifiedName)
- if len(nameParts) == 0 {
- continue
- }
-
- var referencedSchema, referencedTable string
- if len(nameParts) == 1 {
- // No schema specified, use default
- referencedSchema = defaultSchema
- referencedTable = nameParts[0]
- } else if len(nameParts) == 2 {
- // Schema and table specified
- referencedSchema = nameParts[0]
- referencedTable = nameParts[1]
- } else {
- // Unexpected format, skip
- continue
- }
-
- if referencedTable != "" {
- fk := &storepb.ForeignKeyMetadata{
- ReferencedSchema: referencedSchema,
- ReferencedTable: referencedTable,
- }
- foreignKeys = append(foreignKeys, fk)
- }
- }
-
- return foreignKeys
-}
-
-// writeCreateTrigger writes a CREATE TRIGGER or CREATE OR REPLACE TRIGGER statement
-func writeCreateTrigger(buf *strings.Builder, triggerDiff *schema.TriggerDiff) error {
- switch triggerDiff.Action {
- case schema.MetadataDiffActionCreate, schema.MetadataDiffActionAlter:
- // For both CREATE and ALTER, use CREATE OR REPLACE TRIGGER
- if triggerDiff.NewASTNode != nil {
- // AST mode: extract SQL from AST node
- triggerStmt, ok := triggerDiff.NewASTNode.(*pgparser.CreatetrigstmtContext)
- if !ok {
- return errors.New("invalid AST node type for trigger")
- }
-
- // Extract SQL from token stream
- var sqlText string
- if tokenStream := triggerStmt.GetParser().GetTokenStream(); tokenStream != nil {
- start := triggerStmt.GetStart()
- stop := triggerStmt.GetStop()
- if start != nil && stop != nil {
- sqlText = tokenStream.GetTextFromTokens(start, stop)
- }
- }
-
- if sqlText == "" {
- sqlText = triggerStmt.GetText()
- }
-
- // Convert to CREATE OR REPLACE for idempotency
- sqlText = convertTriggerToCreateOrReplace(sqlText)
-
- sqlText = strings.TrimSpace(sqlText)
- if !strings.HasSuffix(sqlText, ";") {
- sqlText += ";"
- }
-
- buf.WriteString(sqlText)
- buf.WriteString("\n\n")
-
- return nil
- } else if triggerDiff.NewTrigger != nil {
- // Metadata mode: convert trigger body to CREATE OR REPLACE
- triggerSQL := convertTriggerToCreateOrReplace(triggerDiff.NewTrigger.Body)
- buf.WriteString(triggerSQL)
- if !strings.HasSuffix(strings.TrimSpace(triggerSQL), ";") {
- buf.WriteString(";")
- }
- buf.WriteString("\n\n")
- return nil
- }
-
- return errors.Errorf("trigger %s requires either NewASTNode or NewTrigger", triggerDiff.Action)
-
- default:
- // Ignore other actions like DROP (handled elsewhere)
- }
-
- return nil
-}
-
-// convertTriggerToCreateOrReplace converts CREATE TRIGGER to CREATE OR REPLACE TRIGGER using ANTLR
-func convertTriggerToCreateOrReplace(definition string) string {
- // Parse the SQL statement using ANTLR
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := pgparser.NewPostgreSQLParser(stream)
-
- // Parse the root
- tree := parser.Root()
- if tree == nil {
- // If parsing fails, return original definition
- return definition
- }
-
- // Create a visitor to find and modify CREATE TRIGGER statements
- visitor := &CreateOrReplaceTriggerVisitor{
- tokens: stream,
- rewriter: antlr.NewTokenStreamRewriter(stream),
- definition: definition,
- }
-
- // Visit the tree
- visitor.Visit(tree)
-
- // Get the modified text
- interval := antlr.NewInterval(0, len(definition)-1)
- result := visitor.rewriter.GetText("", interval)
- if result == "" {
- // If rewriting fails, return original definition
- return definition
- }
-
- return result
-}
-
-// CreateOrReplaceTriggerVisitor visits the parse tree and modifies CREATE TRIGGER to CREATE OR REPLACE TRIGGER
-type CreateOrReplaceTriggerVisitor struct {
- *pgparser.BasePostgreSQLParserVisitor
- tokens *antlr.CommonTokenStream
- rewriter *antlr.TokenStreamRewriter
- definition string
-}
-
-// Visit implements the visitor pattern
-func (v *CreateOrReplaceTriggerVisitor) Visit(tree antlr.ParseTree) any {
- switch t := tree.(type) {
- case *pgparser.CreatetrigstmtContext:
- return v.visitCreateTriggerStmt(t)
- default:
- // Continue visiting children
- return v.visitChildren(tree)
- }
-}
-
-// visitChildren visits all children of a node
-func (v *CreateOrReplaceTriggerVisitor) visitChildren(node antlr.ParseTree) any {
- for i := 0; i < node.GetChildCount(); i++ {
- child := node.GetChild(i)
- if parseTree, ok := child.(antlr.ParseTree); ok {
- v.Visit(parseTree)
- }
- }
- return nil
-}
-
-// visitCreateTriggerStmt handles CREATE TRIGGER statements
-func (v *CreateOrReplaceTriggerVisitor) visitCreateTriggerStmt(ctx *pgparser.CreatetrigstmtContext) any {
- if ctx == nil {
- return nil
- }
-
- // Check if "OR REPLACE" already exists
- if v.hasOrReplace(ctx) {
- // Already has "OR REPLACE", no need to modify
- return nil
- }
-
- // Find the CREATE token
- createToken := ctx.GetStart()
- if createToken == nil {
- return nil
- }
-
- // Look for the TRIGGER keyword after CREATE
- triggerToken := v.findTriggerToken(ctx)
- if triggerToken == nil {
- return nil
- }
-
- // Insert "OR REPLACE" between CREATE and TRIGGER
- // We insert it right before the TRIGGER token
- v.rewriter.InsertBefore("", triggerToken.GetTokenIndex(), "OR REPLACE ")
-
- return nil
-}
-
-// findTriggerToken finds the TRIGGER token in the CREATE TRIGGER statement
-func (v *CreateOrReplaceTriggerVisitor) findTriggerToken(ctx *pgparser.CreatetrigstmtContext) antlr.Token {
- // Get all tokens in the context range
- start := ctx.GetStart().GetTokenIndex()
- stop := ctx.GetStop().GetTokenIndex()
-
- for i := start; i <= stop; i++ {
- token := v.tokens.Get(i)
- if token.GetTokenType() == pgparser.PostgreSQLParserTRIGGER {
- return token
- }
- }
-
- return nil
-}
-
-// hasOrReplace checks if the CREATE TRIGGER statement already contains "OR REPLACE"
-func (v *CreateOrReplaceTriggerVisitor) hasOrReplace(ctx *pgparser.CreatetrigstmtContext) bool {
- // Get all tokens in the context range between CREATE and TRIGGER
- start := ctx.GetStart().GetTokenIndex()
- triggerToken := v.findTriggerToken(ctx)
- if triggerToken == nil {
- return false
- }
- stop := triggerToken.GetTokenIndex()
-
- // Look for "OR" followed by "REPLACE" tokens, skipping whitespace
- for i := start; i < stop; i++ {
- token := v.tokens.Get(i)
- if token.GetTokenType() == pgparser.PostgreSQLParserOR {
- // Found OR, now look for REPLACE after it (skipping whitespace)
- for j := i + 1; j < stop; j++ {
- nextToken := v.tokens.Get(j)
- // Skip whitespace tokens (channel 1 is hidden channel)
- if nextToken.GetChannel() == 1 {
- continue
- }
- // Check if the next non-whitespace token is REPLACE
- if nextToken.GetTokenType() == pgparser.PostgreSQLParserREPLACE {
- return true
- }
- // If it's not REPLACE, this OR is not our OR REPLACE pattern
- break
- }
- }
- }
-
- return false
-}
diff --git a/backend/plugin/schema/pg/generate_migration_testcontainer_test.go b/backend/plugin/schema/pg/generate_migration_testcontainer_test.go
deleted file mode 100644
index 04f10c2a8e..0000000000
--- a/backend/plugin/schema/pg/generate_migration_testcontainer_test.go
+++ /dev/null
@@ -1,2341 +0,0 @@
-package pg
-
-import (
- "context"
- "fmt"
- "slices"
- "strings"
- "testing"
- "time"
-
- "github.com/google/go-cmp/cmp"
- "github.com/stretchr/testify/require"
- "google.golang.org/protobuf/testing/protocmp"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-// TestGenerateMigrationWithTestcontainer tests the generate migration function
-// by applying migrations and rollback to verify the schema can be restored.
-//
-//nolint:tparallel
-func TestGenerateMigrationWithTestcontainer(t *testing.T) {
- ctx := context.Background()
-
- // Start shared PostgreSQL container for all subtests
- container := testcontainer.GetTestPgContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
-
- // Test cases with various schema changes
- testCases := []struct {
- name string
- initialSchema string
- migrationDDL string
- description string
- }{
- {
- name: "bytebase_schema",
- initialSchema: ``,
- migrationDDL: `
- CREATE TABLE public.employee (
- emp_no SERIAL NOT NULL,
- birth_date DATE NOT NULL,
- first_name TEXT NOT NULL,
- last_name TEXT NOT NULL,
- gender TEXT NOT NULL CHECK (gender IN('M', 'F')) NOT NULL,
- hire_date DATE NOT NULL,
- PRIMARY KEY (emp_no)
-);
-
-CREATE INDEX idx_employee_hire_date ON public.employee (hire_date);
-
-CREATE TABLE public.department (
- dept_no TEXT NOT NULL,
- dept_name TEXT NOT NULL,
- PRIMARY KEY (dept_no),
- UNIQUE (dept_name)
-);
-
-CREATE TABLE public.dept_manager (
- emp_no INT NOT NULL,
- dept_no TEXT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE NOT NULL,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- FOREIGN KEY (dept_no) REFERENCES department (dept_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, dept_no)
-);
-
-CREATE TABLE public.dept_emp (
- emp_no INT NOT NULL,
- dept_no TEXT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE NOT NULL,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- FOREIGN KEY (dept_no) REFERENCES department (dept_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, dept_no)
-);
-
-CREATE TABLE public.title (
- emp_no INT NOT NULL,
- title TEXT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, title, from_date)
-);
-
-CREATE TABLE public.salary (
- emp_no INT NOT NULL,
- amount INT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE NOT NULL,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, from_date)
-);
-
-CREATE INDEX idx_salary_amount ON public.salary (amount);
-
-CREATE TABLE public.audit (
- id SERIAL PRIMARY KEY,
- operation TEXT NOT NULL,
- query TEXT,
- user_name TEXT NOT NULL,
- changed_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE INDEX idx_audit_operation ON public.audit (operation);
-CREATE INDEX idx_audit_username ON public.audit (user_name);
-CREATE INDEX idx_audit_changed_at ON public.audit (changed_at);
-
-CREATE OR REPLACE FUNCTION public.log_dml_operations() RETURNS TRIGGER AS $$
-BEGIN
- IF (TG_OP = 'INSERT') THEN
- INSERT INTO public.audit (operation, query, user_name)
- VALUES ('INSERT', current_query(), current_user);
- RETURN NEW;
- ELSIF (TG_OP = 'UPDATE') THEN
- INSERT INTO public.audit (operation, query, user_name)
- VALUES ('UPDATE', current_query(), current_user);
- RETURN NEW;
- ELSIF (TG_OP = 'DELETE') THEN
- INSERT INTO public.audit (operation, query, user_name)
- VALUES ('DELETE', current_query(), current_user);
- RETURN OLD;
- END IF;
- RETURN NULL;
-END;
-$$ LANGUAGE plpgsql;
-
--- only log update and delete, otherwise, it will cause too much change.
-CREATE TRIGGER salary_log_trigger
-AFTER UPDATE OR DELETE ON public.salary
-FOR EACH ROW
-EXECUTE FUNCTION public.log_dml_operations();
-
-CREATE OR REPLACE VIEW public.dept_emp_latest_date AS
-SELECT
- emp_no,
- MAX(
- from_date) AS from_date,
- MAX(
- to_date) AS to_date
-FROM
- public.dept_emp
-GROUP BY
- emp_no;
-
--- shows only the current department for each employee
-CREATE OR REPLACE VIEW public.current_dept_emp AS
-SELECT
- l.emp_no,
- dept_no,
- l.from_date,
- l.to_date
-FROM
- public.dept_emp d
- INNER JOIN public.dept_emp_latest_date l ON d.emp_no = l.emp_no
- AND d.from_date = l.from_date
- AND l.to_date = d.to_date;
- `,
- },
- {
- name: "basic_table_operations",
- initialSchema: `
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id)
-);
-
-CREATE INDEX idx_posts_user_id ON posts(user_id);
-`,
- migrationDDL: `
--- Add new column
-ALTER TABLE users ADD COLUMN is_active BOOLEAN DEFAULT true;
-
--- Create new table
-CREATE TABLE comments (
- id SERIAL PRIMARY KEY,
- post_id INTEGER NOT NULL,
- user_id INTEGER NOT NULL,
- content TEXT NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT fk_post FOREIGN KEY (post_id) REFERENCES posts(id),
- CONSTRAINT fk_comment_user FOREIGN KEY (user_id) REFERENCES users(id)
-);
-
--- Add new index
-CREATE INDEX idx_users_email ON users(email);
-
--- Add check constraint
-ALTER TABLE posts ADD CONSTRAINT check_title_length CHECK (length(title) > 0);
-`,
- description: "Basic table operations with columns, constraints, and indexes",
- },
- {
- name: "views_and_functions",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- price DECIMAL(10, 2) NOT NULL,
- stock INTEGER DEFAULT 0
-);
-
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- product_id INTEGER NOT NULL,
- quantity INTEGER NOT NULL,
- total DECIMAL(10, 2),
- CONSTRAINT fk_product FOREIGN KEY (product_id) REFERENCES products(id)
-);
-`,
- migrationDDL: `
--- Create view
-CREATE VIEW product_inventory AS
-SELECT
- p.id,
- p.name,
- p.price,
- p.stock,
- COALESCE(SUM(o.quantity), 0) as total_ordered
-FROM products p
-LEFT JOIN orders o ON p.id = o.product_id
-GROUP BY p.id, p.name, p.price, p.stock;
-
--- Create function
-CREATE OR REPLACE FUNCTION calculate_order_total()
-RETURNS TRIGGER AS $$
-BEGIN
- NEW.total := NEW.quantity * (SELECT price FROM products WHERE id = NEW.product_id);
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-
--- Create trigger
-CREATE TRIGGER update_order_total
-BEFORE INSERT OR UPDATE ON orders
-FOR EACH ROW
-EXECUTE FUNCTION calculate_order_total();
-
--- Create materialized view
-CREATE MATERIALIZED VIEW product_stats AS
-SELECT
- product_id,
- COUNT(*) as order_count,
- SUM(quantity) as total_quantity,
- SUM(total) as total_revenue
-FROM orders
-GROUP BY product_id;
-
--- Create index on materialized view
-CREATE INDEX idx_product_stats_revenue ON product_stats(total_revenue DESC);
-`,
- description: "Views, functions, triggers, and materialized views",
- },
- {
- name: "schema_and_sequences",
- initialSchema: `
-CREATE SCHEMA inventory;
-
-CREATE TABLE inventory.items (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL
-);
-`,
- migrationDDL: `
--- Create new schema
-CREATE SCHEMA sales;
-
--- Create sequence
-CREATE SEQUENCE sales.order_seq START WITH 1000 INCREMENT BY 10;
-
--- Create table using sequence
-CREATE TABLE sales.orders (
- id INTEGER DEFAULT nextval('sales.order_seq') PRIMARY KEY,
- item_id INTEGER NOT NULL,
- quantity INTEGER NOT NULL,
- CONSTRAINT fk_item FOREIGN KEY (item_id) REFERENCES inventory.items(id)
-);
-
--- Add enum type
-CREATE TYPE inventory.item_status AS ENUM ('available', 'out_of_stock', 'discontinued');
-
--- Alter table to use enum
-ALTER TABLE inventory.items ADD COLUMN status inventory.item_status DEFAULT 'available';
-`,
- description: "Schemas, sequences, and custom types",
- },
- {
- name: "complex_dependencies",
- initialSchema: `
-CREATE TABLE departments (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department_id INTEGER,
- CONSTRAINT fk_department FOREIGN KEY (department_id) REFERENCES departments(id)
-);
-`,
- migrationDDL: `
--- Create base view
-CREATE VIEW department_employees AS
-SELECT d.id as dept_id, d.name as dept_name, e.id as emp_id, e.name as emp_name
-FROM departments d
-LEFT JOIN employees e ON d.id = e.department_id;
-
--- Create dependent view
-CREATE VIEW department_summary AS
-SELECT dept_id, dept_name, COUNT(emp_id) as employee_count
-FROM department_employees
-GROUP BY dept_id, dept_name;
-
--- Create function that depends on table
-CREATE OR REPLACE FUNCTION get_department_employees(dept_id INTEGER)
-RETURNS TABLE(employee_id INTEGER, employee_name VARCHAR) AS $$
-BEGIN
- RETURN QUERY
- SELECT id, name
- FROM employees
- WHERE department_id = dept_id;
-END;
-$$ LANGUAGE plpgsql;
-
--- Create another table with foreign key to existing table
-CREATE TABLE projects (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department_id INTEGER NOT NULL,
- lead_employee_id INTEGER,
- CONSTRAINT fk_project_dept FOREIGN KEY (department_id) REFERENCES departments(id),
- CONSTRAINT fk_project_lead FOREIGN KEY (lead_employee_id) REFERENCES employees(id)
-);
-`,
- description: "Complex dependencies between views, functions, and tables",
- },
- {
- name: "drop_indexes_and_constraints",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- category VARCHAR(50),
- price DECIMAL(10, 2),
- supplier_id INTEGER
-);
-
-CREATE INDEX idx_products_category ON products(category);
-CREATE INDEX idx_products_price ON products(price);
-CREATE UNIQUE INDEX idx_products_name ON products(name);
-
-ALTER TABLE products ADD CONSTRAINT check_price_positive CHECK (price > 0);
-ALTER TABLE products ADD CONSTRAINT check_name_length CHECK (length(name) >= 3);
-`,
- migrationDDL: `
--- Drop indexes and constraints
-DROP INDEX idx_products_category;
-DROP INDEX idx_products_price;
-DROP INDEX idx_products_name;
-ALTER TABLE products DROP CONSTRAINT check_price_positive;
-ALTER TABLE products DROP CONSTRAINT check_name_length;
-`,
- description: "Drop indexes and constraints from tables",
- },
- {
- name: "drop_views_and_functions",
- initialSchema: `
-CREATE TABLE sales (
- id SERIAL PRIMARY KEY,
- product_name VARCHAR(100) NOT NULL,
- sale_amount DECIMAL(10, 2) NOT NULL,
- sale_date DATE NOT NULL
-);
-
-CREATE VIEW monthly_sales AS
-SELECT
- EXTRACT(YEAR FROM sale_date) as year,
- EXTRACT(MONTH FROM sale_date) as month,
- SUM(sale_amount) as total_sales
-FROM sales
-GROUP BY EXTRACT(YEAR FROM sale_date), EXTRACT(MONTH FROM sale_date);
-
-CREATE MATERIALIZED VIEW top_products AS
-SELECT
- product_name,
- COUNT(*) as sale_count,
- SUM(sale_amount) as total_revenue
-FROM sales
-GROUP BY product_name
-ORDER BY total_revenue DESC;
-
-CREATE OR REPLACE FUNCTION get_monthly_total(year_param INTEGER, month_param INTEGER)
-RETURNS DECIMAL AS $$
-BEGIN
- RETURN (
- SELECT COALESCE(SUM(sale_amount), 0)
- FROM sales
- WHERE EXTRACT(YEAR FROM sale_date) = year_param
- AND EXTRACT(MONTH FROM sale_date) = month_param
- );
-END;
-$$ LANGUAGE plpgsql;
-
-CREATE OR REPLACE FUNCTION calculate_discount(amount DECIMAL)
-RETURNS DECIMAL AS $$
-BEGIN
- RETURN amount * 0.1;
-END;
-$$ LANGUAGE plpgsql;
-`,
- migrationDDL: `
--- Drop views and functions
-DROP MATERIALIZED VIEW top_products;
-DROP VIEW monthly_sales;
-DROP FUNCTION get_monthly_total(INTEGER, INTEGER);
-DROP FUNCTION calculate_discount(DECIMAL);
-`,
- description: "Drop views and functions that depend on tables",
- },
- {
- name: "alter_table_columns",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(50) NOT NULL,
- price DECIMAL(8, 2) NOT NULL,
- description TEXT,
- category VARCHAR(30),
- is_active BOOLEAN DEFAULT true
-);
-
-CREATE INDEX idx_products_category ON products(category);
-CREATE INDEX idx_products_price ON products(price);
-`,
- migrationDDL: `
--- Alter table operations
-ALTER TABLE products
- ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- ADD COLUMN stock_quantity INTEGER DEFAULT 0,
- ADD COLUMN weight DECIMAL(5, 2);
-
--- Change column types and constraints
-ALTER TABLE products ALTER COLUMN name TYPE VARCHAR(100);
-ALTER TABLE products ALTER COLUMN price TYPE DECIMAL(10, 2);
-ALTER TABLE products ALTER COLUMN description SET NOT NULL;
-ALTER TABLE products ALTER COLUMN category DROP NOT NULL;
-
--- Add constraints
-ALTER TABLE products ADD CONSTRAINT check_price_positive CHECK (price > 0);
-ALTER TABLE products ADD CONSTRAINT check_stock_non_negative CHECK (stock_quantity >= 0);
-
--- Add new index
-CREATE INDEX idx_products_created_at ON products(created_at);
-CREATE UNIQUE INDEX idx_products_name_category ON products(name, category);
-`,
- description: "Alter table with column additions, type changes, and constraints",
- },
- {
- name: "drop_and_recreate_constraints",
- initialSchema: `
-CREATE TABLE authors (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(100) UNIQUE
-);
-
-CREATE TABLE books (
- id SERIAL PRIMARY KEY,
- title VARCHAR(200) NOT NULL,
- author_id INTEGER NOT NULL,
- isbn VARCHAR(20) UNIQUE,
- published_year INTEGER,
- price DECIMAL(8, 2),
- CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(id),
- CONSTRAINT check_year_valid CHECK (published_year >= 1000 AND published_year <= 2100),
- CONSTRAINT check_price_positive CHECK (price > 0)
-);
-
-CREATE INDEX idx_books_author ON books(author_id);
-CREATE INDEX idx_books_year ON books(published_year);
-`,
- migrationDDL: `
--- Drop and recreate foreign key with different options
-ALTER TABLE books DROP CONSTRAINT fk_author;
-ALTER TABLE books ADD CONSTRAINT fk_author_new FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE;
-
--- Drop and modify check constraints
-ALTER TABLE books DROP CONSTRAINT check_year_valid;
-ALTER TABLE books ADD CONSTRAINT check_year_extended CHECK (published_year >= 1000 AND published_year <= 2030);
-
--- Drop unique constraint and recreate as regular index
-ALTER TABLE books DROP CONSTRAINT books_isbn_key;
-CREATE INDEX idx_books_isbn ON books(isbn);
-
--- Add new constraints
-ALTER TABLE books ADD CONSTRAINT check_title_length CHECK (length(title) >= 3);
-`,
- description: "Drop and recreate constraints with different definitions",
- },
- {
- name: "drop_sequence_and_type",
- initialSchema: `
-CREATE SCHEMA accounting;
-
-CREATE TYPE accounting.transaction_status AS ENUM ('pending', 'completed', 'cancelled');
-
-CREATE SEQUENCE accounting.transaction_seq START 1000;
-
-CREATE TABLE accounting.simple_log (
- message TEXT NOT NULL,
- status accounting.transaction_status DEFAULT 'pending'
-);
-`,
- migrationDDL: `
--- Drop schema objects step by step
-DROP TABLE accounting.simple_log;
-DROP TYPE accounting.transaction_status;
-DROP SEQUENCE accounting.transaction_seq;
-DROP SCHEMA accounting;
-`,
- description: "Drop schema with sequences and custom types",
- },
- {
- name: "mixed_operations_complex",
- initialSchema: `
-CREATE TABLE suppliers (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- contact_email VARCHAR(100)
-);
-
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- supplier_id INTEGER,
- price DECIMAL(10, 2),
- CONSTRAINT fk_supplier FOREIGN KEY (supplier_id) REFERENCES suppliers(id)
-);
-`,
- migrationDDL: `
--- Mix of CREATE, ALTER, and DROP operations
-
--- 1. Create new table
-CREATE TABLE categories (
- id SERIAL PRIMARY KEY,
- name VARCHAR(50) NOT NULL UNIQUE,
- description TEXT
-);
-
--- 2. Alter existing table (add column)
-ALTER TABLE products ADD COLUMN category_id INTEGER;
-
--- 3. Create foreign key to new table
-ALTER TABLE products ADD CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(id);
-
--- 4. Create new view
-CREATE VIEW product_summary AS
-SELECT
- p.id,
- p.name as product_name,
- s.name as supplier_name,
- c.name as category_name,
- p.price
-FROM products p
-LEFT JOIN suppliers s ON p.supplier_id = s.id
-LEFT JOIN categories c ON p.category_id = c.id;
-
--- 5. Drop old constraint and recreate with different action
-ALTER TABLE products DROP CONSTRAINT fk_supplier;
-ALTER TABLE products ADD CONSTRAINT fk_supplier_cascade FOREIGN KEY (supplier_id) REFERENCES suppliers(id) ON DELETE SET NULL;
-
--- 6. Add new constraints
-ALTER TABLE products ADD CONSTRAINT check_price_range CHECK (price >= 0 AND price <= 10000);
-ALTER TABLE suppliers ADD CONSTRAINT check_email_format CHECK (contact_email LIKE '%@%');
-
--- 7. Create indexes
-CREATE INDEX idx_products_category ON products(category_id);
-CREATE INDEX idx_products_price_range ON products(price) WHERE price > 100;
-
--- 8. Create function
-CREATE OR REPLACE FUNCTION get_expensive_products(threshold DECIMAL DEFAULT 100)
-RETURNS TABLE(product_name VARCHAR, supplier_name VARCHAR, price DECIMAL) AS $$
-BEGIN
- RETURN QUERY
- SELECT p.name, s.name, p.price
- FROM products p
- JOIN suppliers s ON p.supplier_id = s.id
- WHERE p.price > threshold
- ORDER BY p.price DESC;
-END;
-$$ LANGUAGE plpgsql;
-`,
- description: "Mixed operations: CREATE, ALTER, DROP with complex dependencies",
- },
- {
- name: "create_tables_with_fk",
- initialSchema: `
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE UNIQUE INDEX uk_email ON users(email);
-CREATE INDEX idx_username ON users(username);
-
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- published_at TIMESTAMP,
- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_user_id ON posts(user_id);
-`,
- migrationDDL: `
-DROP TABLE IF EXISTS posts;
-DROP TABLE IF EXISTS users;`,
- description: "Create tables with foreign key constraints",
- },
- {
- name: "multiple_foreign_keys",
- initialSchema: `
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE UNIQUE INDEX uk_email ON users(email);
-CREATE INDEX idx_username ON users(username);
-
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- published_at TIMESTAMP,
- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_user_id ON posts(user_id);
-`,
- migrationDDL: `
--- Add new column
-ALTER TABLE users ADD COLUMN is_active BOOLEAN DEFAULT true;
-
--- Create new table with multiple foreign keys
-CREATE TABLE comments (
- id SERIAL PRIMARY KEY,
- post_id INTEGER NOT NULL,
- user_id INTEGER NOT NULL,
- content TEXT NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT fk_comment_post FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
- CONSTRAINT fk_comment_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_post_user ON comments(post_id, user_id);
-
--- Add new index
-CREATE INDEX idx_email_active ON users(email, is_active);
-
--- Add check constraint
-ALTER TABLE posts ADD CONSTRAINT chk_title_length CHECK (LENGTH(title) > 0);
-`,
- description: "Tables with multiple foreign key constraints",
- },
- {
- name: "drop_and_recreate_fk_constraints",
- initialSchema: `
-CREATE TABLE authors (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(100)
-);
-
-CREATE UNIQUE INDEX uk_email ON authors(email);
-
-CREATE TABLE books (
- id SERIAL PRIMARY KEY,
- title VARCHAR(200) NOT NULL,
- author_id INTEGER NOT NULL,
- isbn VARCHAR(20),
- published_year INTEGER,
- price DECIMAL(8, 2),
- CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(id),
- CONSTRAINT chk_year_valid CHECK (published_year >= 1000 AND published_year <= 2100),
- CONSTRAINT chk_price_positive CHECK (price > 0)
-);
-
-CREATE UNIQUE INDEX uk_isbn ON books(isbn);
-CREATE INDEX idx_author ON books(author_id);
-CREATE INDEX idx_year ON books(published_year);
-`,
- migrationDDL: `
--- Drop and recreate foreign key with different options
-ALTER TABLE books DROP CONSTRAINT fk_author;
-ALTER TABLE books ADD CONSTRAINT fk_author_new FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE CASCADE;
-
--- Drop and modify check constraints
-ALTER TABLE books DROP CONSTRAINT chk_year_valid;
-ALTER TABLE books ADD CONSTRAINT chk_year_extended CHECK (published_year >= 1000 AND published_year <= 2030);
-
--- Add new constraints
-ALTER TABLE books ADD CONSTRAINT chk_title_length CHECK (LENGTH(title) >= 3);
-`,
- description: "Drop and recreate foreign key constraints with different options",
- },
- {
- name: "self_referencing_foreign_keys",
- initialSchema: `
-CREATE TABLE departments (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- manager_id INTEGER
-);
-
-CREATE TABLE employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department_id INTEGER,
- salary DECIMAL(10, 2),
- hire_date DATE,
- CONSTRAINT fk_dept FOREIGN KEY (department_id) REFERENCES departments(id)
-);
-
-CREATE INDEX idx_dept ON employees(department_id);
-
--- Add self-referencing foreign key
-ALTER TABLE departments ADD CONSTRAINT fk_manager FOREIGN KEY (manager_id) REFERENCES employees(id);
-`,
- migrationDDL: `
--- Create base view
-CREATE VIEW dept_employee_count AS
-SELECT d.id AS dept_id, d.name AS dept_name, COUNT(e.id) AS emp_count
-FROM departments d
-LEFT JOIN employees e ON d.id = e.department_id
-GROUP BY d.id, d.name;
-
--- Create dependent view
-CREATE VIEW dept_summary AS
-SELECT
- dept_id,
- dept_name,
- emp_count,
- 0 AS avg_salary,
- 0 AS max_salary,
- 0 AS min_salary
-FROM dept_employee_count;
-
--- Create highly dependent view
-CREATE VIEW dept_manager_summary AS
-SELECT
- ds.dept_id,
- ds.dept_name,
- ds.emp_count,
- ds.avg_salary,
- m.name AS manager_name
-FROM dept_summary ds
-JOIN departments d ON ds.dept_id = d.id
-LEFT JOIN employees m ON d.manager_id = m.id;
-
--- Create function using views
-CREATE OR REPLACE FUNCTION get_department_report(dept_name_pattern VARCHAR)
-RETURNS TABLE(dept_id INTEGER, dept_name VARCHAR, emp_count BIGINT, avg_salary INTEGER, manager_name VARCHAR) AS $$
-BEGIN
- RETURN QUERY
- SELECT * FROM dept_manager_summary
- WHERE dept_name LIKE '%' || dept_name_pattern || '%';
-END;
-$$ LANGUAGE plpgsql;
-`,
- description: "Self-referencing foreign keys and complex view dependencies",
- },
- {
- name: "circular_foreign_key_dependencies",
- initialSchema: `
-CREATE TABLE customers (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- preferred_order_id INTEGER
-);
-
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- customer_id INTEGER NOT NULL,
- order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- total_amount DECIMAL(10, 2)
-);
-`,
- migrationDDL: `
--- Create circular foreign key dependencies
-ALTER TABLE customers ADD CONSTRAINT fk_preferred_order FOREIGN KEY (preferred_order_id) REFERENCES orders(id) ON DELETE SET NULL;
-ALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE;
-
--- Add more tables with complex relationships
-CREATE TABLE order_items (
- id SERIAL PRIMARY KEY,
- order_id INTEGER NOT NULL,
- product_name VARCHAR(100) NOT NULL,
- quantity INTEGER NOT NULL,
- unit_price DECIMAL(10, 2) NOT NULL,
- CONSTRAINT fk_order_item FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_order ON order_items(order_id);
-
--- Create trigger to update order total
-CREATE OR REPLACE FUNCTION update_order_total()
-RETURNS TRIGGER AS $$
-BEGIN
- UPDATE orders
- SET total_amount = (
- SELECT SUM(quantity * unit_price)
- FROM order_items
- WHERE order_id = NEW.order_id
- )
- WHERE id = NEW.order_id;
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-
-CREATE TRIGGER trg_update_order_total
-AFTER INSERT OR UPDATE ON order_items
-FOR EACH ROW
-EXECUTE FUNCTION update_order_total();
-`,
- description: "Circular foreign key dependencies and triggers",
- },
- {
- name: "table_and_column_comments",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- price DECIMAL(10, 2) NOT NULL,
- category VARCHAR(50),
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE customers (
- id SERIAL PRIMARY KEY,
- first_name VARCHAR(50) NOT NULL,
- last_name VARCHAR(50) NOT NULL,
- email VARCHAR(100) UNIQUE,
- phone VARCHAR(20)
-);`,
- migrationDDL: `
--- Add comments to tables
-COMMENT ON TABLE products IS 'Product catalog with pricing information';
-COMMENT ON TABLE customers IS 'Customer master data';
-
--- Add comments to columns
-COMMENT ON COLUMN products.id IS 'Unique product identifier';
-COMMENT ON COLUMN products.name IS 'Product display name';
-COMMENT ON COLUMN products.price IS 'Product price in USD';
-COMMENT ON COLUMN products.category IS 'Product category classification';
-COMMENT ON COLUMN products.created_at IS 'Record creation timestamp';
-
-COMMENT ON COLUMN customers.id IS 'Unique customer identifier';
-COMMENT ON COLUMN customers.first_name IS 'Customer first name';
-COMMENT ON COLUMN customers.last_name IS 'Customer last name';
-COMMENT ON COLUMN customers.email IS 'Customer email address';
-COMMENT ON COLUMN customers.phone IS 'Customer contact phone number';
-
--- Create table with comments from the start
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- customer_id INTEGER NOT NULL,
- product_id INTEGER NOT NULL,
- quantity INTEGER DEFAULT 1,
- order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT fk_order_customer FOREIGN KEY (customer_id) REFERENCES customers(id),
- CONSTRAINT fk_order_product FOREIGN KEY (product_id) REFERENCES products(id)
-);
-
-COMMENT ON TABLE orders IS 'Customer purchase orders';
-COMMENT ON COLUMN orders.id IS 'Unique order identifier';
-COMMENT ON COLUMN orders.customer_id IS 'Reference to customer who placed the order';
-COMMENT ON COLUMN orders.product_id IS 'Reference to ordered product';
-COMMENT ON COLUMN orders.quantity IS 'Number of items ordered';
-COMMENT ON COLUMN orders.order_date IS 'Date and time when order was placed';`,
- description: "Adding comments to tables and columns",
- },
- {
- name: "modify_and_drop_comments",
- initialSchema: `
-CREATE TABLE inventory (
- id SERIAL PRIMARY KEY,
- product_code VARCHAR(20) NOT NULL UNIQUE,
- stock_level INTEGER DEFAULT 0,
- warehouse_location VARCHAR(100),
- last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
--- Add initial comments
-COMMENT ON TABLE inventory IS 'Initial inventory tracking table';
-COMMENT ON COLUMN inventory.id IS 'Primary key for inventory records';
-COMMENT ON COLUMN inventory.product_code IS 'Unique product identifier code';
-COMMENT ON COLUMN inventory.stock_level IS 'Current stock quantity';
-COMMENT ON COLUMN inventory.warehouse_location IS 'Physical location in warehouse';
-COMMENT ON COLUMN inventory.last_updated IS 'Last modification timestamp';`,
- migrationDDL: `
--- Modify existing comments
-COMMENT ON TABLE inventory IS 'Comprehensive inventory management system';
-COMMENT ON COLUMN inventory.product_code IS 'SKU - Stock Keeping Unit identifier';
-COMMENT ON COLUMN inventory.stock_level IS 'Available quantity for sale';
-
--- Remove some comments
-COMMENT ON COLUMN inventory.warehouse_location IS NULL;
-COMMENT ON COLUMN inventory.last_updated IS NULL;
-
--- Add new column with comment
-ALTER TABLE inventory ADD COLUMN reorder_point INTEGER DEFAULT 10;
-COMMENT ON COLUMN inventory.reorder_point IS 'Minimum stock level before reordering';
-
--- Create view with comment
-CREATE VIEW low_stock_items AS
-SELECT product_code, stock_level, reorder_point
-FROM inventory
-WHERE stock_level <= reorder_point;
-
-COMMENT ON VIEW low_stock_items IS 'Products that need to be restocked';`,
- description: "Modifying and dropping comments on existing objects",
- },
- {
- name: "comments_with_special_characters",
- initialSchema: `
-CREATE TABLE test_table (
- id SERIAL PRIMARY KEY,
- data VARCHAR(255),
- status VARCHAR(20) DEFAULT 'active'
-);`,
- migrationDDL: `
--- Test comments with special characters and escaping
-COMMENT ON TABLE test_table IS 'Test table with "quotes" and ''apostrophes'' and $special$ characters';
-COMMENT ON COLUMN test_table.id IS 'ID with symbols: @#$%^&*()_+-={}|[]\\:";''<>?,./';
-COMMENT ON COLUMN test_table.data IS 'Data field containing
-multiline
-text with various symbols: ñáéíóú àèìòù äëïöü';
-COMMENT ON COLUMN test_table.status IS 'Status: active/inactive (default: active)';
-
--- Create function with comment
-CREATE OR REPLACE FUNCTION get_active_records()
-RETURNS TABLE(id INTEGER, data VARCHAR) AS $$
-BEGIN
- RETURN QUERY
- SELECT t.id, t.data
- FROM test_table t
- WHERE t.status = 'active';
-END;
-$$ LANGUAGE plpgsql;
-
-COMMENT ON FUNCTION get_active_records() IS 'Returns all active records from test_table';`,
- description: "Comments with special characters, quotes, and multiline text",
- },
- // Reverse test cases
- {
- name: "reverse_basic_table_operations",
- initialSchema: `
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- is_active BOOLEAN DEFAULT true
-);
-
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id),
- CONSTRAINT check_title_length CHECK (length(title) > 0)
-);
-
-CREATE TABLE comments (
- id SERIAL PRIMARY KEY,
- post_id INTEGER NOT NULL,
- user_id INTEGER NOT NULL,
- content TEXT NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT fk_post FOREIGN KEY (post_id) REFERENCES posts(id),
- CONSTRAINT fk_comment_user FOREIGN KEY (user_id) REFERENCES users(id)
-);
-
-CREATE INDEX idx_posts_user_id ON posts(user_id);
-CREATE INDEX idx_users_email ON users(email);
-`,
- migrationDDL: `
--- Drop index
-DROP INDEX idx_users_email;
-
--- Drop check constraint
-ALTER TABLE posts DROP CONSTRAINT check_title_length;
-
--- Drop table
-DROP TABLE comments;
-
--- Drop column
-ALTER TABLE users DROP COLUMN is_active;
-`,
- description: "Reverse of basic_table_operations: DROP column, table, index, constraint",
- },
- {
- name: "reverse_views_and_functions",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- price DECIMAL(10, 2) NOT NULL,
- stock INTEGER DEFAULT 0
-);
-
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- product_id INTEGER NOT NULL,
- quantity INTEGER NOT NULL,
- total DECIMAL(10, 2),
- CONSTRAINT fk_product FOREIGN KEY (product_id) REFERENCES products(id)
-);
-
-CREATE VIEW product_inventory AS
-SELECT
- p.id,
- p.name,
- p.price,
- p.stock,
- COALESCE(SUM(o.quantity), 0) as total_ordered
-FROM products p
-LEFT JOIN orders o ON p.id = o.product_id
-GROUP BY p.id, p.name, p.price, p.stock;
-
-CREATE OR REPLACE FUNCTION calculate_order_total()
-RETURNS TRIGGER AS $$
-BEGIN
- NEW.total := NEW.quantity * (SELECT price FROM products WHERE id = NEW.product_id);
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-
-CREATE TRIGGER update_order_total
-BEFORE INSERT OR UPDATE ON orders
-FOR EACH ROW
-EXECUTE FUNCTION calculate_order_total();
-
-CREATE MATERIALIZED VIEW product_stats AS
-SELECT
- product_id,
- COUNT(*) as order_count,
- SUM(quantity) as total_quantity,
- SUM(total) as total_revenue
-FROM orders
-GROUP BY product_id;
-
-CREATE INDEX idx_product_stats_revenue ON product_stats(total_revenue DESC);
-`,
- migrationDDL: `
--- Drop index on materialized view
-DROP INDEX idx_product_stats_revenue;
-
--- Drop materialized view
-DROP MATERIALIZED VIEW product_stats;
-
--- Drop trigger
-DROP TRIGGER update_order_total ON orders;
-
--- Drop function
-DROP FUNCTION calculate_order_total();
-
--- Drop view
-DROP VIEW product_inventory;
-`,
- description: "Reverse of views_and_functions: DROP view, function, trigger, materialized view",
- },
- {
- name: "reverse_schema_and_sequences",
- initialSchema: `
-CREATE SCHEMA inventory;
-CREATE SCHEMA sales;
-
-CREATE SEQUENCE sales.order_seq START WITH 1000 INCREMENT BY 10;
-
-CREATE TYPE inventory.item_status AS ENUM ('available', 'out_of_stock', 'discontinued');
-
-CREATE TABLE inventory.items (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- status inventory.item_status DEFAULT 'available'
-);
-
-CREATE TABLE sales.orders (
- id INTEGER DEFAULT nextval('sales.order_seq') PRIMARY KEY,
- item_id INTEGER NOT NULL,
- quantity INTEGER NOT NULL,
- CONSTRAINT fk_item FOREIGN KEY (item_id) REFERENCES inventory.items(id)
-);
-`,
- migrationDDL: `
--- Drop table in sales schema
-DROP TABLE sales.orders;
-
--- Drop sequence
-DROP SEQUENCE sales.order_seq;
-
--- Drop schema
-DROP SCHEMA sales;
-
--- Drop column that uses enum
-ALTER TABLE inventory.items DROP COLUMN status;
-
--- Drop enum type
-DROP TYPE inventory.item_status;
-`,
- description: "Reverse of schema_and_sequences: DROP schema, sequence, table, type",
- },
- {
- name: "reverse_complex_dependencies",
- initialSchema: `
-CREATE TABLE departments (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department_id INTEGER,
- CONSTRAINT fk_department FOREIGN KEY (department_id) REFERENCES departments(id)
-);
-
-CREATE TABLE projects (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department_id INTEGER NOT NULL,
- lead_employee_id INTEGER,
- CONSTRAINT fk_project_dept FOREIGN KEY (department_id) REFERENCES departments(id),
- CONSTRAINT fk_project_lead FOREIGN KEY (lead_employee_id) REFERENCES employees(id)
-);
-
-CREATE VIEW department_employees AS
-SELECT d.id as dept_id, d.name as dept_name, e.id as emp_id, e.name as emp_name
-FROM departments d
-LEFT JOIN employees e ON d.id = e.department_id;
-
-CREATE VIEW department_summary AS
-SELECT dept_id, dept_name, COUNT(emp_id) as employee_count
-FROM department_employees
-GROUP BY dept_id, dept_name;
-
-CREATE OR REPLACE FUNCTION get_department_employees(dept_id INTEGER)
-RETURNS TABLE(employee_id INTEGER, employee_name VARCHAR) AS $$
-BEGIN
- RETURN QUERY
- SELECT id, name
- FROM employees
- WHERE department_id = dept_id;
-END;
-$$ LANGUAGE plpgsql;
-`,
- migrationDDL: `
--- Drop table with foreign keys
-DROP TABLE projects;
-
--- Drop function
-DROP FUNCTION get_department_employees(INTEGER);
-
--- Drop dependent view first
-DROP VIEW department_summary;
-
--- Drop base view
-DROP VIEW department_employees;
-`,
- description: "Reverse of complex_dependencies: DROP views, functions, tables with dependencies",
- },
- {
- name: "reverse_drop_indexes_and_constraints",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- category VARCHAR(50),
- price DECIMAL(10, 2),
- supplier_id INTEGER
-);
-`,
- migrationDDL: `
--- Create indexes and constraints
-CREATE INDEX idx_products_category ON products(category);
-CREATE INDEX idx_products_price ON products(price);
-CREATE UNIQUE INDEX idx_products_name ON products(name);
-
-ALTER TABLE products ADD CONSTRAINT check_price_positive CHECK (price > 0);
-ALTER TABLE products ADD CONSTRAINT check_name_length CHECK (length(name) >= 3);
-`,
- description: "Reverse of drop_indexes_and_constraints: CREATE indexes and constraints",
- },
- {
- name: "reverse_drop_views_and_functions",
- initialSchema: `
-CREATE TABLE sales (
- id SERIAL PRIMARY KEY,
- product_name VARCHAR(100) NOT NULL,
- sale_amount DECIMAL(10, 2) NOT NULL,
- sale_date DATE NOT NULL
-);
-`,
- migrationDDL: `
--- Create views and functions
-CREATE VIEW monthly_sales AS
-SELECT
- EXTRACT(YEAR FROM sale_date) as year,
- EXTRACT(MONTH FROM sale_date) as month,
- SUM(sale_amount) as total_sales
-FROM sales
-GROUP BY EXTRACT(YEAR FROM sale_date), EXTRACT(MONTH FROM sale_date);
-
-CREATE MATERIALIZED VIEW top_products AS
-SELECT
- product_name,
- COUNT(*) as sale_count,
- SUM(sale_amount) as total_revenue
-FROM sales
-GROUP BY product_name
-ORDER BY total_revenue DESC;
-
-CREATE OR REPLACE FUNCTION get_monthly_total(year_param INTEGER, month_param INTEGER)
-RETURNS DECIMAL AS $$
-BEGIN
- RETURN (
- SELECT COALESCE(SUM(sale_amount), 0)
- FROM sales
- WHERE EXTRACT(YEAR FROM sale_date) = year_param
- AND EXTRACT(MONTH FROM sale_date) = month_param
- );
-END;
-$$ LANGUAGE plpgsql;
-
-CREATE OR REPLACE FUNCTION calculate_discount(amount DECIMAL)
-RETURNS DECIMAL AS $$
-BEGIN
- RETURN amount * 0.1;
-END;
-$$ LANGUAGE plpgsql;
-`,
- description: "Reverse of drop_views_and_functions: CREATE views and functions",
- },
- {
- name: "reverse_alter_table_columns",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- price DECIMAL(10, 2) NOT NULL,
- description TEXT NOT NULL,
- category VARCHAR(30),
- is_active BOOLEAN DEFAULT true,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- stock_quantity INTEGER DEFAULT 0,
- weight DECIMAL(5, 2),
- CONSTRAINT check_price_positive CHECK (price > 0),
- CONSTRAINT check_stock_non_negative CHECK (stock_quantity >= 0)
-);
-
-CREATE INDEX idx_products_category ON products(category);
-CREATE INDEX idx_products_price ON products(price);
-CREATE INDEX idx_products_created_at ON products(created_at);
-CREATE UNIQUE INDEX idx_products_name_category ON products(name, category);
-`,
- migrationDDL: `
--- Drop indexes
-DROP INDEX idx_products_created_at;
-DROP INDEX idx_products_name_category;
-
--- Drop constraints
-ALTER TABLE products DROP CONSTRAINT check_stock_non_negative;
-ALTER TABLE products DROP CONSTRAINT check_price_positive;
-
--- Revert column changes
-ALTER TABLE products ALTER COLUMN category SET NOT NULL;
-ALTER TABLE products ALTER COLUMN description DROP NOT NULL;
-ALTER TABLE products ALTER COLUMN price TYPE DECIMAL(8, 2);
-ALTER TABLE products ALTER COLUMN name TYPE VARCHAR(50);
-
--- Drop columns
-ALTER TABLE products
- DROP COLUMN weight,
- DROP COLUMN stock_quantity,
- DROP COLUMN created_at;
-`,
- description: "Reverse of alter_table_columns: DROP columns, constraints, indexes",
- },
- {
- name: "reverse_drop_and_recreate_constraints",
- initialSchema: `
-CREATE TABLE authors (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(100) UNIQUE
-);
-
-CREATE TABLE books (
- id SERIAL PRIMARY KEY,
- title VARCHAR(200) NOT NULL,
- author_id INTEGER NOT NULL,
- isbn VARCHAR(20),
- published_year INTEGER,
- price DECIMAL(8, 2),
- CONSTRAINT fk_author_new FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE,
- CONSTRAINT check_year_extended CHECK (published_year >= 1000 AND published_year <= 2030),
- CONSTRAINT check_title_length CHECK (length(title) >= 3)
-);
-
-CREATE INDEX idx_books_author ON books(author_id);
-CREATE INDEX idx_books_year ON books(published_year);
-CREATE INDEX idx_books_isbn ON books(isbn);
-`,
- migrationDDL: `
--- Drop new constraints
-ALTER TABLE books DROP CONSTRAINT check_title_length;
-
--- Drop index and recreate unique constraint
-DROP INDEX idx_books_isbn;
-ALTER TABLE books ADD CONSTRAINT books_isbn_key UNIQUE (isbn);
-
--- Drop and recreate check constraints with original definitions
-ALTER TABLE books DROP CONSTRAINT check_year_extended;
-ALTER TABLE books ADD CONSTRAINT check_year_valid CHECK (published_year >= 1000 AND published_year <= 2100);
-
--- Drop and recreate foreign key with original options
-ALTER TABLE books DROP CONSTRAINT fk_author_new;
-ALTER TABLE books ADD CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(id);
-
--- Add back original constraint
-ALTER TABLE books ADD CONSTRAINT check_price_positive CHECK (price > 0);
-`,
- description: "Reverse of drop_and_recreate_constraints: Reverse the constraint changes",
- },
- {
- name: "reverse_drop_sequence_and_type",
- initialSchema: ``,
- migrationDDL: `
--- Create schema and objects
-CREATE SCHEMA accounting;
-
-CREATE TYPE accounting.transaction_status AS ENUM ('pending', 'completed', 'cancelled');
-
-CREATE SEQUENCE accounting.transaction_seq START 1000;
-
-CREATE TABLE accounting.simple_log (
- message TEXT NOT NULL,
- status accounting.transaction_status DEFAULT 'pending'
-);
-`,
- description: "Reverse of drop_sequence_and_type: CREATE schema, sequence, type, table",
- },
- {
- name: "reverse_mixed_operations_complex",
- initialSchema: `
-CREATE TABLE suppliers (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- contact_email VARCHAR(100),
- CONSTRAINT check_email_format CHECK (contact_email LIKE '%@%')
-);
-
-CREATE TABLE categories (
- id SERIAL PRIMARY KEY,
- name VARCHAR(50) NOT NULL UNIQUE,
- description TEXT
-);
-
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- supplier_id INTEGER,
- price DECIMAL(10, 2),
- category_id INTEGER,
- CONSTRAINT fk_supplier_cascade FOREIGN KEY (supplier_id) REFERENCES suppliers(id) ON DELETE SET NULL,
- CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(id),
- CONSTRAINT check_price_range CHECK (price >= 0 AND price <= 10000)
-);
-
-CREATE INDEX idx_products_category ON products(category_id);
-CREATE INDEX idx_products_price_range ON products(price) WHERE price > 100;
-
-CREATE VIEW product_summary AS
-SELECT
- p.id,
- p.name as product_name,
- s.name as supplier_name,
- c.name as category_name,
- p.price
-FROM products p
-LEFT JOIN suppliers s ON p.supplier_id = s.id
-LEFT JOIN categories c ON p.category_id = c.id;
-
-CREATE OR REPLACE FUNCTION get_expensive_products(threshold DECIMAL DEFAULT 100)
-RETURNS TABLE(product_name VARCHAR, supplier_name VARCHAR, price DECIMAL) AS $$
-BEGIN
- RETURN QUERY
- SELECT p.name, s.name, p.price
- FROM products p
- JOIN suppliers s ON p.supplier_id = s.id
- WHERE p.price > threshold
- ORDER BY p.price DESC;
-END;
-$$ LANGUAGE plpgsql;
-`,
- migrationDDL: `
--- Drop function
-DROP FUNCTION get_expensive_products(DECIMAL);
-
--- Drop indexes
-DROP INDEX idx_products_price_range;
-DROP INDEX idx_products_category;
-
--- Drop constraints
-ALTER TABLE suppliers DROP CONSTRAINT check_email_format;
-ALTER TABLE products DROP CONSTRAINT check_price_range;
-
--- Drop and recreate foreign key with original action
-ALTER TABLE products DROP CONSTRAINT fk_supplier_cascade;
-ALTER TABLE products ADD CONSTRAINT fk_supplier FOREIGN KEY (supplier_id) REFERENCES suppliers(id);
-
--- Drop view
-DROP VIEW product_summary;
-
--- Drop foreign key to categories
-ALTER TABLE products DROP CONSTRAINT fk_category;
-
--- Drop column
-ALTER TABLE products DROP COLUMN category_id;
-
--- Drop table
-DROP TABLE categories;
-`,
- description: "Reverse of mixed_operations_complex: Reverse all mixed operations",
- },
- {
- name: "reverse_create_tables_with_fk",
- initialSchema: ``,
- migrationDDL: `
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE UNIQUE INDEX uk_email ON users(email);
-CREATE INDEX idx_username ON users(username);
-
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- published_at TIMESTAMP,
- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_user_id ON posts(user_id);
-`,
- description: "Reverse of create_tables_with_fk: CREATE tables with foreign keys",
- },
- {
- name: "reverse_multiple_foreign_keys",
- initialSchema: `
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- is_active BOOLEAN DEFAULT true
-);
-
-CREATE UNIQUE INDEX uk_email ON users(email);
-CREATE INDEX idx_username ON users(username);
-CREATE INDEX idx_email_active ON users(email, is_active);
-
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- published_at TIMESTAMP,
- CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
- CONSTRAINT chk_title_length CHECK (LENGTH(title) > 0)
-);
-
-CREATE INDEX idx_user_id ON posts(user_id);
-
-CREATE TABLE comments (
- id SERIAL PRIMARY KEY,
- post_id INTEGER NOT NULL,
- user_id INTEGER NOT NULL,
- content TEXT NOT NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT fk_comment_post FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
- CONSTRAINT fk_comment_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_post_user ON comments(post_id, user_id);
-`,
- migrationDDL: `
--- Drop check constraint
-ALTER TABLE posts DROP CONSTRAINT chk_title_length;
-
--- Drop index
-DROP INDEX idx_email_active;
-
--- Drop table with multiple foreign keys
-DROP TABLE comments;
-
--- Drop column
-ALTER TABLE users DROP COLUMN is_active;
-`,
- description: "Reverse of multiple_foreign_keys: DROP column, table, indexes, constraints",
- },
- {
- name: "reverse_drop_and_recreate_fk_constraints",
- initialSchema: `
-CREATE TABLE authors (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- email VARCHAR(100)
-);
-
-CREATE UNIQUE INDEX uk_email ON authors(email);
-
-CREATE TABLE books (
- id SERIAL PRIMARY KEY,
- title VARCHAR(200) NOT NULL,
- author_id INTEGER NOT NULL,
- isbn VARCHAR(20),
- published_year INTEGER,
- price DECIMAL(8, 2),
- CONSTRAINT fk_author_new FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE CASCADE,
- CONSTRAINT chk_year_extended CHECK (published_year >= 1000 AND published_year <= 2030),
- CONSTRAINT chk_price_positive CHECK (price > 0),
- CONSTRAINT chk_title_length CHECK (LENGTH(title) >= 3)
-);
-
-CREATE UNIQUE INDEX uk_isbn ON books(isbn);
-CREATE INDEX idx_author ON books(author_id);
-CREATE INDEX idx_year ON books(published_year);
-`,
- migrationDDL: `
--- Drop new constraints
-ALTER TABLE books DROP CONSTRAINT chk_title_length;
-
--- Drop and modify check constraints
-ALTER TABLE books DROP CONSTRAINT chk_year_extended;
-ALTER TABLE books ADD CONSTRAINT chk_year_valid CHECK (published_year >= 1000 AND published_year <= 2100);
-
--- Drop and recreate foreign key with different options
-ALTER TABLE books DROP CONSTRAINT fk_author_new;
-ALTER TABLE books ADD CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(id);
-`,
- description: "Reverse of drop_and_recreate_fk_constraints: Reverse FK constraint changes",
- },
- {
- name: "reverse_self_referencing_foreign_keys",
- initialSchema: `
-CREATE TABLE departments (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- manager_id INTEGER
-);
-
-CREATE TABLE employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department_id INTEGER,
- salary DECIMAL(10, 2),
- hire_date DATE,
- CONSTRAINT fk_dept FOREIGN KEY (department_id) REFERENCES departments(id)
-);
-
-CREATE INDEX idx_dept ON employees(department_id);
-
-ALTER TABLE departments ADD CONSTRAINT fk_manager FOREIGN KEY (manager_id) REFERENCES employees(id);
-
-CREATE VIEW dept_employee_count AS
-SELECT d.id AS dept_id, d.name AS dept_name, COUNT(e.id) AS emp_count
-FROM departments d
-LEFT JOIN employees e ON d.id = e.department_id
-GROUP BY d.id, d.name;
-
-CREATE VIEW dept_summary AS
-SELECT
- dept_id,
- dept_name,
- emp_count,
- 0 AS avg_salary,
- 0 AS max_salary,
- 0 AS min_salary
-FROM dept_employee_count;
-
-CREATE VIEW dept_manager_summary AS
-SELECT
- ds.dept_id,
- ds.dept_name,
- ds.emp_count,
- ds.avg_salary,
- m.name AS manager_name
-FROM dept_summary ds
-JOIN departments d ON ds.dept_id = d.id
-LEFT JOIN employees m ON d.manager_id = m.id;
-
-CREATE OR REPLACE FUNCTION get_department_report(dept_name_pattern VARCHAR)
-RETURNS TABLE(dept_id INTEGER, dept_name VARCHAR, emp_count BIGINT, avg_salary INTEGER, manager_name VARCHAR) AS $$
-BEGIN
- RETURN QUERY
- SELECT * FROM dept_manager_summary
- WHERE dept_name LIKE '%' || dept_name_pattern || '%';
-END;
-$$ LANGUAGE plpgsql;
-`,
- migrationDDL: `
--- Drop function using views
-DROP FUNCTION get_department_report(VARCHAR);
-
--- Drop highly dependent view
-DROP VIEW dept_manager_summary;
-
--- Drop dependent view
-DROP VIEW dept_summary;
-
--- Drop base view
-DROP VIEW dept_employee_count;
-`,
- description: "Reverse of self_referencing_foreign_keys: DROP views and functions with dependencies",
- },
- {
- name: "reverse_table_and_column_comments",
- initialSchema: `
-CREATE TABLE products (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- price DECIMAL(10, 2) NOT NULL,
- category VARCHAR(50),
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE customers (
- id SERIAL PRIMARY KEY,
- first_name VARCHAR(50) NOT NULL,
- last_name VARCHAR(50) NOT NULL,
- email VARCHAR(100) UNIQUE,
- phone VARCHAR(20)
-);
-
-CREATE TABLE orders (
- id SERIAL PRIMARY KEY,
- customer_id INTEGER NOT NULL,
- product_id INTEGER NOT NULL,
- quantity INTEGER DEFAULT 1,
- order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT fk_order_customer FOREIGN KEY (customer_id) REFERENCES customers(id),
- CONSTRAINT fk_order_product FOREIGN KEY (product_id) REFERENCES products(id)
-);
-
-COMMENT ON TABLE products IS 'Product catalog with pricing information';
-COMMENT ON TABLE customers IS 'Customer master data';
-COMMENT ON TABLE orders IS 'Customer purchase orders';
-
-COMMENT ON COLUMN products.id IS 'Unique product identifier';
-COMMENT ON COLUMN products.name IS 'Product display name';
-COMMENT ON COLUMN products.price IS 'Product price in USD';
-COMMENT ON COLUMN products.category IS 'Product category classification';
-COMMENT ON COLUMN products.created_at IS 'Record creation timestamp';
-
-COMMENT ON COLUMN customers.id IS 'Unique customer identifier';
-COMMENT ON COLUMN customers.first_name IS 'Customer first name';
-COMMENT ON COLUMN customers.last_name IS 'Customer last name';
-COMMENT ON COLUMN customers.email IS 'Customer email address';
-COMMENT ON COLUMN customers.phone IS 'Customer contact phone number';
-
-COMMENT ON COLUMN orders.id IS 'Unique order identifier';
-COMMENT ON COLUMN orders.customer_id IS 'Reference to customer who placed the order';
-COMMENT ON COLUMN orders.product_id IS 'Reference to ordered product';
-COMMENT ON COLUMN orders.quantity IS 'Number of items ordered';
-COMMENT ON COLUMN orders.order_date IS 'Date and time when order was placed';
-`,
- migrationDDL: `
--- Drop all column comments
-COMMENT ON COLUMN orders.order_date IS NULL;
-COMMENT ON COLUMN orders.quantity IS NULL;
-COMMENT ON COLUMN orders.product_id IS NULL;
-COMMENT ON COLUMN orders.customer_id IS NULL;
-COMMENT ON COLUMN orders.id IS NULL;
-
-COMMENT ON COLUMN customers.phone IS NULL;
-COMMENT ON COLUMN customers.email IS NULL;
-COMMENT ON COLUMN customers.last_name IS NULL;
-COMMENT ON COLUMN customers.first_name IS NULL;
-COMMENT ON COLUMN customers.id IS NULL;
-
-COMMENT ON COLUMN products.created_at IS NULL;
-COMMENT ON COLUMN products.category IS NULL;
-COMMENT ON COLUMN products.price IS NULL;
-COMMENT ON COLUMN products.name IS NULL;
-COMMENT ON COLUMN products.id IS NULL;
-
--- Drop table comments
-COMMENT ON TABLE orders IS NULL;
-COMMENT ON TABLE customers IS NULL;
-COMMENT ON TABLE products IS NULL;
-
--- Drop orders table with foreign keys
-DROP TABLE orders;
-`,
- description: "Reverse of table_and_column_comments: DROP comments",
- },
- {
- name: "reverse_modify_and_drop_comments",
- initialSchema: `
-CREATE TABLE inventory (
- id SERIAL PRIMARY KEY,
- product_code VARCHAR(20) NOT NULL UNIQUE,
- stock_level INTEGER DEFAULT 0,
- warehouse_location VARCHAR(100),
- last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- reorder_point INTEGER DEFAULT 10
-);
-
-CREATE VIEW low_stock_items AS
-SELECT product_code, stock_level, reorder_point
-FROM inventory
-WHERE stock_level <= reorder_point;
-
-COMMENT ON TABLE inventory IS 'Comprehensive inventory management system';
-COMMENT ON COLUMN inventory.id IS 'Primary key for inventory records';
-COMMENT ON COLUMN inventory.product_code IS 'SKU - Stock Keeping Unit identifier';
-COMMENT ON COLUMN inventory.stock_level IS 'Available quantity for sale';
-COMMENT ON COLUMN inventory.reorder_point IS 'Minimum stock level before reordering';
-COMMENT ON VIEW low_stock_items IS 'Products that need to be restocked';
-`,
- migrationDDL: `
--- Drop view comment
-COMMENT ON VIEW low_stock_items IS NULL;
-
--- Drop view
-DROP VIEW low_stock_items;
-
--- Drop column with comment
-ALTER TABLE inventory DROP COLUMN reorder_point;
-
--- Add back removed comments
-COMMENT ON COLUMN inventory.warehouse_location IS 'Physical location in warehouse';
-COMMENT ON COLUMN inventory.last_updated IS 'Last modification timestamp';
-
--- Modify existing comments back to original
-COMMENT ON COLUMN inventory.stock_level IS 'Current stock quantity';
-COMMENT ON COLUMN inventory.product_code IS 'Unique product identifier code';
-COMMENT ON TABLE inventory IS 'Initial inventory tracking table';
-`,
- description: "Reverse of modify_and_drop_comments: Reverse comment changes",
- },
- {
- name: "reverse_bytebase_schema",
- initialSchema: `
-CREATE TABLE public.employee (
- emp_no SERIAL NOT NULL,
- birth_date DATE NOT NULL,
- first_name TEXT NOT NULL,
- last_name TEXT NOT NULL,
- gender TEXT NOT NULL CHECK (gender IN('M', 'F')) NOT NULL,
- hire_date DATE NOT NULL,
- PRIMARY KEY (emp_no)
-);
-
-CREATE INDEX idx_employee_hire_date ON public.employee (hire_date);
-
-CREATE TABLE public.department (
- dept_no TEXT NOT NULL,
- dept_name TEXT NOT NULL,
- PRIMARY KEY (dept_no),
- UNIQUE (dept_name)
-);
-
-CREATE TABLE public.dept_manager (
- emp_no INT NOT NULL,
- dept_no TEXT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE NOT NULL,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- FOREIGN KEY (dept_no) REFERENCES department (dept_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, dept_no)
-);
-
-CREATE TABLE public.dept_emp (
- emp_no INT NOT NULL,
- dept_no TEXT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE NOT NULL,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- FOREIGN KEY (dept_no) REFERENCES department (dept_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, dept_no)
-);
-
-CREATE TABLE public.title (
- emp_no INT NOT NULL,
- title TEXT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, title, from_date)
-);
-
-CREATE TABLE public.salary (
- emp_no INT NOT NULL,
- amount INT NOT NULL,
- from_date DATE NOT NULL,
- to_date DATE NOT NULL,
- FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE,
- PRIMARY KEY (emp_no, from_date)
-);
-
-CREATE INDEX idx_salary_amount ON public.salary (amount);
-
-CREATE TABLE public.audit (
- id SERIAL PRIMARY KEY,
- operation TEXT NOT NULL,
- query TEXT,
- user_name TEXT NOT NULL,
- changed_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE INDEX idx_audit_operation ON public.audit (operation);
-CREATE INDEX idx_audit_username ON public.audit (user_name);
-CREATE INDEX idx_audit_changed_at ON public.audit (changed_at);
-
-CREATE OR REPLACE FUNCTION public.log_dml_operations() RETURNS TRIGGER AS $$
-BEGIN
- IF (TG_OP = 'INSERT') THEN
- INSERT INTO public.audit (operation, query, user_name)
- VALUES ('INSERT', current_query(), current_user);
- RETURN NEW;
- ELSIF (TG_OP = 'UPDATE') THEN
- INSERT INTO public.audit (operation, query, user_name)
- VALUES ('UPDATE', current_query(), current_user);
- RETURN NEW;
- ELSIF (TG_OP = 'DELETE') THEN
- INSERT INTO public.audit (operation, query, user_name)
- VALUES ('DELETE', current_query(), current_user);
- RETURN OLD;
- END IF;
- RETURN NULL;
-END;
-$$ LANGUAGE plpgsql;
-
--- only log update and delete, otherwise, it will cause too much change.
-CREATE TRIGGER salary_log_trigger
-AFTER UPDATE OR DELETE ON public.salary
-FOR EACH ROW
-EXECUTE FUNCTION public.log_dml_operations();
-
-CREATE OR REPLACE VIEW public.dept_emp_latest_date AS
-SELECT
- emp_no,
- MAX(
- from_date) AS from_date,
- MAX(
- to_date) AS to_date
-FROM
- public.dept_emp
-GROUP BY
- emp_no;
-
--- shows only the current department for each employee
-CREATE OR REPLACE VIEW public.current_dept_emp AS
-SELECT
- l.emp_no,
- dept_no,
- l.from_date,
- l.to_date
-FROM
- public.dept_emp d
- INNER JOIN public.dept_emp_latest_date l ON d.emp_no = l.emp_no
- AND d.from_date = l.from_date
- AND l.to_date = d.to_date;
- `,
- migrationDDL: `
--- Drop views first (due to dependencies)
-DROP VIEW public.current_dept_emp;
-DROP VIEW public.dept_emp_latest_date;
-
--- Drop trigger
-DROP TRIGGER salary_log_trigger ON public.salary;
-
--- Drop function
-DROP FUNCTION public.log_dml_operations();
-
--- Drop indexes
-DROP INDEX idx_audit_changed_at;
-DROP INDEX idx_audit_username;
-DROP INDEX idx_audit_operation;
-DROP INDEX idx_salary_amount;
-DROP INDEX idx_employee_hire_date;
-
--- Drop tables (respecting foreign key dependencies)
-DROP TABLE public.audit;
-DROP TABLE public.title;
-DROP TABLE public.salary;
-DROP TABLE public.dept_emp;
-DROP TABLE public.dept_manager;
-DROP TABLE public.department;
-DROP TABLE public.employee;
-`,
- description: "Reverse of bytebase_schema: DROP entire schema with all objects",
- },
- {
- name: "reverse_comments_with_special_characters",
- initialSchema: `
-CREATE TABLE test_table (
- id SERIAL PRIMARY KEY,
- data VARCHAR(255),
- status VARCHAR(20) DEFAULT 'active'
-);
-
-CREATE OR REPLACE FUNCTION get_active_records()
-RETURNS TABLE(id INTEGER, data VARCHAR) AS $$
-BEGIN
- RETURN QUERY
- SELECT t.id, t.data
- FROM test_table t
- WHERE t.status = 'active';
-END;
-$$ LANGUAGE plpgsql;
-
-COMMENT ON TABLE test_table IS 'Test table with "quotes" and ''apostrophes'' and $special$ characters';
-COMMENT ON COLUMN test_table.id IS 'ID with symbols: @#$%^&*()_+-={}|[]\\:";''<>?,./';
-COMMENT ON COLUMN test_table.data IS 'Data field containing
-multiline
-text with various symbols: ñáéíóú àèìòù äëïöü';
-COMMENT ON COLUMN test_table.status IS 'Status: active/inactive (default: active)';
-COMMENT ON FUNCTION get_active_records() IS 'Returns all active records from test_table';
-`,
- migrationDDL: `
--- Drop function comment
-COMMENT ON FUNCTION get_active_records() IS NULL;
-
--- Drop function
-DROP FUNCTION get_active_records();
-
--- Drop column comments
-COMMENT ON COLUMN test_table.status IS NULL;
-COMMENT ON COLUMN test_table.data IS NULL;
-COMMENT ON COLUMN test_table.id IS NULL;
-
--- Drop table comment
-COMMENT ON TABLE test_table IS NULL;
-`,
- description: "Reverse of comments_with_special_characters: DROP comments with special chars",
- },
- {
- name: "trigger_function_body_change",
- initialSchema: `
-CREATE TABLE user_activity_log (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- action VARCHAR(50) NOT NULL,
- details TEXT,
- logged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- last_login TIMESTAMP,
- login_count INTEGER DEFAULT 0,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
--- Function that will be used by trigger
-CREATE OR REPLACE FUNCTION log_user_login() RETURNS TRIGGER AS $$
-BEGIN
- -- Original implementation: simple logging
- INSERT INTO user_activity_log (user_id, action, details)
- VALUES (NEW.id, 'login', 'User logged in at ' || NEW.last_login);
-
- UPDATE users SET login_count = COALESCE(login_count, 0) + 1 WHERE id = NEW.id;
-
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-
--- Trigger depending on the function
-CREATE TRIGGER user_login_trigger
- AFTER UPDATE OF last_login ON users
- FOR EACH ROW
- WHEN (OLD.last_login IS DISTINCT FROM NEW.last_login AND NEW.last_login IS NOT NULL)
- EXECUTE FUNCTION log_user_login();
-`,
- migrationDDL: `
--- Modify only the function body (keeping signature identical)
-CREATE OR REPLACE FUNCTION log_user_login() RETURNS TRIGGER AS $$
-BEGIN
- -- Enhanced implementation: more detailed logging with additional checks
- IF NEW.last_login IS NOT NULL THEN
- INSERT INTO user_activity_log (user_id, action, details)
- VALUES (NEW.id, 'login', 'User ' || NEW.username || ' logged in at ' || NEW.last_login || ' (previous: ' || COALESCE(OLD.last_login::text, 'never') || ')');
-
- -- Update login count with better handling
- UPDATE users
- SET login_count = COALESCE(login_count, 0) + 1
- WHERE id = NEW.id;
-
- -- Log additional info for frequent users
- IF NEW.login_count > 100 THEN
- INSERT INTO user_activity_log (user_id, action, details)
- VALUES (NEW.id, 'frequent_user', 'User has logged in more than 100 times');
- END IF;
- END IF;
-
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-`,
- description: "Trigger depending on function with body-only change (should use CREATE OR REPLACE)",
- },
- {
- name: "reverse_trigger_function_body_change",
- initialSchema: `
-CREATE TABLE user_activity_log (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- action VARCHAR(50) NOT NULL,
- details TEXT,
- logged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(100) NOT NULL,
- last_login TIMESTAMP,
- login_count INTEGER DEFAULT 0,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
--- Enhanced function implementation (this is the "after" state)
-CREATE OR REPLACE FUNCTION log_user_login() RETURNS TRIGGER AS $$
-BEGIN
- -- Enhanced implementation: more detailed logging with additional checks
- IF NEW.last_login IS NOT NULL THEN
- INSERT INTO user_activity_log (user_id, action, details)
- VALUES (NEW.id, 'login', 'User ' || NEW.username || ' logged in at ' || NEW.last_login || ' (previous: ' || COALESCE(OLD.last_login::text, 'never') || ')');
-
- -- Update login count with better handling
- UPDATE users
- SET login_count = COALESCE(login_count, 0) + 1
- WHERE id = NEW.id;
-
- -- Log additional info for frequent users
- IF NEW.login_count > 100 THEN
- INSERT INTO user_activity_log (user_id, action, details)
- VALUES (NEW.id, 'frequent_user', 'User has logged in more than 100 times');
- END IF;
- END IF;
-
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-
--- Trigger depending on the function
-CREATE TRIGGER user_login_trigger
- AFTER UPDATE OF last_login ON users
- FOR EACH ROW
- WHEN (OLD.last_login IS DISTINCT FROM NEW.last_login AND NEW.last_login IS NOT NULL)
- EXECUTE FUNCTION log_user_login();
-`,
- migrationDDL: `
--- Revert function to original simpler implementation
-CREATE OR REPLACE FUNCTION log_user_login() RETURNS TRIGGER AS $$
-BEGIN
- -- Original implementation: simple logging
- INSERT INTO user_activity_log (user_id, action, details)
- VALUES (NEW.id, 'login', 'User logged in at ' || NEW.last_login);
-
- UPDATE users SET login_count = COALESCE(login_count, 0) + 1 WHERE id = NEW.id;
-
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql;
-`,
- description: "Reverse of trigger_function_body_change: revert function body to original",
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
-
- // Create a new database for each test case with unique name
- dbName := fmt.Sprintf("test_%s_%s", strings.ReplaceAll(tc.name, "-", "_"), strings.ReplaceAll(fmt.Sprintf("%d", time.Now().UnixNano()%1000000), "-", "_"))
- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
- require.NoError(t, err)
-
- // Create driver for the test database
- driver, err := createPgDriver(ctx, container.GetHost(), container.GetPort(), dbName)
- require.NoError(t, err)
- defer driver.Close(ctx)
-
- // Step 1: Initialize the database schema and get schema result A
- _, err = driver.GetDB().ExecContext(ctx, tc.initialSchema)
- require.NoError(t, err)
-
- schemaA, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 2: Do some migration and get schema result B
- _, err = driver.GetDB().ExecContext(ctx, tc.migrationDDL)
- require.NoError(t, err)
-
- schemaB, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 3: Call generate migration to get the rollback DDL
- // Convert to model.DatabaseSchema
- dbMetadataA := model.NewDatabaseMetadata(schemaA, nil, nil, storepb.Engine_POSTGRES, false)
- dbMetadataB := model.NewDatabaseMetadata(schemaB, nil, nil, storepb.Engine_POSTGRES, false)
-
- // Get diff from B to A (to generate rollback)
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, dbMetadataB, dbMetadataA)
- require.NoError(t, err)
-
- // Log the diff for debugging
- t.Logf("Test case: %s", tc.description)
- t.Logf("Schema changes: %d", len(diff.SchemaChanges))
- for _, sc := range diff.SchemaChanges {
- t.Logf(" Schema: %s, Action: %v", sc.SchemaName, sc.Action)
- }
- t.Logf("Table changes: %d", len(diff.TableChanges))
- for _, tc := range diff.TableChanges {
- t.Logf(" Table: %s.%s, Action: %v", tc.SchemaName, tc.TableName, tc.Action)
- }
- t.Logf("Sequence changes: %d", len(diff.SequenceChanges))
- for _, sc := range diff.SequenceChanges {
- t.Logf(" Sequence: %s.%s, Action: %v", sc.SchemaName, sc.SequenceName, sc.Action)
- }
-
- // Log schema changes for debugging
- if diff.SchemaChanges != nil {
- for _, schemaDiff := range diff.SchemaChanges {
- t.Logf("Schema diff: %s, Action: %v", schemaDiff.SchemaName, schemaDiff.Action)
- if schemaDiff.OldSchema != nil {
- t.Logf(" Old schema %s enum types: %d", schemaDiff.SchemaName, len(schemaDiff.OldSchema.EnumTypes))
- for _, enum := range schemaDiff.OldSchema.EnumTypes {
- t.Logf(" Old Enum: %s.%s", schemaDiff.SchemaName, enum.Name)
- }
- }
- if schemaDiff.NewSchema != nil {
- t.Logf(" New schema %s enum types: %d", schemaDiff.SchemaName, len(schemaDiff.NewSchema.EnumTypes))
- for _, enum := range schemaDiff.NewSchema.EnumTypes {
- t.Logf(" New Enum: %s.%s", schemaDiff.SchemaName, enum.Name)
- }
- }
- }
- }
-
- // Generate rollback migration
- rollbackDDL, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
-
- t.Logf("Rollback DDL:\n%s", rollbackDDL)
-
- // Step 4: Run rollback DDL and get schema result C
- _, err = driver.GetDB().ExecContext(ctx, rollbackDDL)
- require.NoError(t, err)
-
- schemaC, err := driver.SyncDBSchema(ctx)
- require.NoError(t, err)
-
- // Step 5: Compare schema result A and C to ensure they are the same
- normalizeMetadataForComparison(schemaA)
- normalizeMetadataForComparison(schemaC)
-
- // Use cmp with protocmp for proto message comparison
- if diff := cmp.Diff(schemaA, schemaC, protocmp.Transform()); diff != "" {
- t.Errorf("Schema mismatch after rollback (-want +got):\n%s", diff)
- }
- })
- }
-}
-
-// normalizeMetadataForComparison normalizes metadata to ignore differences that don't affect schema equality
-func normalizeMetadataForComparison(metadata *storepb.DatabaseSchemaMetadata) {
- // Clear database name as it might differ
- metadata.Name = ""
-
- // Normalize schemas
- for _, schema := range metadata.Schemas {
- // Clear volatile fields (schemas don't have CreateTime/UpdateTime)
-
- // Normalize tables
- for _, table := range schema.Tables {
- table.DataSize = 0
- table.IndexSize = 0
- table.RowCount = 0
-
- // Clear column positions as they can change when columns are added/dropped
- for _, column := range table.Columns {
- column.Position = 0
- }
-
- // Sort columns by name for consistent comparison
- sortColumnsByName(table.Columns)
-
- // Sort indexes by name
- sortIndexesByName(table.Indexes)
-
- // Sort foreign keys by name
- sortForeignKeysByName(table.ForeignKeys)
-
- // Sort check constraints by name
- sortCheckConstraintsByName(table.CheckConstraints)
- }
-
- // Normalize views (no volatile fields to clear)
-
- // Normalize materialized views (no volatile fields to clear)
-
- // Normalize functions (no volatile fields to clear)
-
- // Normalize sequences (no volatile fields to clear)
-
- // Sort all collections for consistent comparison
- sortTablesByName(schema.Tables)
- sortViewsByName(schema.Views)
- sortMaterializedViewsByName(schema.MaterializedViews)
- sortFunctionsByName(schema.Functions)
- sortSequencesByName(schema.Sequences)
- sortEnumsByName(schema.EnumTypes)
- }
-
- // Sort schemas by name
- sortSchemasByName(metadata.Schemas)
-
- // Sort extensions by name
- sortExtensionsByName(metadata.Extensions)
-}
-
-// Sorting helper functions
-func sortSchemasByName(schemas []*storepb.SchemaMetadata) {
- slices.SortFunc(schemas, func(a, b *storepb.SchemaMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortTablesByName(tables []*storepb.TableMetadata) {
- slices.SortFunc(tables, func(a, b *storepb.TableMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortColumnsByName(columns []*storepb.ColumnMetadata) {
- slices.SortFunc(columns, func(a, b *storepb.ColumnMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortIndexesByName(indexes []*storepb.IndexMetadata) {
- slices.SortFunc(indexes, func(a, b *storepb.IndexMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortForeignKeysByName(fks []*storepb.ForeignKeyMetadata) {
- slices.SortFunc(fks, func(a, b *storepb.ForeignKeyMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortCheckConstraintsByName(checks []*storepb.CheckConstraintMetadata) {
- slices.SortFunc(checks, func(a, b *storepb.CheckConstraintMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortViewsByName(views []*storepb.ViewMetadata) {
- slices.SortFunc(views, func(a, b *storepb.ViewMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortMaterializedViewsByName(mvs []*storepb.MaterializedViewMetadata) {
- slices.SortFunc(mvs, func(a, b *storepb.MaterializedViewMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortFunctionsByName(functions []*storepb.FunctionMetadata) {
- slices.SortFunc(functions, func(a, b *storepb.FunctionMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortSequencesByName(sequences []*storepb.SequenceMetadata) {
- slices.SortFunc(sequences, func(a, b *storepb.SequenceMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortEnumsByName(enums []*storepb.EnumTypeMetadata) {
- slices.SortFunc(enums, func(a, b *storepb.EnumTypeMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
-
-func sortExtensionsByName(extensions []*storepb.ExtensionMetadata) {
- slices.SortFunc(extensions, func(a, b *storepb.ExtensionMetadata) int {
- return strings.Compare(a.Name, b.Name)
- })
-}
diff --git a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
index 7e85fd85e9..0a7a6d5ffc 100644
--- a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
@@ -551,31 +551,14 @@ func compareMaterializedViewsDef(t *testing.T, viewsA, viewsB []*storepb.Materia
mapA[view.Name] = view
}
- // Get PostgreSQL view comparer for sophisticated comparison
- pgComparer := &PostgreSQLViewComparer{}
-
for _, viewB := range viewsB {
viewA, exists := mapA[viewB.Name]
require.True(t, exists, "materialized view %s should exist in metadata A", viewB.Name)
- // Compare view definitions using PostgreSQL view comparer
- definitionsEqual := pgComparer.compareViewsSemanticaly(viewA.Definition, viewB.Definition)
-
- // Ensure definitions are equal using PostgreSQL ANTLR parser comparison
- if !definitionsEqual {
- // Log normalized definitions for debugging
- t.Logf("Materialized view %s definition mismatch:", viewB.Name)
- t.Logf(" A normalized: %q", pgComparer.normalizeExpression(viewA.Definition))
- t.Logf(" B normalized: %q", pgComparer.normalizeExpression(viewB.Definition))
- t.Logf(" A original: %q", viewA.Definition)
- t.Logf(" B original: %q", viewB.Definition)
-
- // Fallback to simple comparison for debugging
- defA := normalizeSQLDef(viewA.Definition)
- defB := normalizeSQLDef(viewB.Definition)
- t.Logf(" A simple normalized: %q", defA)
- t.Logf(" B simple normalized: %q", defB)
- }
+ // Compare view definitions using normalized string comparison
+ defA := normalizeSQLDef(viewA.Definition)
+ defB := normalizeSQLDef(viewB.Definition)
+ definitionsEqual := defA == defB
// Assert that materialized view definitions must be equal after sophisticated PostgreSQL parsing
require.True(t, definitionsEqual,
@@ -839,20 +822,13 @@ func compareViewsDef(t *testing.T, viewsA, viewsB []*storepb.ViewMetadata) {
mapA[view.Name] = view
}
- // Get PostgreSQL view comparer for sophisticated comparison
- pgComparer := &PostgreSQLViewComparer{}
-
for _, viewB := range viewsB {
viewA, exists := mapA[viewB.Name]
require.True(t, exists, "view %s should exist in metadata A", viewB.Name)
- // Compare view definitions using PostgreSQL view comparer
- definitionsEqual := pgComparer.compareViewsSemanticaly(viewA.Definition, viewB.Definition)
-
- // Assert that view definitions must be equal after sophisticated PostgreSQL parsing
- require.True(t, definitionsEqual,
- "view %s: definitions must be equal using PostgreSQL view comparer (A: %q, B: %q)",
- viewB.Name, viewA.Definition, viewB.Definition)
+ // Compare view definitions using normalized string comparison
+ require.Equal(t, normalizeSQLDef(viewA.Definition), normalizeSQLDef(viewB.Definition),
+ "view %s: definitions should match", viewB.Name)
// Compare comment
require.Equal(t, viewA.Comment, viewB.Comment, "view %s: comment should match", viewB.Name)
diff --git a/backend/plugin/schema/pg/index_comparer.go b/backend/plugin/schema/pg/index_comparer.go
deleted file mode 100644
index a59527aace..0000000000
--- a/backend/plugin/schema/pg/index_comparer.go
+++ /dev/null
@@ -1,318 +0,0 @@
-package pg
-
-import (
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- pgparser "github.com/bytebase/parser/postgresql"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
-)
-
-func init() {
- // Register PostgreSQL-specific index comparer
- schema.RegisterIndexComparer(storepb.Engine_POSTGRES, &PostgreSQLIndexComparer{})
-}
-
-// PostgreSQLIndexComparer provides PostgreSQL-specific index comparison logic.
-type PostgreSQLIndexComparer struct{}
-
-// Ensure PostgreSQLIndexComparer implements the IndexComparer interface
-var _ schema.IndexComparer = &PostgreSQLIndexComparer{}
-
-// CompareIndexWhereConditions compares WHERE conditions from PostgreSQL index definitions using AST parsing.
-func (c *PostgreSQLIndexComparer) CompareIndexWhereConditions(def1, def2 string) bool {
- whereClause1 := c.ExtractWhereClauseFromIndexDef(def1)
- whereClause2 := c.ExtractWhereClauseFromIndexDef(def2)
-
- // If both have no WHERE clause, they're equal
- if whereClause1 == "" && whereClause2 == "" {
- return true
- }
-
- // If only one has WHERE clause, they're different
- if whereClause1 == "" || whereClause2 == "" {
- return false
- }
-
- // Use expression comparer for semantic comparison
- return schema.CompareExpressionsSemantically(storepb.Engine_POSTGRES, whereClause1, whereClause2)
-}
-
-// ExtractWhereClauseFromIndexDef extracts the WHERE clause from a CREATE INDEX statement using ANTLR AST parsing.
-func (*PostgreSQLIndexComparer) ExtractWhereClauseFromIndexDef(definition string) string {
- if definition == "" {
- return ""
- }
-
- // Parse the CREATE INDEX statement using ANTLR parser
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := pgparser.NewPostgreSQLParser(stream)
-
- // Disable error reporting for cleaner parsing
- parser.RemoveErrorListeners()
-
- // Parse as root statement
- tree := parser.Root()
-
- // Use visitor to extract WHERE clause from IndexStmt
- extractor := &indexWhereExtractor{}
- extractor.Visit(tree)
-
- return extractor.whereClause
-}
-
-// indexWhereExtractor is a visitor that extracts WHERE clauses from CREATE INDEX statements.
-type indexWhereExtractor struct {
- *pgparser.BasePostgreSQLParserVisitor
- whereClause string
-}
-
-// Visit implements the visitor pattern to walk the AST.
-func (e *indexWhereExtractor) Visit(tree antlr.ParseTree) {
- switch ctx := tree.(type) {
- case *pgparser.IndexstmtContext:
- e.visitIndexStmt(ctx)
- default:
- // Recursively visit children
- if tree != nil {
- for i := 0; i < tree.GetChildCount(); i++ {
- child := tree.GetChild(i)
- if parseTree, ok := child.(antlr.ParseTree); ok {
- e.Visit(parseTree)
- }
- }
- }
- }
-}
-
-// visitIndexStmt extracts the WHERE clause from an IndexStmt AST node.
-func (e *indexWhereExtractor) visitIndexStmt(ctx *pgparser.IndexstmtContext) {
- if ctx == nil {
- return
- }
-
- // Get the WHERE clause from the IndexStmt
- whereClauseCtx := ctx.Where_clause()
- if whereClauseCtx == nil {
- e.whereClause = ""
- return
- }
-
- // Extract the expression from the WHERE clause (excluding the WHERE keyword)
- aExprCtx := whereClauseCtx.A_expr()
- if aExprCtx == nil {
- e.whereClause = ""
- return
- }
-
- // Get the original text of the expression
- e.whereClause = e.getTextFromContext(aExprCtx)
-}
-
-// getTextFromContext extracts the original source text from a parse tree context.
-func (*indexWhereExtractor) getTextFromContext(ctx antlr.ParserRuleContext) string {
- if ctx == nil {
- return ""
- }
-
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start == nil || stop == nil {
- return ""
- }
-
- input := start.GetInputStream()
- if input == nil {
- return ""
- }
-
- startIndex := start.GetStart()
- stopIndex := stop.GetStop()
-
- if startIndex < 0 || stopIndex < 0 || stopIndex < startIndex {
- return ""
- }
-
- // Extract the original text from the input stream
- return input.GetText(startIndex, stopIndex)
-}
-
-// ExtractIncludeClauseFromIndexDef extracts the INCLUDE clause from a CREATE INDEX statement using ANTLR AST parsing.
-// Returns the full "INCLUDE (col1, col2, ...)" string, or empty if no INCLUDE clause exists.
-func (*PostgreSQLIndexComparer) ExtractIncludeClauseFromIndexDef(definition string) string {
- if definition == "" {
- return ""
- }
-
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := pgparser.NewPostgreSQLParser(stream)
- parser.RemoveErrorListeners()
-
- tree := parser.Root()
-
- extractor := &indexIncludeExtractor{}
- extractor.Visit(tree)
-
- return extractor.includeClause
-}
-
-// indexIncludeExtractor is a visitor that extracts INCLUDE clauses from CREATE INDEX statements.
-type indexIncludeExtractor struct {
- *pgparser.BasePostgreSQLParserVisitor
- includeClause string
-}
-
-// Visit implements the visitor pattern to walk the AST.
-func (e *indexIncludeExtractor) Visit(tree antlr.ParseTree) {
- switch ctx := tree.(type) {
- case *pgparser.IndexstmtContext:
- e.visitIndexStmt(ctx)
- default:
- if tree != nil {
- for i := 0; i < tree.GetChildCount(); i++ {
- child := tree.GetChild(i)
- if parseTree, ok := child.(antlr.ParseTree); ok {
- e.Visit(parseTree)
- }
- }
- }
- }
-}
-
-func (e *indexIncludeExtractor) visitIndexStmt(ctx *pgparser.IndexstmtContext) {
- if ctx == nil {
- return
- }
-
- optInclude := ctx.Opt_include()
- if optInclude == nil {
- return
- }
-
- // Get the full text of the INCLUDE clause: "INCLUDE (col1, col2, ...)"
- e.includeClause = e.getTextFromContext(optInclude)
-}
-
-func (*indexIncludeExtractor) getTextFromContext(ctx antlr.ParserRuleContext) string {
- if ctx == nil {
- return ""
- }
-
- start := ctx.GetStart()
- stop := ctx.GetStop()
- if start == nil || stop == nil {
- return ""
- }
-
- input := start.GetInputStream()
- if input == nil {
- return ""
- }
-
- startIndex := start.GetStart()
- stopIndex := stop.GetStop()
-
- if startIndex < 0 || stopIndex < 0 || stopIndex < startIndex {
- return ""
- }
-
- return input.GetText(startIndex, stopIndex)
-}
-
-// IndexDefinition represents the parsed structure of a CREATE INDEX statement.
-type IndexDefinition struct {
- IndexName string
- TableName string
- Unique bool
- Method string // btree, gin, gist, etc.
- Expressions []string
- WhereClause string
-}
-
-// ParseIndexDefinition parses a complete CREATE INDEX statement and returns structured information.
-func (c *PostgreSQLIndexComparer) ParseIndexDefinition(definition string) (*IndexDefinition, error) {
- if definition == "" {
- return nil, nil
- }
-
- result := &IndexDefinition{
- WhereClause: c.ExtractWhereClauseFromIndexDef(definition),
- }
-
- // Parse the CREATE INDEX statement using ANTLR parser
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := pgparser.NewPostgreSQLParser(stream)
-
- // Disable error reporting for cleaner parsing
- parser.RemoveErrorListeners()
-
- // Parse as root statement
- tree := parser.Root()
-
- // Use visitor to extract structured information from IndexStmt
- extractor := &indexDefinitionExtractor{result: result}
- extractor.Visit(tree)
-
- return result, nil
-}
-
-// indexDefinitionExtractor is a visitor that extracts structured information from CREATE INDEX statements.
-type indexDefinitionExtractor struct {
- *pgparser.BasePostgreSQLParserVisitor
- result *IndexDefinition
-}
-
-// Visit implements the visitor pattern to walk the AST.
-func (e *indexDefinitionExtractor) Visit(tree antlr.ParseTree) {
- switch ctx := tree.(type) {
- case *pgparser.IndexstmtContext:
- e.visitIndexStmtForDefinition(ctx)
- default:
- // Recursively visit children
- if tree != nil {
- for i := 0; i < tree.GetChildCount(); i++ {
- child := tree.GetChild(i)
- if parseTree, ok := child.(antlr.ParseTree); ok {
- e.Visit(parseTree)
- }
- }
- }
- }
-}
-
-// visitIndexStmtForDefinition extracts structured information from an IndexStmt AST node.
-func (e *indexDefinitionExtractor) visitIndexStmtForDefinition(ctx *pgparser.IndexstmtContext) {
- if ctx == nil || e.result == nil {
- return
- }
-
- // Extract UNIQUE flag
- if ctx.Opt_unique() != nil {
- e.result.Unique = true
- }
-
- // Extract index name
- if ctx.Name() != nil {
- e.result.IndexName = ctx.Name().GetText()
- }
-
- // Extract table name from relation_expr
- if ctx.Relation_expr() != nil {
- e.result.TableName = ctx.Relation_expr().GetText()
- }
-
- // Extract access method
- if ctx.Access_method_clause() != nil {
- e.result.Method = strings.ToLower(ctx.Access_method_clause().GetText())
- }
-
- // WHERE clause is already extracted in the result initialization
-}
diff --git a/backend/plugin/schema/pg/materialized_view_index_test.go b/backend/plugin/schema/pg/materialized_view_index_test.go
deleted file mode 100644
index d17347f363..0000000000
--- a/backend/plugin/schema/pg/materialized_view_index_test.go
+++ /dev/null
@@ -1,248 +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"
-)
-
-// TestMaterializedViewIndexInSDLOutput tests that materialized view indexes are included in SDL output
-func TestMaterializedViewIndexInSDLOutput(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "product_summary_mv",
- Definition: "SELECT id, name FROM products",
- Comment: "Product summary view",
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "idx_product_name",
- Expressions: []string{"name"},
- Type: "btree",
- Unique: false,
- Primary: false,
- IsConstraint: false,
- Comment: "Index on product name",
- },
- {
- Name: "idx_product_id_unique",
- Expressions: []string{"id"},
- Type: "btree",
- Unique: true,
- Primary: false,
- IsConstraint: false,
- },
- },
- },
- },
- },
- },
- }
-
- // Test single-file SDL format
- sdl, err := getSDLFormat(metadata)
- require.NoError(t, err)
-
- // Verify materialized view is present
- require.Contains(t, sdl, "CREATE MATERIALIZED VIEW")
- require.Contains(t, sdl, "product_summary_mv")
-
- // Verify indexes are present
- require.Contains(t, sdl, "CREATE INDEX \"idx_product_name\"")
- require.Contains(t, sdl, "CREATE UNIQUE INDEX \"idx_product_id_unique\"")
-
- // Verify index is on materialized view (no ON ONLY for non-partitioned)
- require.Contains(t, sdl, "ON \"public\".\"product_summary_mv\"")
- require.NotContains(t, sdl, "ON ONLY")
-
- // Verify comments
- require.Contains(t, sdl, "COMMENT ON MATERIALIZED VIEW")
- require.Contains(t, sdl, "Product summary view")
- require.Contains(t, sdl, "COMMENT ON INDEX")
- require.Contains(t, sdl, "Index on product name")
-}
-
-// TestMaterializedViewIndexInMultiFileSDL tests that materialized view indexes are included in multi-file SDL
-func TestMaterializedViewIndexInMultiFileSDL(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "user_stats_mv",
- Definition: "SELECT user_id, COUNT(*) as count FROM orders GROUP BY user_id",
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "idx_user_stats_user_id",
- Expressions: []string{"user_id"},
- Type: "btree",
- Unique: false,
- Primary: false,
- IsConstraint: false,
- },
- },
- },
- },
- },
- },
- }
-
- // Test multi-file SDL format
- result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{}, metadata)
- require.NoError(t, err)
- require.NotNil(t, result)
-
- // Find the materialized view file
- var mvFile *schema.File
- for i := range result.Files {
- if result.Files[i].Name == "schemas/public/materialized_views/user_stats_mv.sql" {
- mvFile = &result.Files[i]
- break
- }
- }
- require.NotNil(t, mvFile, "Materialized view file not found")
-
- // Verify content includes both MV and index
- require.Contains(t, mvFile.Content, "CREATE MATERIALIZED VIEW")
- require.Contains(t, mvFile.Content, "user_stats_mv")
- require.Contains(t, mvFile.Content, "CREATE INDEX \"idx_user_stats_user_id\"")
- require.Contains(t, mvFile.Content, "ON \"public\".\"user_stats_mv\"")
- require.NotContains(t, mvFile.Content, "ON ONLY")
-}
-
-// TestMaterializedViewIndexDependencyOrdering tests that materialized view indexes have correct dependency ordering in migrations
-func TestMaterializedViewIndexDependencyOrdering(t *testing.T) {
- t.Run("CREATE: MV before Index", func(t *testing.T) {
- // Test that when creating a materialized view with indexes,
- // CREATE MATERIALIZED VIEW comes before CREATE INDEX
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "customer_segmentation_mv",
- Definition: "SELECT customer_id, SUM(amount) as total_spending FROM orders GROUP BY customer_id",
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "idx_customer_seg_spending",
- Expressions: []string{"total_spending"},
- Type: "btree",
- Unique: false,
- Primary: false,
- },
- },
- },
- },
- },
- },
- }
-
- // Convert to model.DatabaseSchema
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- // Get diff
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Generate migration
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
- require.NotEmpty(t, migration)
-
- // Find positions of CREATE MATERIALIZED VIEW and CREATE INDEX
- mvPos := -1
- idxPos := -1
- for i := 0; i < len(migration); i++ {
- if mvPos == -1 && i+len("CREATE MATERIALIZED VIEW") <= len(migration) {
- if migration[i:i+len("CREATE MATERIALIZED VIEW")] == "CREATE MATERIALIZED VIEW" {
- mvPos = i
- }
- }
- if idxPos == -1 && i+len("CREATE INDEX") <= len(migration) {
- if migration[i:i+len("CREATE INDEX")] == "CREATE INDEX" {
- idxPos = i
- }
- }
- }
-
- require.NotEqual(t, -1, mvPos, "CREATE MATERIALIZED VIEW not found in migration")
- require.NotEqual(t, -1, idxPos, "CREATE INDEX not found in migration")
- require.Less(t, mvPos, idxPos, "CREATE MATERIALIZED VIEW should come before CREATE INDEX")
- })
-
- t.Run("DROP: Whole MV with indexes", func(t *testing.T) {
- // Test that when dropping an entire materialized view with indexes,
- // PostgreSQL automatically drops indexes so we don't need separate DROP INDEX statements
-
- oldMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- MaterializedViews: []*storepb.MaterializedViewMetadata{
- {
- Name: "customer_segmentation_mv",
- Definition: "SELECT customer_id, SUM(amount) as total_spending FROM orders GROUP BY customer_id",
- Indexes: []*storepb.IndexMetadata{
- {
- Name: "idx_customer_seg_spending",
- Expressions: []string{"total_spending"},
- Type: "btree",
- Unique: false,
- Primary: false,
- },
- },
- },
- },
- },
- },
- }
-
- newMetadata := &storepb.DatabaseSchemaMetadata{
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- },
- },
- }
-
- // Convert to model.DatabaseSchema
- oldSchema := model.NewDatabaseMetadata(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
- newSchema := model.NewDatabaseMetadata(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)
-
- // Get diff
- diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)
- require.NoError(t, err)
-
- // Generate migration
- migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
- require.NoError(t, err)
- t.Logf("Generated migration:\n%s", migration)
- require.NotEmpty(t, migration)
-
- // Verify DROP MATERIALIZED VIEW is present
- require.Contains(t, migration, "DROP MATERIALIZED VIEW")
- // Note: PostgreSQL automatically drops indexes when dropping a materialized view,
- // so we don't need (and shouldn't have) separate DROP INDEX statements
- require.NotContains(t, migration, "DROP INDEX")
- })
-}
diff --git a/backend/plugin/schema/pg/view_comparer.go b/backend/plugin/schema/pg/view_comparer.go
deleted file mode 100644
index f6c2c7d24a..0000000000
--- a/backend/plugin/schema/pg/view_comparer.go
+++ /dev/null
@@ -1,820 +0,0 @@
-package pg
-
-import (
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- pgparser "github.com/bytebase/parser/postgresql"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/plugin/schema/pg/ast"
-)
-
-func init() {
- // Register PostgreSQL-specific view comparer
- schema.RegisterViewComparer(storepb.Engine_POSTGRES, &PostgreSQLViewComparer{})
-}
-
-// PostgreSQLViewComparer provides PostgreSQL-specific view comparison logic.
-type PostgreSQLViewComparer struct {
- schema.DefaultViewComparer
-}
-
-// CompareView compares two views with PostgreSQL-specific logic using AST semantic analysis.
-func (c *PostgreSQLViewComparer) CompareView(oldView, newView *storepb.ViewMetadata) ([]schema.ViewChange, error) {
- if oldView == nil || newView == nil {
- return nil, nil
- }
-
- var changes []schema.ViewChange
-
- // Use AST-based semantic comparison
- semanticallyEqual := c.compareViewsSemanticaly(oldView.Definition, newView.Definition)
-
- if !semanticallyEqual {
- // Views are semantically different
- changes = append(changes, schema.ViewChange{
- Type: schema.ViewChangeDefinition,
- Description: "View definition changed (semantic difference)",
- RequiresRecreation: true,
- })
- }
-
- // Compare comment from proto
- oldComment := oldView.Comment
- newComment := newView.Comment
- if oldComment != newComment {
- changes = append(changes, schema.ViewChange{
- Type: schema.ViewChangeComment,
- Description: "View comment changed",
- RequiresRecreation: false,
- })
- }
-
- return changes, nil
-}
-
-// CompareMaterializedView compares two materialized views with PostgreSQL-specific logic.
-func (c *PostgreSQLViewComparer) CompareMaterializedView(oldMV, newMV *storepb.MaterializedViewMetadata) ([]schema.MaterializedViewChange, error) {
- if oldMV == nil || newMV == nil {
- return nil, nil
- }
-
- var changes []schema.MaterializedViewChange
-
- // Use AST-based semantic comparison
- semanticallyEqual := c.compareViewsSemanticaly(oldMV.Definition, newMV.Definition)
-
- if !semanticallyEqual {
- changes = append(changes, schema.MaterializedViewChange{
- Type: schema.MaterializedViewChangeDefinition,
- Description: "Materialized view definition changed (semantic difference)",
- RequiresRecreation: true,
- })
- }
-
- // Compare comment from proto
- oldComment := oldMV.Comment
- newComment := newMV.Comment
- if oldComment != newComment {
- changes = append(changes, schema.MaterializedViewChange{
- Type: schema.MaterializedViewChangeComment,
- Description: "Materialized view comment changed",
- RequiresRecreation: false,
- })
- }
-
- // Compare indexes - PostgreSQL can add/drop indexes without recreating materialized views
- if !c.compareIndexes(oldMV.Indexes, newMV.Indexes) {
- changes = append(changes, schema.MaterializedViewChange{
- Type: schema.MaterializedViewChangeIndex,
- Description: "Materialized view indexes changed",
- RequiresRecreation: false, // PostgreSQL can modify indexes separately
- })
- }
-
- // Compare triggers
- if !c.compareTriggers(oldMV.Triggers, newMV.Triggers) {
- changes = append(changes, schema.MaterializedViewChange{
- Type: schema.MaterializedViewChangeTrigger,
- Description: "Materialized view triggers changed",
- RequiresRecreation: false,
- })
- }
-
- return changes, nil
-}
-
-// compareIndexes compares two index slices for equality.
-func (*PostgreSQLViewComparer) compareIndexes(oldIndexes, newIndexes []*storepb.IndexMetadata) bool {
- if len(oldIndexes) != len(newIndexes) {
- return false
- }
-
- // Create maps for easier comparison
- oldMap := make(map[string]*storepb.IndexMetadata)
- for _, index := range oldIndexes {
- oldMap[index.Name] = index
- }
-
- for _, newIndex := range newIndexes {
- oldIndex, exists := oldMap[newIndex.Name]
- if !exists {
- return false
- }
-
- // Compare index properties
- if oldIndex.Unique != newIndex.Unique ||
- oldIndex.Primary != newIndex.Primary ||
- oldIndex.Type != newIndex.Type ||
- oldIndex.Comment != newIndex.Comment ||
- len(oldIndex.Expressions) != len(newIndex.Expressions) {
- return false
- }
-
- // Compare index expressions using semantic comparison
- for i, oldExpr := range oldIndex.Expressions {
- newExpr := newIndex.Expressions[i]
- if !ast.CompareExpressionsSemantically(oldExpr, newExpr) {
- return false
- }
- }
- }
-
- return true
-}
-
-// compareTriggers compares two trigger slices for equality.
-func (*PostgreSQLViewComparer) compareTriggers(oldTriggers, newTriggers []*storepb.TriggerMetadata) bool {
- if len(oldTriggers) != len(newTriggers) {
- return false
- }
-
- // Create maps for easier comparison
- oldMap := make(map[string]*storepb.TriggerMetadata)
- for _, trigger := range oldTriggers {
- oldMap[trigger.Name] = trigger
- }
-
- for _, newTrigger := range newTriggers {
- oldTrigger, exists := oldMap[newTrigger.Name]
- if !exists {
- return false
- }
- if oldTrigger.Body != newTrigger.Body {
- return false
- }
- }
-
- return true
-}
-
-// compareViewsSemanticaly compares two view definitions using enhanced semantic analysis.
-func (c *PostgreSQLViewComparer) compareViewsSemanticaly(def1, def2 string) bool {
- // Use enhanced token-based semantic comparison
- // This is a practical approach that leverages the existing working tokenizer
- // while adding semantic understanding
-
- tokens1 := c.extractSemanticTokens(def1)
- tokens2 := c.extractSemanticTokens(def2)
-
- // Compare semantic token structures
- return c.compareSemanticTokens(tokens1, tokens2)
-}
-
-// isWhitespaceToken checks if the token is whitespace.
-func (*PostgreSQLViewComparer) isWhitespaceToken(tokenType int) bool {
- // PostgreSQL lexer whitespace token types
- return tokenType == pgparser.PostgreSQLLexerWhitespace ||
- tokenType == pgparser.PostgreSQLLexerNewline
-}
-
-// SemanticTokens represents the semantic structure of a SQL query.
-type SemanticTokens struct {
- SelectItems []string // normalized select items
- TableRefs []string // normalized table references
- JoinClauses []string // normalized join clauses
- WhereClause string // normalized where condition
- GroupByItems []string // normalized group by items
- OrderByItems []string // normalized order by items
-}
-
-// extractSemanticTokens extracts semantic tokens from a view definition.
-func (c *PostgreSQLViewComparer) extractSemanticTokens(definition string) *SemanticTokens {
- // Use ANTLR tokenizer to parse the SQL directly without any preprocessing
- inputStream := antlr.NewInputStream(definition)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- stream.Fill()
- tokens := stream.GetAllTokens()
-
- // Extract semantic information from tokens
- return c.extractSemanticStructure(tokens)
-}
-
-// extractSemanticStructure extracts semantic structure from ANTLR tokens.
-func (c *PostgreSQLViewComparer) extractSemanticStructure(tokens []antlr.Token) *SemanticTokens {
- semantic := &SemanticTokens{}
-
- // Simple state machine to parse the token sequence
- state := "INITIAL"
- currentItem := ""
-
- // Preprocess tokens to handle schema prefixes for semantic equivalence
- processedTokens := c.preprocessTokensForSemantics(tokens)
-
- for _, token := range processedTokens {
- tokenText := strings.ToLower(strings.TrimSpace(token.GetText()))
-
- // Skip whitespace and EOF tokens
- if c.isWhitespaceToken(token.GetTokenType()) || tokenText == "<eof>" || tokenText == ";" {
- continue
- }
-
- switch state {
- case "INITIAL":
- if tokenText == "select" {
- state = "SELECT_LIST"
- }
-
- case "SELECT_LIST":
- switch tokenText {
- case "from":
- if currentItem != "" {
- semantic.SelectItems = append(semantic.SelectItems, strings.TrimSpace(currentItem))
- currentItem = ""
- }
- state = "FROM_CLAUSE"
- case ",":
- if currentItem != "" {
- semantic.SelectItems = append(semantic.SelectItems, strings.TrimSpace(currentItem))
- currentItem = ""
- }
- default:
- // Handle qualified identifiers (table.column) properly
- if tokenText == "." {
- currentItem += tokenText // Don't add space before dots
- } else {
- if currentItem != "" && !strings.HasSuffix(currentItem, ".") {
- currentItem += " "
- }
- currentItem += tokenText
- }
- }
-
- case "FROM_CLAUSE":
- switch tokenText {
- case "where":
- if currentItem != "" {
- semantic.TableRefs = append(semantic.TableRefs, strings.TrimSpace(currentItem))
- currentItem = ""
- }
- state = "WHERE_CLAUSE"
- case "join", "inner", "left", "right", "full":
- if currentItem != "" {
- semantic.TableRefs = append(semantic.TableRefs, strings.TrimSpace(currentItem))
- currentItem = ""
- }
- state = "JOIN_CLAUSE"
- if tokenText != "inner" { // inner is often followed by join
- currentItem = tokenText
- }
- case "group":
- if currentItem != "" {
- semantic.TableRefs = append(semantic.TableRefs, strings.TrimSpace(currentItem))
- currentItem = ""
- }
- state = "GROUP_BY"
- case "order":
- if currentItem != "" {
- semantic.TableRefs = append(semantic.TableRefs, strings.TrimSpace(currentItem))
- currentItem = ""
- }
- state = "ORDER_BY"
- default:
- // Skip unnecessary parentheses for semantic comparison
- if tokenText == "(" || tokenText == ")" {
- // Skip parentheses that don't affect semantics
- continue
- }
-
- // Handle qualified identifiers (table.column) properly
- if tokenText == "." {
- currentItem += tokenText // Don't add space before dots
- } else {
- if currentItem != "" && !strings.HasSuffix(currentItem, ".") {
- currentItem += " "
- }
- currentItem += tokenText
- }
- }
-
- case "JOIN_CLAUSE":
- if tokenText == "on" {
- if currentItem != "" {
- semantic.JoinClauses = append(semantic.JoinClauses, strings.TrimSpace(currentItem))
- currentItem = ""
- }
- state = "JOIN_CONDITION"
- } else {
- if currentItem != "" {
- currentItem += " "
- }
- currentItem += tokenText
- }
-
- case "JOIN_CONDITION":
- switch tokenText {
- case "join", "inner", "left", "right", "full":
- if currentItem != "" {
- // This was actually a join condition, add it to join clauses
- if len(semantic.JoinClauses) > 0 {
- semantic.JoinClauses[len(semantic.JoinClauses)-1] += " ON " + strings.TrimSpace(currentItem)
- }
- currentItem = ""
- }
- state = "JOIN_CLAUSE"
- if tokenText != "inner" {
- currentItem = tokenText
- }
- case "where":
- if currentItem != "" {
- if len(semantic.JoinClauses) > 0 {
- semantic.JoinClauses[len(semantic.JoinClauses)-1] += " ON " + strings.TrimSpace(currentItem)
- }
- currentItem = ""
- }
- state = "WHERE_CLAUSE"
- case "group":
- if currentItem != "" {
- if len(semantic.JoinClauses) > 0 {
- semantic.JoinClauses[len(semantic.JoinClauses)-1] += " ON " + strings.TrimSpace(currentItem)
- }
- currentItem = ""
- }
- state = "GROUP_BY"
- case "order":
- if currentItem != "" {
- if len(semantic.JoinClauses) > 0 {
- semantic.JoinClauses[len(semantic.JoinClauses)-1] += " ON " + strings.TrimSpace(currentItem)
- }
- currentItem = ""
- }
- state = "ORDER_BY"
- default:
- // Skip unnecessary parentheses in JOIN conditions for semantic comparison
- if tokenText == "(" || tokenText == ")" {
- continue
- }
-
- // Handle qualified identifiers (table.column) properly
- if tokenText == "." {
- currentItem += tokenText // Don't add space before dots
- } else {
- if currentItem != "" && !strings.HasSuffix(currentItem, ".") {
- currentItem += " "
- }
- currentItem += tokenText
- }
- }
-
- case "WHERE_CLAUSE":
- switch tokenText {
- case "group":
- if currentItem != "" {
- semantic.WhereClause = strings.TrimSpace(currentItem)
- currentItem = ""
- }
- state = "GROUP_BY"
- case "order":
- if currentItem != "" {
- semantic.WhereClause = strings.TrimSpace(currentItem)
- currentItem = ""
- }
- state = "ORDER_BY"
- default:
- if currentItem != "" {
- currentItem += " "
- }
- currentItem += tokenText
- }
-
- case "GROUP_BY":
- if tokenText == "by" {
- // Skip "BY" in "GROUP BY"
- continue
- } else if tokenText == "having" {
- if currentItem != "" {
- semantic.GroupByItems = c.parseCommaSeparatedList(currentItem)
- currentItem = ""
- }
- state = "HAVING"
- } else if tokenText == "order" {
- if currentItem != "" {
- semantic.GroupByItems = c.parseCommaSeparatedList(currentItem)
- currentItem = ""
- }
- state = "ORDER_BY"
- } else {
- if currentItem != "" {
- currentItem += " "
- }
- currentItem += tokenText
- }
-
- case "ORDER_BY":
- if tokenText == "by" {
- // Skip "BY" in "ORDER BY"
- continue
- } else if tokenText == "limit" || tokenText == "offset" {
- if currentItem != "" {
- semantic.OrderByItems = c.parseCommaSeparatedList(currentItem)
- currentItem = ""
- }
- state = "LIMIT_OFFSET"
- } else {
- if currentItem != "" {
- currentItem += " "
- }
- currentItem += tokenText
- }
- default:
- // Handle unrecognized state - should not happen in normal parsing
- }
- }
-
- // Handle any remaining content
- switch state {
- case "SELECT_LIST":
- if currentItem != "" {
- semantic.SelectItems = append(semantic.SelectItems, strings.TrimSpace(currentItem))
- }
- case "FROM_CLAUSE":
- if currentItem != "" {
- semantic.TableRefs = append(semantic.TableRefs, strings.TrimSpace(currentItem))
- }
- case "JOIN_CLAUSE":
- if currentItem != "" {
- semantic.JoinClauses = append(semantic.JoinClauses, strings.TrimSpace(currentItem))
- }
- case "JOIN_CONDITION":
- if currentItem != "" && len(semantic.JoinClauses) > 0 {
- semantic.JoinClauses[len(semantic.JoinClauses)-1] += " ON " + strings.TrimSpace(currentItem)
- }
- case "WHERE_CLAUSE":
- if currentItem != "" {
- semantic.WhereClause = strings.TrimSpace(currentItem)
- }
- case "GROUP_BY":
- if currentItem != "" {
- semantic.GroupByItems = c.parseCommaSeparatedList(currentItem)
- }
- case "ORDER_BY":
- if currentItem != "" {
- semantic.OrderByItems = c.parseCommaSeparatedList(currentItem)
- }
- default:
- // Handle any unrecognized final state - no action needed
- }
-
- return semantic
-}
-
-// preprocessTokensForSemantics preprocesses tokens to handle schema prefixes and other semantic equivalences.
-func (*PostgreSQLViewComparer) preprocessTokensForSemantics(tokens []antlr.Token) []antlr.Token {
- var result []antlr.Token
-
- for i, token := range tokens {
- tokenText := strings.ToLower(strings.TrimSpace(token.GetText()))
-
- // Handle schema prefix removal for semantic equivalence
- if tokenText == "public" && i+1 < len(tokens) {
- // Look for the next non-whitespace token to see if it's a dot
- foundDot := false
- for j := i + 1; j < len(tokens); j++ {
- nextTokenText := strings.TrimSpace(tokens[j].GetText())
- nextTokenType := tokens[j].GetTokenType()
-
- // Skip whitespace tokens
- if isWhitespaceTokenType(nextTokenType) {
- continue
- }
-
- // If next non-whitespace token is ".", skip both "public" and "."
- if nextTokenText == "." {
- foundDot = true
- i = j // Skip both "public" and "." tokens
- }
- break // Found non-whitespace token, make decision
- }
-
- if foundDot {
- continue // Skip "public" token
- }
- }
-
- // Skip dots that follow "public" (handled above)
- if tokenText == "." && i > 0 {
- prevTokenFound := false
- for j := i - 1; j >= 0; j-- {
- prevTokenText := strings.ToLower(strings.TrimSpace(tokens[j].GetText()))
- prevTokenType := tokens[j].GetTokenType()
-
- // Skip whitespace tokens
- if isWhitespaceTokenType(prevTokenType) {
- continue
- }
-
- // If previous non-whitespace token is "public", skip this dot
- if prevTokenText == "public" {
- prevTokenFound = true
- }
- break // Found non-whitespace token, make decision
- }
-
- if prevTokenFound {
- continue // Skip dot that follows "public"
- }
- }
-
- result = append(result, token)
- }
-
- return result
-}
-
-// parseCommaSeparatedList parses a comma-separated list of items.
-func (*PostgreSQLViewComparer) parseCommaSeparatedList(text string) []string {
- if text == "" {
- return nil
- }
-
- items := strings.Split(text, ",")
- result := make([]string, 0, len(items))
- for _, item := range items {
- trimmed := strings.TrimSpace(item)
- if trimmed != "" {
- result = append(result, trimmed)
- }
- }
- return result
-}
-
-// compareSemanticTokens compares two semantic token structures.
-func (c *PostgreSQLViewComparer) compareSemanticTokens(tokens1, tokens2 *SemanticTokens) bool {
- // Compare SELECT items (order matters)
- if !c.compareStringArrays(tokens1.SelectItems, tokens2.SelectItems) {
- return false
- }
-
- // Compare table references (order shouldn't matter for most cases)
- if !c.compareStringArraysUnordered(tokens1.TableRefs, tokens2.TableRefs) {
- return false
- }
-
- // Compare JOIN clauses (order matters)
- if !c.compareStringArrays(tokens1.JoinClauses, tokens2.JoinClauses) {
- return false
- }
-
- // Compare WHERE clause using AST-based semantic comparison
- if !ast.CompareExpressionsSemantically(tokens1.WhereClause, tokens2.WhereClause) {
- return false
- }
-
- // Compare GROUP BY items (order shouldn't matter)
- if !c.compareStringArraysUnordered(tokens1.GroupByItems, tokens2.GroupByItems) {
- return false
- }
-
- // Compare ORDER BY items (order matters)
- if !c.compareStringArrays(tokens1.OrderByItems, tokens2.OrderByItems) {
- return false
- }
-
- return true
-}
-
-// compareStringArrays compares two string arrays (order-dependent).
-func (c *PostgreSQLViewComparer) compareStringArrays(arr1, arr2 []string) bool {
- if len(arr1) != len(arr2) {
- return false
- }
-
- for i, item1 := range arr1 {
- // Use AST-based semantic comparison for expressions
- if ast.CompareExpressionsSemantically(item1, arr2[i]) {
- continue // They're semantically equivalent
- }
-
- // Fallback to normalization for cases where AST comparison might not work
- normalized1 := c.normalizeExpression(item1)
- normalized2 := c.normalizeExpression(arr2[i])
-
- // Check if they're exactly equal after normalization
- if normalized1 == normalized2 {
- continue // They're equivalent
- }
-
- // Special handling for column references that might have different table alias usage
- if c.areSemanticallySimilarColumns(normalized1, normalized2) {
- continue // Consider them equivalent
- }
-
- // They're different
- return false
- }
-
- return true
-}
-
-// areSemanticallySimilarColumns checks if two column references are semantically similar
-// even if they use different table alias patterns (e.g., "d.dept_no" vs "dept_no").
-func (*PostgreSQLViewComparer) areSemanticallySimilarColumns(col1, col2 string) bool {
- // Handle cases where one column has table alias and the other doesn't
- // e.g., "d.dept_no" vs "dept_no"
-
- // Normalize spaces around dots first
- col1 = strings.ReplaceAll(col1, " . ", ".")
- col2 = strings.ReplaceAll(col2, " . ", ".")
- col1 = strings.ReplaceAll(col1, ". ", ".")
- col2 = strings.ReplaceAll(col2, " .", ".")
- col1 = strings.TrimSpace(col1)
- col2 = strings.TrimSpace(col2)
-
- // Split on dots to check for table alias patterns
- parts1 := strings.Split(col1, ".")
- parts2 := strings.Split(col2, ".")
-
- // If one has alias and other doesn't, compare the column names
- if len(parts1) == 2 && len(parts2) == 1 {
- // col1 has alias (table.column), col2 is just column
- return strings.TrimSpace(parts1[1]) == strings.TrimSpace(parts2[0])
- } else if len(parts1) == 1 && len(parts2) == 2 {
- // col2 has alias (table.column), col1 is just column
- return strings.TrimSpace(parts1[0]) == strings.TrimSpace(parts2[1])
- } else if len(parts1) == 2 && len(parts2) == 2 {
- // Both have aliases, compare just the column names
- return strings.TrimSpace(parts1[1]) == strings.TrimSpace(parts2[1])
- }
-
- // No special handling needed
- return false
-}
-
-// compareStringArraysUnordered compares two string arrays (order-independent).
-func (c *PostgreSQLViewComparer) compareStringArraysUnordered(arr1, arr2 []string) bool {
- if len(arr1) != len(arr2) {
- return false
- }
-
- // For each item in arr1, find a semantically matching item in arr2
- used := make([]bool, len(arr2))
-
- for _, item1 := range arr1 {
- found := false
- for j, item2 := range arr2 {
- if used[j] {
- continue
- }
-
- // Use AST-based semantic comparison first
- if ast.CompareExpressionsSemantically(item1, item2) {
- used[j] = true
- found = true
- break
- }
-
- // Fallback to normalization comparison
- norm1 := c.normalizeExpression(item1)
- norm2 := c.normalizeExpression(item2)
- if norm1 == norm2 {
- used[j] = true
- found = true
- break
- }
- }
-
- if !found {
- return false
- }
- }
-
- return true
-}
-
-// normalizeExpression normalizes SQL expressions using AST-based semantic analysis.
-func (*PostgreSQLViewComparer) normalizeExpression(expr string) string {
- if expr == "" {
- return ""
- }
-
- // Parse the expression using ANTLR PostgreSQL lexer for semantic analysis
- inputStream := antlr.NewInputStream(expr)
- lexer := pgparser.NewPostgreSQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- stream.Fill()
- tokens := stream.GetAllTokens()
-
- var normalizedTokens []string
-
- for i, token := range tokens {
- tokenText := strings.TrimSpace(token.GetText())
- tokenType := token.GetTokenType()
-
- // Skip whitespace and EOF tokens
- if isWhitespaceTokenType(tokenType) || tokenText == "<EOF>" || tokenText == ";" {
- continue
- }
-
- // Handle schema prefix removal (semantic equivalence)
- if tokenText == "public" && i+1 < len(tokens) {
- // Look ahead to see if there's a dot following
- nextToken := tokens[i+1]
- if strings.TrimSpace(nextToken.GetText()) == "." {
- continue // Skip "public" - the dot will be handled in next iteration
- }
- }
- if tokenText == "." && i > 0 {
- // Check if this dot follows a "public" that we should skip
- prevToken := tokens[i-1]
- if strings.ToLower(strings.TrimSpace(prevToken.GetText())) == "public" {
- continue // Skip the dot that follows "public"
- }
- }
-
- // Semantic normalization: normalize JOIN keywords for equivalence
- if strings.ToLower(tokenText) == "inner" {
- // Look for the next non-whitespace token
- skipInner := false
- for j := i + 1; j < len(tokens); j++ {
- nextTokenText := strings.TrimSpace(tokens[j].GetText())
- nextTokenType := tokens[j].GetTokenType()
-
- // Skip whitespace tokens
- if isWhitespaceTokenType(nextTokenType) {
- continue
- }
-
- // If next non-whitespace token is "JOIN", skip "INNER"
- if strings.ToLower(nextTokenText) == "join" {
- skipInner = true
- }
- break // Found non-whitespace token, make decision
- }
-
- if skipInner {
- continue // Skip "INNER" - we'll just use "join"
- }
- }
-
- // Normalize operator spacing and case for semantic equivalence
- normalizedToken := normalizeTokenForSemantics(tokenText)
- normalizedTokens = append(normalizedTokens, normalizedToken)
- }
-
- // Join with single spaces and clean up
- normalized := strings.Join(normalizedTokens, " ")
- normalized = strings.TrimSpace(normalized)
-
- return normalized
-}
-
-// normalizeTokenForSemantics normalizes individual tokens for semantic equivalence.
-func normalizeTokenForSemantics(token string) string {
- // Don't normalize quoted identifiers
- if isQuotedIdentifierText(token) {
- return token
- }
-
- // Convert to lowercase for case-insensitive comparison
- token = strings.ToLower(token)
-
- // Normalize common operator variations (semantic equivalence)
- switch token {
- case "=":
- return "=" // Already normalized
- case "!=", "<>":
- return "<>" // Standardize inequality operator
- case "&&":
- return "and" // Convert && to AND for PostgreSQL semantic equivalence
- case "||":
- // Context-sensitive: could be OR or concatenation
- // For now, keep as-is since we don't have full context
- return token
- default:
- return token
- }
-}
-
-// isWhitespaceTokenType checks if the token type represents whitespace.
-func isWhitespaceTokenType(tokenType int) bool {
- return tokenType == pgparser.PostgreSQLLexerWhitespace ||
- tokenType == pgparser.PostgreSQLLexerNewline
-}
-
-// isQuotedIdentifierText checks if the text represents a quoted identifier.
-func isQuotedIdentifierText(text string) bool {
- return len(text) >= 2 && text[0] == '"' && text[len(text)-1] == '"'
-}