Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Database migrations with Alembic
Lecture: Concept: Getting started
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's take a moment and review
0:01
the core concepts of using Alembic.
0:03
And I think the place to get started is getting started.
0:06
The way we're going to get started is
0:07
we have to make sure we have Alembic.
0:09
We simply get that by pip installing Alembic.
0:12
Make sure this is the virtual environment
0:14
that you want it to be in, okay?
0:16
So that works great, let's go into install.
0:18
Then we need to initialize the project structure just once.
0:21
So we say alembic init alembic.
0:24
First one is the command, last one is the folder.
0:26
And then it's going to go through and create all the structure.
0:28
And says, don't forget
0:30
you have to set your connection string
0:31
in alembic.ini or this isn't going to work.
0:34
Afterwards, you have a project
0:36
that looks a little bit different.
0:38
You've got an alembic folder, and here we have versions.
0:42
It's going to be initially empty but as you apply
0:44
as you create revisions
0:46
they're going to be stored in here
0:47
and chained together as we saw.
0:49
Some scripts that are used to
0:52
generate the SQL and run Alembic.
0:55
So environment does things like
0:57
tells Alembic what your SQLalchemy models are
1:00
the scripts stop.py and .mako is actually
1:03
going to generate the SQL structure
1:06
that's going to be sent to the database.
1:08
Things like that.
1:09
Then finally we have our alembic.ini
1:11
that configures how Alembic runs.
1:14
In order to use Alembic you have to configure it.
1:17
So there's a whole bunch of settings
1:19
in Alembic.ini but the one that you
1:20
probably care about first is sqlalchemy.url
1:23
and then you just set it to whatever
1:25
your connection string is.
1:27
We're using SQLite so it's sqlite/// the name of the file
1:31
and were able to use a relative path.
1:33
I think this may only work if you run
1:35
Alembic from the right location, so do that.
1:39
Turns out that's pretty easy from PyCharm
1:41
cause the terminal always opens in the right place.