Skip to content

Commit

Permalink
run migrations using scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ae2079 committed Jul 31, 2024
1 parent a02e696 commit 0b6839d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"lint": "eslint",
"lint:fix": "eslint --fix",
"prettify": "prettier --write '**/*.ts*'",
"db:migrate:run:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts",
"db:migrate:run:staging": "NODE_ENV=staging ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts",
"db:migrate:revert:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:revert -d ./src/dataSource.ts",
"db:migrate:run:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts",
"db:migrate:revert:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:revert -d ./src/dataSource.ts",
"db:migrate:run:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts",
"db:migrate:run:staging": "NODE_ENV=staging ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts",
"db:migrate:revert:local": "NODE_ENV=development ts-node -r tsconfig-paths/register ./src/scripts/revertMigrations.ts",
"db:migrate:run:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts",
"db:migrate:revert:test": "NODE_ENV=test ts-node -r tsconfig-paths/register ./src/scripts/revertMigrations.ts",
"db:migrate:run:production": "NODE_ENV=production ts-node -r tsconfig-paths/register ./src/scripts/runMigrations.ts",
"start:server:staging": "npm run db:migrate:run:staging && cd dist && pm2-runtime src/index.js",
"db:migrate:run:production": "NODE_ENV=production ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d ./src/dataSource.ts",
"start:server:production": "npm run db:migrate:run:production && cd dist && pm2-runtime src/index.js"
},
"keywords": [],
Expand Down
6 changes: 6 additions & 0 deletions src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ export const AppDataSource = new DataSource({
migrations: ['./migrations/*.ts'],
subscribers: [],
});

export async function initializeDataSource() {
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}
}
13 changes: 13 additions & 0 deletions src/scripts/revertMigrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { initializeDataSource, AppDataSource } from '../dataSource';

(async () => {
try {
await initializeDataSource();
await AppDataSource.undoLastMigration();
console.log('Last migration has been reverted successfully');
process.exit(0);
} catch (error) {
console.error('Error reverting migrations:', error);
process.exit(1);
}
})();
13 changes: 13 additions & 0 deletions src/scripts/runMigrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { initializeDataSource, AppDataSource } from '../dataSource';

(async () => {
try {
await initializeDataSource();
await AppDataSource.runMigrations();
console.log('Migrations have been executed successfully');
process.exit(0);
} catch (error) {
console.error('Error running migrations:', error);
process.exit(1);
}
})();

0 comments on commit 0b6839d

Please sign in to comment.