Skip to content
Snippets Groups Projects
Commit e38c7022 authored by Adriaan Renting's avatar Adriaan Renting
Browse files

Task #10981: Added script to read and process MoM DB dump

parent b7b2f0d1
No related branches found
No related tags found
No related merge requests found
......@@ -5564,4 +5564,5 @@ SubSystems/RAServices/RAServices.ini -text
SubSystems/SAS_Tools/CMakeLists.txt -text
/jenkins_make -text
/lofar_config.h.cmake -text
support/tools/MoM/process_mom_sql.py -text
support/tools/lofstorman -text
#!/usr/bin/python
infile = "backup_lofar_mom3.sql.1"
indatabase = "lofar_mom3"
outdatabase = "lofar_mom_test_rt_trigger"
outdir = "test/"
outfile = open(outdir +"000000_CREATE_DATABASE.sql", 'w')
# Please note that "Comments" in the SQL that look like this
# /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
# are not really comments, but conditionals, which mean that they will
# only be interpreted by MySQL and only if the version is above e.g. 4.01.01
# see https://dev.mysql.com/doc/refman/5.7/en/comments.html
# This script is not robust, but tries to be efficient based on assumptions
# on the current mom database. When adding new triggers or views, it might need updates.
# after running this script a "grep <indatabase> *.sql" should be used to verify
# Fun state machine to parse a SQL dump follows
buffer = "" # Buffer to store lines until we know what we found next
use_found = False # When to close the CREATE_DATABASE script
line_nr = 0
possible_trigger = False
trigger = False
with open(infile) as f:
for line in f:
if not use_found or trigger or (line[0:13] == "/*!50001 VIEW") or (line[0:2] == '--'):
if indatabase in line: #doing this for each line would be expensive
line = line.replace(indatabase, outdatabase)
if line[0:3] == "USE":
if use_found:
outfile = open(outdir + "%06d_" % line_nr + "USE_%s.sql" % outdatabase, 'w')
outfile.write(buffer)
buffer = ""
if indatabase in line: # could also be in the first if in the for loop
line = line.replace(indatabase, outdatabase)
outfile.write(line)
else:
use_found = True # End of the general database creation
outfile.write(buffer)
buffer = ""
outfile.write(line)
elif "UNLOCK TABLES" in line: # Triggers on a table are possible after the WRITE
possible_trigger = True
outfile.write(line)
elif possible_trigger and line[0:8] == "/*!50003": #Special BiRT views
buffer += line
elif possible_trigger and "DELIMITER ;;" in line: # Trigger code follows
buffer += line
trigger = True
possible_trigger = False
elif "END */;;" in line: # End of trigger code
trigger = False
outfile.write(line)
possible_trigger = True # There might be more trigger code.
elif not trigger and (line[0:2] == '--' or line.isspace() or not use_found):
buffer += line
elif buffer: # We should have a line that tells us what is happening next
print line
outfile.close()
possible_trigger = False
filename = line.translate(None, '/*;`!@=\n()').replace(' ','_')
if 'DROP_TABLE_IF_EXISTS_' in filename:
filename = 'CREATE_TABLE_OR_VIEW_' + filename[filename.find('DROP_TABLE_IF_EXISTS_') + 21:]
if 'LOCK_TABLES_' in filename:
filename = 'WRITE_TABLE_' + filename[12:-6]
if 'TRIGGER' in line:
filename = 'CREATE_' + filename[filename.find('TRIGGER'):-19]
outfile = open(outdir + "%06d_" % line_nr + filename + ".sql", 'w')
outfile.write(buffer)
buffer = ""
outfile.write(line)
elif trigger:
outfile.write(line)
elif "Dump completed" in line: # Last line of dump
outfile.write(buffer)
buffer = ""
outfile.close()
break
else:
outfile.write(line)
line_nr += 1
\ No newline at end of file
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