diff --git a/circle.yml b/circle.yml index eeeb00653c..fbbc7ab8f3 100644 --- a/circle.yml +++ b/circle.yml @@ -20,6 +20,9 @@ dependencies: # We install and test with all the supported MongoDB versions - case $CIRCLE_NODE_INDEX in [1,2]) sudo service mongodb stop ; MONGODB_VERSION=3.2.12 sudo -E .circle/install-and-run-mongodb.sh > /tmp/mongodb-install.log 2>&1 ;; [3,4]) sudo service mongodb stop ; MONGODB_VERSION=3.4.2 sudo -E .circle/install-and-run-mongodb.sh > /tmp/mongodb-install.log 2>&1 ;; esac: background: true + # We sleep a bit to wait for the background process to start and script to + # finish + - case $CIRCLE_NODE_INDEX in [1,2]) sleep 10 ;; esac # Tail the logs so it's easier to see what is going on. Sadly when using "background: true" # whole output is ignored. - case $CIRCLE_NODE_INDEX in [1,2]) tail -50 /tmp/mongodb-install.log ;; [3,4]) tail -50 /tmp/mongodb-install.log ;; esac diff --git a/st2common/st2common/models/db/__init__.py b/st2common/st2common/models/db/__init__.py index c0be7d6705..e131d4ecdc 100644 --- a/st2common/st2common/models/db/__init__.py +++ b/st2common/st2common/models/db/__init__.py @@ -118,6 +118,17 @@ def db_ensure_indexes(): try: model_class.ensure_indexes() + except OperationFailure as e: + # Special case for "uid" index. MongoDB 3.4 has dropped "_types" index option so we + # need to re-create the index to make it work and avoid "index with different options + # already exists" error. + # Note: This condition would only be encountered when upgrading existing StackStorm + # installation from MongoDB 3.2 to 3.4. + msg = str(e) + if 'already exists with different options' in msg and 'uid_1' in msg: + drop_obsolete_types_indexes(model_class=model_class) + else: + raise e except Exception as e: tb_msg = traceback.format_exc() msg = 'Failed to ensure indexes for model "%s": %s' % (class_name, str(e)) @@ -157,6 +168,32 @@ def cleanup_extra_indexes(model_class): return removed_count +def drop_obsolete_types_indexes(model_class): + """ + Special class for droping offending "types" indexes for which support has + been removed in mongoengine and MongoDB 3.4. + For more info, see: http://docs.mongoengine.org/upgrade.html#inheritance + """ + class_name = model_class.__name__ + + LOG.debug('Dropping obsolete types index for model "%s"' % (class_name)) + collection = model_class._get_collection() + collection.update({}, {'$unset': {'_types': 1}}, multi=True) + + info = collection.index_information() + indexes_to_drop = [key for key, value in info.iteritems() + if '_types' in dict(value['key']) or 'types' in value] + + LOG.debug('Will drop obsolete types indexes for model "%s": %s' % (class_name, + str(indexes_to_drop))) + + for index in indexes_to_drop: + collection.drop_index(index) + + LOG.debug('Recreating indexes for model "%s"' % (class_name)) + model_class.ensure_indexes() + + def db_teardown(): mongoengine.connection.disconnect()