# 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..
def _session(self):
return inspect(self).session
+ @property
+ def _app(self):
+ return self._session.bind.app
+
if not DISABLE_GLOBALS:
query = Session.query_property()
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:
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