From f6a7cdded4285af2335263a058479fa158980b31 Mon Sep 17 00:00:00 2001 From: NavaL Date: Fri, 29 Apr 2016 18:47:13 +0200 Subject: [bug] ensures docs_received table has the sync_id column For the case where the user already has data synced, this commit will migrate the docs_received table to have the column sync_id. That is required by the refactoring in the previous commits. --- client/src/leap/soledad/client/encdecpool.py | 12 ++++++-- .../leap/soledad/common/tests/test_encdecpool.py | 34 ++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/client/src/leap/soledad/client/encdecpool.py b/client/src/leap/soledad/client/encdecpool.py index 7d646c51..e348f545 100644 --- a/client/src/leap/soledad/client/encdecpool.py +++ b/client/src/leap/soledad/client/encdecpool.py @@ -369,14 +369,22 @@ class SyncDecrypterPool(SyncEncryptDecryptPool): def _init_db(self): """ + Ensure sync_id column is present then Empty the received docs table of the sync database. :return: A deferred that will fire when the operation in the database has finished. :rtype: twisted.internet.defer.Deferred """ - query = "DELETE FROM %s WHERE sync_id <> ?" % (self.TABLE_NAME,) - return self._runOperation(query, (self._sync_id,)) + ensure_sync_id_column = "ALTER TABLE %s ADD COLUMN sync_id" % self.TABLE_NAME + d = self._runQuery(ensure_sync_id_column) + + def empty_received_docs(_): + query = "DELETE FROM %s WHERE sync_id <> ?" % (self.TABLE_NAME,) + return self._runOperation(query, (self._sync_id,)) + + d.addCallbacks(empty_received_docs, empty_received_docs) + return d def _errback(self, failure): log.err(failure) diff --git a/common/src/leap/soledad/common/tests/test_encdecpool.py b/common/src/leap/soledad/common/tests/test_encdecpool.py index 6676c298..9d98f44d 100644 --- a/common/src/leap/soledad/common/tests/test_encdecpool.py +++ b/common/src/leap/soledad/common/tests/test_encdecpool.py @@ -20,6 +20,7 @@ Tests for encryption and decryption pool. import json from random import shuffle +from mock import MagicMock from twisted.internet.defer import inlineCallbacks from leap.soledad.client.encdecpool import SyncEncrypterPool @@ -27,7 +28,7 @@ from leap.soledad.client.encdecpool import SyncDecrypterPool from leap.soledad.common.document import SoledadDocument from leap.soledad.common.tests.util import BaseSoledadTest - +from twisted.internet import defer DOC_ID = "mydoc" DOC_REV = "rev" @@ -84,14 +85,18 @@ class TestSyncDecrypterPool(BaseSoledadTest): """ self._inserted_docs.append((doc, gen, trans_id)) - def setUp(self): - BaseSoledadTest.setUp(self) - # setup the pool - self._pool = SyncDecrypterPool( + def _setup_pool(self, sync_db=None): + sync_db = sync_db or self._soledad._sync_db + return SyncDecrypterPool( self._soledad._crypto, - self._soledad._sync_db, + sync_db, source_replica_uid=self._soledad._dbpool.replica_uid, insert_doc_cb=self._insert_doc_cb) + + def setUp(self): + BaseSoledadTest.setUp(self) + # setup the pool + self._pool = self._setup_pool() # reset the inserted docs mock self._inserted_docs = [] @@ -127,6 +132,23 @@ class TestSyncDecrypterPool(BaseSoledadTest): self.assertFalse(self._pool.running) self.assertTrue(self._pool.deferred.called) + def test_sync_id_column_is_created_if_non_existing_in_docs_received_table(self): + """ + Test that docs_received table is migrated, and has the sync_id column + """ + mock_run_query = MagicMock(return_value=defer.succeed(None)) + mock_sync_db = MagicMock() + mock_sync_db.runQuery = mock_run_query + pool = self._setup_pool(mock_sync_db) + d = pool.start(10) + pool.stop() + + def assert_trial_to_create_sync_id_column(_): + mock_run_query.assert_called_once_with("ALTER TABLE docs_received ADD COLUMN sync_id") + + d.addCallback(assert_trial_to_create_sync_id_column) + return d + def test_insert_received_doc_many(self): """ Test that many documents added to the pool are inserted using the -- cgit v1.2.3