feat(migration): enhance primary key handling and add GIN index support in migration writer
This commit is contained in:
@@ -673,9 +673,14 @@ func TestPrimaryKeyExistenceCheck(t *testing.T) {
|
||||
t.Errorf("Output missing logic to drop auto-generated primary key\nFull output:\n%s", output)
|
||||
}
|
||||
|
||||
// Verify it checks for our specific named constraint before adding it
|
||||
if !strings.Contains(output, "constraint_name = 'pk_public_products'") {
|
||||
t.Errorf("Output missing check for our named primary key constraint\nFull output:\n%s", output)
|
||||
// Verify it compares the current primary key columns before dropping/recreating
|
||||
if !strings.Contains(output, "current_pk_matches") || !strings.Contains(output, "ARRAY['id']") {
|
||||
t.Errorf("Output missing safe primary key comparison logic\nFull output:\n%s", output)
|
||||
}
|
||||
|
||||
// Verify it only adds the desired key when no PK exists or an auto-generated mismatch was dropped
|
||||
if !strings.Contains(output, "current_pk_name IS NULL") || !strings.Contains(output, "current_pk_name IN ('products_pkey', 'public_products_pkey')") {
|
||||
t.Errorf("Output missing guarded primary key creation logic\nFull output:\n%s", output)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -766,6 +771,43 @@ func TestColumnSizeSpecifiers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteDatabase_PrimaryKeyTemplateDoesNotDropMatchingAutoPrimaryKey(t *testing.T) {
|
||||
db := models.InitDatabase("testdb")
|
||||
schema := models.InitSchema("public")
|
||||
table := models.InitTable("learnings", "public")
|
||||
|
||||
idCol := models.InitColumn("id", "learnings", "public")
|
||||
idCol.Type = "bigint"
|
||||
idCol.IsPrimaryKey = true
|
||||
table.Columns["id"] = idCol
|
||||
|
||||
parentCol := models.InitColumn("duplicate_of_learning_id", "learnings", "public")
|
||||
parentCol.Type = "bigint"
|
||||
table.Columns["duplicate_of_learning_id"] = parentCol
|
||||
|
||||
schema.Tables = append(schema.Tables, table)
|
||||
db.Schemas = append(db.Schemas, schema)
|
||||
|
||||
var buf bytes.Buffer
|
||||
writer := NewWriter(&writers.WriterOptions{})
|
||||
writer.writer = &buf
|
||||
|
||||
if err := writer.WriteDatabase(db); err != nil {
|
||||
t.Fatalf("WriteDatabase failed: %v", err)
|
||||
}
|
||||
|
||||
output := buf.String()
|
||||
if !strings.Contains(output, "current_pk_matches") {
|
||||
t.Fatalf("expected generated SQL to compare current PK columns, got:\n%s", output)
|
||||
}
|
||||
if !strings.Contains(output, "ARRAY['id']") {
|
||||
t.Fatalf("expected generated SQL to compare against desired PK columns, got:\n%s", output)
|
||||
}
|
||||
if !strings.Contains(output, "NOT current_pk_matches") {
|
||||
t.Fatalf("expected generated SQL to avoid dropping matching PKs, got:\n%s", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateColumnDefinition_PreservesExplicitTypeModifiers(t *testing.T) {
|
||||
writer := NewWriter(&writers.WriterOptions{})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user