Adding a column (a table step)

This is what you used to do by adding a block to upgradedb.sql. Now it is a step file in ~/dev/improvize/lesage/sql/steps/.

1. Pick the version

Look at lesage/sql/version and lesage/released.json:

Several files per version are fine. They run in byte-wise filename order, so use a suffix to order them: 0002-1-…, 0002-2-…. Lint refuses a step numbered above version.

Never add a block to a step that belongs to a released version (e.g. 0001-1-upgradedb.sql). The databases that already have version 1 would never run it, and lint fails because the released checksum no longer matches.

2. Write the step

-- steps/0002-1-oeu-isdeposited.sql
-- Why: (ticket / one sentence). Converges on every replay: addcolumn skips an existing column.
DO $step$
BEGIN
	IF tableexists('oeu') THEN
		PERFORM addcolumn('oeu', 'IsDeposited', 'BOOLEAN NOT NULL DEFAULT FALSE',
			'The work has been deposited with the society');
	END IF;
END
$step$;

Same idea as in upgradedb.sql, with three differences:

Backfill only when the column is new

addcolumn returns TRUE when it created the column, so a backfill runs once and a replay skips it:

DO $step$
BEGIN
	IF addcolumn('oeu', 'IsDeposited', 'BOOLEAN NOT NULL DEFAULT FALSE') THEN
		UPDATE oeu SET IsDeposited = TRUE WHERE datedepotsacem IS NOT NULL;
	END IF;
END
$step$;

Other guarded changes

DO $step$
BEGIN
	-- new table
	CREATE TABLE IF NOT EXISTS public.depots (iddepot SERIAL PRIMARY KEY, idoeu INTEGER NOT NULL, datedepot DATE);

	-- index
	IF NOT indexexists('ix_depots_idoeu') THEN
		CREATE INDEX ix_depots_idoeu ON public.depots (idoeu);
	END IF;

	-- foreign key (helper checks constraintexists itself)
	PERFORM addconstraintforeignkey('fk_depots_oeu', 'depots', 'idoeu', 'oeu', 'idoeu', 'ON DELETE CASCADE');

	-- other constraint
	IF NOT constraintexists('depots', 'ck_depots_date') THEN
		ALTER TABLE public.depots ADD CONSTRAINT ck_depots_date CHECK (datedepot >= '1900-01-01');
	END IF;

	-- remove a column / widen a VARCHAR
	PERFORM dropcolumn('oeu', 'oldfield');
	PERFORM resizefield('oeu', 'soustitre', 100);
END
$step$;

addconstraintforeignkey cleans orphans before adding the key: it sets them to NULL when the column is nullable and deletes the rows otherwise. Check the data first.

Helpers available in steps (from common)

HelperReturns
addcolumn(table, column, type [, comment])TRUE if it created the column
dropcolumn(table, column)boolean
resizefield(table, column, newsize)boolean
addconstraintforeignkey(name, table, column, othertable, othercolumn, options)void, no-op if the constraint exists
tableexists, columnexists, indexexists, constraintexists, triggerexists, columnisnullable, columntype, hasdefaulttests for guards
dropconstraint, addcreatorcreated(table), fixsequence(table)

Table names without a schema resolve to the bundle's schema (search_path file, public for Le Sage).

What "idempotent" means here

Forbidden in every bundle file (lint refuses)

BEGIN / COMMIT / ROLLBACK / SAVEPOINT, SET ROLE / RESET ROLE / setrole(), plain session SET (SET LOCAL is fine), CONCURRENTLY, VACUUM, COPY, CREATE/ALTER/DROP EXTENSION, psql \ commands, udf_dropfunction. Files are UTF-8 without BOM, LF line endings.

Then

Run the checks and try it on your test database: see Testing locally without releasing.


Revision #1
Created 2026-11-18 14:55:36 UTC by Claude-Aida
Updated 2026-11-18 14:55:36 UTC by Claude-Aida