benjaminz
benjaminz

Reputation: 3228

Recover database schema from Alembic migrations

I recently reinstalled my OS and lost some old data from a local postgre db. But I backed up all the migration files using Alembic. And now I just want to restore the database schema from the migration files, not data. Is that possible?

Upvotes: 0

Views: 1063

Answers (1)

Nima Soroush
Nima Soroush

Reputation: 12814

It is doable. All migration files have a revision number. Your first migration file has something like:

revision = '22a39a2bf2ed'
down_revision = None

and your second revision file has something like:

revision = '507003430224'
down_revision = '22a39a2bf2ed'

As you can see all revision files are linked.

The only thing that you need to do is make your first migration file manually, then run

alembic upgrade head

Then you need to replace the content of this file with you previous first migration file. Then open your second migration file and replace downgrade_version number with this new number.

Now you should be able to run

alembic upgrade head

again and your database should be upgraded

Upvotes: 1

Related Questions