Attach the MediaGoblinApp to the engine, and provide a way for models to access
authorChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 3 Dec 2014 19:13:58 +0000 (13:13 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 3 Dec 2014 21:40:58 +0000 (15:40 -0600)
This allows SQLAlchemy models to gain access to app-level configuration
without the need for global variables.

This commit sponsored by Peter Hogg.  Thank you, Peter!

mediagoblin/app.py
mediagoblin/db/base.py
mediagoblin/db/open.py
mediagoblin/init/__init__.py

index 9eb2e8f26d90624641b448e6136a7a93382430b5..b98469625e74cc732814b911e29a71ad64b96c55 100644 (file)
@@ -108,9 +108,9 @@ class MediaGoblinApp(object):
 
         # Set up the database
         if DISABLE_GLOBALS:
-            self.db_manager = setup_database(self.app_config['run_migrations'])
+            self.db_manager = setup_database(self)
         else:
-            self.db = setup_database(self.app_config['run_migrations'])
+            self.db = setup_database(self)
 
         # Quit app if need to run dbupdate
         ## NOTE: This is currently commented out due to session errors..
index c3c3476336ff1857b39093d0358bd3fa3fb25748..6acb0b790372f25a73975a86550e78b64f26a7b2 100644 (file)
@@ -30,6 +30,10 @@ class GMGTableBase(object):
     def _session(self):
         return inspect(self).session
 
+    @property
+    def _app(self):
+        return self._session.bind.app
+
     if not DISABLE_GLOBALS:
         query = Session.query_property()
 
index e85536dee23d2fdf774ef890ca282a4f586ff308..bd629909c84caa8b9b9f0e11766bdffef16cddf9 100644 (file)
@@ -158,9 +158,14 @@ def _sqlite_disable_fk_pragma_on_connect(dbapi_con, con_record):
     dbapi_con.execute('pragma foreign_keys=off')
 
 
-def setup_connection_and_db_from_config(app_config, migrations=False):
+def setup_connection_and_db_from_config(app_config, migrations=False, app=None):
     engine = create_engine(app_config['sql_engine'])
 
+    # @@: Maybe make a weak-ref so an engine can get garbage
+    # collected?  Not that we expect to make a lot of MediaGoblinApp
+    # instances in a single process...
+    engine.app = app
+
     # Enable foreign key checking for sqlite
     if app_config['sql_engine'].startswith('sqlite://'):
         if migrations:
index b6421e06c8ca6e02c740b460ff05b070ec1c1919..38ec126048f8a04e1e26c362cac3155517b47fba 100644 (file)
@@ -60,14 +60,16 @@ def setup_global_and_app_config(config_path):
     return global_config, app_config
 
 
-def setup_database(run_migrations=False):
-    app_config = mg_globals.app_config
-    global_config = mg_globals.global_config
+def setup_database(app):
+    app_config = app.app_config
+    global_config = app.global_config
+    run_migrations = app_config['run_migrations']
 
     # Load all models for media types (plugins, ...)
     load_models(app_config)
     # Set up the database
-    db = setup_connection_and_db_from_config(app_config, run_migrations)
+    db = setup_connection_and_db_from_config(
+        app_config, run_migrations, app=app)
     if run_migrations:
         #Run the migrations to initialize/update the database.
         from mediagoblin.gmg_commands.dbupdate import run_all_migrations