Chapter 13. Controlling Alembic

In the previous chapter, we learned how to create and apply migrations, and in this chapter we’re going to discuss how to further control Alembic. We’ll explore how to learn the current migration level of the database, how to downgrade from a migration, and how to mark the database at a certain migration level.

Determining a Database’s Migration Level

Before performing migrations, you should double-check to make sure what migrations have been applied to the database. You can determine what the last migration applied to the database is by using the alembic current command. It will return the revision ID of the current migration, and tell you whether it is the latest migration (also known as the head). Let’s run the alembic current command in the CH12/ folder of the sample code for this book:

# alembic current
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
2e6a6cc63e9 (head) 1
1

The last migration applied to the database

While this output shows that we are on migration 2e6a6cc63e9, it also tells us we are on the head or latest migration. This was the migration that changed the cookies table to new_cookies. We can confirm that with the alembic history command, which will show ...

Get Essential SQLAlchemy, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.