mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
add migrations
This commit is contained in:
45
CommonServer/Infrastructure/PostgresConfig.ts
Normal file
45
CommonServer/Infrastructure/PostgresConfig.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
|
||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
import {
|
||||||
|
DatabaseHost,
|
||||||
|
DatabaseName,
|
||||||
|
DatabasePassword,
|
||||||
|
DatabasePort,
|
||||||
|
DatabaseUsername,
|
||||||
|
Env,
|
||||||
|
} from '../Config';
|
||||||
|
import Entities from 'Model/Models/Index';
|
||||||
|
import Migrations from 'Model/Migrations/Index';
|
||||||
|
import DatabaseType from 'Common/Types/DatabaseType';
|
||||||
|
import AppEnvironment from 'Common/Types/AppEnvironment';
|
||||||
|
import Faker from 'Common/Utils/Faker';
|
||||||
|
|
||||||
|
|
||||||
|
export const dataSourceOptions = {
|
||||||
|
type: DatabaseType.Postgres,
|
||||||
|
host: DatabaseHost.toString(),
|
||||||
|
port: DatabasePort.toNumber(),
|
||||||
|
username: DatabaseUsername,
|
||||||
|
password: DatabasePassword,
|
||||||
|
database: DatabaseName,
|
||||||
|
migrationsTableName: 'migrations',
|
||||||
|
migrations: Migrations,
|
||||||
|
entities: Entities,
|
||||||
|
//logging: 'all',
|
||||||
|
synchronize: Env === AppEnvironment.Development,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const datasource = new DataSource(dataSourceOptions)
|
||||||
|
|
||||||
|
export const testDataSourceOptions = {
|
||||||
|
type: DatabaseType.Postgres,
|
||||||
|
host: DatabaseHost.toString(),
|
||||||
|
port: DatabasePort.toNumber(),
|
||||||
|
username: DatabaseUsername,
|
||||||
|
password: DatabasePassword,
|
||||||
|
database: DatabaseName + Faker.random16Numbers(),
|
||||||
|
entities: Entities,
|
||||||
|
synchronize:
|
||||||
|
Env === AppEnvironment.Test ||
|
||||||
|
Env === AppEnvironment.Development,
|
||||||
|
};
|
||||||
@@ -1,52 +1,16 @@
|
|||||||
import { DataSource, DataSourceOptions } from 'typeorm';
|
import { DataSource, DataSourceOptions } from 'typeorm';
|
||||||
import {
|
|
||||||
DatabaseHost,
|
|
||||||
DatabaseName,
|
|
||||||
DatabasePassword,
|
|
||||||
DatabasePort,
|
|
||||||
DatabaseUsername,
|
|
||||||
Env,
|
|
||||||
} from '../Config';
|
|
||||||
|
|
||||||
import Entities from 'Model/Models/Index';
|
|
||||||
import AppEnvironment from 'Common/Types/AppEnvironment';
|
|
||||||
import DatabaseType from 'Common/Types/DatabaseType';
|
|
||||||
import Faker from 'Common/Utils/Faker';
|
|
||||||
import logger from '../Utils/Logger';
|
import logger from '../Utils/Logger';
|
||||||
|
import { dataSourceOptions, testDataSourceOptions } from './PostgresConfig';
|
||||||
|
|
||||||
export default class Database {
|
export default class Database {
|
||||||
private dataSource!: DataSource | null;
|
private dataSource!: DataSource | null;
|
||||||
|
|
||||||
public getDatasourceOptions(): DataSourceOptions {
|
public getDatasourceOptions(): DataSourceOptions {
|
||||||
return {
|
return dataSourceOptions;
|
||||||
type: DatabaseType.Postgres,
|
|
||||||
host: DatabaseHost.toString(),
|
|
||||||
port: DatabasePort.toNumber(),
|
|
||||||
username: DatabaseUsername,
|
|
||||||
password: DatabasePassword,
|
|
||||||
database: DatabaseName,
|
|
||||||
migrationsTableName: 'migrations',
|
|
||||||
entities: Entities,
|
|
||||||
//logging: 'all',
|
|
||||||
synchronize:
|
|
||||||
Env === AppEnvironment.Test ||
|
|
||||||
Env === AppEnvironment.Development,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTestDatasourceOptions(): DataSourceOptions {
|
public getTestDatasourceOptions(): DataSourceOptions {
|
||||||
return {
|
return testDataSourceOptions;
|
||||||
type: DatabaseType.Postgres,
|
|
||||||
host: DatabaseHost.toString(),
|
|
||||||
port: DatabasePort.toNumber(),
|
|
||||||
username: DatabaseUsername,
|
|
||||||
password: DatabasePassword,
|
|
||||||
database: DatabaseName + Faker.random16Numbers(),
|
|
||||||
entities: Entities,
|
|
||||||
synchronize:
|
|
||||||
Env === AppEnvironment.Test ||
|
|
||||||
Env === AppEnvironment.Development,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getDataSource(): DataSource | null {
|
public getDataSource(): DataSource | null {
|
||||||
|
|||||||
3
Model/Migrations/Index.ts
Normal file
3
Model/Migrations/Index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export default [
|
||||||
|
|
||||||
|
];
|
||||||
11
Model/Migrations/Readme.md
Normal file
11
Model/Migrations/Readme.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
To create a new migration:
|
||||||
|
|
||||||
|
```
|
||||||
|
bash migration-create.sh <name>
|
||||||
|
```
|
||||||
|
|
||||||
|
To run a migration:
|
||||||
|
|
||||||
|
```
|
||||||
|
bash migration-run.sh
|
||||||
|
```
|
||||||
@@ -119,4 +119,7 @@ DATABASE_RESTORE_PASSWORD={{ .Env.DATABASE_PASSWORD }}
|
|||||||
DATABASE_RESTORE_FILENAME=db-12.backup
|
DATABASE_RESTORE_FILENAME=db-12.backup
|
||||||
|
|
||||||
ANALYTICS_KEY=
|
ANALYTICS_KEY=
|
||||||
ANALYTICS_HOST=
|
ANALYTICS_HOST=
|
||||||
|
|
||||||
|
DATABASE_MIGRATIONS_HOST=localhost
|
||||||
|
DATABASE_MIGRATIONS_PORT=5400
|
||||||
2
migration-create.sh
Normal file
2
migration-create.sh
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
sudo npm i -g typeorm
|
||||||
|
typeorm migration:create ./Model/Migrations/$1
|
||||||
8
migration-run.sh
Normal file
8
migration-run.sh
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Load env values from config.env
|
||||||
|
export $(grep -v '^#' config.env | xargs)
|
||||||
|
npm run prerun
|
||||||
|
export DATABASE_HOST=$DATABASE_MIGRATIONS_HOST
|
||||||
|
export DATABASE_PORT=$DATABASE_MIGRATIONS_PORT
|
||||||
|
|
||||||
|
sudo npm i -g ts-node
|
||||||
|
npx typeorm-ts-node-esm migration:run --dataSource=./CommonServer/Infrastructure/PostgresConfig.ts
|
||||||
1250
package-lock.json
generated
1250
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -7,7 +7,9 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/lodash": "^4.14.182",
|
"@types/lodash": "^4.14.182",
|
||||||
"eslint-plugin-progress": "^0.0.1",
|
"eslint-plugin-progress": "^0.0.1",
|
||||||
"the-new-css-reset": "^1.7.3"
|
"the-new-css-reset": "^1.7.3",
|
||||||
|
"ts-node": "^10.9.1",
|
||||||
|
"typeorm": "^0.3.11"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.17.8",
|
"@babel/core": "^7.17.8",
|
||||||
|
|||||||
Reference in New Issue
Block a user