Schema migrations
Local baseline: Node.js 22.16.0. Actual local SQLite 3.49.1 via experimental node:sqlite. No third-party packages or external connections.
Try the local example
import {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');try { db.exec("CREATE TABLE book(id INTEGER PRIMARY KEY,title TEXT); INSERT INTO book VALUES(1,'Rivers')"); db.exec('ALTER TABLE book ADD COLUMN edition INTEGER NOT NULL DEFAULT 1'); console.log(JSON.stringify(db.prepare('SELECT * FROM book').get())); db.exec('ALTER TABLE book DROP COLUMN edition'); console.log(JSON.stringify(db.prepare('SELECT * FROM book').get()));}finally{db.close();}Run node main.mjs in a disposable directory with the files shown. Expected standard output:
{"id":1,"title":"Rivers","edition":1}{"id":1,"title":"Rivers"}An experimental warning is expected on stderr for this local version.
How it works
For a compatible change, add a column with a clear default or nullable transition, backfill in a controlled way when needed, then update application reads and writes. The fixture starts with a populated SQLite table, adds a non-null edition with default 1, checks the old row and reverses the change in memory. In production, dropping a column loses its data, so reversibility of a toy schema is not proof of recoverability. Drizzle Kit offers generated SQL workflows; Prisma and TypeORM also have versioned migration workflows with different commands. Review the SQL, backup/restore plan, locks, data size and compatibility with old app replicas. Avoid automatic destructive sync at startup.
Common mistake
A schema file changing in Git does not change a running database. A migration record and actual verification of the database state are necessary.
Practice
Insert a second row after the migration, then verify its default edition. Describe why a rollback that drops this column could destroy new data.
Documentation: primary source. Candidate lookup: node-schema-migrations-lookup. Prerequisite: node-query-builders-and-orm-boundaries (platform route unverified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Schema migrations — knowledge check
Apply the lesson’s contracts and identify common boundary mistakes.
What does changing an ORM model file prove?
What happens to edition data when its column is dropped?
Before applying a migration, what should be reviewed?