added openid docs
[mediagoblin.git] / mediagoblin / gmg_commands / dbupdate.py
index 52819c6dbaef24c8cc1db0eda9b56977b37f5684..22ad426c1046dd0fbe8bfb9518490870c4335326 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from mediagoblin.db.sql.open import setup_connection_and_db_from_config
-from mediagoblin.db.sql.util import (
-    MigrationManager, assure_migrations_table_setup)
+import logging
+
+from sqlalchemy.orm import sessionmaker
+
+from mediagoblin.db.open import setup_connection_and_db_from_config
+from mediagoblin.db.migration_tools import MigrationManager
 from mediagoblin.init import setup_global_and_app_config
 from mediagoblin.tools.common import import_component
 
+_log = logging.getLogger(__name__)
+logging.basicConfig()
+_log.setLevel(logging.DEBUG)
+
+def dbupdate_parse_setup(subparser):
+    pass
+
 
 class DatabaseData(object):
     def __init__(self, name, models, migrations):
@@ -27,12 +37,12 @@ class DatabaseData(object):
         self.models = models
         self.migrations = migrations
 
-    def make_migration_manager(self, db):
+    def make_migration_manager(self, session):
         return MigrationManager(
-            self.name, self.models, self.migrations, db)
+            self.name, self.models, self.migrations, session)
 
 
-def gather_database_data(self, media_types):
+def gather_database_data(media_types, plugins):
     """
     Gather all database data relevant to the extensions we have
     installed so we can do migrations and table initialization.
@@ -42,12 +52,12 @@ def gather_database_data(self, media_types):
     managed_dbdata = []
 
     # Add main first
-    from mediagoblin.db.sql.models import MODELS as MAIN_MODELS
-    from mediagoblin.db.sql.migrations import MIGRATIONS as MAIN_MIGRATIONS
+    from mediagoblin.db.models import MODELS as MAIN_MODELS
+    from mediagoblin.db.migrations import MIGRATIONS as MAIN_MIGRATIONS
 
     managed_dbdata.append(
         DatabaseData(
-            '__main__', MAIN_MODELS, MAIN_MIGRATIONS))
+            u'__main__', MAIN_MODELS, MAIN_MIGRATIONS))
 
     # Then get all registered media managers (eventually, plugins)
     for media_type in media_types:
@@ -56,29 +66,79 @@ def gather_database_data(self, media_types):
         managed_dbdata.append(
             DatabaseData(media_type, models, migrations))
 
+    for plugin in plugins:
+        try:
+            models = import_component('{0}.models:MODELS'.format(plugin))
+        except ImportError as exc:
+            _log.debug('No models found for {0}: {1}'.format(
+                plugin,
+                exc))
+
+            models = []
+        except AttributeError as exc:
+            _log.warning('Could not find MODELS in {0}.models, have you \
+forgotten to add it? ({1})'.format(plugin, exc))
+            models = []
+
+        try:
+            migrations = import_component('{0}.migrations:MIGRATIONS'.format(
+                plugin))
+        except ImportError as exc:
+            _log.debug('No migrations found for {0}: {1}'.format(
+                plugin,
+                exc))
+
+            migrations = {}
+        except AttributeError as exc:
+            _log.debug('Cloud not find MIGRATIONS in {0}.migrations, have you \
+forgotten to add it? ({1})'.format(plugin, exc))
+            migrations = {}
+
+        if models:
+            managed_dbdata.append(
+                    DatabaseData(plugin, models, migrations))
+
+
     return managed_dbdata
 
 
-def dbupdate(args):
+def run_dbupdate(app_config, global_config):
     """
     Initialize or migrate the database as specified by the config file.
 
     Will also initialize or migrate all extensions (media types, and
     in the future, plugins)
     """
-    globa_config, app_config = setup_global_and_app_config(args.conf_file)
-
-    # Gather information from all media managers / projects
-    dbdatas = gather_database_data(app_config['media_types'])
 
     # Set up the database
-    connection, db = setup_connection_and_db_from_config(app_config)
+    db = setup_connection_and_db_from_config(app_config, migrations=True)
+    #Run the migrations
+    run_all_migrations(db, app_config, global_config)
 
-    # If migrations table not made, make it!
-    assure_migrations_table_setup(db)
+
+def run_all_migrations(db, app_config, global_config):
+    """
+    Initializes or migrates a database that already has a 
+    connection setup and also initializes or migrates all
+    extensions based on the config files.
+
+    It can be used to initialize an in-memory database for
+    testing.
+    """
+    # Gather information from all media managers / projects
+    dbdatas = gather_database_data(
+            app_config['media_types'],
+            global_config.get('plugins', {}).keys())
+
+    Session = sessionmaker(bind=db.engine)
 
     # Setup media managers for all dbdata, run init/migrate and print info
     # For each component, create/migrate tables
     for dbdata in dbdatas:
-        migration_manager = dbdata.make_migration_manager(db)
+        migration_manager = dbdata.make_migration_manager(Session())
         migration_manager.init_or_migrate()
+
+
+def dbupdate(args):
+    global_config, app_config = setup_global_and_app_config(args.conf_file)
+    run_dbupdate(app_config, global_config)