Преглед изворни кода

imprv: enable config timestamp
fix migration correctly

Tatsuya Ise пре 2 година
родитељ
комит
988bc0ce6f

+ 2 - 0
apps/app/config/logger/config.dev.js

@@ -29,6 +29,8 @@ module.exports = {
   'growi:service:search-delegator:elasticsearch': 'debug',
   'growi:service:g2g-transfer': 'debug',
 
+  'growi:migration:add-installed-date-to-config': 'debug',
+
   /*
    * configure level for client
    */

+ 20 - 9
apps/app/src/migrations/20230731075753-add_installed_date_to_config.js

@@ -1,10 +1,10 @@
 // eslint-disable-next-line import/no-named-as-default
-import Config from '~/server/models/config';
+import ConfigModel from '~/server/models/config';
 import { getModelSafely, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
 import loggerFactory from '~/utils/logger';
 
 
-const logger = loggerFactory('growi:remove-presentation-configurations');
+const logger = loggerFactory('growi:migration:add-installed-date-to-config');
 
 const mongoose = require('mongoose');
 
@@ -12,14 +12,21 @@ module.exports = {
   async up() {
     logger.info('Apply migration');
     mongoose.connect(getMongoUri(), mongoOptions);
-
+    const Config = getModelSafely('Config') || ConfigModel;
     const User = getModelSafely('User') || require('~/server/models/user')();
 
     const appInstalled = await Config.findOne({ key: 'app:installed' });
     if (appInstalled != null && appInstalled.createdAt == null) {
-      const initialUser = await User.find().limit(1).sort({ createdAt: 1 });
-      appInstalled.createdAt = initialUser.createdAt;
-      await appInstalled.save();
+      // Get the oldest user who probably installed this GROWI.
+      const users = await User.find().limit(1).sort({ createdAt: 1 });
+      const initialUserCreatedAt = users[0].createdAt;
+      logger.debug('initialUserCreatedAt: ', initialUserCreatedAt);
+      const updatedConfig = await Config.findOneAndUpdate({ _id: appInstalled._id }, { createdAt: initialUserCreatedAt }, {
+        new: true,
+        timestamps: false,
+        strict: false,
+      });
+      logger.debug('updatedConfig: ', updatedConfig);
     }
 
     logger.info('Migration has successfully applied');
@@ -28,12 +35,16 @@ module.exports = {
   async down() {
     logger.info('Rollback migration');
     mongoose.connect(getMongoUri(), mongoOptions);
+    const Config = getModelSafely('Config') || ConfigModel;
 
     const appInstalled = await Config.findOne({ key: 'app:installed' });
     if (appInstalled != null) {
-      appInstalled.createdAt = null;
-
-      await appInstalled.save();
+      const updatedConfig = await Config.findOneAndUpdate({ _id: appInstalled._id }, { $unset: { createdAt: 1 } }, {
+        new: true,
+        timestamps: false,
+        strict: false,
+      });
+      logger.debug('updatedConfig: ', updatedConfig);
     }
 
     logger.info('Migration has been successfully rollbacked');