#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
0:03 to have it automatically track changes to our code
0:06 and then migrate our database accordingly.
0:09 So we had already connected the database to Alembic
0:12 now we need to connect our SQLAlchemy models
0:15 over to our database.
0:17 So here you can see that we're importing the package
0:20 and we're importing all of the models from our data
0:23 and then we're going to go and actually get the modelbase
0:26 SQLAlchemyBase, and set the metadata
0:28 to the target metadata in the env.py file.
0:32 This is in the Alembic folder.
0:33 So it's that line right there that connects
0:36 our ORM models to Alembic, so it can do the
0:39 comparison and figure out what's changed.
0:41 Now, in order to make a change
0:43 we simply run a single command, alembic revision.
0:46 Normally we're going to give it this -m message here
0:49 so it can always track what the change was.
0:51 This is going to help name the file
0:53 that's going to appear in that version's folder.
0:55 But we're also going to give it the --autogenerate.
0:57 So it's going to go and say, Oh, look we found this
0:59 in this case, user last login, and it kicks out
1:02 that file into versions and some hash there
1:05 and then basically the name of the comment
1:08 in a file friendly way, and it's done.
1:10 If you open up that file you'll see something
1:11 kind of like this.
1:12 Oh, we've added a column to the users and
1:15 it was the last login with a string and nullable and so on.
1:17 This discovered the change, but this doesn't actually
1:20 do anything for our database.
1:22 It's not until we run the migration that that works.
1:25 So the next step is to run alembic upgrade
1:28 and you give it some version.
1:29 If you just want the latest
1:30 it's almost always what you want anyway
1:32 you say, alembic upgrade head
1:33 and it spits out a couple pieces of information.
1:36 But the most important part is running upgrade
1:38 from one version to another
1:39 and then it prints out the comment.
1:41 If there were more changes, you would see more output.
1:43 Finally you can ask, What version are you on?
1:45 alembic current, and you'll see now
1:47 that we're on this one which is the head.
1:49 That means it's the latest.
1:50 We also saw that this version is stored in your database
1:54 so there's a new table that will be there
1:55 called alembic_version. It's super simple.
1:58 If you open it up, it's one row, one column
2:01 named version_num. Those are the versions.
2:04 That corresponds to one of the entries in the
2:07 Alembic versions folder, and Alembic knows how to
2:09 figure out where that is in the whole set of changes
2:12 and what other changes need to be applied.
2:14 So it's this one simple little bit of information
2:16 in your database that tells Alembic where it is
2:19 and what changes will need to be applied to it.
2:21 Best of all, after we run that migration
2:24 our website works again!
2:25 We saw by adding a column or making changes
2:28 to the SQLAlchemy models can easily easily
2:30 break our code.
2:32 If those become out of sync, SQLAlchemy checks that
2:35 and freaks out and says
2:36 Whoa, there's something different here.
2:37 I can't talk to this anymore.
2:38 So Alembic was able to determine those changes
2:41 apply them to the database
2:43 and now our website works again.
2:45 Hooray!
2:46 If you're using SQLAlchemy or some other ORM
2:48 I definitely recommend you check out Migrations.
2:51 It's really really important and makes it
2:54 so much easier to keep your code in sync.
2:56 The alternative is to do it by hand
2:58 and that's error prone and not always going to go well.