Skip to content

Commit

Permalink
Merge pull request #11421 from ggovi/conddbV2-fix-pytools-0
Browse files Browse the repository at this point in the history
Fixes for conddb python tools: tag end_of_validity set to infinity + …
  • Loading branch information
cmsbuild committed Sep 23, 2015
2 parents 2315273 + 60b2572 commit 66ecca2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
42 changes: 22 additions & 20 deletions CondCore/Utilities/python/conddblib.py
Expand Up @@ -176,8 +176,8 @@ class Tag(_Base):
object_type = sqlalchemy.Column(sqlalchemy.String(name_length), nullable=False)
synchronization = sqlalchemy.Column(sqlalchemy.Enum(*tuple(Synchronization)), nullable=False)
description = sqlalchemy.Column(sqlalchemy.String(description_length), nullable=False)
last_validated_time = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
end_of_validity = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
last_validated_time = sqlalchemy.Column(sqlalchemy.BIGINT, nullable=False)
end_of_validity = sqlalchemy.Column(sqlalchemy.BIGINT, nullable=False)
insertion_time = sqlalchemy.Column(sqlalchemy.TIMESTAMP, nullable=False)
modification_time = sqlalchemy.Column(sqlalchemy.TIMESTAMP, nullable=False)

Expand All @@ -188,7 +188,7 @@ class IOV(_Base):
__tablename__ = 'IOV'

tag_name = sqlalchemy.Column(sqlalchemy.ForeignKey('TAG.name'), primary_key=True)
since = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
since = sqlalchemy.Column(sqlalchemy.BIGINT, primary_key=True)
insertion_time = sqlalchemy.Column(sqlalchemy.TIMESTAMP, primary_key=True)
payload_hash = sqlalchemy.Column(sqlalchemy.ForeignKey('PAYLOAD.hash'), nullable=False)

Expand All @@ -211,7 +211,7 @@ class GlobalTag(_Base):
__tablename__ = 'GLOBAL_TAG'

name = sqlalchemy.Column(sqlalchemy.String(name_length), primary_key=True)
validity = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
validity = sqlalchemy.Column(sqlalchemy.BIGINT, nullable=False)
description = sqlalchemy.Column(sqlalchemy.String(description_length), nullable=False)
release = sqlalchemy.Column(sqlalchemy.String(name_length), nullable=False)
insertion_time = sqlalchemy.Column(sqlalchemy.TIMESTAMP, nullable=False)
Expand Down Expand Up @@ -306,7 +306,6 @@ def is_official(self):
def is_valid(self):
'''Tests whether the current DB looks like a valid CMS Conditions one.
'''

engine_connection = self.engine.connect()
ret = all([self.engine.dialect.has_table(engine_connection, table.__tablename__) for table in [Tag, IOV, Payload, GlobalTag, GlobalTagMap]])
engine_connection.close()
Expand Down Expand Up @@ -346,18 +345,7 @@ def _getCMSOracleSQLAlchemyConnectionString(database, schema = 'cms_conditions')


# Entry point
def connect(database='pro', init=False, verbose=0):
'''Returns a Connection instance to the CMS Condition DB.
See database_help for the description of the database parameter.
The verbosity level is as follows:
0 = No output (default).
1 = SQL statements issued, including their parameters.
2 = In addition, results of the queries (all rows and the column headers).
'''

def make_url(database='pro'):
# Lazy in order to avoid calls to cmsGetFnConnect
mapping = {
'pro': lambda: _getCMSFrontierSQLAlchemyConnectionString('PromptProd'),
Expand All @@ -383,11 +371,25 @@ def connect(database='pro', init=False, verbose=0):

try:
url = sqlalchemy.engine.url.make_url(database)
if url.drivername == 'oracle' and url.password is None:
import getpass
url.password = getpass.getpass('Password for %s: ' % str(url))
except sqlalchemy.exc.ArgumentError:
url = sqlalchemy.engine.url.make_url('sqlite:///%s' % database)
return url

def connect(url, init=False, verbose=0):
'''Returns a Connection instance to the CMS Condition DB.
See database_help for the description of the database parameter.
The verbosity level is as follows:
0 = No output (default).
1 = SQL statements issued, including their parameters.
2 = In addition, results of the queries (all rows and the column headers).
'''

if url.drivername == 'oracle' and url.password is None:
import getpass
url.password = getpass.getpass('Password for %s: ' % str(url))

if verbose >= 1:
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
Expand Down
52 changes: 32 additions & 20 deletions CondCore/Utilities/scripts/conddb
Expand Up @@ -240,8 +240,16 @@ def _check_same_object(args):


def _connect(db, init, verbose, read_only, force):
logging.debug('Connecting to %s ...', db)
connection = conddb.connect(db, init=init, verbose=0 if verbose is None else verbose - 1)
url = conddb.make_url( db )
pretty_url = url
if url.drivername == 'oracle+frontier':
ws = url.host.rsplit('%2F')
if ws is not None:
pretty_url = 'frontier://%s/%s' %(ws[-1],url.database)
connTo = '%s [%s]' %(db,pretty_url)
logging.info('Connecting to %s', connTo)
logging.debug('DB url: %s',url)
connection = conddb.connect(url, init=init, verbose=0 if verbose is None else verbose - 1)

if not read_only:
if connection.is_read_only:
Expand All @@ -252,29 +260,32 @@ def _connect(db, init, verbose, read_only, force):
logging.warning('You are going to edit an official database. If you are not one of the Offline DB experts but have access to the password for other reasons, please stop now.')
else:
raise Exception('Editing official databases is forbidden. Use the official DropBox to upload conditions. If you need a special intervention on the database, see the contact help: %s' % conddb.contact_help)
# for sqlite we trigger the implicit schema creation
if init and url.drivername == 'sqlite':
connection.init()
if not connection.is_valid():
raise Exception('No valid schema found in the database: %s' %url)

return connection


def connect(args, init=False, read_only=True):
args.force = args.force if 'force' in dir(args) else False

if 'destdb' in args:
if args.destdb is None or args.db == args.destdb:
if args.destdb is None:
args.destdb = args.db
connection = _connect(args.db, init, args.verbose, read_only, args.force)
return connection, connection

# read_only refers to the destination database only
# (the source is always read_only) -- see copy() command
if os.path.exists(args.destdb):
return _connect(args.db, init, args.verbose, True, args.force), _connect(args.destdb, init, args.verbose, read_only, args.force)
else: # dest DB not yet there, needs init ...
conn1 = _connect(args.db, init, args.verbose, True, args.force)
conn2 = _connect(args.destdb, True, args.verbose, False, args.force)
conn2.init()
return conn1, conn2
if args.db == args.destdb:
conn1 = _connect(args.destdb, init, args.verbose, read_only, args.force)
return conn1, conn1
conn1 = _connect( args.db, init, args.verbose, True, args.force)
conn2url = conddb.make_url(args.destdb)
if conn2url.drivername == 'sqlite' and not os.path.exists(args.destdb):
init = True
conn2 = _connect(conn2url, init, args.verbose, False, args.force)
return conn1, conn2

return _connect(args.db, init, args.verbose, read_only, args.force)
return _connect( args.db, init, args.verbose, read_only, args.force)


def str_db_object(db, name):
Expand Down Expand Up @@ -901,10 +912,11 @@ def _copy_tag(args, session1, session2, first, second, fromIOV=None, toIOV=None,
# Copy the tag
tag = _rawdict(session1.query(conddb.Tag).get(first))
tag['name'] = second
tag['end_of_validity'] = 0 # XXX: SQLite does not work with long ints...
tag['end_of_validity'] = -1

if not timeMap and not fromIOV and not toIOV:
raise Exception("_copy_tag> One of timeMap, fromIOV, toIOV needs to be given ...")
if fromIOV is None:
fromIOV = 1


if timeMap and not fromIOV and not toIOV:
fromIOV = timeMap['start'][ tag['time_type'].lower().strip() ]
Expand Down Expand Up @@ -967,7 +979,7 @@ def _copy_tag(args, session1, session2, first, second, fromIOV=None, toIOV=None,
# Note that we need to replace it for every insertion time (since
# the primary key is (since, insertion_time).
if prev_iov is not None and iov['since'] == prev_iov:
iov['since'] = getattr(args, 'from')
iov['since'] = fromIOV
first_iov = False

session2.add(conddb.IOV(**iov))
Expand Down

0 comments on commit 66ecca2

Please sign in to comment.