2 import sqlalchemy.ext.declarative
5 import CondCore.Utilities.credentials
as auth
7 prod_db_service = [
'cms_orcon_prod',
'cms_orcon_prod/cms_cond_general_w']
8 dev_db_service = [
'cms_orcoff_prep',
'cms_orcoff_prep/cms_test_conditions']
9 schema_dict = {
'cms_orcon_prod':
'cms_cond_o2o',
'cms_orcoff_prep':
'cms_cond_strip'}
10 sqlalchemy_tpl =
'oracle://%s:%s@%s'
11 coral_tpl =
'oracle://%s/%s'
12 private_db =
'sqlite:///post_o2o.db'
14 _Base = sqlalchemy.ext.declarative.declarative_base()
18 name = re.sub(
'Def$',
'', base_class.__name__)
20 members[
'__tablename__'] = base_class.__tablename__
21 members[
'__table_args__'] =
None
24 members[
'__table_args__'] = {
'schema' : schema}
25 dbType = type(name, (_Base, base_class), members)
39 raise RuntimeError(
'Option db(=%s) is not in the supported database list: [prod, dev, private]' % db)
41 logging.info(
'Connecting to %s database' % self.
db_service[0]
if self.
db_service else private_db)
50 if force_schema
and self.
schema:
52 username, _, pwd = auth.get_credentials( authEntry, self.
authPath)
53 url = sqlalchemy_tpl % (username, pwd, self.
db_service[0])
58 if not self.
engine.has_table(table_def.__tablename__, self.
schema):
59 logging.info(
'Creating table %s on %s' % (table_def.__tablename__,
62 table_class.__table__.create(bind=self.
engine)
63 self.
session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker(bind=self.
engine))
66 engine = sqlalchemy.create_engine(url)
if url
else self.
engine
67 session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker(bind=engine))