From ae39c144c0ed86080243b9a17edda393afe38a14 Mon Sep 17 00:00:00 2001 From: tianzhou Date: Wed, 15 Oct 2025 15:07:30 +0800 Subject: [PATCH] feat: always dump separate constraint with name --- cmd/dump/dump_integration_test.go | 7 + internal/diff/constraint.go | 44 +- internal/diff/identifier_quote_test.go | 9 +- internal/diff/table.go | 202 ++++----- .../add_column_generated/diff.sql | 3 +- .../create_table/add_column_generated/new.sql | 5 +- .../add_column_generated/plan.json | 2 +- .../add_column_generated/plan.sql | 2 +- .../add_column_generated/plan.txt | 2 +- .../diff/create_table/add_pk_bigint/diff.sql | 2 +- .../diff/create_table/add_pk_bigint/new.sql | 5 +- .../diff/create_table/add_pk_bigint/plan.json | 2 +- .../diff/create_table/add_pk_bigint/plan.sql | 2 +- .../diff/create_table/add_pk_bigint/plan.txt | 2 +- .../create_table/add_pk_identity/diff.sql | 2 +- .../diff/create_table/add_pk_identity/new.sql | 5 +- .../create_table/add_pk_identity/plan.json | 2 +- .../create_table/add_pk_identity/plan.sql | 2 +- .../create_table/add_pk_identity/plan.txt | 2 +- .../diff/create_table/add_pk_serial/diff.sql | 2 +- .../diff/create_table/add_pk_serial/new.sql | 5 +- .../diff/create_table/add_pk_serial/plan.json | 2 +- .../diff/create_table/add_pk_serial/plan.sql | 2 +- .../diff/create_table/add_pk_serial/plan.txt | 2 +- .../diff/create_table/add_pk_single/diff.sql | 2 +- .../diff/create_table/add_pk_single/new.sql | 5 +- .../diff/create_table/add_pk_single/plan.json | 2 +- .../diff/create_table/add_pk_single/plan.sql | 2 +- .../diff/create_table/add_pk_single/plan.txt | 2 +- .../diff/create_table/add_pk_text/diff.sql | 2 +- .../diff/create_table/add_pk_text/new.sql | 5 +- .../diff/create_table/add_pk_text/plan.json | 2 +- .../diff/create_table/add_pk_text/plan.sql | 2 +- .../diff/create_table/add_pk_text/plan.txt | 2 +- .../diff/create_table/add_pk_uuid/diff.sql | 2 +- .../diff/create_table/add_pk_uuid/new.sql | 5 +- .../diff/create_table/add_pk_uuid/plan.json | 2 +- .../diff/create_table/add_pk_uuid/plan.sql | 2 +- .../diff/create_table/add_pk_uuid/plan.txt | 2 +- testdata/diff/create_table/add_table/diff.sql | 5 +- .../diff/create_table/add_table/plan.json | 2 +- testdata/diff/create_table/add_table/plan.sql | 5 +- testdata/diff/create_table/add_table/plan.txt | 5 +- .../add_table_composite_keys/diff.sql | 10 +- .../add_table_composite_keys/plan.json | 4 +- .../add_table_composite_keys/plan.sql | 10 +- .../add_table_composite_keys/plan.txt | 10 +- .../diff/create_table/add_table_like/diff.sql | 8 +- .../create_table/add_table_like/plan.json | 4 +- .../diff/create_table/add_table_like/plan.sql | 8 +- .../diff/create_table/add_table_like/plan.txt | 8 +- .../add_table_like_forward_ref/diff.sql | 10 +- .../add_table_like_forward_ref/plan.json | 4 +- .../add_table_like_forward_ref/plan.sql | 10 +- .../add_table_like_forward_ref/plan.txt | 10 +- .../add_table_no_online_rewrite/diff.sql | 11 +- .../add_table_no_online_rewrite/plan.json | 4 +- .../add_table_no_online_rewrite/plan.sql | 11 +- .../add_table_no_online_rewrite/plan.txt | 11 +- .../create_table/add_table_serial_pk/diff.sql | 5 +- .../add_table_serial_pk/plan.json | 2 +- .../create_table/add_table_serial_pk/plan.sql | 5 +- .../create_table/add_table_serial_pk/plan.txt | 5 +- .../diff/create_table/add_uk_bigint/diff.sql | 2 +- .../diff/create_table/add_uk_bigint/new.sql | 5 +- .../diff/create_table/add_uk_bigint/plan.json | 2 +- .../diff/create_table/add_uk_bigint/plan.sql | 2 +- .../diff/create_table/add_uk_bigint/plan.txt | 2 +- .../create_table/add_uk_identity/diff.sql | 2 +- .../diff/create_table/add_uk_identity/new.sql | 5 +- .../create_table/add_uk_identity/plan.json | 2 +- .../create_table/add_uk_identity/plan.sql | 2 +- .../create_table/add_uk_identity/plan.txt | 2 +- .../diff/create_table/add_uk_serial/diff.sql | 2 +- .../diff/create_table/add_uk_serial/new.sql | 5 +- .../diff/create_table/add_uk_serial/plan.json | 2 +- .../diff/create_table/add_uk_serial/plan.sql | 2 +- .../diff/create_table/add_uk_serial/plan.txt | 2 +- .../diff/create_table/add_uk_single/diff.sql | 2 +- .../diff/create_table/add_uk_single/new.sql | 5 +- .../diff/create_table/add_uk_single/plan.json | 2 +- .../diff/create_table/add_uk_single/plan.sql | 2 +- .../diff/create_table/add_uk_single/plan.txt | 2 +- .../diff/create_table/add_uk_text/diff.sql | 2 +- .../diff/create_table/add_uk_text/new.sql | 5 +- .../diff/create_table/add_uk_text/plan.json | 2 +- .../diff/create_table/add_uk_text/plan.sql | 2 +- .../diff/create_table/add_uk_text/plan.txt | 2 +- .../diff/create_table/add_uk_uuid/diff.sql | 2 +- .../diff/create_table/add_uk_uuid/new.sql | 5 +- .../diff/create_table/add_uk_uuid/plan.json | 2 +- .../diff/create_table/add_uk_uuid/plan.sql | 2 +- .../diff/create_table/add_uk_uuid/plan.txt | 2 +- .../dependency/function_to_trigger/diff.sql | 8 +- .../dependency/function_to_trigger/plan.json | 2 +- .../dependency/function_to_trigger/plan.sql | 8 +- .../dependency/function_to_trigger/plan.txt | 8 +- .../dependency/table_to_function/diff.sql | 5 +- .../dependency/table_to_function/plan.json | 2 +- .../dependency/table_to_function/plan.sql | 5 +- .../dependency/table_to_function/plan.txt | 5 +- .../diff/dependency/table_to_table/diff.sql | 14 +- .../diff/dependency/table_to_table/plan.json | 4 +- .../diff/dependency/table_to_table/plan.sql | 14 +- .../diff/dependency/table_to_table/plan.txt | 14 +- testdata/diff/migrate/v1/diff.sql | 36 +- testdata/diff/migrate/v1/plan.json | 12 +- testdata/diff/migrate/v1/plan.sql | 36 +- testdata/diff/migrate/v1/plan.txt | 36 +- testdata/diff/migrate/v3/diff.sql | 5 +- testdata/diff/migrate/v3/plan.json | 2 +- testdata/diff/migrate/v3/plan.sql | 5 +- testdata/diff/migrate/v3/plan.txt | 5 +- testdata/diff/migrate/v5/diff.sql | 8 +- testdata/diff/migrate/v5/plan.json | 2 +- testdata/diff/migrate/v5/plan.sql | 8 +- testdata/diff/migrate/v5/plan.txt | 8 +- testdata/dump/bytebase/pgschema.sql | 285 +++++++----- testdata/dump/employee/pgschema.sql | 40 +- .../issue_80_index_name_quote/pgschema.sql | 10 +- .../manifest.json | 8 + .../pgdump.sql | 406 ++++++++++++++++++ .../pgschema.sql | 99 +++++ .../issue_83_explicit_constraint_name/raw.sql | 106 +++++ testdata/dump/sakila/pgschema.sql | 197 +++++---- testdata/dump/tenant/pgschema.sql | 15 +- testdata/include/expected_full_schema.sql | 9 +- testdata/include/tables/orders.sql | 6 +- testdata/include/tables/users.sql | 3 +- 129 files changed, 1434 insertions(+), 601 deletions(-) create mode 100644 testdata/dump/issue_83_explicit_constraint_name/manifest.json create mode 100644 testdata/dump/issue_83_explicit_constraint_name/pgdump.sql create mode 100644 testdata/dump/issue_83_explicit_constraint_name/pgschema.sql create mode 100644 testdata/dump/issue_83_explicit_constraint_name/raw.sql diff --git a/cmd/dump/dump_integration_test.go b/cmd/dump/dump_integration_test.go index a7d574c3..aca0ac1d 100644 --- a/cmd/dump/dump_integration_test.go +++ b/cmd/dump/dump_integration_test.go @@ -61,6 +61,13 @@ func TestDumpCommand_Issue80IndexNameQuote(t *testing.T) { runExactMatchTest(t, "issue_80_index_name_quote") } +func TestDumpCommand_Issue83ExplicitConstraintName(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + runExactMatchTest(t, "issue_83_explicit_constraint_name") +} + func runExactMatchTest(t *testing.T, testDataDir string) { runExactMatchTestWithContext(t, context.Background(), testDataDir) } diff --git a/internal/diff/constraint.go b/internal/diff/constraint.go index 48ce1114..ddf97cb8 100644 --- a/internal/diff/constraint.go +++ b/internal/diff/constraint.go @@ -20,11 +20,15 @@ func generateConstraintSQL(constraint *ir.Constraint, _ string) string { switch constraint.Type { case ir.ConstraintTypePrimaryKey: - return fmt.Sprintf("PRIMARY KEY (%s)", strings.Join(getColumnNames(constraint.Columns), ", ")) + // Always include CONSTRAINT name to be explicit and consistent + return fmt.Sprintf("CONSTRAINT %s PRIMARY KEY (%s)", constraint.Name, strings.Join(getColumnNames(constraint.Columns), ", ")) case ir.ConstraintTypeUnique: - return fmt.Sprintf("UNIQUE (%s)", strings.Join(getColumnNames(constraint.Columns), ", ")) + // Always include CONSTRAINT name to be explicit and consistent + return fmt.Sprintf("CONSTRAINT %s UNIQUE (%s)", constraint.Name, strings.Join(getColumnNames(constraint.Columns), ", ")) case ir.ConstraintTypeForeignKey: - stmt := fmt.Sprintf("FOREIGN KEY (%s) REFERENCES %s (%s)", + // Always include CONSTRAINT name to preserve explicit FK names + stmt := fmt.Sprintf("CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)", + constraint.Name, strings.Join(getColumnNames(constraint.Columns), ", "), ir.QuoteIdentifier(constraint.ReferencedTable), strings.Join(getColumnNames(constraint.ReferencedColumns), ", ")) // Only add ON UPDATE/DELETE if they are not the default "NO ACTION" @@ -34,6 +38,18 @@ func generateConstraintSQL(constraint *ir.Constraint, _ string) string { if constraint.DeleteRule != "" && constraint.DeleteRule != "NO ACTION" { stmt += fmt.Sprintf(" ON DELETE %s", constraint.DeleteRule) } + // Add deferrable clause + if constraint.Deferrable { + if constraint.InitiallyDeferred { + stmt += " DEFERRABLE INITIALLY DEFERRED" + } else { + stmt += " DEFERRABLE" + } + } + // Add NOT VALID if needed + if !constraint.IsValid { + stmt += " NOT VALID" + } return stmt case ir.ConstraintTypeCheck: // Generate CHECK constraint with proper NOT VALID placement @@ -66,28 +82,16 @@ func getInlineConstraintsForTable(table *ir.Table) []*ir.Constraint { constraint := table.Constraints[constraintName] // Categorize constraints by type + // ALL constraints are now included as table-level constraints for consistency + // This ensures all constraint names are preserved and provides cleaner formatting switch constraint.Type { case ir.ConstraintTypePrimaryKey: - // Only include multi-column primary keys as inline constraints - // Single-column primary keys are handled inline with the column definition - if len(constraint.Columns) > 1 { - primaryKeys = append(primaryKeys, constraint) - } + primaryKeys = append(primaryKeys, constraint) case ir.ConstraintTypeUnique: - // Only include multi-column unique constraints as inline constraints - // Single-column unique constraints are handled inline with the column definition - if len(constraint.Columns) > 1 { - uniques = append(uniques, constraint) - } + uniques = append(uniques, constraint) case ir.ConstraintTypeForeignKey: - // Only include multi-column foreign key constraints as inline constraints - // Single-column foreign key constraints are handled inline with the column definition - if len(constraint.Columns) > 1 { - foreignKeys = append(foreignKeys, constraint) - } + foreignKeys = append(foreignKeys, constraint) case ir.ConstraintTypeCheck: - // Always include ALL CHECK constraints as table-level named constraints - // This eliminates complexity and makes constraints explicit and manageable checkConstraints = append(checkConstraints, constraint) } } diff --git a/internal/diff/identifier_quote_test.go b/internal/diff/identifier_quote_test.go index bb0bba8a..4275497c 100644 --- a/internal/diff/identifier_quote_test.go +++ b/internal/diff/identifier_quote_test.go @@ -22,7 +22,7 @@ func TestGenerateConstraintSQL_WithQuoting(t *testing.T) { {Name: "accountId", Position: 2}, }, }, - want: `UNIQUE ("userId", "accountId")`, + want: `CONSTRAINT test_unique UNIQUE ("userId", "accountId")`, }, { name: "PRIMARY KEY with reserved word", @@ -34,7 +34,7 @@ func TestGenerateConstraintSQL_WithQuoting(t *testing.T) { {Name: "order", Position: 2}, }, }, - want: `PRIMARY KEY ("user", "order")`, + want: `CONSTRAINT test_pk PRIMARY KEY ("user", "order")`, }, { name: "FOREIGN KEY with camelCase", @@ -49,8 +49,9 @@ func TestGenerateConstraintSQL_WithQuoting(t *testing.T) { {Name: "id", Position: 1}, }, DeleteRule: "CASCADE", + IsValid: true, }, - want: `FOREIGN KEY ("userId") REFERENCES users (id) ON DELETE CASCADE`, + want: `CONSTRAINT test_fk FOREIGN KEY ("userId") REFERENCES users (id) ON DELETE CASCADE`, }, { name: "UNIQUE with lowercase columns (no quotes needed)", @@ -62,7 +63,7 @@ func TestGenerateConstraintSQL_WithQuoting(t *testing.T) { {Name: "username", Position: 2}, }, }, - want: `UNIQUE (email, username)`, + want: `CONSTRAINT test_unique_lower UNIQUE (email, username)`, }, } diff --git a/internal/diff/table.go b/internal/diff/table.go index 1986aad5..b9e1e18a 100644 --- a/internal/diff/table.go +++ b/internal/diff/table.go @@ -523,54 +523,81 @@ func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector collector.collect(context, sql) } - // Add new columns - already sorted by the Diff operation - // Track which constraints are handled inline with column additions - handledFKConstraints := make(map[string]bool) - handledPKConstraints := make(map[string]bool) - handledUKConstraints := make(map[string]bool) + // Track constraints that are added inline with columns to avoid duplicate generation + inlineConstraints := make(map[string]bool) + // Add new columns - already sorted by the Diff operation for _, column := range td.AddedColumns { - // Convert table.Constraints map to slice for helper - var allConstraints []*ir.Constraint - for _, constraint := range td.Table.Constraints { - allConstraints = append(allConstraints, constraint) - } - - // Find all single-column constraints for this column - // For ALTER TABLE, FK/CHECK come from table.Constraints, but PK/UK come from AddedConstraints - constraints, isPartOfAnyPK := findSingleColumnConstraints(column, allConstraints, td.AddedConstraints) - - // Mark constraints as handled so we don't add them again later - if constraints.ForeignKey != nil { - handledFKConstraints[constraints.ForeignKey.Name] = true - } - if constraints.PrimaryKey != nil { - handledPKConstraints[constraints.PrimaryKey.Name] = true - } - if constraints.Unique != nil { - handledUKConstraints[constraints.Unique.Name] = true + // Check if column is part of any primary key constraint for NOT NULL handling + isPartOfAnyPK := false + for _, constraint := range td.AddedConstraints { + if constraint.Type == ir.ConstraintTypePrimaryKey { + for _, col := range constraint.Columns { + if col.Name == column.Name { + isPartOfAnyPK = true + break + } + } + if isPartOfAnyPK { + break + } + } } // Build column type columnType := formatColumnDataType(column) tableName := getTableNameWithSchema(td.Table.Schema, td.Table.Name, targetSchema) - // Use line break format for complex statements (with foreign keys, primary keys, or unique keys) + // Build and append all column clauses + clauses := buildColumnClauses(column, isPartOfAnyPK, td.Table.Schema, targetSchema) + + // Check for single-column constraints that can be added inline + var inlineConstraint string + for _, constraint := range td.AddedConstraints { + // Only add inline for single-column constraints + if len(constraint.Columns) == 1 && constraint.Columns[0].Name == column.Name { + switch constraint.Type { + case ir.ConstraintTypePrimaryKey: + inlineConstraint = fmt.Sprintf(" CONSTRAINT %s PRIMARY KEY", constraint.Name) + case ir.ConstraintTypeUnique: + inlineConstraint = fmt.Sprintf(" CONSTRAINT %s UNIQUE", constraint.Name) + case ir.ConstraintTypeForeignKey: + // For FK, use the generateForeignKeyClause with inline=true + fkClause := generateForeignKeyClause(constraint, targetSchema, true) + inlineConstraint = fmt.Sprintf(" CONSTRAINT %s%s", constraint.Name, fkClause) + case ir.ConstraintTypeCheck: + // For CHECK, format the clause inline + checkExpr := constraint.CheckClause + // Strip "CHECK" prefix if present + if len(checkExpr) >= 5 && strings.EqualFold(checkExpr[:5], "check") { + checkExpr = strings.TrimSpace(checkExpr[5:]) + } + checkExpr = strings.TrimSpace(checkExpr) + // Ensure parentheses + if !strings.HasPrefix(checkExpr, "(") { + checkExpr = "(" + checkExpr + ")" + } + inlineConstraint = fmt.Sprintf(" CONSTRAINT %s CHECK %s", constraint.Name, checkExpr) + } + + if inlineConstraint != "" { + inlineConstraints[constraint.Name] = true + break + } + } + } + + // Build base ALTER TABLE ADD COLUMN statement + // Use newline format if there's an inline constraint for better readability var stmt string - if constraints.ForeignKey != nil || constraints.PrimaryKey != nil || constraints.Unique != nil { - // Use multi-line format for complex statements with constraints - stmt = fmt.Sprintf("ALTER TABLE %s\nADD COLUMN %s %s", - tableName, ir.QuoteIdentifier(column.Name), columnType) + if inlineConstraint != "" { + stmt = fmt.Sprintf("ALTER TABLE %s\nADD COLUMN %s %s%s%s", + tableName, ir.QuoteIdentifier(column.Name), columnType, clauses, inlineConstraint) } else { - // Use single-line format for simple column additions - stmt = fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", - tableName, ir.QuoteIdentifier(column.Name), columnType) + stmt = fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s%s", + tableName, ir.QuoteIdentifier(column.Name), columnType, clauses) } - // Build and append all column clauses - clauses := buildColumnClauses(column, constraints, isPartOfAnyPK, td.Table.Schema, targetSchema) - stmt += clauses - context := &diffContext{ Type: DiffTypeTableColumn, Operation: DiffOperationCreate, @@ -601,18 +628,11 @@ func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector // Add new constraints - already sorted by the Diff operation for _, constraint := range td.AddedConstraints { - // Skip FK constraints that were already handled inline with column additions - if constraint.Type == ir.ConstraintTypeForeignKey && handledFKConstraints[constraint.Name] { - continue - } - // Skip PK constraints that were already handled inline with column additions - if constraint.Type == ir.ConstraintTypePrimaryKey && handledPKConstraints[constraint.Name] { - continue - } - // Skip UK constraints that were already handled inline with column additions - if constraint.Type == ir.ConstraintTypeUnique && handledUKConstraints[constraint.Name] { + // Skip constraints that were already added inline with columns + if inlineConstraints[constraint.Name] { continue } + switch constraint.Type { case ir.ConstraintTypeUnique: // Sort columns by position @@ -1032,75 +1052,32 @@ func writeColumnDefinitionToBuilder(builder *strings.Builder, table *ir.Table, c builder.WriteString(dataType) - // Convert table.Constraints map to slice for helper - var allConstraints []*ir.Constraint - for _, constraint := range table.Constraints { - allConstraints = append(allConstraints, constraint) - } - - // Find all single-column constraints for this column - // For CREATE TABLE, both FK/CHECK and PK/UK come from table.Constraints - constraints, isPartOfAnyPK := findSingleColumnConstraints(column, allConstraints, allConstraints) - - // Build and append all column clauses - clauses := buildColumnClauses(column, constraints, isPartOfAnyPK, table.Schema, targetSchema) - builder.WriteString(clauses) -} - -// columnConstraints holds single-column constraints found for a specific column -type columnConstraints struct { - PrimaryKey *ir.Constraint - Unique *ir.Constraint - ForeignKey *ir.Constraint - Checks []*ir.Constraint -} - -// findSingleColumnConstraints finds all single-column constraints for a given column -// Also checks if column is part of any (single or multi-column) primary key for NOT NULL handling -func findSingleColumnConstraints(column *ir.Column, allConstraints []*ir.Constraint, pkUKSource []*ir.Constraint) (columnConstraints, bool) { - result := columnConstraints{ - Checks: []*ir.Constraint{}, - } + // Check if column is part of any primary key constraint for NOT NULL handling isPartOfAnyPK := false - - // Search for FK and CHECK in allConstraints (always from table.Constraints) - for _, constraint := range allConstraints { - if len(constraint.Columns) == 1 && constraint.Columns[0].Name == column.Name { - switch constraint.Type { - case ir.ConstraintTypeForeignKey: - result.ForeignKey = constraint - // CHECK constraints are always handled as table-level constraints - } - } - } - - // Search for PK and UK in pkUKSource (could be table.Constraints or td.AddedConstraints) - // Also check if column is part of any primary key (including multi-column PKs) - for _, constraint := range pkUKSource { + for _, constraint := range table.Constraints { if constraint.Type == ir.ConstraintTypePrimaryKey { - // Check if this column is in this PK constraint for _, col := range constraint.Columns { if col.Name == column.Name { isPartOfAnyPK = true - // Only set PrimaryKey if it's a single-column PK (for inline PRIMARY KEY) - if len(constraint.Columns) == 1 { - result.PrimaryKey = constraint - } break } } - } else if constraint.Type == ir.ConstraintTypeUnique && len(constraint.Columns) == 1 && constraint.Columns[0].Name == column.Name { - result.Unique = constraint + if isPartOfAnyPK { + break + } } } - return result, isPartOfAnyPK + // Build and append all column clauses + clauses := buildColumnClauses(column, isPartOfAnyPK, table.Schema, targetSchema) + builder.WriteString(clauses) } + // buildColumnClauses builds the SQL clauses for a column definition (works for both CREATE TABLE and ALTER TABLE) // Returns the clauses as a string to be appended to the column name and type // Order follows PostgreSQL documentation: https://www.postgresql.org/docs/current/sql-altertable.html -func buildColumnClauses(column *ir.Column, constraints columnConstraints, isPartOfAnyPK bool, tableSchema string, targetSchema string) string { +func buildColumnClauses(column *ir.Column, isPartOfAnyPK bool, tableSchema string, targetSchema string) string { var parts []string // 1. Identity columns (must come early, before DEFAULT) @@ -1144,32 +1121,15 @@ func buildColumnClauses(column *ir.Column, constraints columnConstraints, isPart parts = append(parts, "NOT NULL") } - // 5. PRIMARY KEY (must come after GENERATED for generated columns) - if constraints.PrimaryKey != nil { - parts = append(parts, "PRIMARY KEY") - } - - // 6. UNIQUE (skip if PRIMARY KEY present, since PK implies UNIQUE) - if constraints.Unique != nil && constraints.PrimaryKey == nil { - parts = append(parts, "UNIQUE") - } + // Note: No inline constraints (PRIMARY KEY, UNIQUE, FOREIGN KEY) are added here + // ALL constraints are now handled as table-level constraints for consistency + // This ensures all constraint names are preserved and provides cleaner formatting - // 7. FOREIGN KEY (REFERENCES) - // Note: generateForeignKeyClause with inline=true already includes the leading space - fkClause := "" - if constraints.ForeignKey != nil { - fkClause = generateForeignKeyClause(constraints.ForeignKey, targetSchema, true) - } - - if len(parts) == 0 && fkClause == "" { + if len(parts) == 0 { return "" } - result := "" - if len(parts) > 0 { - result = " " + strings.Join(parts, " ") - } - result += fkClause // FK clause already has leading space + result := " " + strings.Join(parts, " ") return result } diff --git a/testdata/diff/create_table/add_column_generated/diff.sql b/testdata/diff/create_table/add_column_generated/diff.sql index 923e9c33..237798bb 100644 --- a/testdata/diff/create_table/add_column_generated/diff.sql +++ b/testdata/diff/create_table/add_column_generated/diff.sql @@ -1,3 +1,4 @@ ALTER TABLE merge_request -ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED PRIMARY KEY; +ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED CONSTRAINT pk_merge_request_iid PRIMARY KEY; + ALTER TABLE merge_request ADD COLUMN title text GENERATED ALWAYS AS (data ->> 'title') STORED; \ No newline at end of file diff --git a/testdata/diff/create_table/add_column_generated/new.sql b/testdata/diff/create_table/add_column_generated/new.sql index cd2ffd56..f278c718 100644 --- a/testdata/diff/create_table/add_column_generated/new.sql +++ b/testdata/diff/create_table/add_column_generated/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.merge_request ( data jsonb NOT NULL, - iid integer PRIMARY KEY GENERATED ALWAYS AS ((data ->> 'iid')::integer) STORED, - title text GENERATED ALWAYS AS (data ->> 'title') STORED + iid integer GENERATED ALWAYS AS ((data ->> 'iid')::integer) STORED, + title text GENERATED ALWAYS AS (data ->> 'title') STORED, + CONSTRAINT pk_merge_request_iid PRIMARY KEY (iid) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_column_generated/plan.json b/testdata/diff/create_table/add_column_generated/plan.json index a6ac62e6..a811cb7b 100644 --- a/testdata/diff/create_table/add_column_generated/plan.json +++ b/testdata/diff/create_table/add_column_generated/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE merge_request\nADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED PRIMARY KEY;", + "sql": "ALTER TABLE merge_request\nADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED CONSTRAINT pk_merge_request_iid PRIMARY KEY;", "type": "table.column", "operation": "create", "path": "public.merge_request.iid" diff --git a/testdata/diff/create_table/add_column_generated/plan.sql b/testdata/diff/create_table/add_column_generated/plan.sql index 7e61dd1d..efc1fa5f 100644 --- a/testdata/diff/create_table/add_column_generated/plan.sql +++ b/testdata/diff/create_table/add_column_generated/plan.sql @@ -1,4 +1,4 @@ ALTER TABLE merge_request -ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED PRIMARY KEY; +ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED CONSTRAINT pk_merge_request_iid PRIMARY KEY; ALTER TABLE merge_request ADD COLUMN title text GENERATED ALWAYS AS (data ->> 'title') STORED; diff --git a/testdata/diff/create_table/add_column_generated/plan.txt b/testdata/diff/create_table/add_column_generated/plan.txt index 03d5df8d..5317def3 100644 --- a/testdata/diff/create_table/add_column_generated/plan.txt +++ b/testdata/diff/create_table/add_column_generated/plan.txt @@ -12,6 +12,6 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE merge_request -ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED PRIMARY KEY; +ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED CONSTRAINT pk_merge_request_iid PRIMARY KEY; ALTER TABLE merge_request ADD COLUMN title text GENERATED ALWAYS AS (data ->> 'title') STORED; diff --git a/testdata/diff/create_table/add_pk_bigint/diff.sql b/testdata/diff/create_table/add_pk_bigint/diff.sql index a0576dfe..7c68ff4d 100644 --- a/testdata/diff/create_table/add_pk_bigint/diff.sql +++ b/testdata/diff/create_table/add_pk_bigint/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE transactions -ADD COLUMN id bigint PRIMARY KEY; +ADD COLUMN id bigint CONSTRAINT transactions_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_bigint/new.sql b/testdata/diff/create_table/add_pk_bigint/new.sql index fa7cf5a6..5f2cc07e 100644 --- a/testdata/diff/create_table/add_pk_bigint/new.sql +++ b/testdata/diff/create_table/add_pk_bigint/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.transactions ( - id bigint PRIMARY KEY, + id bigint, amount numeric(15,2) NOT NULL, - created_at timestamp with time zone + created_at timestamp with time zone, + CONSTRAINT transactions_pkey PRIMARY KEY (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_pk_bigint/plan.json b/testdata/diff/create_table/add_pk_bigint/plan.json index 00f59c0c..6f400a81 100644 --- a/testdata/diff/create_table/add_pk_bigint/plan.json +++ b/testdata/diff/create_table/add_pk_bigint/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE transactions\nADD COLUMN id bigint PRIMARY KEY;", + "sql": "ALTER TABLE transactions\nADD COLUMN id bigint CONSTRAINT transactions_pkey PRIMARY KEY;", "type": "table.column", "operation": "create", "path": "public.transactions.id" diff --git a/testdata/diff/create_table/add_pk_bigint/plan.sql b/testdata/diff/create_table/add_pk_bigint/plan.sql index a0576dfe..7c68ff4d 100644 --- a/testdata/diff/create_table/add_pk_bigint/plan.sql +++ b/testdata/diff/create_table/add_pk_bigint/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE transactions -ADD COLUMN id bigint PRIMARY KEY; +ADD COLUMN id bigint CONSTRAINT transactions_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_bigint/plan.txt b/testdata/diff/create_table/add_pk_bigint/plan.txt index c182d2ee..5d8d75c0 100644 --- a/testdata/diff/create_table/add_pk_bigint/plan.txt +++ b/testdata/diff/create_table/add_pk_bigint/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE transactions -ADD COLUMN id bigint PRIMARY KEY; +ADD COLUMN id bigint CONSTRAINT transactions_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_identity/diff.sql b/testdata/diff/create_table/add_pk_identity/diff.sql index 25f11142..c5310e56 100644 --- a/testdata/diff/create_table/add_pk_identity/diff.sql +++ b/testdata/diff/create_table/add_pk_identity/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY; +ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_identity/new.sql b/testdata/diff/create_table/add_pk_identity/new.sql index 84f6e819..2f4c74f8 100644 --- a/testdata/diff/create_table/add_pk_identity/new.sql +++ b/testdata/diff/create_table/add_pk_identity/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.users ( - id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + id integer GENERATED ALWAYS AS IDENTITY, username text NOT NULL, - email text + email text, + CONSTRAINT users_pkey PRIMARY KEY (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_pk_identity/plan.json b/testdata/diff/create_table/add_pk_identity/plan.json index b37fcd2e..1a33575d 100644 --- a/testdata/diff/create_table/add_pk_identity/plan.json +++ b/testdata/diff/create_table/add_pk_identity/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE users\nADD COLUMN id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY;", + "sql": "ALTER TABLE users\nADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_pkey PRIMARY KEY;", "type": "table.column", "operation": "create", "path": "public.users.id" diff --git a/testdata/diff/create_table/add_pk_identity/plan.sql b/testdata/diff/create_table/add_pk_identity/plan.sql index 25f11142..c5310e56 100644 --- a/testdata/diff/create_table/add_pk_identity/plan.sql +++ b/testdata/diff/create_table/add_pk_identity/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY; +ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_identity/plan.txt b/testdata/diff/create_table/add_pk_identity/plan.txt index da8cf02c..3f98c474 100644 --- a/testdata/diff/create_table/add_pk_identity/plan.txt +++ b/testdata/diff/create_table/add_pk_identity/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE users -ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY; +ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_serial/diff.sql b/testdata/diff/create_table/add_pk_serial/diff.sql index b6fdeda3..13884a43 100644 --- a/testdata/diff/create_table/add_pk_serial/diff.sql +++ b/testdata/diff/create_table/add_pk_serial/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id serial PRIMARY KEY; +ADD COLUMN id serial CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_serial/new.sql b/testdata/diff/create_table/add_pk_serial/new.sql index 372b07c6..749560b5 100644 --- a/testdata/diff/create_table/add_pk_serial/new.sql +++ b/testdata/diff/create_table/add_pk_serial/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.users ( - id serial PRIMARY KEY, + id serial, username text NOT NULL, - email text + email text, + CONSTRAINT users_pkey PRIMARY KEY (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_pk_serial/plan.json b/testdata/diff/create_table/add_pk_serial/plan.json index 9dbb05a0..4f26f712 100644 --- a/testdata/diff/create_table/add_pk_serial/plan.json +++ b/testdata/diff/create_table/add_pk_serial/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE users\nADD COLUMN id serial PRIMARY KEY;", + "sql": "ALTER TABLE users\nADD COLUMN id serial CONSTRAINT users_pkey PRIMARY KEY;", "type": "table.column", "operation": "create", "path": "public.users.id" diff --git a/testdata/diff/create_table/add_pk_serial/plan.sql b/testdata/diff/create_table/add_pk_serial/plan.sql index b6fdeda3..13884a43 100644 --- a/testdata/diff/create_table/add_pk_serial/plan.sql +++ b/testdata/diff/create_table/add_pk_serial/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id serial PRIMARY KEY; +ADD COLUMN id serial CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_serial/plan.txt b/testdata/diff/create_table/add_pk_serial/plan.txt index b261cebf..359b634b 100644 --- a/testdata/diff/create_table/add_pk_serial/plan.txt +++ b/testdata/diff/create_table/add_pk_serial/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE users -ADD COLUMN id serial PRIMARY KEY; +ADD COLUMN id serial CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_single/diff.sql b/testdata/diff/create_table/add_pk_single/diff.sql index 41b2cfa6..151d8af5 100644 --- a/testdata/diff/create_table/add_pk_single/diff.sql +++ b/testdata/diff/create_table/add_pk_single/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer PRIMARY KEY; +ADD COLUMN id integer CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_single/new.sql b/testdata/diff/create_table/add_pk_single/new.sql index 0e052770..6a5b3581 100644 --- a/testdata/diff/create_table/add_pk_single/new.sql +++ b/testdata/diff/create_table/add_pk_single/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.users ( - id integer PRIMARY KEY, + id integer, username text NOT NULL, - email text + email text, + CONSTRAINT users_pkey PRIMARY KEY (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_pk_single/plan.json b/testdata/diff/create_table/add_pk_single/plan.json index 3ebdd7b8..7c76036f 100644 --- a/testdata/diff/create_table/add_pk_single/plan.json +++ b/testdata/diff/create_table/add_pk_single/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE users\nADD COLUMN id integer PRIMARY KEY;", + "sql": "ALTER TABLE users\nADD COLUMN id integer CONSTRAINT users_pkey PRIMARY KEY;", "type": "table.column", "operation": "create", "path": "public.users.id" diff --git a/testdata/diff/create_table/add_pk_single/plan.sql b/testdata/diff/create_table/add_pk_single/plan.sql index 41b2cfa6..151d8af5 100644 --- a/testdata/diff/create_table/add_pk_single/plan.sql +++ b/testdata/diff/create_table/add_pk_single/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer PRIMARY KEY; +ADD COLUMN id integer CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_single/plan.txt b/testdata/diff/create_table/add_pk_single/plan.txt index 2c28519f..45ce7d4d 100644 --- a/testdata/diff/create_table/add_pk_single/plan.txt +++ b/testdata/diff/create_table/add_pk_single/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE users -ADD COLUMN id integer PRIMARY KEY; +ADD COLUMN id integer CONSTRAINT users_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_text/diff.sql b/testdata/diff/create_table/add_pk_text/diff.sql index 978650aa..00dedb6f 100644 --- a/testdata/diff/create_table/add_pk_text/diff.sql +++ b/testdata/diff/create_table/add_pk_text/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE countries -ADD COLUMN code text PRIMARY KEY; +ADD COLUMN code text CONSTRAINT countries_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_text/new.sql b/testdata/diff/create_table/add_pk_text/new.sql index 6dfe9435..08056aab 100644 --- a/testdata/diff/create_table/add_pk_text/new.sql +++ b/testdata/diff/create_table/add_pk_text/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.countries ( - code text PRIMARY KEY, + code text, name text NOT NULL, - continent text + continent text, + CONSTRAINT countries_pkey PRIMARY KEY (code) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_pk_text/plan.json b/testdata/diff/create_table/add_pk_text/plan.json index 06be204e..e031b0ea 100644 --- a/testdata/diff/create_table/add_pk_text/plan.json +++ b/testdata/diff/create_table/add_pk_text/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE countries\nADD COLUMN code text PRIMARY KEY;", + "sql": "ALTER TABLE countries\nADD COLUMN code text CONSTRAINT countries_pkey PRIMARY KEY;", "type": "table.column", "operation": "create", "path": "public.countries.code" diff --git a/testdata/diff/create_table/add_pk_text/plan.sql b/testdata/diff/create_table/add_pk_text/plan.sql index 978650aa..00dedb6f 100644 --- a/testdata/diff/create_table/add_pk_text/plan.sql +++ b/testdata/diff/create_table/add_pk_text/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE countries -ADD COLUMN code text PRIMARY KEY; +ADD COLUMN code text CONSTRAINT countries_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_text/plan.txt b/testdata/diff/create_table/add_pk_text/plan.txt index fccb1c1c..62e4f788 100644 --- a/testdata/diff/create_table/add_pk_text/plan.txt +++ b/testdata/diff/create_table/add_pk_text/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE countries -ADD COLUMN code text PRIMARY KEY; +ADD COLUMN code text CONSTRAINT countries_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_uuid/diff.sql b/testdata/diff/create_table/add_pk_uuid/diff.sql index 54c2a5ac..6679666d 100644 --- a/testdata/diff/create_table/add_pk_uuid/diff.sql +++ b/testdata/diff/create_table/add_pk_uuid/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE documents -ADD COLUMN id uuid PRIMARY KEY; +ADD COLUMN id uuid CONSTRAINT documents_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_uuid/new.sql b/testdata/diff/create_table/add_pk_uuid/new.sql index c3a699d1..cf27d3ce 100644 --- a/testdata/diff/create_table/add_pk_uuid/new.sql +++ b/testdata/diff/create_table/add_pk_uuid/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.documents ( - id uuid PRIMARY KEY, + id uuid, title text NOT NULL, - content text + content text, + CONSTRAINT documents_pkey PRIMARY KEY (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_pk_uuid/plan.json b/testdata/diff/create_table/add_pk_uuid/plan.json index 88467886..dde7eae9 100644 --- a/testdata/diff/create_table/add_pk_uuid/plan.json +++ b/testdata/diff/create_table/add_pk_uuid/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE documents\nADD COLUMN id uuid PRIMARY KEY;", + "sql": "ALTER TABLE documents\nADD COLUMN id uuid CONSTRAINT documents_pkey PRIMARY KEY;", "type": "table.column", "operation": "create", "path": "public.documents.id" diff --git a/testdata/diff/create_table/add_pk_uuid/plan.sql b/testdata/diff/create_table/add_pk_uuid/plan.sql index 54c2a5ac..6679666d 100644 --- a/testdata/diff/create_table/add_pk_uuid/plan.sql +++ b/testdata/diff/create_table/add_pk_uuid/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE documents -ADD COLUMN id uuid PRIMARY KEY; +ADD COLUMN id uuid CONSTRAINT documents_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_pk_uuid/plan.txt b/testdata/diff/create_table/add_pk_uuid/plan.txt index b79f093b..931d4ca7 100644 --- a/testdata/diff/create_table/add_pk_uuid/plan.txt +++ b/testdata/diff/create_table/add_pk_uuid/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE documents -ADD COLUMN id uuid PRIMARY KEY; +ADD COLUMN id uuid CONSTRAINT documents_pkey PRIMARY KEY; diff --git a/testdata/diff/create_table/add_table/diff.sql b/testdata/diff/create_table/add_table/diff.sql index f6146c08..f371030f 100644 --- a/testdata/diff/create_table/add_table/diff.sql +++ b/testdata/diff/create_table/add_table/diff.sql @@ -1,4 +1,5 @@ CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT departments_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_table/plan.json b/testdata/diff/create_table/add_table/plan.json index 576db5c1..65d248e6 100644 --- a/testdata/diff/create_table/add_table/plan.json +++ b/testdata/diff/create_table/add_table/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS departments (\n id integer PRIMARY KEY,\n name text NOT NULL\n);", + "sql": "CREATE TABLE IF NOT EXISTS departments (\n id integer,\n name text NOT NULL,\n CONSTRAINT departments_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.departments" diff --git a/testdata/diff/create_table/add_table/plan.sql b/testdata/diff/create_table/add_table/plan.sql index f6146c08..f371030f 100644 --- a/testdata/diff/create_table/add_table/plan.sql +++ b/testdata/diff/create_table/add_table/plan.sql @@ -1,4 +1,5 @@ CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT departments_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_table/plan.txt b/testdata/diff/create_table/add_table/plan.txt index 047bcbc4..5fa7dcc0 100644 --- a/testdata/diff/create_table/add_table/plan.txt +++ b/testdata/diff/create_table/add_table/plan.txt @@ -10,6 +10,7 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT departments_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_table_composite_keys/diff.sql b/testdata/diff/create_table/add_table_composite_keys/diff.sql index 8aeef022..b6e096ef 100644 --- a/testdata/diff/create_table/add_table_composite_keys/diff.sql +++ b/testdata/diff/create_table/add_table_composite_keys/diff.sql @@ -3,8 +3,8 @@ CREATE TABLE IF NOT EXISTS organizations ( org_id integer, org_name text NOT NULL, org_type text NOT NULL, - PRIMARY KEY (tenant_id, org_id), - UNIQUE (org_type, org_id, tenant_id) + CONSTRAINT organizations_pkey PRIMARY KEY (tenant_id, org_id), + CONSTRAINT organizations_org_type_org_id_tenant_id_key UNIQUE (org_type, org_id, tenant_id) ); CREATE TABLE IF NOT EXISTS projects ( @@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS projects ( project_name text NOT NULL, project_code text NOT NULL, description text, - PRIMARY KEY (tenant_id, org_id, project_id), - UNIQUE (project_name, tenant_id, project_id), - FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id) + CONSTRAINT projects_pkey PRIMARY KEY (tenant_id, org_id, project_id), + CONSTRAINT projects_project_name_tenant_id_project_id_key UNIQUE (project_name, tenant_id, project_id), + CONSTRAINT projects_tenant_id_org_id_fkey FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id) ); diff --git a/testdata/diff/create_table/add_table_composite_keys/plan.json b/testdata/diff/create_table/add_table_composite_keys/plan.json index 33a7c01a..1393354d 100644 --- a/testdata/diff/create_table/add_table_composite_keys/plan.json +++ b/testdata/diff/create_table/add_table_composite_keys/plan.json @@ -9,13 +9,13 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS organizations (\n tenant_id integer,\n org_id integer,\n org_name text NOT NULL,\n org_type text NOT NULL,\n PRIMARY KEY (tenant_id, org_id),\n UNIQUE (org_type, org_id, tenant_id)\n);", + "sql": "CREATE TABLE IF NOT EXISTS organizations (\n tenant_id integer,\n org_id integer,\n org_name text NOT NULL,\n org_type text NOT NULL,\n CONSTRAINT organizations_pkey PRIMARY KEY (tenant_id, org_id),\n CONSTRAINT organizations_org_type_org_id_tenant_id_key UNIQUE (org_type, org_id, tenant_id)\n);", "type": "table", "operation": "create", "path": "public.organizations" }, { - "sql": "CREATE TABLE IF NOT EXISTS projects (\n tenant_id integer,\n org_id integer,\n project_id integer,\n project_name text NOT NULL,\n project_code text NOT NULL,\n description text,\n PRIMARY KEY (tenant_id, org_id, project_id),\n UNIQUE (project_name, tenant_id, project_id),\n FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id)\n);", + "sql": "CREATE TABLE IF NOT EXISTS projects (\n tenant_id integer,\n org_id integer,\n project_id integer,\n project_name text NOT NULL,\n project_code text NOT NULL,\n description text,\n CONSTRAINT projects_pkey PRIMARY KEY (tenant_id, org_id, project_id),\n CONSTRAINT projects_project_name_tenant_id_project_id_key UNIQUE (project_name, tenant_id, project_id),\n CONSTRAINT projects_tenant_id_org_id_fkey FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id)\n);", "type": "table", "operation": "create", "path": "public.projects" diff --git a/testdata/diff/create_table/add_table_composite_keys/plan.sql b/testdata/diff/create_table/add_table_composite_keys/plan.sql index 8aeef022..b6e096ef 100644 --- a/testdata/diff/create_table/add_table_composite_keys/plan.sql +++ b/testdata/diff/create_table/add_table_composite_keys/plan.sql @@ -3,8 +3,8 @@ CREATE TABLE IF NOT EXISTS organizations ( org_id integer, org_name text NOT NULL, org_type text NOT NULL, - PRIMARY KEY (tenant_id, org_id), - UNIQUE (org_type, org_id, tenant_id) + CONSTRAINT organizations_pkey PRIMARY KEY (tenant_id, org_id), + CONSTRAINT organizations_org_type_org_id_tenant_id_key UNIQUE (org_type, org_id, tenant_id) ); CREATE TABLE IF NOT EXISTS projects ( @@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS projects ( project_name text NOT NULL, project_code text NOT NULL, description text, - PRIMARY KEY (tenant_id, org_id, project_id), - UNIQUE (project_name, tenant_id, project_id), - FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id) + CONSTRAINT projects_pkey PRIMARY KEY (tenant_id, org_id, project_id), + CONSTRAINT projects_project_name_tenant_id_project_id_key UNIQUE (project_name, tenant_id, project_id), + CONSTRAINT projects_tenant_id_org_id_fkey FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id) ); diff --git a/testdata/diff/create_table/add_table_composite_keys/plan.txt b/testdata/diff/create_table/add_table_composite_keys/plan.txt index ec6a0f0c..f7c86fd2 100644 --- a/testdata/diff/create_table/add_table_composite_keys/plan.txt +++ b/testdata/diff/create_table/add_table_composite_keys/plan.txt @@ -15,8 +15,8 @@ CREATE TABLE IF NOT EXISTS organizations ( org_id integer, org_name text NOT NULL, org_type text NOT NULL, - PRIMARY KEY (tenant_id, org_id), - UNIQUE (org_type, org_id, tenant_id) + CONSTRAINT organizations_pkey PRIMARY KEY (tenant_id, org_id), + CONSTRAINT organizations_org_type_org_id_tenant_id_key UNIQUE (org_type, org_id, tenant_id) ); CREATE TABLE IF NOT EXISTS projects ( @@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS projects ( project_name text NOT NULL, project_code text NOT NULL, description text, - PRIMARY KEY (tenant_id, org_id, project_id), - UNIQUE (project_name, tenant_id, project_id), - FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id) + CONSTRAINT projects_pkey PRIMARY KEY (tenant_id, org_id, project_id), + CONSTRAINT projects_project_name_tenant_id_project_id_key UNIQUE (project_name, tenant_id, project_id), + CONSTRAINT projects_tenant_id_org_id_fkey FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id) ); diff --git a/testdata/diff/create_table/add_table_like/diff.sql b/testdata/diff/create_table/add_table_like/diff.sql index 0d54488a..df640d87 100644 --- a/testdata/diff/create_table/add_table_like/diff.sql +++ b/testdata/diff/create_table/add_table_like/diff.sql @@ -1,15 +1,17 @@ CREATE TABLE IF NOT EXISTS products ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - deleted_at timestamptz + deleted_at timestamptz, + CONSTRAINT products_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, deleted_at timestamptz, + CONSTRAINT users_pkey PRIMARY KEY (id), CONSTRAINT users_check CHECK (created_at <= updated_at) ); diff --git a/testdata/diff/create_table/add_table_like/plan.json b/testdata/diff/create_table/add_table_like/plan.json index 02c41863..ff0017e4 100644 --- a/testdata/diff/create_table/add_table_like/plan.json +++ b/testdata/diff/create_table/add_table_like/plan.json @@ -9,13 +9,13 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS products (\n id SERIAL PRIMARY KEY,\n created_at timestamptz DEFAULT now() NOT NULL,\n updated_at timestamptz DEFAULT now() NOT NULL,\n deleted_at timestamptz\n);", + "sql": "CREATE TABLE IF NOT EXISTS products (\n id SERIAL,\n created_at timestamptz DEFAULT now() NOT NULL,\n updated_at timestamptz DEFAULT now() NOT NULL,\n deleted_at timestamptz,\n CONSTRAINT products_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.products" }, { - "sql": "CREATE TABLE IF NOT EXISTS users (\n id SERIAL PRIMARY KEY,\n created_at timestamptz DEFAULT now() NOT NULL,\n updated_at timestamptz DEFAULT now() NOT NULL,\n deleted_at timestamptz,\n CONSTRAINT users_check CHECK (created_at <= updated_at)\n);", + "sql": "CREATE TABLE IF NOT EXISTS users (\n id SERIAL,\n created_at timestamptz DEFAULT now() NOT NULL,\n updated_at timestamptz DEFAULT now() NOT NULL,\n deleted_at timestamptz,\n CONSTRAINT users_pkey PRIMARY KEY (id),\n CONSTRAINT users_check CHECK (created_at <= updated_at)\n);", "type": "table", "operation": "create", "path": "public.users" diff --git a/testdata/diff/create_table/add_table_like/plan.sql b/testdata/diff/create_table/add_table_like/plan.sql index 124af5b1..3188884a 100644 --- a/testdata/diff/create_table/add_table_like/plan.sql +++ b/testdata/diff/create_table/add_table_like/plan.sql @@ -1,15 +1,17 @@ CREATE TABLE IF NOT EXISTS products ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - deleted_at timestamptz + deleted_at timestamptz, + CONSTRAINT products_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, deleted_at timestamptz, + CONSTRAINT users_pkey PRIMARY KEY (id), CONSTRAINT users_check CHECK (created_at <= updated_at) ); diff --git a/testdata/diff/create_table/add_table_like/plan.txt b/testdata/diff/create_table/add_table_like/plan.txt index 14108068..5d92a6c0 100644 --- a/testdata/diff/create_table/add_table_like/plan.txt +++ b/testdata/diff/create_table/add_table_like/plan.txt @@ -15,17 +15,19 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS products ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - deleted_at timestamptz + deleted_at timestamptz, + CONSTRAINT products_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, deleted_at timestamptz, + CONSTRAINT users_pkey PRIMARY KEY (id), CONSTRAINT users_check CHECK (created_at <= updated_at) ); diff --git a/testdata/diff/create_table/add_table_like_forward_ref/diff.sql b/testdata/diff/create_table/add_table_like_forward_ref/diff.sql index 1f350642..9a5c3d87 100644 --- a/testdata/diff/create_table/add_table_like_forward_ref/diff.sql +++ b/testdata/diff/create_table/add_table_like_forward_ref/diff.sql @@ -1,17 +1,19 @@ CREATE TABLE IF NOT EXISTS customers ( customer_id integer NOT NULL, name varchar(100) NOT NULL, - email varchar(255) UNIQUE, + email varchar(255), created_at timestamp DEFAULT now(), - updated_at timestamp DEFAULT now() + updated_at timestamp DEFAULT now(), + CONSTRAINT customers_email_key UNIQUE (email) ); CREATE TABLE IF NOT EXISTS orders ( - id SERIAL PRIMARY KEY, + id SERIAL, order_date date NOT NULL, customer_id integer NOT NULL, name varchar(100) NOT NULL, email varchar(255), created_at timestamp DEFAULT now(), - updated_at timestamp DEFAULT now() + updated_at timestamp DEFAULT now(), + CONSTRAINT orders_pkey PRIMARY KEY (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_table_like_forward_ref/plan.json b/testdata/diff/create_table/add_table_like_forward_ref/plan.json index 59cb3854..5ed58830 100644 --- a/testdata/diff/create_table/add_table_like_forward_ref/plan.json +++ b/testdata/diff/create_table/add_table_like_forward_ref/plan.json @@ -9,13 +9,13 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS customers (\n customer_id integer NOT NULL,\n name varchar(100) NOT NULL,\n email varchar(255) UNIQUE,\n created_at timestamp DEFAULT now(),\n updated_at timestamp DEFAULT now()\n);", + "sql": "CREATE TABLE IF NOT EXISTS customers (\n customer_id integer NOT NULL,\n name varchar(100) NOT NULL,\n email varchar(255),\n created_at timestamp DEFAULT now(),\n updated_at timestamp DEFAULT now(),\n CONSTRAINT customers_email_key UNIQUE (email)\n);", "type": "table", "operation": "create", "path": "public.customers" }, { - "sql": "CREATE TABLE IF NOT EXISTS orders (\n id SERIAL PRIMARY KEY,\n order_date date NOT NULL,\n customer_id integer NOT NULL,\n name varchar(100) NOT NULL,\n email varchar(255),\n created_at timestamp DEFAULT now(),\n updated_at timestamp DEFAULT now()\n);", + "sql": "CREATE TABLE IF NOT EXISTS orders (\n id SERIAL,\n order_date date NOT NULL,\n customer_id integer NOT NULL,\n name varchar(100) NOT NULL,\n email varchar(255),\n created_at timestamp DEFAULT now(),\n updated_at timestamp DEFAULT now(),\n CONSTRAINT orders_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.orders" diff --git a/testdata/diff/create_table/add_table_like_forward_ref/plan.sql b/testdata/diff/create_table/add_table_like_forward_ref/plan.sql index afdaa507..00063a42 100644 --- a/testdata/diff/create_table/add_table_like_forward_ref/plan.sql +++ b/testdata/diff/create_table/add_table_like_forward_ref/plan.sql @@ -1,17 +1,19 @@ CREATE TABLE IF NOT EXISTS customers ( customer_id integer NOT NULL, name varchar(100) NOT NULL, - email varchar(255) UNIQUE, + email varchar(255), created_at timestamp DEFAULT now(), - updated_at timestamp DEFAULT now() + updated_at timestamp DEFAULT now(), + CONSTRAINT customers_email_key UNIQUE (email) ); CREATE TABLE IF NOT EXISTS orders ( - id SERIAL PRIMARY KEY, + id SERIAL, order_date date NOT NULL, customer_id integer NOT NULL, name varchar(100) NOT NULL, email varchar(255), created_at timestamp DEFAULT now(), - updated_at timestamp DEFAULT now() + updated_at timestamp DEFAULT now(), + CONSTRAINT orders_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_table_like_forward_ref/plan.txt b/testdata/diff/create_table/add_table_like_forward_ref/plan.txt index 1ca58df0..34452521 100644 --- a/testdata/diff/create_table/add_table_like_forward_ref/plan.txt +++ b/testdata/diff/create_table/add_table_like_forward_ref/plan.txt @@ -13,17 +13,19 @@ DDL to be executed: CREATE TABLE IF NOT EXISTS customers ( customer_id integer NOT NULL, name varchar(100) NOT NULL, - email varchar(255) UNIQUE, + email varchar(255), created_at timestamp DEFAULT now(), - updated_at timestamp DEFAULT now() + updated_at timestamp DEFAULT now(), + CONSTRAINT customers_email_key UNIQUE (email) ); CREATE TABLE IF NOT EXISTS orders ( - id SERIAL PRIMARY KEY, + id SERIAL, order_date date NOT NULL, customer_id integer NOT NULL, name varchar(100) NOT NULL, email varchar(255), created_at timestamp DEFAULT now(), - updated_at timestamp DEFAULT now() + updated_at timestamp DEFAULT now(), + CONSTRAINT orders_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_table_no_online_rewrite/diff.sql b/testdata/diff/create_table/add_table_no_online_rewrite/diff.sql index dda8b794..39a990ad 100644 --- a/testdata/diff/create_table/add_table_no_online_rewrite/diff.sql +++ b/testdata/diff/create_table/add_table_no_online_rewrite/diff.sql @@ -1,14 +1,17 @@ CREATE TABLE IF NOT EXISTS companies ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT companies_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, + id integer, name text NOT NULL, - company_id integer NOT NULL REFERENCES companies (id), + company_id integer NOT NULL, budget numeric(10,2), created_at timestamp DEFAULT now(), + CONSTRAINT departments_pkey PRIMARY KEY (id), + CONSTRAINT departments_company_id_fkey FOREIGN KEY (company_id) REFERENCES companies (id), CONSTRAINT departments_budget_check CHECK (budget > 0) ); diff --git a/testdata/diff/create_table/add_table_no_online_rewrite/plan.json b/testdata/diff/create_table/add_table_no_online_rewrite/plan.json index 8e3bc8bc..64bc1283 100644 --- a/testdata/diff/create_table/add_table_no_online_rewrite/plan.json +++ b/testdata/diff/create_table/add_table_no_online_rewrite/plan.json @@ -9,13 +9,13 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS companies (\n id integer PRIMARY KEY,\n name text NOT NULL\n);", + "sql": "CREATE TABLE IF NOT EXISTS companies (\n id integer,\n name text NOT NULL,\n CONSTRAINT companies_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.companies" }, { - "sql": "CREATE TABLE IF NOT EXISTS departments (\n id integer PRIMARY KEY,\n name text NOT NULL,\n company_id integer NOT NULL REFERENCES companies (id),\n budget numeric(10,2),\n created_at timestamp DEFAULT now(),\n CONSTRAINT departments_budget_check CHECK (budget > 0)\n);", + "sql": "CREATE TABLE IF NOT EXISTS departments (\n id integer,\n name text NOT NULL,\n company_id integer NOT NULL,\n budget numeric(10,2),\n created_at timestamp DEFAULT now(),\n CONSTRAINT departments_pkey PRIMARY KEY (id),\n CONSTRAINT departments_company_id_fkey FOREIGN KEY (company_id) REFERENCES companies (id),\n CONSTRAINT departments_budget_check CHECK (budget > 0)\n);", "type": "table", "operation": "create", "path": "public.departments" diff --git a/testdata/diff/create_table/add_table_no_online_rewrite/plan.sql b/testdata/diff/create_table/add_table_no_online_rewrite/plan.sql index dda8b794..39a990ad 100644 --- a/testdata/diff/create_table/add_table_no_online_rewrite/plan.sql +++ b/testdata/diff/create_table/add_table_no_online_rewrite/plan.sql @@ -1,14 +1,17 @@ CREATE TABLE IF NOT EXISTS companies ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT companies_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, + id integer, name text NOT NULL, - company_id integer NOT NULL REFERENCES companies (id), + company_id integer NOT NULL, budget numeric(10,2), created_at timestamp DEFAULT now(), + CONSTRAINT departments_pkey PRIMARY KEY (id), + CONSTRAINT departments_company_id_fkey FOREIGN KEY (company_id) REFERENCES companies (id), CONSTRAINT departments_budget_check CHECK (budget > 0) ); diff --git a/testdata/diff/create_table/add_table_no_online_rewrite/plan.txt b/testdata/diff/create_table/add_table_no_online_rewrite/plan.txt index c54558c4..3ecece24 100644 --- a/testdata/diff/create_table/add_table_no_online_rewrite/plan.txt +++ b/testdata/diff/create_table/add_table_no_online_rewrite/plan.txt @@ -12,16 +12,19 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS companies ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT companies_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, + id integer, name text NOT NULL, - company_id integer NOT NULL REFERENCES companies (id), + company_id integer NOT NULL, budget numeric(10,2), created_at timestamp DEFAULT now(), + CONSTRAINT departments_pkey PRIMARY KEY (id), + CONSTRAINT departments_company_id_fkey FOREIGN KEY (company_id) REFERENCES companies (id), CONSTRAINT departments_budget_check CHECK (budget > 0) ); diff --git a/testdata/diff/create_table/add_table_serial_pk/diff.sql b/testdata/diff/create_table/add_table_serial_pk/diff.sql index 9cede6cf..23cb0673 100644 --- a/testdata/diff/create_table/add_table_serial_pk/diff.sql +++ b/testdata/diff/create_table/add_table_serial_pk/diff.sql @@ -1,5 +1,6 @@ CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, name text, - email text + email text, + CONSTRAINT users_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_table_serial_pk/plan.json b/testdata/diff/create_table/add_table_serial_pk/plan.json index 3c9d531e..00e315bf 100644 --- a/testdata/diff/create_table/add_table_serial_pk/plan.json +++ b/testdata/diff/create_table/add_table_serial_pk/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS users (\n id SERIAL PRIMARY KEY,\n name text,\n email text\n);", + "sql": "CREATE TABLE IF NOT EXISTS users (\n id SERIAL,\n name text,\n email text,\n CONSTRAINT users_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.users" diff --git a/testdata/diff/create_table/add_table_serial_pk/plan.sql b/testdata/diff/create_table/add_table_serial_pk/plan.sql index 9cede6cf..23cb0673 100644 --- a/testdata/diff/create_table/add_table_serial_pk/plan.sql +++ b/testdata/diff/create_table/add_table_serial_pk/plan.sql @@ -1,5 +1,6 @@ CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, name text, - email text + email text, + CONSTRAINT users_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_table_serial_pk/plan.txt b/testdata/diff/create_table/add_table_serial_pk/plan.txt index 6c645a29..f8b3105b 100644 --- a/testdata/diff/create_table/add_table_serial_pk/plan.txt +++ b/testdata/diff/create_table/add_table_serial_pk/plan.txt @@ -10,7 +10,8 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, name text, - email text + email text, + CONSTRAINT users_pkey PRIMARY KEY (id) ); diff --git a/testdata/diff/create_table/add_uk_bigint/diff.sql b/testdata/diff/create_table/add_uk_bigint/diff.sql index d11fac34..d691d218 100644 --- a/testdata/diff/create_table/add_uk_bigint/diff.sql +++ b/testdata/diff/create_table/add_uk_bigint/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE transactions -ADD COLUMN id bigint UNIQUE; +ADD COLUMN id bigint CONSTRAINT transactions_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_bigint/new.sql b/testdata/diff/create_table/add_uk_bigint/new.sql index 479fb194..0cc9e457 100644 --- a/testdata/diff/create_table/add_uk_bigint/new.sql +++ b/testdata/diff/create_table/add_uk_bigint/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.transactions ( - id bigint UNIQUE, + id bigint, amount numeric(15,2) NOT NULL, - created_at timestamp with time zone + created_at timestamp with time zone, + CONSTRAINT transactions_id_key UNIQUE (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_uk_bigint/plan.json b/testdata/diff/create_table/add_uk_bigint/plan.json index 617ea017..c0d5082b 100644 --- a/testdata/diff/create_table/add_uk_bigint/plan.json +++ b/testdata/diff/create_table/add_uk_bigint/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE transactions\nADD COLUMN id bigint UNIQUE;", + "sql": "ALTER TABLE transactions\nADD COLUMN id bigint CONSTRAINT transactions_id_key UNIQUE;", "type": "table.column", "operation": "create", "path": "public.transactions.id" diff --git a/testdata/diff/create_table/add_uk_bigint/plan.sql b/testdata/diff/create_table/add_uk_bigint/plan.sql index d11fac34..d691d218 100644 --- a/testdata/diff/create_table/add_uk_bigint/plan.sql +++ b/testdata/diff/create_table/add_uk_bigint/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE transactions -ADD COLUMN id bigint UNIQUE; +ADD COLUMN id bigint CONSTRAINT transactions_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_bigint/plan.txt b/testdata/diff/create_table/add_uk_bigint/plan.txt index 1fbae8cc..95bd5aa1 100644 --- a/testdata/diff/create_table/add_uk_bigint/plan.txt +++ b/testdata/diff/create_table/add_uk_bigint/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE transactions -ADD COLUMN id bigint UNIQUE; +ADD COLUMN id bigint CONSTRAINT transactions_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_identity/diff.sql b/testdata/diff/create_table/add_uk_identity/diff.sql index eeb2580e..d91f4442 100644 --- a/testdata/diff/create_table/add_uk_identity/diff.sql +++ b/testdata/diff/create_table/add_uk_identity/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY UNIQUE; +ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_identity/new.sql b/testdata/diff/create_table/add_uk_identity/new.sql index 9ffb3a03..2077037d 100644 --- a/testdata/diff/create_table/add_uk_identity/new.sql +++ b/testdata/diff/create_table/add_uk_identity/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.users ( - id integer GENERATED ALWAYS AS IDENTITY UNIQUE, + id integer GENERATED ALWAYS AS IDENTITY, username text NOT NULL, - email text + email text, + CONSTRAINT users_id_key UNIQUE (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_uk_identity/plan.json b/testdata/diff/create_table/add_uk_identity/plan.json index be02b631..5882e08f 100644 --- a/testdata/diff/create_table/add_uk_identity/plan.json +++ b/testdata/diff/create_table/add_uk_identity/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE users\nADD COLUMN id integer GENERATED ALWAYS AS IDENTITY UNIQUE;", + "sql": "ALTER TABLE users\nADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_id_key UNIQUE;", "type": "table.column", "operation": "create", "path": "public.users.id" diff --git a/testdata/diff/create_table/add_uk_identity/plan.sql b/testdata/diff/create_table/add_uk_identity/plan.sql index eeb2580e..d91f4442 100644 --- a/testdata/diff/create_table/add_uk_identity/plan.sql +++ b/testdata/diff/create_table/add_uk_identity/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY UNIQUE; +ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_identity/plan.txt b/testdata/diff/create_table/add_uk_identity/plan.txt index 71c8956a..193b1b59 100644 --- a/testdata/diff/create_table/add_uk_identity/plan.txt +++ b/testdata/diff/create_table/add_uk_identity/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE users -ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY UNIQUE; +ADD COLUMN id integer GENERATED ALWAYS AS IDENTITY CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_serial/diff.sql b/testdata/diff/create_table/add_uk_serial/diff.sql index 3d683c88..5d51092c 100644 --- a/testdata/diff/create_table/add_uk_serial/diff.sql +++ b/testdata/diff/create_table/add_uk_serial/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id serial UNIQUE; +ADD COLUMN id serial CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_serial/new.sql b/testdata/diff/create_table/add_uk_serial/new.sql index 6715d62c..5065808c 100644 --- a/testdata/diff/create_table/add_uk_serial/new.sql +++ b/testdata/diff/create_table/add_uk_serial/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.users ( - id serial UNIQUE, + id serial, username text NOT NULL, - email text + email text, + CONSTRAINT users_id_key UNIQUE (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_uk_serial/plan.json b/testdata/diff/create_table/add_uk_serial/plan.json index 0c5906f9..945aa7a5 100644 --- a/testdata/diff/create_table/add_uk_serial/plan.json +++ b/testdata/diff/create_table/add_uk_serial/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE users\nADD COLUMN id serial UNIQUE;", + "sql": "ALTER TABLE users\nADD COLUMN id serial CONSTRAINT users_id_key UNIQUE;", "type": "table.column", "operation": "create", "path": "public.users.id" diff --git a/testdata/diff/create_table/add_uk_serial/plan.sql b/testdata/diff/create_table/add_uk_serial/plan.sql index 3d683c88..5d51092c 100644 --- a/testdata/diff/create_table/add_uk_serial/plan.sql +++ b/testdata/diff/create_table/add_uk_serial/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id serial UNIQUE; +ADD COLUMN id serial CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_serial/plan.txt b/testdata/diff/create_table/add_uk_serial/plan.txt index c015796f..e77e2d9a 100644 --- a/testdata/diff/create_table/add_uk_serial/plan.txt +++ b/testdata/diff/create_table/add_uk_serial/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE users -ADD COLUMN id serial UNIQUE; +ADD COLUMN id serial CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_single/diff.sql b/testdata/diff/create_table/add_uk_single/diff.sql index d61fbca4..61563fcd 100644 --- a/testdata/diff/create_table/add_uk_single/diff.sql +++ b/testdata/diff/create_table/add_uk_single/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer UNIQUE; +ADD COLUMN id integer CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_single/new.sql b/testdata/diff/create_table/add_uk_single/new.sql index 90175c18..9b38243d 100644 --- a/testdata/diff/create_table/add_uk_single/new.sql +++ b/testdata/diff/create_table/add_uk_single/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.users ( - id integer UNIQUE, + id integer, username text NOT NULL, - email text + email text, + CONSTRAINT users_id_key UNIQUE (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_uk_single/plan.json b/testdata/diff/create_table/add_uk_single/plan.json index 66b016ed..9e9d377b 100644 --- a/testdata/diff/create_table/add_uk_single/plan.json +++ b/testdata/diff/create_table/add_uk_single/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE users\nADD COLUMN id integer UNIQUE;", + "sql": "ALTER TABLE users\nADD COLUMN id integer CONSTRAINT users_id_key UNIQUE;", "type": "table.column", "operation": "create", "path": "public.users.id" diff --git a/testdata/diff/create_table/add_uk_single/plan.sql b/testdata/diff/create_table/add_uk_single/plan.sql index d61fbca4..61563fcd 100644 --- a/testdata/diff/create_table/add_uk_single/plan.sql +++ b/testdata/diff/create_table/add_uk_single/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE users -ADD COLUMN id integer UNIQUE; +ADD COLUMN id integer CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_single/plan.txt b/testdata/diff/create_table/add_uk_single/plan.txt index 38cf7dfc..694a46e4 100644 --- a/testdata/diff/create_table/add_uk_single/plan.txt +++ b/testdata/diff/create_table/add_uk_single/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE users -ADD COLUMN id integer UNIQUE; +ADD COLUMN id integer CONSTRAINT users_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_text/diff.sql b/testdata/diff/create_table/add_uk_text/diff.sql index 64259237..ca8b062b 100644 --- a/testdata/diff/create_table/add_uk_text/diff.sql +++ b/testdata/diff/create_table/add_uk_text/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE countries -ADD COLUMN code text UNIQUE; +ADD COLUMN code text CONSTRAINT countries_code_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_text/new.sql b/testdata/diff/create_table/add_uk_text/new.sql index 89ff3e96..17fba953 100644 --- a/testdata/diff/create_table/add_uk_text/new.sql +++ b/testdata/diff/create_table/add_uk_text/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.countries ( - code text UNIQUE, + code text, name text NOT NULL, - continent text + continent text, + CONSTRAINT countries_code_key UNIQUE (code) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_uk_text/plan.json b/testdata/diff/create_table/add_uk_text/plan.json index 4a8bb48b..aa5971be 100644 --- a/testdata/diff/create_table/add_uk_text/plan.json +++ b/testdata/diff/create_table/add_uk_text/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE countries\nADD COLUMN code text UNIQUE;", + "sql": "ALTER TABLE countries\nADD COLUMN code text CONSTRAINT countries_code_key UNIQUE;", "type": "table.column", "operation": "create", "path": "public.countries.code" diff --git a/testdata/diff/create_table/add_uk_text/plan.sql b/testdata/diff/create_table/add_uk_text/plan.sql index 64259237..ca8b062b 100644 --- a/testdata/diff/create_table/add_uk_text/plan.sql +++ b/testdata/diff/create_table/add_uk_text/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE countries -ADD COLUMN code text UNIQUE; +ADD COLUMN code text CONSTRAINT countries_code_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_text/plan.txt b/testdata/diff/create_table/add_uk_text/plan.txt index deb6e2e1..e1765a57 100644 --- a/testdata/diff/create_table/add_uk_text/plan.txt +++ b/testdata/diff/create_table/add_uk_text/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE countries -ADD COLUMN code text UNIQUE; +ADD COLUMN code text CONSTRAINT countries_code_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_uuid/diff.sql b/testdata/diff/create_table/add_uk_uuid/diff.sql index 6375c499..e9a1437f 100644 --- a/testdata/diff/create_table/add_uk_uuid/diff.sql +++ b/testdata/diff/create_table/add_uk_uuid/diff.sql @@ -1,2 +1,2 @@ ALTER TABLE documents -ADD COLUMN id uuid UNIQUE; +ADD COLUMN id uuid CONSTRAINT documents_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_uuid/new.sql b/testdata/diff/create_table/add_uk_uuid/new.sql index c4362730..78090481 100644 --- a/testdata/diff/create_table/add_uk_uuid/new.sql +++ b/testdata/diff/create_table/add_uk_uuid/new.sql @@ -1,5 +1,6 @@ CREATE TABLE public.documents ( - id uuid UNIQUE, + id uuid, title text NOT NULL, - content text + content text, + CONSTRAINT documents_id_key UNIQUE (id) ); \ No newline at end of file diff --git a/testdata/diff/create_table/add_uk_uuid/plan.json b/testdata/diff/create_table/add_uk_uuid/plan.json index e9dfc03a..1ee5f47d 100644 --- a/testdata/diff/create_table/add_uk_uuid/plan.json +++ b/testdata/diff/create_table/add_uk_uuid/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "ALTER TABLE documents\nADD COLUMN id uuid UNIQUE;", + "sql": "ALTER TABLE documents\nADD COLUMN id uuid CONSTRAINT documents_id_key UNIQUE;", "type": "table.column", "operation": "create", "path": "public.documents.id" diff --git a/testdata/diff/create_table/add_uk_uuid/plan.sql b/testdata/diff/create_table/add_uk_uuid/plan.sql index 6375c499..e9a1437f 100644 --- a/testdata/diff/create_table/add_uk_uuid/plan.sql +++ b/testdata/diff/create_table/add_uk_uuid/plan.sql @@ -1,2 +1,2 @@ ALTER TABLE documents -ADD COLUMN id uuid UNIQUE; +ADD COLUMN id uuid CONSTRAINT documents_id_key UNIQUE; diff --git a/testdata/diff/create_table/add_uk_uuid/plan.txt b/testdata/diff/create_table/add_uk_uuid/plan.txt index f91f8a77..ecc9d49e 100644 --- a/testdata/diff/create_table/add_uk_uuid/plan.txt +++ b/testdata/diff/create_table/add_uk_uuid/plan.txt @@ -11,4 +11,4 @@ DDL to be executed: -------------------------------------------------- ALTER TABLE documents -ADD COLUMN id uuid UNIQUE; +ADD COLUMN id uuid CONSTRAINT documents_id_key UNIQUE; diff --git a/testdata/diff/dependency/function_to_trigger/diff.sql b/testdata/diff/dependency/function_to_trigger/diff.sql index 395c898a..118ef64e 100644 --- a/testdata/diff/dependency/function_to_trigger/diff.sql +++ b/testdata/diff/dependency/function_to_trigger/diff.sql @@ -1,9 +1,11 @@ CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, name text NOT NULL, - email text UNIQUE, + email text, created_at timestamp DEFAULT CURRENT_TIMESTAMP, - updated_at timestamp DEFAULT CURRENT_TIMESTAMP + updated_at timestamp DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT users_pkey PRIMARY KEY (id), + CONSTRAINT users_email_key UNIQUE (email) ); CREATE OR REPLACE FUNCTION update_modified_time() diff --git a/testdata/diff/dependency/function_to_trigger/plan.json b/testdata/diff/dependency/function_to_trigger/plan.json index eff5c92b..871bebbe 100644 --- a/testdata/diff/dependency/function_to_trigger/plan.json +++ b/testdata/diff/dependency/function_to_trigger/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS users (\n id SERIAL PRIMARY KEY,\n name text NOT NULL,\n email text UNIQUE,\n created_at timestamp DEFAULT CURRENT_TIMESTAMP,\n updated_at timestamp DEFAULT CURRENT_TIMESTAMP\n);", + "sql": "CREATE TABLE IF NOT EXISTS users (\n id SERIAL,\n name text NOT NULL,\n email text,\n created_at timestamp DEFAULT CURRENT_TIMESTAMP,\n updated_at timestamp DEFAULT CURRENT_TIMESTAMP,\n CONSTRAINT users_pkey PRIMARY KEY (id),\n CONSTRAINT users_email_key UNIQUE (email)\n);", "type": "table", "operation": "create", "path": "public.users" diff --git a/testdata/diff/dependency/function_to_trigger/plan.sql b/testdata/diff/dependency/function_to_trigger/plan.sql index 395c898a..118ef64e 100644 --- a/testdata/diff/dependency/function_to_trigger/plan.sql +++ b/testdata/diff/dependency/function_to_trigger/plan.sql @@ -1,9 +1,11 @@ CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, name text NOT NULL, - email text UNIQUE, + email text, created_at timestamp DEFAULT CURRENT_TIMESTAMP, - updated_at timestamp DEFAULT CURRENT_TIMESTAMP + updated_at timestamp DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT users_pkey PRIMARY KEY (id), + CONSTRAINT users_email_key UNIQUE (email) ); CREATE OR REPLACE FUNCTION update_modified_time() diff --git a/testdata/diff/dependency/function_to_trigger/plan.txt b/testdata/diff/dependency/function_to_trigger/plan.txt index 09725ba2..dafd2521 100644 --- a/testdata/diff/dependency/function_to_trigger/plan.txt +++ b/testdata/diff/dependency/function_to_trigger/plan.txt @@ -15,11 +15,13 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, name text NOT NULL, - email text UNIQUE, + email text, created_at timestamp DEFAULT CURRENT_TIMESTAMP, - updated_at timestamp DEFAULT CURRENT_TIMESTAMP + updated_at timestamp DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT users_pkey PRIMARY KEY (id), + CONSTRAINT users_email_key UNIQUE (email) ); CREATE OR REPLACE FUNCTION update_modified_time() diff --git a/testdata/diff/dependency/table_to_function/diff.sql b/testdata/diff/dependency/table_to_function/diff.sql index 20467cac..bf16996a 100644 --- a/testdata/diff/dependency/table_to_function/diff.sql +++ b/testdata/diff/dependency/table_to_function/diff.sql @@ -1,8 +1,9 @@ CREATE TABLE IF NOT EXISTS documents ( - id SERIAL PRIMARY KEY, + id SERIAL, title text NOT NULL, content text, - created_at timestamp DEFAULT CURRENT_TIMESTAMP + created_at timestamp DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT documents_pkey PRIMARY KEY (id) ); CREATE OR REPLACE FUNCTION get_document_count() diff --git a/testdata/diff/dependency/table_to_function/plan.json b/testdata/diff/dependency/table_to_function/plan.json index 38353438..79f272f6 100644 --- a/testdata/diff/dependency/table_to_function/plan.json +++ b/testdata/diff/dependency/table_to_function/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS documents (\n id SERIAL PRIMARY KEY,\n title text NOT NULL,\n content text,\n created_at timestamp DEFAULT CURRENT_TIMESTAMP\n);", + "sql": "CREATE TABLE IF NOT EXISTS documents (\n id SERIAL,\n title text NOT NULL,\n content text,\n created_at timestamp DEFAULT CURRENT_TIMESTAMP,\n CONSTRAINT documents_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.documents" diff --git a/testdata/diff/dependency/table_to_function/plan.sql b/testdata/diff/dependency/table_to_function/plan.sql index 20467cac..bf16996a 100644 --- a/testdata/diff/dependency/table_to_function/plan.sql +++ b/testdata/diff/dependency/table_to_function/plan.sql @@ -1,8 +1,9 @@ CREATE TABLE IF NOT EXISTS documents ( - id SERIAL PRIMARY KEY, + id SERIAL, title text NOT NULL, content text, - created_at timestamp DEFAULT CURRENT_TIMESTAMP + created_at timestamp DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT documents_pkey PRIMARY KEY (id) ); CREATE OR REPLACE FUNCTION get_document_count() diff --git a/testdata/diff/dependency/table_to_function/plan.txt b/testdata/diff/dependency/table_to_function/plan.txt index 70d3191c..c3558420 100644 --- a/testdata/diff/dependency/table_to_function/plan.txt +++ b/testdata/diff/dependency/table_to_function/plan.txt @@ -14,10 +14,11 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS documents ( - id SERIAL PRIMARY KEY, + id SERIAL, title text NOT NULL, content text, - created_at timestamp DEFAULT CURRENT_TIMESTAMP + created_at timestamp DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT documents_pkey PRIMARY KEY (id) ); CREATE OR REPLACE FUNCTION get_document_count() diff --git a/testdata/diff/dependency/table_to_table/diff.sql b/testdata/diff/dependency/table_to_table/diff.sql index ee27da57..085a72e5 100644 --- a/testdata/diff/dependency/table_to_table/diff.sql +++ b/testdata/diff/dependency/table_to_table/diff.sql @@ -1,11 +1,15 @@ CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT departments_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS users ( - id integer PRIMARY KEY, + id integer, name text, - email text UNIQUE, - department_id integer REFERENCES departments (id) + email text, + department_id integer, + CONSTRAINT users_pkey PRIMARY KEY (id), + CONSTRAINT users_email_key UNIQUE (email), + CONSTRAINT users_department_id_fkey FOREIGN KEY (department_id) REFERENCES departments (id) ); diff --git a/testdata/diff/dependency/table_to_table/plan.json b/testdata/diff/dependency/table_to_table/plan.json index ee2c97ef..461b63e4 100644 --- a/testdata/diff/dependency/table_to_table/plan.json +++ b/testdata/diff/dependency/table_to_table/plan.json @@ -9,13 +9,13 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS departments (\n id integer PRIMARY KEY,\n name text NOT NULL\n);", + "sql": "CREATE TABLE IF NOT EXISTS departments (\n id integer,\n name text NOT NULL,\n CONSTRAINT departments_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.departments" }, { - "sql": "CREATE TABLE IF NOT EXISTS users (\n id integer PRIMARY KEY,\n name text,\n email text UNIQUE,\n department_id integer REFERENCES departments (id)\n);", + "sql": "CREATE TABLE IF NOT EXISTS users (\n id integer,\n name text,\n email text,\n department_id integer,\n CONSTRAINT users_pkey PRIMARY KEY (id),\n CONSTRAINT users_email_key UNIQUE (email),\n CONSTRAINT users_department_id_fkey FOREIGN KEY (department_id) REFERENCES departments (id)\n);", "type": "table", "operation": "create", "path": "public.users" diff --git a/testdata/diff/dependency/table_to_table/plan.sql b/testdata/diff/dependency/table_to_table/plan.sql index ee27da57..085a72e5 100644 --- a/testdata/diff/dependency/table_to_table/plan.sql +++ b/testdata/diff/dependency/table_to_table/plan.sql @@ -1,11 +1,15 @@ CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT departments_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS users ( - id integer PRIMARY KEY, + id integer, name text, - email text UNIQUE, - department_id integer REFERENCES departments (id) + email text, + department_id integer, + CONSTRAINT users_pkey PRIMARY KEY (id), + CONSTRAINT users_email_key UNIQUE (email), + CONSTRAINT users_department_id_fkey FOREIGN KEY (department_id) REFERENCES departments (id) ); diff --git a/testdata/diff/dependency/table_to_table/plan.txt b/testdata/diff/dependency/table_to_table/plan.txt index f3e5f99c..cc9d15ab 100644 --- a/testdata/diff/dependency/table_to_table/plan.txt +++ b/testdata/diff/dependency/table_to_table/plan.txt @@ -11,13 +11,17 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS departments ( - id integer PRIMARY KEY, - name text NOT NULL + id integer, + name text NOT NULL, + CONSTRAINT departments_pkey PRIMARY KEY (id) ); CREATE TABLE IF NOT EXISTS users ( - id integer PRIMARY KEY, + id integer, name text, - email text UNIQUE, - department_id integer REFERENCES departments (id) + email text, + department_id integer, + CONSTRAINT users_pkey PRIMARY KEY (id), + CONSTRAINT users_email_key UNIQUE (email), + CONSTRAINT users_department_id_fkey FOREIGN KEY (department_id) REFERENCES departments (id) ); diff --git a/testdata/diff/migrate/v1/diff.sql b/testdata/diff/migrate/v1/diff.sql index 2292ffec..c2fc5910 100644 --- a/testdata/diff/migrate/v1/diff.sql +++ b/testdata/diff/migrate/v1/diff.sql @@ -1,45 +1,53 @@ CREATE TABLE IF NOT EXISTS department ( - dept_no text PRIMARY KEY, - dept_name text NOT NULL + dept_no text, + dept_name text NOT NULL, + CONSTRAINT department_pkey PRIMARY KEY (dept_no) ); CREATE TABLE IF NOT EXISTS employee ( - emp_no SERIAL PRIMARY KEY, + emp_no SERIAL, birth_date date NOT NULL, first_name text NOT NULL, last_name text NOT NULL, gender text NOT NULL, - hire_date date NOT NULL + hire_date date NOT NULL, + CONSTRAINT employee_pkey PRIMARY KEY (emp_no) ); CREATE TABLE IF NOT EXISTS dept_emp ( - emp_no integer REFERENCES employee (emp_no), - dept_no text REFERENCES department (dept_no), + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_emp_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_emp_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no), + CONSTRAINT dept_emp_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS dept_manager ( - emp_no integer REFERENCES employee (emp_no), - dept_no text REFERENCES department (dept_no), + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_manager_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_manager_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no), + CONSTRAINT dept_manager_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS salary ( - emp_no integer REFERENCES employee (emp_no), + emp_no integer, amount integer NOT NULL, from_date date, to_date date NOT NULL, - PRIMARY KEY (emp_no, from_date) + CONSTRAINT salary_pkey PRIMARY KEY (emp_no, from_date), + CONSTRAINT salary_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS title ( - emp_no integer REFERENCES employee (emp_no), + emp_no integer, title text, from_date date, to_date date, - PRIMARY KEY (emp_no, title, from_date) + CONSTRAINT title_pkey PRIMARY KEY (emp_no, title, from_date), + CONSTRAINT title_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); diff --git a/testdata/diff/migrate/v1/plan.json b/testdata/diff/migrate/v1/plan.json index 1cd1391a..81cff889 100644 --- a/testdata/diff/migrate/v1/plan.json +++ b/testdata/diff/migrate/v1/plan.json @@ -9,37 +9,37 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS department (\n dept_no text PRIMARY KEY,\n dept_name text NOT NULL\n);", + "sql": "CREATE TABLE IF NOT EXISTS department (\n dept_no text,\n dept_name text NOT NULL,\n CONSTRAINT department_pkey PRIMARY KEY (dept_no)\n);", "type": "table", "operation": "create", "path": "public.department" }, { - "sql": "CREATE TABLE IF NOT EXISTS employee (\n emp_no SERIAL PRIMARY KEY,\n birth_date date NOT NULL,\n first_name text NOT NULL,\n last_name text NOT NULL,\n gender text NOT NULL,\n hire_date date NOT NULL\n);", + "sql": "CREATE TABLE IF NOT EXISTS employee (\n emp_no SERIAL,\n birth_date date NOT NULL,\n first_name text NOT NULL,\n last_name text NOT NULL,\n gender text NOT NULL,\n hire_date date NOT NULL,\n CONSTRAINT employee_pkey PRIMARY KEY (emp_no)\n);", "type": "table", "operation": "create", "path": "public.employee" }, { - "sql": "CREATE TABLE IF NOT EXISTS dept_emp (\n emp_no integer REFERENCES employee (emp_no),\n dept_no text REFERENCES department (dept_no),\n from_date date NOT NULL,\n to_date date NOT NULL,\n PRIMARY KEY (emp_no, dept_no)\n);", + "sql": "CREATE TABLE IF NOT EXISTS dept_emp (\n emp_no integer,\n dept_no text,\n from_date date NOT NULL,\n to_date date NOT NULL,\n CONSTRAINT dept_emp_pkey PRIMARY KEY (emp_no, dept_no),\n CONSTRAINT dept_emp_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no),\n CONSTRAINT dept_emp_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no)\n);", "type": "table", "operation": "create", "path": "public.dept_emp" }, { - "sql": "CREATE TABLE IF NOT EXISTS dept_manager (\n emp_no integer REFERENCES employee (emp_no),\n dept_no text REFERENCES department (dept_no),\n from_date date NOT NULL,\n to_date date NOT NULL,\n PRIMARY KEY (emp_no, dept_no)\n);", + "sql": "CREATE TABLE IF NOT EXISTS dept_manager (\n emp_no integer,\n dept_no text,\n from_date date NOT NULL,\n to_date date NOT NULL,\n CONSTRAINT dept_manager_pkey PRIMARY KEY (emp_no, dept_no),\n CONSTRAINT dept_manager_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no),\n CONSTRAINT dept_manager_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no)\n);", "type": "table", "operation": "create", "path": "public.dept_manager" }, { - "sql": "CREATE TABLE IF NOT EXISTS salary (\n emp_no integer REFERENCES employee (emp_no),\n amount integer NOT NULL,\n from_date date,\n to_date date NOT NULL,\n PRIMARY KEY (emp_no, from_date)\n);", + "sql": "CREATE TABLE IF NOT EXISTS salary (\n emp_no integer,\n amount integer NOT NULL,\n from_date date,\n to_date date NOT NULL,\n CONSTRAINT salary_pkey PRIMARY KEY (emp_no, from_date),\n CONSTRAINT salary_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no)\n);", "type": "table", "operation": "create", "path": "public.salary" }, { - "sql": "CREATE TABLE IF NOT EXISTS title (\n emp_no integer REFERENCES employee (emp_no),\n title text,\n from_date date,\n to_date date,\n PRIMARY KEY (emp_no, title, from_date)\n);", + "sql": "CREATE TABLE IF NOT EXISTS title (\n emp_no integer,\n title text,\n from_date date,\n to_date date,\n CONSTRAINT title_pkey PRIMARY KEY (emp_no, title, from_date),\n CONSTRAINT title_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no)\n);", "type": "table", "operation": "create", "path": "public.title" diff --git a/testdata/diff/migrate/v1/plan.sql b/testdata/diff/migrate/v1/plan.sql index 2292ffec..c2fc5910 100644 --- a/testdata/diff/migrate/v1/plan.sql +++ b/testdata/diff/migrate/v1/plan.sql @@ -1,45 +1,53 @@ CREATE TABLE IF NOT EXISTS department ( - dept_no text PRIMARY KEY, - dept_name text NOT NULL + dept_no text, + dept_name text NOT NULL, + CONSTRAINT department_pkey PRIMARY KEY (dept_no) ); CREATE TABLE IF NOT EXISTS employee ( - emp_no SERIAL PRIMARY KEY, + emp_no SERIAL, birth_date date NOT NULL, first_name text NOT NULL, last_name text NOT NULL, gender text NOT NULL, - hire_date date NOT NULL + hire_date date NOT NULL, + CONSTRAINT employee_pkey PRIMARY KEY (emp_no) ); CREATE TABLE IF NOT EXISTS dept_emp ( - emp_no integer REFERENCES employee (emp_no), - dept_no text REFERENCES department (dept_no), + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_emp_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_emp_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no), + CONSTRAINT dept_emp_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS dept_manager ( - emp_no integer REFERENCES employee (emp_no), - dept_no text REFERENCES department (dept_no), + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_manager_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_manager_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no), + CONSTRAINT dept_manager_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS salary ( - emp_no integer REFERENCES employee (emp_no), + emp_no integer, amount integer NOT NULL, from_date date, to_date date NOT NULL, - PRIMARY KEY (emp_no, from_date) + CONSTRAINT salary_pkey PRIMARY KEY (emp_no, from_date), + CONSTRAINT salary_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS title ( - emp_no integer REFERENCES employee (emp_no), + emp_no integer, title text, from_date date, to_date date, - PRIMARY KEY (emp_no, title, from_date) + CONSTRAINT title_pkey PRIMARY KEY (emp_no, title, from_date), + CONSTRAINT title_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); diff --git a/testdata/diff/migrate/v1/plan.txt b/testdata/diff/migrate/v1/plan.txt index c792e060..c4d3c2ff 100644 --- a/testdata/diff/migrate/v1/plan.txt +++ b/testdata/diff/migrate/v1/plan.txt @@ -15,47 +15,55 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS department ( - dept_no text PRIMARY KEY, - dept_name text NOT NULL + dept_no text, + dept_name text NOT NULL, + CONSTRAINT department_pkey PRIMARY KEY (dept_no) ); CREATE TABLE IF NOT EXISTS employee ( - emp_no SERIAL PRIMARY KEY, + emp_no SERIAL, birth_date date NOT NULL, first_name text NOT NULL, last_name text NOT NULL, gender text NOT NULL, - hire_date date NOT NULL + hire_date date NOT NULL, + CONSTRAINT employee_pkey PRIMARY KEY (emp_no) ); CREATE TABLE IF NOT EXISTS dept_emp ( - emp_no integer REFERENCES employee (emp_no), - dept_no text REFERENCES department (dept_no), + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_emp_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_emp_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no), + CONSTRAINT dept_emp_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS dept_manager ( - emp_no integer REFERENCES employee (emp_no), - dept_no text REFERENCES department (dept_no), + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_manager_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_manager_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no), + CONSTRAINT dept_manager_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS salary ( - emp_no integer REFERENCES employee (emp_no), + emp_no integer, amount integer NOT NULL, from_date date, to_date date NOT NULL, - PRIMARY KEY (emp_no, from_date) + CONSTRAINT salary_pkey PRIMARY KEY (emp_no, from_date), + CONSTRAINT salary_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); CREATE TABLE IF NOT EXISTS title ( - emp_no integer REFERENCES employee (emp_no), + emp_no integer, title text, from_date date, to_date date, - PRIMARY KEY (emp_no, title, from_date) + CONSTRAINT title_pkey PRIMARY KEY (emp_no, title, from_date), + CONSTRAINT title_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ); diff --git a/testdata/diff/migrate/v3/diff.sql b/testdata/diff/migrate/v3/diff.sql index 42748a5c..601646cc 100644 --- a/testdata/diff/migrate/v3/diff.sql +++ b/testdata/diff/migrate/v3/diff.sql @@ -1,9 +1,10 @@ CREATE TABLE IF NOT EXISTS audit ( - id SERIAL PRIMARY KEY, + id SERIAL, operation text NOT NULL, query text, user_name text NOT NULL, - changed_at timestamptz DEFAULT CURRENT_TIMESTAMP + changed_at timestamptz DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT audit_pkey PRIMARY KEY (id) ); CREATE INDEX IF NOT EXISTS idx_audit_changed_at ON audit (changed_at); diff --git a/testdata/diff/migrate/v3/plan.json b/testdata/diff/migrate/v3/plan.json index 3f967213..f093774d 100644 --- a/testdata/diff/migrate/v3/plan.json +++ b/testdata/diff/migrate/v3/plan.json @@ -9,7 +9,7 @@ { "steps": [ { - "sql": "CREATE TABLE IF NOT EXISTS audit (\n id SERIAL PRIMARY KEY,\n operation text NOT NULL,\n query text,\n user_name text NOT NULL,\n changed_at timestamptz DEFAULT CURRENT_TIMESTAMP\n);", + "sql": "CREATE TABLE IF NOT EXISTS audit (\n id SERIAL,\n operation text NOT NULL,\n query text,\n user_name text NOT NULL,\n changed_at timestamptz DEFAULT CURRENT_TIMESTAMP,\n CONSTRAINT audit_pkey PRIMARY KEY (id)\n);", "type": "table", "operation": "create", "path": "public.audit" diff --git a/testdata/diff/migrate/v3/plan.sql b/testdata/diff/migrate/v3/plan.sql index 42748a5c..601646cc 100644 --- a/testdata/diff/migrate/v3/plan.sql +++ b/testdata/diff/migrate/v3/plan.sql @@ -1,9 +1,10 @@ CREATE TABLE IF NOT EXISTS audit ( - id SERIAL PRIMARY KEY, + id SERIAL, operation text NOT NULL, query text, user_name text NOT NULL, - changed_at timestamptz DEFAULT CURRENT_TIMESTAMP + changed_at timestamptz DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT audit_pkey PRIMARY KEY (id) ); CREATE INDEX IF NOT EXISTS idx_audit_changed_at ON audit (changed_at); diff --git a/testdata/diff/migrate/v3/plan.txt b/testdata/diff/migrate/v3/plan.txt index 222133a0..cccb1741 100644 --- a/testdata/diff/migrate/v3/plan.txt +++ b/testdata/diff/migrate/v3/plan.txt @@ -17,11 +17,12 @@ DDL to be executed: -------------------------------------------------- CREATE TABLE IF NOT EXISTS audit ( - id SERIAL PRIMARY KEY, + id SERIAL, operation text NOT NULL, query text, user_name text NOT NULL, - changed_at timestamptz DEFAULT CURRENT_TIMESTAMP + changed_at timestamptz DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT audit_pkey PRIMARY KEY (id) ); CREATE INDEX IF NOT EXISTS idx_audit_changed_at ON audit (changed_at); diff --git a/testdata/diff/migrate/v5/diff.sql b/testdata/diff/migrate/v5/diff.sql index a33897e8..178b1412 100644 --- a/testdata/diff/migrate/v5/diff.sql +++ b/testdata/diff/migrate/v5/diff.sql @@ -11,11 +11,13 @@ CREATE TYPE employee_status AS ENUM ( ); CREATE TABLE IF NOT EXISTS employee_status_log ( - id SERIAL PRIMARY KEY, - emp_no integer NOT NULL REFERENCES employee (emp_no) ON DELETE CASCADE, + id SERIAL, + emp_no integer NOT NULL, status employee_status NOT NULL, effective_date date DEFAULT CURRENT_DATE NOT NULL, - notes text + notes text, + CONSTRAINT employee_status_log_pkey PRIMARY KEY (id), + CONSTRAINT employee_status_log_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS idx_employee_status_log_effective_date ON employee_status_log (effective_date); diff --git a/testdata/diff/migrate/v5/plan.json b/testdata/diff/migrate/v5/plan.json index c2d3d263..fc1499f8 100644 --- a/testdata/diff/migrate/v5/plan.json +++ b/testdata/diff/migrate/v5/plan.json @@ -33,7 +33,7 @@ "path": "public.employee_status" }, { - "sql": "CREATE TABLE IF NOT EXISTS employee_status_log (\n id SERIAL PRIMARY KEY,\n emp_no integer NOT NULL REFERENCES employee (emp_no) ON DELETE CASCADE,\n status employee_status NOT NULL,\n effective_date date DEFAULT CURRENT_DATE NOT NULL,\n notes text\n);", + "sql": "CREATE TABLE IF NOT EXISTS employee_status_log (\n id SERIAL,\n emp_no integer NOT NULL,\n status employee_status NOT NULL,\n effective_date date DEFAULT CURRENT_DATE NOT NULL,\n notes text,\n CONSTRAINT employee_status_log_pkey PRIMARY KEY (id),\n CONSTRAINT employee_status_log_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE\n);", "type": "table", "operation": "create", "path": "public.employee_status_log" diff --git a/testdata/diff/migrate/v5/plan.sql b/testdata/diff/migrate/v5/plan.sql index a33897e8..178b1412 100644 --- a/testdata/diff/migrate/v5/plan.sql +++ b/testdata/diff/migrate/v5/plan.sql @@ -11,11 +11,13 @@ CREATE TYPE employee_status AS ENUM ( ); CREATE TABLE IF NOT EXISTS employee_status_log ( - id SERIAL PRIMARY KEY, - emp_no integer NOT NULL REFERENCES employee (emp_no) ON DELETE CASCADE, + id SERIAL, + emp_no integer NOT NULL, status employee_status NOT NULL, effective_date date DEFAULT CURRENT_DATE NOT NULL, - notes text + notes text, + CONSTRAINT employee_status_log_pkey PRIMARY KEY (id), + CONSTRAINT employee_status_log_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS idx_employee_status_log_effective_date ON employee_status_log (effective_date); diff --git a/testdata/diff/migrate/v5/plan.txt b/testdata/diff/migrate/v5/plan.txt index 9f954c82..90960853 100644 --- a/testdata/diff/migrate/v5/plan.txt +++ b/testdata/diff/migrate/v5/plan.txt @@ -41,11 +41,13 @@ CREATE TYPE employee_status AS ENUM ( ); CREATE TABLE IF NOT EXISTS employee_status_log ( - id SERIAL PRIMARY KEY, - emp_no integer NOT NULL REFERENCES employee (emp_no) ON DELETE CASCADE, + id SERIAL, + emp_no integer NOT NULL, status employee_status NOT NULL, effective_date date DEFAULT CURRENT_DATE NOT NULL, - notes text + notes text, + CONSTRAINT employee_status_log_pkey PRIMARY KEY (id), + CONSTRAINT employee_status_log_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS idx_employee_status_log_effective_date ON employee_status_log (effective_date); diff --git a/testdata/dump/bytebase/pgschema.sql b/testdata/dump/bytebase/pgschema.sql index 4d2873f4..ff2ed787 100644 --- a/testdata/dump/bytebase/pgschema.sql +++ b/testdata/dump/bytebase/pgschema.sql @@ -11,9 +11,10 @@ -- CREATE TABLE IF NOT EXISTS audit_log ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, created_at timestamptz DEFAULT now() NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT audit_log_pkey PRIMARY KEY (id) ); -- @@ -51,10 +52,11 @@ CREATE INDEX IF NOT EXISTS idx_audit_log_payload_user ON audit_log (((payload->> -- CREATE TABLE IF NOT EXISTS export_archive ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, bytes bytea, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT export_archive_pkey PRIMARY KEY (id) ); -- @@ -62,12 +64,13 @@ CREATE TABLE IF NOT EXISTS export_archive ( -- CREATE TABLE IF NOT EXISTS idp ( - id SERIAL PRIMARY KEY, + id SERIAL, resource_id text NOT NULL, name text NOT NULL, domain text NOT NULL, type text NOT NULL, config jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT idp_pkey PRIMARY KEY (id), CONSTRAINT idp_type_check CHECK (type IN ('OAUTH2', 'OIDC', 'LDAP')) ); @@ -82,11 +85,12 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_idp_unique_resource_id ON idp (resource_id -- CREATE TABLE IF NOT EXISTS instance ( - id SERIAL PRIMARY KEY, + id SERIAL, deleted boolean DEFAULT false NOT NULL, environment text, resource_id text NOT NULL, - metadata jsonb DEFAULT '{}' NOT NULL + metadata jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT instance_pkey PRIMARY KEY (id) ); -- @@ -100,9 +104,11 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_instance_unique_resource_id ON instance (r -- CREATE TABLE IF NOT EXISTS data_source ( - id SERIAL PRIMARY KEY, - instance text NOT NULL REFERENCES instance (resource_id), - options jsonb DEFAULT '{}' NOT NULL + id SERIAL, + instance text NOT NULL, + options jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT data_source_pkey PRIMARY KEY (id), + CONSTRAINT data_source_instance_fkey FOREIGN KEY (instance) REFERENCES instance (resource_id) ); -- @@ -110,8 +116,9 @@ CREATE TABLE IF NOT EXISTS data_source ( -- CREATE TABLE IF NOT EXISTS instance_change_history ( - id BIGSERIAL PRIMARY KEY, - version text NOT NULL + id BIGSERIAL, + version text NOT NULL, + CONSTRAINT instance_change_history_pkey PRIMARY KEY (id) ); -- @@ -125,14 +132,15 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_instance_change_history_unique_version ON -- CREATE TABLE IF NOT EXISTS policy ( - id SERIAL PRIMARY KEY, + id SERIAL, enforce boolean DEFAULT true NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, resource_type text NOT NULL, resource text NOT NULL, type text NOT NULL, payload jsonb DEFAULT '{}' NOT NULL, - inherit_from_parent boolean DEFAULT true NOT NULL + inherit_from_parent boolean DEFAULT true NOT NULL, + CONSTRAINT policy_pkey PRIMARY KEY (id) ); -- @@ -146,7 +154,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_policy_unique_resource_type_resource_type -- CREATE TABLE IF NOT EXISTS principal ( - id SERIAL PRIMARY KEY, + id SERIAL, deleted boolean DEFAULT false NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, type text NOT NULL, @@ -156,6 +164,7 @@ CREATE TABLE IF NOT EXISTS principal ( phone text DEFAULT '' NOT NULL, mfa_config jsonb DEFAULT '{}' NOT NULL, profile jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT principal_pkey PRIMARY KEY (id), CONSTRAINT principal_type_check CHECK (type IN ('END_USER', 'SYSTEM_BOT', 'SERVICE_ACCOUNT')) ); @@ -164,12 +173,13 @@ CREATE TABLE IF NOT EXISTS principal ( -- CREATE TABLE IF NOT EXISTS project ( - id SERIAL PRIMARY KEY, + id SERIAL, deleted boolean DEFAULT false NOT NULL, name text NOT NULL, resource_id text NOT NULL, data_classification_config_id text DEFAULT '' NOT NULL, - setting jsonb DEFAULT '{}' NOT NULL + setting jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT project_pkey PRIMARY KEY (id) ); -- @@ -183,12 +193,15 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_project_unique_resource_id ON project (res -- CREATE TABLE IF NOT EXISTS changelist ( - id SERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id SERIAL, + creator_id integer NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - project text NOT NULL REFERENCES project (resource_id), + project text NOT NULL, name text NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT changelist_pkey PRIMARY KEY (id), + CONSTRAINT changelist_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT changelist_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -202,13 +215,16 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_changelist_project_name ON changelist (pro -- CREATE TABLE IF NOT EXISTS db ( - id SERIAL PRIMARY KEY, + id SERIAL, deleted boolean DEFAULT false NOT NULL, - project text NOT NULL REFERENCES project (resource_id), - instance text NOT NULL REFERENCES instance (resource_id), + project text NOT NULL, + instance text NOT NULL, name text NOT NULL, environment text, - metadata jsonb DEFAULT '{}' NOT NULL + metadata jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT db_pkey PRIMARY KEY (id), + CONSTRAINT db_instance_fkey FOREIGN KEY (instance) REFERENCES instance (resource_id), + CONSTRAINT db_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -228,12 +244,14 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_db_unique_instance_name ON db (instance, n -- CREATE TABLE IF NOT EXISTS db_group ( - id BIGSERIAL PRIMARY KEY, - project text NOT NULL REFERENCES project (resource_id), + id BIGSERIAL, + project text NOT NULL, resource_id text NOT NULL, placeholder text DEFAULT '' NOT NULL, expression jsonb DEFAULT '{}' NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT db_group_pkey PRIMARY KEY (id), + CONSTRAINT db_group_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -253,13 +271,14 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_db_group_unique_project_resource_id ON db_ -- CREATE TABLE IF NOT EXISTS db_schema ( - id SERIAL PRIMARY KEY, + id SERIAL, instance text NOT NULL, db_name text NOT NULL, metadata json DEFAULT '{}' NOT NULL, raw_dump text DEFAULT '' NOT NULL, config jsonb DEFAULT '{}' NOT NULL, - FOREIGN KEY (instance, db_name) REFERENCES db (instance, name) + CONSTRAINT db_schema_pkey PRIMARY KEY (id), + CONSTRAINT db_schema_instance_db_name_fkey FOREIGN KEY (instance, db_name) REFERENCES db (instance, name) ); -- @@ -273,11 +292,14 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_db_schema_unique_instance_db_name ON db_sc -- CREATE TABLE IF NOT EXISTS pipeline ( - id SERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id SERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, - project text NOT NULL REFERENCES project (resource_id), - name text NOT NULL + project text NOT NULL, + name text NOT NULL, + CONSTRAINT pipeline_pkey PRIMARY KEY (id), + CONSTRAINT pipeline_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT pipeline_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -285,15 +307,19 @@ CREATE TABLE IF NOT EXISTS pipeline ( -- CREATE TABLE IF NOT EXISTS plan ( - id BIGSERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id BIGSERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - project text NOT NULL REFERENCES project (resource_id), - pipeline_id integer REFERENCES pipeline (id), + project text NOT NULL, + pipeline_id integer, name text NOT NULL, description text NOT NULL, - config jsonb DEFAULT '{}' NOT NULL + config jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT plan_pkey PRIMARY KEY (id), + CONSTRAINT plan_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT plan_pipeline_id_fkey FOREIGN KEY (pipeline_id) REFERENCES pipeline (id), + CONSTRAINT plan_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -313,19 +339,24 @@ CREATE INDEX IF NOT EXISTS idx_plan_project ON plan (project); -- CREATE TABLE IF NOT EXISTS issue ( - id SERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id SERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - project text NOT NULL REFERENCES project (resource_id), - plan_id bigint REFERENCES plan (id), - pipeline_id integer REFERENCES pipeline (id), + project text NOT NULL, + plan_id bigint, + pipeline_id integer, name text NOT NULL, status text NOT NULL, type text NOT NULL, description text DEFAULT '' NOT NULL, payload jsonb DEFAULT '{}' NOT NULL, ts_vector tsvector, + CONSTRAINT issue_pkey PRIMARY KEY (id), + CONSTRAINT issue_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT issue_pipeline_id_fkey FOREIGN KEY (pipeline_id) REFERENCES pipeline (id), + CONSTRAINT issue_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES plan (id), + CONSTRAINT issue_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id), CONSTRAINT issue_status_check CHECK (status IN ('OPEN', 'DONE', 'CANCELED')) ); @@ -364,12 +395,15 @@ CREATE INDEX IF NOT EXISTS idx_issue_ts_vector ON issue USING gin (ts_vector); -- CREATE TABLE IF NOT EXISTS issue_comment ( - id BIGSERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id BIGSERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - issue_id integer NOT NULL REFERENCES issue (id), - payload jsonb DEFAULT '{}' NOT NULL + issue_id integer NOT NULL, + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT issue_comment_pkey PRIMARY KEY (id), + CONSTRAINT issue_comment_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT issue_comment_issue_id_fkey FOREIGN KEY (issue_id) REFERENCES issue (id) ); -- @@ -383,9 +417,11 @@ CREATE INDEX IF NOT EXISTS idx_issue_comment_issue_id ON issue_comment (issue_id -- CREATE TABLE IF NOT EXISTS issue_subscriber ( - issue_id integer REFERENCES issue (id), - subscriber_id integer REFERENCES principal (id), - PRIMARY KEY (issue_id, subscriber_id) + issue_id integer, + subscriber_id integer, + CONSTRAINT issue_subscriber_pkey PRIMARY KEY (issue_id, subscriber_id), + CONSTRAINT issue_subscriber_issue_id_fkey FOREIGN KEY (issue_id) REFERENCES issue (id), + CONSTRAINT issue_subscriber_subscriber_id_fkey FOREIGN KEY (subscriber_id) REFERENCES principal (id) ); -- @@ -399,15 +435,17 @@ CREATE INDEX IF NOT EXISTS idx_issue_subscriber_subscriber_id ON issue_subscribe -- CREATE TABLE IF NOT EXISTS plan_check_run ( - id SERIAL PRIMARY KEY, + id SERIAL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - plan_id bigint NOT NULL REFERENCES plan (id), + plan_id bigint NOT NULL, status text NOT NULL, type text NOT NULL, config jsonb DEFAULT '{}' NOT NULL, result jsonb DEFAULT '{}' NOT NULL, payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT plan_check_run_pkey PRIMARY KEY (id), + CONSTRAINT plan_check_run_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES plan (id), CONSTRAINT plan_check_run_status_check CHECK (status IN ('RUNNING', 'DONE', 'FAILED', 'CANCELED')), CONSTRAINT plan_check_run_type_check CHECK (type LIKE 'bb.plan-check.%') ); @@ -423,13 +461,15 @@ CREATE INDEX IF NOT EXISTS idx_plan_check_run_plan_id ON plan_check_run (plan_id -- CREATE TABLE IF NOT EXISTS project_webhook ( - id SERIAL PRIMARY KEY, - project text NOT NULL REFERENCES project (resource_id), + id SERIAL, + project text NOT NULL, type text NOT NULL, name text NOT NULL, url text NOT NULL, event_list text[] NOT NULL, payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT project_webhook_pkey PRIMARY KEY (id), + CONSTRAINT project_webhook_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id), CONSTRAINT project_webhook_type_check CHECK (type LIKE 'bb.plugin.webhook.%') ); @@ -444,14 +484,16 @@ CREATE INDEX IF NOT EXISTS idx_project_webhook_project ON project_webhook (proje -- CREATE TABLE IF NOT EXISTS query_history ( - id BIGSERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id BIGSERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, project_id text NOT NULL, database text NOT NULL, statement text NOT NULL, type text NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT query_history_pkey PRIMARY KEY (id), + CONSTRAINT query_history_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id) ); -- @@ -465,12 +507,15 @@ CREATE INDEX IF NOT EXISTS idx_query_history_creator_id_created_at_project_id ON -- CREATE TABLE IF NOT EXISTS release ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, deleted boolean DEFAULT false NOT NULL, - project text NOT NULL REFERENCES project (resource_id), - creator_id integer NOT NULL REFERENCES principal (id), + project text NOT NULL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT release_pkey PRIMARY KEY (id), + CONSTRAINT release_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT release_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -484,10 +529,11 @@ CREATE INDEX IF NOT EXISTS idx_release_project ON release (project); -- CREATE TABLE IF NOT EXISTS review_config ( - id text PRIMARY KEY, + id text, enabled boolean DEFAULT true NOT NULL, name text NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT review_config_pkey PRIMARY KEY (id) ); -- @@ -495,15 +541,17 @@ CREATE TABLE IF NOT EXISTS review_config ( -- CREATE TABLE IF NOT EXISTS revision ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, instance text NOT NULL, db_name text NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, - deleter_id integer REFERENCES principal (id), + deleter_id integer, deleted_at timestamptz, version text NOT NULL, payload jsonb DEFAULT '{}' NOT NULL, - FOREIGN KEY (instance, db_name) REFERENCES db (instance, name) + CONSTRAINT revision_pkey PRIMARY KEY (id), + CONSTRAINT revision_deleter_id_fkey FOREIGN KEY (deleter_id) REFERENCES principal (id), + CONSTRAINT revision_instance_db_name_fkey FOREIGN KEY (instance, db_name) REFERENCES db (instance, name) ); -- @@ -523,12 +571,13 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_revision_unique_instance_db_name_version_d -- CREATE TABLE IF NOT EXISTS risk ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, source text NOT NULL, level bigint NOT NULL, name text NOT NULL, active boolean NOT NULL, expression jsonb NOT NULL, + CONSTRAINT risk_pkey PRIMARY KEY (id), CONSTRAINT risk_source_check CHECK (source LIKE 'bb.risk.%') ); @@ -537,12 +586,13 @@ CREATE TABLE IF NOT EXISTS risk ( -- CREATE TABLE IF NOT EXISTS role ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, resource_id text NOT NULL, name text NOT NULL, description text NOT NULL, permissions jsonb DEFAULT '{}' NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT role_pkey PRIMARY KEY (id) ); -- @@ -556,9 +606,10 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_role_unique_resource_id ON role (resource_ -- CREATE TABLE IF NOT EXISTS setting ( - id SERIAL PRIMARY KEY, + id SERIAL, name text NOT NULL, - value text NOT NULL + value text NOT NULL, + CONSTRAINT setting_pkey PRIMARY KEY (id) ); -- @@ -572,13 +623,16 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_setting_unique_name ON setting (name); -- CREATE TABLE IF NOT EXISTS sheet ( - id SERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id SERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, - project text NOT NULL REFERENCES project (resource_id), + project text NOT NULL, name text NOT NULL, sha256 bytea NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT sheet_pkey PRIMARY KEY (id), + CONSTRAINT sheet_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT sheet_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -592,8 +646,9 @@ CREATE INDEX IF NOT EXISTS idx_sheet_project ON sheet (project); -- CREATE TABLE IF NOT EXISTS sheet_blob ( - sha256 bytea PRIMARY KEY, - content text NOT NULL + sha256 bytea, + content text NOT NULL, + CONSTRAINT sheet_blob_pkey PRIMARY KEY (sha256) ); -- @@ -601,13 +656,14 @@ CREATE TABLE IF NOT EXISTS sheet_blob ( -- CREATE TABLE IF NOT EXISTS sync_history ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, created_at timestamptz DEFAULT now() NOT NULL, instance text NOT NULL, db_name text NOT NULL, metadata json DEFAULT '{}' NOT NULL, raw_dump text DEFAULT '' NOT NULL, - FOREIGN KEY (instance, db_name) REFERENCES db (instance, name) + CONSTRAINT sync_history_pkey PRIMARY KEY (id), + CONSTRAINT sync_history_instance_db_name_fkey FOREIGN KEY (instance, db_name) REFERENCES db (instance, name) ); -- @@ -621,15 +677,18 @@ CREATE INDEX IF NOT EXISTS idx_sync_history_instance_db_name_created_at ON sync_ -- CREATE TABLE IF NOT EXISTS changelog ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, created_at timestamptz DEFAULT now() NOT NULL, instance text NOT NULL, db_name text NOT NULL, status text NOT NULL, - prev_sync_history_id bigint REFERENCES sync_history (id), - sync_history_id bigint REFERENCES sync_history (id), + prev_sync_history_id bigint, + sync_history_id bigint, payload jsonb DEFAULT '{}' NOT NULL, - FOREIGN KEY (instance, db_name) REFERENCES db (instance, name), + CONSTRAINT changelog_pkey PRIMARY KEY (id), + CONSTRAINT changelog_instance_db_name_fkey FOREIGN KEY (instance, db_name) REFERENCES db (instance, name), + CONSTRAINT changelog_prev_sync_history_id_fkey FOREIGN KEY (prev_sync_history_id) REFERENCES sync_history (id), + CONSTRAINT changelog_sync_history_id_fkey FOREIGN KEY (sync_history_id) REFERENCES sync_history (id), CONSTRAINT changelog_status_check CHECK (status IN ('PENDING', 'DONE', 'FAILED')) ); @@ -644,13 +703,16 @@ CREATE INDEX IF NOT EXISTS idx_changelog_instance_db_name ON changelog (instance -- CREATE TABLE IF NOT EXISTS task ( - id SERIAL PRIMARY KEY, - pipeline_id integer NOT NULL REFERENCES pipeline (id), - instance text NOT NULL REFERENCES instance (resource_id), + id SERIAL, + pipeline_id integer NOT NULL, + instance text NOT NULL, environment text, db_name text, type text NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT task_pkey PRIMARY KEY (id), + CONSTRAINT task_instance_fkey FOREIGN KEY (instance) REFERENCES instance (resource_id), + CONSTRAINT task_pipeline_id_fkey FOREIGN KEY (pipeline_id) REFERENCES pipeline (id) ); -- @@ -664,18 +726,22 @@ CREATE INDEX IF NOT EXISTS idx_task_pipeline_id_environment ON task (pipeline_id -- CREATE TABLE IF NOT EXISTS task_run ( - id SERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id SERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - task_id integer NOT NULL REFERENCES task (id), - sheet_id integer REFERENCES sheet (id), + task_id integer NOT NULL, + sheet_id integer, attempt integer NOT NULL, status text NOT NULL, started_at timestamptz, run_at timestamptz, code integer DEFAULT 0 NOT NULL, result jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT task_run_pkey PRIMARY KEY (id), + CONSTRAINT task_run_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT task_run_sheet_id_fkey FOREIGN KEY (sheet_id) REFERENCES sheet (id), + CONSTRAINT task_run_task_id_fkey FOREIGN KEY (task_id) REFERENCES task (id), CONSTRAINT task_run_status_check CHECK (status IN ('PENDING', 'RUNNING', 'DONE', 'FAILED', 'CANCELED')) ); @@ -696,10 +762,12 @@ CREATE UNIQUE INDEX IF NOT EXISTS uk_task_run_task_id_attempt ON task_run (task_ -- CREATE TABLE IF NOT EXISTS task_run_log ( - id BIGSERIAL PRIMARY KEY, - task_run_id integer NOT NULL REFERENCES task_run (id), + id BIGSERIAL, + task_run_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT task_run_log_pkey PRIMARY KEY (id), + CONSTRAINT task_run_log_task_run_id_fkey FOREIGN KEY (task_run_id) REFERENCES task_run (id) ); -- @@ -713,10 +781,11 @@ CREATE INDEX IF NOT EXISTS idx_task_run_log_task_run_id ON task_run_log (task_ru -- CREATE TABLE IF NOT EXISTS user_group ( - email text PRIMARY KEY, + email text, name text NOT NULL, description text DEFAULT '' NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT user_group_pkey PRIMARY KEY (email) ); -- @@ -724,17 +793,20 @@ CREATE TABLE IF NOT EXISTS user_group ( -- CREATE TABLE IF NOT EXISTS worksheet ( - id SERIAL PRIMARY KEY, - creator_id integer NOT NULL REFERENCES principal (id), + id SERIAL, + creator_id integer NOT NULL, created_at timestamptz DEFAULT now() NOT NULL, updated_at timestamptz DEFAULT now() NOT NULL, - project text NOT NULL REFERENCES project (resource_id), + project text NOT NULL, instance text, db_name text, name text NOT NULL, statement text NOT NULL, visibility text NOT NULL, - payload jsonb DEFAULT '{}' NOT NULL + payload jsonb DEFAULT '{}' NOT NULL, + CONSTRAINT worksheet_pkey PRIMARY KEY (id), + CONSTRAINT worksheet_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES principal (id), + CONSTRAINT worksheet_project_fkey FOREIGN KEY (project) REFERENCES project (resource_id) ); -- @@ -748,10 +820,13 @@ CREATE INDEX IF NOT EXISTS idx_worksheet_creator_id_project ON worksheet (creato -- CREATE TABLE IF NOT EXISTS worksheet_organizer ( - id SERIAL PRIMARY KEY, - worksheet_id integer NOT NULL REFERENCES worksheet (id) ON DELETE CASCADE, - principal_id integer NOT NULL REFERENCES principal (id), - starred boolean DEFAULT false NOT NULL + id SERIAL, + worksheet_id integer NOT NULL, + principal_id integer NOT NULL, + starred boolean DEFAULT false NOT NULL, + CONSTRAINT worksheet_organizer_pkey PRIMARY KEY (id), + CONSTRAINT worksheet_organizer_principal_id_fkey FOREIGN KEY (principal_id) REFERENCES principal (id), + CONSTRAINT worksheet_organizer_worksheet_id_fkey FOREIGN KEY (worksheet_id) REFERENCES worksheet (id) ON DELETE CASCADE ); -- diff --git a/testdata/dump/employee/pgschema.sql b/testdata/dump/employee/pgschema.sql index 5fee2596..a13833cc 100644 --- a/testdata/dump/employee/pgschema.sql +++ b/testdata/dump/employee/pgschema.sql @@ -11,11 +11,12 @@ -- CREATE TABLE IF NOT EXISTS audit ( - id SERIAL PRIMARY KEY, + id SERIAL, operation text NOT NULL, query text, user_name text NOT NULL, - changed_at timestamptz DEFAULT CURRENT_TIMESTAMP + changed_at timestamptz DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT audit_pkey PRIMARY KEY (id) ); -- @@ -59,8 +60,10 @@ CREATE POLICY audit_user_isolation ON audit TO PUBLIC USING (user_name = CURRENT -- CREATE TABLE IF NOT EXISTS department ( - dept_no text PRIMARY KEY, - dept_name text NOT NULL UNIQUE + dept_no text, + dept_name text NOT NULL, + CONSTRAINT department_pkey PRIMARY KEY (dept_no), + CONSTRAINT department_dept_name_key UNIQUE (dept_name) ); -- @@ -68,12 +71,13 @@ CREATE TABLE IF NOT EXISTS department ( -- CREATE TABLE IF NOT EXISTS employee ( - emp_no SERIAL PRIMARY KEY, + emp_no SERIAL, birth_date date NOT NULL, first_name text NOT NULL, last_name text NOT NULL, gender text NOT NULL, hire_date date NOT NULL, + CONSTRAINT employee_pkey PRIMARY KEY (emp_no), CONSTRAINT employee_gender_check CHECK (gender IN ('M', 'F')) ); @@ -88,11 +92,13 @@ CREATE INDEX IF NOT EXISTS idx_employee_hire_date ON employee (hire_date); -- CREATE TABLE IF NOT EXISTS dept_emp ( - emp_no integer REFERENCES employee (emp_no) ON DELETE CASCADE, - dept_no text REFERENCES department (dept_no) ON DELETE CASCADE, + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_emp_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_emp_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no) ON DELETE CASCADE, + CONSTRAINT dept_emp_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE ); -- @@ -100,11 +106,13 @@ CREATE TABLE IF NOT EXISTS dept_emp ( -- CREATE TABLE IF NOT EXISTS dept_manager ( - emp_no integer REFERENCES employee (emp_no) ON DELETE CASCADE, - dept_no text REFERENCES department (dept_no) ON DELETE CASCADE, + emp_no integer, + dept_no text, from_date date NOT NULL, to_date date NOT NULL, - PRIMARY KEY (emp_no, dept_no) + CONSTRAINT dept_manager_pkey PRIMARY KEY (emp_no, dept_no), + CONSTRAINT dept_manager_dept_no_fkey FOREIGN KEY (dept_no) REFERENCES department (dept_no) ON DELETE CASCADE, + CONSTRAINT dept_manager_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE ); -- @@ -112,11 +120,12 @@ CREATE TABLE IF NOT EXISTS dept_manager ( -- CREATE TABLE IF NOT EXISTS salary ( - emp_no integer REFERENCES employee (emp_no) ON DELETE CASCADE, + emp_no integer, amount integer NOT NULL, from_date date, to_date date NOT NULL, - PRIMARY KEY (emp_no, from_date) + CONSTRAINT salary_pkey PRIMARY KEY (emp_no, from_date), + CONSTRAINT salary_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE ); -- @@ -130,11 +139,12 @@ CREATE INDEX IF NOT EXISTS idx_salary_amount ON salary (amount); -- CREATE TABLE IF NOT EXISTS title ( - emp_no integer REFERENCES employee (emp_no) ON DELETE CASCADE, + emp_no integer, title text, from_date date, to_date date, - PRIMARY KEY (emp_no, title, from_date) + CONSTRAINT title_pkey PRIMARY KEY (emp_no, title, from_date), + CONSTRAINT title_emp_no_fkey FOREIGN KEY (emp_no) REFERENCES employee (emp_no) ON DELETE CASCADE ); -- diff --git a/testdata/dump/issue_80_index_name_quote/pgschema.sql b/testdata/dump/issue_80_index_name_quote/pgschema.sql index dfc03aed..b8e074e1 100644 --- a/testdata/dump/issue_80_index_name_quote/pgschema.sql +++ b/testdata/dump/issue_80_index_name_quote/pgschema.sql @@ -11,10 +11,11 @@ -- CREATE TABLE IF NOT EXISTS products ( - id uuid DEFAULT gen_random_uuid() PRIMARY KEY, + id uuid DEFAULT gen_random_uuid(), name text NOT NULL, price numeric(10,2), - category text + category text, + CONSTRAINT products_pkey PRIMARY KEY (id) ); -- @@ -40,13 +41,14 @@ CREATE INDEX IF NOT EXISTS products_category_idx_v2 ON products (category); -- CREATE TABLE IF NOT EXISTS users ( - id uuid DEFAULT gen_random_uuid() PRIMARY KEY, + id uuid DEFAULT gen_random_uuid(), email text NOT NULL, username text NOT NULL, created_at timestamp DEFAULT now(), status text, position integer, - department text + department text, + CONSTRAINT users_pkey PRIMARY KEY (id) ); -- diff --git a/testdata/dump/issue_83_explicit_constraint_name/manifest.json b/testdata/dump/issue_83_explicit_constraint_name/manifest.json new file mode 100644 index 00000000..4ae71c3f --- /dev/null +++ b/testdata/dump/issue_83_explicit_constraint_name/manifest.json @@ -0,0 +1,8 @@ +{ + "name": "issue_83_explicit_constraint_name", + "description": "Test case for named foreign key constraint preservation (GitHub issue #83)", + "source": "https://github.com/pgschema/pgschema/issues/83", + "notes": [ + "Tests that explicitly named foreign key constraints retain their names when dumped" + ] +} diff --git a/testdata/dump/issue_83_explicit_constraint_name/pgdump.sql b/testdata/dump/issue_83_explicit_constraint_name/pgdump.sql new file mode 100644 index 00000000..f91b316a --- /dev/null +++ b/testdata/dump/issue_83_explicit_constraint_name/pgdump.sql @@ -0,0 +1,406 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 17.5 (Debian 17.5-1.pgdg120+1) +-- Dumped by pg_dump version 17.2 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET transaction_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +SET default_tablespace = ''; + +SET default_table_access_method = heap; + +-- +-- Name: authors; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.authors ( + id integer NOT NULL, + name character varying(255) NOT NULL +); + + +-- +-- Name: authors_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.authors_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: authors_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.authors_id_seq OWNED BY public.authors.id; + + +-- +-- Name: books; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.books ( + id integer NOT NULL, + title character varying(255) NOT NULL, + author_id integer NOT NULL +); + + +-- +-- Name: books_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.books_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: books_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.books_id_seq OWNED BY public.books.id; + + +-- +-- Name: departments; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.departments ( + id integer NOT NULL, + name character varying(255) NOT NULL +); + + +-- +-- Name: departments_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.departments_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: departments_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.departments_id_seq OWNED BY public.departments.id; + + +-- +-- Name: employees; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.employees ( + id integer NOT NULL, + name character varying(255) NOT NULL, + department_id integer, + manager_id integer +); + + +-- +-- Name: employees_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.employees_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: employees_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.employees_id_seq OWNED BY public.employees.id; + + +-- +-- Name: projects; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.projects ( + project_id integer NOT NULL, + phase_id integer NOT NULL, + name character varying(255) NOT NULL +); + + +-- +-- Name: some_other_table; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.some_other_table ( + id integer NOT NULL, + some_table_id integer NOT NULL +); + + +-- +-- Name: some_other_table_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.some_other_table_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: some_other_table_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.some_other_table_id_seq OWNED BY public.some_other_table.id; + + +-- +-- Name: some_table; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.some_table ( + id integer NOT NULL, + name character varying(255) NOT NULL +); + + +-- +-- Name: some_table_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.some_table_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: some_table_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.some_table_id_seq OWNED BY public.some_table.id; + + +-- +-- Name: tasks; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.tasks ( + id integer NOT NULL, + project_id integer NOT NULL, + phase_id integer NOT NULL, + description text +); + + +-- +-- Name: tasks_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.tasks_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: tasks_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.tasks_id_seq OWNED BY public.tasks.id; + + +-- +-- Name: authors id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.authors ALTER COLUMN id SET DEFAULT nextval('public.authors_id_seq'::regclass); + + +-- +-- Name: books id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.books ALTER COLUMN id SET DEFAULT nextval('public.books_id_seq'::regclass); + + +-- +-- Name: departments id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.departments ALTER COLUMN id SET DEFAULT nextval('public.departments_id_seq'::regclass); + + +-- +-- Name: employees id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.employees ALTER COLUMN id SET DEFAULT nextval('public.employees_id_seq'::regclass); + + +-- +-- Name: some_other_table id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.some_other_table ALTER COLUMN id SET DEFAULT nextval('public.some_other_table_id_seq'::regclass); + + +-- +-- Name: some_table id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.some_table ALTER COLUMN id SET DEFAULT nextval('public.some_table_id_seq'::regclass); + + +-- +-- Name: tasks id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.tasks ALTER COLUMN id SET DEFAULT nextval('public.tasks_id_seq'::regclass); + + +-- +-- Name: authors authors_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.authors + ADD CONSTRAINT authors_pkey PRIMARY KEY (id); + + +-- +-- Name: books books_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.books + ADD CONSTRAINT books_pkey PRIMARY KEY (id); + + +-- +-- Name: departments departments_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.departments + ADD CONSTRAINT departments_pkey PRIMARY KEY (id); + + +-- +-- Name: employees employees_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.employees + ADD CONSTRAINT employees_pkey PRIMARY KEY (id); + + +-- +-- Name: projects projects_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.projects + ADD CONSTRAINT projects_pkey PRIMARY KEY (project_id, phase_id); + + +-- +-- Name: some_other_table some_other_table_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.some_other_table + ADD CONSTRAINT some_other_table_pkey PRIMARY KEY (id); + + +-- +-- Name: some_table some_table_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.some_table + ADD CONSTRAINT some_table_pkey PRIMARY KEY (id); + + +-- +-- Name: tasks tasks_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.tasks + ADD CONSTRAINT tasks_pkey PRIMARY KEY (id); + + +-- +-- Name: books fk_book_author_deferred; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.books + ADD CONSTRAINT fk_book_author_deferred FOREIGN KEY (author_id) REFERENCES public.authors(id) DEFERRABLE INITIALLY DEFERRED; + + +-- +-- Name: employees fk_employee_department; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.employees + ADD CONSTRAINT fk_employee_department FOREIGN KEY (department_id) REFERENCES public.departments(id) ON DELETE CASCADE; + + +-- +-- Name: employees fk_employee_manager; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.employees + ADD CONSTRAINT fk_employee_manager FOREIGN KEY (manager_id) REFERENCES public.employees(id) ON DELETE SET NULL; + + +-- +-- Name: some_other_table fk_rails_8bfc3d9c01; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.some_other_table + ADD CONSTRAINT fk_rails_8bfc3d9c01 FOREIGN KEY (some_table_id) REFERENCES public.some_table(id); + + +-- +-- Name: tasks fk_task_project_phase; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.tasks + ADD CONSTRAINT fk_task_project_phase FOREIGN KEY (project_id, phase_id) REFERENCES public.projects(project_id, phase_id); + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/testdata/dump/issue_83_explicit_constraint_name/pgschema.sql b/testdata/dump/issue_83_explicit_constraint_name/pgschema.sql new file mode 100644 index 00000000..aef43141 --- /dev/null +++ b/testdata/dump/issue_83_explicit_constraint_name/pgschema.sql @@ -0,0 +1,99 @@ +-- +-- pgschema database dump +-- + +-- Dumped from database version PostgreSQL 17.5 +-- Dumped by pgschema version 1.3.0 + + +-- +-- Name: authors; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS authors ( + id SERIAL, + name varchar(255) NOT NULL, + CONSTRAINT authors_pkey PRIMARY KEY (id) +); + +-- +-- Name: books; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS books ( + id SERIAL, + title varchar(255) NOT NULL, + author_id integer NOT NULL, + CONSTRAINT books_pkey PRIMARY KEY (id), + CONSTRAINT fk_book_author_deferred FOREIGN KEY (author_id) REFERENCES authors (id) DEFERRABLE INITIALLY DEFERRED +); + +-- +-- Name: departments; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS departments ( + id SERIAL, + name varchar(255) NOT NULL, + CONSTRAINT departments_pkey PRIMARY KEY (id) +); + +-- +-- Name: employees; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS employees ( + id SERIAL, + name varchar(255) NOT NULL, + department_id integer, + manager_id integer, + CONSTRAINT employees_pkey PRIMARY KEY (id), + CONSTRAINT fk_employee_department FOREIGN KEY (department_id) REFERENCES departments (id) ON DELETE CASCADE, + CONSTRAINT fk_employee_manager FOREIGN KEY (manager_id) REFERENCES employees (id) ON DELETE SET NULL +); + +-- +-- Name: projects; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS projects ( + project_id integer, + phase_id integer, + name varchar(255) NOT NULL, + CONSTRAINT projects_pkey PRIMARY KEY (project_id, phase_id) +); + +-- +-- Name: some_table; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS some_table ( + id SERIAL, + name varchar(255) NOT NULL, + CONSTRAINT some_table_pkey PRIMARY KEY (id) +); + +-- +-- Name: some_other_table; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS some_other_table ( + id SERIAL, + some_table_id integer NOT NULL, + CONSTRAINT some_other_table_pkey PRIMARY KEY (id), + CONSTRAINT fk_rails_8bfc3d9c01 FOREIGN KEY (some_table_id) REFERENCES some_table (id) +); + +-- +-- Name: tasks; Type: TABLE; Schema: -; Owner: - +-- + +CREATE TABLE IF NOT EXISTS tasks ( + id SERIAL, + project_id integer NOT NULL, + phase_id integer NOT NULL, + description text, + CONSTRAINT tasks_pkey PRIMARY KEY (id), + CONSTRAINT fk_task_project_phase FOREIGN KEY (project_id, phase_id) REFERENCES projects (project_id, phase_id) +); + diff --git a/testdata/dump/issue_83_explicit_constraint_name/raw.sql b/testdata/dump/issue_83_explicit_constraint_name/raw.sql new file mode 100644 index 00000000..f4337f8c --- /dev/null +++ b/testdata/dump/issue_83_explicit_constraint_name/raw.sql @@ -0,0 +1,106 @@ +-- +-- Test case for GitHub issue #83: Named constraint preservation +-- +-- This test verifies that ALL constraints (PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK) +-- are output as table-level constraints with explicit names in dump output. +-- +-- Expected behavior in DUMP output: +-- - ALL constraints output as table-level with explicit CONSTRAINT names +-- - Explicitly named constraints preserve their custom names (e.g., fk_rails_8bfc3d9c01) +-- - Auto-generated names follow PostgreSQL conventions (e.g., table_column_fkey) +-- - No inline constraint syntax in dump output +-- +-- Note: Inline syntax (e.g., "id integer PRIMARY KEY") is only used in +-- migration/diff output when adding single-column constraints via ALTER TABLE ADD COLUMN. +-- + +-- +-- Case 1: Explicitly named foreign key (from the original issue) +-- This is the problematic case - the name should be preserved +-- +CREATE TABLE some_table ( + id serial PRIMARY KEY, + name varchar(255) NOT NULL +); + +CREATE TABLE some_other_table ( + id serial PRIMARY KEY, + some_table_id integer NOT NULL +); + +-- Named constraint - should preserve the explicit name "fk_rails_8bfc3d9c01" +ALTER TABLE ONLY some_other_table + ADD CONSTRAINT fk_rails_8bfc3d9c01 + FOREIGN KEY (some_table_id) REFERENCES some_table(id); + + +-- +-- Case 2: Multiple named foreign keys with different actions +-- Tests that various FK configurations preserve their names +-- +CREATE TABLE departments ( + id serial PRIMARY KEY, + name varchar(255) NOT NULL +); + +CREATE TABLE employees ( + id serial PRIMARY KEY, + name varchar(255) NOT NULL, + department_id integer, + manager_id integer +); + +-- Named FK with ON DELETE CASCADE +ALTER TABLE ONLY employees + ADD CONSTRAINT fk_employee_department + FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE; + +-- Named FK with ON DELETE SET NULL +ALTER TABLE ONLY employees + ADD CONSTRAINT fk_employee_manager + FOREIGN KEY (manager_id) REFERENCES employees(id) ON DELETE SET NULL; + + +-- +-- Case 3: Composite foreign key with explicit name +-- Tests multi-column foreign keys preserve names +-- +CREATE TABLE projects ( + project_id integer NOT NULL, + phase_id integer NOT NULL, + name varchar(255) NOT NULL, + PRIMARY KEY (project_id, phase_id) +); + +CREATE TABLE tasks ( + id serial PRIMARY KEY, + project_id integer NOT NULL, + phase_id integer NOT NULL, + description text +); + +-- Composite FK with explicit name +ALTER TABLE ONLY tasks + ADD CONSTRAINT fk_task_project_phase + FOREIGN KEY (project_id, phase_id) REFERENCES projects(project_id, phase_id); + + +-- +-- Case 4: Named FK with DEFERRABLE option +-- Tests that constraint options are preserved along with names +-- +CREATE TABLE authors ( + id serial PRIMARY KEY, + name varchar(255) NOT NULL +); + +CREATE TABLE books ( + id serial PRIMARY KEY, + title varchar(255) NOT NULL, + author_id integer NOT NULL +); + +-- Named FK with DEFERRABLE INITIALLY DEFERRED +ALTER TABLE ONLY books + ADD CONSTRAINT fk_book_author_deferred + FOREIGN KEY (author_id) REFERENCES authors(id) DEFERRABLE INITIALLY DEFERRED; diff --git a/testdata/dump/sakila/pgschema.sql b/testdata/dump/sakila/pgschema.sql index ed7ab002..d8a0fcb0 100644 --- a/testdata/dump/sakila/pgschema.sql +++ b/testdata/dump/sakila/pgschema.sql @@ -36,10 +36,11 @@ CREATE DOMAIN year AS integer -- CREATE TABLE IF NOT EXISTS actor ( - actor_id SERIAL PRIMARY KEY, + actor_id SERIAL, first_name text NOT NULL, last_name text NOT NULL, - last_update timestamptz DEFAULT now() NOT NULL + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT actor_pkey PRIMARY KEY (actor_id) ); -- @@ -53,9 +54,10 @@ CREATE INDEX IF NOT EXISTS idx_actor_last_name ON actor (last_name); -- CREATE TABLE IF NOT EXISTS category ( - category_id SERIAL PRIMARY KEY, + category_id SERIAL, name text NOT NULL, - last_update timestamptz DEFAULT now() NOT NULL + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT category_pkey PRIMARY KEY (category_id) ); -- @@ -63,9 +65,10 @@ CREATE TABLE IF NOT EXISTS category ( -- CREATE TABLE IF NOT EXISTS country ( - country_id SERIAL PRIMARY KEY, + country_id SERIAL, country text NOT NULL, - last_update timestamptz DEFAULT now() NOT NULL + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT country_pkey PRIMARY KEY (country_id) ); -- @@ -73,10 +76,12 @@ CREATE TABLE IF NOT EXISTS country ( -- CREATE TABLE IF NOT EXISTS city ( - city_id SERIAL PRIMARY KEY, + city_id SERIAL, city text NOT NULL, - country_id integer NOT NULL REFERENCES country (country_id) ON UPDATE CASCADE ON DELETE RESTRICT, - last_update timestamptz DEFAULT now() NOT NULL + country_id integer NOT NULL, + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT city_pkey PRIMARY KEY (city_id), + CONSTRAINT city_country_id_fkey FOREIGN KEY (country_id) REFERENCES country (country_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -90,14 +95,16 @@ CREATE INDEX IF NOT EXISTS idx_fk_country_id ON city (country_id); -- CREATE TABLE IF NOT EXISTS address ( - address_id SERIAL PRIMARY KEY, + address_id SERIAL, address text NOT NULL, address2 text, district text NOT NULL, - city_id integer NOT NULL REFERENCES city (city_id) ON UPDATE CASCADE ON DELETE RESTRICT, + city_id integer NOT NULL, postal_code text, phone text NOT NULL, - last_update timestamptz DEFAULT now() NOT NULL + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT address_pkey PRIMARY KEY (address_id), + CONSTRAINT address_city_id_fkey FOREIGN KEY (city_id) REFERENCES city (city_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -111,9 +118,10 @@ CREATE INDEX IF NOT EXISTS idx_fk_city_id ON address (city_id); -- CREATE TABLE IF NOT EXISTS language ( - language_id SERIAL PRIMARY KEY, + language_id SERIAL, name character(20) NOT NULL, - last_update timestamptz DEFAULT now() NOT NULL + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT language_pkey PRIMARY KEY (language_id) ); -- @@ -121,12 +129,12 @@ CREATE TABLE IF NOT EXISTS language ( -- CREATE TABLE IF NOT EXISTS film ( - film_id SERIAL PRIMARY KEY, + film_id SERIAL, title text NOT NULL, description text, release_year year, - language_id integer NOT NULL REFERENCES language (language_id) ON UPDATE CASCADE ON DELETE RESTRICT, - original_language_id integer REFERENCES language (language_id) ON UPDATE CASCADE ON DELETE RESTRICT, + language_id integer NOT NULL, + original_language_id integer, rental_duration smallint DEFAULT 3 NOT NULL, rental_rate numeric(4,2) DEFAULT 4.99 NOT NULL, length smallint, @@ -134,7 +142,10 @@ CREATE TABLE IF NOT EXISTS film ( rating mpaa_rating DEFAULT 'G', last_update timestamptz DEFAULT now() NOT NULL, special_features text[], - fulltext tsvector NOT NULL + fulltext tsvector NOT NULL, + CONSTRAINT film_pkey PRIMARY KEY (film_id), + CONSTRAINT film_language_id_fkey FOREIGN KEY (language_id) REFERENCES language (language_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT film_original_language_id_fkey FOREIGN KEY (original_language_id) REFERENCES language (language_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -166,10 +177,12 @@ CREATE INDEX IF NOT EXISTS idx_title ON film (title); -- CREATE TABLE IF NOT EXISTS film_actor ( - actor_id integer REFERENCES actor (actor_id) ON UPDATE CASCADE ON DELETE RESTRICT, - film_id integer REFERENCES film (film_id) ON UPDATE CASCADE ON DELETE RESTRICT, + actor_id integer, + film_id integer, last_update timestamptz DEFAULT now() NOT NULL, - PRIMARY KEY (actor_id, film_id) + CONSTRAINT film_actor_pkey PRIMARY KEY (actor_id, film_id), + CONSTRAINT film_actor_actor_id_fkey FOREIGN KEY (actor_id) REFERENCES actor (actor_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT film_actor_film_id_fkey FOREIGN KEY (film_id) REFERENCES film (film_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -183,10 +196,12 @@ CREATE INDEX IF NOT EXISTS idx_fk_film_id ON film_actor (film_id); -- CREATE TABLE IF NOT EXISTS film_category ( - film_id integer REFERENCES film (film_id) ON UPDATE CASCADE ON DELETE RESTRICT, - category_id integer REFERENCES category (category_id) ON UPDATE CASCADE ON DELETE RESTRICT, + film_id integer, + category_id integer, last_update timestamptz DEFAULT now() NOT NULL, - PRIMARY KEY (film_id, category_id) + CONSTRAINT film_category_pkey PRIMARY KEY (film_id, category_id), + CONSTRAINT film_category_category_id_fkey FOREIGN KEY (category_id) REFERENCES category (category_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT film_category_film_id_fkey FOREIGN KEY (film_id) REFERENCES film (film_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -200,7 +215,7 @@ CREATE TABLE IF NOT EXISTS payment ( rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_pkey PRIMARY KEY (payment_date, payment_id) ) PARTITION BY RANGE (payment_date); -- @@ -208,10 +223,12 @@ CREATE TABLE IF NOT EXISTS payment ( -- CREATE TABLE IF NOT EXISTS store ( - store_id SERIAL PRIMARY KEY, + store_id SERIAL, manager_staff_id integer NOT NULL, - address_id integer NOT NULL REFERENCES address (address_id) ON UPDATE CASCADE ON DELETE RESTRICT, - last_update timestamptz DEFAULT now() NOT NULL + address_id integer NOT NULL, + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT store_pkey PRIMARY KEY (store_id), + CONSTRAINT store_address_id_fkey FOREIGN KEY (address_id) REFERENCES address (address_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -225,16 +242,19 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_unq_manager_staff_id ON store (manager_sta -- CREATE TABLE IF NOT EXISTS customer ( - customer_id SERIAL PRIMARY KEY, - store_id integer NOT NULL REFERENCES store (store_id) ON UPDATE CASCADE ON DELETE RESTRICT, + customer_id SERIAL, + store_id integer NOT NULL, first_name text NOT NULL, last_name text NOT NULL, email text, - address_id integer NOT NULL REFERENCES address (address_id) ON UPDATE CASCADE ON DELETE RESTRICT, + address_id integer NOT NULL, activebool boolean DEFAULT true NOT NULL, create_date date DEFAULT CURRENT_DATE NOT NULL, last_update timestamptz DEFAULT now(), - active integer + active integer, + CONSTRAINT customer_pkey PRIMARY KEY (customer_id), + CONSTRAINT customer_address_id_fkey FOREIGN KEY (address_id) REFERENCES address (address_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT customer_store_id_fkey FOREIGN KEY (store_id) REFERENCES store (store_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -260,10 +280,13 @@ CREATE INDEX IF NOT EXISTS idx_last_name ON customer (last_name); -- CREATE TABLE IF NOT EXISTS inventory ( - inventory_id SERIAL PRIMARY KEY, - film_id integer NOT NULL REFERENCES film (film_id) ON UPDATE CASCADE ON DELETE RESTRICT, - store_id integer NOT NULL REFERENCES store (store_id) ON UPDATE CASCADE ON DELETE RESTRICT, - last_update timestamptz DEFAULT now() NOT NULL + inventory_id SERIAL, + film_id integer NOT NULL, + store_id integer NOT NULL, + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT inventory_pkey PRIMARY KEY (inventory_id), + CONSTRAINT inventory_film_id_fkey FOREIGN KEY (film_id) REFERENCES film (film_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT inventory_store_id_fkey FOREIGN KEY (store_id) REFERENCES store (store_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -277,17 +300,20 @@ CREATE INDEX IF NOT EXISTS idx_store_id_film_id ON inventory (store_id, film_id) -- CREATE TABLE IF NOT EXISTS staff ( - staff_id SERIAL PRIMARY KEY, + staff_id SERIAL, first_name text NOT NULL, last_name text NOT NULL, - address_id integer NOT NULL REFERENCES address (address_id) ON UPDATE CASCADE ON DELETE RESTRICT, + address_id integer NOT NULL, email text, - store_id integer NOT NULL REFERENCES store (store_id), + store_id integer NOT NULL, active boolean DEFAULT true NOT NULL, username text NOT NULL, password text, last_update timestamptz DEFAULT now() NOT NULL, - picture bytea + picture bytea, + CONSTRAINT staff_pkey PRIMARY KEY (staff_id), + CONSTRAINT staff_address_id_fkey FOREIGN KEY (address_id) REFERENCES address (address_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT staff_store_id_fkey FOREIGN KEY (store_id) REFERENCES store (store_id) ); -- @@ -295,13 +321,17 @@ CREATE TABLE IF NOT EXISTS staff ( -- CREATE TABLE IF NOT EXISTS rental ( - rental_id SERIAL PRIMARY KEY, + rental_id SERIAL, rental_date timestamptz NOT NULL, - inventory_id integer NOT NULL REFERENCES inventory (inventory_id) ON UPDATE CASCADE ON DELETE RESTRICT, - customer_id integer NOT NULL REFERENCES customer (customer_id) ON UPDATE CASCADE ON DELETE RESTRICT, + inventory_id integer NOT NULL, + customer_id integer NOT NULL, return_date timestamptz, - staff_id integer NOT NULL REFERENCES staff (staff_id) ON UPDATE CASCADE ON DELETE RESTRICT, - last_update timestamptz DEFAULT now() NOT NULL + staff_id integer NOT NULL, + last_update timestamptz DEFAULT now() NOT NULL, + CONSTRAINT rental_pkey PRIMARY KEY (rental_id), + CONSTRAINT rental_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT rental_inventory_id_fkey FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT rental_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ON UPDATE CASCADE ON DELETE RESTRICT ); -- @@ -322,12 +352,15 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_unq_rental_rental_date_inventory_id_custom CREATE TABLE IF NOT EXISTS payment_p2022_01 ( payment_id SERIAL, - customer_id integer NOT NULL REFERENCES customer (customer_id), - staff_id integer NOT NULL REFERENCES staff (staff_id), - rental_id integer NOT NULL REFERENCES rental (rental_id), + customer_id integer NOT NULL, + staff_id integer NOT NULL, + rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_p2022_01_pkey PRIMARY KEY (payment_date, payment_id), + CONSTRAINT payment_p2022_01_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id), + CONSTRAINT payment_p2022_01_rental_id_fkey FOREIGN KEY (rental_id) REFERENCES rental (rental_id), + CONSTRAINT payment_p2022_01_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ); -- @@ -354,12 +387,15 @@ CREATE INDEX IF NOT EXISTS payment_p2022_01_customer_id_idx ON payment_p2022_01 CREATE TABLE IF NOT EXISTS payment_p2022_02 ( payment_id SERIAL, - customer_id integer NOT NULL REFERENCES customer (customer_id), - staff_id integer NOT NULL REFERENCES staff (staff_id), - rental_id integer NOT NULL REFERENCES rental (rental_id), + customer_id integer NOT NULL, + staff_id integer NOT NULL, + rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_p2022_02_pkey PRIMARY KEY (payment_date, payment_id), + CONSTRAINT payment_p2022_02_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id), + CONSTRAINT payment_p2022_02_rental_id_fkey FOREIGN KEY (rental_id) REFERENCES rental (rental_id), + CONSTRAINT payment_p2022_02_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ); -- @@ -386,12 +422,15 @@ CREATE INDEX IF NOT EXISTS payment_p2022_02_customer_id_idx ON payment_p2022_02 CREATE TABLE IF NOT EXISTS payment_p2022_03 ( payment_id SERIAL, - customer_id integer NOT NULL REFERENCES customer (customer_id), - staff_id integer NOT NULL REFERENCES staff (staff_id), - rental_id integer NOT NULL REFERENCES rental (rental_id), + customer_id integer NOT NULL, + staff_id integer NOT NULL, + rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_p2022_03_pkey PRIMARY KEY (payment_date, payment_id), + CONSTRAINT payment_p2022_03_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id), + CONSTRAINT payment_p2022_03_rental_id_fkey FOREIGN KEY (rental_id) REFERENCES rental (rental_id), + CONSTRAINT payment_p2022_03_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ); -- @@ -418,12 +457,15 @@ CREATE INDEX IF NOT EXISTS payment_p2022_03_customer_id_idx ON payment_p2022_03 CREATE TABLE IF NOT EXISTS payment_p2022_04 ( payment_id SERIAL, - customer_id integer NOT NULL REFERENCES customer (customer_id), - staff_id integer NOT NULL REFERENCES staff (staff_id), - rental_id integer NOT NULL REFERENCES rental (rental_id), + customer_id integer NOT NULL, + staff_id integer NOT NULL, + rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_p2022_04_pkey PRIMARY KEY (payment_date, payment_id), + CONSTRAINT payment_p2022_04_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id), + CONSTRAINT payment_p2022_04_rental_id_fkey FOREIGN KEY (rental_id) REFERENCES rental (rental_id), + CONSTRAINT payment_p2022_04_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ); -- @@ -450,12 +492,15 @@ CREATE INDEX IF NOT EXISTS payment_p2022_04_customer_id_idx ON payment_p2022_04 CREATE TABLE IF NOT EXISTS payment_p2022_05 ( payment_id SERIAL, - customer_id integer NOT NULL REFERENCES customer (customer_id), - staff_id integer NOT NULL REFERENCES staff (staff_id), - rental_id integer NOT NULL REFERENCES rental (rental_id), + customer_id integer NOT NULL, + staff_id integer NOT NULL, + rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_p2022_05_pkey PRIMARY KEY (payment_date, payment_id), + CONSTRAINT payment_p2022_05_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id), + CONSTRAINT payment_p2022_05_rental_id_fkey FOREIGN KEY (rental_id) REFERENCES rental (rental_id), + CONSTRAINT payment_p2022_05_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ); -- @@ -482,12 +527,15 @@ CREATE INDEX IF NOT EXISTS payment_p2022_05_customer_id_idx ON payment_p2022_05 CREATE TABLE IF NOT EXISTS payment_p2022_06 ( payment_id SERIAL, - customer_id integer NOT NULL REFERENCES customer (customer_id), - staff_id integer NOT NULL REFERENCES staff (staff_id), - rental_id integer NOT NULL REFERENCES rental (rental_id), + customer_id integer NOT NULL, + staff_id integer NOT NULL, + rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_p2022_06_pkey PRIMARY KEY (payment_date, payment_id), + CONSTRAINT payment_p2022_06_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id), + CONSTRAINT payment_p2022_06_rental_id_fkey FOREIGN KEY (rental_id) REFERENCES rental (rental_id), + CONSTRAINT payment_p2022_06_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ); -- @@ -514,12 +562,15 @@ CREATE INDEX IF NOT EXISTS payment_p2022_06_customer_id_idx ON payment_p2022_06 CREATE TABLE IF NOT EXISTS payment_p2022_07 ( payment_id SERIAL, - customer_id integer NOT NULL REFERENCES customer (customer_id), - staff_id integer NOT NULL REFERENCES staff (staff_id), - rental_id integer NOT NULL REFERENCES rental (rental_id), + customer_id integer NOT NULL, + staff_id integer NOT NULL, + rental_id integer NOT NULL, amount numeric(5,2) NOT NULL, payment_date timestamptz, - PRIMARY KEY (payment_date, payment_id) + CONSTRAINT payment_p2022_07_pkey PRIMARY KEY (payment_date, payment_id), + CONSTRAINT payment_p2022_07_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer (customer_id), + CONSTRAINT payment_p2022_07_rental_id_fkey FOREIGN KEY (rental_id) REFERENCES rental (rental_id), + CONSTRAINT payment_p2022_07_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ); -- diff --git a/testdata/dump/tenant/pgschema.sql b/testdata/dump/tenant/pgschema.sql index 9285a279..afce6fe3 100644 --- a/testdata/dump/tenant/pgschema.sql +++ b/testdata/dump/tenant/pgschema.sql @@ -3,7 +3,7 @@ -- -- Dumped from database version PostgreSQL 17.5 --- Dumped by pgschema version 1.2.1 +-- Dumped by pgschema version 1.3.0 -- @@ -11,12 +11,13 @@ -- CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id SERIAL, username varchar(100) NOT NULL, email varchar(100) NOT NULL, role public.user_role DEFAULT 'user', status public.status DEFAULT 'active', - created_at timestamp DEFAULT now() + created_at timestamp DEFAULT now(), + CONSTRAINT users_pkey PRIMARY KEY (id) ); -- @@ -30,11 +31,13 @@ CREATE INDEX IF NOT EXISTS idx_users_email ON users (email); -- CREATE TABLE IF NOT EXISTS posts ( - id SERIAL PRIMARY KEY, + id SERIAL, title varchar(200) NOT NULL, content text, - author_id integer REFERENCES users (id), + author_id integer, status public.status DEFAULT 'active', - created_at timestamp DEFAULT now() + created_at timestamp DEFAULT now(), + CONSTRAINT posts_pkey PRIMARY KEY (id), + CONSTRAINT posts_author_id_fkey FOREIGN KEY (author_id) REFERENCES users (id) ); diff --git a/testdata/include/expected_full_schema.sql b/testdata/include/expected_full_schema.sql index 8e9f1452..8d444e55 100644 --- a/testdata/include/expected_full_schema.sql +++ b/testdata/include/expected_full_schema.sql @@ -74,9 +74,10 @@ $$; -- CREATE TABLE IF NOT EXISTS users ( - id integer PRIMARY KEY, + id integer, email text NOT NULL, name text NOT NULL, + CONSTRAINT users_pkey PRIMARY KEY (id), CONSTRAINT users_email_check CHECK (email LIKE '%@%') ); @@ -121,10 +122,12 @@ CREATE OR REPLACE TRIGGER users_update_trigger -- CREATE TABLE IF NOT EXISTS orders ( - id integer PRIMARY KEY, - user_id integer NOT NULL REFERENCES users (id), + id integer, + user_id integer NOT NULL, status text DEFAULT 'pending' NOT NULL, amount numeric(10,2) DEFAULT 0.00, + CONSTRAINT orders_pkey PRIMARY KEY (id), + CONSTRAINT orders_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id), CONSTRAINT orders_status_check CHECK (status IN ('pending', 'completed')) ); diff --git a/testdata/include/tables/orders.sql b/testdata/include/tables/orders.sql index 2608dfc7..d64bdd44 100644 --- a/testdata/include/tables/orders.sql +++ b/testdata/include/tables/orders.sql @@ -3,10 +3,12 @@ -- CREATE TABLE IF NOT EXISTS orders ( - id integer PRIMARY KEY, - user_id integer NOT NULL REFERENCES users (id), + id integer, + user_id integer NOT NULL, status text DEFAULT 'pending' NOT NULL, amount numeric(10,2) DEFAULT 0.00, + CONSTRAINT orders_pkey PRIMARY KEY (id), + CONSTRAINT orders_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id), CONSTRAINT orders_status_check CHECK (status IN ('pending', 'completed')) ); diff --git a/testdata/include/tables/users.sql b/testdata/include/tables/users.sql index 6a088c3d..926305ad 100644 --- a/testdata/include/tables/users.sql +++ b/testdata/include/tables/users.sql @@ -3,9 +3,10 @@ -- CREATE TABLE IF NOT EXISTS users ( - id integer PRIMARY KEY, + id integer, email text NOT NULL, name text NOT NULL, + CONSTRAINT users_pkey PRIMARY KEY (id), CONSTRAINT users_email_check CHECK (email LIKE '%@%') );