allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject allura git commit: [#8079] only drop extra mongo indexes if specified with --clean arg
Date Mon, 09 May 2016 19:21:29 GMT
Repository: allura
Updated Branches:
  refs/heads/master 78007dba6 -> a451b1506


[#8079] only drop extra mongo indexes if specified with --clean arg


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/a451b150
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/a451b150
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/a451b150

Branch: refs/heads/master
Commit: a451b15067853403bb9f7dab3ba5c25565a86b9a
Parents: 78007db
Author: Dave Brondsema <dave@brondsema.net>
Authored: Mon May 2 17:37:45 2016 -0400
Committer: Dave Brondsema <dave@brondsema.net>
Committed: Mon May 9 15:21:21 2016 -0400

----------------------------------------------------------------------
 Allura/allura/command/show_models.py | 20 +++++++++++++++-----
 Allura/allura/tests/test_commands.py | 19 +++++++++++++++++++
 2 files changed, 34 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/a451b150/Allura/allura/command/show_models.py
----------------------------------------------------------------------
diff --git a/Allura/allura/command/show_models.py b/Allura/allura/command/show_models.py
index 95f362b..b77bc0c 100644
--- a/Allura/allura/command/show_models.py
+++ b/Allura/allura/command/show_models.py
@@ -203,8 +203,10 @@ class EnsureIndexCommand(base.Command):
     min_args = 1
     max_args = 1
     usage = '[<ini file>]'
-    summary = 'Create all the Mongo indexes specified by Ming models, drop any unspecified
indexes.'
+    summary = 'Create all the Mongo indexes specified by Ming models'
     parser = base.Command.standard_parser(verbose=True)
+    parser.add_option('--clean', action='store_true', dest='clean',
+                      help='Drop any unneeded indexes')
 
     def command(self):
         from allura import model as M
@@ -298,12 +300,20 @@ class EnsureIndexCommand(base.Command):
         # Drop obsolete indexes
         for iname, keys in prev_indexes.iteritems():
             if keys not in indexes:
-                base.log.info('...... drop index %s:%s', collection.name, iname)
-                collection.drop_index(iname)
+                if self.options.clean:
+                    base.log.info('...... drop index %s:%s', collection.name, iname)
+                    collection.drop_index(iname)
+                else:
+                    base.log.info('...... potentially unneeded index, could be removed by
running with --clean %s:%s',
+                                  collection.name, iname)
         for iname, keys in prev_uindexes.iteritems():
             if keys not in uindexes:
-                base.log.info('...... drop index %s:%s', collection.name, iname)
-                collection.drop_index(iname)
+                if self.options.clean:
+                    base.log.info('...... drop index %s:%s', collection.name, iname)
+                    collection.drop_index(iname)
+                else:
+                    base.log.info('...... potentially unneeded index, could be removed by
running with --clean %s:%s',
+                                  collection.name, iname)
 
     def _recreate_index(self, collection, iname, keys, **creation_options):
         '''Recreate an index with new creation options, using a temporary index

http://git-wip-us.apache.org/repos/asf/allura/blob/a451b150/Allura/allura/tests/test_commands.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_commands.py b/Allura/allura/tests/test_commands.py
index fb5fb03..ee97f7e 100644
--- a/Allura/allura/tests/test_commands.py
+++ b/Allura/allura/tests/test_commands.py
@@ -17,6 +17,8 @@
 
 from nose.tools import assert_raises, assert_in
 from datadiff.tools import assert_equal
+
+from ming.base import Object
 from ming.orm import ThreadLocalORMSession
 from mock import Mock, call, patch
 import pymongo
@@ -180,6 +182,22 @@ class TestEnsureIndexCommand(object):
         cmd = show_models.EnsureIndexCommand('ensure_index')
         cmd.run([test_config])
 
+    def test_no_drop(self):
+        collection = Mock(name='collection')
+        collection.index_information.return_value = {
+            '_id_': {'key': '_id'},
+            '_foo_bar': {'key': [('foo', 1), ('bar', 1)]},
+        }
+        indexes = [
+            Mock(unique=False, index_spec=[('foo', 1)],
+                 index_options={'unique': False, 'sparse': False}),
+        ]
+        cmd = show_models.EnsureIndexCommand('ensure_index')
+        cmd.options = Object(clean=False)
+        cmd._update_indexes(collection, indexes)
+        assert collection.ensure_index.called
+        assert not collection.drop_index.called
+
     def test_update_indexes_order(self):
         collection = Mock(name='collection')
         collection.index_information.return_value = {
@@ -191,6 +209,7 @@ class TestEnsureIndexCommand(object):
                  index_options={'unique': False, 'sparse': False}),
         ]
         cmd = show_models.EnsureIndexCommand('ensure_index')
+        cmd.options = Object(clean=True)
         cmd._update_indexes(collection, indexes)
 
         collection_call_order = {}


Mime
View raw message