Backend Migrations
This is a guide to use migrations for LearnHouse, it will walk you through the process of using migrations in the backend.
Prerequisites
- Docker (opens in a new tab)
- Python 3.12.x (opens in a new tab)
- Poetry (opens in a new tab)
- Alembic (opens in a new tab)
- macOS, Linux or Windows
For details on setting up the backend dev environment, see Setting Up Your Dev Environment
General Concepts
Generally, we use Alembic for SQL migrations. Make sure to check out their tutorial (opens in a new tab) if you have never used it before.
Adding Migrations
Navigate to your backend directory at apps/api
Then use the following command to indicate that the database is already using the newest state.
Make sure that the production app (in the container stack) was running at least once. This way, you can guarantee that the DB is up-to-date.
poetry run alembic stamp head
Perform Changes In The DB
Perform any change on the schema of the database, you could add a table for the "Example" feature, for instance. To generate a revision with the changes you did on your DB.
poetry run alembic revision --autogenerate -m "Example"
Check Migrations History
Check your migrations history
poetry run alembic history
Perform all the Migrations
Do all the migrations
poetry run alembic upgrade head
In order to check the migrations, visit a local directory that maps to this path (opens in a new tab).
For downgrading the latest migration, you can use alembic downgrade -2
.
Make sure to consult the Alembic docs for further information.