From c70a00484f0261038e078bbfc3adecd6c67e7a0a Mon Sep 17 00:00:00 2001 From: virgilchiriac Date: Mon, 15 Jan 2024 23:13:50 +0100 Subject: [PATCH] BC-5836 - convert a migration --- .github/workflows/migrations.yml | 15 ++++------ .../mikro-orm/Migration20240108145519.ts | 1 + .../mikro-orm/Migration20240115103302.ts | 29 +++++++++++++++++++ package.json | 1 - 4 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 apps/server/src/migrations/mikro-orm/Migration20240115103302.ts diff --git a/.github/workflows/migrations.yml b/.github/workflows/migrations.yml index e736c30ee1d..9386f51c1af 100644 --- a/.github/workflows/migrations.yml +++ b/.github/workflows/migrations.yml @@ -6,6 +6,9 @@ on: pull_request: branches: [ main ] +env: + MONGODB_VERSION: 4.4 + NODE_VERSION: '18' jobs: migration: runs-on: ubuntu-latest @@ -14,19 +17,13 @@ jobs: timeout-minutes: 5 steps: - uses: actions/checkout@v4 - - name: check all migrations are up in database seed - run: test $(grep "\"down\"" ./backup/setup/migrations.json -c) -eq 0 - name: mongodb setup uses: supercharge/mongodb-github-action@1.8.0 - name: setup uses: actions/setup-node@v4 with: - node-version: '16' + node-version: ${{ env.NODE_VERSION }} - run: npm ci - run: npm run setup:db:seed - - name: check migrations.json formatting - run: | - npm run migration:persisted - git diff --exit-code backup/** - - name: check filesystem migrations have been added to database - run: npm run migration:list + - name: check no pending migrations (migration is in db) + run: npx mikro-orm migration:pending diff --git a/apps/server/src/migrations/mikro-orm/Migration20240108145519.ts b/apps/server/src/migrations/mikro-orm/Migration20240108145519.ts index c049d0b71c1..aeab8dd7650 100644 --- a/apps/server/src/migrations/mikro-orm/Migration20240108145519.ts +++ b/apps/server/src/migrations/mikro-orm/Migration20240108145519.ts @@ -9,6 +9,7 @@ export class Migration20240108111130 extends Migration { console.log(`removed ${deletedCount} records`); } + // eslint-disable-next-line @typescript-eslint/require-await async down(): Promise { // do nothing console.error(`Migration down not implemented. You might need to restore database from backup!`); diff --git a/apps/server/src/migrations/mikro-orm/Migration20240115103302.ts b/apps/server/src/migrations/mikro-orm/Migration20240115103302.ts new file mode 100644 index 00000000000..6947e820e04 --- /dev/null +++ b/apps/server/src/migrations/mikro-orm/Migration20240115103302.ts @@ -0,0 +1,29 @@ +import { Migration } from '@mikro-orm/migrations-mongodb'; + +// remove-undefined-parameters-from-external-tool +export class Migration20240115103302 extends Migration { + async up(): Promise { + const contextExternalToolResponse = await this.driver.nativeUpdate( + 'context-external-tools', + { $or: [{ 'parameters.value': undefined }, { 'parameters.value': '' }] }, + { $pull: { parameters: { $or: [{ value: undefined }, { value: '' }] } } } + // { ctx: this.ctx } + ); + + console.info(`Removed ${contextExternalToolResponse.affectedRows} parameter(s) in context-external-tools`); + + const schoolExternalToolResponse = await this.driver.nativeUpdate( + 'school-external-tools', + { $or: [{ 'parameters.value': undefined }, { 'parameters.value': '' }] }, + { $pull: { parameters: { $or: [{ value: undefined }, { value: '' }] } } } + // { ctx: this.ctx } + ); + + console.info(`Removed ${schoolExternalToolResponse.affectedRows} parameter(s) in school-external-tools`); + } + + // eslint-disable-next-line @typescript-eslint/require-await + async down() { + console.error('This migration cannot be undone'); + } +} diff --git a/package.json b/package.json index d3ce2eb167d..e5b6c96c19e 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "mocha-metrics": "cross-env NODE_ENV=test mocha \"test/routes/*.metrics.js\" --exclude \"{test,src}/**/*.test.{js,ts}\" --no-timeout --exit", "migration:up": "npm run nest:start:console -- database migration --up", "migration:down": "npm run nest:start:console -- database migration --down", - "migration:list": "npx mikro-orm migration:list", "migration:persisted": "npm run nest:start:console -- database export --collection migrations --override", "nest:prebuild": "rimraf dist", "nest:build": "nest build",