Skip to content

Commit

Permalink
Merge pull request #16655 from ggovi/conddb-utilities-conddb-tool-fix…
Browse files Browse the repository at this point in the history
…1.1-81X

Fix for GLOBAL_TAG table names in sqlite files
  • Loading branch information
cmsbuild committed Nov 22, 2016
2 parents 47456da + 77e23e5 commit 29b3e45
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions CondCore/Utilities/python/conddblib.py
Expand Up @@ -273,6 +273,8 @@ class IOV:
'payload_hash':(DbRef(Payload,'hash'),_Col.pk) }


# the string 'GLOBAL' being a keyword in sqlalchemy ( upper case ), when used in the model cause the two GT tables to be unreadable ( bug )
# the only choice is to use lower case names, and rename the tables in sqlite after creation!!
class GlobalTag:
__tablename__ = 'global_tag'
columns = { 'name':(sqlalchemy.String(name_length),_Col.pk),
Expand All @@ -282,7 +284,6 @@ class GlobalTag:
'insertion_time':(sqlalchemy.TIMESTAMP,_Col.notNull),
'snapshot_time':(sqlalchemy.TIMESTAMP,_Col.notNull) }


class GlobalTagMap:
__tablename__ = 'global_tag_map'
columns = { 'global_tag_name':(DbRef(GlobalTag,'name'),_Col.pk),
Expand Down Expand Up @@ -348,6 +349,7 @@ def __init__(self, url, init=False):
'cms_orcon_prod',
'cmsintr_lb',
}
self._url = url
self._backendName = ('sqlite' if self._is_sqlite else 'oracle' )
self._schemaName = ( None if self._is_sqlite else schema_name )
logging.debug(' ... using db "%s", schema "%s"' % (url, self._schemaName) )
Expand Down Expand Up @@ -429,7 +431,19 @@ def init(self, drop=False):
self.get_dbtype(GlobalTag).__table__.create(bind = self.engine)
self.get_dbtype(GlobalTagMap).__table__.create(bind = self.engine)
#self.metadata.create_all(self.engine)

if self.is_sqlite:
# horrible hack, but no choice because of the sqlalchemy bug ( see comment in the model)
import sqlite3
import string
conn = sqlite3.connect( self._url.database )
c = conn.cursor()
stmt = string.Template('ALTER TABLE $before RENAME TO $after')
c.execute( stmt.substitute( before=GlobalTag.__tablename__, after='TMP0' ) )
c.execute( stmt.substitute( before='TMP0', after=GlobalTag.__tablename__.upper() ) )
c.execute( stmt.substitute( before=GlobalTagMap.__tablename__, after='TMP1' ) )
c.execute( stmt.substitute( before='TMP1', after=GlobalTagMap.__tablename__.upper() ) )
conn.commit()
conn.close()
# TODO: Create indexes
#logger.debug('Creating indexes...')

Expand Down

0 comments on commit 29b3e45

Please sign in to comment.