T-2: M0.2 — Finish shared Postgres migrations: bun run db:migrate + fresh-database schema test
This commit is contained in:
parent
890761e0d5
commit
654782a6a1
6 changed files with 508 additions and 5 deletions
139
scripts/db-migrate.ts
Normal file
139
scripts/db-migrate.ts
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
import { getLogger } from '@stock-bot/logger';
|
||||
import {
|
||||
PostgreSQLClient,
|
||||
runPostgresMigrations,
|
||||
type MigrationTrack,
|
||||
type PostgreSQLClientConfig,
|
||||
} from '@stock-bot/postgres';
|
||||
import {
|
||||
assertEnvironmentSafety,
|
||||
initializeStockConfig,
|
||||
type StockAppConfig,
|
||||
} from '@stock-bot/stock-config';
|
||||
|
||||
const VALID_TRACKS = new Set<MigrationTrack>(['master', 'research', 'all']);
|
||||
const TRACK_USAGE = 'Valid tracks: master|research|all';
|
||||
|
||||
export interface DbMigrateArgs {
|
||||
track: MigrationTrack;
|
||||
}
|
||||
|
||||
export interface MigrationTarget {
|
||||
clientConfig: PostgreSQLClientConfig;
|
||||
targetLabel: string;
|
||||
}
|
||||
|
||||
function isValidTrack(value: string): value is MigrationTrack {
|
||||
return VALID_TRACKS.has(value as MigrationTrack);
|
||||
}
|
||||
|
||||
export function parseDbMigrateArgs(argv: readonly string[]): DbMigrateArgs {
|
||||
let track: MigrationTrack = 'all';
|
||||
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const arg = argv[i];
|
||||
if (arg === '--track') {
|
||||
const value = argv[i + 1];
|
||||
if (!value || value.startsWith('-')) {
|
||||
throw new Error(`Missing value for --track. ${TRACK_USAGE}`);
|
||||
}
|
||||
if (!isValidTrack(value)) {
|
||||
throw new Error(`Invalid --track value "${value}". ${TRACK_USAGE}`);
|
||||
}
|
||||
track = value;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg.startsWith('--track=')) {
|
||||
const value = arg.slice('--track='.length);
|
||||
if (!isValidTrack(value)) {
|
||||
throw new Error(`Invalid --track value "${value}". ${TRACK_USAGE}`);
|
||||
}
|
||||
track = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new Error(`Unknown argument "${arg}". Usage: db:migrate [--track master|research|all]`);
|
||||
}
|
||||
|
||||
return { track };
|
||||
}
|
||||
|
||||
export function resolveMigrationTarget(
|
||||
config: StockAppConfig,
|
||||
env: Record<string, string | undefined> = process.env
|
||||
): MigrationTarget {
|
||||
const pg = config.database.postgres;
|
||||
if (!pg.enabled) {
|
||||
throw new Error('PostgreSQL is disabled in the active configuration.');
|
||||
}
|
||||
|
||||
// Disable unrelated MongoDB checks so Postgres migrations only enforce PostgreSQL/environment guards.
|
||||
assertEnvironmentSafety(
|
||||
{
|
||||
...config,
|
||||
database: {
|
||||
...config.database,
|
||||
mongodb: {
|
||||
...config.database.mongodb,
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
'dataPipeline',
|
||||
env
|
||||
);
|
||||
|
||||
const clientConfig: PostgreSQLClientConfig = {
|
||||
host: pg.host,
|
||||
port: pg.port,
|
||||
database: pg.database,
|
||||
username: pg.user,
|
||||
password: pg.password,
|
||||
ssl: { enabled: pg.ssl, rejectUnauthorized: true },
|
||||
poolSettings: {
|
||||
min: 0,
|
||||
max: pg.poolSize,
|
||||
idleTimeoutMillis: pg.idleTimeout,
|
||||
},
|
||||
timeouts: { connection: pg.connectionTimeout },
|
||||
};
|
||||
|
||||
return {
|
||||
clientConfig,
|
||||
targetLabel: `${pg.host}:${pg.port}/${pg.database}`,
|
||||
};
|
||||
}
|
||||
|
||||
async function main(argv: readonly string[]): Promise<void> {
|
||||
const { track } = parseDbMigrateArgs(argv);
|
||||
const config = initializeStockConfig('dataPipeline', { mongoReadOnly: true });
|
||||
const { clientConfig, targetLabel } = resolveMigrationTarget(config, process.env);
|
||||
const logger = getLogger('db-migrate');
|
||||
const client = new PostgreSQLClient(clientConfig, logger);
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
const applied = await runPostgresMigrations(client, { track });
|
||||
console.log(`Target: ${targetLabel} (track: ${track})`);
|
||||
if (applied.length === 0) {
|
||||
console.log('nothing to apply');
|
||||
} else {
|
||||
console.log(`Applied ${applied.length} migration(s):`);
|
||||
for (const filename of applied) {
|
||||
console.log(` - ${filename}`);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await client.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
main(process.argv.slice(2)).catch(error => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error(`db:migrate failed: ${message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue