From af38c54d394b2e15f541bce9e4178ac9ca953aa7 Mon Sep 17 00:00:00 2001
From: Ambrus Simon <ambrussimon@invenshure.com>
Date: Fri, 16 Jun 2017 17:16:06 +0200
Subject: [PATCH] add db schema migration function

---
 bin/database.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/bin/database.py b/bin/database.py
index 0ff1bdd8..c830602a 100755
--- a/bin/database.py
+++ b/bin/database.py
@@ -1116,6 +1116,28 @@ def upgrade_to_31():
     config.db.sessions.update_many({'subject.lastname_hash': {'$exists': True}}, {'$unset': {'subject.lastname_hash':""}})
 
 
+def upgrade_to_32_closure(cont, cont_name):
+    cont_type = cont_name[:-1]
+    for analysis in cont['analyses']:
+        analysis['_id'] = bson.ObjectId(analysis['_id'])
+        analysis['parent'] = {'type': cont_type, 'id': cont['_id']}
+        analysis['permissions'] = cont_type['permissions'] = cont['permissions']
+    config.db['analyses'].insert_many(cont['analyses'])
+    config.db[cont_name].update_one(
+        {'_id': cont['_id']},
+        {'$unset': {'analyses': ''}})
+    return True
+
+
+def upgrade_to_32():
+    """
+    scitran/core issue #808 - make analyses use their own collection
+    """
+    for cont_name in ['projects', 'sessions', 'acquisitions', 'collections']:
+        cursor = config.db[cont_name].find({'analyses': {'$exists': True}})
+        process_cursor(cursor, upgrade_to_31_closure, context=cont_name)
+
+
 def upgrade_schema(force_from = None):
     """
     Upgrades db to the current schema version
-- 
GitLab