Skip to content

Commit

Permalink
add initial alembic migrations setup
Browse files Browse the repository at this point in the history
  • Loading branch information
lgo committed Jul 29, 2015
1 parent 4ac32f8 commit 6f625ba
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 14 deletions.
2 changes: 1 addition & 1 deletion fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def update():

def migrate():
with cd('/kerbos'):
run('python3 manage.py migrate')
run('python3 manage.py db upgrade')

def reload_application():
with cd('/kerbos'):
Expand Down
17 changes: 11 additions & 6 deletions kerbos/config.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import os


class Config(object):
class DefaultConfig(object):
DEBUG = False
TESTING = False
SECRET_KEY = 'thisisnotthesecretyouarelookingfor'
DEFAULT_DATABASE_URI = "postgresql://localhost/kerbos/"
DEFAULT_DATABASE_URI = "postgresql://postgres@localhost/kerbos"
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', default=DEFAULT_DATABASE_URI)


class ProductionConfig(Config):
class ProductionConfig(DefaultConfig):
DEBUG = False


class StagingConfig(Config):
class StagingConfig(DefaultConfig):
DEVELOPMENT = True
DEBUG = True


class DevelopmentConfig(Config):
class DevelopmentConfig(DefaultConfig):
DEVELOPMENT = True
DEBUG = True


class TestingConfig(Config):
class TestingConfig(DefaultConfig):
TESTING = True


class CircleCIConfig(DefaultConfig):
TESTING = True
DEFAULT_DATABASE_URI = "postgresql://ubuntu@localhost/circle_test"
7 changes: 0 additions & 7 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
import os

from kerbos import get_app
from kerbos.shared import APP as app, DB as db
Expand All @@ -15,12 +14,6 @@ def run():
app = get_app(config='Development')
get_app()

@manager.command
def migrate():
app = get_app(config='Production')
from kerbos.shared import DB as db
app.run()

@manager.command
def prod():
app = get_app(config='Production')
Expand Down
1 change: 1 addition & 0 deletions migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
45 changes: 45 additions & 0 deletions migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# A generic, single database configuration.

[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

[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

[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
73 changes: 73 additions & 0 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig

# 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)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option('sqlalchemy.url', current_app.config.get('SQLALCHEMY_DATABASE_URI'))
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)

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.
"""
engine = engine_from_config(config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)

connection = engine.connect()
context.configure(connection=connection,
target_metadata=target_metadata,
**current_app.extensions['migrate'].configure_args)

try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()

if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

22 changes: 22 additions & 0 deletions migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision}
Create Date: ${create_date}

"""

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}

0 comments on commit 6f625ba

Please sign in to comment.