Просмотр исходного кода

Merge pull request #7936 from weseek/imprv/124027-127339-add-timestamp-config

imprv: Persist the installed date in the Config collection
Yuki Takei 2 лет назад
Родитель
Сommit
262074af70

+ 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
    */

+ 58 - 0
apps/app/src/migrations/20230731075753-add_installed_date_to_config.js

@@ -0,0 +1,58 @@
+// eslint-disable-next-line import/no-named-as-default
+import ConfigModel from '~/server/models/config';
+import { getModelSafely, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
+import loggerFactory from '~/utils/logger';
+
+
+const logger = loggerFactory('growi:migration:add-installed-date-to-config');
+
+const mongoose = require('mongoose');
+
+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) {
+      // 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);
+
+      // Set app:installed date.
+      // refs: https://mongoosejs.com/docs/6.x/docs/timestamps.html#disabling-timestamps
+      //       Read the section after "Disabling timestamps also lets you set timestamps yourself..."
+      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');
+  },
+
+  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) {
+
+      // Unset app:installed date.
+      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');
+  },
+};

+ 3 - 0
apps/app/src/server/models/config.ts

@@ -23,7 +23,10 @@ const schema = new Schema<Config>({
   ns: { type: String, required: true },
   key: { type: String, required: true },
   value: { type: String, required: true },
+}, {
+  timestamps: true,
 });
+
 // define unique compound index
 schema.index({ ns: 1, key: 1 }, { unique: true });
 schema.plugin(uniqueValidator);