Database migrations
Database migrations exist to run one-time queries against the database, typically to modify the tables structure or the data when upgrading the Strapi application. These migrations are run automatically when the application starts and are executed before the automated schema migrations that Strapi also performs on boot.
Understanding database migration files
Migrations are run using JavaScript migration files stored in ./database/migrations
.
Strapi automatically detects migration files and run them once at the next startup in alphabetical order. Every new file is executed once. Migrations are run before the database tables are synced with the content-types schemas.
Currently Strapi does not support down migrations. This means that if you need to revert a migration, you will have to do it manually. It is planned to implement down migrations in the future but no timeline is currently available.
Migration files should export the function up()
, which is used when upgrading (e.g. adding a new table my_new_table
).
The up()
function runs in a database transaction which means if a query fails during the migration, the whole migration is cancelled, and no changes are applied to the database. If another transaction is created within the migration function, it will act as a nested transaction.
There is no CLI to manually execute the database migrations.
Creating a migration file
To create a migration file:
In the
./database/migrations
folder, create a new file named after the date and the name of the migration (e.g.2022.05.10T00.00.00.name-of-my-migration.js
). Make sure that the file name follows this naming pattern, because the alphabetical order of the files defines the order in which the migrations have to run.Copy and paste the following template in the previously created file:
'use strict'
async function up(knex) {}
module.exports = { up };
- Fill in the template by adding actual migration code inside the
up()
function.up()
receives a Knex instance, already in a transaction state, that can be used to run the database queries.
Example of migration file
module.exports = {
async up(knex) {
// You have full access to the Knex.js API with an already initialized connection to the database
// Example: renaming a table
await knex.schema.renameTable('oldName', 'newName');
// Example: renaming a column
await knex.schema.table('someTable', table => {
table.renameColumn('oldName', 'newName');
});
// Example: updating data
await knex.from('someTable').update({ columnName: 'newValue' }).where({ columnName: 'oldValue' });
},
};