Make db archival return a meaningful result code
This helps with automating to know when it's done. Update our own post-test-hook to use it. Change-Id: I61c3b187edab3e0e612470735eecd2db59028413
This commit is contained in:
parent
d5f113de34
commit
85bf71b2ae
@ -832,16 +832,20 @@ class DbCommands(object):
|
|||||||
def archive_deleted_rows(self, max_rows, verbose=False):
|
def archive_deleted_rows(self, max_rows, verbose=False):
|
||||||
"""Move up to max_rows deleted rows from production tables to shadow
|
"""Move up to max_rows deleted rows from production tables to shadow
|
||||||
tables.
|
tables.
|
||||||
|
|
||||||
|
Returns 0 if nothing was archived, 1 if some number of rows were
|
||||||
|
archived, 2 if max_rows is invalid. If automating, this should be
|
||||||
|
run continuously while the result is 1, stopping at 0.
|
||||||
"""
|
"""
|
||||||
if max_rows is not None:
|
if max_rows is not None:
|
||||||
max_rows = int(max_rows)
|
max_rows = int(max_rows)
|
||||||
if max_rows < 0:
|
if max_rows < 0:
|
||||||
print(_("Must supply a positive value for max_rows"))
|
print(_("Must supply a positive value for max_rows"))
|
||||||
return(1)
|
return(2)
|
||||||
if max_rows > db.MAX_INT:
|
if max_rows > db.MAX_INT:
|
||||||
print(_('max rows must be <= %(max_value)d') %
|
print(_('max rows must be <= %(max_value)d') %
|
||||||
{'max_value': db.MAX_INT})
|
{'max_value': db.MAX_INT})
|
||||||
return(1)
|
return(2)
|
||||||
table_to_rows_archived = db.archive_deleted_rows(max_rows)
|
table_to_rows_archived = db.archive_deleted_rows(max_rows)
|
||||||
if verbose:
|
if verbose:
|
||||||
if table_to_rows_archived:
|
if table_to_rows_archived:
|
||||||
@ -849,6 +853,8 @@ class DbCommands(object):
|
|||||||
dict_value=_('Number of Rows Archived'))
|
dict_value=_('Number of Rows Archived'))
|
||||||
else:
|
else:
|
||||||
print(_('Nothing was archived.'))
|
print(_('Nothing was archived.'))
|
||||||
|
# NOTE(danms): Return nonzero if we archived something
|
||||||
|
return int(bool(table_to_rows_archived))
|
||||||
|
|
||||||
@args('--delete', action='store_true', dest='delete',
|
@args('--delete', action='store_true', dest='delete',
|
||||||
help='If specified, automatically delete any records found where '
|
help='If specified, automatically delete any records found where '
|
||||||
|
@ -469,16 +469,16 @@ class DBCommandsTestCase(test.NoDBTestCase):
|
|||||||
self.commands = manage.DbCommands()
|
self.commands = manage.DbCommands()
|
||||||
|
|
||||||
def test_archive_deleted_rows_negative(self):
|
def test_archive_deleted_rows_negative(self):
|
||||||
self.assertEqual(1, self.commands.archive_deleted_rows(-1))
|
self.assertEqual(2, self.commands.archive_deleted_rows(-1))
|
||||||
|
|
||||||
def test_archive_deleted_rows_large_number(self):
|
def test_archive_deleted_rows_large_number(self):
|
||||||
large_number = '1' * 100
|
large_number = '1' * 100
|
||||||
self.assertEqual(1, self.commands.archive_deleted_rows(large_number))
|
self.assertEqual(2, self.commands.archive_deleted_rows(large_number))
|
||||||
|
|
||||||
@mock.patch.object(db, 'archive_deleted_rows',
|
@mock.patch.object(db, 'archive_deleted_rows',
|
||||||
return_value=dict(instances=10, consoles=5))
|
return_value=dict(instances=10, consoles=5))
|
||||||
def _test_archive_deleted_rows(self, mock_db_archive, verbose=False):
|
def _test_archive_deleted_rows(self, mock_db_archive, verbose=False):
|
||||||
self.commands.archive_deleted_rows(20, verbose=verbose)
|
result = self.commands.archive_deleted_rows(20, verbose=verbose)
|
||||||
mock_db_archive.assert_called_once_with(20)
|
mock_db_archive.assert_called_once_with(20)
|
||||||
output = self.output.getvalue()
|
output = self.output.getvalue()
|
||||||
if verbose:
|
if verbose:
|
||||||
@ -493,6 +493,7 @@ class DBCommandsTestCase(test.NoDBTestCase):
|
|||||||
self.assertEqual(expected, output)
|
self.assertEqual(expected, output)
|
||||||
else:
|
else:
|
||||||
self.assertEqual(0, len(output))
|
self.assertEqual(0, len(output))
|
||||||
|
self.assertEqual(1, result)
|
||||||
|
|
||||||
def test_archive_deleted_rows(self):
|
def test_archive_deleted_rows(self):
|
||||||
# Tests that we don't show any table output (not verbose).
|
# Tests that we don't show any table output (not verbose).
|
||||||
@ -504,10 +505,11 @@ class DBCommandsTestCase(test.NoDBTestCase):
|
|||||||
|
|
||||||
@mock.patch.object(db, 'archive_deleted_rows', return_value={})
|
@mock.patch.object(db, 'archive_deleted_rows', return_value={})
|
||||||
def test_archive_deleted_rows_verbose_no_results(self, mock_db_archive):
|
def test_archive_deleted_rows_verbose_no_results(self, mock_db_archive):
|
||||||
self.commands.archive_deleted_rows(20, verbose=True)
|
result = self.commands.archive_deleted_rows(20, verbose=True)
|
||||||
mock_db_archive.assert_called_once_with(20)
|
mock_db_archive.assert_called_once_with(20)
|
||||||
output = self.output.getvalue()
|
output = self.output.getvalue()
|
||||||
self.assertIn('Nothing was archived.', output)
|
self.assertIn('Nothing was archived.', output)
|
||||||
|
self.assertEqual(0, result)
|
||||||
|
|
||||||
@mock.patch.object(migration, 'db_null_instance_uuid_scan',
|
@mock.patch.object(migration, 'db_null_instance_uuid_scan',
|
||||||
return_value={'foo': 0})
|
return_value={'foo': 0})
|
||||||
|
@ -6,9 +6,13 @@ function archive_deleted_rows {
|
|||||||
# NOTE(danms): Run this a few times to make sure that we end
|
# NOTE(danms): Run this a few times to make sure that we end
|
||||||
# up with nothing more to archive
|
# up with nothing more to archive
|
||||||
for i in `seq 30`; do
|
for i in `seq 30`; do
|
||||||
out=$($MANAGE db archive_deleted_rows --verbose --max_rows 1000)
|
$MANAGE db archive_deleted_rows --verbose --max_rows 1000
|
||||||
echo $?
|
RET=$?
|
||||||
if [[ $out =~ "Nothing was archived" ]]; then
|
if [[ $RET -gt 1 ]]; then
|
||||||
|
echo Archiving failed with result $RET
|
||||||
|
return $RET
|
||||||
|
elif [[ $RET -eq 0 ]]; then
|
||||||
|
echo Archiving Complete
|
||||||
break;
|
break;
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
Loading…
x
Reference in New Issue
Block a user