Yuki Takei 1 год назад
Родитель
Сommit
34d7b291a5

+ 38 - 0
apps/app/src/migrations/19700101000000-foremost-1000-20241123211930-remove-ns-from-configs.js

@@ -0,0 +1,38 @@
+import mongoose from 'mongoose';
+
+import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
+import loggerFactory from '~/utils/logger';
+
+
+const logger = loggerFactory('growi:migrate:remove-ns-from-configs');
+
+async function dropIndexIfExists(db, collectionName, indexName) {
+  // check existence of the collection
+  const items = await db.listCollections({ name: collectionName }, { nameOnly: true }).toArray();
+  if (items.length === 0) {
+    return;
+  }
+
+  const collection = await db.collection(collectionName);
+  if (await collection.indexExists(indexName)) {
+    await collection.dropIndex(indexName);
+  }
+}
+
+module.exports = {
+  async up(db) {
+    logger.info('Apply migration');
+    await mongoose.connect(getMongoUri(), mongoOptions);
+
+    // drop index
+    await dropIndexIfExists(db, 'configs', 'ns_1_key_1');
+
+    // create index
+    const collection = await db.collection('configs');
+    await collection.createIndex({ key: 1 }, { unique: true });
+  },
+
+  async down() {
+    // No rollback
+  },
+};

+ 2 - 6
apps/app/src/migrations/20180927102719-init-serverurl.js

@@ -24,7 +24,6 @@ module.exports = {
 
     // find 'app:siteUrl'
     const siteUrlConfig = await Config.findOne({
-      ns: 'crowi',
       key: 'app:siteUrl',
     });
     // exit if exists
@@ -35,7 +34,6 @@ module.exports = {
 
     // find all callbackUrls
     const configs = await Config.find({
-      ns: 'crowi',
       $or: [
         { key: 'security:passport-github:callbackUrl' },
         { key: 'security:passport-google:callbackUrl' },
@@ -63,11 +61,10 @@ module.exports = {
     }
 
     if (siteUrl != null) {
-      const ns = 'crowi';
       const key = 'app:siteUrl';
       await Config.findOneAndUpdate(
-        { ns, key },
-        { ns, key, value: JSON.stringify(siteUrl) },
+        { key },
+        { key, value: JSON.stringify(siteUrl) },
         { upsert: true },
       );
       logger.info('Migration has successfully applied');
@@ -80,7 +77,6 @@ module.exports = {
 
     // remote 'app:siteUrl'
     await Config.findOneAndDelete({
-      ns: 'crowi',
       key: 'app:siteUrl',
     });
 

+ 6 - 6
apps/app/src/migrations/20190618055300-abolish-crowi-classic-auth.js

@@ -15,18 +15,18 @@ module.exports = {
     // enable passport and delete configs for crowi classic auth
     await Promise.all([
       Config.findOneAndUpdate(
-        { ns: 'crowi', key: 'security:isEnabledPassport' },
-        { ns: 'crowi', key: 'security:isEnabledPassport', value: JSON.stringify(true) },
+        { key: 'security:isEnabledPassport' },
+        { key: 'security:isEnabledPassport', value: JSON.stringify(true) },
         { upsert: true },
       ),
       Config.findOneAndUpdate(
-        { ns: 'crowi', key: 'google:clientId' },
-        { ns: 'crowi', key: 'google:clientId', value: JSON.stringify(null) },
+        { key: 'google:clientId' },
+        { key: 'google:clientId', value: JSON.stringify(null) },
         { upsert: true },
       ),
       Config.findOneAndUpdate(
-        { ns: 'crowi', key: 'google:clientSecret' },
-        { ns: 'crowi', key: 'google:clientSecret', value: JSON.stringify(null) },
+        { key: 'google:clientSecret' },
+        { key: 'google:clientSecret', value: JSON.stringify(null) },
         { upsert: true },
       ),
     ]);

+ 3 - 6
apps/app/src/migrations/20190618104011-add-config-app-installed.js

@@ -9,9 +9,9 @@ const logger = loggerFactory('growi:migrate:add-config-app-installed');
 
 /**
  * BEFORE
- *   - Config document { ns: 'crowi', key: 'app:installed' } does not exist
+ *   - Config document { key: 'app:installed' } does not exist
  * AFTER
- *   - Config document { ns: 'crowi', key: 'app:installed' } is created
+ *   - Config document { key: 'app:installed' } is created
  *     - value will be true if one or more users exist
  *     - value will be false if no users exist
  */
@@ -23,9 +23,8 @@ module.exports = {
 
     const User = userModelFactory();
 
-    // find 'app:siteUrl'
+    // find 'app:installed'
     const appInstalled = await Config.findOne({
-      ns: 'crowi',
       key: 'app:installed',
     });
     // exit if exists
@@ -38,7 +37,6 @@ module.exports = {
 
     if (userCount > 0) {
       await Config.create({
-        ns: 'crowi',
         key: 'app:installed',
         value: true,
       });
@@ -53,7 +51,6 @@ module.exports = {
 
     // remote 'app:siteUrl'
     await Config.findOneAndDelete({
-      ns: 'crowi',
       key: 'app:installed',
     });
 

+ 0 - 1
apps/app/src/migrations/20200512005851-remove-behavior-type.js

@@ -23,7 +23,6 @@ module.exports = {
     await mongoose.connect(getMongoUri(), mongoOptions);
 
     const insertConfig = new Config({
-      ns: 'crowi',
       key: 'customize:behavior',
       value: JSON.stringify('growi'),
     });

+ 0 - 1
apps/app/src/migrations/20200827045151-remove-layout-setting.js

@@ -45,7 +45,6 @@ module.exports = {
     const insertLayoutType = (theme.value === '"kibela"') ? 'kibela' : 'growi';
 
     const insertConfig = new Config({
-      ns: 'crowi',
       key: 'customize:layout',
       value: JSON.stringify(insertLayoutType),
     });

+ 0 - 2
apps/app/src/migrations/20200828024025-copy-aws-setting.js

@@ -25,7 +25,6 @@ module.exports = {
           insertOne: {
             document: {
               key: 'mail:sesAccessKeyId',
-              ns: 'crowi',
               value: accessKeyId.value,
             },
           },
@@ -39,7 +38,6 @@ module.exports = {
           insertOne: {
             document: {
               key: 'mail:sesSecretAccessKey',
-              ns: 'crowi',
               value: secretAccessKey.value,
             },
           },

+ 0 - 3
apps/app/src/migrations/20200901034313-update-mail-transmission.js

@@ -13,11 +13,9 @@ module.exports = {
     await mongoose.connect(getMongoUri(), mongoOptions);
 
     const sesAccessKeyId = await Config.findOne({
-      ns: 'crowi',
       key: 'mail:sesAccessKeyId',
     });
     const transmissionMethod = await Config.findOne({
-      ns: 'crowi',
       key: 'mail:transmissionMethod',
     });
 
@@ -47,7 +45,6 @@ module.exports = {
 
     // remote 'mail:transmissionMethod'
     await Config.findOneAndDelete({
-      ns: 'crowi',
       key: 'mail:transmissionMethod',
     });
 

+ 0 - 1
apps/app/src/migrations/20200903080025-remove-timeline-type.js.js

@@ -24,7 +24,6 @@ module.exports = {
     await mongoose.connect(getMongoUri(), mongoOptions);
 
     const insertConfig = new Config({
-      ns: 'crowi',
       key: 'customize:isEnabledTimeline',
       value: true,
     });

+ 0 - 5
apps/app/src/migrations/20220311011114-convert-page-delete-config.js

@@ -15,7 +15,6 @@ module.exports = {
     await mongoose.connect(getMongoUri(), mongoOptions);
 
     const isNewConfigExists = await Config.count({
-      ns: 'crowi',
       key: 'security:pageDeletionAuthority',
     }) > 0;
 
@@ -26,7 +25,6 @@ module.exports = {
     }
 
     const oldConfig = await Config.findOne({
-      ns: 'crowi',
       key: 'security:pageCompleteDeletionAuthority',
     });
 
@@ -37,17 +35,14 @@ module.exports = {
       await Config.insertMany(
         [
           {
-            ns: 'crowi',
             key: 'security:pageDeletionAuthority',
             value: oldValue,
           },
           {
-            ns: 'crowi',
             key: 'security:pageRecursiveDeletionAuthority',
             value: `"${PageRecursiveDeleteConfigValue.Inherit}"`,
           },
           {
-            ns: 'crowi',
             key: 'security:pageRecursiveCompleteDeletionAuthority',
             value: `"${PageRecursiveDeleteCompConfigValue.Inherit}"`,
           },

+ 0 - 1
apps/app/src/migrations/20221014130200-remove-customize-is-saved-states-of-tab-changes.js

@@ -23,7 +23,6 @@ module.exports = {
     await mongoose.connect(getMongoUri(), mongoOptions);
 
     const insertConfig = new Config({
-      ns: 'crowi',
       key: 'customize:isSavedStatesOfTabChanges',
       value: false,
     });

+ 0 - 1
apps/app/src/migrations/20230213090921-remove-presentation-configurations.js

@@ -24,7 +24,6 @@ module.exports = {
     await mongoose.connect(getMongoUri(), mongoOptions);
 
     const insertConfig = new Config({
-      ns: 'crowi',
       key: 'markdown:presentation:pageBreakSeparator',
       value: 2,
     });

+ 1 - 5
apps/app/src/server/models/config.ts

@@ -9,7 +9,6 @@ import { getOrCreateModel } from '../util/mongoose-utils';
 
 export interface IConfig {
   _id: Types.ObjectId;
-  ns: string;
   key: string;
   value: string;
   createdAt: Date;
@@ -22,15 +21,12 @@ interface ModelMethods { any }
 
 
 const schema = new Schema<IConfig>({
-  ns: { type: String, required: true },
-  key: { type: String, required: true },
+  key: { type: String, required: true, unique: true },
   value: { type: String, required: true },
 }, {
   timestamps: true,
 });
 
-// define unique compound index
-schema.index({ ns: 1, key: 1 }, { unique: true });
 schema.plugin(uniqueValidator);
 
 /**

+ 0 - 1
apps/app/test/integration/service/questionnaire.test.ts

@@ -21,7 +21,6 @@ describe('QuestionnaireService', () => {
     });
 
     await mongoose.model('Config').create({
-      ns: 'crowi',
       key: 'app:installed',
       value: true,
       createdAt: '2000-01-01',