Good Code
The good version adds the column, fills existing rows, then tightens the constraint after data is ready.
Lesson 08
Split schema changes, backfills, and stricter constraints into safe migration steps.
ALTER TABLE users
ADD COLUMN display_name TEXT;
UPDATE users
SET display_name = COALESCE(NULLIF(name, ''), email)
WHERE display_name IS NULL;
ALTER TABLE users
ALTER COLUMN display_name SET NOT NULL;ALTER TABLE users
ADD COLUMN display_name TEXT NOT NULL;
UPDATE users
SET display_name = name;The good version adds the column, fills existing rows, then tightens the constraint after data is ready.
The bad version adds a required column before existing rows have a value. It can fail immediately or force unsafe defaults.