Skip to content
Snippets Groups Projects
Commit f32865ad authored by Adam Edward Barton's avatar Adam Edward Barton
Browse files

Merge branch 'master-Oracle2SQLite2' into 'master'

Update on Run 2 MC TriggerDB oracle->sqlite

See merge request atlas/athena!38264
parents 411bac90 53be02e7
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# script to produce a one-to-one copy of the oracle Run 2 MC TriggerDB to sqlite # script to produce a one-to-one copy of the oracle Run 2 MC TriggerDB to sqlite
from TrigConfIO.TriggerConfigAccessBase import ConfigDBLoader from TrigConfIO.TriggerConfigAccessBase import ConfigDBLoader
from copy import deepcopy
import sqlite3 import sqlite3
def parseCmdline(): def parseCmdline():
...@@ -105,25 +105,32 @@ class OracleExporter: ...@@ -105,25 +105,32 @@ class OracleExporter:
self.connection = connection self.connection = connection
self.primaryKeys = None self.primaryKeys = None
self.foreignKeys = None self.foreignKeys = None
self.tables = None self.indexes = None
self.ignoreTablesR2 = [ 'TT_WRITELOCK', 'HLT_SMT_TO_HRE', 'HLT_PARAMETER', 'HLT_RELEASE', 'TRIGGER_LOG', self.allTables = None
'DBCOPY_SOURCE_DATABASE', 'HLT_RULE_SET', 'TEMP', 'HLT_RULE_PARAMETER', 'HLT_RULE_COMPONENT', self.ignoreTablesCreate = [ 'TT_WRITELOCK', 'HLT_SMT_TO_HRE', 'HLT_PARAMETER', 'HLT_RELEASE', 'TRIGGER_LOG',
'HLT_SETUP', 'TT_USERS', 'HLT_RULE', "HLT_HRC_TO_HRP", "HLT_HRE_TO_HRS", 'DBCOPY_SOURCE_DATABASE', 'HLT_RULE_SET', 'TEMP', 'HLT_RULE_PARAMETER', 'HLT_RULE_COMPONENT',
"HLT_HRS_TO_HRU", "HLT_HRU_TO_HRC", "HLT_PRESCALE_SET_ALIAS", "HLT_PRESCALE_SET_COLL", 'HLT_SETUP', 'TT_USERS', 'HLT_RULE', "HLT_HRC_TO_HRP", "HLT_HRE_TO_HRS",
"PRESCALE_SET_ALIAS", "TRIGGER_ALIAS", "L1_PRESCALE_SET_ALIAS", "L1_CALO_SIN_COS", "HLT_HRS_TO_HRU", "HLT_HRU_TO_HRC", "HLT_PRESCALE_SET_ALIAS", "HLT_PRESCALE_SET_COLL",
"L1_CI_TO_CSC", "L1_JET_INPUT", "L1_MUON_THRESHOLD_SET", "L1_CTP_FILES", "L1_CTP_SMX" ] "PRESCALE_SET_ALIAS", "TRIGGER_ALIAS", "L1_PRESCALE_SET_ALIAS", "L1_CALO_SIN_COS",
"L1_CI_TO_CSC", "L1_JET_INPUT", "L1_MUON_THRESHOLD_SET"]
def getTables(self, isRun2MC = True): self.ignoreTablesFill = self.ignoreTablesCreate + [ ]
if self.tables: print("Tables to ignore creation: %r" % len(self.ignoreTablesCreate))
return self.tables print("Tables to ignore filling: %r" % len(self.ignoreTablesFill))
query_ListAllTables = "SELECT table_name FROM all_tables WHERE owner=:SCHEMA"
data = { "SCHEMA" : self.connection.schema }
result = self.executeQuery(query_ListAllTables, data) def getTables(self, exceptTables):
self.tables = [x[0] for x in result] if self.allTables is None:
if isRun2MC: query_ListAllTables = "SELECT table_name FROM all_tables WHERE owner=:SCHEMA"
for x in self.ignoreTablesR2: data = { "SCHEMA" : self.connection.schema }
self.tables.remove(x) result = self.executeQuery(query_ListAllTables, data)
return self.tables self.allTables = [x[0] for x in result]
print("All tables: %i" % len(self.allTables))
tables = deepcopy(self.allTables)
if exceptTables is not None:
for x in exceptTables:
if x in tables:
tables.remove(x)
return tables
def executeQuery(self, query, data = {}): def executeQuery(self, query, data = {}):
cursor = self.connection.cursor() cursor = self.connection.cursor()
...@@ -145,6 +152,22 @@ class OracleExporter: ...@@ -145,6 +152,22 @@ class OracleExporter:
colNames, colTypes = zip(*result) colNames, colTypes = zip(*result)
return colNames, colTypes return colNames, colTypes
def getIndexes(self):
if self.indexes is not None:
return self.indexes
self.indexes = {}
print("retrieving indexes from Oracle")
query_Indexes = """
SELECT table_name, index_name, column_name FROM sys.all_ind_columns WHERE table_owner = :SCHEMA
"""
data = { "SCHEMA" : self.connection.schema }
result = self.executeQuery( query = query_Indexes, data = data )
for table_name, index_name, column_name in result:
if table_name not in self.indexes:
self.indexes[table_name] = {}
self.indexes[table_name][index_name] = column_name
return self.indexes
def getPrimaryKeys(self): def getPrimaryKeys(self):
if self.primaryKeys is not None: if self.primaryKeys is not None:
return self.primaryKeys return self.primaryKeys
...@@ -198,7 +221,7 @@ class OracleExporter: ...@@ -198,7 +221,7 @@ class OracleExporter:
} }
return d[oraType] return d[oraType]
def tableCreationCommand(self, tableName, primaryKeys, foreignKeys): def tableCreationCommand(self, tableName, primaryKeys, foreignKeys, indexes):
colNames, colTypes = self.columnNames(tableName) colNames, colTypes = self.columnNames(tableName)
lines = [] lines = []
for colName, colType in zip(colNames,colTypes): for colName, colType in zip(colNames,colTypes):
...@@ -209,15 +232,18 @@ class OracleExporter: ...@@ -209,15 +232,18 @@ class OracleExporter:
creationCommand = f"CREATE TABLE IF NOT EXISTS {tableName} (\n " creationCommand = f"CREATE TABLE IF NOT EXISTS {tableName} (\n "
creationCommand += ",\n ".join(lines) creationCommand += ",\n ".join(lines)
creationCommand += "\n);\n" creationCommand += "\n);\n"
for index_name in indexes:
creationCommand += "CREATE INDEX %s on %s(%s);\n" % (index_name, tableName, indexes[index_name])
return creationCommand return creationCommand
def extractSchema(self, creationFileName, isRun2MC = True): def extractSchema(self, creationFileName):
fk = self.getForeignKeys() fk = self.getForeignKeys()
pk = self.getPrimaryKeys() pk = self.getPrimaryKeys()
indexes = self.getIndexes()
with open(creationFileName, "w") as fh: with open(creationFileName, "w") as fh:
print("Creating schema file for sqlite: %s" % creationFileName) print("Creating schema file for sqlite: %s" % creationFileName)
for tableName in self.getTables(isRun2MC): for tableName in self.getTables(exceptTables = self.ignoreTablesCreate):
print(self.tableCreationCommand(tableName, pk[tableName], fk[tableName] if tableName in fk else []), file = fh) print(self.tableCreationCommand( tableName, pk[tableName], fk.get(tableName, list()), indexes.get(tableName, list())), file = fh)
def copyTable(self, tableName, sqliteInserter, size): def copyTable(self, tableName, sqliteInserter, size):
# build insert statement # build insert statement
...@@ -264,16 +290,23 @@ def main(): ...@@ -264,16 +290,23 @@ def main():
# collect some info about the size, to not run completely blind # collect some info about the size, to not run completely blind
entries = {} entries = {}
for tableName in oraExp.getTables(): tablesToCreate = oraExp.getTables(exceptTables = oraExp.ignoreTablesCreate)
tablesToFill = oraExp.getTables(exceptTables = oraExp.ignoreTablesFill)
print("Tables to create: %i" % len(tablesToCreate))
print("Tables to fill: %i" % len(tablesToFill))
for tableName in tablesToCreate:
entries[tableName] = oraExp.tableSize(tableName) entries[tableName] = oraExp.tableSize(tableName)
print(" table %s has %i entries" % (tableName, entries[tableName]) ) doNotCopy = tableName not in tablesToFill
print(" table %s has %i entries %s" % (tableName, entries[tableName], ("(will not copy)" if doNotCopy else "") ) )
totalEntries = sum(entries.values()) totalEntries = sum(entries.values())
print("\nTotal number of entries: %i" %totalEntries) print("\nTotal number of entries: %i" %totalEntries)
# copy the data one table at the time # copy the data one table at the time
print("Start copying data") print("Start copying data")
copiedEntries = 0 copiedEntries = 0
for tableName in oraExp.getTables(): for tableName in tablesToFill:
print("Copying table %s" % tableName, end = '', flush=True) print("Copying table %s" % tableName, end = '', flush=True)
oraExp.copyTable(tableName, sqliteInserter, entries[tableName]) oraExp.copyTable(tableName, sqliteInserter, entries[tableName])
copiedEntries += entries[tableName] copiedEntries += entries[tableName]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment