Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Database migrations with Alembic
Lecture: More database changes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we're able to add that change to that column. We also added our auditing table, but I commented it out so, Alembic wouldn't notice those changes.
0:10 So now let's go over here and we can have it do another change. So I'm going to say generate another upgrade
0:18 and this upgrade is going to be adding auditing. Now it goes and runs again and says oh, this time we
0:28 are going to create a new table and that table's going to have a special index on created date. Cool, huh. So let's see what else we got.
0:35 We now have two pieces and this one says here's the revision and it derives from it depends upon this previous one. So this is how they chain together.
0:45 We're going to upgrade. It's going to go create the table in the primary key and all that. Then it's going to create the index and of course
0:52 dropping that in reverse is what's going to happen if we undo it. So let's go and run this. Let's get this database updated to the current version.
1:04 Again alembic upgrade head. This time it's going to upgrade from this version do as many steps as necessary, to that version
1:13 by adding auditing. Now again, what do we get? We now have an auditing table, beautiful. Granted that auditing table could have been added by
1:21 SQLAlchemy, but suppose that auditing table was defining a foreign key and maintainers that linked back to it.
1:29 It wouldn't be all put together correctly. Here's how we can use Alembic to manage the structure of our database.
1:37 What's really nice is we can use the Auto Generate feature here to actually keep it in sync with whatever's happening on our database.
1:48 Let's do one more quick thing to round out this lecture. At the beginning we asked alembic current to see
1:53 what the version was and it kind of did a no op and so that meant there was no version. Now we can say alembic current and it will actually
2:00 tell us this is the current version of the database and that's the latest as far as Alembic's concerned.
2:05 Double check the state of any database by asking Alembic about Current. Also in the slides I'm going to talk about this
2:13 alembic_helpers and so I'm dropping this in here so you have it. If you want to use the operations that I talk about
2:20 in a moment, but they're not actually used in the demo. I'm just including this here for the manual side of things.
2:27 Maybe it will even be helpful in the auto generated steps as well. It's especially helpful in the manual bits.
2:34 I'll just throw that in there just so you have it.


Talk Python's Mastodon Michael Kennedy's Mastodon