#100DaysOfWeb in Python Transcripts
Chapter: Days 61-64: Database migrations
Lecture: Where is the DB version stored?

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw the we can go to the terminal
0:02 and we can ask Alembic for the current.
0:05 And when you run that it says, "this is the head."
0:07 We could actually run a downgrade on this
0:09 to get to another one
0:10 then this little head part would be gone
0:12 but this is the latest.
0:13 So how does it know what version the database has?
0:16 How does it know whether that's the head or not?
0:18 All these things are interesting questions.
0:20 Well, the way it knows which is the head
0:21 is it parses all the files here
0:23 and figures out how to order them
0:25 based on their dependencies
0:27 so it knows what one is the last one there.
0:28 But how does it know what number it's on?
0:32 So we're going to look at that real quick.
0:33 So if we go over to our database here
0:35 and we go to our DB file, and we open this up
0:39 Oh, we already have it open.
0:40 So we go over here to the schemas.
0:42 Make sure you refresh this.
0:43 And now we have a new column called alembic_version
0:46 and it has just the version number.
0:48 It's super, super simple.
0:49 So we can open this in a console
0:51 and say select * from alembic_version.
0:54 We run this, we get a very impressive
0:57 one row, one column in this table
0:59 but it is not surprising that it is this 99977
1:04 which we saw right there. It's that version.
1:07 So there's simply a table in our database
1:10 that has a single row in a single column
1:13 that is a varchar32 which is this.
1:16 So we just, that is the stored version of the database
1:19 and then these are all the possible versions
1:21 and Alembic syncs those too
1:22 and figures out what changes it should apply
1:25 what changes already apply.
1:26 It's important you don't make changes to the database
1:29 directly, but that you always use Alembic
1:32 because it doesn't completely analyze the database
1:35 I don't think.
1:36 I think it just looks at a number and go
1:37 okay we're going to apply those changes
1:39 that are tracked in Alembic.
1:41 So that's where this is stored and how you can find it.