From 8e87fecc4f9262ee290c0a148cdbfb214cc0417d Mon Sep 17 00:00:00 2001
From: drebs <drebs@leap.se>
Date: Mon, 22 Aug 2016 20:56:49 -0300
Subject: [test] avoid failing on interrupted couch schema migrations

---
 .../migration/0.8.2/migrate_couch_schema/__init__.py    | 17 +++++++++++++----
 scripts/migration/0.8.2/tests/conftest.py               |  4 ++++
 2 files changed, 17 insertions(+), 4 deletions(-)

(limited to 'scripts/migration')

diff --git a/scripts/migration/0.8.2/migrate_couch_schema/__init__.py b/scripts/migration/0.8.2/migrate_couch_schema/__init__.py
index edf671ae..f0b456e4 100644
--- a/scripts/migration/0.8.2/migrate_couch_schema/__init__.py
+++ b/scripts/migration/0.8.2/migrate_couch_schema/__init__.py
@@ -32,9 +32,7 @@ def _get_couch_server(couch_url):
 
 def _is_migrateable(db):
     config_doc = db.get('u1db_config')
-    if config_doc is None:
-        return False
-    return True
+    return bool(config_doc)
 
 
 def _get_transaction_log(db):
@@ -126,6 +124,8 @@ def _migrate_config_doc(db, do_migrate):
     logger.info("[%s] moving config doc: %s -> %s"
                 % (db.name, old_doc['_id'], new_doc['_id']))
     if do_migrate:
+        # the config doc must not exist, otherwise we would have skipped this
+        # database.
         db.save(new_doc)
         db.delete(old_doc)
 
@@ -167,7 +167,16 @@ def _migrate_sync_docs(db, do_migrate):
         logger.debug("[%s] moving sync doc: %s -> %s"
                      % (db.name, old_id, new_id))
         if do_migrate:
-            db.save(new_doc)
+            try:
+                db.save(new_doc)
+            except ResourceConflict:
+                # this sync document already exists. if documents are the same,
+                # continue with migration.
+                existing_doc = db.get(new_id)
+                for key in [GENERATION_KEY, TRANSACTION_ID_KEY,
+                            REPLICA_UID_KEY]:
+                    if existing_doc[key] != new_doc[key]:
+                        raise
             db.delete(old_doc)
 
 
diff --git a/scripts/migration/0.8.2/tests/conftest.py b/scripts/migration/0.8.2/tests/conftest.py
index 8e49891c..61f6c7ee 100644
--- a/scripts/migration/0.8.2/tests/conftest.py
+++ b/scripts/migration/0.8.2/tests/conftest.py
@@ -33,6 +33,10 @@ initial_docs = [
     {'_id': '_design/syncs'},
     {'_id': '_design/transactions',
      'views': {'log': {'map': transaction_map}}},
+    # add some data from previous interrupted migration
+    {'_id': '_local/sync_A', 'gen': 0, 'trans_id': '', 'replica_uid': 'A'},
+    {'_id': 'gen-0000000002',
+     'gen': 2, 'trans_id': 'trans-2', 'doc_id': 'doc2'},
     # the following should be removed if found in the dbs
     {'_id': 'u1db_sync_log'},
     {'_id': 'u1db_sync_state'},
-- 
cgit v1.2.3