FIXED SQL MIGRATION #2
[mediagoblin.git] / mediagoblin / db / sql / open.py
CommitLineData
fbad3a9f 1# GNU MediaGoblin -- federated, autonomous media hosting
7f4ebeed 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
fbad3a9f
E
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
7b194a79 18from sqlalchemy import create_engine
193e0f53 19import logging
7b194a79 20
39fd817a 21from mediagoblin.db.sql.base import Base, Session
7b194a79 22
b8295953
E
23_log = logging.getLogger(__name__)
24
7b194a79
E
25
26class DatabaseMaster(object):
27 def __init__(self, engine):
28 self.engine = engine
29
fbad3a9f 30 for k, v in Base._decl_class_registry.iteritems():
7b194a79
E
31 setattr(self, k, v)
32
33 def commit(self):
34 Session.commit()
35
36 def save(self, obj):
37 Session.add(obj)
38 Session.flush()
39
e824570a
E
40 def check_session_clean(self):
41 for dummy in Session():
42 _log.warn("STRANGE: There are elements in the sql session. "
43 "Please report this and help us track this down.")
44 break
45
7b194a79 46 def reset_after_request(self):
2bc8ff0d 47 Session.rollback()
7b194a79
E
48 Session.remove()
49
50
b8295953
E
51def load_models(app_config):
52 import mediagoblin.db.sql.models
53
e136ad3e 54 if True:
b8295953
E
55 for media_type in app_config['media_types']:
56 _log.debug("Loading %s.models", media_type)
57 __import__(media_type + ".models")
58
59
7b194a79 60def setup_connection_and_db_from_config(app_config):
193e0f53 61 engine = create_engine(app_config['sql_engine'])
df7c4058 62 # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
7b194a79
E
63 Session.configure(bind=engine)
64
65 return "dummy conn", DatabaseMaster(engine)
415077a7
E
66
67
68def check_db_migrations_current(db):
69 pass