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 is to let SQLAlchemy at least prebuild the various revision changes for you
0:09 and then you can edit them still if you like. In order to do that, you have to edit the file and you have to set the target metadata
0:18 to the SQLAlchemyBase.metadata. And just like before, it is super important that that SQLAlchemyBase thing sees all of your various models.
0:27 That means you have to do some form of import like this from import * or something to that effect to make sure that everything is loaded up.
0:37 But adding these two things we'll connect SQLAlchemy and Alembic so that Alembic can use your models
0:43 to generate the revisions. And that is way, way sweeter. So just simply setting this target metadata
0:51 is all we have to do to connect our ORM models to Alembic. And then to make a change, we just add the auto generate like we had before but now we say
0:59 auto generate this change. It's going to go detect the various changes and it's going to create another one of these files.
1:07 We open up that file and now, it's better. We have this little section in upgrade. Instead of pass, it says, "These were auto generated
1:14 "by Alembic, please review them and if you're happy "just leave them, or if you need to, make changes." Nothing happens when you auto generate this
1:24 but it generates the file if it can guess and then you edit it to do what it needs. Finally you just apply it again
1:32 and it's going to apply that auto generated revision change rather than the one you had to manually write. And that's Alembic.
1:39 A various nice way to keep evolving your database structure along with the evolution of your SQLAlchemy models
1:48 and for the most part making that automatic.

Talk Python's Mastodon Michael Kennedy's Mastodon