#100DaysOfWeb in Python Transcripts
Chapter: Days 61-64: Database migrations
Lecture: Installing Alembic

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw this last_payment column being in our SQLAlchemy model but not in the database causes problems. Boom. No such column bills.last_payment.
0:11 So we've decided to use Alembic to keep our SQLAlchemy classes in sync with our database. Now, in order to use Alembic
0:19 there's two things we have to do. First, we have to simply install the package. Right? Like any other package in Python, we have to install it.
0:27 So, we can come down here and we don't have the requirements for this project but we have our setup. So let's go down here and say
0:34 this is going to require Alembic. Put the closing comma there to avoid more diffs. We can install this, we don't want to install it there.
0:45 Probably the easiest way is just to say Python. Make sure your virtual environment is active. Python setup develop, and that will reinstall
0:52 the one thing that we need which is Alembic. If we do a pip list, let me get it scrolled in right you can see we have the current version of Alembic.
1:00 And so, great. So, that warning's going to go away. Now, that's part one. There's actually another part.
1:06 Notice over here, there's nothing to do with Alembic. There's not an Alembic folder, things like that. So, in this directory
1:13 the one that has the development.ini the production.ini, and so on. In that directory, we want to run the command alembic init alembic.
1:23 Now, it looks kind of symmetric and whatnot. Redundant. That's the command, that's the subcommand, init and then this is the directory.
1:31 So, we're going to create a directory called alembic and that's where Alembic is going to keep track of all the stuff happening.
1:37 So, we run this, we see it's generated a couple of things. At the top level, we now have alembic.ini right there. Okay? And we open this up, we see
1:47 generally, you don't have to mess with this but you can configure Alembic this way. There's one thing we have to change there.
1:51 And we have an Alembic folder that has versions and a bunch of other stuff. Versions, right now, is empty.
1:58 We're going to work on that in just a second. This versions folder is where various changes in form of Python files that need to be applied
2:06 to our database. Those will be generated and stored and tracked there by Alembic. Okay, so we do see a little warning down here that says
2:15 you need to mess with this file before proceeding. We need to set that up. So, we do have it open and the thing that we need to set here
2:25 is the sqlalchemy.url. So, what is that? Let's go find that. Remember, in our DB session, we had to come up with this
2:33 and we said, connecting to our database at such and such. If we go back and we ran it way at the top before it crashed, that big sucker right there
2:42 if we get all the way to the end that is the URL that we need to put right here. However, how generic does this look?
2:49 Would this also work on another machine if another developer checks it out? Probably no. So, what we can do is we can go over here
2:55 and if I get this right you can put a dot there. And so, the scheme is always specified by database type colon three slashes.
3:04 And then, this says current working directory down to build tracker, into db, and so on. Alright, so, this folder this folder, there.
3:13 Okay, hopefully we have everything right. So, let's give this a quick test. We come down here and we can say Alembic, we can ask
3:20 what current version of Alembic do we have installed? Now, it spit out some information and then said nothing. So, this info stuff you can ignore
3:28 but basically, the fact that it did not say a version with a little parenthesis head or what step it was on means that there've been no migrations
3:36 applied to this database. Alright, so, just to verify things are still broken, but we do have Alembic setup.


Talk Python's Mastodon Michael Kennedy's Mastodon