Change SQL database schema
Encore database schemas are changed over time using migration files.
Each migration file has a sequence number, and migration files are run in sequence when deploying. Encore tracks which migrations have already run and only runs new ones.
To change your database schema, add a new migration file using the next available migration number.
For example, if you have two migration files already,
the next migration file should be named
something is a short description of what the migration does.
Database migrations are applied before the application is restarted with the new code. Always make sure the old application code works with the new database schema, so that things don't break while your new code is being rolled out.
Let's say you have a single migration file that creates a
CREATE TABLE todo_item ( id BIGSERIAL PRIMARY KEY, title TEXT NOT NULL, done BOOLEAN NOT NULL );
And now you want to add a
created column to track when each todo was created.
Add a new file:
ALTER TABLE todo_item ADD created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW();
The next deploy Encore will notice the new migration file and run it, adding a new column.