Przeglądaj źródła

if index already exists, dropIndex() and createIndex()

Shun Miyazawa 3 lat temu
rodzic
commit
4d3cf30175
1 zmienionych plików z 6 dodań i 0 usunięć
  1. 6 0
      packages/app/src/server/service/activity.ts

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

@@ -58,6 +58,12 @@ class ActivityService {
     const collection = mongoose.connection.collection('activities');
 
     try {
+      const indexes = await collection.getIndexes();
+      const isExistTargetField = Object.keys(indexes).includes('createdAt_1');
+      if (isExistTargetField) {
+        await collection.dropIndex('createdAt_1');
+      }
+
       // Set retention period only if activityExpirationSeconds is not null
       await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: activityExpirationSeconds });
     }