Skip to content

Commit 84326e3

Browse files
author
David Cain
committed
Install Alembic
We'll use Alembic to manage schema and data migrations to our SQLite database. Install and configure the package for use.
1 parent d26ad42 commit 84326e3

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

alembic.ini

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = alembic
6+
7+
# template used to generate migration files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
# max length of characters to apply to the
11+
# "slug" field
12+
#truncate_slug_length = 40
13+
14+
# set to 'true' to run the environment during
15+
# the 'revision' command, regardless of autogenerate
16+
# revision_environment = false
17+
18+
# set to 'true' to allow .pyc and .pyo files without
19+
# a source .py file to be detected as revisions in the
20+
# versions/ directory
21+
# sourceless = false
22+
23+
# version location specification; this defaults
24+
# to alembic/versions. When using multiple version
25+
# directories, initial revisions must be specified with --version-path
26+
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
27+
28+
# the output encoding used when revision files
29+
# are written from script.py.mako
30+
# output_encoding = utf-8
31+
32+
33+
# Logging configuration
34+
[loggers]
35+
keys = root,alembic
36+
37+
[handlers]
38+
keys = console
39+
40+
[formatters]
41+
keys = generic
42+
43+
[logger_root]
44+
level = WARN
45+
handlers = console
46+
qualname =
47+
48+
[logger_alembic]
49+
level = INFO
50+
handlers =
51+
qualname = alembic
52+
53+
[handler_console]
54+
class = StreamHandler
55+
args = (sys.stderr,)
56+
level = NOTSET
57+
formatter = generic
58+
59+
[formatter_generic]
60+
format = %(levelname)-5.5s [%(name)s] %(message)s
61+
datefmt = %H:%M:%S

alembic/env.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from __future__ import with_statement
2+
3+
from logging.config import fileConfig
4+
5+
from alembic import context
6+
from sqlalchemy import engine_from_config, pool
7+
8+
from todo import app
9+
10+
# this is the Alembic Config object, which provides
11+
# access to the values within the .ini file in use.
12+
config = context.config
13+
14+
# Interpret the config file for Python logging.
15+
# This line sets up loggers basically.
16+
fileConfig(config.config_file_name)
17+
18+
# other values from the config, defined by the needs of env.py,
19+
# can be acquired:
20+
# my_important_option = config.get_main_option("my_important_option")
21+
# ... etc.
22+
23+
24+
db_url = 'sqlite:///{}'.format(app.config['DATABASE'])
25+
26+
config.set_main_option('sqlalchemy.url', db_url)
27+
28+
29+
def run_migrations_offline():
30+
"""Run migrations in 'offline' mode.
31+
32+
This configures the context with just a URL
33+
and not an Engine, though an Engine is acceptable
34+
here as well. By skipping the Engine creation
35+
we don't even need a DBAPI to be available.
36+
37+
Calls to context.execute() here emit the given string to the
38+
script output.
39+
40+
"""
41+
context.configure(
42+
url=db_url, literal_binds=True)
43+
44+
with context.begin_transaction():
45+
context.run_migrations()
46+
47+
48+
def run_migrations_online():
49+
"""Run migrations in 'online' mode.
50+
51+
In this scenario we need to create an Engine
52+
and associate a connection with the context.
53+
54+
"""
55+
connectable = engine_from_config(
56+
config.get_section(config.config_ini_section),
57+
prefix='sqlalchemy.',
58+
poolclass=pool.NullPool)
59+
60+
with connectable.connect() as connection:
61+
context.configure(
62+
connection=connection
63+
)
64+
65+
with context.begin_transaction():
66+
context.run_migrations()
67+
68+
69+
if context.is_offline_mode():
70+
run_migrations_offline()
71+
else:
72+
run_migrations_online()

alembic/script.py.mako

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
""" ${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
${imports if imports else ""}
10+
11+
# revision identifiers, used by Alembic.
12+
revision = ${repr(up_revision)}
13+
down_revision = ${repr(down_revision)}
14+
branch_labels = ${repr(branch_labels)}
15+
depends_on = ${repr(depends_on)}
16+
17+
18+
def upgrade():
19+
${upgrades if upgrades else "pass"}
20+
21+
22+
def downgrade():
23+
${downgrades if downgrades else "pass"}

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
include_package_data=True,
88
install_requires=[
99
'flask ~= 0.12',
10+
'alembic',
11+
'sqlalchemy ~= 1.0',
1012
],
1113
setup_requires=[
1214
'pytest-runner',

0 commit comments

Comments
 (0)