Skip to content

Migrations

Migrations

Sometimes we need to modify the content of the database, to do that we implement migrations within laputa. These migrations are always checked before starting the server and ran if needed

How do migrations work

To manage all the migrations, we use our custom MigrationManager to check and apply (forward or backward) them.

Using the CLI (manage.py):

$ pip install -e .

$ toucan-ctl migrate list
  [APPLIED] Migration name: "migration_2022_01_06_15_19_00_ms_teams_team_id.py"
  [APPLIED] Migration name: "migration_2022_01_27_17_31_00_refresh_tokens.py"

$ toucan-ctl migrate down migration_2022_01_27_17_31_00_refresh_tokens.py --force
  2022-06-07 14:50:27 - INFO  - laputa.app.migrations | Downgrading migration "migration_2022_01_27_17_31_00_refresh_tokens.py"

$ toucan-ctl migrate up migration_2022_01_27_17_31_00_refresh_tokens.py
  2022-06-07 14:51:17 - INFO  - laputa.app.migrations | Applying migration "migration_2022_01_27_17_31_00_refresh_tokens.py"
  2022-06-07 14:51:17 - INFO  - laputa.app.migrations.migration_2022_01_27_17_31_00_refresh_tokens | Ran refresh tokens migration

The MigrationManager is also usable laputa, but you shouldn't have to:

  from laputa.app.migrations import MigrationManager
  migration_manager = MigrationManager(database_manager)
  migration_manager.migrations

Behind the hood, the state of the migrations is saved in the mongo collection migrations. We documents contain the migration name, the date it has been applied at and whether it is skipped.

How to write a migration

You can refer to this HowTo page