dbupdate updates to use plugin migrations if available
authorChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 9 Mar 2016 21:31:39 +0000 (13:31 -0800)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 26 Mar 2016 18:39:07 +0000 (11:39 -0700)
This makes use of the recently added "build_alembic_config" tool and
removes AlembicMigrationManager.

* mediagoblin/db/migration_tools.py (AlembicMigrationManager): Removed.
* mediagoblin/gmg_commands/dbupdate.py (run_alembic_migrations):
  Adjusted to use recently added build_alembic_config tool.

mediagoblin/db/migration_tools.py
mediagoblin/gmg_commands/dbupdate.py

index c6beba8bd31c48a76a8332dfcc6321a4946df38f..2706c9b73bf360f4f78a9da5dce036e31118ac7b 100644 (file)
@@ -36,52 +36,6 @@ class TableAlreadyExists(Exception):
     pass
 
 
-class AlembicMigrationManager(object):
-
-    def __init__(self, session):
-        root_dir = os.path.abspath(os.path.dirname(os.path.dirname(
-            os.path.dirname(__file__))))
-
-        self.session = session
-        self.engine = self.session.get_bind()
-        alembic_cfg_path = os.path.join(root_dir, 'alembic.ini')
-        self.alembic_cfg = Config(alembic_cfg_path)
-        
-        self.alembic_cfg.attributes["session"] = self.session
-        self.alembic_cfg.set_main_option("sqlalchemy.url", str(self.engine.url))
-
-    def get_current_revision(self):
-        context = MigrationContext.configure(self.session.bind)
-        return context.get_current_revision()
-
-    def upgrade(self, version):
-        return command.upgrade(self.alembic_cfg, version or 'head')
-
-    def downgrade(self, version):
-        if isinstance(version, int) or version is None or version.isdigit():
-            version = 'base'
-        return command.downgrade(self.alembic_cfg, version)
-
-    def stamp(self, revision):
-        return command.stamp(self.alembic_cfg, revision=revision)
-
-    def init_tables(self):
-        Base.metadata.create_all(self.session.bind)
-        # load the Alembic configuration and generate the
-        # version table, "stamping" it with the most recent rev:
-        # XXX: we need to find a better way to detect current installations
-        # using sqlalchemy-migrate because we don't have to create all table
-        # for them
-        command.stamp(self.alembic_cfg, 'head')
-
-    def init_or_migrate(self, version=None):
-        # XXX: we need to call this method when we ditch
-        # sqlalchemy-migrate entirely
-        # if self.get_current_revision() is None:
-        #     self.init_tables()
-        self.upgrade(version)
-
-
 class MigrationManager(object):
     """
     Migration handling tool.
index 6227ad0e737dcd6667698eb95c5655402b848e2a..e9f05e01e657aedfeb435caa977c506dd8aca613 100644 (file)
 import logging
 
 import six
+from alembic import command
 from sqlalchemy.orm import sessionmaker
 
 from mediagoblin.db.open import setup_connection_and_db_from_config
-from mediagoblin.db.migration_tools import MigrationManager, AlembicMigrationManager
+from mediagoblin.db.migration_tools import (
+    MigrationManager, build_alembic_config)
 from mediagoblin.init import setup_global_and_app_config
 from mediagoblin.tools.common import import_component
 
@@ -112,8 +114,15 @@ def gather_database_data(plugins):
 def run_alembic_migrations(db, app_config, global_config):
     """Initialize a database and runs all Alembic migrations."""
     Session = sessionmaker(bind=db.engine)
-    manager = AlembicMigrationManager(Session())
-    manager.init_or_migrate()
+    session = Session()
+    cfg = build_alembic_config(global_config, None, session)
+
+    # XXX: we need to call this method when we ditch
+    # sqlalchemy-migrate entirely
+    # if self.get_current_revision() is None:
+    #     self.init_tables()
+    return command.upgrade(cfg, 'heads')
+
 
 
 def run_dbupdate(app_config, global_config):