This is an automated email from the ASF dual-hosted git repository.

kentontaylor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 5392f636e80cb37b1a30d0b7d3d47445f06c88ed
Author: Dave Brondsema <[email protected]>
AuthorDate: Thu Mar 6 11:57:23 2025 -0500

    ensure_index: require explicit --delete-duplicate-key-records to delete 
records
---
 Allura/allura/command/show_models.py | 11 +++++++++--
 Allura/allura/tests/test_commands.py |  4 ++--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/Allura/allura/command/show_models.py 
b/Allura/allura/command/show_models.py
index 6702ba990..e39677225 100644
--- a/Allura/allura/command/show_models.py
+++ b/Allura/allura/command/show_models.py
@@ -18,6 +18,7 @@
 from collections import defaultdict
 from contextlib import contextmanager
 from itertools import groupby
+import sys
 
 from paste.deploy.converters import asbool
 from tg import tmpl_context as c, app_globals as g
@@ -213,6 +214,8 @@ class EnsureIndexCommand(base.Command):
     parser = base.Command.standard_parser(verbose=True)
     parser.add_option('--clean', action='store_true', dest='clean',
                       help='Drop any unneeded indexes')
+    parser.add_option('--delete-duplicate-key-records', action='store_true', 
dest='delete_dupes',
+                      help='Delete records that violate unique indexes')
 
     def command(self):
         from allura import model as M
@@ -304,8 +307,12 @@ def _update_indexes(self, collection, indexes):
                     collection.create_index(idx.index_spec, **index_options)
                     break
                 except DuplicateKeyError as err:
-                    base.log.info('Found dupe key(%s), eliminating dupes', err)
-                    self._remove_dupes(collection, idx.index_spec)
+                    if self.options.delete_dupes:
+                        base.log.warning('Found dupe key(%s), eliminating 
dupes', err)
+                        self._remove_dupes(collection, idx.index_spec)
+                    else:
+                        print('Error creating unique index.  Run with 
--delete-duplicate-key-records if you want to delete records that violate this 
index', file=sys.stderr)
+                        raise
         for keys, idx in indexes.items():
             base.log.info('...... ensure %s:%s', collection.name, idx)
             collection.create_index(idx.index_spec, background=True, 
**idx.index_options)
diff --git a/Allura/allura/tests/test_commands.py 
b/Allura/allura/tests/test_commands.py
index 994b44d33..beb4f494c 100644
--- a/Allura/allura/tests/test_commands.py
+++ b/Allura/allura/tests/test_commands.py
@@ -197,7 +197,7 @@ def test_no_drop(self):
                  index_options={'unique': False, 'sparse': False}),
         ]
         cmd = show_models.EnsureIndexCommand('ensure_index')
-        cmd.options = Object(clean=False)
+        cmd.options = Object(clean=False, delete_dupes=False)
         cmd._update_indexes(collection, indexes)
         assert collection.create_index.called
         assert not collection.drop_index.called
@@ -213,7 +213,7 @@ def test_update_indexes_order(self):
                  index_options={'unique': False, 'sparse': False}),
         ]
         cmd = show_models.EnsureIndexCommand('ensure_index')
-        cmd.options = Object(clean=True)
+        cmd.options = Object(clean=True, delete_dupes=False)
         cmd._update_indexes(collection, indexes)
 
         collection_call_order = {}

Reply via email to