Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Database migrations with Alembic
Lecture: Concept: Alembic - the manual version

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've seen there are two ways to make Alembic do changes to our database. We have the SQLAlchemy class-based changes
0:09 and just straight-up manual changes. I didn't show you the manual changes but I did tell you about them. I'll show them to you now so you can see
0:16 what the whole set of steps are and appreciate a little bit better what the SQLAlchemy version, the autogen version is doing for ya.
0:23 If we're going to make change we're going to record a revision the main way we're going to get started by creating the pieces
0:29 by running alembic revision and then give it a comment -m add keywords to the column. This is important. It's going to show up when you apply it
0:37 it's also going to show up in the file name that stores it. So, we do that and it'll generate some hash
0:42 and then a file name friendly version of your comment. Done. Then we go and open that file and we see it looks like this.
0:49 It has a revision, the first one will have no down revision no relationship but as you build upon them of course
0:56 they do and then it has an empty upgrade and an empty pass. So, your main job in the manual version is to actually implement those things.
1:05 That may or may not be something you want to do. It's not too hard. Here's another one we can do. We can come and say we'd like to do the op.add_column
1:13 to the packages table and just pass in a column like you would in your SQLAlchemy class, right? sa.Column, keyword string nullable and so on.
1:22 Then downgrade of course is drop column. This is pretty straight forward but it can be a little bit tricky.
1:28 One thing you may want to do is double check that this column does not exist before you try to add it. If it does, this migration will fail
1:35 and everything that depends upon it will subsequently fail as well and that won't be great so I'm going to leave for dropping it.
1:41 I talked a little bit earlier about the Alembic helpers. There's this function that I got called table_has_column and give it a table
1:50 and a column and it'll say yes, I have it. So, it'll go and do some funky introspection on the table and tell you whether or not that is there.
1:58 The way you import it is a little weird because where we run the code from and where it lives is not exactly the same place
2:06 so the way Alembic runs these it's a little bit funky so you got to do this weird import to get it but once you do, then you can ask questions
2:15 like if the table doesn't have a keyword column then add it, things like that so this may be helpful to you. We could also ask Alembic current.
2:24 By default it'll just say nothing which means there's no version although it does when you run that
2:29 create the table, just there's no version put into it. And then we say Upgrade and it's going to apply whatever upgrade
2:36 so in this case, we ran it on a database that had revision 078 and we upgraded a 689 and that was adding the keywords column.
2:45 Now if you ask Alembic.current it'll say we're on that 689 version that we just upgraded to and by the way, that is the latest. And of course as we saw
2:54 this version is stored in your database so you have an Alembic.version and if you go do a query on it you'll see 689 the one column with one row
3:01 which is just that string super easy to check you might just open it up in your tools and go what version is this in
3:08 even if you can't run Alembic directly on it.


Talk Python's Mastodon Michael Kennedy's Mastodon