Archive instance_actions and instance_actions_event

This try to handle the soft-delete of instance_actions and
instance_actions_event before the archive of deleted rows. So in
this way theses tables will be archived like others.

Change-Id: Ia0fe130a35a6c9bb16a940f3f4cea6b1aaee9668
Partial-Bug: #1183523
This commit is contained in:
Cedric LECOMTE 2016-03-30 15:18:22 +00:00 committed by Cédric LECOMTE
parent 40196d34db
commit 59439059ed
2 changed files with 35 additions and 6 deletions

View File

@ -6470,6 +6470,35 @@ def _archive_deleted_rows_for_table(tablename, max_rows):
# database's limit of maximum parameter in one SQL statement.
deleted_column = table.c.deleted
columns = [c.name for c in table.c]
# NOTE(clecomte): Tables instance_actions and instances_actions_events
# have to be manage differently so we soft-delete them here to let
# the archive work the same for all tables
if tablename == "instance_actions":
instances = models.BASE.metadata.tables["instances"]
deleted_instances = sql.select([instances.c.uuid]).\
where(instances.c.deleted != instances.c.deleted.default.arg)
update_statement = table.update().values(deleted=table.c.id).\
where(table.c.instance_uuid.in_(deleted_instances))
conn.execute(update_statement)
elif tablename == "instance_actions_events":
# NOTE(clecomte): we have to grab all the relation from
# instances because instance_actions_events rely on
# action_id and not uuid
instances = models.BASE.metadata.tables["instances"]
instance_actions = models.BASE.metadata.tables["instance_actions"]
deleted_instances = sql.select([instances.c.uuid]).\
where(instances.c.deleted != instances.c.deleted.default.arg)
deleted_actions = sql.select([instance_actions.c.id]).\
where(instance_actions.c.instance_uuid.in_(deleted_instances))
update_statement = table.update().values(deleted=table.c.id).\
where(table.c.action_id.in_(deleted_actions))
conn.execute(update_statement)
insert = shadow_table.insert(inline=True).\
from_select(columns,
sql.select([table],

View File

@ -96,9 +96,9 @@ class TestDatabaseArchive(test_servers.ServersTestBase):
self.assertIn('instance_system_metadata', results)
self.assertEqual(len(instance.system_metadata),
results['instance_system_metadata'])
# FIXME(mriedem): we fail to archive instances because of a fkey
# referential constraint error with instance_actions not being deleted
self.assertNotIn('instances', results)
# FIXME(mriedem): instance_actions aren't soft deleted so they aren't
# archived, which we need to fix.
self.assertNotIn('instance_actions', results)
# Verify that instances rows are dropped
self.assertIn('instances', results)
# Verify that instance_actions and actions_event are dropped
# by the archive
self.assertIn('instance_actions', results)
self.assertIn('instance_actions_events', results)