Skip to content

Node.js based forum software built for the modern web

fix: closes #14289, index conflicts in createIndices

Barış Soner Uşaklıe73e0d1a3873
diff --git a/src/database/mongo.js b/src/database/mongo.js
index 4179111812..b8a84696be 100644
--- a/src/database/mongo.js
+++ b/src/database/mongo.js
@@ -83,20 +83,50 @@ mongoModule.createIndices = async function () {
}
winston.info('[database] Checking database indices.');
- const collection = mongoModule.client.collection('objects');
- await collection.createIndex({ _key: 1, score: -1 }, { background: true });
- await collection.createIndex({ _key: 1, value: -1 }, { background: true, unique: true, sparse: true });
- await collection.createIndex(
- { members: 1, _key: 1},
+ await safeCreateIndex({ _key: 1, score: -1 }, { background: true });
+ await safeCreateIndex({ _key: 1, value: -1 }, { background: true, unique: true, sparse: true });
+ await safeCreateIndex(
+ { members: 1, _key: 1 },
{ background: true, partialFilterExpression: { members: { $exists: true } } }
);
- await collection.createIndex(
+ await safeCreateIndex(
{ expireAt: 1 },
{ expireAfterSeconds: 0, background: true, partialFilterExpression: { expireAt: { $exists: true } } },
);
+
winston.info('[database] Checking database indices done!');
};
+function generateIndexName(indexSpec) {
+ return Object.entries(indexSpec)
+ .map(([key, value]) => `${key}_${value}`)
+ .join('_');
+}
+
+async function safeCreateIndex(indexSpec, options) {
+ try {
+ await mongoModule.client.collection('objects').createIndex(indexSpec, options);
+ } catch (err) {
+ if (err.code === 85) { // index options conflict, retry by dropping the index
+ await safeDropIndex(generateIndexName(indexSpec));
+ await safeCreateIndex(indexSpec, options);
+ return;
+ }
+ throw err;
+ }
+}
+
+async function safeDropIndex(indexName) {
+ try {
+ await mongoModule.client.collection('objects').dropIndex(indexName);
+ } catch (err) {
+ // Ignore "index not found (27)" error
+ if (err.code !== 27) {
+ throw err;
+ }
+ }
+};
+
mongoModule.checkCompatibility = function (callback) {
const mongoPkg = require('mongodb/package.json');
mongoModule.checkCompatibilityVersion(mongoPkg.version, callback);