Reset primary key back to 1 after deleting all revisions

This PS resets the primary key for each table in the
database back to 1 after deleting all revisions
(i.e. DELETE /revisions) which effectively purges all
the data in the database.

Change-Id: Id64e01ea85d7098e261a56cf9eacdcce716b1f48
This commit is contained in:
Felipe Monteiro 2017-11-24 17:39:34 +00:00
parent 8cc63164d2
commit d941b09ef0
2 changed files with 26 additions and 7 deletions

View File

@ -607,15 +607,28 @@ def revision_get_all(session=None, **filters):
return result
def revision_delete_all(session=None):
"""Delete all revisions.
def revision_delete_all():
"""Delete all revisions and resets primary key index back to 1 for each
table in the database.
.. warning::
Effectively purges all data from database.
:param session: Database session object.
:returns: None
"""
session = session or get_session()
session.query(models.Revision)\
.delete(synchronize_session=False)
engine = get_engine()
if engine.name == 'postgresql':
# NOTE(fmontei): While cascade should delete all data from all tables,
# we also need to reset the index to 1 for each table.
for table in ['buckets', 'revisions', 'revision_tags', 'documents',
'validations']:
engine.execute(
text("TRUNCATE TABLE %s RESTART IDENTITY CASCADE;" % table)
.execution_options(autocommit=True))
else:
raw_query("DELETE FROM revisions;")
def _exclude_deleted_documents(documents):

View File

@ -132,8 +132,7 @@ class TestRevisions(base.TestDbBase):
all_revision_ids = []
for _ in range(3):
document_payload = [base.DocumentFixture.get_minimal_fixture()
for _ in range(3)]
document_payload = [base.DocumentFixture.get_minimal_fixture()]
bucket_name = test_utils.rand_name('bucket')
created_documents = self.create_documents(
bucket_name, document_payload)
@ -156,6 +155,13 @@ class TestRevisions(base.TestDbBase):
self.assertRaisesRegex(errors.DocumentNotFound, error_re,
self.show_document, **filters)
# Validate that the revision/document ID was reset back to 1.
bucket_name = test_utils.rand_name('bucket')
created_documents = self.create_documents(
bucket_name, [base.DocumentFixture.get_minimal_fixture()])
self.assertEqual(1, created_documents[0]['revision_id'])
self.assertEqual(1, created_documents[0]['id'])
def test_revision_history_multiple_buckets(self):
documents = base.DocumentFixture.get_minimal_fixture()
alt_documents = base.DocumentFixture.get_minimal_fixture()