feat(models): add support for generated and identity columns
* extend Column struct with Generated, GenerationExpression, Identity, and IdentityGeneration fields * update queryColumns to handle new column properties * modify migration writer to avoid altering generated and identity columns * add tests to ensure correct handling of generated and identity columns
This commit is contained in:
@@ -1510,3 +1510,42 @@ func TestIndexStorageParameters(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateColumnDefinition_GeneratedColumnEmitsGeneratedClauseNotDefault(t *testing.T) {
|
||||
w := &Writer{}
|
||||
|
||||
col := models.InitColumn("geom", "info_city", "public")
|
||||
col.Type = "text"
|
||||
col.NotNull = true
|
||||
col.Generated = true
|
||||
col.GenerationExpression = "st_makepoint(lon, lat)"
|
||||
// A generated column's introspected "default" is actually its generation
|
||||
// expression surfaced via pg_attrdef; it must never be rendered as DEFAULT.
|
||||
col.Default = "st_makepoint(lon, lat)"
|
||||
|
||||
got := w.generateColumnDefinition(col)
|
||||
|
||||
want := "geom text GENERATED ALWAYS AS (st_makepoint(lon, lat)) STORED NOT NULL"
|
||||
if got != want {
|
||||
t.Fatalf("generateColumnDefinition() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateColumnDefinition_IdentityColumnEmitsIdentityClauseNotDefault(t *testing.T) {
|
||||
w := &Writer{}
|
||||
|
||||
col := models.InitColumn("id", "users", "public")
|
||||
col.Type = "bigint"
|
||||
col.NotNull = true
|
||||
col.Identity = true
|
||||
col.IdentityGeneration = "ALWAYS"
|
||||
// Identity columns never carry a literal default; this must never surface.
|
||||
col.Default = "some-stray-value"
|
||||
|
||||
got := w.generateColumnDefinition(col)
|
||||
|
||||
want := "id bigint GENERATED ALWAYS AS IDENTITY NOT NULL"
|
||||
if got != want {
|
||||
t.Fatalf("generateColumnDefinition() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user