T-2: M0.2 — Finish shared Postgres migrations: bun run db:migrate + fresh-database schema test
This commit is contained in:
parent
654782a6a1
commit
b82fc7c905
3 changed files with 114 additions and 7 deletions
|
|
@ -60,6 +60,10 @@ export function parseDbMigrateArgs(argv: readonly string[]): DbMigrateArgs {
|
|||
return { track };
|
||||
}
|
||||
|
||||
export function loadMigrationConfig(): StockAppConfig {
|
||||
return initializeStockConfig('dataPipeline', { postgresOnly: true });
|
||||
}
|
||||
|
||||
export function resolveMigrationTarget(
|
||||
config: StockAppConfig,
|
||||
env: Record<string, string | undefined> = process.env
|
||||
|
|
@ -69,7 +73,9 @@ export function resolveMigrationTarget(
|
|||
throw new Error('PostgreSQL is disabled in the active configuration.');
|
||||
}
|
||||
|
||||
// Disable unrelated MongoDB checks so Postgres migrations only enforce PostgreSQL/environment guards.
|
||||
// Never allow the shared dev-on-QA override to bypass the PostgreSQL guard in db:migrate,
|
||||
// and disable unrelated MongoDB checks so Postgres migrations only enforce PostgreSQL/environment guards.
|
||||
const { STOCK_BOT_ALLOW_DEV_ON_QA_DB: _ignored, ...safeEnv } = env;
|
||||
assertEnvironmentSafety(
|
||||
{
|
||||
...config,
|
||||
|
|
@ -82,7 +88,7 @@ export function resolveMigrationTarget(
|
|||
},
|
||||
},
|
||||
'dataPipeline',
|
||||
env
|
||||
safeEnv
|
||||
);
|
||||
|
||||
const clientConfig: PostgreSQLClientConfig = {
|
||||
|
|
@ -108,7 +114,7 @@ export function resolveMigrationTarget(
|
|||
|
||||
async function main(argv: readonly string[]): Promise<void> {
|
||||
const { track } = parseDbMigrateArgs(argv);
|
||||
const config = initializeStockConfig('dataPipeline', { mongoReadOnly: true });
|
||||
const config = loadMigrationConfig();
|
||||
const { clientConfig, targetLabel } = resolveMigrationTarget(config, process.env);
|
||||
const logger = getLogger('db-migrate');
|
||||
const client = new PostgreSQLClient(clientConfig, logger);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { fileURLToPath } from 'node:url';
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import { stockAppSchema, type StockAppConfig } from '@stock-bot/stock-config';
|
||||
import { parseDbMigrateArgs, resolveMigrationTarget } from '../db-migrate';
|
||||
import { resetStockConfig, stockAppSchema, type StockAppConfig } from '@stock-bot/stock-config';
|
||||
import { loadMigrationConfig, parseDbMigrateArgs, resolveMigrationTarget } from '../db-migrate';
|
||||
|
||||
const root = fileURLToPath(new URL('../../', import.meta.url));
|
||||
|
||||
|
|
@ -121,6 +121,16 @@ describe('resolveMigrationTarget', () => {
|
|||
expect(() => resolveMigrationTarget(config, {})).toThrow(/dev PostgreSQL/i);
|
||||
});
|
||||
|
||||
test('rejects development targeting QA PostgreSQL even when STOCK_BOT_ALLOW_DEV_ON_QA_DB=true', () => {
|
||||
const config = makeConfig({
|
||||
environment: 'development',
|
||||
postgres: { host: 'localhost', port: 5432, database: 'trading_bot' },
|
||||
});
|
||||
expect(() =>
|
||||
resolveMigrationTarget(config, { STOCK_BOT_ALLOW_DEV_ON_QA_DB: 'true' })
|
||||
).toThrow(/QA PostgreSQL/i);
|
||||
});
|
||||
|
||||
test('rejects disabled PostgreSQL configuration', () => {
|
||||
const config = makeConfig({
|
||||
environment: 'development',
|
||||
|
|
@ -130,6 +140,95 @@ describe('resolveMigrationTarget', () => {
|
|||
});
|
||||
});
|
||||
|
||||
function withEnv(overrides: Record<string, string | undefined>, fn: () => void): void {
|
||||
const previous = new Map<string, string | undefined>();
|
||||
for (const [key, value] of Object.entries(overrides)) {
|
||||
previous.set(key, process.env[key]);
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
resetStockConfig();
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
resetStockConfig();
|
||||
for (const [key, value] of previous.entries()) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe('loadMigrationConfig (real initialization path)', () => {
|
||||
test('succeeds for dev PostgreSQL even when QA MongoDB admin URI is set without MONGODB_READ_URI', () => {
|
||||
withEnv(
|
||||
{
|
||||
NODE_ENV: 'development',
|
||||
POSTGRES_HOST: 'localhost',
|
||||
POSTGRES_PORT: '5433',
|
||||
POSTGRES_DATABASE: 'trading_bot_dev',
|
||||
POSTGRES_USERNAME: 'dev_user',
|
||||
POSTGRES_PASSWORD: 'dev_pass',
|
||||
DATABASE_POSTGRES_HOST: 'localhost',
|
||||
DATABASE_POSTGRES_PORT: '5433',
|
||||
DATABASE_POSTGRES_DATABASE: 'trading_bot_dev',
|
||||
MONGODB_URI: 'mongodb://trading_admin:trading_mongo_dev@localhost:27017/stock?authSource=admin',
|
||||
MONGODB_READ_URI: undefined,
|
||||
STOCK_BOT_ALLOW_DEV_ON_QA_DB: undefined,
|
||||
},
|
||||
() => {
|
||||
const config = loadMigrationConfig();
|
||||
expect(config.database.mongodb.enabled).toBe(false);
|
||||
const resolved = resolveMigrationTarget(config, process.env);
|
||||
expect(resolved.targetLabel).toBe('localhost:5433/trading_bot_dev');
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects development targeting QA PostgreSQL during real config initialization', () => {
|
||||
withEnv(
|
||||
{
|
||||
NODE_ENV: 'development',
|
||||
POSTGRES_HOST: 'localhost',
|
||||
POSTGRES_PORT: '5432',
|
||||
POSTGRES_DATABASE: 'trading_bot',
|
||||
DATABASE_POSTGRES_HOST: 'localhost',
|
||||
DATABASE_POSTGRES_PORT: '5432',
|
||||
DATABASE_POSTGRES_DATABASE: 'trading_bot',
|
||||
STOCK_BOT_ALLOW_DEV_ON_QA_DB: undefined,
|
||||
},
|
||||
() => {
|
||||
expect(() => loadMigrationConfig()).toThrow(/QA PostgreSQL/i);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects development targeting QA PostgreSQL even when STOCK_BOT_ALLOW_DEV_ON_QA_DB=true in real env', () => {
|
||||
withEnv(
|
||||
{
|
||||
NODE_ENV: 'development',
|
||||
POSTGRES_HOST: 'localhost',
|
||||
POSTGRES_PORT: '5432',
|
||||
POSTGRES_DATABASE: 'trading_bot',
|
||||
DATABASE_POSTGRES_HOST: 'localhost',
|
||||
DATABASE_POSTGRES_PORT: '5432',
|
||||
DATABASE_POSTGRES_DATABASE: 'trading_bot',
|
||||
STOCK_BOT_ALLOW_DEV_ON_QA_DB: 'true',
|
||||
},
|
||||
() => {
|
||||
const config = loadMigrationConfig();
|
||||
expect(() => resolveMigrationTarget(config, process.env)).toThrow(/QA PostgreSQL/i);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('db-migrate CLI subprocess', () => {
|
||||
test('exits non-zero with valid tracks on stderr for --track bogus without connecting to DB', () => {
|
||||
const result = Bun.spawnSync(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue