Summary
When a new table is added to the schema and make generate-argen (diff
mode) produces an incremental migration in migrations/NNN_*.sql, the
generator emits a series of ALTER TABLE <new_table> ADD COLUMN … —
one per field — but never the preceding CREATE TABLE <new_table>.
schema.sql (the full-state snapshot, regenerated in the same run)
correctly contains CREATE TABLE IF NOT EXISTS <new_table> (…).
Impact
On a fresh install where schema.sql is applied before migrations,
the bug is invisible — the table already exists, the ALTERs are no-op.
On an existing install where only the migration files are replayed,
the migration aborts on the very first statement with:
ERROR: relation "<new_table>" does not exist
This blocks the whole deploy and is not recoverable without
hand-editing the generated migration to prepend a CREATE TABLE.
Repro
In a project consuming argen, add a new struct under
internal/.../repository/decl/:
//ar:serverConf:/example/db/main
//ar:namespace:new_table
//ar:backend:postgres
type FieldsNewTable struct {
ID int64 `ar:"primary_key;init_by_db"`
Name string `ar:"size:64"`
}
Run make generate-argen. The generated
migrations/NNN_add_table_new_table.sql contains:
ALTER TABLE new_table ADD COLUMN id BIGSERIAL NOT NULL;
ALTER TABLE new_table ADD COLUMN name VARCHAR(64) NOT NULL DEFAULT '';
CREATE UNIQUE INDEX new_table_pkey ON new_table (id ASC);
— no CREATE TABLE new_table (…).
Expected
The migration should prepend (in +up):
CREATE TABLE IF NOT EXISTS new_table (
id BIGSERIAL NOT NULL,
name VARCHAR(64) NOT NULL DEFAULT '',
PRIMARY KEY (id)
);
…and append (in +down):
DROP TABLE IF EXISTS new_table;
This mirrors what schema.sql already does for the same table.
Real-world example
We hit this in virtualIT-orchestrator-v2 migration 009 (adds
task_spawn_log) — fresh installs OK for months, first existing-install
deploy failed in staging with the error above. Prior migrations 003 and
005 also introduce new tables and exhibit the same pattern but never
failed because they happened to land on fresh DBs.
Workaround
Hand-edit the generated migration after every make generate-argen:
prepend CREATE TABLE IF NOT EXISTS <name> (…) copied verbatim from the
new schema.sql, append DROP TABLE IF EXISTS <name>; to +down.
Related
#5 — ALTER … ADD COLUMN lacks IF NOT EXISTS guards (same root cause:
diff-mode generator doesn't account for repeated apply on stateful DBs).
Summary
When a new table is added to the schema and
make generate-argen(diffmode) produces an incremental migration in
migrations/NNN_*.sql, thegenerator emits a series of
ALTER TABLE <new_table> ADD COLUMN …—one per field — but never the preceding
CREATE TABLE <new_table>.schema.sql(the full-state snapshot, regenerated in the same run)correctly contains
CREATE TABLE IF NOT EXISTS <new_table> (…).Impact
On a fresh install where
schema.sqlis applied before migrations,the bug is invisible — the table already exists, the
ALTERs are no-op.On an existing install where only the migration files are replayed,
the migration aborts on the very first statement with:
This blocks the whole deploy and is not recoverable without
hand-editing the generated migration to prepend a
CREATE TABLE.Repro
In a project consuming argen, add a new struct under
internal/.../repository/decl/:Run
make generate-argen. The generatedmigrations/NNN_add_table_new_table.sqlcontains:— no
CREATE TABLE new_table (…).Expected
The migration should prepend (in
+up):…and append (in
+down):This mirrors what
schema.sqlalready does for the same table.Real-world example
We hit this in
virtualIT-orchestrator-v2migration 009 (addstask_spawn_log) — fresh installs OK for months, first existing-installdeploy failed in staging with the error above. Prior migrations 003 and
005 also introduce new tables and exhibit the same pattern but never
failed because they happened to land on fresh DBs.
Workaround
Hand-edit the generated migration after every
make generate-argen:prepend
CREATE TABLE IF NOT EXISTS <name> (…)copied verbatim from thenew
schema.sql, appendDROP TABLE IF EXISTS <name>;to+down.Related
#5 —
ALTER … ADD COLUMNlacksIF NOT EXISTSguards (same root cause:diff-mode generator doesn't account for repeated apply on stateful DBs).