Skip to content

Commit

Permalink
refactor alembic implementation
Browse files Browse the repository at this point in the history
refactor alembic implementation to facilitate database upgrades when
using scaffolds
  • Loading branch information
BartSaelen committed Apr 15, 2014
1 parent b7780a2 commit 2a21946
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 4 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include *.txt *.ini *.cfg *.rst
recursive-include atramhasis *.ico *.png *.css *.gif *.jpg *.pt *.txt *.jinja2 *.js *.html *.xml
graft atramhasis/scaffolds
graft atramhasis/alembic
5 changes: 3 additions & 2 deletions alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[alembic]
# path to migration scripts
script_location = alembic
script_location = atramhasis:alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
Expand All @@ -20,8 +20,9 @@ script_location = alembic
# versions/ directory
# sourceless = false

sqlalchemy.url = sqlite:///%(here)s/atramhasis.sqlite
# sqlalchemy.url = sqlite:///%(here)s/atramhasis.sqlite

ini_location = %(here)s/development.ini

# Logging configuration
[loggers]
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions alembic/env.py → atramhasis/alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import with_statement
from configparser import ConfigParser
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
from atramhasis.models import Base
from skosprovider_sqlalchemy.models import Base as SkosBase
from sqlalchemy.schema import MetaData
from os import path

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -25,6 +27,19 @@
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

def load_app_ini(ini_file):
"""Load the settings for the application.ini file."""
ini = ConfigParser()
ini.read_file(open(ini_file))
here = path.abspath(path.dirname(ini_file))
ini.set('app:main', 'here', here)
return ini

app_ini = config.get_main_option('ini_location')
app_config = load_app_ini(app_ini)
sa_url = app_config.get('app:main', 'sqlalchemy.url')
config.set_main_option('sqlalchemy.url', sa_url)

def run_migrations_offline():
"""Run migrations in 'offline' mode.
Expand Down
File renamed without changes.
File renamed without changes.
60 changes: 60 additions & 0 deletions atramhasis/scaffolds/atramhasis_demo/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = atramhasis:alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# sqlalchemy.url = sqlite:///%(here)s/atramhasis.sqlite

ini_location = %(here)s/development.ini

# 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
Binary file not shown.
2 changes: 1 addition & 1 deletion atramhasis/scaffolds/atramhasis_demo/development.ini_tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pyramid.includes =
pyramid_debugtoolbar
pyramid_tm

sqlalchemy.url = sqlite:///%(here)s/atramhasis.sqlite
sqlalchemy.url = sqlite:///%(here)s/{{package}}.sqlite

# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
Expand Down
60 changes: 60 additions & 0 deletions atramhasis/scaffolds/atramhasis_scaffold/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = atramhasis:alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# sqlalchemy.url = sqlite:///%(here)s/atramhasis.sqlite

ini_location = %(here)s/development.ini

# 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
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pyramid.includes =
pyramid_debugtoolbar
pyramid_tm

sqlalchemy.url = sqlite:///%(here)s/atramhasis.sqlite
sqlalchemy.url = sqlite:///%(here)s/{{package}}.sqlite

# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
Expand Down

0 comments on commit 2a21946

Please sign in to comment.