diff --git a/package.json b/package.json index f03edc696..757cf316f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "commitmsg": "commitlint -E GIT_PARAMS", "docker:start": "./bin/start-dbs.sh", "docker:stop": "./bin/stop-dbs.sh", + "migrate": "node ./packages/shopping/dist/migrate", "lint": "npm run prettier:check && npm run eslint", "lint:fix": "npm run prettier:fix && npm run eslint:fix", "prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\" \"**/*.md\"", diff --git a/packages/shopping/package.json b/packages/shopping/package.json index 65082e6a4..44a8ef7ec 100644 --- a/packages/shopping/package.json +++ b/packages/shopping/package.json @@ -17,6 +17,7 @@ "clean": "lb-clean dist*", "docker:start": "../../bin/start-dbs.sh", "docker:stop": "../../bin/stop-dbs.sh", + "migrate": "node ./dist/migrate", "pretest": "npm run clean && npm run build && npm run docker:start", "pretest:ci": "npm run build", "test": "lb-mocha --allow-console-logs \"dist/__tests__/**/*.js\"", diff --git a/packages/shopping/src/migrate.ts b/packages/shopping/src/migrate.ts new file mode 100644 index 000000000..84eaaf18e --- /dev/null +++ b/packages/shopping/src/migrate.ts @@ -0,0 +1,20 @@ +import {ShoppingApplication} from './application'; + +export async function migrate(args: string[]) { + const existingSchema = args.includes('--rebuild') ? 'drop' : 'alter'; + console.log('Migrating schemas (%s existing schema)', existingSchema); + + const app = new ShoppingApplication(); + await app.boot(); + await app.migrateSchema({existingSchema}); + + // Connectors usually keep a pool of opened connections, + // this keeps the process running even after all work is done. + // We need to exit explicitly. + process.exit(0); +} + +migrate(process.argv).catch(err => { + console.error('Cannot migrate database schema', err); + process.exit(1); +});