From d5683afb84e712a5333fade3abe936370137948d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 7 Jul 2016 12:44:06 +0200 Subject: [PATCH] manager command '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. --- pillar/manage.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pillar/manage.py b/pillar/manage.py index a817ce01..d211a9b6 100755 --- a/pillar/manage.py +++ b/pillar/manage.py @@ -142,6 +142,41 @@ def setup_db(admin_email): 'is_private': False}) +@manager.command +def setup_db_indices(): + """Adds missing database 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)]) + + coll_names = db.collection_names(include_system_collections=False) + for coll_name in sorted(coll_names): + stats = db.command('collStats', coll_name) + log.info('Collection %25s takes up %.3f MiB index space', + coll_name, stats['totalIndexSize'] / 2**20) + + def _default_permissions(): """Returns a dict of default permissions.