#100DaysOfWeb in Python Transcripts
Chapter: Days 61-64: Database migrations
Lecture: Your-turn: Days 2, 3, and 4

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I hope it was fun to watch me
0:01 use Alembic and write some code
0:03 and do some migrations, but it's your turn now.
0:06 Let's walk through really quickly what you're
0:07 going to do on the rest of the days that you have
0:11 for working with Alembic in a hands-on form.
0:13 Here we are on our DB Migrations section.
0:16 Of course these numbers could always change
0:18 but I think they'll ...
0:19 We're pretty sure about them, but you never know.
0:20 Let's go over to the Your Turn section.
0:23 Notice we have this hover share project in here.
0:25 This is a copy of a previous one
0:27 we worked on in SQLAlchemy. We're going to use that.
0:30 We're going to start by just watching the videos on
0:32 the first two days.
0:34 Watch the videos, play around with the code
0:36 things like that.
0:37 Then when we get to the third day
0:39 that's when it's time to start
0:41 playing with Alembic yourself.
0:42 We're going to take that copy of hover share you saw
0:44 and we're going to configure it
0:47 so create a virtual environment, update pip
0:50 install the requirements, things like that
0:52 and then just run it to make sure it works.
0:54 You should be able to run this of course
0:55 with the virtual environment
0:57 and then you should see it running.
0:58 Everything should be great.
1:00 The other thing to do is remind yourself of
1:02 the SQLAlchemy classes and how SQLAlchemy is working.
1:05 In that folder, we have all of our
1:07 SQLAlchemy classes that we're mapping to our database.
1:10 Have a look around there once you get this going
1:12 and that's pretty much day three.
1:13 I think that one will be pretty quick actually.
1:15 If you want, you can just do
1:16 a little work ahead on day four I guess.
1:19 Final one to wrap up this chapter on Alembic.
1:22 We're going to make sure that
1:23 we have everything installed.
1:26 We've already run this command
1:27 but the goal here is to add Alembic to
1:29 your requirements.txt and then rerun it.
1:31 Of course that's just going to be
1:32 equivalent to installing Alembic.
1:34 Now we need to create the file structure as we saw.
1:38 We're going to alembic init alembic on
1:40 the command prompt or terminal there.
1:43 Remember, again, this has to have
1:44 the virtual environment activated.
1:45 When you're done with all that, you should have
1:47 a structure that looks like this.
1:48 We have our hover share code
1:49 and we have Alembic and its versions
1:52 as a sibling folder.
1:54 The two steps that we're going to have to do
1:57 in order to get Alembic to automatically
1:59 generate these migrations is to teach it
2:02 about the connection string.
2:04 In the alembic.ini, we've got to set that value
2:07 and we need to import all the models into this.
2:10 That was pretty straightforward.
2:11 Let's import all the models.
2:13 But it turns out that was actually simple
2:15 because our Pyramid app was an installed package.
2:19 When it's not an installed package, this gets harder.
2:21 But I wanted to show it to you here
2:23 and have you go through it because
2:25 not everything we work on in Python is a package.
2:27 But it's a very valuable skill to have.
2:30 We're going to look through here, and it tells you
2:33 how to basically add the root of your project
2:36 this project, to the path, to Python path.
2:39 Then we can go and import the files as if
2:41 Alembic was working at the top level of
2:43 our project, which it's not actually.
2:45 But this will make it think that it is.
2:47 Once you get that all done
2:48 we'll do a few migrations down here.
2:49 Just follow along and you should be able to
2:52 add some features to your SQLAlchemy classes
2:54 see that it no longer runs
2:56 auto-generate a migration, apply the migration
2:59 and your new code should be
3:00 running on your new version
3:02 new scheme of your database with everything working.
3:05 That's what we have in store for you
3:07 for the next couple of days.
3:08 Play around with Alembic.
3:09 It saves you so much trouble
3:11 and takes so many things that are manual
3:13 and error-prone and scary
3:14 and makes them easy and automatic.