Changeset 61184 in vbox for trunk/src/VBox/ValidationKit
- Timestamp:
- May 25, 2016 3:04:15 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/ValidationKit/testmanager/db/partial-db-dump.py
r61183 r61184 34 34 import sys; 35 35 import os; 36 import zipfile; 36 37 from optparse import OptionParser; 37 38 import xml.etree.ElementTree as ET; … … 65 66 oParser.add_option('-q', '--quiet', dest = 'fQuiet', action = 'store_true', 66 67 help = 'Quiet execution'); 67 oParser.add_option('-b', '--base-filename', dest = 'sBaseFilename', metavar = '<base-filename>', 68 default = '/tmp/testmanager-partial-dump', 69 help = 'Absolute base filename (dash + table name + sql appended).'); 68 oParser.add_option('-f', '--filename', dest = 'sFilename', metavar = '<filename>', 69 default = 'partial-db-dump.zip', help = 'The name of the partial database zip file to write/load.'); 70 71 oParser.add_option('-t', '--tmp-file', dest = 'sTempFile', metavar = '<temp-file>', 72 default = '/tmp/tm-partial-db-dump.pgtxt', 73 help = 'Name of temporary file for duping tables. Must be absolute'); 70 74 oParser.add_option('--days-to-dump', dest = 'cDays', metavar = '<days>', type = 'int', default = 14, 71 75 help = 'How many days to dump (counting backward from current date).'); … … 96 100 'SchedQueues', 97 101 'Builds', # ?? 98 'SystemLog', # ?99 102 'VcsRevisions', # ? 100 103 'TestResultStrTab', # 36K rows, never mind complicated then. 101 'GlobalResourceStatuses', # ?102 'TestBoxStatuses', # ?103 104 ]; 104 105 … … 114 115 'TestResultValues', # 2016-05-25: ca. 3728 MB 115 116 'TestResultFailures', 117 'SystemLog', 116 118 ]; 119 120 def _doCopyTo(self, sTable, oZipFile, oDb, sSql, aoArgs = None): 121 """ Does one COPY TO job. """ 122 print 'Dumping %s...' % (sTable,); 123 124 if aoArgs is not None: 125 sSql = oDb.formatBindArgs(sSql, aoArgs); 126 127 oFile = open(self.oConfig.sTempFile, 'w'); 128 oDb.copyExpert(sSql, oFile); 129 cRows = oDb.getRowCount(); 130 oFile.close(); 131 print '... %s rows.' % (cRows,); 132 133 oZipFile.write(self.oConfig.sTempFile, sTable); 134 return True; 117 135 118 136 def _doDump(self, oDb): 119 137 """ Does the dumping of the database. """ 120 138 139 oZipFile = zipfile.ZipFile(self.oConfig.sFilename, 'w', zipfile.ZIP_DEFLATED); 140 121 141 oDb.begin(); 122 142 123 143 # Dumping full tables is simple. 124 144 for sTable in self.kasTablesToDumpInFull: 125 sFile = self.oConfig.sBaseFilename + '-' + sTable + '.pgtxt'; 126 print 'Dumping %s into "%s"...' % (sTable, sFile,); 127 oDb.execute('COPY ' + sTable + ' TO %s WITH (FORMAT TEXT)', (sFile,)); 128 cRows = oDb.getRowCount(); 129 print '... %s rows.' % (cRows,); 145 self._doCopyTo(sTable, oZipFile, oDb, 'COPY ' + sTable + ' TO STDOUT WITH (FORMAT TEXT)'); 130 146 131 147 # Figure out how far back we need to go. … … 140 156 # references when loading. 141 157 for sTable in [ 'TestBoxes', ]: 142 sFile = self.oConfig.sBaseFilename + '-' + sTable + '.pgtxt'; 143 print 'Dumping %s into "%s"...' % (sTable, sFile,); 144 oDb.execute('COPY (SELECT * FROM ' + sTable + ' WHERE tsExpire >= %s) TO %s WITH (FORMAT TEXT)', 145 (tsEffectiveSafe, sFile,)); 146 cRows = oDb.getRowCount(); 147 print '... %s rows.' % (cRows,); 158 self._doCopyTo(sTable, oZipFile, oDb, 159 'COPY (SELECT * FROM ' + sTable + ' WHERE tsExpire >= %s) TO STDOUT WITH (FORMAT TEXT)', 160 (tsEffectiveSafe,)); 148 161 149 162 # The test results needs to start with test sets and then dump everything … … 158 171 # Tables with idTestSet member. 159 172 for sTable in [ 'TestSets', 'TestResults', 'TestResultValues' ]: 160 sFile = self.oConfig.sBaseFilename + '-' + sTable + '.pgtxt'; 161 print 'Dumping %s into "%s"...' % (sTable, sFile,); 162 oDb.execute('COPY (SELECT * FROM ' + sTable + ' WHERE idTestSet >= %s) TO %s WITH (FORMAT TEXT)', 163 (idFirstTestSet, sFile,)); 173 self._doCopyTo(sTable, oZipFile, oDb, 174 'COPY (SELECT * FROM ' + sTable + ' WHERE idTestSet >= %s) TO STDOUT WITH (FORMAT TEXT)', 175 (idFirstTestSet,)); 176 177 # Tables where we have to go via TestResult. 178 for sTable in [ 'TestResultFiles', 'TestResultMsgs', 'TestResultFailures' ]: 179 self._doCopyTo(sTable, oZipFile, oDb, 180 'COPY (SELECT it.*\n' 181 ' FROM ' + sTable + ' it, TestResults tr\n' 182 ' WHERE tr.idTestSet >= %s\n' 183 ' AND tr.tsCreated >= %s\n' # performance hack. 184 ' AND it.idTestResult = tr.idTestResult\n' 185 ') TO STDOUT WITH (FORMAT TEXT)', 186 (idFirstTestSet, tsEffective,)); 187 188 # Tables which goes exclusively by tsCreated. 189 for sTable in [ 'SystemLog', ]: 190 self._doCopyTo(sTable, oZipFile, oDb, 191 'COPY (SELECT * FROM ' + sTable + ' WHERE tsCreated >= %s) TO STDOUT WITH (FORMAT TEXT)', 192 (tsEffective,)); 193 194 oZipFile.close(); 195 print "Done!"; 196 return 0; 197 198 def _doLoad(self, oDb): 199 """ Does the loading of the dumped data into the database. """ 200 201 oZipFile = zipfile.ZipFile(self.oConfig.sFilename, 'r'); 202 203 asTablesInLoadOrder = [ 204 'Users', 205 'BuildBlacklist', 206 'BuildCategories', 207 'BuildSources', 208 'FailureCategories', 209 'FailureReasons', 210 'GlobalResources', 211 'Testcases', 212 'TestcaseArgs', 213 'TestcaseDeps', 214 'TestcaseGlobalRsrcDeps', 215 'TestGroups', 216 'TestGroupMembers', 217 'TestBoxes', 218 'SchedGroupMembers', 219 'SchedQueues', 220 'Builds', 221 'SystemLog', 222 'VcsRevisions', 223 'TestResultStrTab', 224 'TestSets', 225 'TestResults', 226 'TestResultFiles', 227 'TestResultMsgs', 228 'TestResultValues', 229 'TestResultFailures', 230 ]; 231 assert len(asTablesInLoadOrder) == len(self.kasTablesToDumpInFull) + len(self.kasTablesToPartiallyDump); 232 233 oDb.begin(); 234 oDb.execute('SET CONSTRAINTS ALL DEFERRED;'); 235 236 print 'Checking if the database looks empty...\n' 237 for sTable in asTablesInLoadOrder + [ 'TestBoxStatuses', 'GlobalResourceStatuses' ]: 238 oDb.execute('SELECT COUNT(*) FROM ' + sTable); 239 cRows = oDb.fetchOne()[0]; 240 cMaxRows = 0; 241 if sTable in [ 'TestResultStrTab', 'Users' ]: cMaxRows = 1; 242 if cRows > cMaxRows: 243 print 'error: Table %s has %u rows which is more than %u - refusing to delete and load.' \ 244 % (sTable, cRows, cMaxRows,); 245 print 'info: Please drop and recreate the database before loading!' 246 return 1; 247 248 print 'Dropping default table content...\n' 249 for sTable in [ 'TestResultStrTab', 'Users']: 250 oDb.execute('DELETE FROM ' + sTable); 251 252 oDb.execute('ALTER TABLE TestSets DROP CONSTRAINT IF EXISTS TestSets_idTestResult_fkey'); 253 254 for sTable in asTablesInLoadOrder: 255 print 'Loading %s...' % (sTable,); 256 oFile = oZipFile.open(sTable); 257 oDb.copyExpert('COPY ' + sTable + ' FROM STDIN WITH (FORMAT TEXT)', oFile); 164 258 cRows = oDb.getRowCount(); 165 259 print '... %s rows.' % (cRows,); 166 260 167 # Tables where we have to go via TestResult. 168 for sTable in [ 'TestResultFiles', 'TestResultMsgs', 'TestResultFailures' ]: 169 sFile = self.oConfig.sBaseFilename + '-' + sTable + '.pgtxt'; 170 print 'Dumping %s into "%s"...' % (sTable, sFile,); 171 oDb.execute('COPY (SELECT it.*\n' 172 ' FROM ' + sTable + ' it, TestResults tr\n' 173 ' WHERE tr.idTestSet >= %s\n' 174 ' AND tr.tsCreated >= %s\n' # performance hack. 175 ' AND it.idTestResult = tr.idTestResult\n' 176 ') TO %s WITH (FORMAT TEXT)', 177 (idFirstTestSet, tsEffective, sFile,)); 178 cRows = oDb.getRowCount(); 179 print '... %s rows.' % (cRows,); 180 181 return 0; 182 183 def _doLoad(self, oDb): 184 """ Does the loading of the dumped data into the database. """ 261 oDb.execute('ALTER TABLE TestSets ADD FOREIGN KEY (idTestResult) REFERENCES TestResults(idTestResult)'); 262 oDb.commit(); 263 264 # Last step. 265 print 'Analyzing...' 266 oDb.execute('ANALYZE'); 267 oDb.commit(); 268 269 print 'Done!' 185 270 return 0; 186 271
Note:
See TracChangeset
for help on using the changeset viewer.