Search⌘ K
AI Features

Migrations

Understand how to use Alembic for handling PostgreSQL database migrations in a production-ready Python system. This lesson covers initializing Alembic, configuring it to work with environment variables, autogenerating migration scripts, applying migrations, and verifying database changes through PostgreSQL commands.

We need a way to create tables that correspond to the objects defined in rentomatic/repository/postgres_objects.py.

Alembic

The best strategy when we use an ORM like SQLAlchemy is to create and run migrations. For this, we can use Alembic.

Perform migrations using Alembic

First of all, let’s initialize Alembic. In the project’s main directory (where manage.py is stored), let’s run the command below:

$ alembic init migrations

The command above creates a directory called migrations that contains Alembic’s configuration files and the migrations that will be created in ...