mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 19:58:11 +00:00
add migration file when the app is starting
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { registerAs } from "@nestjs/config";
|
||||
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
|
||||
import { join, dirname } from "path";
|
||||
import {
|
||||
DefaultPosition,
|
||||
DefaultUnit,
|
||||
@@ -44,6 +45,7 @@ import {
|
||||
NotificationTemplate,
|
||||
} from "@tria-plc/iamapi-common";
|
||||
import { OrganizationSetting } from "@tria-plc/iamapi-common/entities/iam/organization-structure/organization-setting.entity";
|
||||
import { APPLICATION_SEARCH_PATH } from "./ensure-postgres-schemas";
|
||||
|
||||
const iamEntities = [
|
||||
DefaultPosition,
|
||||
@@ -90,26 +92,37 @@ const iamEntities = [
|
||||
NotificationTemplate,
|
||||
];
|
||||
|
||||
const iamMigrationsGlob = join(
|
||||
dirname(require.resolve("@tria-plc/iamapi-common/package.json")),
|
||||
"dist/db/migrations/*.js",
|
||||
);
|
||||
const freightMigrationsGlob = join(__dirname, "../migrations/*.js");
|
||||
|
||||
export default registerAs(
|
||||
"database",
|
||||
(): TypeOrmModuleOptions => ({
|
||||
(): TypeOrmModuleOptions => {
|
||||
return {
|
||||
type: "postgres",
|
||||
host: process.env.DB_HOST ?? "localhost",
|
||||
port: parseInt(process.env.DB_PORT ?? "5433", 10),
|
||||
username: process.env.DB_USER ?? "postgres",
|
||||
password: process.env.DB_PASSWORD ?? "",
|
||||
database: process.env.DB_NAME ?? "edr_freight",
|
||||
schema: "public",
|
||||
extra: {
|
||||
options: `-c search_path=${APPLICATION_SEARCH_PATH}`,
|
||||
},
|
||||
entities: [__dirname + "/../**/*.entity.{ts,js}", ...iamEntities],
|
||||
autoLoadEntities: true,
|
||||
migrations: [
|
||||
// IAM schema + tables must be created before freight migrations
|
||||
__dirname +
|
||||
"/../../node_modules/@tria-plc/iamapi-common/dist/db/migrations/*.js",
|
||||
__dirname + "/../migrations/*.js",
|
||||
iamMigrationsGlob,
|
||||
freightMigrationsGlob,
|
||||
],
|
||||
migrationsRun: true,
|
||||
// Schema changes via migrations only (synchronize breaks ITMLS backfill on existing rows).
|
||||
synchronize: false,
|
||||
logging: process.env.NODE_ENV === "development",
|
||||
}),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
50
apps/edr-freight-api/src/config/ensure-postgres-schemas.ts
Normal file
50
apps/edr-freight-api/src/config/ensure-postgres-schemas.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { DataSource, DataSourceOptions } from "typeorm";
|
||||
|
||||
/** Schemas required before TypeORM migrations and entity access. */
|
||||
export const APPLICATION_SCHEMAS = [
|
||||
"public",
|
||||
"iam",
|
||||
"freight",
|
||||
"audit",
|
||||
] as const;
|
||||
|
||||
export const APPLICATION_SEARCH_PATH = APPLICATION_SCHEMAS.join(",");
|
||||
|
||||
/**
|
||||
* TypeORM creates the migrations table before any migration runs. If `public` was
|
||||
* dropped, current_schema() is null and CREATE TABLE migrations fails.
|
||||
* IAM/freight migrations assume their schemas already exist.
|
||||
*/
|
||||
export async function ensurePostgresSchemas(
|
||||
options: DataSourceOptions,
|
||||
): Promise<void> {
|
||||
const bootstrap = new DataSource({
|
||||
...options,
|
||||
entities: [],
|
||||
migrations: [],
|
||||
migrationsRun: false,
|
||||
synchronize: false,
|
||||
});
|
||||
|
||||
await bootstrap.initialize();
|
||||
|
||||
for (const schema of APPLICATION_SCHEMAS) {
|
||||
if (schema === "public") {
|
||||
await bootstrap.query(`CREATE SCHEMA IF NOT EXISTS public`);
|
||||
await bootstrap.query(`GRANT ALL ON SCHEMA public TO public`);
|
||||
await bootstrap.query(`GRANT CREATE ON SCHEMA public TO public`);
|
||||
} else {
|
||||
await bootstrap.query(`CREATE SCHEMA IF NOT EXISTS "${schema}"`);
|
||||
await bootstrap.query(`GRANT USAGE ON SCHEMA "${schema}" TO public`);
|
||||
await bootstrap.query(
|
||||
`GRANT CREATE ON SCHEMA "${schema}" TO public`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await bootstrap.query(
|
||||
`SET search_path TO ${APPLICATION_SEARCH_PATH}`,
|
||||
);
|
||||
|
||||
await bootstrap.destroy();
|
||||
}
|
||||
Reference in New Issue
Block a user