From 6511d6da3899eff8ba59aeb1f51500813f73405b Mon Sep 17 00:00:00 2001 From: "Fabian P. Schmidt" Date: Sun, 31 Jan 2016 02:20:00 +0100 Subject: [PATCH 1/3] alembic: Load configuration from ogn config If environment variable OGN_CONFIG_MODULE is defined, load configuration from this python module, otherwise load "config.default". --- alembic.ini | 31 ++----------------------------- alembic/env.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 34 deletions(-) diff --git a/alembic.ini b/alembic.ini index fc13137..a8cb586 100644 --- a/alembic.ini +++ b/alembic.ini @@ -1,37 +1,10 @@ -# A generic, single database configuration. +# A single database configuration, +# sqlalchemy.url defined in env.py [alembic] # path to migration scripts script_location = 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 - -# version location specification; this defaults -# to alembic/versions. When using multiple version -# directories, initial revisions must be specified with --version-path -# version_locations = %(here)s/bar %(here)s/bat alembic/versions - -# the output encoding used when revision files -# are written from script.py.mako -# output_encoding = utf-8 - -sqlalchemy.url = sqlite:///beacons.db - - # Logging configuration [loggers] keys = root,sqlalchemy,alembic diff --git a/alembic/env.py b/alembic/env.py index 058378b..2f7c635 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,4 +1,6 @@ -from __future__ import with_statement +import os +import importlib + from alembic import context from sqlalchemy import engine_from_config, pool from logging.config import fileConfig @@ -11,16 +13,17 @@ config = context.config # This line sets up loggers basically. fileConfig(config.config_file_name) +# Get database path from ogn config +os.environ.setdefault('OGN_CONFIG_MODULE', 'config.default') +ogn_config = importlib.import_module(os.environ['OGN_CONFIG_MODULE']) + +alembic_config.set_main_option('sqlalchemy.url', ogn_config.SQLALCHEMY_DATABASE_URI) # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata target_metadata = None -# 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(): From 0e66e77b3ee0400be39216379de22aebc7161116 Mon Sep 17 00:00:00 2001 From: "Fabian P. Schmidt" Date: Sun, 31 Jan 2016 02:23:26 +0100 Subject: [PATCH 2/3] alembic: Link metadata to enable autogeneration of migrations --- alembic/env.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 2f7c635..36626d7 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -5,25 +5,23 @@ 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 + +# Provides access to the values within the .ini file in use. +alembic_config = context.config # Interpret the config file for Python logging. # This line sets up loggers basically. -fileConfig(config.config_file_name) +fileConfig(alembic_config.config_file_name) # Get database path from ogn config os.environ.setdefault('OGN_CONFIG_MODULE', 'config.default') ogn_config = importlib.import_module(os.environ['OGN_CONFIG_MODULE']) alembic_config.set_main_option('sqlalchemy.url', ogn_config.SQLALCHEMY_DATABASE_URI) -# add your model's MetaData object here -# for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -target_metadata = None +# Import metadata for autogeneration of migrations +from ogn.model import Base +target_metadata = Base.metadata def run_migrations_offline(): @@ -54,7 +52,7 @@ def run_migrations_online(): """ connectable = engine_from_config( - config.get_section(config.config_ini_section), + alembic_config.get_section(alembic_config.config_ini_section), prefix='sqlalchemy.', poolclass=pool.NullPool) From d8046043ae7df975eadbeca0eccb0c4d5371f932 Mon Sep 17 00:00:00 2001 From: "Fabian P. Schmidt" Date: Sun, 31 Jan 2016 02:25:21 +0100 Subject: [PATCH 3/3] CLI: Add command db.upgrade This command is a shortcut for "alembic upgrade head". --- README.md | 1 + ogn/commands/database.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c302bbb..054545b 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ available commands: import_ddb Import registered devices from the DDB. import_file Import registered devices from a local file. init Initialize the database. + upgrade Upgrade database to the latest version. [gateway] run Run the aprs client. diff --git a/ogn/commands/database.py b/ogn/commands/database.py index c534044..4c2e1e8 100644 --- a/ogn/commands/database.py +++ b/ogn/commands/database.py @@ -6,6 +6,8 @@ from ogn.collect.database import update_devices from manager import Manager manager = Manager() +ALEMBIC_CONFIG_FILE = "alembic.ini" + @manager.command def init(): @@ -15,11 +17,22 @@ def init(): from alembic import command Base.metadata.create_all(engine) - alembic_cfg = Config("alembic.ini") + alembic_cfg = Config(ALEMBIC_CONFIG_FILE) command.stamp(alembic_cfg, "head") print("Done.") +@manager.command +def upgrade(): + """Upgrade database to the latest version.""" + + from alembic.config import Config + from alembic import command + + alembic_cfg = Config(ALEMBIC_CONFIG_FILE) + command.upgrade(alembic_cfg, 'head') + + @manager.command def drop(sure='n'): """Drop all tables."""