Skip to content

Commit

Permalink
feat: switch migration tool (#729)
Browse files Browse the repository at this point in the history
Signed-off-by: hxtree <[email protected]>
  • Loading branch information
hxtree authored Nov 29, 2023
1 parent 0803245 commit c75bdd6
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 136 deletions.
4 changes: 4 additions & 0 deletions common/config/rush/browser-approved-packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@
"name": "migrate",
"allowedCategories": [ "apis", "libraries" ]
},
{
"name": "mongo-migrate-ts",
"allowedCategories": [ "apis" ]
},
{
"name": "mongodb",
"allowedCategories": [ "apis", "libraries" ]
Expand Down
62 changes: 14 additions & 48 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/rush/repo-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "226112290342a77c7f773aeecbc45e260e27e9bc",
"pnpmShrinkwrapHash": "789c7de00d79d30378a52f3bcfad002fc81af8d2",
"preferredVersionsHash": "7c6836c4ff2ee31a263e87ea93a487fc752ca3f6"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"sourceMap": true,
"include": [
"../../../../../migrations/*.ts",
"../../../../../package.json",
"../../../../../src/*.{ts|tsx}",
"../../../../../src/**/*.{ts|tsx}",
Expand Down
9 changes: 0 additions & 9 deletions services/player-achievements/.migrate

This file was deleted.

22 changes: 22 additions & 0 deletions services/player-achievements/migrations/1701265134891_Migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Db } from 'mongodb';
import { MigrationInterface } from 'mongo-migrate-ts';
import { asyncForEach } from '@cats-cradle/nestjs-modules';
import { ObjectId, UUID } from 'bson';
import { v4 } from 'uuid';
import { Achievements } from '../src/models/achievements';

export class Migration1701265134891 implements MigrationInterface {
public async up(db: Db): Promise<any> {
await asyncForEach(Achievements, async (trophy: object) => {
const record = { _id: new UUID(v4()) as any as ObjectId };

await db.collection('trophies').insertOne({ ...record, ...trophy });
});
}

public async down(db: Db): Promise<any> {
await asyncForEach(Achievements, async (trophy) => {
await db.collection('trophies').deleteOne(trophy);
});
}
}
18 changes: 18 additions & 0 deletions services/player-achievements/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { mongoMigrateCli } from 'mongo-migrate-ts';
import dotenv from 'dotenv';

dotenv.config();

const username = process.env.MONGO_DATABASE_USER;
const password = process.env.MONGO_DATABASE_PASSWORD;
const MONGO_URI = `${process.env.MONGO_DATABASE_URI}`;

const db = `${process.env.STAGE}-${process.env.APP_NAME}`;
const uri = MONGO_URI.replace('//', `//${username}:${password}@`);

mongoMigrateCli({
uri,
database: db,
migrationsDir: __dirname,
migrationsCollection: 'migration_versions',
});
12 changes: 6 additions & 6 deletions services/player-achievements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"test:cov": "jest --coverage",
"lint": "eslint --format visualstudio \"./{stacks,src}/**/*.ts\" --fix",
"lint:ci": "eslint --format visualstudio \"./{stacks,src}/*.ts\" --fix-dry-run",
"migrate:create": "migrate create --template-file ./src/migrations-utils/template.ts --migrations-dir=\"./src/migrations\" --compiler=\"ts:./src/migrations-utils/ts-compiler.js\"",
"migrate:up": "migrate --migrations-dir=\"./src/migrations\" --compiler=\"ts:./src/migrations-utils/ts-compiler.js\" up",
"migrate:down": "migrate --migrations-dir=\"./src/migrations\" --compiler=\"ts:./src/migrations-utils/ts-compiler.js\" down",
"migrate:status": "ts-node migrations/index.ts status",
"migrate:new": "ts-node migrations/index.ts new",
"migrate:up": "ts-node migrations/index.ts up",
"migrate:down": "ts-node migrations/index.ts down --all",
"docs": "typedoc --entryPointStrategy expand ./src",
"cdk": "cdk",
"cdk:bootstrap": "cdk bootstrap",
Expand Down Expand Up @@ -51,11 +52,10 @@
"@cats-cradle/create-bundle": "1.0.7",
"@nestjs/mongoose": "9.2.1",
"mongoose": "6.9.1",
"migrate": "~2.0.1",
"mongodb": "6.0.0",
"tsconfig-paths": "~4.2.0",
"dotenv": "~16.0.3",
"bson": "~6.2.0"
"bson": "~6.2.0",
"mongo-migrate-ts": "~1.3.1"
},
"devDependencies": {
"@shelf/jest-mongodb": "~4.1.7",
Expand Down
22 changes: 0 additions & 22 deletions services/player-achievements/src/migrations-utils/db.ts

This file was deleted.

20 changes: 0 additions & 20 deletions services/player-achievements/src/migrations-utils/template.ts

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit c75bdd6

Please sign in to comment.