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