From 30d99f514071a9272caa018d140c9f7a367a7b6f Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Thu, 7 Nov 2019 00:15:25 +0000 Subject: [PATCH] Get rid of _delete_db It was parctically the same between both, only the table name was different -- and we already have a way to accomodate that. Change-Id: I112bef1cf44c7d6eeaacd30cea27cb71fb2e2406 --- swift/account/backend.py | 14 ---------- swift/common/db.py | 9 ++++++- swift/container/backend.py | 14 ---------- test/unit/common/test_db.py | 51 ++++++++++++++++++++++--------------- 4 files changed, 39 insertions(+), 49 deletions(-) diff --git a/swift/account/backend.py b/swift/account/backend.py index 1e4b5d7018..7e65fa054e 100644 --- a/swift/account/backend.py +++ b/swift/account/backend.py @@ -189,20 +189,6 @@ class AccountBroker(DatabaseBroker): self._db_version = 1 return self._db_version - def _delete_db(self, conn, timestamp, force=False): - """ - Mark the DB as deleted. - - :param conn: DB connection object - :param timestamp: timestamp to mark as deleted - """ - conn.execute(""" - UPDATE account_stat - SET delete_timestamp = ?, - status = 'DELETED', - status_changed_at = ? - WHERE delete_timestamp < ? """, (timestamp, timestamp, timestamp)) - def _commit_puts_load(self, item_list, entry): """See :func:`swift.common.db.DatabaseBroker._commit_puts_load`""" # check to see if the update includes policy_index or not diff --git a/swift/common/db.py b/swift/common/db.py index 2fdc94260e..c5e189f1d7 100644 --- a/swift/common/db.py +++ b/swift/common/db.py @@ -356,7 +356,14 @@ class DatabaseBroker(object): self.update_metadata(cleared_meta) # then mark the db as deleted with self.get() as conn: - self._delete_db(conn, timestamp) + conn.execute( + """ + UPDATE %s_stat + SET delete_timestamp = ?, + status = 'DELETED', + status_changed_at = ? + WHERE delete_timestamp < ? """ % self.db_type, + (timestamp, timestamp, timestamp)) conn.commit() @property diff --git a/swift/container/backend.py b/swift/container/backend.py index 8b00ef34ca..0915901a05 100644 --- a/swift/container/backend.py +++ b/swift/container/backend.py @@ -626,20 +626,6 @@ class ContainerBroker(DatabaseBroker): SET reported_put_timestamp = 0, reported_delete_timestamp = 0, reported_object_count = 0, reported_bytes_used = 0''') - def _delete_db(self, conn, timestamp): - """ - Mark the DB as deleted - - :param conn: DB connection object - :param timestamp: timestamp to mark as deleted - """ - conn.execute(""" - UPDATE container_stat - SET delete_timestamp = ?, - status = 'DELETED', - status_changed_at = ? - WHERE delete_timestamp < ? """, (timestamp, timestamp, timestamp)) - def _commit_puts_load(self, item_list, entry): """See :func:`swift.common.db.DatabaseBroker._commit_puts_load`""" (name, timestamp, size, content_type, etag, deleted) = entry[:6] diff --git a/test/unit/common/test_db.py b/test/unit/common/test_db.py index fee6902bb0..cfef1b55aa 100644 --- a/test/unit/common/test_db.py +++ b/test/unit/common/test_db.py @@ -231,6 +231,7 @@ class ExampleBroker(DatabaseBroker): delete_timestamp TEXT DEFAULT '0', hash TEXT default '00000000000000000000000000000000', id TEXT, + status TEXT DEFAULT '', status_changed_at TEXT DEFAULT '0', metadata TEXT DEFAULT '' ); @@ -253,10 +254,10 @@ class ExampleBroker(DatabaseBroker): ''') conn.execute(""" INSERT INTO test_stat ( - account, created_at, id, put_timestamp, status_changed_at) - VALUES (?, ?, ?, ?, ?); + account, created_at, id, put_timestamp, status_changed_at, status) + VALUES (?, ?, ?, ?, ?, ?); """, (self.account, Timestamp.now().internal, str(uuid4()), - put_timestamp, put_timestamp)) + put_timestamp, put_timestamp, '')) def merge_items(self, item_list): with self.get() as conn: @@ -307,6 +308,7 @@ class ExampleBroker(DatabaseBroker): conn.execute(""" UPDATE test_stat SET delete_timestamp = ?, + status = 'DELETED', status_changed_at = ? WHERE delete_timestamp < ? """, (timestamp, timestamp, timestamp)) @@ -715,38 +717,47 @@ class TestDatabaseBroker(unittest.TestCase): def test_delete_db(self): def init_stub(conn, put_timestamp, **kwargs): conn.execute('CREATE TABLE test (one TEXT)') - conn.execute('CREATE TABLE test_stat (id TEXT)') - conn.execute('INSERT INTO test_stat (id) VALUES (?)', - (str(uuid4),)) + conn.execute('''CREATE TABLE test_stat ( + id TEXT, put_timestamp TEXT, delete_timestamp TEXT, + status TEXT, status_changed_at TEXT, metadata TEXT)''') + meta = {'foo': ('bar', normalize_timestamp('0'))} + conn.execute( + '''INSERT INTO test_stat ( + id, put_timestamp, delete_timestamp, status, + status_changed_at, metadata) VALUES (?, ?, ?, ?, ?, ?)''', + (str(uuid4), put_timestamp, '0', '', '0', json.dumps(meta))) conn.execute('INSERT INTO test (one) VALUES ("1")') conn.commit() - stub_called = [False] - def delete_stub(*a, **kw): - stub_called[0] = True broker = DatabaseBroker(':memory:') broker.db_type = 'test' broker._initialize = init_stub # Initializes a good broker for us broker.initialize(normalize_timestamp('1')) + info = broker.get_info() + self.assertEqual('0', info['delete_timestamp']) + self.assertEqual('', info['status']) self.assertIsNotNone(broker.conn) - broker._delete_db = delete_stub - stub_called[0] = False - broker.delete_db('2') - self.assertTrue(stub_called[0]) + broker.delete_db(normalize_timestamp('2')) + info = broker.get_info() + self.assertEqual(normalize_timestamp('2'), info['delete_timestamp']) + self.assertEqual('DELETED', info['status']) + broker = DatabaseBroker(os.path.join(self.testdir, '1.db')) broker.db_type = 'test' broker._initialize = init_stub broker.initialize(normalize_timestamp('1')) - broker._delete_db = delete_stub - stub_called[0] = False - broker.delete_db('2') - self.assertTrue(stub_called[0]) + info = broker.get_info() + self.assertEqual('0', info['delete_timestamp']) + self.assertEqual('', info['status']) + broker.delete_db(normalize_timestamp('2')) + info = broker.get_info() + self.assertEqual(normalize_timestamp('2'), info['delete_timestamp']) + self.assertEqual('DELETED', info['status']) + # ensure that metadata was cleared m2 = broker.metadata - self.assertTrue(not any(v[0] for v in m2.values())) - self.assertTrue(all(v[1] == normalize_timestamp('2') - for v in m2.values())) + self.assertEqual(m2, {'foo': ['', normalize_timestamp('2')]}) def test_get(self): broker = DatabaseBroker(':memory:')