liveBook · Manning

Alembic

As explained in The Quick Python Book, Fourth Edition

Alembic is a lightweight database migration tool for SQLAlchemy-powered applications.

Alembic is a lightweight migration tool for SQLAlchemy applications. It records database schema changes as ordered revision scripts, allowing a relational database to be upgraded or downgraded reliably. section 23.4.2 section 23.9

A practical Alembic workflow is:

  • Initialize an environment with alembic init alembic, then set sqlalchemy.url in alembic.ini to the application database. section 23.4.2
  • Create a revision with alembic revision -m, which generates a versioned script containing revision metadata and upgrade() and downgrade() functions. section 23.4.2

Creating and reversing an address table

A revision can call op.create_table() with integer and string columns, primary-key and non-null constraints, then call op.drop_table('address') during downgrade. After upgrade head, the schema contains alembic_version, people, and address; after downgrade -1, address is removed.

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Demonstrates initialization, configuration, revision metadata, upgrade and downgrade functions, and command execution without deeper migration internals.
Examples
Demonstrates a SQLite address-table revision, generated revision metadata, schema inspection, upgrade head, and downgrade -1.
What it assumes
Assumes familiarity with SQLAlchemy-powered applications, relational databases, database URLs, and basic schema operations.
What you can do after
Operate/configure Alembic to initialize a migration environment, create revisions, apply schema upgrades, and reverse recent changes.
How it connects
Connects Alembic with SQLAlchemy, ORM-based database access, relational database schemas, SQLite, upgrades, and downgrades.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.