No history yet

Alembic Environment Setup

Setting Up the Migration Environment

Your SQLAlchemy models define the ideal state of your database schema. But as your application evolves, those models will change. You'll add new tables, remove columns, and modify data types. How do you apply these changes to a live database without losing data? That's where a migration tool comes in.

Alembic acts like version control for your database. It compares your SQLAlchemy models to the actual database schema and generates migration scripts to bridge the gap. This allows you to apply changes systematically and even roll them back if needed.

To get started, you need to create an Alembic environment. This is a one-time setup process. In your project's root directory, run the following command:

$ alembic init migrations

This command creates a new migrations directory and populates it with everything Alembic needs to manage your schema versions. It also creates a configuration file named alembic.ini in your project root.

File / DirectoryPurpose
alembic.iniThe main configuration file for Alembic.
migrations/The directory that will hold all your migration scripts.
migrations/env.pyA Python script that's run every time you invoke Alembic.
migrations/script.py.makoA template used to generate new migration files.
migrations/versions/This is where Alembic stores the individual migration script files.

Connecting to Your Database

With the environment created, the next step is to tell Alembic how to connect to your database. This is done in the alembic.ini file. Open it and look for the sqlalchemy.url line.

[alembic]
# ... other settings

# A relative path to the 'migrations' directory. You can use an absolute
# path if you prefer.
script_location = migrations

# The database connection URL. This is the line you need to change.
sqlalchemy.url = driver://user:pass@localhost/dbname

You need to replace driver://user:pass@localhost/dbname with the actual connection string for your database, the same one you use to create your SQLAlchemy engine. For example, for a local PostgreSQL database named myapp, it might look like postgresql://user:password@localhost/myapp.

Linking Your Models

Finally, and most importantly, we need to tell Alembic about our SQLAlchemy models. Alembic needs access to your DeclarativeBase metadata to compare the state of your models against the state of the database.

This connection is made in the migrations/env.py file. Open that file and you'll see a lot of boilerplate code. The key is to import your models' base metadata and assign it to the target_metadata variable.

Find the line that says target_metadata = None and replace it with code that points to your models.

Let's assume your models are defined in a file called models.py inside an app directory, and your DeclarativeBase instance is named Base. You would add an import statement at the top of env.py and then modify the target_metadata assignment.

# In migrations/env.py

# ... other imports

# Add this line to import your Base from your models file
# The path might be different for your project structure.
from app.models import Base

# ... other code

# Find this line, which is usually around line 21
target_metadata = None

# And change it to this:
target_metadata = Base.metadata

# ... rest of the file

By setting target_metadata = Base.metadata, you give Alembic the blueprint it needs. When you ask it to generate a migration, Alembic will connect to the database specified in alembic.ini, inspect its current schema, compare it to the target_metadata you just provided, and generate the necessary script to align the two.

With alembic.ini pointing to your database and env.py pointing to your models, your migration environment is now fully configured and ready to use.

Quiz Questions 1/5

What is the primary function of Alembic in a project that uses SQLAlchemy?

Quiz Questions 2/5

Which command is used to create the initial Alembic environment, including the migrations directory and the alembic.ini file?