Skip to content

Commit

Permalink
Merge pull request #113 from Giveth/fix-migrations-execution-issue
Browse files Browse the repository at this point in the history
Fix migrations execution issue
  • Loading branch information
ae2079 authored Aug 1, 2024
2 parents 42428dc + 5a290b8 commit 1436576
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: givethio
POSTGRES_DB: notification-center
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/staging-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: givethio
POSTGRES_DB: notification-center
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand Down
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
"swagger": "tsoa spec",
"prebuild": "npm run swagger",
"start": "NODE_ENV=development ts-node-dev --project ./tsconfig.json --respawn ./src/index.ts",
"test": "NODE_ENV=test mocha -t 30000 --exit -r ts-node/register ./test/pre-test-scripts.ts ./src/**/*.test.ts ./src/**/**/*.test.ts",
"test:notificationRepository": "NODE_ENV=test mocha -t 30000 --exit -r ts-node/register ./test/pre-test-scripts.ts ./src/repositories/notificationRepository.test.ts ",
"test:notificationSettingRepository": "NODE_ENV=test mocha -t 30000 --exit -r ts-node/register ./test/pre-test-scripts.ts ./src/repositories/notificationSettingRepository.test.ts ",
"test": "NODE_ENV=test mocha -t 30000 --exit -r ts-node/register ./test/pre-test-scripts.ts ./src/**/*.test.ts ./src/**/**/*.test.ts",
"test:notificationRepository": "NODE_ENV=test mocha -t 30000 --exit -r ts-node/register ./test/pre-test-scripts.ts ./src/repositories/notificationRepository.test.ts",
"test:notificationSettingRepository": "NODE_ENV=test mocha -t 30000 --exit -r ts-node/register ./test/pre-test-scripts.ts ./src/repositories/notificationSettingRepository.test.ts",
"lint": "eslint",
"lint:fix": "eslint --fix",
"prettify": "prettier --write '**/*.ts*'",
"db:migrate:run:local": "NODE_ENV=development npx typeorm-ts-node-commonjs migration:run -d ./src/dataSource.ts ",
"db:migrate:run:staging": "NODE_ENV=staging npx typeorm-ts-node-esm migration:run -d ./src/dataSource.ts ",
"db:migrate:revert:local": "NODE_ENV=development npx typeorm-ts-node-esm migration:revert -d ./src/dataSource.ts ",
"db:migrate:run:test": "NODE_ENV=test npx typeorm-ts-node-esm migration:run -d ./src/dataSource.ts ",
"db:migrate:revert:test": "NODE_ENV=test npx typeorm-ts-node-esm migration:revert -d ./src/dataSource.ts ",
"start:server:staging": "npm run db:migrate:run:staging && cd dist && pm2-runtime src/index.js",
"db:migrate:run:production": "NODE_ENV=production npx typeorm-ts-node-esm migration:run -d ./src/dataSource.ts",
"start:server:production": "npm run db:migrate:run:production && cd dist && pm2-runtime src/index.js"
"prettify": "prettier --write '**/*.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",
"start:server:production": "npm run db:migrate:run:production && cd dist && pm2-runtime src/index.js"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -54,11 +54,11 @@
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"pg": "8.5.1",
"reflect-metadata": "0.1.13",
"reflect-metadata": "^0.2.2",
"segment-analytics-node": "^1.0.24",
"swagger-ui-express": "4.3.0",
"tsoa": "3.14.1",
"typeorm": "0.3.6"
"typeorm": "^0.3.20"
},
"devDependencies": {
"@eslint/compat": "^1.1.0",
Expand All @@ -77,7 +77,7 @@
"@types/ioredis": "^4.28.10",
"@types/jsonwebtoken": "^8.5.8",
"@types/mocha": "^9.1.1",
"@types/node": "^16.11.10",
"@types/node": "^22.0.0",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
Expand All @@ -91,8 +91,9 @@
"mocha": "9.2.2",
"pgtools": "^0.3.2",
"prettier": "^3.3.2",
"ts-node": "10.9.1",
"ts-node": "^10.9.2",
"ts-node-dev": "2.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "^4.5.2"
}
}
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);
}
})();
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "resolveJsonModule": true, /* Enable importing .json files */
"resolveJsonModule": true, /* Enable importing .json files */
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Expand Down
Loading

0 comments on commit 1436576

Please sign in to comment.