|
|
@@ -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');
|