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