Shun Miyazawa пре 3 година
родитељ
комит
7f1d3671d3
1 измењених фајлова са 10 додато и 6 уклоњено
  1. 10 6
      packages/app/src/server/service/activity.ts

+ 10 - 6
packages/app/src/server/service/activity.ts

@@ -51,16 +51,20 @@ class ActivityService {
     const collection = mongoose.connection.collection('activities');
 
     try {
+      const targetField = 'createdAt_1';
+
       const indexes = await collection.indexes();
-      const ttlIndex = indexes.find(i => i.name === 'createdAt_1');
-      const shoudCreateIndex = ttlIndex == null;
-      const shoudDropAndCreateIndex = ttlIndex != null && ttlIndex.expireAfterSeconds !== activityExpirationSeconds;
+      const foundCreatedAt = indexes.find(i => i.name === targetField);
+
+      const isNotSpec = foundCreatedAt?.expireAfterSeconds == null || foundCreatedAt?.expireAfterSeconds !== activityExpirationSeconds;
+      const shoudDropIndex = foundCreatedAt != null && isNotSpec;
+      const shoudCreateIndex = foundCreatedAt == null || shoudDropIndex;
 
-      if (shoudDropAndCreateIndex) {
-        await collection.dropIndex('createdAt_1');
+      if (shoudDropIndex) {
+        await collection.dropIndex(targetField);
       }
 
-      if (shoudDropAndCreateIndex || shoudCreateIndex) {
+      if (shoudCreateIndex) {
         await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: activityExpirationSeconds });
       }
     }