mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 04:50:54 +00:00
refactor(freight-api): split migration histories between IAM and freight
This commit is contained in:
@@ -118,30 +118,91 @@ const iamMigrationsGlob = join(
|
||||
);
|
||||
const freightMigrationsGlob = join(__dirname, "../migrations/*.js");
|
||||
|
||||
export function buildDataSourceOptions(): DataSourceOptions {
|
||||
/**
|
||||
* Migration history is split per owner instead of sharing one `public.migrations`
|
||||
* table:
|
||||
*
|
||||
* - IAM migrations ship with `@tria-plc/iamapi-common`, target the `iam` schema
|
||||
* and are recorded in `iam.typeorm_migrations` — the same table the package's
|
||||
* own CLI (`pnpm iam:migration:run|show|revert`) uses, so both paths agree on
|
||||
* what has been applied.
|
||||
* - Freight migrations are recorded in `freight.migrations`.
|
||||
*
|
||||
* `public.migrations` is the pre-split table; `src/scripts/migrate.ts` adopts its
|
||||
* rows into the two tables above on first run and then leaves it untouched.
|
||||
*/
|
||||
export const IAM_MIGRATIONS = {
|
||||
schema: "iam",
|
||||
table: "typeorm_migrations",
|
||||
} as const;
|
||||
|
||||
export const FREIGHT_MIGRATIONS = {
|
||||
schema: "freight",
|
||||
table: "migrations",
|
||||
} as const;
|
||||
|
||||
export const LEGACY_MIGRATIONS = {
|
||||
schema: "public",
|
||||
table: "migrations",
|
||||
} as const;
|
||||
|
||||
function buildConnectionOptions() {
|
||||
return {
|
||||
type: "postgres",
|
||||
type: "postgres" as const,
|
||||
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",
|
||||
// NOTE: do NOT pass `extra.options: '-c search_path=...'`. That sends the
|
||||
// Postgres startup `options` parameter, which connection poolers (PgBouncer /
|
||||
// proxies fronting the remote edr_dev DB) reject with
|
||||
// `08P01 unsupported startup parameter in options: search_path`.
|
||||
// The search_path is instead applied per-connection via a pool `connect`
|
||||
// handler in app.module.ts (see setPoolSearchPath).
|
||||
entities: [__dirname + "/../**/*.entity.{ts,js}", ...iamEntities],
|
||||
migrations: [
|
||||
iamMigrationsGlob,
|
||||
freightMigrationsGlob,
|
||||
],
|
||||
migrationsTransactionMode: "each",
|
||||
// handler (app.module.ts `setPoolSearchPath`, migrate.ts `applySearchPath`).
|
||||
synchronize: false,
|
||||
logging:
|
||||
process.env.TYPEORM_LOGGING === "true" ? true : ["error", "warn"],
|
||||
process.env.TYPEORM_LOGGING === "true"
|
||||
? true
|
||||
: (["error", "warn"] as DataSourceOptions["logging"]),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime options for the API (and the seed scripts using `AppDataSource`).
|
||||
* Carries no migrations: migrations run only through `src/scripts/migrate.ts`,
|
||||
* which uses the two dedicated DataSources below.
|
||||
*/
|
||||
export function buildDataSourceOptions(): DataSourceOptions {
|
||||
return {
|
||||
...buildConnectionOptions(),
|
||||
schema: "public",
|
||||
entities: [__dirname + "/../**/*.entity.{ts,js}", ...iamEntities],
|
||||
migrations: [],
|
||||
};
|
||||
}
|
||||
|
||||
/** IAM migrations only, recorded in `iam.typeorm_migrations`. */
|
||||
export function buildIamMigrationDataSourceOptions(): DataSourceOptions {
|
||||
return {
|
||||
...buildConnectionOptions(),
|
||||
schema: IAM_MIGRATIONS.schema,
|
||||
entities: [],
|
||||
migrations: [iamMigrationsGlob],
|
||||
migrationsTableName: IAM_MIGRATIONS.table,
|
||||
migrationsTransactionMode: "each",
|
||||
};
|
||||
}
|
||||
|
||||
/** Freight migrations only, recorded in `freight.migrations`. */
|
||||
export function buildFreightMigrationDataSourceOptions(): DataSourceOptions {
|
||||
return {
|
||||
...buildConnectionOptions(),
|
||||
schema: FREIGHT_MIGRATIONS.schema,
|
||||
entities: [],
|
||||
migrations: [freightMigrationsGlob],
|
||||
migrationsTableName: FREIGHT_MIGRATIONS.table,
|
||||
migrationsTransactionMode: "each",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user