#100DaysOfWeb in Python Transcripts
Chapter: Days 61-64: Database migrations
Lecture: Introduction to db migrations

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Hello and welcome to our next 100 days project
0:03 in our 100 days course.
0:05 This time we're going to talk about
0:06 SQLAlchemy database migrations.
0:09 Now, we've previously spoken about SQLAlchemy.
0:12 It's a really great way to create classes
0:14 they map to your database
0:15 and you can work with Python objects
0:17 and just magically read and save those
0:20 into read from save them into your database.
0:23 However, that's the perfect starting from scratch story.
0:27 In practice, your code evolves over time.
0:30 The chances that your SQLAlchemy classes
0:32 which generated your database
0:34 will stay that way, the chance that you got
0:37 that database structure exactly right the first time
0:39 is very small. Right? Code evolves over time.
0:42 So in order to evolve our database
0:44 along with our code using SQLAlchemy
0:47 or other ORMs, like Django ORM
0:49 you have to use this concept called migrations.
0:51 It's a database migration.
0:52 It takes our database from one schema
0:55 one shape, into another.
0:56 And the goal of that is to keep our database
0:58 in sync with our ORM classes.
1:01 So, what's the problem?
1:02 Well, we're starting with our application in one form.
1:05 We've made some changes, and we want to
1:08 run our code again, this time with
1:10 a different database schema.
1:11 Well, sometimes you can get away with the
1:14 schema not matching exactly.
1:15 But generally what's going to happen is
1:17 when you go to run your site
1:18 you won't have this beautiful working
1:20 application any more.
1:21 You're going to have this error page.
1:23 SQLAlchemy exception, operational exception
1:25 no such column, whatever it is.
1:28 So, how do we fix this?
1:29 Well, we could by hand manually go in there
1:32 and continuously change and evolve our
1:35 database schema, that's one option.
1:37 Or, we can use some kind of package
1:39 that will do that for us. So enter Alembic.
1:42 Alembic is a database migration library
1:45 created by Mike Bayer.
1:47 Mike Bayer is the same guy who created SQLAlchemy.
1:49 So, those two go really well together
1:51 as you can imagine.
1:52 And the idea is, we're going to set up our
1:54 web application with Alembic
1:56 tell it about our SQLAlchemy models
1:58 tell it about our database
1:59 and then we're going to say, "Alembic, keep our database
2:02 in sync with our code."
2:04 And this is great because as we move from
2:06 dev machine to dev machine
2:07 maybe the database looks different
2:09 so we move from development to staging to production
2:12 we might need to make different changes to those
2:14 different databases to get them in sync.
2:16 And Alembic handles all of that for us.
2:19 So throughout this chapter
2:20 we're basically going to focus on using Alembic
2:23 to make changes throughout our code
2:26 and keep our database in sync.