O'Reilly logo

Essential SQLAlchemy, 2nd Edition by Rick Copeland, Jason Myers

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required