#100DaysOfWeb in Python Transcripts
Chapter: Days 61-64: Database migrations
Lecture: Seeing the problem in action
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So let's get started with Alembic. And I don't actually have any code to get started with. So what we're going to do
0:06
is we're going to take the last bit of code that we wrote from the Pyramid chapter. Remember, we did SQLAlchemy and then we did Pyramid
0:13
and our Pyramid app had our database stuff in there. Clear this bill tracker thing. So I'm going to copy this over.
0:19
Maybe I'll copy that part over for you so you can just see how to how I got totally started from scratch. So we need to do just two quick things
0:26
to make sure that this is a fresh copy and we didn't break it by moving basically our virtual environment and our installed stuff.
0:33
So we're going to go to this folder. Going to set it up so everything will runs well. Might as well update that.
0:39
So when I go over here, and we're going to run our venv command, which will create a virtual environment. You're going to see the command at the top
0:45
it's creating the virtual environment, updating it and activating it. And then, down here we need to run our setup in development mode.
0:53
Okay, everything should be in place. Should build a runner app. Looks like we can. Alright, so let's get out of the terminal
1:00
and go over to PyCharm here and run it. Super, we have our app here and we should build a run it also in PyCharm.
1:07
See, it's running our migration one and does it work? Course it does, it works like a charm. So it looks like everything is working great.
1:15
First thing we want to do is like let's just observe that what I showed you in the slides there, that's actually a problem. Let's go over to our models
1:22
and let's say we're just going to change something super simple about the bill. Like we've had paid, we've had the total
1:28
we had what's created, let's do this. Let's have one more column here called last_payment. Maybe we want to track this.
1:36
Like, show me all the things I haven't paid in 30 days or something to that effect. I have an index, but let's not have a default value.
1:44
So I guess we should set null ability to true. Alright, seems like that should work. Let's run our code again. Boom, it's crashed.
1:52
There's the dreaded operational error I just told you about. Bills.last_payment. Doesn't exist. Right, so our model believes there's a last payment
2:01
'cause it's right here, but our database doesn't have it. And some databases are more flexible but relational data bases, they're generally
2:08
very, very strict about their schema and their relationship. And remember, SQLAlchemy won't update existing tables.
2:16
It will create new ones but it will not update existing ones. So we're stuck, we have to fix our database to make this work.
2:21
Alright, so we can't even get started. Right, we can't even get our website to load much less to get an error out of one of our web pages.
2:29
So that's where we are and we're going to fix it with Alembic and database migrations.