-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added list-templates command and support for custom templates
- Loading branch information
1 parent
fbaaa37
commit 0f9094a
Showing
13 changed files
with
298 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Multi-database configuration for aioflask and alchemical. | ||
Multi-database configuration for aioflask. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Generic single-database configuration with an async engine. | ||
Single-database configuration for aioflask. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Multi-database configuration. | ||
Multi-database configuration for Flask. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Generic single-database configuration. | ||
Single-database configuration for Flask. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Custom template. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Custom template | ||
|
||
[alembic] | ||
# template used to generate migration files | ||
# file_template = %%(rev)s_%%(slug)s | ||
|
||
# set to 'true' to run the environment during | ||
# the 'revision' command, regardless of autogenerate | ||
# revision_environment = false | ||
|
||
|
||
# Logging configuration | ||
[loggers] | ||
keys = root,sqlalchemy,alembic,flask_migrate | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[logger_flask_migrate] | ||
level = INFO | ||
handlers = | ||
qualname = flask_migrate | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Custom template | ||
from __future__ import with_statement | ||
|
||
import logging | ||
from logging.config import fileConfig | ||
|
||
from flask import current_app | ||
|
||
from alembic import context | ||
|
||
# this is the Alembic Config object, which provides | ||
# access to the values within the .ini file in use. | ||
config = context.config | ||
|
||
# Interpret the config file for Python logging. | ||
# This line sets up loggers basically. | ||
fileConfig(config.config_file_name) | ||
logger = logging.getLogger('alembic.env') | ||
|
||
# add your model's MetaData object here | ||
# for 'autogenerate' support | ||
# from myapp import mymodel | ||
# target_metadata = mymodel.Base.metadata | ||
config.set_main_option( | ||
'sqlalchemy.url', | ||
str(current_app.extensions['migrate'].db.get_engine().url).replace( | ||
'%', '%%')) | ||
target_metadata = current_app.extensions['migrate'].db.metadata | ||
|
||
# other values from the config, defined by the needs of env.py, | ||
# can be acquired: | ||
# my_important_option = config.get_main_option("my_important_option") | ||
# ... etc. | ||
|
||
|
||
def run_migrations_offline(): | ||
"""Run migrations in 'offline' mode. | ||
This configures the context with just a URL | ||
and not an Engine, though an Engine is acceptable | ||
here as well. By skipping the Engine creation | ||
we don't even need a DBAPI to be available. | ||
Calls to context.execute() here emit the given string to the | ||
script output. | ||
""" | ||
url = config.get_main_option("sqlalchemy.url") | ||
context.configure( | ||
url=url, target_metadata=target_metadata, literal_binds=True | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
def run_migrations_online(): | ||
"""Run migrations in 'online' mode. | ||
In this scenario we need to create an Engine | ||
and associate a connection with the context. | ||
""" | ||
|
||
# this callback is used to prevent an auto-migration from being generated | ||
# when there are no changes to the schema | ||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html | ||
def process_revision_directives(context, revision, directives): | ||
if getattr(config.cmd_opts, 'autogenerate', False): | ||
script = directives[0] | ||
if script.upgrade_ops.is_empty(): | ||
directives[:] = [] | ||
logger.info('No changes in schema detected.') | ||
|
||
connectable = current_app.extensions['migrate'].db.get_engine() | ||
|
||
with connectable.connect() as connection: | ||
context.configure( | ||
connection=connection, | ||
target_metadata=target_metadata, | ||
process_revision_directives=process_revision_directives, | ||
**current_app.extensions['migrate'].configure_args | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
if context.is_offline_mode(): | ||
run_migrations_offline() | ||
else: | ||
run_migrations_online() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Custom template | ||
"""${message} | ||
|
||
Revision ID: ${up_revision} | ||
Revises: ${down_revision | comma,n} | ||
Create Date: ${create_date} | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
${imports if imports else ""} | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = ${repr(up_revision)} | ||
down_revision = ${repr(down_revision)} | ||
branch_labels = ${repr(branch_labels)} | ||
depends_on = ${repr(depends_on)} | ||
|
||
|
||
def upgrade(): | ||
${upgrades if upgrades else "pass"} | ||
|
||
|
||
def downgrade(): | ||
${downgrades if downgrades else "pass"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import shutil | ||
import unittest | ||
import subprocess | ||
import shlex | ||
|
||
|
||
def run_cmd(app, cmd): | ||
"""Run a command and return a tuple with (stdout, stderr, exit_code)""" | ||
os.environ['FLASK_APP'] = app | ||
process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE) | ||
(stdout, stderr) = process.communicate() | ||
print('\n$ ' + cmd) | ||
print(stdout.decode('utf-8')) | ||
print(stderr.decode('utf-8')) | ||
return stdout, stderr, process.wait() | ||
|
||
|
||
class TestMigrate(unittest.TestCase): | ||
def setUp(self): | ||
os.chdir(os.path.split(os.path.abspath(__file__))[0]) | ||
try: | ||
os.remove('app.db') | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree('migrations') | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree('temp_folder') | ||
except OSError: | ||
pass | ||
|
||
def tearDown(self): | ||
try: | ||
os.remove('app.db') | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree('migrations') | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree('temp_folder') | ||
except OSError: | ||
pass | ||
|
||
def test_alembic_version(self): | ||
from flask_migrate import alembic_version | ||
self.assertEqual(len(alembic_version), 3) | ||
for v in alembic_version: | ||
self.assertTrue(isinstance(v, int)) | ||
|
||
def test_migrate_upgrade(self): | ||
(o, e, s) = run_cmd('app.py', 'flask db init -t ./custom_template') | ||
self.assertTrue(s == 0) | ||
(o, e, s) = run_cmd('app.py', 'flask db migrate') | ||
self.assertTrue(s == 0) | ||
(o, e, s) = run_cmd('app.py', 'flask db upgrade') | ||
self.assertTrue(s == 0) | ||
|
||
from .app import db, User | ||
db.session.add(User(name='test')) | ||
db.session.commit() | ||
|
||
with open('migrations/README', 'rt') as f: | ||
assert f.readline().strip() == 'Custom template.' | ||
with open('migrations/alembic.ini', 'rt') as f: | ||
assert f.readline().strip() == '# Custom template' | ||
with open('migrations/env.py', 'rt') as f: | ||
assert f.readline().strip() == '# Custom template' | ||
with open('migrations/script.py.mako', 'rt') as f: | ||
assert f.readline().strip() == '# Custom template' |