feat(pgsql): ✨ Add templates for constraints and sequences
All checks were successful
CI / Test (1.24) (push) Successful in -26m21s
CI / Test (1.25) (push) Successful in -26m13s
CI / Build (push) Successful in -26m39s
CI / Lint (push) Successful in -26m29s
Release / Build and Release (push) Successful in -26m28s
Integration Tests / Integration Tests (push) Successful in -26m10s
All checks were successful
CI / Test (1.24) (push) Successful in -26m21s
CI / Test (1.25) (push) Successful in -26m13s
CI / Build (push) Successful in -26m39s
CI / Lint (push) Successful in -26m29s
Release / Build and Release (push) Successful in -26m28s
Integration Tests / Integration Tests (push) Successful in -26m10s
* Introduce new templates for creating unique, check, and foreign key constraints with existence checks. * Add templates for setting sequence values and creating sequences. * Refactor existing SQL generation logic to utilize new templates for better maintainability and readability. * Ensure identifiers are properly quoted to handle special characters and reserved keywords.
This commit is contained in:
@@ -215,3 +215,70 @@ func TestTemplateExecutor_AuditFunction(t *testing.T) {
|
||||
t.Error("SQL missing DELETE handling")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteMigration_NumericConstraintNames(t *testing.T) {
|
||||
// Current database (empty)
|
||||
current := models.InitDatabase("testdb")
|
||||
currentSchema := models.InitSchema("entity")
|
||||
current.Schemas = append(current.Schemas, currentSchema)
|
||||
|
||||
// Model database (with constraint starting with number)
|
||||
model := models.InitDatabase("testdb")
|
||||
modelSchema := models.InitSchema("entity")
|
||||
|
||||
// Create individual_actor_relationship table
|
||||
table := models.InitTable("individual_actor_relationship", "entity")
|
||||
idCol := models.InitColumn("id", "individual_actor_relationship", "entity")
|
||||
idCol.Type = "integer"
|
||||
idCol.IsPrimaryKey = true
|
||||
table.Columns["id"] = idCol
|
||||
|
||||
actorIDCol := models.InitColumn("actor_id", "individual_actor_relationship", "entity")
|
||||
actorIDCol.Type = "integer"
|
||||
table.Columns["actor_id"] = actorIDCol
|
||||
|
||||
// Add constraint with name starting with number
|
||||
constraint := &models.Constraint{
|
||||
Name: "215162_fk_actor",
|
||||
Type: models.ForeignKeyConstraint,
|
||||
Columns: []string{"actor_id"},
|
||||
ReferencedSchema: "entity",
|
||||
ReferencedTable: "actor",
|
||||
ReferencedColumns: []string{"id"},
|
||||
OnDelete: "CASCADE",
|
||||
OnUpdate: "NO ACTION",
|
||||
}
|
||||
table.Constraints["215162_fk_actor"] = constraint
|
||||
|
||||
modelSchema.Tables = append(modelSchema.Tables, table)
|
||||
model.Schemas = append(model.Schemas, modelSchema)
|
||||
|
||||
// Generate migration
|
||||
var buf bytes.Buffer
|
||||
writer, err := NewMigrationWriter(&writers.WriterOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create writer: %v", err)
|
||||
}
|
||||
writer.writer = &buf
|
||||
|
||||
err = writer.WriteMigration(model, current)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteMigration failed: %v", err)
|
||||
}
|
||||
|
||||
output := buf.String()
|
||||
t.Logf("Generated migration:\n%s", output)
|
||||
|
||||
// Verify constraint name is properly quoted
|
||||
if !strings.Contains(output, `"215162_fk_actor"`) {
|
||||
t.Error("Constraint name starting with number should be quoted")
|
||||
}
|
||||
|
||||
// Verify the SQL is syntactically correct (contains required keywords)
|
||||
if !strings.Contains(output, "ADD CONSTRAINT") {
|
||||
t.Error("Migration missing ADD CONSTRAINT")
|
||||
}
|
||||
if !strings.Contains(output, "FOREIGN KEY") {
|
||||
t.Error("Migration missing FOREIGN KEY")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user