#100DaysOfWeb in Python Transcripts
Chapter: Days 61-64: Database migrations
Lecture: Concept: Alembic migrations
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's review how we configured and used Alembic to have it automatically track changes to our code and then migrate our database accordingly.
0:10
So we had already connected the database to Alembic now we need to connect our SQLAlchemy models over to our database.
0:18
So here you can see that we're importing the package and we're importing all of the models from our data
0:24
and then we're going to go and actually get the modelbase SQLAlchemyBase, and set the metadata to the target metadata in the env.py file.
0:33
This is in the Alembic folder. So it's that line right there that connects our ORM models to Alembic, so it can do the
0:40
comparison and figure out what's changed. Now, in order to make a change we simply run a single command, alembic revision.
0:47
Normally we're going to give it this -m message here so it can always track what the change was. This is going to help name the file
0:54
that's going to appear in that version's folder. But we're also going to give it the --autogenerate.
0:58
So it's going to go and say, Oh, look we found this in this case, user last login, and it kicks out that file into versions and some hash there
1:06
and then basically the name of the comment in a file friendly way, and it's done. If you open up that file you'll see something kind of like this.
1:13
Oh, we've added a column to the users and it was the last login with a string and nullable and so on.
1:18
This discovered the change, but this doesn't actually do anything for our database. It's not until we run the migration that that works.
1:26
So the next step is to run alembic upgrade and you give it some version. If you just want the latest it's almost always what you want anyway
1:33
you say, alembic upgrade head and it spits out a couple pieces of information. But the most important part is running upgrade
1:39
from one version to another and then it prints out the comment. If there were more changes, you would see more output.
1:44
Finally you can ask, What version are you on? alembic current, and you'll see now that we're on this one which is the head. That means it's the latest.
1:51
We also saw that this version is stored in your database so there's a new table that will be there called alembic_version. It's super simple.
1:59
If you open it up, it's one row, one column named version_num. Those are the versions. That corresponds to one of the entries in the
2:08
Alembic versions folder, and Alembic knows how to figure out where that is in the whole set of changes and what other changes need to be applied.
2:15
So it's this one simple little bit of information in your database that tells Alembic where it is and what changes will need to be applied to it.
2:22
Best of all, after we run that migration our website works again! We saw by adding a column or making changes
2:29
to the SQLAlchemy models can easily easily break our code. If those become out of sync, SQLAlchemy checks that and freaks out and says
2:37
Whoa, there's something different here. I can't talk to this anymore. So Alembic was able to determine those changes apply them to the database
2:44
and now our website works again. Hooray! If you're using SQLAlchemy or some other ORM I definitely recommend you check out Migrations.
2:52
It's really really important and makes it so much easier to keep your code in sync. The alternative is to do it by hand
2:59
and that's error prone and not always going to go well.