Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: Database migrations with Alembic
Lecture: Concept: auto-generating changes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The best way to use a Alembic, I think
0:02 is to let SQLAlchemy at least prebuild
0:06 the various revision changes for you
0:08 and then you can edit them still if you like.
0:11 In order to do that, you have to edit the
0:12 file and you have to set the target metadata
0:17 to the SQLAlchemyBase.metadata.
0:19 And just like before, it is super important
0:21 that that SQLAlchemyBase thing
0:23 sees all of your various models.
0:26 That means you have to do some form of import like this
0:29 from import * or something to that effect
0:33 to make sure that everything is loaded up.
0:36 But adding these two things we'll connect SQLAlchemy
0:38 and Alembic so that Alembic can use your models
0:42 to generate the revisions. And that is way, way sweeter.
0:47 So just simply setting this target metadata
0:50 is all we have to do to connect our ORM models to Alembic.
0:53 And then to make a change, we just add the
0:55 auto generate like we had before but now we say
0:58 auto generate this change.
1:00 It's going to go detect the various changes
1:03 and it's going to create another one of these files.
1:06 We open up that file and now, it's better.
1:08 We have this little section in upgrade.
1:10 Instead of pass, it says, "These were auto generated
1:13 "by Alembic, please review them and if you're happy
1:17 "just leave them, or if you need to, make changes."
1:21 Nothing happens when you auto generate this
1:23 but it generates the file if it can guess
1:25 and then you edit it to do what it needs.
1:28 Finally you just apply it again
1:31 and it's going to apply that auto generated revision change
1:34 rather than the one you had to manually write. And that's Alembic.
1:38 A various nice way to keep evolving your database structure
1:43 along with the evolution of your SQLAlchemy models
1:47 and for the most part making that automatic.