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/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..36626d7 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,26 +1,27 @@ -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 -# 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) -# add your model's MetaData object here -# for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -target_metadata = None +# Get database path from ogn config +os.environ.setdefault('OGN_CONFIG_MODULE', 'config.default') +ogn_config = importlib.import_module(os.environ['OGN_CONFIG_MODULE']) -# 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. +alembic_config.set_main_option('sqlalchemy.url', ogn_config.SQLALCHEMY_DATABASE_URI) + +# Import metadata for autogeneration of migrations +from ogn.model import Base +target_metadata = Base.metadata def run_migrations_offline(): @@ -51,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) 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."""