add migration file when the app is starting

This commit is contained in:
marshal
2026-06-02 16:43:37 +03:00
parent e4859b3eeb
commit f93a502247
9 changed files with 316 additions and 54 deletions

View File

@@ -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",
}),
};
},
);

View 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();
}