Browse Source

imprv: enable config timestamp
prepare migration

Tatsuya Ise 2 years ago
parent
commit
7d43f0f0f2

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

@@ -0,0 +1,38 @@
+// eslint-disable-next-line import/no-named-as-default
+import Config from '~/server/models/config';
+import User from '~/server/models/user';
+import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
+import loggerFactory from '~/utils/logger';
+
+
+const logger = loggerFactory('growi:remove-presentation-configurations');
+
+const mongoose = require('mongoose');
+
+module.exports = {
+  async up() {
+    logger.info('Apply migration');
+    mongoose.connect(getMongoUri(), mongoOptions);
+
+    const appInstalled = await Config.findOne({ key: 'app:installed' });
+    if (appInstalled.createdAt == null) {
+      const initialUser = await User.find().limit(1).sort({ createdAt: 1 });
+      appInstalled.createdAt = initialUser.createdAt;
+      await appInstalled.save();
+    }
+
+    logger.info('Migration has successfully applied');
+  },
+
+  async down(db, client) {
+    logger.info('Rollback migration');
+    mongoose.connect(getMongoUri(), mongoOptions);
+
+    const appInstalled = await Config.findOne({ key: 'app:installed' });
+    appInstalled.createdAt = null;
+
+    await appInstalled.save();
+
+    logger.info('Migration has been successfully rollbacked');
+  },
+};