Skip to content

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

test: add column tests for PostgreSQL SDL (#17340)

rebelice494c25034fb6
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.go
index ad9c661d42..6a1e644e30 100644
--- a/backend/plugin/schema/pg/generate_migration.go
+++ b/backend/plugin/schema/pg/generate_migration.go
@@ -1408,9 +1408,9 @@ func writeAddCheckConstraint(out *strings.Builder, schema, table string, check *
_, _ = out.WriteString(table)
_, _ = out.WriteString(`" ADD CONSTRAINT "`)
_, _ = out.WriteString(check.Name)
- _, _ = out.WriteString(`" CHECK `)
+ _, _ = out.WriteString(`" CHECK (`)
_, _ = out.WriteString(check.Expression)
- _, _ = out.WriteString(`;`)
+ _, _ = out.WriteString(`);`)
_, _ = out.WriteString("\n")
}
diff --git a/backend/plugin/schema/pg/sdl_comprehensive_test.go b/backend/plugin/schema/pg/sdl_comprehensive_test.go
index 3bf4777e49..aa452e6a22 100644
--- a/backend/plugin/schema/pg/sdl_comprehensive_test.go
+++ b/backend/plugin/schema/pg/sdl_comprehensive_test.go
@@ -93,8 +93,7 @@ func loadTestScenarios() ([]TestScenario, error) {
testDataDir := "sdl_testdata"
if _, err := os.Stat(testDataDir); os.IsNotExist(err) {
- // Return some built-in scenarios for initial testing
- return getBuiltinTestScenarios(), nil
+ return nil, errors.Errorf("test data directory %s does not exist", testDataDir)
}
err := filepath.Walk(testDataDir, func(path string, info os.FileInfo, err error) error {
@@ -117,9 +116,9 @@ func loadTestScenarios() ([]TestScenario, error) {
return nil, err
}
- // If no scenarios found in files, return built-in scenarios
+ // If no scenarios found in files, return error
if len(scenarios) == 0 {
- return getBuiltinTestScenarios(), nil
+ return nil, errors.New("no test scenarios found in test data directory")
}
return scenarios, nil
@@ -181,13 +180,27 @@ func runSDLTestScenario(ctx context.Context, t *testing.T, connConfig *pgx.ConnC
t.Logf("Description: %s", strings.TrimSpace(scenario.Description))
}
- // Create a unique database for this test
- dbName := fmt.Sprintf("sdl_test_%s_%s",
- strings.ReplaceAll(scenario.Category, "/", "_"),
- strings.ReplaceAll(scenario.Name, "/", "_"))
+ // Create a unique database for this test (PostgreSQL limit is 63 chars)
+ category := strings.ReplaceAll(scenario.Category, "/", "_")
+ name := strings.ReplaceAll(scenario.Name, "/", "_")
+
+ // Shorten common prefixes to avoid length issues
+ category = strings.ReplaceAll(category, "table_operations", "tbl_ops")
+ name = strings.ReplaceAll(name, "column_operations_", "col_")
+ name = strings.ReplaceAll(name, "add_column_", "add_")
+ name = strings.ReplaceAll(name, "constraints", "cons")
+ name = strings.ReplaceAll(name, "foreign_keys", "fk")
+ name = strings.ReplaceAll(name, "indexes", "idx")
+
+ dbName := fmt.Sprintf("sdl_test_%s_%s", category, name)
dbName = strings.ReplaceAll(dbName, "-", "_") // Replace hyphens with underscores
dbName = strings.ToLower(dbName)
+ // Ensure length doesn't exceed PostgreSQL's 63 character limit
+ if len(dbName) > 63 {
+ dbName = dbName[:63]
+ }
+
_, err := mainDB.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
require.NoError(t, err)
defer func() {
@@ -236,6 +249,13 @@ func executeEnhancedSDLValidation(ctx context.Context, t *testing.T, connConfig
migrationDDL, schemaDiff, err := generateMigrationDDLFromMetadata(schemaB, schemaCMetadata)
require.NoError(t, err, "Failed to generate migration DDL from metadata")
+ // Use validateOrSaveTestFiles to generate/validate diff.json and ddl.sql files
+ testName := strings.ReplaceAll(t.Name(), "/", "_")
+ err = validateOrSaveTestFiles(t, schemaDiff, testName, migrationDDL)
+ if err != nil {
+ return errors.Wrapf(err, "test file validation/creation failed")
+ }
+
// Compare with expected diff if provided
if scenario.ExpectedDiff != "" {
err := validateSchemaDiff(t, schemaDiff, scenario.ExpectedDiff)
@@ -300,144 +320,6 @@ func executeEnhancedSDLValidation(ctx context.Context, t *testing.T, connConfig
return nil
}
-// getBuiltinTestScenarios returns built-in test scenarios for immediate testing
-func getBuiltinTestScenarios() []TestScenario {
- return []TestScenario{
- {
- Name: "basic_table_creation",
- Category: "data_types/basic_types",
- Description: "Test basic table creation with various data types",
- InitialSchema: ``,
- ExpectedSchema: `
-CREATE TABLE public.users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL UNIQUE,
- email VARCHAR(100) NOT NULL,
- age INTEGER,
- salary DECIMAL(10,2),
- is_active BOOLEAN DEFAULT true,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- profile JSONB,
- tags TEXT[]
-);
-
-CREATE INDEX idx_users_email ON public.users(email);
-CREATE INDEX idx_users_created_at ON public.users(created_at);
-`,
- },
- {
- Name: "add_foreign_key_table",
- Category: "constraints_indexes/foreign_keys",
- Description: "Test adding table with foreign key constraint",
- InitialSchema: `
-CREATE TABLE public.users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL UNIQUE,
- email VARCHAR(100) NOT NULL
-);
-`,
- ExpectedSchema: `
-CREATE TABLE public.users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL UNIQUE,
- email VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE public.posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- content TEXT,
- published BOOLEAN DEFAULT false,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-);
-
-CREATE INDEX idx_posts_user_id ON public.posts(user_id);
-CREATE INDEX idx_posts_published ON public.posts(published) WHERE published = true;
-`,
- },
- {
- Name: "enum_and_custom_types",
- Category: "schema_objects/custom_types",
- Description: "Test enum and custom type creation",
- InitialSchema: ``,
- ExpectedSchema: `
-CREATE TYPE public.status_enum AS ENUM ('pending', 'active', 'inactive');
-CREATE TYPE public.user_role AS ENUM ('admin', 'user', 'guest');
-
-CREATE TABLE public.accounts (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- status status_enum DEFAULT 'pending',
- role user_role DEFAULT 'user',
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-);
-
-CREATE INDEX idx_accounts_status ON public.accounts(status);
-CREATE INDEX idx_accounts_role ON public.accounts(role);
-`,
- },
- {
- Name: "view_and_function",
- Category: "schema_objects/views",
- Description: "Test view and function creation",
- InitialSchema: `
-CREATE TABLE public.employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department VARCHAR(50),
- salary DECIMAL(10, 2),
- hire_date DATE DEFAULT CURRENT_DATE
-);
-`,
- ExpectedSchema: `
-CREATE TABLE public.employees (
- id SERIAL PRIMARY KEY,
- name VARCHAR(100) NOT NULL,
- department VARCHAR(50),
- salary DECIMAL(10, 2),
- hire_date DATE DEFAULT CURRENT_DATE
-);
-
-CREATE OR REPLACE VIEW public.active_employees AS
-SELECT id, name, department, salary
-FROM employees
-WHERE department IS NOT NULL;
-
-CREATE OR REPLACE FUNCTION public.get_employee_count(dept VARCHAR)
-RETURNS INTEGER AS $$
-BEGIN
- RETURN (SELECT COUNT(*) FROM employees WHERE department = dept);
-END;
-$$ LANGUAGE plpgsql;
-`,
- },
- {
- Name: "drop_objects",
- Category: "edge_cases/complex_scenarios",
- Description: "Test dropping all objects from database",
- InitialSchema: `
-CREATE TABLE public.users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(50) NOT NULL UNIQUE,
- email VARCHAR(100) NOT NULL
-);
-
-CREATE TABLE public.posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL,
- title VARCHAR(200) NOT NULL,
- FOREIGN KEY (user_id) REFERENCES users(id)
-);
-
-CREATE INDEX idx_posts_user_id ON public.posts(user_id);
-`,
- ExpectedSchema: ``, // Empty - expect all objects to be dropped
- },
- }
-}
-
// normalizeSQLString normalizes SQL for comparison by removing extra whitespace and standardizing formatting
func normalizeSQLString(sql string) string {
// Remove extra whitespace and normalize line endings
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/ddl.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/ddl.sql
new file mode 100644
index 0000000000..6fba820fa4
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/ddl.sql
@@ -0,0 +1,7 @@
+ALTER TABLE "public"."employees" ALTER COLUMN "department" SET NOT NULL;
+ALTER TABLE "public"."employees" ALTER COLUMN "email" SET NOT NULL;
+ALTER TABLE "public"."employees" ALTER COLUMN "name" SET NOT NULL;
+ALTER TABLE "public"."employees" ADD CONSTRAINT "employees_email_key" UNIQUE (email);
+ALTER TABLE "public"."employees" ADD CONSTRAINT "employees_age_check" CHECK (age >= 18 AND age <= 100);
+ALTER TABLE "public"."employees" ADD CONSTRAINT "employees_salary_check" CHECK (salary > 0);
+
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/diff.json b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/diff.json
new file mode 100644
index 0000000000..b80c7dfb55
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/diff.json
@@ -0,0 +1,104 @@
+{
+ "hasDifferences": true,
+ "summary": "Table changes: 1",
+ "tableChanges": 1,
+ "viewChanges": 0,
+ "functionChanges": 0,
+ "schemaChanges": 0,
+ "sequenceChanges": 0,
+ "enumTypeChanges": 0,
+ "details": {
+ "tables": [
+ {
+ "action": "ALTER",
+ "schemaName": "public",
+ "tableName": "employees",
+ "columnChanges": 3,
+ "indexChanges": 1,
+ "constraintChanges": 2,
+ "foreignKeyChanges": 0,
+ "columns": [
+ {
+ "action": "ALTER",
+ "name": "department",
+ "oldColumn": {
+ "name": "department",
+ "type": "character varying(50)",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "department",
+ "type": "character varying(50)",
+ "nullable": false
+ }
+ },
+ {
+ "action": "ALTER",
+ "name": "email",
+ "oldColumn": {
+ "name": "email",
+ "type": "character varying(150)",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "email",
+ "type": "character varying(150)",
+ "nullable": false
+ }
+ },
+ {
+ "action": "ALTER",
+ "name": "name",
+ "oldColumn": {
+ "name": "name",
+ "type": "character varying(100)",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "name",
+ "type": "character varying(100)",
+ "nullable": false
+ }
+ }
+ ],
+ "indexes": [
+ {
+ "action": "CREATE",
+ "name": "employees_email_key",
+ "newIndex": {
+ "name": "employees_email_key",
+ "type": "btree",
+ "unique": true,
+ "expressions": [
+ "email"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ }
+ ],
+ "constraints": [
+ {
+ "action": "CREATE",
+ "name": "employees_age_check",
+ "newConstraint": {
+ "name": "employees_age_check",
+ "type": "CHECK",
+ "expression": "age \u003e= 18 AND age \u003c= 100"
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "employees_salary_check",
+ "newConstraint": {
+ "name": "employees_salary_check",
+ "type": "CHECK",
+ "expression": "salary \u003e 0"
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/expected_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/expected_schema.sql
new file mode 100644
index 0000000000..9dae553c71
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/expected_schema.sql
@@ -0,0 +1,8 @@
+CREATE TABLE public.employees (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ email VARCHAR(150) NOT NULL UNIQUE,
+ age INTEGER CHECK (age >= 18 AND age <= 100),
+ salary DECIMAL(10,2) CHECK (salary > 0),
+ department VARCHAR(50) NOT NULL
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/initial_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/initial_schema.sql
new file mode 100644
index 0000000000..2bd39b951f
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/initial_schema.sql
@@ -0,0 +1,8 @@
+CREATE TABLE public.employees (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100),
+ email VARCHAR(150),
+ age INTEGER,
+ salary DECIMAL(10,2),
+ department VARCHAR(50)
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/test_description.md b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/test_description.md
new file mode 100644
index 0000000000..436b149123
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_constraints/test_description.md
@@ -0,0 +1,22 @@
+# Add Column Constraints Test
+
+This test validates the SDL migration handling of adding various constraints to columns:
+
+## Test Coverage
+
+- **NOT NULL Constraints**: Adding NOT NULL to nullable columns
+- **UNIQUE Constraints**: Adding uniqueness constraints
+- **CHECK Constraints**: Adding value validation constraints
+- **Default Values**: Adding default value constraints
+- **Combined Constraints**: Multiple constraint types on single columns
+
+## Validation Goals
+
+- Verify accurate detection of new column constraints
+- Validate proper DDL generation for constraint additions
+- Ensure constraint names are generated correctly
+- Test constraint validation logic
+
+## Expected Behavior
+
+The migration should generate proper ALTER TABLE statements to add constraints, including ALTER COLUMN SET NOT NULL and ADD CONSTRAINT statements.
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/ddl.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/ddl.sql
new file mode 100644
index 0000000000..2c0d07b267
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/ddl.sql
@@ -0,0 +1,6 @@
+ALTER TABLE "public"."posts" ALTER COLUMN "author_id" SET NOT NULL;
+CREATE INDEX idx_posts_author_id ON public.posts USING btree (author_id);
+CREATE INDEX idx_posts_category_id ON public.posts USING btree (category_id);
+ALTER TABLE "public"."posts" ADD CONSTRAINT "fk_posts_author" FOREIGN KEY ("author_id") REFERENCES "public"."users" ("id") ON UPDATE NO ACTION ON DELETE CASCADE;
+ALTER TABLE "public"."posts" ADD CONSTRAINT "fk_posts_category" FOREIGN KEY ("category_id") REFERENCES "public"."categories" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;
+
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/diff.json b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/diff.json
new file mode 100644
index 0000000000..21b0727c81
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/diff.json
@@ -0,0 +1,109 @@
+{
+ "hasDifferences": true,
+ "summary": "Table changes: 1",
+ "tableChanges": 1,
+ "viewChanges": 0,
+ "functionChanges": 0,
+ "schemaChanges": 0,
+ "sequenceChanges": 0,
+ "enumTypeChanges": 0,
+ "details": {
+ "tables": [
+ {
+ "action": "ALTER",
+ "schemaName": "public",
+ "tableName": "posts",
+ "columnChanges": 1,
+ "indexChanges": 2,
+ "constraintChanges": 0,
+ "foreignKeyChanges": 2,
+ "columns": [
+ {
+ "action": "ALTER",
+ "name": "author_id",
+ "oldColumn": {
+ "name": "author_id",
+ "type": "integer",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "author_id",
+ "type": "integer",
+ "nullable": false
+ }
+ }
+ ],
+ "indexes": [
+ {
+ "action": "CREATE",
+ "name": "idx_posts_author_id",
+ "newIndex": {
+ "name": "idx_posts_author_id",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "author_id"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_posts_category_id",
+ "newIndex": {
+ "name": "idx_posts_category_id",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "category_id"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ }
+ ],
+ "foreignKeys": [
+ {
+ "action": "CREATE",
+ "name": "fk_posts_author",
+ "newForeignKey": {
+ "name": "fk_posts_author",
+ "columns": [
+ "author_id"
+ ],
+ "referencedSchema": "public",
+ "referencedTable": "users",
+ "referencedColumns": [
+ "id"
+ ],
+ "onUpdate": "NO ACTION",
+ "onDelete": "CASCADE",
+ "matchType": "SIMPLE"
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "fk_posts_category",
+ "newForeignKey": {
+ "name": "fk_posts_category",
+ "columns": [
+ "category_id"
+ ],
+ "referencedSchema": "public",
+ "referencedTable": "categories",
+ "referencedColumns": [
+ "id"
+ ],
+ "onUpdate": "NO ACTION",
+ "onDelete": "SET NULL",
+ "matchType": "SIMPLE"
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/expected_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/expected_schema.sql
new file mode 100644
index 0000000000..139a69545f
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/expected_schema.sql
@@ -0,0 +1,24 @@
+CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ username VARCHAR(50) NOT NULL UNIQUE,
+ email VARCHAR(100) NOT NULL
+);
+
+CREATE TABLE public.categories (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(50) NOT NULL UNIQUE
+);
+
+CREATE TABLE public.posts (
+ id SERIAL PRIMARY KEY,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ author_id INTEGER NOT NULL,
+ category_id INTEGER,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT fk_posts_author FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE CASCADE,
+ CONSTRAINT fk_posts_category FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL
+);
+
+CREATE INDEX idx_posts_author_id ON public.posts(author_id);
+CREATE INDEX idx_posts_category_id ON public.posts(category_id);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/initial_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/initial_schema.sql
new file mode 100644
index 0000000000..cbe0c51c3a
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/initial_schema.sql
@@ -0,0 +1,19 @@
+CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ username VARCHAR(50) NOT NULL UNIQUE,
+ email VARCHAR(100) NOT NULL
+);
+
+CREATE TABLE public.categories (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(50) NOT NULL UNIQUE
+);
+
+CREATE TABLE public.posts (
+ id SERIAL PRIMARY KEY,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ author_id INTEGER,
+ category_id INTEGER,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+)
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/test_description.md b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/test_description.md
new file mode 100644
index 0000000000..5106fbe369
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_foreign_keys/test_description.md
@@ -0,0 +1,22 @@
+# Add Column Foreign Keys Test
+
+This test validates the SDL migration handling of adding foreign key constraints to columns:
+
+## Test Coverage
+
+- **Basic Foreign Keys**: Simple references to primary keys
+- **CASCADE Actions**: ON DELETE CASCADE, ON UPDATE CASCADE
+- **SET NULL Actions**: ON DELETE SET NULL, ON UPDATE SET NULL
+- **Multiple Foreign Keys**: Multiple references in same table
+- **Self-References**: Foreign keys referencing the same table
+
+## Validation Goals
+
+- Verify accurate detection of new foreign key constraints
+- Validate proper ALTER TABLE ADD CONSTRAINT DDL generation
+- Ensure referential actions are correctly specified
+- Test constraint naming and dependency resolution
+
+## Expected Behavior
+
+The migration should generate proper ALTER TABLE ADD CONSTRAINT statements with correct references and cascade actions.
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/ddl.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/ddl.sql
new file mode 100644
index 0000000000..e41f1a7ea4
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/ddl.sql
@@ -0,0 +1,8 @@
+CREATE INDEX idx_posts_author_id ON public.posts USING btree (author_id);
+CREATE INDEX idx_posts_author_status ON public.posts USING btree (author_id, status);
+CREATE INDEX idx_posts_category ON public.posts USING btree (category);
+CREATE INDEX idx_posts_created_at ON public.posts USING btree (created_at);
+CREATE INDEX idx_posts_published ON public.posts USING btree (created_at) WHERE ((status)::text = 'published'::text);
+CREATE INDEX idx_posts_status ON public.posts USING btree (status);
+CREATE INDEX idx_posts_tags ON public.posts USING gin (tags);
+
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/diff.json b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/diff.json
new file mode 100644
index 0000000000..23e0c192b2
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/diff.json
@@ -0,0 +1,132 @@
+{
+ "hasDifferences": true,
+ "summary": "Table changes: 1",
+ "tableChanges": 1,
+ "viewChanges": 0,
+ "functionChanges": 0,
+ "schemaChanges": 0,
+ "sequenceChanges": 0,
+ "enumTypeChanges": 0,
+ "details": {
+ "tables": [
+ {
+ "action": "ALTER",
+ "schemaName": "public",
+ "tableName": "posts",
+ "columnChanges": 0,
+ "indexChanges": 7,
+ "constraintChanges": 0,
+ "foreignKeyChanges": 0,
+ "indexes": [
+ {
+ "action": "CREATE",
+ "name": "idx_posts_author_id",
+ "newIndex": {
+ "name": "idx_posts_author_id",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "author_id"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_posts_author_status",
+ "newIndex": {
+ "name": "idx_posts_author_status",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "author_id",
+ "status"
+ ],
+ "descending": [
+ false,
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_posts_category",
+ "newIndex": {
+ "name": "idx_posts_category",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "category"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_posts_created_at",
+ "newIndex": {
+ "name": "idx_posts_created_at",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "created_at"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_posts_published",
+ "newIndex": {
+ "name": "idx_posts_published",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "created_at"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_posts_status",
+ "newIndex": {
+ "name": "idx_posts_status",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "status"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_posts_tags",
+ "newIndex": {
+ "name": "idx_posts_tags",
+ "type": "gin",
+ "unique": false,
+ "expressions": [
+ "tags"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/expected_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/expected_schema.sql
new file mode 100644
index 0000000000..cffd9738c8
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/expected_schema.sql
@@ -0,0 +1,18 @@
+CREATE TABLE public.posts (
+ id SERIAL PRIMARY KEY,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ author_id INTEGER,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ category VARCHAR(50),
+ tags TEXT[],
+ status VARCHAR(20) DEFAULT 'draft'
+);
+
+CREATE INDEX idx_posts_author_id ON public.posts(author_id);
+CREATE INDEX idx_posts_created_at ON public.posts(created_at);
+CREATE INDEX idx_posts_category ON public.posts(category);
+CREATE INDEX idx_posts_status ON public.posts(status);
+CREATE INDEX idx_posts_author_status ON public.posts(author_id, status);
+CREATE INDEX idx_posts_published ON public.posts(created_at) WHERE ((status)::text = 'published'::text);
+CREATE INDEX idx_posts_tags ON public.posts USING gin(tags);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/initial_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/initial_schema.sql
new file mode 100644
index 0000000000..7f1596c142
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/initial_schema.sql
@@ -0,0 +1,10 @@
+CREATE TABLE public.posts (
+ id SERIAL PRIMARY KEY,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ author_id INTEGER,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ category VARCHAR(50),
+ tags TEXT[],
+ status VARCHAR(20) DEFAULT 'draft'
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/test_description.md b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/test_description.md
new file mode 100644
index 0000000000..4567342dfe
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_column_indexes/test_description.md
@@ -0,0 +1,22 @@
+# Add Column Indexes Test
+
+This test validates the SDL migration handling of adding indexes on existing columns:
+
+## Test Coverage
+
+- **Simple Indexes**: Basic B-tree indexes on single columns
+- **Composite Indexes**: Multi-column indexes
+- **Partial Indexes**: Indexes with WHERE clauses
+- **Specialized Indexes**: GIN indexes for arrays and text search
+- **Unique Indexes**: Unique constraint indexes
+
+## Validation Goals
+
+- Verify accurate detection of new indexes
+- Validate proper CREATE INDEX DDL generation
+- Ensure index types and expressions are correct
+- Test partial index conditions and composite column ordering
+
+## Expected Behavior
+
+The migration should generate proper CREATE INDEX statements with correct index types, expressions, and conditions.
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/ddl.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/ddl.sql
new file mode 100644
index 0000000000..84edefe571
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/ddl.sql
@@ -0,0 +1,4 @@
+ALTER TABLE "public"."products" ADD COLUMN "category" character varying(50) DEFAULT 'general';
+ALTER TABLE "public"."products" ADD COLUMN "created_at" timestamp(6) without time zone DEFAULT CURRENT_TIMESTAMP;
+ALTER TABLE "public"."products" ADD COLUMN "description" text;
+ALTER TABLE "public"."products" ADD COLUMN "is_featured" boolean DEFAULT false;
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/diff.json b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/diff.json
new file mode 100644
index 0000000000..cf51ba19f1
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/diff.json
@@ -0,0 +1,64 @@
+{
+ "hasDifferences": true,
+ "summary": "Table changes: 1",
+ "tableChanges": 1,
+ "viewChanges": 0,
+ "functionChanges": 0,
+ "schemaChanges": 0,
+ "sequenceChanges": 0,
+ "enumTypeChanges": 0,
+ "details": {
+ "tables": [
+ {
+ "action": "ALTER",
+ "schemaName": "public",
+ "tableName": "products",
+ "columnChanges": 4,
+ "indexChanges": 0,
+ "constraintChanges": 0,
+ "foreignKeyChanges": 0,
+ "columns": [
+ {
+ "action": "CREATE",
+ "name": "category",
+ "newColumn": {
+ "name": "category",
+ "type": "character varying(50)",
+ "nullable": true,
+ "defaultValue": "'general'"
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "created_at",
+ "newColumn": {
+ "name": "created_at",
+ "type": "timestamp(6) without time zone",
+ "nullable": true,
+ "defaultValue": "CURRENT_TIMESTAMP"
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "description",
+ "newColumn": {
+ "name": "description",
+ "type": "text",
+ "nullable": true
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "is_featured",
+ "newColumn": {
+ "name": "is_featured",
+ "type": "boolean",
+ "nullable": true,
+ "defaultValue": "false"
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/expected_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/expected_schema.sql
new file mode 100644
index 0000000000..855abb82ac
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/expected_schema.sql
@@ -0,0 +1,9 @@
+CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ price DECIMAL(10,2),
+ description TEXT,
+ category VARCHAR(50) DEFAULT 'general',
+ is_featured BOOLEAN DEFAULT false,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/initial_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/initial_schema.sql
new file mode 100644
index 0000000000..1362b39560
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/initial_schema.sql
@@ -0,0 +1,5 @@
+CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ price DECIMAL(10,2)
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/test_description.md b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/test_description.md
new file mode 100644
index 0000000000..3c937e024b
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/add_simple_columns/test_description.md
@@ -0,0 +1,21 @@
+# Add Simple Columns Test
+
+This test validates the SDL migration handling of adding simple columns to existing tables:
+
+## Test Coverage
+
+- **Column Addition**: Adding columns with various data types
+- **Default Values**: Columns with default values
+- **Nullable Columns**: Adding nullable and NOT NULL columns
+- **Basic Data Types**: VARCHAR, INTEGER, BOOLEAN, TIMESTAMP, TEXT
+
+## Validation Goals
+
+- Verify accurate detection of new columns
+- Validate proper DDL generation for column additions
+- Ensure column constraints and defaults are applied correctly
+- Test column ordering is preserved
+
+## Expected Behavior
+
+The migration should generate proper ALTER TABLE ADD COLUMN statements for each new column while preserving existing table structure and data integrity.
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/ddl.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/ddl.sql
new file mode 100644
index 0000000000..9aa5c010c5
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/ddl.sql
@@ -0,0 +1,21 @@
+DROP INDEX IF EXISTS "public"."idx_products_category_name";
+DROP INDEX IF EXISTS "public"."idx_products_temp";
+ALTER TABLE "public"."products" DROP COLUMN IF EXISTS "old_name";
+ALTER TABLE "public"."products" DROP COLUMN IF EXISTS "temp_field";
+ALTER TABLE "public"."products" DROP COLUMN IF EXISTS "category_name";
+
+ALTER TABLE "public"."products" ADD COLUMN "category_id" integer NOT NULL;
+ALTER TABLE "public"."products" ADD COLUMN "created_at" timestamp(6) without time zone DEFAULT CURRENT_TIMESTAMP;
+ALTER TABLE "public"."products" ADD COLUMN "description" text;
+ALTER TABLE "public"."products" ADD COLUMN "is_active" boolean DEFAULT true;
+ALTER TABLE "public"."products" ADD COLUMN "name" character varying(200) NOT NULL;
+ALTER TABLE "public"."products" ADD COLUMN "updated_at" timestamp(6) without time zone DEFAULT CURRENT_TIMESTAMP;
+ALTER TABLE "public"."products" ALTER COLUMN "price" TYPE numeric(10,2);
+ALTER TABLE "public"."products" ALTER COLUMN "price" SET NOT NULL;
+CREATE INDEX idx_products_active ON public.products USING btree (is_active) WHERE is_active = true;
+CREATE INDEX idx_products_category_id ON public.products USING btree (category_id);
+CREATE INDEX idx_products_name ON public.products USING btree (name);
+CREATE INDEX idx_products_price ON public.products USING btree (price);
+ALTER TABLE "public"."products" ADD CONSTRAINT "products_price_check" CHECK (price > 0);
+ALTER TABLE "public"."products" ADD CONSTRAINT "fk_products_category" FOREIGN KEY ("category_id") REFERENCES "public"."categories" ("id") ON UPDATE NO ACTION ON DELETE RESTRICT;
+
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/diff.json b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/diff.json
new file mode 100644
index 0000000000..688be8708d
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/diff.json
@@ -0,0 +1,246 @@
+{
+ "hasDifferences": true,
+ "summary": "Table changes: 1",
+ "tableChanges": 1,
+ "viewChanges": 0,
+ "functionChanges": 0,
+ "schemaChanges": 0,
+ "sequenceChanges": 0,
+ "enumTypeChanges": 0,
+ "details": {
+ "tables": [
+ {
+ "action": "ALTER",
+ "schemaName": "public",
+ "tableName": "products",
+ "columnChanges": 10,
+ "indexChanges": 6,
+ "constraintChanges": 1,
+ "foreignKeyChanges": 1,
+ "columns": [
+ {
+ "action": "CREATE",
+ "name": "category_id",
+ "newColumn": {
+ "name": "category_id",
+ "type": "integer",
+ "nullable": false
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "old_name",
+ "oldColumn": {
+ "name": "old_name",
+ "type": "character varying(50)",
+ "nullable": true
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "temp_field",
+ "oldColumn": {
+ "name": "temp_field",
+ "type": "text",
+ "nullable": true
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "category_name",
+ "oldColumn": {
+ "name": "category_name",
+ "type": "character varying(30)",
+ "nullable": true
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "created_at",
+ "newColumn": {
+ "name": "created_at",
+ "type": "timestamp(6) without time zone",
+ "nullable": true,
+ "defaultValue": "CURRENT_TIMESTAMP"
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "description",
+ "newColumn": {
+ "name": "description",
+ "type": "text",
+ "nullable": true
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "is_active",
+ "newColumn": {
+ "name": "is_active",
+ "type": "boolean",
+ "nullable": true,
+ "defaultValue": "true"
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "name",
+ "newColumn": {
+ "name": "name",
+ "type": "character varying(200)",
+ "nullable": false
+ }
+ },
+ {
+ "action": "ALTER",
+ "name": "price",
+ "oldColumn": {
+ "name": "price",
+ "type": "integer",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "price",
+ "type": "numeric(10,2)",
+ "nullable": false
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "updated_at",
+ "newColumn": {
+ "name": "updated_at",
+ "type": "timestamp(6) without time zone",
+ "nullable": true,
+ "defaultValue": "CURRENT_TIMESTAMP"
+ }
+ }
+ ],
+ "indexes": [
+ {
+ "action": "CREATE",
+ "name": "idx_products_active",
+ "newIndex": {
+ "name": "idx_products_active",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "is_active"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_products_category_id",
+ "newIndex": {
+ "name": "idx_products_category_id",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "category_id"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "idx_products_category_name",
+ "oldIndex": {
+ "name": "idx_products_category_name",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "category_name"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_products_name",
+ "newIndex": {
+ "name": "idx_products_name",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "name"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "CREATE",
+ "name": "idx_products_price",
+ "newIndex": {
+ "name": "idx_products_price",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "price"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "idx_products_temp",
+ "oldIndex": {
+ "name": "idx_products_temp",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "temp_field"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ }
+ ],
+ "constraints": [
+ {
+ "action": "CREATE",
+ "name": "products_price_check",
+ "newConstraint": {
+ "name": "products_price_check",
+ "type": "CHECK",
+ "expression": "price \u003e 0"
+ }
+ }
+ ],
+ "foreignKeys": [
+ {
+ "action": "CREATE",
+ "name": "fk_products_category",
+ "newForeignKey": {
+ "name": "fk_products_category",
+ "columns": [
+ "category_id"
+ ],
+ "referencedSchema": "public",
+ "referencedTable": "categories",
+ "referencedColumns": [
+ "id"
+ ],
+ "onUpdate": "NO ACTION",
+ "onDelete": "RESTRICT",
+ "matchType": "SIMPLE"
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/expected_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/expected_schema.sql
new file mode 100644
index 0000000000..94f46f371c
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/expected_schema.sql
@@ -0,0 +1,21 @@
+CREATE TABLE public.categories (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(50) NOT NULL UNIQUE
+);
+
+CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(200) NOT NULL,
+ price DECIMAL(10,2) NOT NULL CHECK (price > 0),
+ description TEXT,
+ category_id INTEGER NOT NULL,
+ is_active BOOLEAN DEFAULT true,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT fk_products_category FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE RESTRICT
+);
+
+CREATE INDEX idx_products_name ON public.products(name);
+CREATE INDEX idx_products_category_id ON public.products(category_id);
+CREATE INDEX idx_products_active ON public.products(is_active) WHERE is_active = true;
+CREATE INDEX idx_products_price ON public.products(price);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/initial_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/initial_schema.sql
new file mode 100644
index 0000000000..293033c331
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/initial_schema.sql
@@ -0,0 +1,15 @@
+CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ old_name VARCHAR(50),
+ price INTEGER,
+ temp_field TEXT,
+ category_name VARCHAR(30)
+);
+
+CREATE TABLE public.categories (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(50) NOT NULL UNIQUE
+);
+
+CREATE INDEX idx_products_temp ON public.products(temp_field);
+CREATE INDEX idx_products_category_name ON public.products(category_name);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/test_description.md b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/test_description.md
new file mode 100644
index 0000000000..555864a1d7
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/complex_column_operations/test_description.md
@@ -0,0 +1,22 @@
+# Complex Column Operations Test
+
+This test validates the SDL migration handling of complex scenarios combining multiple column operations:
+
+## Test Coverage
+
+- **Mixed Operations**: Adding, modifying, and dropping columns in same migration
+- **Type Changes with Constraints**: Changing types while adding constraints
+- **Dependency Management**: Handling indexes and foreign keys during column changes
+- **Column Renaming**: Simulating column renames through drop/add operations
+- **Data Preservation**: Ensuring operations preserve data integrity
+
+## Validation Goals
+
+- Verify proper operation ordering and dependency resolution
+- Validate complex DDL generation with multiple ALTER statements
+- Ensure constraint and index management during column changes
+- Test migration safety and data integrity
+
+## Expected Behavior
+
+The migration should generate properly ordered DDL statements that handle dependencies correctly while preserving data integrity throughout the transformation.
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/ddl.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/ddl.sql
new file mode 100644
index 0000000000..84afb93f3e
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/ddl.sql
@@ -0,0 +1,7 @@
+DROP INDEX IF EXISTS "public"."idx_products_legacy_data";
+DROP INDEX IF EXISTS "public"."idx_products_old_price";
+ALTER TABLE "public"."products" DROP COLUMN IF EXISTS "old_price";
+ALTER TABLE "public"."products" DROP COLUMN IF EXISTS "deprecated_field1";
+ALTER TABLE "public"."products" DROP COLUMN IF EXISTS "deprecated_field2";
+ALTER TABLE "public"."products" DROP COLUMN IF EXISTS "legacy_data";
+
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/diff.json b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/diff.json
new file mode 100644
index 0000000000..e6dd2ea33d
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/diff.json
@@ -0,0 +1,93 @@
+{
+ "hasDifferences": true,
+ "summary": "Table changes: 1",
+ "tableChanges": 1,
+ "viewChanges": 0,
+ "functionChanges": 0,
+ "schemaChanges": 0,
+ "sequenceChanges": 0,
+ "enumTypeChanges": 0,
+ "details": {
+ "tables": [
+ {
+ "action": "ALTER",
+ "schemaName": "public",
+ "tableName": "products",
+ "columnChanges": 4,
+ "indexChanges": 2,
+ "constraintChanges": 0,
+ "foreignKeyChanges": 0,
+ "columns": [
+ {
+ "action": "DROP",
+ "name": "old_price",
+ "oldColumn": {
+ "name": "old_price",
+ "type": "numeric(10,2)",
+ "nullable": true
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "deprecated_field1",
+ "oldColumn": {
+ "name": "deprecated_field1",
+ "type": "text",
+ "nullable": true
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "deprecated_field2",
+ "oldColumn": {
+ "name": "deprecated_field2",
+ "type": "integer",
+ "nullable": true
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "legacy_data",
+ "oldColumn": {
+ "name": "legacy_data",
+ "type": "jsonb",
+ "nullable": true
+ }
+ }
+ ],
+ "indexes": [
+ {
+ "action": "DROP",
+ "name": "idx_products_legacy_data",
+ "oldIndex": {
+ "name": "idx_products_legacy_data",
+ "type": "gin",
+ "unique": false,
+ "expressions": [
+ "legacy_data"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ },
+ {
+ "action": "DROP",
+ "name": "idx_products_old_price",
+ "oldIndex": {
+ "name": "idx_products_old_price",
+ "type": "btree",
+ "unique": false,
+ "expressions": [
+ "old_price"
+ ],
+ "descending": [
+ false
+ ]
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/expected_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/expected_schema.sql
new file mode 100644
index 0000000000..497df097a2
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/expected_schema.sql
@@ -0,0 +1,6 @@
+CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ description TEXT,
+ category VARCHAR(50)
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/initial_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/initial_schema.sql
new file mode 100644
index 0000000000..a18cf92cc7
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/initial_schema.sql
@@ -0,0 +1,13 @@
+CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ old_price DECIMAL(10,2),
+ deprecated_field1 TEXT,
+ description TEXT,
+ deprecated_field2 INTEGER,
+ category VARCHAR(50),
+ legacy_data JSONB
+);
+
+CREATE INDEX idx_products_old_price ON public.products(old_price);
+CREATE INDEX idx_products_legacy_data ON public.products USING gin(legacy_data);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/test_description.md b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/test_description.md
new file mode 100644
index 0000000000..b12987eff3
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/drop_columns/test_description.md
@@ -0,0 +1,21 @@
+# Drop Columns Test
+
+This test validates the SDL migration handling of dropping columns from existing tables:
+
+## Test Coverage
+
+- **Single Column Drop**: Removing individual columns
+- **Multiple Column Drops**: Removing several columns at once
+- **Various Data Types**: Dropping columns with different data types
+- **Dependency Handling**: Ensuring constraints and indexes are properly handled
+
+## Validation Goals
+
+- Verify accurate detection of dropped columns
+- Validate proper DDL generation for DROP COLUMN statements
+- Ensure dependent indexes and constraints are dropped
+- Test table structure integrity after column removal
+
+## Expected Behavior
+
+The migration should generate proper ALTER TABLE DROP COLUMN statements while automatically handling dependent objects like indexes and constraints.
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/ddl.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/ddl.sql
new file mode 100644
index 0000000000..82cbe49072
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/ddl.sql
@@ -0,0 +1,5 @@
+ALTER TABLE "public"."customers" ALTER COLUMN "age" TYPE integer;
+ALTER TABLE "public"."customers" ALTER COLUMN "balance" TYPE bigint;
+ALTER TABLE "public"."customers" ALTER COLUMN "is_active" SET DEFAULT true;
+ALTER TABLE "public"."customers" ALTER COLUMN "name" TYPE character varying(200);
+
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/diff.json b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/diff.json
new file mode 100644
index 0000000000..45c9feab86
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/diff.json
@@ -0,0 +1,82 @@
+{
+ "hasDifferences": true,
+ "summary": "Table changes: 1",
+ "tableChanges": 1,
+ "viewChanges": 0,
+ "functionChanges": 0,
+ "schemaChanges": 0,
+ "sequenceChanges": 0,
+ "enumTypeChanges": 0,
+ "details": {
+ "tables": [
+ {
+ "action": "ALTER",
+ "schemaName": "public",
+ "tableName": "customers",
+ "columnChanges": 4,
+ "indexChanges": 0,
+ "constraintChanges": 0,
+ "foreignKeyChanges": 0,
+ "columns": [
+ {
+ "action": "ALTER",
+ "name": "age",
+ "oldColumn": {
+ "name": "age",
+ "type": "smallint",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "age",
+ "type": "integer",
+ "nullable": true
+ }
+ },
+ {
+ "action": "ALTER",
+ "name": "balance",
+ "oldColumn": {
+ "name": "balance",
+ "type": "integer",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "balance",
+ "type": "bigint",
+ "nullable": true
+ }
+ },
+ {
+ "action": "ALTER",
+ "name": "is_active",
+ "oldColumn": {
+ "name": "is_active",
+ "type": "boolean",
+ "nullable": true
+ },
+ "newColumn": {
+ "name": "is_active",
+ "type": "boolean",
+ "nullable": true,
+ "defaultValue": "true"
+ }
+ },
+ {
+ "action": "ALTER",
+ "name": "name",
+ "oldColumn": {
+ "name": "name",
+ "type": "character varying(50)",
+ "nullable": false
+ },
+ "newColumn": {
+ "name": "name",
+ "type": "character varying(200)",
+ "nullable": false
+ }
+ }
+ ]
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/expected_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/expected_schema.sql
new file mode 100644
index 0000000000..2321d55bbf
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/expected_schema.sql
@@ -0,0 +1,8 @@
+CREATE TABLE public.customers (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(200) NOT NULL,
+ age INTEGER,
+ balance BIGINT,
+ is_active BOOLEAN DEFAULT true,
+ notes TEXT
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/initial_schema.sql b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/initial_schema.sql
new file mode 100644
index 0000000000..9b5849b312
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/initial_schema.sql
@@ -0,0 +1,8 @@
+CREATE TABLE public.customers (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(50) NOT NULL,
+ age SMALLINT,
+ balance INTEGER,
+ is_active BOOLEAN,
+ notes TEXT
+);
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/test_description.md b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/test_description.md
new file mode 100644
index 0000000000..81eadf1bf5
--- /dev/null
+++ b/backend/plugin/schema/pg/sdl_testdata/table_operations/column_operations/modify_column_types/test_description.md
@@ -0,0 +1,21 @@
+# Modify Column Types Test
+
+This test validates the SDL migration handling of modifying column data types:
+
+## Test Coverage
+
+- **Type Changes**: VARCHAR size changes, numeric precision changes
+- **Type Conversions**: SMALLINT to INTEGER, INTEGER to BIGINT
+- **Nullability Changes**: Adding/removing NOT NULL constraints
+- **Default Value Changes**: Adding, modifying, and removing default values
+
+## Validation Goals
+
+- Verify accurate detection of column type modifications
+- Validate proper DDL generation for ALTER COLUMN statements
+- Ensure data compatibility during type conversions
+- Test constraint changes are applied correctly
+
+## Expected Behavior
+
+The migration should generate proper ALTER TABLE ALTER COLUMN statements with appropriate type casting and constraint modifications.
\ No newline at end of file
diff --git a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
index 4e976dabed..ff3b9d1ae5 100644
--- a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
+++ b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
@@ -1661,14 +1661,16 @@ func validateFileContent(filename, expectedContent, fileType string) error {
// getTestDataDirectory converts a test name to the corresponding test data directory path
func getTestDataDirectory(testName string) string {
- // Remove "TestSDLValidationFromTestData_" prefix
- if !strings.HasPrefix(testName, "TestSDLValidationFromTestData_") {
+ // Handle both TestSDLValidationFromTestData_ and TestSDLComprehensiveValidation_ prefixes
+ var pathPart string
+ if strings.HasPrefix(testName, "TestSDLValidationFromTestData_") {
+ pathPart = strings.TrimPrefix(testName, "TestSDLValidationFromTestData_")
+ } else if strings.HasPrefix(testName, "TestSDLComprehensiveValidation_") {
+ pathPart = strings.TrimPrefix(testName, "TestSDLComprehensiveValidation_")
+ } else {
return ""
}
- // Extract the path part after the prefix
- pathPart := strings.TrimPrefix(testName, "TestSDLValidationFromTestData_")
-
// Convert from test name format to directory format
// TestSDLValidationFromTestData_data_types_basic_types_datetime_types -> data_types/basic_types/datetime_types
@@ -1699,6 +1701,10 @@ func getTestDataDirectory(testName string) string {
// constraints_indexes_foreign_keys_cascade_actions -> constraints_indexes/foreign_keys/cascade_actions
suffix := strings.TrimPrefix(pathPart, "constraints_indexes_foreign_keys_")
testDataPath = filepath.Join("constraints_indexes", "foreign_keys", suffix)
+ case strings.HasPrefix(pathPart, "table_operations_column_operations_"):
+ // table_operations_column_operations_add_simple_columns -> table_operations/column_operations/add_simple_columns
+ suffix := strings.TrimPrefix(pathPart, "table_operations_column_operations_")
+ testDataPath = filepath.Join("table_operations", "column_operations", suffix)
default:
// Fallback: try to split intelligently
parts := strings.Split(pathPart, "_")