From 32c0c3f8b72c5c007fd86f273650077f55e71d7c Mon Sep 17 00:00:00 2001 From: Uwe Ilgenstein Date: Wed, 11 Dec 2024 12:06:41 +0100 Subject: [PATCH 1/2] remove unneeded migrations --- .../mikro-orm/Migration20241112163538.ts | 31 ------ .../mikro-orm/Migration20241113152001.ts | 102 ------------------ .../mikro-orm/Migration20241127195120.ts | 15 --- .../mikro-orm/Migration20241128155801.ts | 61 ----------- 4 files changed, 209 deletions(-) delete mode 100644 apps/server/src/migrations/mikro-orm/Migration20241112163538.ts delete mode 100644 apps/server/src/migrations/mikro-orm/Migration20241113152001.ts delete mode 100644 apps/server/src/migrations/mikro-orm/Migration20241127195120.ts delete mode 100644 apps/server/src/migrations/mikro-orm/Migration20241128155801.ts diff --git a/apps/server/src/migrations/mikro-orm/Migration20241112163538.ts b/apps/server/src/migrations/mikro-orm/Migration20241112163538.ts deleted file mode 100644 index 26e50089fd7..00000000000 --- a/apps/server/src/migrations/mikro-orm/Migration20241112163538.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Migration } from '@mikro-orm/migrations-mongodb'; - -export class Migration20241112163538 extends Migration { - async up(): Promise { - const collection = this.getCollection('room-members'); - - await collection.updateMany({ roomId: { $type: 'string' } }, [ - { - $set: { - roomId: { - $convert: { - input: '$roomId', - to: 'objectId', - onError: '$roomId', // Keep the original value if conversion fails - onNull: '$roomId', // Keep the original value if the input is null - }, - }, - }, - }, - ]); - console.info('Converted roomId from string to ObjectId'); - - await collection.updateMany({}, { $rename: { roomId: 'room' } }); - console.info('Renamed roomId to room'); - } - - async down(): Promise { - await Promise.resolve(); - console.error(`Migration down not implemented. You might need to restore database from backup!`); - } -} diff --git a/apps/server/src/migrations/mikro-orm/Migration20241113152001.ts b/apps/server/src/migrations/mikro-orm/Migration20241113152001.ts deleted file mode 100644 index 9914935c0a9..00000000000 --- a/apps/server/src/migrations/mikro-orm/Migration20241113152001.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { Migration } from '@mikro-orm/migrations-mongodb'; - -export class Migration20241113152001 extends Migration { - async up(): Promise { - const roomsToSchoolView = [ - { - $lookup: { - from: 'rooms', - localField: 'room', - foreignField: '_id', - as: 'roomDetails', - }, - }, - { - $unwind: '$roomDetails', - }, - { - $match: { - 'roomDetails.school': { $exists: false, $eq: null }, - }, - }, - { - $lookup: { - from: 'groups', - localField: 'userGroup', - foreignField: '_id', - as: 'groupDetails', - }, - }, - { - $unwind: '$groupDetails', - }, - { - $unwind: '$groupDetails.users', - }, - { - $lookup: { - from: 'roles', - localField: 'groupDetails.users.role', - foreignField: '_id', - as: 'roleDetails', - }, - }, - { - $unwind: '$roleDetails', - }, - { - $match: { - 'roleDetails.name': 'roomeditor', - }, - }, - { - $lookup: { - from: 'users', - localField: 'groupDetails.users.user', - foreignField: '_id', - as: 'userDetails', - }, - }, - { - $unwind: '$userDetails', - }, - { - $group: { - _id: '$userDetails.schoolId', - rooms: { $push: '$roomDetails._id' }, - }, - }, - { - $project: { - _id: 0, - school: '$_id', - rooms: 1, - }, - }, - ]; - - const mappings = await this.driver.aggregate('room-members', roomsToSchoolView); - - for await (const mapping of mappings) { - const schoolUpdate = await this.driver.nativeUpdate( - 'rooms', - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access - { _id: { $in: mapping.rooms } }, - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access - { $set: { school: mapping.school } } - ); - - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call - console.info(`Updated ${schoolUpdate.affectedRows} rooms with school ${mapping.school.toHexString()}`); - } - - if (mappings.length === 0) { - console.info(`No rooms without school to update`); - } - } - - async down(): Promise { - await Promise.resolve(); - console.error(`Migration down not implemented. You might need to restore database from backup!`); - } -} diff --git a/apps/server/src/migrations/mikro-orm/Migration20241127195120.ts b/apps/server/src/migrations/mikro-orm/Migration20241127195120.ts deleted file mode 100644 index 0b74ac61e63..00000000000 --- a/apps/server/src/migrations/mikro-orm/Migration20241127195120.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Migration } from '@mikro-orm/migrations-mongodb'; - -export class Migration20241127195120 extends Migration { - async up(): Promise { - const db = this.driver.getConnection().getDb(); - await db.renameCollection('room-members', 'room-memberships'); - console.info('Collection renamed from room-members to room-memberships'); - } - - async down(): Promise { - const db = this.driver.getConnection().getDb(); - await db.renameCollection('room-memberships', 'room-members'); - console.info('Collection renamed from room-memberships to room-members'); - } -} diff --git a/apps/server/src/migrations/mikro-orm/Migration20241128155801.ts b/apps/server/src/migrations/mikro-orm/Migration20241128155801.ts deleted file mode 100644 index 725ef54ecd3..00000000000 --- a/apps/server/src/migrations/mikro-orm/Migration20241128155801.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Migration } from '@mikro-orm/migrations-mongodb'; - -export class Migration20241128155801 extends Migration { - async up(): Promise { - const roomMembershipToSchoolView = [ - { - $match: { - school: { $exists: false, $eq: null }, - }, - }, - { - $lookup: { - from: 'rooms', - localField: 'room', - foreignField: '_id', - as: 'roomDetails', - }, - }, - { - $unwind: '$roomDetails', - }, - { - $group: { - _id: '$roomDetails.school', - roomMemberships: { $push: '$_id' }, - }, - }, - { - $project: { - _id: 0, - school: '$_id', - roomMemberships: 1, - }, - }, - ]; - - const mappings = await this.driver.aggregate('room-memberships', roomMembershipToSchoolView); - - for await (const mapping of mappings) { - const schoolUpdate = await this.driver.nativeUpdate( - 'room-memberships', - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access - { _id: { $in: mapping.roomMemberships } }, - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access - { $set: { school: mapping.school } } - ); - - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call - console.info(`Updated ${schoolUpdate.affectedRows} rooms with school ${mapping.school.toHexString()}`); - } - - if (mappings.length === 0) { - console.info(`No roomMemberships without school to update`); - } - } - - async down(): Promise { - await Promise.resolve(); - console.error(`Migration down not implemented. You might need to restore database from backup!`); - } -} From 0df50c1d03a91882e6e6196e2b351777f460ffac Mon Sep 17 00:00:00 2001 From: Uwe Ilgenstein Date: Wed, 11 Dec 2024 12:20:53 +0100 Subject: [PATCH 2/2] remove migrations from seeds --- backup/setup/migrations.json | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/backup/setup/migrations.json b/backup/setup/migrations.json index 980709a2218..d99686e576e 100644 --- a/backup/setup/migrations.json +++ b/backup/setup/migrations.json @@ -269,15 +269,6 @@ "$date": "2024-11-11T16:59:21.768Z" } }, - { - "_id": { - "$oid": "673387c13aba1e283484119d" - }, - "name": "Migration20241112163538", - "created_at": { - "$date": "2024-11-12T16:52:17.292Z" - } - }, { "_id": { "$oid": "67347bb8b1bcb78aecbab90f" @@ -287,15 +278,6 @@ "$date": "2024-11-13T10:13:12.411Z" } }, - { - "_id": { - "$oid": "67361aa7776f2f3e5a519735" - }, - "name": "Migration20241113152001", - "created_at": { - "$date": "2024-11-14T15:43:35.024Z" - } - }, { "_id": { "$oid": "674444262ba8186272dc8abd" @@ -314,24 +296,6 @@ "$date": "2024-11-28T10:37:51.515Z" } }, - { - "_id": { - "$oid": "67477a7455d881b78f7a79fa" - }, - "name": "Migration20241127195120", - "created_at": { - "$date": "2024-11-27T20:00:52.582Z" - } - }, - { - "_id": { - "$oid": "6748b0f451c62e9dc6899983" - }, - "name": "Migration20241128155801", - "created_at": { - "$date": "2024-11-28T18:05:40.839Z" - } - }, { "_id": { "$oid": "673fca34cc4a3264457c8ad1"