Skip to content

Commit

Permalink
Speeding up migrations (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinejaussoin authored Jul 11, 2021
1 parent 9eedb03 commit 6f90b74
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Alpha Build'

on:
push:
branches: [alpha]
branches: [v460/speed-up-migrate]

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ When using the Docker deployment, your database runs from a container. But if yo
### Version 4.6.0 (not released)

- Support OKTA for authentication
- Speeding up the migration on production (using transpiled JavaScript instead of TypeScript via ts-node)

### Version 4.5.0

Expand Down
9 changes: 5 additions & 4 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"scripts": {
"build": "rimraf dist && tsc --build",
"start": "nodemon --exec 'yarn fix & ts-node' --files ./src/index.ts",
"create-migration": "ts-node ../node_modules/typeorm/cli.js migration:generate -n ",
"migrate": "ts-node ./src/init.ts && ts-node ../node_modules/typeorm/cli.js migration:run",
"revert": "ts-node ../node_modules/typeorm/cli.js migration:revert",
"create-migration": "ts-node ./src/init.ts --ts && ts-node ../node_modules/typeorm/cli.js migration:generate -n ",
"migrate": "node ./dist/src/init.js && node ../node_modules/typeorm/cli.js migration:run",
"revert": "node ./dist/src/init.js && node ../node_modules/typeorm/cli.js migration:revert",
"lint": "eslint 'src/**/*.ts'",
"fix": "eslint 'src/**/*.ts' --fix"
},
Expand Down Expand Up @@ -88,7 +88,8 @@
"ts-jest": "^27.0.3",
"ts-node": "^9.1.1",
"typeorm": "^0.2.34",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"yargs": "^17.0.1"
},
"resolutions": {
"@types/connect-redis": "0.0.16",
Expand Down
40 changes: 35 additions & 5 deletions backend/src/build-config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
import getOrmConfig from './db/orm-config';
import path from 'path';
import fs from 'fs';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';

const argv = yargs(hideBin(process.argv)).argv;

async function buildOrmConfig() {
const config = getOrmConfig();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).entities = [
`${path.resolve(__dirname, 'db', 'entities')}/*.ts`,
];
if (argv.ts) {
console.log('Creating TypeScript ormconfig.json before migrations');
await buildOrmConfigTs();
} else {
console.log('Creating JavaScript ormconfig.json before migrations');
await buildOrmConfigJs();
}
}

async function buildOrmConfigJs() {
const config = getConfig('js');
const jsonPath = path.resolve(__dirname, '..', '..', 'ormconfig.json');
fs.writeFileSync(jsonPath, JSON.stringify(config, null, 2));
}

async function buildOrmConfigTs() {
const config = getConfig('ts');
const jsonPath = path.resolve(__dirname, '..', 'ormconfig.json');
fs.writeFileSync(jsonPath, JSON.stringify(config, null, 2));
}

function getConfig(extension: 'js' | 'ts') {
const config = getOrmConfig();
const rootPath = path.resolve(__dirname);
const entities = path.resolve(rootPath, 'db', 'entities');
const migrations = path.resolve(rootPath, 'db', 'migrations');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).entities = [`${entities}/*.${extension}`];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).migrations = [`${migrations}/*.${extension}`];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).cli.migrationsDir = migrations;
return config;
}

export default buildOrmConfig;
27 changes: 20 additions & 7 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ import fs from 'fs';
import path from 'path';
import { BackendConfig } from './types';
import dotenv from 'dotenv';
const confPath = path.resolve(__dirname, '../../.env');
const defaultConfPath = path.resolve(__dirname, '../../.env.example');

const fileExist = fs.existsSync(confPath);
function findDotEnvPath(): string | null {
let current = path.resolve(__dirname);
for (let i = 0; i < 5; i++) {
const custom = path.resolve(current, '.env');
const example = path.resolve(current, '.env.example');
if (fs.existsSync(custom)) {
console.log('Found custom .env: ', custom);
return custom;
}
if (fs.existsSync(example)) {
console.log('Found example .env: ', example);
return example;
}
current = path.resolve(current, '..');
}
return null;
}

if (fileExist) {
dotenv.config({ path: confPath });
} else {
dotenv.config({ path: defaultConfPath });
const dotEnvPath = findDotEnvPath();
if (dotEnvPath) {
dotenv.config({ path: dotEnvPath });
}

function defaults(key: string, defaultValue: string): string {
Expand Down
1 change: 0 additions & 1 deletion backend/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import buildConfig from './build-config';

async function go() {
console.log('Creating ormconfig.json before migrations');
await buildConfig();
}

Expand Down
12 changes: 7 additions & 5 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
"baseUrl": "."
},
"include": ["src", "./package.json"],
"exclude" : ["src/**/*.test.ts"],
"references": [{
"path": "../common"
}]
}
"exclude": ["src/**/*.test.ts"],
"references": [
{
"path": "../common"
}
]
}
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15559,6 +15559,19 @@ yargs@^16.0.0, yargs@^16.0.3, yargs@^16.2.0:
y18n "^5.0.5"
yargs-parser "^20.2.2"

yargs@^17.0.1:
version "17.0.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.0.1.tgz#6a1ced4ed5ee0b388010ba9fd67af83b9362e0bb"
integrity sha512-xBBulfCc8Y6gLFcrPvtqKz9hz8SO0l1Ni8GgDekvBX2ro0HRQImDGnikfc33cgzcYUSncapnNcZDjVFIH3f6KQ==
dependencies:
cliui "^7.0.2"
escalade "^3.1.1"
get-caller-file "^2.0.5"
require-directory "^2.1.1"
string-width "^4.2.0"
y18n "^5.0.5"
yargs-parser "^20.2.2"

[email protected]:
version "0.1.2"
resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"
Expand Down

0 comments on commit 6f90b74

Please sign in to comment.