The lesson Choosing the Right Data Type showed that money stored as REAL can drift. So Sunset Coffee wants to replace the price_usd (REAL) column in export_products with price_cents (INTEGER). The quickest way: one migration that adds price_cents, fills it, then drops price_usd right away. The migration succeeds. But the new version of the app code only finishes deploying (being installed on the server) 2 minutes later. For those 2 minutes, the old code still reads price_usd, so the catalogue throws errors for every shopper.
The migration isn't what went wrong, the order is. A migration is a file that records one change to the database's structure, for example adding a table, adding a column or dropping a column. Migrations run in order, and the database records which ones have run. The result: your laptop, your teammate's laptop and the production server (the server that serves real shoppers) all have exactly the same structure. The expand/contract pattern is a way to change that structure without making the app throw errors. Picture renovating the kitchen of a restaurant that stays open. You don't tear down the old kitchen before the new one is ready. You build the new kitchen next to it (expand), move the cooks to it one by one, and only then tear down the old one (contract). In the database, the old kitchen is the old column price_usd, the new kitchen is the column price_cents, and the cooks are the app code that uses those columns.
One migration file in Laravel
Make the file with artisan, Laravel's built-in command-line program:
php artisan make:migration add_price_cents_to_export_products_table
Laravel creates a file whose name starts with the date and time, for example 2026_10_08_090000_add_price_cents_to_export_products_table.php. You write its contents like this:
return new class extends Migration
{
public function up(): void
{
Schema::table('export_products', function (Blueprint $table) {
$table->integer('price_cents')->nullable();
});
}
public function down(): void
{
Schema::table('export_products', function (Blueprint $table) {
$table->dropColumn('price_cents');
});
}
};
up()holds the change: it adds aprice_centscolumn that may be empty (->nullable()). On SQLite, Laravel turns it into this SQL:alter table "export_products" add column "price_cents" integer.down()holds the opposite, for a rollback (undoing the change).php artisan migraterunsup()of every file that hasn't run yet. Then it records the file's name in themigrationstable, along with a batch number. All the files run by onemigrateget the same batch number.php artisan migrate:rollbackrunsdown()of the files in the last batch.
A file already recorded in the migrations table is never run again. So if you edit an old migration that has already run in production, that server answers Nothing to migrate., and your change never reaches the database. For the next change, always make a new migration.
How risky a change is
| Change | Risk | Why |
|---|---|---|
add column (nullable), add index |
low | Old code doesn't care about a new column or index |
rename column, change type |
medium | Old code still uses the old name or type, and data may fail to convert |
drop column, drop table |
high | The data is gone for good, and code that still uses it fails at once |
Additive changes (adding) are safe. Destructive changes (removing or replacing) need careful steps. One note about indexes: in PostgreSQL, a normal CREATE INDEX blocks every write to the table (INSERT, UPDATE, DELETE) until the index is built. On a table with millions of rows, that can take a long time. That is why PostgreSQL has CREATE INDEX CONCURRENTLY, which builds the index without blocking writes.
The shortcut that throws errors
This is what happens in the Lab when the shortcut from the start of this lesson runs: DROP COLUMN (removing the column) runs while the old app is still reading price_usd. The -- line above a query says what the old app does, and the -- line under it is SQLite's error:
ALTER TABLE export_products ADD COLUMN price_cents INTEGER;
UPDATE export_products SET price_cents = CAST(ROUND(price_usd * 100) AS INTEGER);
ALTER TABLE export_products DROP COLUMN price_usd;
-- the old app opens the catalogue:
SELECT name, price_usd FROM export_products;
-- no such column: price_usd
-- the old app adds a product:
INSERT INTO export_products (name, price_usd) VALUES ('Red Ginger', 2.75);
-- table export_products has no column named price_usd
Until the new code finishes deploying, the catalogue and the admin page throw errors. A migration and a code deploy never happen in the same second. During a gradual deploy (servers are replaced one at a time), servers with the old code and the new code even run side by side. So at every second, the database structure has to fit every version of the code that is running.
Expand/contract, step by step
The table starts as export_products (id, name, price_usd) holding Arabica Coffee 250 g $4.35, Milk Tea Sachet $1.10, Palm Sugar 500 g $2.20. In the table of steps below, v1, v2 and v3 are three versions of the app code, and a backfill means filling the new column for the old rows. These five steps are exactly the buttons in the Lab:
| Step | What runs | App that's live |
|---|---|---|
| 1. Expand | ALTER TABLE export_products ADD COLUMN price_cents INTEGER; |
v1: reads and writes price_usd |
| 2. Write both columns | deploy v2 | v2: reads price_usd, writes both |
| 3. Backfill | UPDATE export_products SET price_cents = CAST(ROUND(price_usd * 100) AS INTEGER) WHERE price_cents IS NULL; |
v2 |
| 4. Switch reads | deploy v3 | v3: reads and writes price_cents only |
| 5. Contract | ALTER TABLE export_products DROP COLUMN price_usd; |
v3 |
- Expand adds a nullable column (one that may be empty). Code v1 doesn't know the column exists, so it keeps working.
- Write both columns first, then backfill. If the backfill ran while v1 was still live, a product that v1 added afterwards would have an empty
price_cents. With v2 live first, every new product is already filled in both columns, so the backfill only has to fill the old products. - Backfill uses
ROUND(rounding). WithoutROUND,CAST(4.35 * 100 AS INTEGER)gives434, so one cent is lost.WHERE price_cents IS NULLmakes the backfill fill only the rows that are still empty. - Switch reads after the backfill. If v3 goes live first, old products appear with no price.
- Contract comes last, once no code uses
price_usd.
At every step, every request in the Lab succeeds. It takes longer than the shortcut, but there isn't a single second of errors.
📌 On big production tables
- Backfill a little at a time. An
UPDATEof millions of rows at once locks all those rows (in SQLite, the whole database) for a long time. Do it 1,000 rows at a time, for example withchunkById()in Laravel, in a background job (a separate task that runs on the server). - Test on staging first, a test server with a copy of data that looks like production. Make a backup before a high-risk step.
down()can't always bring data back. Adown()forDROP COLUMNcan create the column again, but its contents are gone. For a high-risk change, the real way to undo it is to restore the backup.
⚠️ What makes a migration fail, or break the app
- Editing a migration that has already run. It's recorded in the
migrationstable, so it doesn't run again. Make a new migration. - Adding a
NOT NULLcolumn without a default. SQLite refuses:Cannot add a NOT NULL column with default value NULL. Add it as nullable first, fill it, then add the rule in the next migration. CASTwithoutROUNDwhen turning dollars into cents.- Dropping before all the code has moved. Old code that's still running fails at once.
- In the exercise, the risk is written in lower case:
"low","medium","high".
TL;DR: A migration is a file with one change to the database structure. It is made with php artisan make:migration, run with php artisan migrate, then recorded in the migrations table, so it never runs twice. That is why a new change always needs a new migration. Additive changes (adding: add column, add index) are low risk, rename or change type (a new name or type) medium, drop column or drop table (removing a column or a table) high. To replace a column without errors, use expand/contract. Add the new column as nullable (it may be empty), change the code to write to both columns, then backfill (fill the new column for the old rows). After that, change the code to read the new column, and only then drop the old column.
⚡ One migration, one change
Write:
php artisan make:migration add_price_cents_to_export_products_table
Guess first: Laravel records every migration that has run in the migrations table. You edit an old migration that has already run in production (the server shoppers use), then run php artisan migrate. What happens?
The server answers Nothing to migrate.. That file is already recorded in the migrations table, so it doesn't run again.
Meaning: This command makes a new migration file. A migration is a file with one change to the database structure. Inside that file, up() holds the change, down() its opposite, for a rollback (undoing it). A new change always needs a new migration.
⚡ How risky each change is
Meaning: Additive changes (adding: add column, add index) are low risk. Rename or change type (a new column name or type) is medium, because old code still uses the old name or type. Drop column or drop table (removing a column or a table) is high: the data is gone, and code that still uses it fails at once.
In the exercise, the risk is written in lower case: "low", "medium", "high".
⚡ Expand first, contract last
Write:
ALTER TABLE export_products ADD COLUMN price_cents INTEGER;
Meaning: This is the expand step: the new column price_cents is added next to the old column price_usd, and it may be empty. The full expand/contract order: add the new column, change the code to write to both columns, backfill (fill the new column for the old rows), change the code to read the new column, and only then drop the old column (contract).