Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 88d376e

Browse files
committed
Add Flask-Migrate support
Add Flask-Migrate to manage database schema changes
1 parent 71e866f commit 88d376e

File tree

11 files changed

+220
-13
lines changed

11 files changed

+220
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ development machine so that it pretends to be
3030

3131
4. Create the database tables:
3232

33-
$ python manage.py create_tables
33+
$ python manage.py db upgrade
3434

3535
5. Start the server:
3636

manage.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
from flask.ext.migrate import MigrateCommand
12
from flask.ext.script import Manager
2-
from studygroup.application import create_app, db
33

4+
from studygroup.application import create_app
45

56
manager = Manager(create_app)
7+
manager.add_command('db', MigrateCommand)
68

7-
@manager.command
8-
def create_tables():
9-
db.create_all()
109

1110
if __name__ == "__main__":
1211
manager.run()

migrations/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

migrations/alembic.ini

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
# file_template = %%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
11+
12+
# Logging configuration
13+
[loggers]
14+
keys = root,sqlalchemy,alembic
15+
16+
[handlers]
17+
keys = console
18+
19+
[formatters]
20+
keys = generic
21+
22+
[logger_root]
23+
level = WARN
24+
handlers = console
25+
qualname =
26+
27+
[logger_sqlalchemy]
28+
level = WARN
29+
handlers =
30+
qualname = sqlalchemy.engine
31+
32+
[logger_alembic]
33+
level = INFO
34+
handlers =
35+
qualname = alembic
36+
37+
[handler_console]
38+
class = StreamHandler
39+
args = (sys.stderr,)
40+
level = NOTSET
41+
formatter = generic
42+
43+
[formatter_generic]
44+
format = %(levelname)-5.5s [%(name)s] %(message)s
45+
datefmt = %H:%M:%S

migrations/env.py

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

migrations/script.py.mako

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

migrations/versions/9975799c2b8_.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Initial schema
2+
3+
Revision ID: 9975799c2b8
4+
Revises: None
5+
Create Date: 2014-04-17 21:20:28.470577
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = '9975799c2b8'
11+
down_revision = None
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
16+
17+
def upgrade():
18+
### commands auto generated by Alembic - please adjust! ###
19+
op.create_table('user',
20+
sa.Column('id', sa.Integer(), nullable=False),
21+
sa.Column('meetup_member_id', sa.String(), nullable=True),
22+
sa.Column('full_name', sa.String(), nullable=True),
23+
sa.Column('is_admin', sa.Boolean(), nullable=False),
24+
sa.PrimaryKeyConstraint('id'),
25+
sa.UniqueConstraint('meetup_member_id')
26+
)
27+
op.create_table('group',
28+
sa.Column('id', sa.Integer(), nullable=False),
29+
sa.Column('name', sa.String(), nullable=True),
30+
sa.Column('description', sa.String(), nullable=True),
31+
sa.Column('max_members', sa.Integer(), nullable=False),
32+
sa.PrimaryKeyConstraint('id'),
33+
sa.UniqueConstraint('name')
34+
)
35+
op.create_table('membership',
36+
sa.Column('id', sa.Integer(), nullable=False),
37+
sa.Column('user_id', sa.Integer(), nullable=True),
38+
sa.Column('group_id', sa.Integer(), nullable=True),
39+
sa.Column('role', sa.Integer(), nullable=False),
40+
sa.ForeignKeyConstraint(['group_id'], ['group.id'], ),
41+
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
42+
sa.PrimaryKeyConstraint('id')
43+
)
44+
### end Alembic commands ###
45+
46+
47+
def downgrade():
48+
### commands auto generated by Alembic - please adjust! ###
49+
op.drop_table('membership')
50+
op.drop_table('group')
51+
op.drop_table('user')
52+
### end Alembic commands ###

requirements.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
Flask==0.10.1
21
Flask-Bootstrap==3.1.0.1
2+
Flask-Migrate==1.2.0
33
Flask-OAuthlib==0.4.2
44
Flask-SQLAlchemy==1.0
55
Flask-Script==0.6.6
66
Flask-WTF==0.9.4
7+
Flask==0.10.1
78
Jinja2==2.7.2
9+
Mako==0.9.1
810
MarkupSafe==0.18
911
SQLAlchemy==0.9.1
1012
WTForms==1.0.5
1113
Werkzeug==0.9.4
14+
alembic==0.6.4
1215
itsdangerous==0.23
1316
oauthlib==0.6.1
1417
psycopg2==2.5.2

studygroup/application.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from flask import Flask
22
from flask_oauthlib.client import OAuth
33
from flask_bootstrap import Bootstrap
4+
from flask.ext.migrate import Migrate
45
from flask.ext.sqlalchemy import SQLAlchemy
56

67
import settings
78

89

910
db = SQLAlchemy()
1011
oauth = OAuth()
12+
migrate = Migrate()
1113

1214
def create_app(debug=True):
1315
from views import studygroup
@@ -22,6 +24,7 @@ def create_app(debug=True):
2224
Bootstrap(app)
2325
db.init_app(app)
2426
oauth.init_app(app)
27+
migrate.init_app(app, db)
2528

2629
return app
2730

tests/test_app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_logged_in(self):
1616
self.login(self.alice_id)
1717
resp = self.client.get('/')
1818
self.assert200(resp)
19-
self.assertIn('Welcome, Alice B. Admin', resp.data)
19+
self.assertIn('Welcome, Alice B.', resp.data)
2020
self.assertNotIn('Sign In Now', resp.data)
2121

2222

tests/tools.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@
22
Tools to help test StudyGroup.
33
"""
44

5-
import unittest
5+
import os.path
66

7-
from flask import session
7+
import flask.ext.migrate
88
from flask.ext.testing import TestCase
99

1010
from studygroup.application import create_app, db
1111
from studygroup.models import User
1212

1313

14+
DB_PATH = 'sqlite:///' + os.path.dirname(__file__) + '/../test.db'
15+
16+
1417
class StudyGroupTestCase(TestCase):
1518
"""
1619
Base class for all StudyGroup tests.
1720
"""
1821
def setUp(self):
19-
db.create_all()
20-
self.alice_id = self.create_user(full_name="Alice B. Admin")
22+
flask.ext.migrate.upgrade()
23+
self.alice_id = self.create_user(full_name='Alice B.')
24+
self.admin_id = self.create_user(full_name='Bob Admin', is_admin=True)
2125

2226
def tearDown(self):
27+
db.session.execute('DROP TABLE alembic_version')
2328
db.session.remove()
2429
db.drop_all()
2530

@@ -30,15 +35,19 @@ def create_app(self):
3035
app = create_app()
3136
app.config['TESTING'] = True
3237
app.config['WTF_CSRF_ENABLED'] = False
33-
# Use an in-memory SQLite db.
34-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
38+
# Use a SQLite db. Migrations will not work on an in-memory db.
39+
app.config['SQLALCHEMY_DATABASE_URI'] = DB_PATH
40+
app.config['SQLALCHEMY_POOL_SIZE'] = None
3541
return app
3642

3743
def create_user(self, **kwargs):
3844
"""
3945
Make a new user, and return the user id.
4046
"""
4147
user = User(**kwargs)
48+
import sqlalchemy.schema
49+
metadata = sqlalchemy.schema.MetaData()
50+
metadata.reflect(db.session.bind)
4251
db.session.add(user)
4352
db.session.commit()
4453
return user.id

0 commit comments

Comments
 (0)