From d56674c2db7fd1b4fc95db1e6c92599c68dc8404 Mon Sep 17 00:00:00 2001 From: tobslob Date: Thu, 1 Jul 2021 07:42:44 +0100 Subject: [PATCH 1/8] chore: document migration process --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index 8c9fdbb..cc2fefc 100755 --- a/README.md +++ b/README.md @@ -90,3 +90,44 @@ await mongoose.connect(url, defaultMongoOpts) const person = await PersonRepo.byID(id)); const people = await PersonRepo.all({}); ``` + +## How to run a migration + +```shell +yarn migrations init +``` + +The above command will initialize a `migrate-mongo-config.js` in the root directory and create a migration folder in your `src` folder. + +Now run + +```shell +yarn migrations create [description] +``` + +The command helps to create a migration.ts file in the `src -> migration` directory + +Now it's time to write your migration code in the `function up() inside` `-migration.ts` + +```ts +import { Db, MongoClient } from "mongodb"; + +export async function up(db: Db, conn: MongoClient) { + // TODO write your migration here. + // Example: + // await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}}); +} + +export async function down(db: Db, conn: MongoClient) { + // TODO write the statements to rollback your migration (if possible) + // Example: + // await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}}); +} +``` + +In your index.ts add the code below, this helps to call the `function up()` + +```ts +await migrateUp(app.db); +Log.info("🚛 Completed DB migration"); +``` From 042c3be806b4ebebde5876917abdae212ed1d2a6 Mon Sep 17 00:00:00 2001 From: tobslob Date: Thu, 1 Jul 2021 12:49:41 +0100 Subject: [PATCH 2/8] fix: more descriptive command --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cc2fefc..cb889a1 100755 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ The above command will initialize a `migrate-mongo-config.js` in the root direct Now run ```shell -yarn migrations create [description] +yarn migrations create ``` The command helps to create a migration.ts file in the `src -> migration` directory @@ -125,9 +125,4 @@ export async function down(db: Db, conn: MongoClient) { } ``` -In your index.ts add the code below, this helps to call the `function up()` - -```ts -await migrateUp(app.db); -Log.info("🚛 Completed DB migration"); -``` +Now you can call `function up()` inside your server to run migration and also `function down()` can contain logic to undo migrations. From 50ec9e1a9a7308fb02f52a537cb05e34724ea00f Mon Sep 17 00:00:00 2001 From: tobslob Date: Thu, 1 Jul 2021 15:39:42 +0100 Subject: [PATCH 3/8] fix: a better description --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cb889a1..8950b42 100755 --- a/README.md +++ b/README.md @@ -105,9 +105,7 @@ Now run yarn migrations create ``` -The command helps to create a migration.ts file in the `src -> migration` directory - -Now it's time to write your migration code in the `function up() inside` `-migration.ts` +The command helps to create a migration script file in the `src -> migration` directory as below: ```ts import { Db, MongoClient } from "mongodb"; @@ -125,4 +123,4 @@ export async function down(db: Db, conn: MongoClient) { } ``` -Now you can call `function up()` inside your server to run migration and also `function down()` can contain logic to undo migrations. +When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state. From 800d21b76e6199303a36427298a032541348be52 Mon Sep 17 00:00:00 2001 From: tobslob Date: Thu, 1 Jul 2021 15:39:42 +0100 Subject: [PATCH 4/8] fix: a better description --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cb889a1..d036d96 100755 --- a/README.md +++ b/README.md @@ -105,9 +105,7 @@ Now run yarn migrations create ``` -The command helps to create a migration.ts file in the `src -> migration` directory - -Now it's time to write your migration code in the `function up() inside` `-migration.ts` +The command helps to create a migration script in the `src -> migration` directory as below: ```ts import { Db, MongoClient } from "mongodb"; @@ -125,4 +123,4 @@ export async function down(db: Db, conn: MongoClient) { } ``` -Now you can call `function up()` inside your server to run migration and also `function down()` can contain logic to undo migrations. +When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state. From 7507908bcb89e1c01626aeee520229dbe7d36a39 Mon Sep 17 00:00:00 2001 From: tobslob Date: Tue, 6 Jul 2021 19:39:36 +0100 Subject: [PATCH 5/8] fix: readme didn't describe how to apply migration --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e4ef43e..ada5832 100755 --- a/README.md +++ b/README.md @@ -123,4 +123,6 @@ export async function down(db: Db, conn: MongoClient) { } ``` +import `migrateUp` or `migrateDown` function in your server (i.e index.ts) to apply migration. + When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state. From b9bbcab1cce0860800616572fad522870393cc07 Mon Sep 17 00:00:00 2001 From: tobslob Date: Tue, 6 Jul 2021 19:39:36 +0100 Subject: [PATCH 6/8] fix: readme didn't describe how to apply migration --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e4ef43e..ed1b69a 100755 --- a/README.md +++ b/README.md @@ -123,4 +123,6 @@ export async function down(db: Db, conn: MongoClient) { } ``` +Import `migrateUp` or `migrateDown` function in your server (i.e index.ts) to apply migration. + When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state. From 8902473bb6740374d4ecd689e037223133c64d63 Mon Sep 17 00:00:00 2001 From: tobslob Date: Wed, 7 Jul 2021 08:52:38 +0100 Subject: [PATCH 7/8] fix: has no use --- README.md | 3 ++- bin/migrate.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ed1b69a..2c492ec 100755 --- a/README.md +++ b/README.md @@ -123,6 +123,7 @@ export async function down(db: Db, conn: MongoClient) { } ``` -Import `migrateUp` or `migrateDown` function in your server (i.e index.ts) to apply migration. +Call `migrateUp` or `migrateDown` function to apply migration. +You can either call the function in your index.ts so that migration can apply at server start up or run `migrateUp` or `migrateDown` function in a script to apply migration. When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state. diff --git a/bin/migrate.js b/bin/migrate.js index 6e63a85..44bbb40 100755 --- a/bin/migrate.js +++ b/bin/migrate.js @@ -22,7 +22,7 @@ program program .command("create [description]") .description("create a new database migration with the provided description") - .option("-f --file ", "use a custom config file") + .option("-f ", "use a custom config file") .action((description, options) => { global.options = options; migrate From c2a8e198d07471992c915906bd8aacf2df3d600f Mon Sep 17 00:00:00 2001 From: tobslob Date: Thu, 15 Jul 2021 07:48:42 +0100 Subject: [PATCH 8/8] fix: improve doc --- README.md | 9 +++++++-- bin/migrate.js | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2c492ec..d5bc9bf 100755 --- a/README.md +++ b/README.md @@ -123,7 +123,12 @@ export async function down(db: Db, conn: MongoClient) { } ``` +When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state. + Call `migrateUp` or `migrateDown` function to apply migration. -You can either call the function in your index.ts so that migration can apply at server start up or run `migrateUp` or `migrateDown` function in a script to apply migration. -When the script is applied, the `up` function is responsible for changing the database schema, while the `down` function is responsible for going back to the previous database state. +```ts +migrateUp(connection); +``` + +`connection` is a mongodb connection. diff --git a/bin/migrate.js b/bin/migrate.js index 44bbb40..0ded7e2 100755 --- a/bin/migrate.js +++ b/bin/migrate.js @@ -22,7 +22,6 @@ program program .command("create [description]") .description("create a new database migration with the provided description") - .option("-f ", "use a custom config file") .action((description, options) => { global.options = options; migrate