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

Talk Python's Mastodon Michael Kennedy's Mastodon