From be1deb7eb60a5a0b15f6ed37bc235ae66f7f1a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 7 Jul 2016 14:55:07 +0200 Subject: [PATCH] Moved MongoDB index creation to app startup. At the first request to Pillar, the application will create all missing indices. This also happens in unittests, so that we get the correct behaviour for 'unique' indices. --- pillar/application/__init__.py | 35 ++++++++++++++++++++++++++++++++++ pillar/manage.py | 19 +++--------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/pillar/application/__init__.py b/pillar/application/__init__.py index 58467059..bc76d834 100644 --- a/pillar/application/__init__.py +++ b/pillar/application/__init__.py @@ -207,6 +207,41 @@ app.on_fetched_item_notifications += before_returning_item_notifications app.on_fetched_resource_notifications += before_returning_resource_notifications +@app.before_first_request +def setup_db_indices(): + """Adds missing database indices. + + This does NOT drop and recreate existing indices, + nor does it reconfigure existing indices. + If you want that, drop them manually first. + """ + + log.debug('Adding missing database indices.') + + import pymongo + + db = app.data.driver.db + + coll = db['tokens'] + coll.create_index([('user', pymongo.ASCENDING)]) + coll.create_index([('token', pymongo.ASCENDING)]) + + coll = db['notifications'] + coll.create_index([('user', pymongo.ASCENDING)]) + + coll = db['activities-subscriptions'] + coll.create_index([('context_object', pymongo.ASCENDING)]) + + coll = db['nodes'] + # This index is used for queries on project, and for queries on + # the combination (project, node type). + coll.create_index([('project', pymongo.ASCENDING), + ('node_type', pymongo.ASCENDING)]) + coll.create_index([('parent', pymongo.ASCENDING)]) + coll.create_index([('short_codes', pymongo.ASCENDING)], + sparse=True, unique=True) + + # The encoding module (receive notification and report progress) from modules.encoding import encoding from modules.blender_id import blender_id diff --git a/pillar/manage.py b/pillar/manage.py index d211a9b6..ccaaf2f3 100755 --- a/pillar/manage.py +++ b/pillar/manage.py @@ -146,29 +146,16 @@ def setup_db(admin_email): def setup_db_indices(): """Adds missing database indices.""" + from application import setup_db_indices + import pymongo log.info('Adding missing database indices.') log.warning('This does NOT drop and recreate existing indices, ' 'nor does it reconfigure existing indices. ' 'If you want that, drop them manually first.') - db = app.data.driver.db - coll = db['tokens'] - coll.create_index([('user', pymongo.ASCENDING)]) - coll.create_index([('token', pymongo.ASCENDING)]) - coll = db['notifications'] - coll.create_index([('user', pymongo.ASCENDING)]) - - coll = db['activities-subscriptions'] - coll.create_index([('context_object', pymongo.ASCENDING)]) - - coll = db['nodes'] - # This index is used for queries on project, and for queries on - # the combination (project, node type). - coll.create_index([('project', pymongo.ASCENDING), - ('node_type', pymongo.ASCENDING)]) - coll.create_index([('parent', pymongo.ASCENDING)]) + setup_db_indices() coll_names = db.collection_names(include_system_collections=False) for coll_name in sorted(coll_names):