Platformatic DB is already set up to run migrations for you when it starts. It uses Postgrator under the hood to run migrations. Please refer to the Postgrator documentation for guidance on writing migration files.
In brief, you should create a file structure like this
|- ... and so on
Postgrator uses a table in your schema, to store which migrations have been already processed, so that only new ones will be applied at every server start.
You can always rollback some migrations specifing what version you would like to rollback to.
$ platformatic db migrations apply --to 002
003.undo.sql in this order. If you keep those files in migrations directory, when the server restarts it will execute
004.do.sql in this order.
It's also possible to rollback a single migration with
$ platformatic db migrations apply -r
How to run migrations
There are two ways to run migrations in Platformatic DB. They can be processed automatically when the server starts, or you can just run the
db migrations apply command.
In both cases you have to edit your config file to tell Platformatic DB where are your migration files.
Automatically on server start
To run migrations when Platformatic DB starts, you need to use the config file root property
There are two options in the
dir(required) the directory where the migration files are located. It will be relative to the config file path.
autoApplya boolean value that tells Platformatic DB to auto-apply migrations or not (default:
Manually with the CLI
See documentation about
db migrations apply command
- be sure to define a correct
migrations.dirfolder under the config on
- get the
MIGRATION_NUMBER(f.e. if the file is named
npx platformatic db migrations apply --to MIGRATION_NUMBER