VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/db/partial-db-dump.py@ 85582

Last change on this file since 85582 was 83363, checked in by vboxsync, 5 years ago

TestManager/partial-db-dump.py: Include the TestBoxesInSchedGroups table.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: partial-db-dump.py 83363 2020-03-22 18:26:38Z vboxsync $
4# pylint: disable=line-too-long
5
6"""
7Utility for dumping the last X days of data.
8"""
9
10__copyright__ = \
11"""
12Copyright (C) 2012-2020 Oracle Corporation
13
14This file is part of VirtualBox Open Source Edition (OSE), as
15available from http://www.virtualbox.org. This file is free software;
16you can redistribute it and/or modify it under the terms of the GNU
17General Public License (GPL) as published by the Free Software
18Foundation, in version 2 as it comes in the "COPYING" file of the
19VirtualBox OSE distribution. VirtualBox OSE is distributed in the
20hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21
22The contents of this file may alternatively be used under the terms
23of the Common Development and Distribution License Version 1.0
24(CDDL) only, as it comes in the "COPYING.CDDL" file of the
25VirtualBox OSE distribution, in which case the provisions of the
26CDDL are applicable instead of those of the GPL.
27
28You may elect to license modified versions of this file under the
29terms and conditions of either the GPL or the CDDL or both.
30"""
31__version__ = "$Revision: 83363 $"
32
33# Standard python imports
34import sys;
35import os;
36import zipfile;
37from optparse import OptionParser;
38import xml.etree.ElementTree as ET;
39
40# Add Test Manager's modules path
41g_ksTestManagerDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
42sys.path.append(g_ksTestManagerDir);
43
44# Test Manager imports
45from testmanager.core.db import TMDatabaseConnection;
46from common import utils;
47
48
49class PartialDbDump(object): # pylint: disable=too-few-public-methods
50 """
51 Dumps or loads the last X days of database data.
52
53 This is a useful tool when hacking on the test manager locally. You can get
54 a small sample from the last few days from the production test manager server
55 without spending hours dumping, downloading, and loading the whole database
56 (because it is gigantic).
57
58 """
59
60 def __init__(self):
61 """
62 Parse command line.
63 """
64
65 oParser = OptionParser()
66 oParser.add_option('-q', '--quiet', dest = 'fQuiet', action = 'store_true',
67 help = 'Quiet execution');
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');
74 oParser.add_option('--days-to-dump', dest = 'cDays', metavar = '<days>', type = 'int', default = 14,
75 help = 'How many days to dump (counting backward from current date).');
76 oParser.add_option('--load-dump-into-database', dest = 'fLoadDumpIntoDatabase', action = 'store_true',
77 default = False, help = 'For loading instead of dumping.');
78
79 (self.oConfig, _) = oParser.parse_args();
80
81
82 ##
83 # Tables dumped in full because they're either needed in full or they normally
84 # aren't large enough to bother reducing.
85 kasTablesToDumpInFull = [
86 'Users',
87 'BuildBlacklist',
88 'BuildCategories',
89 'BuildSources',
90 'FailureCategories',
91 'FailureReasons',
92 'GlobalResources',
93 'TestBoxStrTab',
94 'Testcases',
95 'TestcaseArgs',
96 'TestcaseDeps',
97 'TestcaseGlobalRsrcDeps',
98 'TestGroups',
99 'TestGroupMembers',
100 'SchedGroups',
101 'SchedGroupMembers', # ?
102 'TestBoxesInSchedGroups', # ?
103 'SchedQueues',
104 'Builds', # ??
105 'VcsRevisions', # ?
106 'TestResultStrTab', # 36K rows, never mind complicated then.
107 ];
108
109 ##
110 # Tables where we only dump partial info (the TestResult* tables are rather
111 # gigantic).
112 kasTablesToPartiallyDump = [
113 'TestBoxes', # 2016-05-25: ca. 641 MB
114 'TestSets', # 2016-05-25: ca. 525 MB
115 'TestResults', # 2016-05-25: ca. 13 GB
116 'TestResultFiles', # 2016-05-25: ca. 87 MB
117 'TestResultMsgs', # 2016-05-25: ca. 29 MB
118 'TestResultValues', # 2016-05-25: ca. 3728 MB
119 'TestResultFailures',
120 'SystemLog',
121 ];
122
123 def _doCopyTo(self, sTable, oZipFile, oDb, sSql, aoArgs = None):
124 """ Does one COPY TO job. """
125 print('Dumping %s...' % (sTable,));
126
127 if aoArgs is not None:
128 sSql = oDb.formatBindArgs(sSql, aoArgs);
129
130 oFile = open(self.oConfig.sTempFile, 'w');
131 oDb.copyExpert(sSql, oFile);
132 cRows = oDb.getRowCount();
133 oFile.close();
134 print('... %s rows.' % (cRows,));
135
136 oZipFile.write(self.oConfig.sTempFile, sTable);
137 return True;
138
139 def _doDump(self, oDb):
140 """ Does the dumping of the database. """
141
142 oZipFile = zipfile.ZipFile(self.oConfig.sFilename, 'w', zipfile.ZIP_DEFLATED);
143
144 oDb.begin();
145
146 # Dumping full tables is simple.
147 for sTable in self.kasTablesToDumpInFull:
148 self._doCopyTo(sTable, oZipFile, oDb, 'COPY ' + sTable + ' TO STDOUT WITH (FORMAT TEXT)');
149
150 # Figure out how far back we need to go.
151 oDb.execute('SELECT CURRENT_TIMESTAMP - INTERVAL \'%s days\'' % (self.oConfig.cDays,));
152 tsEffective = oDb.fetchOne()[0];
153 oDb.execute('SELECT CURRENT_TIMESTAMP - INTERVAL \'%s days\'' % (self.oConfig.cDays + 2,));
154 tsEffectiveSafe = oDb.fetchOne()[0];
155 print('Going back to: %s (safe: %s)' % (tsEffective, tsEffectiveSafe));
156
157 # We dump test boxes back to the safe timestamp because the test sets may
158 # use slightly dated test box references and we don't wish to have dangling
159 # references when loading.
160 for sTable in [ 'TestBoxes', ]:
161 self._doCopyTo(sTable, oZipFile, oDb,
162 'COPY (SELECT * FROM ' + sTable + ' WHERE tsExpire >= %s) TO STDOUT WITH (FORMAT TEXT)',
163 (tsEffectiveSafe,));
164
165 # The test results needs to start with test sets and then dump everything
166 # releated to them. So, figure the lowest (oldest) test set ID we'll be
167 # dumping first.
168 oDb.execute('SELECT idTestSet FROM TestSets WHERE tsCreated >= %s', (tsEffective, ));
169 idFirstTestSet = 0;
170 if oDb.getRowCount() > 0:
171 idFirstTestSet = oDb.fetchOne()[0];
172 print('First test set ID: %s' % (idFirstTestSet,));
173
174 oDb.execute('SELECT MAX(idTestSet) FROM TestSets WHERE tsCreated >= %s', (tsEffective, ));
175 idLastTestSet = 0;
176 if oDb.getRowCount() > 0:
177 idLastTestSet = oDb.fetchOne()[0];
178 print('Last test set ID: %s' % (idLastTestSet,));
179
180 oDb.execute('SELECT MAX(idTestResult) FROM TestResults WHERE tsCreated >= %s', (tsEffective, ));
181 idLastTestResult = 0;
182 if oDb.getRowCount() > 0:
183 idLastTestResult = oDb.fetchOne()[0];
184 print('Last test result ID: %s' % (idLastTestResult,));
185
186
187 # Tables with idTestSet member.
188 for sTable in [ 'TestSets', 'TestResults', 'TestResultValues' ]:
189 self._doCopyTo(sTable, oZipFile, oDb,
190 'COPY (SELECT *\n'
191 ' FROM ' + sTable + '\n'
192 ' WHERE idTestSet >= %s\n'
193 ' AND idTestSet <= %s\n'
194 ' AND idTestResult <= %s\n'
195 ') TO STDOUT WITH (FORMAT TEXT)'
196 , ( idFirstTestSet, idLastTestSet, idLastTestResult,));
197
198 # Tables where we have to go via TestResult.
199 for sTable in [ 'TestResultFiles', 'TestResultMsgs', 'TestResultFailures' ]:
200 self._doCopyTo(sTable, oZipFile, oDb,
201 'COPY (SELECT it.*\n'
202 ' FROM ' + sTable + ' it, TestResults tr\n'
203 ' WHERE tr.idTestSet >= %s\n'
204 ' AND tr.idTestSet <= %s\n'
205 ' AND tr.idTestResult <= %s\n'
206 ' AND tr.tsCreated >= %s\n' # performance hack.
207 ' AND it.idTestResult = tr.idTestResult\n'
208 ') TO STDOUT WITH (FORMAT TEXT)'
209 , ( idFirstTestSet, idLastTestSet, idLastTestResult, tsEffective,));
210
211 # Tables which goes exclusively by tsCreated.
212 for sTable in [ 'SystemLog', ]:
213 self._doCopyTo(sTable, oZipFile, oDb,
214 'COPY (SELECT * FROM ' + sTable + ' WHERE tsCreated >= %s) TO STDOUT WITH (FORMAT TEXT)',
215 (tsEffective,));
216
217 oZipFile.close();
218 print('Done!');
219 return 0;
220
221 def _doLoad(self, oDb):
222 """ Does the loading of the dumped data into the database. """
223
224 oZipFile = zipfile.ZipFile(self.oConfig.sFilename, 'r');
225
226 asTablesInLoadOrder = [
227 'Users',
228 'BuildBlacklist',
229 'BuildCategories',
230 'BuildSources',
231 'FailureCategories',
232 'FailureReasons',
233 'GlobalResources',
234 'Testcases',
235 'TestcaseArgs',
236 'TestcaseDeps',
237 'TestcaseGlobalRsrcDeps',
238 'TestGroups',
239 'TestGroupMembers',
240 'SchedGroups',
241 'TestBoxStrTab',
242 'TestBoxes',
243 'SchedGroupMembers',
244 'TestBoxesInSchedGroups',
245 'SchedQueues',
246 'Builds',
247 'SystemLog',
248 'VcsRevisions',
249 'TestResultStrTab',
250 'TestSets',
251 'TestResults',
252 'TestResultFiles',
253 'TestResultMsgs',
254 'TestResultValues',
255 'TestResultFailures',
256 ];
257 assert len(asTablesInLoadOrder) == len(self.kasTablesToDumpInFull) + len(self.kasTablesToPartiallyDump);
258
259 oDb.begin();
260 oDb.execute('SET CONSTRAINTS ALL DEFERRED;');
261
262 print('Checking if the database looks empty...\n');
263 for sTable in asTablesInLoadOrder + [ 'TestBoxStatuses', 'GlobalResourceStatuses' ]:
264 oDb.execute('SELECT COUNT(*) FROM ' + sTable);
265 cRows = oDb.fetchOne()[0];
266 cMaxRows = 0;
267 if sTable in [ 'SchedGroups', 'TestBoxStrTab', 'TestResultStrTab', 'Users' ]: cMaxRows = 1;
268 if cRows > cMaxRows:
269 print('error: Table %s has %u rows which is more than %u - refusing to delete and load.'
270 % (sTable, cRows, cMaxRows,));
271 print('info: Please drop and recreate the database before loading!')
272 return 1;
273
274 print('Dropping default table content...\n');
275 for sTable in [ 'SchedGroups', 'TestBoxStrTab', 'TestResultStrTab', 'Users']:
276 oDb.execute('DELETE FROM ' + sTable);
277
278 oDb.execute('ALTER TABLE TestSets DROP CONSTRAINT IF EXISTS TestSets_idTestResult_fkey');
279
280 for sTable in asTablesInLoadOrder:
281 print('Loading %s...' % (sTable,));
282 oFile = oZipFile.open(sTable);
283 oDb.copyExpert('COPY ' + sTable + ' FROM STDIN WITH (FORMAT TEXT)', oFile);
284 cRows = oDb.getRowCount();
285 print('... %s rows.' % (cRows,));
286
287 oDb.execute('ALTER TABLE TestSets ADD FOREIGN KEY (idTestResult) REFERENCES TestResults(idTestResult)');
288 oDb.commit();
289
290 # Correct sequences.
291 atSequences = [
292 ( 'UserIdSeq', 'Users', 'uid' ),
293 ( 'GlobalResourceIdSeq', 'GlobalResources', 'idGlobalRsrc' ),
294 ( 'BuildSourceIdSeq', 'BuildSources', 'idBuildSrc' ),
295 ( 'TestCaseIdSeq', 'TestCases', 'idTestCase' ),
296 ( 'TestCaseGenIdSeq', 'TestCases', 'idGenTestCase' ),
297 ( 'TestCaseArgsIdSeq', 'TestCaseArgs', 'idTestCaseArgs' ),
298 ( 'TestCaseArgsGenIdSeq', 'TestCaseArgs', 'idGenTestCaseArgs' ),
299 ( 'TestGroupIdSeq', 'TestGroups', 'idTestGroup' ),
300 ( 'SchedGroupIdSeq', 'SchedGroups', 'idSchedGroup' ),
301 ( 'TestBoxStrTabIdSeq', 'TestBoxStrTab', 'idStr' ),
302 ( 'TestBoxIdSeq', 'TestBoxes', 'idTestBox' ),
303 ( 'TestBoxGenIdSeq', 'TestBoxes', 'idGenTestBox' ),
304 ( 'FailureCategoryIdSeq', 'FailureCategories', 'idFailureCategory' ),
305 ( 'FailureReasonIdSeq', 'FailureReasons', 'idFailureReason' ),
306 ( 'BuildBlacklistIdSeq', 'BuildBlacklist', 'idBlacklisting' ),
307 ( 'BuildCategoryIdSeq', 'BuildCategories', 'idBuildCategory' ),
308 ( 'BuildIdSeq', 'Builds', 'idBuild' ),
309 ( 'TestResultStrTabIdSeq', 'TestResultStrTab', 'idStr' ),
310 ( 'TestResultIdSeq', 'TestResults', 'idTestResult' ),
311 ( 'TestResultValueIdSeq', 'TestResultValues', 'idTestResultValue' ),
312 ( 'TestResultFileId', 'TestResultFiles', 'idTestResultFile' ),
313 ( 'TestResultMsgIdSeq', 'TestResultMsgs', 'idTestResultMsg' ),
314 ( 'TestSetIdSeq', 'TestSets', 'idTestSet' ),
315 ( 'SchedQueueItemIdSeq', 'SchedQueues', 'idItem' ),
316 ];
317 for (sSeq, sTab, sCol) in atSequences:
318 oDb.execute('SELECT MAX(%s) FROM %s' % (sCol, sTab,));
319 idMax = oDb.fetchOne()[0];
320 print('%s: idMax=%s' % (sSeq, idMax));
321 if idMax is not None:
322 oDb.execute('SELECT setval(\'%s\', %s)' % (sSeq, idMax));
323
324 # Last step.
325 print('Analyzing...');
326 oDb.execute('ANALYZE');
327 oDb.commit();
328
329 print('Done!');
330 return 0;
331
332 def main(self):
333 """
334 Main function.
335 """
336 oDb = TMDatabaseConnection();
337
338 if self.oConfig.fLoadDumpIntoDatabase is not True:
339 rc = self._doDump(oDb);
340 else:
341 rc = self._doLoad(oDb);
342
343 oDb.close();
344 return 0;
345
346if __name__ == '__main__':
347 sys.exit(PartialDbDump().main());
348
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette