Skip to content

Commit

Permalink
BC-5836 - convert a migration
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Jan 15, 2024
1 parent b57178e commit c70a004
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/migrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
pull_request:
branches: [ main ]

env:
MONGODB_VERSION: 4.4
NODE_VERSION: '18'
jobs:
migration:
runs-on: ubuntu-latest
Expand All @@ -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/[email protected]
- 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
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
// do nothing
console.error(`Migration down not implemented. You might need to restore database from backup!`);
Expand Down
29 changes: 29 additions & 0 deletions apps/server/src/migrations/mikro-orm/Migration20240115103302.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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');
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c70a004

Please sign in to comment.