diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2016-03-05 17:37:58 -0800 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2016-03-26 11:39:07 -0700 |
commit | 6e9041aa4bab0761c3772d44a3b8a5a5005261e2 (patch) | |
tree | 4aa81566dae15a56232a74639b480d9f9e0d90bb /mediagoblin/db/migration_tools.py | |
parent | 3f08f780f6698622dba0e8de74ece9194fdb7726 (diff) | |
download | mediagoblin-6e9041aa4bab0761c3772d44a3b8a5a5005261e2.tar.lz mediagoblin-6e9041aa4bab0761c3772d44a3b8a5a5005261e2.tar.xz mediagoblin-6e9041aa4bab0761c3772d44a3b8a5a5005261e2.zip |
Add build_alembic_config, use it to add plugin migrations to alembic config
Diffstat (limited to 'mediagoblin/db/migration_tools.py')
-rw-r--r-- | mediagoblin/db/migration_tools.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/mediagoblin/db/migration_tools.py b/mediagoblin/db/migration_tools.py index bd242894..c6beba8b 100644 --- a/mediagoblin/db/migration_tools.py +++ b/mediagoblin/db/migration_tools.py @@ -18,6 +18,7 @@ from __future__ import unicode_literals import logging import os +import pkg_resources from alembic import command from alembic.config import Config @@ -401,3 +402,36 @@ def model_iteration_hack(db, query): return db.execute(query) +def build_alembic_config(global_config, cmd_options, session): + """ + Build up a config that the alembic tooling can use based on our + configuration. Initialize the database session appropriately + as well. + """ + root_dir = os.path.abspath(os.path.dirname(os.path.dirname( + os.path.dirname(__file__)))) + alembic_cfg_path = os.path.join(root_dir, 'alembic.ini') + cfg = Config(alembic_cfg_path, + cmd_opts=cmd_options) + cfg.attributes["session"] = session + + version_locations = [ + pkg_resources.resource_filename( + "mediagoblin.db", os.path.join("migrations", "versions")), + ] + + cfg.set_main_option("sqlalchemy.url", str(session.get_bind().url)) + + for plugin in global_config.get("plugins", []): + plugin_migrations = pkg_resources.resource_filename( + plugin, "migrations") + is_migrations_dir = (os.path.exists(plugin_migrations) and + os.path.isdir(plugin_migrations)) + if is_migrations_dir: + version_locations.append(plugin_migrations) + + cfg.set_main_option( + "version_locations", + " ".join(version_locations)) + + return cfg |