Skip to content

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

feat: support enum type for PG SDL (#18074)

rebelice926d4886f9ca
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.go
index 2d0ffb0bab..e3d42413b0 100644
--- a/backend/plugin/schema/differ.go
+++ b/backend/plugin/schema/differ.go
@@ -243,6 +243,8 @@ 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
}
// EventDiff represents changes to an event.
@@ -265,6 +267,7 @@ const (
CommentObjectTypeFunction CommentObjectType = "FUNCTION"
CommentObjectTypeSequence CommentObjectType = "SEQUENCE"
CommentObjectTypeIndex CommentObjectType = "INDEX"
+ CommentObjectTypeType CommentObjectType = "TYPE"
)
// CommentDiff represents changes to database object comments.
diff --git a/backend/plugin/schema/pg/commented_enum_test.go b/backend/plugin/schema/pg/commented_enum_test.go
new file mode 100644
index 0000000000..4dea632e5c
--- /dev/null
+++ b/backend/plugin/schema/pg/commented_enum_test.go
@@ -0,0 +1,220 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// TestCommentedOutEnumType tests that commented-out enum type definitions are ignored
+func TestCommentedOutEnumType(t *testing.T) {
+ // This reproduces the issue from the screenshot where commented SQL causes syntax error
+ sdl := `-- Enum type: Order status
+-- CREATE TYPE "public"."order_status_enum" AS ENUM (
+-- 'draft',
+-- 'pending',
+-- 'processing',
+-- 'shipped',
+-- 'delivered',
+-- 'cancelled',
+-- 'refunded'
+-- );
+--
+-- COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';
+--
+-- Enum type: User role
+CREATE TYPE "public"."user_role" AS ENUM (
+ 'admin',
+ 'user'
+);
+`
+
+ // Parse the SDL
+ chunks, err := ChunkSDLText(sdl)
+ require.NoError(t, err, "Should successfully parse SDL with commented-out enum type")
+ require.NotNil(t, chunks)
+
+ // Should only have 1 enum type (user_role), not 2
+ // The commented-out order_status_enum should be ignored
+ require.Equal(t, 1, len(chunks.EnumTypes), "Should only parse non-commented enum types")
+
+ // Verify we got the correct enum type
+ _, exists := chunks.EnumTypes["public.user_role"]
+ require.True(t, exists, "Should have parsed user_role enum type")
+
+ // Verify the commented-out enum type is NOT parsed
+ _, exists = chunks.EnumTypes["public.order_status_enum"]
+ require.False(t, exists, "Should NOT parse commented-out order_status_enum")
+}
+
+// TestMultipleCommentedOutStatements tests various commented-out SQL patterns
+func TestMultipleCommentedOutStatements(t *testing.T) {
+ sdl := `-- This is a comment
+-- CREATE TYPE "public"."status1" AS ENUM ('a', 'b');
+
+CREATE TYPE "public"."status2" AS ENUM ('x', 'y');
+
+-- CREATE TYPE "public"."status3" AS ENUM (
+-- 'foo',
+-- 'bar'
+-- );
+
+CREATE TABLE "public"."users" (
+ id INTEGER PRIMARY KEY,
+ status "public"."status2"
+);
+
+-- CREATE TABLE "public"."orders" (
+-- id INTEGER PRIMARY KEY
+-- );
+`
+
+ chunks, err := ChunkSDLText(sdl)
+ require.NoError(t, err, "Should parse SDL with mixed commented and uncommented statements")
+ require.NotNil(t, chunks)
+
+ // Should only have 1 enum type
+ require.Equal(t, 1, len(chunks.EnumTypes), "Should only parse uncommented enum type")
+
+ // Should only have 1 table
+ require.Equal(t, 1, len(chunks.Tables), "Should only parse uncommented table")
+
+ // Verify we got the correct objects
+ _, exists := chunks.EnumTypes["public.status2"]
+ require.True(t, exists, "Should have parsed status2")
+
+ _, exists = chunks.Tables["public.users"]
+ require.True(t, exists, "Should have parsed users table")
+}
+
+// TestCommentedEnumFromFile simulates reading SDL from a file with commented-out enum
+func TestCommentedEnumFromFile(t *testing.T) {
+ // Simulate exact content from user's screenshot
+ fileContent := `-- Enum type: Order status
+-- CREATE TYPE "public"."order_status_enum" AS ENUM (
+-- 'draft',
+-- 'pending',
+-- 'processing',
+-- 'shipped',
+-- 'delivered',
+-- 'cancelled',
+-- 'refunded'
+-- );
+--
+-- COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';
+--
+-- Enum type: User role
+CREATE TYPE "public"."user_role_enum" AS ENUM (
+ 'admin',
+ 'moderator',
+ 'user',
+ 'guest'
+);
+
+COMMENT ON TYPE "public"."user_role_enum" IS 'User role types';
+`
+
+ // Test ChunkSDLText
+ chunks, err := ChunkSDLText(fileContent)
+ require.NoError(t, err, "Failed to chunk SDL: %v", err)
+ require.NotNil(t, chunks)
+
+ t.Logf("Number of enum types parsed: %d", len(chunks.EnumTypes))
+ for k := range chunks.EnumTypes {
+ t.Logf(" - %s", k)
+ }
+
+ // Should only have 1 enum type
+ require.Equal(t, 1, len(chunks.EnumTypes), "Should only parse non-commented enum types")
+
+ // Test GetSDLDiff
+ previousSDL := ""
+ currentSDL := fileContent
+
+ diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)
+ require.NoError(t, err, "GetSDLDiff should not fail with commented SQL: %v", err)
+ require.NotNil(t, diff)
+
+ // Should have 1 enum type creation
+ require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")
+ require.Equal(t, "user_role_enum", diff.EnumTypeChanges[0].EnumTypeName)
+}
+
+// TestMultiFileScenarioWithComments simulates reading multiple files
+func TestMultiFileScenarioWithComments(t *testing.T) {
+ // File 1: types.sql with commented and uncommented enums
+ typesFile := `-- Commented out enum
+-- CREATE TYPE "public"."old_status" AS ENUM ('a', 'b');
+
+CREATE TYPE "public"."current_status" AS ENUM ('active', 'inactive');
+`
+
+ // File 2: tables.sql
+ tablesFile := `CREATE TABLE "public"."users" (
+ id SERIAL PRIMARY KEY,
+ status "public"."current_status" NOT NULL
+);
+`
+
+ // Concatenate files (simulating what might happen in multi-file mode)
+ combinedSDL := typesFile + "\n" + tablesFile
+
+ chunks, err := ChunkSDLText(combinedSDL)
+ require.NoError(t, err, "Should parse combined SDL from multiple files")
+ require.NotNil(t, chunks)
+
+ // Should only have 1 enum type
+ require.Equal(t, 1, len(chunks.EnumTypes))
+
+ // Should have 1 table
+ require.Equal(t, 1, len(chunks.Tables))
+}
+
+// TestExactContentFromScreenshot tests the exact content from user's screenshot
+func TestExactContentFromScreenshot(t *testing.T) {
+ // Exact content from screenshot (lines 18188-18202)
+ sdl := `-- Enum type: Order status
+-- CREATE TYPE "public"."order_status_enum" AS ENUM (
+-- 'draft',
+-- 'pending',
+-- 'processing',
+-- 'shipped',
+-- 'delivered',
+-- 'cancelled',
+-- 'refunded'
+-- );
+--
+-- COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';
+--
+-- Enum type: User role
+CREATE TYPE "public"."user_role_enum" AS ENUM (
+ 'admin',
+ 'user'
+);
+`
+
+ // This should parse without errors
+ chunks, err := ChunkSDLText(sdl)
+ if err != nil {
+ t.Fatalf("Failed to parse SDL with commented enum: %v\nSDL content:\n%s", err, sdl)
+ }
+ require.NotNil(t, chunks)
+
+ // Verify only the uncommented enum was parsed
+ require.Equal(t, 1, len(chunks.EnumTypes), "Should parse only user_role_enum")
+
+ _, exists := chunks.EnumTypes["public.user_role_enum"]
+ require.True(t, exists, "Should have user_role_enum")
+}
+
+// TestCommentWithSpecialQuotes tests comments with different quote patterns
+func TestCommentWithSpecialQuotes(t *testing.T) {
+ sdl := `-- COMMENT ON TYPE "public"."order_status_enum" IS 'Order lifecycle status values';
+CREATE TYPE "public"."status" AS ENUM ('active');
+`
+
+ chunks, err := ChunkSDLText(sdl)
+ require.NoError(t, err, "Should handle commented COMMENT ON TYPE")
+ require.NotNil(t, chunks)
+ require.Equal(t, 1, len(chunks.EnumTypes))
+}
diff --git a/backend/plugin/schema/pg/enum_special_chars_test.go b/backend/plugin/schema/pg/enum_special_chars_test.go
new file mode 100644
index 0000000000..f0c6dd6c4f
--- /dev/null
+++ b/backend/plugin/schema/pg/enum_special_chars_test.go
@@ -0,0 +1,124 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
+
+// TestEnumTypeWithSingleQuotes tests that enum values containing single quotes are properly escaped
+func TestEnumTypeWithSingleQuotes(t *testing.T) {
+ // Create database metadata with enum type that has single quotes in values
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {
+ Name: "status_with_quotes",
+ Values: []string{
+ "active",
+ "don't use", // Contains single quote
+ "can't access", // Contains single quote
+ "won't work", // Contains single quote
+ },
+ },
+ },
+ },
+ },
+ }
+
+ // Generate single-file SDL
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err, "Failed to generate SDL")
+ require.Contains(t, sdl, "don''t use", "Single quotes should be escaped as double single quotes")
+ require.Contains(t, sdl, "can''t access", "Single quotes should be escaped as double single quotes")
+ require.Contains(t, sdl, "won''t work", "Single quotes should be escaped as double single quotes")
+
+ // Parse the generated SDL - it should parse without errors
+ chunks, err := ChunkSDLText(sdl)
+ require.NoError(t, err, "Generated SDL with escaped quotes should parse correctly")
+ require.Equal(t, 1, len(chunks.EnumTypes), "Should have 1 enum type")
+
+ // Verify the enum type was parsed
+ _, exists := chunks.EnumTypes["public.status_with_quotes"]
+ require.True(t, exists, "Should have parsed status_with_quotes enum type")
+}
+
+// TestEnumTypeWithOtherSpecialChars tests enum values with various special characters
+func TestEnumTypeWithOtherSpecialChars(t *testing.T) {
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {
+ Name: "special_chars",
+ Values: []string{
+ "normal",
+ "with space",
+ "with\ttab",
+ "with\nnewline",
+ "with-dash",
+ "with_underscore",
+ "with.dot",
+ },
+ },
+ },
+ },
+ },
+ }
+
+ // Generate SDL
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err, "Failed to generate SDL")
+
+ // Parse the generated SDL - should work for all special characters
+ chunks, err := ChunkSDLText(sdl)
+ require.NoError(t, err, "Generated SDL with special characters should parse correctly")
+ require.Equal(t, 1, len(chunks.EnumTypes), "Should have 1 enum type")
+}
+
+// TestEnumTypeRoundTrip tests that enum values survive a full round-trip through SDL
+func TestEnumTypeRoundTrip(t *testing.T) {
+ originalValues := []string{
+ "active",
+ "inactive",
+ "don't care",
+ "user's choice",
+ "it's working",
+ }
+
+ metadata := &storepb.DatabaseSchemaMetadata{
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ EnumTypes: []*storepb.EnumTypeMetadata{
+ {
+ Name: "roundtrip_status",
+ Values: originalValues,
+ },
+ },
+ },
+ },
+ }
+
+ // Generate SDL
+ sdl, err := getSDLFormat(metadata)
+ require.NoError(t, err, "Failed to generate SDL")
+
+ // Parse it back
+ chunks, err := ChunkSDLText(sdl)
+ require.NoError(t, err, "Failed to parse generated SDL")
+ require.Equal(t, 1, len(chunks.EnumTypes), "Should have 1 enum type")
+
+ // Verify the enum exists
+ _, exists := chunks.EnumTypes["public.roundtrip_status"]
+ require.True(t, exists, "Should have parsed roundtrip_status enum type")
+
+ // Note: We can't easily verify the exact values from the parsed chunks
+ // because the parser doesn't expose the enum values directly.
+ // The important thing is that it parses without errors.
+}
diff --git a/backend/plugin/schema/pg/enum_type_test.go b/backend/plugin/schema/pg/enum_type_test.go
new file mode 100644
index 0000000000..6734e71429
--- /dev/null
+++ b/backend/plugin/schema/pg/enum_type_test.go
@@ -0,0 +1,437 @@
+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.NewDatabaseSchema(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ newSchema := model.NewDatabaseSchema(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.NewDatabaseSchema(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)
+ newSchema := model.NewDatabaseSchema(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")
+ })
+
+ t.Run("ADD Enum via SDL chunks", func(t *testing.T) {
+ // This tests adding an enum type via SDL diff
+
+ // Previous SDL (no enum)
+ previousSDL := `CREATE TABLE "public"."users" (
+ "id" integer NOT NULL,
+ "name" text
+);`
+
+ // Current SDL (with new enum type)
+ currentSDL := `CREATE TYPE "public"."status_type" AS ENUM (
+ 'active',
+ 'inactive',
+ 'pending'
+);
+
+CREATE TABLE "public"."users" (
+ "id" integer NOT NULL,
+ "name" text
+);`
+
+ // Get SDL diff (simulates bb rollout behavior)
+ diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ // Debug: log all diffs
+ t.Logf("EnumTypeChanges: %d", len(diff.EnumTypeChanges))
+ for i, enumDiff := range diff.EnumTypeChanges {
+ t.Logf(" Enum[%d]: Action=%v, Schema=%s, Name=%s, HasNewAST=%v",
+ i, enumDiff.Action, enumDiff.SchemaName, enumDiff.EnumTypeName,
+ enumDiff.NewASTNode != nil)
+ }
+
+ // Verify we have an EnumTypeDiff with CREATE action
+ require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")
+ enumDiff := diff.EnumTypeChanges[0]
+ require.Equal(t, schema.MetadataDiffActionCreate, enumDiff.Action)
+ require.Equal(t, "public", enumDiff.SchemaName)
+ require.Equal(t, "status_type", enumDiff.EnumTypeName)
+ require.NotNil(t, enumDiff.NewASTNode, "NewASTNode should be set for SDL mode")
+
+ // Generate migration
+ migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
+ require.NoError(t, err)
+ t.Logf("Generated migration:\n%s", migration)
+
+ // Verify migration creates the enum type
+ require.Contains(t, migration, "CREATE TYPE")
+ require.Contains(t, migration, "status_type")
+ require.Contains(t, migration, "'active'")
+ require.Contains(t, migration, "'inactive'")
+ require.Contains(t, migration, "'pending'")
+ })
+
+ t.Run("REMOVE Enum via SDL chunks", func(t *testing.T) {
+ // This tests removing an enum type via SDL diff
+
+ // Previous SDL (with enum)
+ previousSDL := `CREATE TYPE "public"."status_type" AS ENUM (
+ 'active',
+ 'inactive'
+);
+
+CREATE TABLE "public"."users" (
+ "id" integer NOT NULL,
+ "name" text
+);`
+
+ // Current SDL (enum removed)
+ currentSDL := `CREATE TABLE "public"."users" (
+ "id" integer NOT NULL,
+ "name" text
+);`
+
+ // Get SDL diff
+ diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ // Verify we have an EnumTypeDiff with DROP action
+ require.Equal(t, 1, len(diff.EnumTypeChanges), "Should have 1 enum type change")
+ enumDiff := diff.EnumTypeChanges[0]
+ require.Equal(t, schema.MetadataDiffActionDrop, enumDiff.Action)
+ require.Equal(t, "public", enumDiff.SchemaName)
+ require.Equal(t, "status_type", enumDiff.EnumTypeName)
+
+ // Generate migration
+ migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
+ require.NoError(t, err)
+ t.Logf("Generated migration:\n%s", migration)
+
+ // Verify migration drops the enum type
+ require.Contains(t, migration, "DROP TYPE")
+ require.Contains(t, migration, "status_type")
+ })
+
+ t.Run("MODIFY Enum via SDL chunks (DROP + CREATE)", func(t *testing.T) {
+ // This tests modifying an enum type via SDL diff
+ // PostgreSQL doesn't support renaming enum values, so we use DROP + CREATE pattern
+
+ // Previous SDL (with original values)
+ previousSDL := `CREATE TYPE "public"."status_type" AS ENUM (
+ 'active',
+ 'inactive'
+);`
+
+ // Current SDL (with modified values)
+ currentSDL := `CREATE TYPE "public"."status_type" AS ENUM (
+ 'active',
+ 'inactive',
+ 'pending'
+);`
+
+ // Get SDL diff
+ diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ // Verify we have two EnumTypeDiff entries: DROP then CREATE
+ require.Equal(t, 2, len(diff.EnumTypeChanges), "Should have 2 enum type changes (DROP + CREATE)")
+ dropDiff := diff.EnumTypeChanges[0]
+ createDiff := diff.EnumTypeChanges[1]
+
+ require.Equal(t, schema.MetadataDiffActionDrop, dropDiff.Action)
+ require.Equal(t, "public", dropDiff.SchemaName)
+ require.Equal(t, "status_type", dropDiff.EnumTypeName)
+
+ require.Equal(t, schema.MetadataDiffActionCreate, createDiff.Action)
+ require.Equal(t, "public", createDiff.SchemaName)
+ require.Equal(t, "status_type", createDiff.EnumTypeName)
+ require.NotNil(t, createDiff.NewASTNode, "NewASTNode should be set for SDL mode")
+
+ // Generate migration
+ migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
+ require.NoError(t, err)
+ t.Logf("Generated migration:\n%s", migration)
+
+ // Verify migration has both DROP and CREATE
+ require.Contains(t, migration, "DROP TYPE")
+ require.Contains(t, migration, "CREATE TYPE")
+ require.Contains(t, migration, "'pending'")
+ })
+}
+
+// TestEnumTypeCommentChanges tests that enum type comment changes are properly detected and generated
+func TestEnumTypeCommentChanges(t *testing.T) {
+ // Previous SDL (enum without comment)
+ previousSDL := `CREATE TYPE "public"."status_type" AS ENUM (
+ 'active',
+ 'inactive'
+);`
+
+ // Current SDL (enum with comment)
+ currentSDL := `CREATE TYPE "public"."status_type" AS ENUM (
+ 'active',
+ 'inactive'
+);
+
+COMMENT ON TYPE "public"."status_type" IS 'User status enum';`
+
+ // Get SDL diff
+ diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ // Verify we have a comment change
+ hasCommentDiff := false
+ for _, commentDiff := range diff.CommentChanges {
+ if commentDiff.ObjectType == schema.CommentObjectTypeType &&
+ commentDiff.ObjectName == "status_type" {
+ hasCommentDiff = true
+ break
+ }
+ }
+ require.True(t, hasCommentDiff, "Should have comment diff for enum type")
+
+ // Generate migration
+ migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)
+ require.NoError(t, err)
+ t.Logf("Generated migration:\n%s", migration)
+
+ // Verify migration includes comment
+ require.Contains(t, migration, "COMMENT ON TYPE")
+ require.Contains(t, migration, "status_type")
+ require.Contains(t, migration, "User status enum")
+}
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.go
index 5655f9c732..454cdc9378 100644
--- a/backend/plugin/schema/pg/generate_migration.go
+++ b/backend/plugin/schema/pg/generate_migration.go
@@ -634,8 +634,17 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error
// Create enum types (before tables as they might be used in column definitions)
for _, enumDiff := range diff.EnumTypeChanges {
if enumDiff.Action == schema.MetadataDiffActionCreate {
- if err := writeCreateEnumType(buf, enumDiff.SchemaName, enumDiff.NewEnumType); err != nil {
- return err
+ // Support both metadata and AST-only modes
+ if enumDiff.NewEnumType != nil {
+ // Metadata mode: use enum type metadata
+ if err := writeCreateEnumType(buf, enumDiff.SchemaName, enumDiff.NewEnumType); err != nil {
+ return err
+ }
+ } else if enumDiff.NewASTNode != nil {
+ // AST-only mode: extract SQL from AST node
+ if err := writeMigrationEnumTypeFromAST(buf, enumDiff.NewASTNode); err != nil {
+ return err
+ }
}
}
}
@@ -1676,6 +1685,49 @@ func writeMigrationSequenceFromAST(out *strings.Builder, astNode any) error {
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 {
@@ -3466,6 +3518,10 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDi
}
writeCommentOnIndex(buf, commentDiff.SchemaName, indexName, newComment)
+ case schema.CommentObjectTypeType:
+ // COMMENT ON TYPE (for enum types)
+ writeCommentOnType(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
+
default:
// Unknown object type, skip
continue
diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.go
index 7e974421db..d9abaa7473 100644
--- a/backend/plugin/schema/pg/get_database_definition.go
+++ b/backend/plugin/schema/pg/get_database_definition.go
@@ -92,6 +92,15 @@ func GetDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *storepb.Da
if err := writeEnum(&buf, schema.Name, enum); err != nil {
return "", err
}
+ if _, err := buf.WriteString(";\n\n"); err != nil {
+ return "", err
+ }
+ // Write enum comment if present
+ if len(enum.Comment) > 0 {
+ if err := writeEnumComment(&buf, schema.Name, enum); err != nil {
+ return "", err
+ }
+ }
}
}
@@ -362,6 +371,15 @@ func GetSchemaDefinition(schema *storepb.SchemaMetadata) (string, error) {
if err := writeEnum(&buf, schema.Name, enum); err != nil {
return "", err
}
+ if _, err := buf.WriteString(";\n\n"); err != nil {
+ return "", err
+ }
+ // Write enum comment if present
+ if len(enum.Comment) > 0 {
+ if err := writeEnumComment(&buf, schema.Name, enum); err != nil {
+ return "", err
+ }
+ }
}
// Build the graph for topological sort.
@@ -786,7 +804,7 @@ func writeEnum(out io.Writer, schema string, enum *storepb.EnumTypeMetadata) err
if _, err := io.WriteString(out, ` '`); err != nil {
return err
}
- if _, err := io.WriteString(out, value); err != nil {
+ if _, err := io.WriteString(out, escapeSingleQuote(value)); err != nil {
return err
}
if _, err := io.WriteString(out, `'`); err != nil {
@@ -794,16 +812,10 @@ func writeEnum(out io.Writer, schema string, enum *storepb.EnumTypeMetadata) err
}
}
- if _, err := io.WriteString(out, "\n);\n\n"); err != nil {
+ if _, err := io.WriteString(out, "\n)"); err != nil {
return err
}
- if len(enum.Comment) > 0 {
- if err := writeEnumComment(out, schema, enum); err != nil {
- return err
- }
- }
-
return nil
}
@@ -2253,6 +2265,31 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {
}
}
+ // Write all enum types before sequences and tables
+ for _, schema := range metadata.Schemas {
+ if schema.SkipDump {
+ continue
+ }
+ for _, enumType := range schema.EnumTypes {
+ if enumType.SkipDump {
+ continue
+ }
+ if err := writeEnum(&buf, schema.Name, enumType); err != nil {
+ return "", err
+ }
+ if _, err := buf.WriteString(";\n\n"); err != nil {
+ return "", err
+ }
+
+ // Write enum type comment if present
+ if len(enumType.Comment) > 0 {
+ if err := writeEnumComment(&buf, schema.Name, enumType); err != nil {
+ return "", err
+ }
+ }
+ }
+ }
+
// Write all sequences before tables to ensure they exist before any table references them.
// Skip sequences that belong to serial or identity columns as they will be implicitly created.
sequenceOwnershipMap := make(map[string][]*storepb.SequenceMetadata)
@@ -3460,6 +3497,46 @@ func GetMultiFileDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *s
})
}
+ // Generate a single file for all enum types in this schema
+ if len(schemaMetadata.EnumTypes) > 0 {
+ var buf strings.Builder
+ hasEnumTypes := false
+ for i, enumType := range schemaMetadata.EnumTypes {
+ if enumType.SkipDump {
+ continue
+ }
+
+ if hasEnumTypes {
+ buf.WriteString("\n")
+ }
+ hasEnumTypes = true
+
+ if i > 0 {
+ buf.WriteString("\n")
+ }
+
+ if err := writeEnum(&buf, schemaName, enumType); err != nil {
+ return nil, errors.Wrapf(err, "failed to generate enum type SDL for %s.%s", schemaName, enumType.Name)
+ }
+ buf.WriteString(";\n")
+
+ // Add enum type comment if present
+ if len(enumType.Comment) > 0 {
+ buf.WriteString("\n")
+ if err := writeEnumComment(&buf, schemaName, enumType); err != nil {
+ return nil, errors.Wrapf(err, "failed to generate enum type comment for %s.%s", schemaName, enumType.Name)
+ }
+ }
+ }
+
+ if hasEnumTypes {
+ files = append(files, schema.File{
+ Name: fmt.Sprintf("schemas/%s/types.sql", schemaName),
+ Content: buf.String(),
+ })
+ }
+ }
+
// Generate a single file for all independent sequences (no owner) in this schema
if len(independentSequences) > 0 {
var buf strings.Builder
diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.go
index aa1a4c09fd..4e3aef240f 100644
--- a/backend/plugin/schema/pg/get_sdl_diff.go
+++ b/backend/plugin/schema/pg/get_sdl_diff.go
@@ -100,6 +100,9 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previ
// Process sequence changes
processSequenceChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)
+ // Process enum type changes
+ processEnumTypeChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)
+
// Process explicit schema changes (CREATE SCHEMA statements)
processSchemaChanges(currentChunks, previousChunks, currentSchema, diff)
@@ -123,6 +126,7 @@ func ChunkSDLText(sdlText string) (*schema.SDLChunks, error) {
Indexes: make(map[string]*schema.SDLChunk),
Sequences: make(map[string]*schema.SDLChunk),
Schemas: make(map[string]*schema.SDLChunk),
+ EnumTypes: make(map[string]*schema.SDLChunk),
ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),
IndexComments: make(map[string]map[string]antlr.ParserRuleContext),
}, nil
@@ -143,6 +147,7 @@ func ChunkSDLText(sdlText string) (*schema.SDLChunks, error) {
Indexes: make(map[string]*schema.SDLChunk),
Sequences: make(map[string]*schema.SDLChunk),
Schemas: make(map[string]*schema.SDLChunk),
+ EnumTypes: make(map[string]*schema.SDLChunk),
ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),
IndexComments: make(map[string]map[string]antlr.ParserRuleContext),
},
@@ -243,6 +248,40 @@ func (l *sdlChunkExtractor) EnterAlterseqstmt(ctx *parser.AlterseqstmtContext) {
}
}
+// EnterDefinestmt handles CREATE TYPE AS ENUM statements
+func (l *sdlChunkExtractor) EnterDefinestmt(ctx *parser.DefinestmtContext) {
+ // Check if this is CREATE TYPE AS ENUM
+ if ctx.CREATE() == nil || ctx.TYPE_P() == nil || ctx.AS() == nil || ctx.ENUM_P() == nil {
+ return
+ }
+
+ // Extract type name
+ typeNames := ctx.AllAny_name()
+ if len(typeNames) == 0 {
+ return
+ }
+
+ // Get the enum type name (first Any_name)
+ typeName := typeNames[0]
+ identifier := pgparser.NormalizePostgreSQLAnyName(typeName)
+ identifierStr := strings.Join(identifier, ".")
+
+ // Ensure schema.enumName format (default to "public" if no schema specified)
+ var schemaQualifiedName string
+ if strings.Contains(identifierStr, ".") {
+ schemaQualifiedName = identifierStr
+ } else {
+ schemaQualifiedName = "public." + identifierStr
+ }
+
+ chunk := &schema.SDLChunk{
+ Identifier: schemaQualifiedName,
+ ASTNode: ctx,
+ }
+
+ l.chunks.EnumTypes[schemaQualifiedName] = chunk
+}
+
func (l *sdlChunkExtractor) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {
funcNameCtx := ctx.Func_name()
if funcNameCtx == nil {
@@ -483,6 +522,32 @@ func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {
return
}
+ // Check for TYPE comment: COMMENT ON TYPE typename IS comment_text
+ if ctx.TYPE_P() != nil {
+ // Extract typename from the third child (TYPE_P is second, typename is third)
+ if ctx.GetChildCount() >= 4 {
+ child := ctx.GetChild(3)
+ if typenameCtx, ok := child.(*parser.TypenameContext); ok {
+ // Extract schema.type from typename
+ identifier := extractSchemaAndTypeFromTypename(typenameCtx)
+ if identifier != "" {
+ if chunk, exists := l.chunks.EnumTypes[identifier]; exists {
+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)
+ } else {
+ // Create a new chunk for the comment (type may not have CREATE TYPE statement)
+ chunk := &schema.SDLChunk{
+ Identifier: identifier,
+ ASTNode: nil,
+ CommentStatements: []antlr.ParserRuleContext{ctx},
+ }
+ l.chunks.EnumTypes[identifier] = chunk
+ }
+ }
+ }
+ }
+ return
+ }
+
// Check for object_type_any_name: TABLE, SEQUENCE, VIEW, INDEX, etc.
if ctx.Object_type_any_name() != nil && ctx.Any_name() != nil {
objectType := ctx.Object_type_any_name().GetText()
@@ -564,8 +629,20 @@ func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {
}
l.chunks.Indexes[identifier] = chunk
}
+ case "TYPE":
+ // Handle COMMENT ON TYPE (for enum types)
+ if chunk, exists := l.chunks.EnumTypes[identifier]; exists {
+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)
+ } else {
+ chunk := &schema.SDLChunk{
+ Identifier: identifier,
+ ASTNode: nil,
+ CommentStatements: []antlr.ParserRuleContext{ctx},
+ }
+ l.chunks.EnumTypes[identifier] = chunk
+ }
default:
- // Unsupported object type for comment tracking (e.g., MATERIALIZED VIEW, FOREIGN TABLE)
+ // Unsupported object type for comment tracking (e.g., FOREIGN TABLE)
// We skip these for now
}
return
@@ -914,6 +991,64 @@ func parseIdentifier(identifier string) (schemaName, objectName string) {
return "public", identifier
}
+// extractSchemaAndTypeFromTypename extracts schema.type identifier from a Typename context
+// Used for parsing COMMENT ON TYPE statements
+func extractSchemaAndTypeFromTypename(typenameCtx *parser.TypenameContext) string {
+ if typenameCtx == nil {
+ return ""
+ }
+
+ // Helper function to normalize a PostgreSQL identifier string
+ normalizeIdentifier := func(text string) string {
+ if len(text) >= 2 && text[0] == '"' && text[len(text)-1] == '"' {
+ // Quoted identifier - preserve case but remove quotes
+ return text[1 : len(text)-1]
+ }
+ // Unquoted identifier - convert to lowercase
+ return strings.ToLower(text)
+ }
+
+ // Navigate to Simpletypename -> Generictype
+ if typenameCtx.GetChildCount() >= 1 {
+ if simpleTypeCtx, ok := typenameCtx.GetChild(0).(*parser.SimpletypenameContext); ok {
+ if simpleTypeCtx.GetChildCount() >= 1 {
+ if genericTypeCtx, ok := simpleTypeCtx.GetChild(0).(*parser.GenerictypeContext); ok {
+ // Generictype contains Type_function_name (schema) and Attrs (.typename)
+ var schemaName string
+ var typeName string
+
+ if genericTypeCtx.GetChildCount() >= 1 {
+ if tfnCtx, ok := genericTypeCtx.GetChild(0).(*parser.Type_function_nameContext); ok {
+ schemaName = normalizeIdentifier(tfnCtx.GetText())
+ }
+ }
+
+ if genericTypeCtx.GetChildCount() >= 2 {
+ if attrsCtx, ok := genericTypeCtx.GetChild(1).(*parser.AttrsContext); ok {
+ // Attrs contains ".typename", need to extract the typename
+ attrsText := attrsCtx.GetText()
+ // Remove leading dot and normalize
+ if len(attrsText) > 1 && attrsText[0] == '.' {
+ typeName = normalizeIdentifier(attrsText[1:])
+ }
+ }
+ }
+
+ if schemaName != "" && typeName != "" {
+ return schemaName + "." + typeName
+ }
+ // If only Type_function_name is present (no attrs), treat it as the type name with public schema
+ if schemaName != "" && typeName == "" {
+ return "public." + schemaName
+ }
+ }
+ }
+ }
+ }
+
+ return ""
+}
+
// extractAlterTexts extracts and concatenates text from a list of ALTER statement nodes
func extractAlterTexts(alterNodes []antlr.ParserRuleContext) string {
if len(alterNodes) == 0 {
@@ -2595,6 +2730,12 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchema
return errors.Wrap(err, "failed to apply materialized view changes")
}
+ // Process enum type changes: apply minimal changes to enum type chunks
+ err = applyEnumTypeChangesToChunks(previousChunks, currentSchema, previousSchema)
+ if err != nil {
+ return errors.Wrap(err, "failed to apply enum type changes")
+ }
+
// Process column comment changes: sync column comments based on metadata
err = applyColumnCommentChanges(previousChunks, currentSchema, previousSchema)
if err != nil {
@@ -3256,6 +3397,14 @@ func buildCurrentDatabaseSDLChunks(currentSchema *model.DatabaseSchema) (*curren
sdlChunks.comments[identifier] = []string{commentText}
}
}
+ for identifier, chunk := range currentSDLChunks.EnumTypes {
+ sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())
+ // Store comment text for usability check
+ commentText := extractCommentTextFromChunk(chunk)
+ if commentText != "" {
+ sdlChunks.comments[identifier] = []string{commentText}
+ }
+ }
return sdlChunks, nil
}
@@ -5126,6 +5275,7 @@ func processCommentChanges(currentChunks, previousChunks *schema.SDLChunks, curr
processObjectComments(currentChunks.MaterializedViews, previousChunks.MaterializedViews, schema.CommentObjectTypeMaterializedView, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Functions, previousChunks.Functions, schema.CommentObjectTypeFunction, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Sequences, previousChunks.Sequences, schema.CommentObjectTypeSequence, createdObjects, droppedObjects, currentDBSDLChunks, diff)
+ processObjectComments(currentChunks.EnumTypes, previousChunks.EnumTypes, schema.CommentObjectTypeType, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Indexes, previousChunks.Indexes, schema.CommentObjectTypeIndex, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Schemas, previousChunks.Schemas, schema.CommentObjectTypeSchema, createdObjects, droppedObjects, currentDBSDLChunks, diff)
@@ -5172,6 +5322,13 @@ func buildCreatedObjectsSet(diff *schema.MetadataDiff) map[string]bool {
}
}
+ for _, enumDiff := range diff.EnumTypeChanges {
+ if enumDiff.Action == schema.MetadataDiffActionCreate {
+ identifier := enumDiff.SchemaName + "." + enumDiff.EnumTypeName
+ created[identifier] = true
+ }
+ }
+
return created
}
@@ -5214,6 +5371,13 @@ func buildDroppedObjectsSet(diff *schema.MetadataDiff) map[string]bool {
}
}
+ for _, enumDiff := range diff.EnumTypeChanges {
+ if enumDiff.Action == schema.MetadataDiffActionDrop {
+ identifier := enumDiff.SchemaName + "." + enumDiff.EnumTypeName
+ dropped[identifier] = true
+ }
+ }
+
return dropped
}
@@ -5872,6 +6036,255 @@ func generateCommentOnMaterializedViewSQL(schemaName, mvName, comment string) st
return fmt.Sprintf("COMMENT ON MATERIALIZED VIEW \"%s\".\"%s\" IS '%s';", schemaName, mvName, escapedComment)
}
+// applyEnumTypeChangesToChunks applies minimal changes to enum type chunks based on schema metadata
+func applyEnumTypeChangesToChunks(previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema) error {
+ if currentSchema == nil || previousSchema == nil || previousChunks == nil {
+ return nil
+ }
+
+ // Get enum type differences by comparing schema metadata
+ currentMetadata := currentSchema.GetMetadata()
+ previousMetadata := previousSchema.GetMetadata()
+ if currentMetadata == nil || previousMetadata == nil {
+ return nil
+ }
+
+ // Build enum type maps for current and previous schemas
+ currentEnumTypes := make(map[string]*storepb.EnumTypeMetadata)
+ previousEnumTypes := make(map[string]*storepb.EnumTypeMetadata)
+
+ // Collect all enum types from current schema
+ for _, schema := range currentMetadata.Schemas {
+ for _, enumType := range schema.EnumTypes {
+ enumKey := schema.Name + "." + enumType.Name
+ currentEnumTypes[enumKey] = enumType
+ }
+ }
+
+ // Collect all enum types from previous schema
+ for _, schema := range previousMetadata.Schemas {
+ for _, enumType := range schema.EnumTypes {
+ enumKey := schema.Name + "." + enumType.Name
+ previousEnumTypes[enumKey] = enumType
+ }
+ }
+
+ // Process enum type additions: create new enum type chunks
+ for enumKey, currentEnum := range currentEnumTypes {
+ if _, exists := previousEnumTypes[enumKey]; !exists {
+ // New enum type - create a chunk for it
+ err := createEnumTypeChunk(previousChunks, currentEnum, enumKey)
+ if err != nil {
+ return errors.Wrapf(err, "failed to create enum type chunk for %s", enumKey)
+ }
+ }
+ }
+
+ // Process enum type modifications: update existing chunks
+ for enumKey, currentEnum := range currentEnumTypes {
+ if previousEnum, exists := previousEnumTypes[enumKey]; exists {
+ // Enum type exists in both metadata
+ // Only update if chunk exists in SDL (user explicitly defined it)
+ if _, chunkExists := previousChunks.EnumTypes[enumKey]; chunkExists {
+ // Chunk exists - update if needed
+ err := updateEnumTypeChunkIfNeeded(previousChunks, currentEnum, previousEnum, enumKey)
+ if err != nil {
+ return errors.Wrapf(err, "failed to update enum type chunk for %s", enumKey)
+ }
+ }
+ // If chunk doesn't exist, skip - user didn't define this enum type in SDL
+ }
+ }
+
+ // Process enum type deletions: remove dropped enum type chunks
+ for enumKey := range previousEnumTypes {
+ if _, exists := currentEnumTypes[enumKey]; !exists {
+ // Enum type was dropped - remove it from chunks
+ deleteEnumTypeChunk(previousChunks, enumKey)
+ }
+ }
+
+ return nil
+}
+
+// createEnumTypeChunk creates a new CREATE TYPE AS ENUM chunk and adds it to the chunks
+func createEnumTypeChunk(chunks *schema.SDLChunks, enumType *storepb.EnumTypeMetadata, enumKey string) error {
+ if enumType == nil || chunks == nil {
+ return nil
+ }
+
+ // Generate SDL text for the enum type
+ schemaName, _ := parseIdentifier(enumKey)
+ enumSDL := generateCreateEnumTypeSDL(schemaName, enumType)
+ if enumSDL == "" {
+ return errors.New("failed to generate SDL for enum type")
+ }
+
+ // Parse the SDL to get AST node
+ parseResult, err := pgparser.ParsePostgreSQL(enumSDL)
+ if err != nil {
+ return errors.Wrapf(err, "failed to parse generated enum type SDL: %s", enumSDL)
+ }
+
+ // Extract the CREATE TYPE AS ENUM AST node
+ var enumASTNode *parser.DefinestmtContext
+ antlr.ParseTreeWalkerDefault.Walk(&enumTypeExtractor{
+ result: &enumASTNode,
+ }, parseResult.Tree)
+
+ if enumASTNode == nil {
+ return errors.New("failed to extract CREATE TYPE AS ENUM AST node")
+ }
+
+ // Create and add the chunk
+ chunk := &schema.SDLChunk{
+ Identifier: enumKey,
+ ASTNode: enumASTNode,
+ }
+
+ // Handle comment if present
+ if len(enumType.Comment) > 0 {
+ commentSQL := generateCommentOnTypeSQL(schemaName, enumType.Name, enumType.Comment)
+ commentParseResult, err := pgparser.ParsePostgreSQL(commentSQL)
+ if err == nil {
+ var commentNode *parser.CommentstmtContext
+ antlr.ParseTreeWalkerDefault.Walk(&commentExtractor{
+ result: &commentNode,
+ }, commentParseResult.Tree)
+ if commentNode != nil {
+ chunk.CommentStatements = []antlr.ParserRuleContext{commentNode}
+ }
+ }
+ }
+
+ chunks.EnumTypes[enumKey] = chunk
+ return nil
+}
+
+// updateEnumTypeChunkIfNeeded updates an enum type chunk if the definition or comment changed
+func updateEnumTypeChunkIfNeeded(chunks *schema.SDLChunks, currentEnum, previousEnum *storepb.EnumTypeMetadata, enumKey string) error {
+ if currentEnum == nil || previousEnum == nil {
+ return nil
+ }
+
+ chunk, exists := chunks.EnumTypes[enumKey]
+ if !exists {
+ return errors.Errorf("enum type chunk not found for key %s", enumKey)
+ }
+
+ // Check if the enum definition has changed (values changed)
+ definitionChanged := !enumTypesEqual(currentEnum, previousEnum)
+
+ if definitionChanged {
+ // Enum definition has changed - regenerate the CREATE TYPE chunk
+ schemaName, _ := parseIdentifier(enumKey)
+ enumSDL := generateCreateEnumTypeSDL(schemaName, currentEnum)
+ if enumSDL == "" {
+ return errors.New("failed to generate SDL for enum type")
+ }
+
+ // Parse the SDL to get AST node
+ parseResult, err := pgparser.ParsePostgreSQL(enumSDL)
+ if err != nil {
+ return errors.Wrapf(err, "failed to parse generated enum type SDL: %s", enumSDL)
+ }
+
+ // Extract the CREATE TYPE AS ENUM AST node
+ var enumASTNode *parser.DefinestmtContext
+ antlr.ParseTreeWalkerDefault.Walk(&enumTypeExtractor{
+ result: &enumASTNode,
+ }, parseResult.Tree)
+
+ if enumASTNode == nil {
+ return errors.New("failed to extract CREATE TYPE AS ENUM AST node")
+ }
+
+ // Update the CREATE TYPE AST node
+ chunk.ASTNode = enumASTNode
+ }
+
+ // Synchronize COMMENT ON TYPE statements only if comment has changed
+ if currentEnum.Comment != previousEnum.Comment {
+ schemaName, _ := parseIdentifier(enumKey)
+ if err := syncObjectCommentStatements(chunk, currentEnum.Comment, "TYPE", schemaName, currentEnum.Name); err != nil {
+ return errors.Wrapf(err, "failed to sync COMMENT statements for enum type %s", enumKey)
+ }
+ }
+
+ return nil
+}
+
+// deleteEnumTypeChunk removes an enum type chunk from the chunks
+func deleteEnumTypeChunk(chunks *schema.SDLChunks, enumKey string) {
+ if chunks != nil && chunks.EnumTypes != nil {
+ delete(chunks.EnumTypes, enumKey)
+ }
+}
+
+// enumTypesEqual compares two enum type definitions excluding comments
+func enumTypesEqual(enum1, enum2 *storepb.EnumTypeMetadata) bool {
+ if enum1 == nil || enum2 == nil {
+ return false
+ }
+
+ // Compare name
+ if enum1.Name != enum2.Name {
+ return false
+ }
+
+ // Compare values
+ if len(enum1.Values) != len(enum2.Values) {
+ return false
+ }
+ for i, v1 := range enum1.Values {
+ if v1 != enum2.Values[i] {
+ return false
+ }
+ }
+
+ return true
+}
+
+// generateCreateEnumTypeSDL generates the SDL text for a CREATE TYPE AS ENUM statement
+func generateCreateEnumTypeSDL(schemaName string, enumType *storepb.EnumTypeMetadata) string {
+ if enumType == nil {
+ return ""
+ }
+
+ var buf strings.Builder
+ if err := writeEnum(&buf, schemaName, enumType); err != nil {
+ return ""
+ }
+ buf.WriteString(";")
+
+ return buf.String()
+}
+
+// generateCommentOnTypeSQL generates a COMMENT ON TYPE statement
+func generateCommentOnTypeSQL(schemaName, typeName, comment string) string {
+ if schemaName == "" {
+ schemaName = "public"
+ }
+ // Escape single quotes in comment
+ escapedComment := strings.ReplaceAll(comment, "'", "''")
+ return fmt.Sprintf("COMMENT ON TYPE \"%s\".\"%s\" IS '%s';", schemaName, typeName, escapedComment)
+}
+
+// enumTypeExtractor is a walker to extract CREATE TYPE AS ENUM AST nodes
+type enumTypeExtractor struct {
+ parser.BasePostgreSQLParserListener
+ result **parser.DefinestmtContext
+}
+
+func (e *enumTypeExtractor) EnterDefinestmt(ctx *parser.DefinestmtContext) {
+ // Only extract CREATE TYPE AS ENUM statements
+ if ctx.CREATE() != nil && ctx.TYPE_P() != nil && ctx.AS() != nil && ctx.ENUM_P() != nil {
+ if e.result != nil && *e.result == nil {
+ *e.result = ctx
+ }
+ }
+}
+
// materializedViewExtractor is a walker to extract CREATE MATERIALIZED VIEW AST nodes
type materializedViewExtractor struct {
parser.BasePostgreSQLParserListener
@@ -6029,6 +6442,109 @@ func syncColumnComment(chunks *schema.SDLChunks, tableKey, columnName, comment s
return nil
}
+// processEnumTypeChanges analyzes enum type changes between current and previous chunks
+// Enum types use DROP + CREATE pattern for modifications (PostgreSQL doesn't support ALTER TYPE ... RENAME VALUE)
+func processEnumTypeChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {
+ // Process current enum types to find created and modified ones
+ for _, currentChunk := range currentChunks.EnumTypes {
+ if previousChunk, exists := previousChunks.EnumTypes[currentChunk.Identifier]; exists {
+ // Enum type exists in both - check if modified by comparing text (excluding comments)
+ currentText := currentChunk.GetTextWithoutComments()
+ previousText := previousChunk.GetTextWithoutComments()
+ if currentText != previousText {
+ // Apply usability check
+ if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {
+ continue
+ }
+ // Enum type was modified - use DROP + CREATE pattern
+ schemaName, enumName := parseIdentifier(currentChunk.Identifier)
+ // Add DROP diff
+ diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{
+ Action: schema.MetadataDiffActionDrop,
+ SchemaName: schemaName,
+ EnumTypeName: enumName,
+ OldEnumType: nil,
+ NewEnumType: nil,
+ OldASTNode: previousChunk.ASTNode,
+ NewASTNode: nil,
+ })
+ // Add CREATE diff
+ diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: schemaName,
+ EnumTypeName: enumName,
+ OldEnumType: nil,
+ NewEnumType: nil,
+ OldASTNode: nil,
+ NewASTNode: currentChunk.ASTNode,
+ })
+ // Add COMMENT ON TYPE diffs if they exist in the new version
+ if len(currentChunk.CommentStatements) > 0 {
+ for _, commentNode := range currentChunk.CommentStatements {
+ commentText := extractCommentTextFromNode(commentNode)
+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{
+ Action: schema.MetadataDiffActionCreate,
+ ObjectType: schema.CommentObjectTypeType,
+ SchemaName: schemaName,
+ ObjectName: enumName,
+ OldComment: "",
+ NewComment: commentText,
+ OldASTNode: nil,
+ NewASTNode: commentNode,
+ })
+ }
+ }
+ }
+ // If text is identical, skip - no changes detected
+ } else {
+ // New enum type
+ schemaName, enumName := parseIdentifier(currentChunk.Identifier)
+ diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: schemaName,
+ EnumTypeName: enumName,
+ OldEnumType: nil,
+ NewEnumType: nil,
+ OldASTNode: nil,
+ NewASTNode: currentChunk.ASTNode,
+ })
+ // Add COMMENT ON TYPE diffs if they exist
+ if len(currentChunk.CommentStatements) > 0 {
+ for _, commentNode := range currentChunk.CommentStatements {
+ commentText := extractCommentTextFromNode(commentNode)
+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{
+ Action: schema.MetadataDiffActionCreate,
+ ObjectType: schema.CommentObjectTypeType,
+ SchemaName: schemaName,
+ ObjectName: enumName,
+ OldComment: "",
+ NewComment: commentText,
+ OldASTNode: nil,
+ NewASTNode: commentNode,
+ })
+ }
+ }
+ }
+ }
+
+ // Process previous enum types to find dropped ones
+ for identifier, previousChunk := range previousChunks.EnumTypes {
+ if _, exists := currentChunks.EnumTypes[identifier]; !exists {
+ // Enum type was dropped
+ schemaName, enumName := parseIdentifier(identifier)
+ diff.EnumTypeChanges = append(diff.EnumTypeChanges, &schema.EnumTypeDiff{
+ Action: schema.MetadataDiffActionDrop,
+ SchemaName: schemaName,
+ EnumTypeName: enumName,
+ OldEnumType: nil,
+ NewEnumType: nil,
+ OldASTNode: previousChunk.ASTNode,
+ NewASTNode: nil,
+ })
+ }
+ }
+}
+
// processSchemaChanges processes explicit CREATE SCHEMA statements in the SDL
func processSchemaChanges(currentChunks, previousChunks *schema.SDLChunks, currentSchema *model.DatabaseSchema, diff *schema.MetadataDiff) {
// Build set of existing schemas in current database
diff --git a/backend/plugin/schema/sdl_differ.go b/backend/plugin/schema/sdl_differ.go
index 85a23ea168..f05aeff25b 100644
--- a/backend/plugin/schema/sdl_differ.go
+++ b/backend/plugin/schema/sdl_differ.go
@@ -124,6 +124,7 @@ type SDLChunks struct {
Indexes map[string]*SDLChunk // key: index name/identifier
Sequences map[string]*SDLChunk // key: sequence name/identifier
Schemas map[string]*SDLChunk // key: schema name/identifier
+ EnumTypes map[string]*SDLChunk // key: enum type name/identifier
// Column comments: map[schemaName.tableName][columnName] -> COMMENT ON COLUMN AST node
ColumnComments map[string]map[string]antlr.ParserRuleContext
diff --git a/docs/adding-new-object-to-sdl-mode.md b/docs/adding-new-object-to-sdl-mode.md
index 20e03ca841..665b1f98ab 100644
--- a/docs/adding-new-object-to-sdl-mode.md
+++ b/docs/adding-new-object-to-sdl-mode.md
@@ -462,11 +462,16 @@ The comment processing system needs to know which objects are being created or d
---
-### 2.5 Implement Drift Handling (Optional)
+### 2.5 Implement Drift Handling (REQUIRED)
-Drift handling synchronizes SDL chunks with actual database state when they differ. This is needed when:
+Drift handling synchronizes SDL chunks with actual database state when they differ. **This is mandatory for all object types.**
+
+This is needed when:
- User's SDL file doesn't match database state
- Auto-sync is enabled
+- Database schema differs from SDL (drift detection)
+
+**Without this implementation, the object type will not work correctly in drift scenarios.**
**File**: `backend/plugin/schema/pg/get_sdl_diff.go`
@@ -1930,7 +1935,8 @@ Use this checklist to ensure complete implementation:
- [ ] Registered in `processCommentChanges`
- [ ] Registered in `buildCreatedObjectsSet`
- [ ] Registered in `buildDroppedObjectsSet`
-- [ ] Drift handling implemented (optional)
+- [ ] **Drift handling implemented (`applyXXXChangesToChunks`) - REQUIRED**
+- [ ] **Drift handler called in `applyMinimalChangesToChunks` - REQUIRED**
#### Migration Generation - CREATE
- [ ] Comment type registered in `differ.go`
@@ -1976,12 +1982,14 @@ Use this checklist to ensure complete implementation:
Adding a new database object to SDL mode requires changes in **5 major phases**:
1. **Parser**: Extract AST nodes and associate COMMENT statements
-2. **SDL Diff**: Detect CREATE/DROP/MODIFY and comment-only changes
+2. **SDL Diff**: Detect CREATE/DROP/MODIFY and comment-only changes + **Drift handling (REQUIRED)**
3. **Migration Generation**: Add to topological sort with **AST-only dependency extraction** (both CREATE and DROP!)
4. **SDL Output**: Generate single-file and multi-file SDL formats
5. **Testing**: Comprehensive tests including dependency ordering
-The **most critical** and **most often forgotten** part is:
+The **most critical** and **most often forgotten** parts are:
+
+### 1. AST-only mode dependency extraction (CRITICAL!)
> **AST-only mode dependency extraction for both CREATE and DROP operations**
Without this, migrations will have wrong ordering in `bb rollout` (SDL mode), leading to:
@@ -1989,3 +1997,13 @@ Without this, migrations will have wrong ordering in `bb rollout` (SDL mode), le
- ❌ "Cannot drop because other objects depend on it" errors on DROP
Always implement and test dependency ordering with the `TestXXXDependencyOrder` test!
+
+### 2. Drift handling implementation (REQUIRED!)
+> **`applyXXXChangesToChunks` function and call in `applyMinimalChangesToChunks`**
+
+Without this, the object type will not work correctly when:
+- User's SDL file doesn't match database state
+- Auto-sync is enabled
+- Database schema differs from SDL (drift detection)
+
+All other object types (tables, views, functions, sequences, materialized views) implement drift handling. Your new object type must too.