-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- migration script for garbage data cleanup - adapt logic for sharing tools in course for different schools
- Loading branch information
1 parent
2104505
commit 2b7c697
Showing
27 changed files
with
1,425 additions
and
196 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
apps/server/src/migrations/mikro-orm/Migration20241120100616.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Migration } from '@mikro-orm/migrations-mongodb'; | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
|
||
export class Migration20241120100616 extends Migration { | ||
async up(): Promise<void> { | ||
const cursor = this.getCollection<{ contextType: string; contextId: ObjectId; schoolTool: ObjectId }>( | ||
'context-external-tools' | ||
).find({ | ||
$or: [{ contextType: 'course' }, { contextType: 'boardElement' }], | ||
}); | ||
|
||
let numberOfDeletedTools = 0; | ||
let numberOfDeletedElements = 0; | ||
for await (const tool of cursor) { | ||
let courseId: ObjectId | undefined; | ||
if (tool.contextType === 'course') { | ||
courseId = tool.contextId; | ||
} else if (tool.contextType === 'boardElement') { | ||
const element = await this.getCollection<{ path: string }>('boardnodes').findOne({ | ||
_id: tool.contextId, | ||
}); | ||
|
||
if (element) { | ||
const boardId = new ObjectId(element.path.split(',')[1]); | ||
|
||
const board = await this.getCollection<{ context: ObjectId }>('boardnodes').findOne({ | ||
_id: boardId, | ||
}); | ||
|
||
if (board) { | ||
courseId = board.context; | ||
} | ||
} | ||
} | ||
|
||
if (courseId) { | ||
const course = await this.getCollection<{ schoolId: ObjectId }>('courses').findOne({ _id: courseId }); | ||
|
||
const schoolTool = await this.getCollection<{ school: ObjectId }>('school-external-tools').findOne({ | ||
_id: tool.schoolTool, | ||
}); | ||
|
||
if (!course || !schoolTool || course.schoolId.toString() !== schoolTool.school.toString()) { | ||
await this.driver.nativeDelete('context-external-tools', { _id: tool._id }); | ||
console.info(`deleted context external tool: ${tool._id.toString()}`); | ||
numberOfDeletedTools += 1; | ||
if (tool.contextType === 'boardElement') { | ||
await this.driver.nativeDelete('boardnodes', { _id: tool.contextId }); | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
console.info(`deleted boardnode: ${tool.contextId}`); | ||
numberOfDeletedElements += 1; | ||
} | ||
} | ||
} | ||
} | ||
console.info( | ||
`Deleted ${numberOfDeletedTools} context external tools and ${numberOfDeletedElements} external tool elements.` | ||
); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async down(): Promise<void> { | ||
console.info('Unfortunately the deleted documents cannot be restored. Use a backup.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.