Migrations, Seeds, and Test Data

Rehearse a schema change on made-up data, check the old rows afterward, and prove you can get back to where you started.

Content date: October 15, 2026.

Plain English

A migration is a versioned change to the structure or contents of a database. Seed data is a known set of starting records for development and tests. A safe migration accounts for existing rows, for old and new app versions running at once, and for a failure halfway through. A database can accept a rename or drop while the app that still uses the old name breaks.

Use this when

Use this before you change a database that a live app already uses. Use it also whenever a new environment needs a repeatable starting state.

If you are just starting

Work in a throwaway database filled with made-up records. Write down the starting schema version and make a backup you have actually opened. Prefer an additive change that old code can still handle, such as a new column with a default. Fill in old rows on purpose, then check them. Keep schema migrations separate from demo seed scripts.

When your project grows

Ask how long the change holds locks, how many rows it touches, and whether old and new app versions can run side by side. Postgres adds a column with a constant default almost instantly, but a volatile default such as clock_timestamp() rewrites every row, so add the column empty, fill it in, then set the default. SQLite requires a non-null default when you add a NOT NULL column. When needed, split the work: add the new column, fill in data, switch the app, and remove the old column in a later release. Test a fresh install and an upgrade from the real previous state. A rollback script cannot restore thrown-away data, so rehearse a restore or a fix that moves forward.

Worked example

A tutoring app's documents table gains a category field. The first migration adds it as NOT NULL with the default 'general', so every existing row stays readable by old queries. A later app release starts using category. If an older field must go, removing it is a separate step, taken only after every caller has moved to the new one.

Practice

Add a field without losing the old workflow

  1. Start a throwaway version-one database. Save its schema and made-up rows.
  2. Make a backup and open it separately to confirm it works.
  3. Apply one additive migration that gives existing rows a sensible value.
  4. Run the old query and the new query, check the default, then run the migration a second time and note what happens.
  5. Restore the starting state into a separate database. Write down what the restore keeps and what you would lose if new writes had happened since the backup.

Deliverable: The migration, your before-and-after checks, and a recovery note naming the database and version you tested.

Common mistakes to watch for

  • A required column is added to a table with 4,000 rows, and the migration fails because old rows have no value for it.
  • A demo seed script runs against the real database and deletes customer bookings.
  • The migration succeeds, but a server running last week's code keeps asking for the removed column.
  • A nightly backup exists, but nobody has restored from it until an outage.
  • A migration fails halfway, someone reruns it blindly, and the data ends up half converted.

Prompting

A weak prompt

Drop the old columns and reseed the database so it matches the new code.

A better prompt

Plan this schema change: [describe the change]. Use the current schema and a throwaway database with made-up data. List risks around old rows, locking, backfill, and failure, and choose an additive path where practical. Rehearse a fresh setup and an upgrade, check old and new queries, and show me how to recover. Do not touch production data or run destructive seeds.

Most migration damage happens in the transition and recovery, so those are what you rehearse.

Check before you move on

  • You wrote down the starting schema version.
  • Every seed record is made up.
  • You checked which old code still reads the changed tables.
  • You looked at existing rows after the change.
  • You know what happens if the migration fails halfway or runs twice.
  • You restored the backup at least once.

You are done when

  • A fresh setup and an upgrade of the old database both end in the intended state.
  • You restored the starting state and can say exactly which new writes that recovery would lose.

Questions to think about

Your ALTER TABLE ran without an error. Is the upgrade safe to ship?

Not yet. The command only proves the database accepted it. Check existing rows, old and new queries, lock time, and what a failure leaves behind.

You dropped a column, then wrote a down migration that adds it back. Is the old data back?

No. Re-adding the column creates an empty one. The values only return from a backup or another copy, and writes made since then would still be missing.

A demo seed script reads the database address from a config file. Is that enough to make it safe to run?

No. Someone can point that config at the real database by accident. Make the script refuse to run anywhere except a database you have marked as disposable.

Official references