From b82fc7c905e26ec1ed02894787d20b01babdad11 Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 10:39:55 -0500 Subject: [PATCH] =?UTF-8?q?T-2:=20M0.2=20=E2=80=94=20Finish=20shared=20Pos?= =?UTF-8?q?tgres=20migrations:=20`bun=20run=20db:migrate`=20+=20fresh-data?= =?UTF-8?q?base=20schema=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/stock/config/src/config-instance.ts | 6 +- scripts/db-migrate.ts | 12 ++- scripts/test/db-migrate.test.ts | 103 ++++++++++++++++++++++- 3 files changed, 114 insertions(+), 7 deletions(-) diff --git a/apps/stock/config/src/config-instance.ts b/apps/stock/config/src/config-instance.ts index b5eda87a..d949c8f8 100644 --- a/apps/stock/config/src/config-instance.ts +++ b/apps/stock/config/src/config-instance.ts @@ -13,6 +13,7 @@ const __dirname = path.dirname(__filename); export interface InitializeStockConfigOptions { mongoReadOnly?: boolean; + postgresOnly?: boolean; } /** @@ -33,14 +34,15 @@ export function initializeStockConfig( const rawConfig = configInstance.initialize(stockAppSchema); const config: StockAppConfig = - options?.mongoReadOnly === true + options?.mongoReadOnly === true || options?.postgresOnly === true ? { ...rawConfig, database: { ...rawConfig.database, mongodb: { ...rawConfig.database.mongodb, - readOnly: true, + ...(options?.mongoReadOnly === true ? { readOnly: true } : {}), + ...(options?.postgresOnly === true ? { enabled: false } : {}), }, }, } diff --git a/scripts/db-migrate.ts b/scripts/db-migrate.ts index d8f6434f..3ef20669 100644 --- a/scripts/db-migrate.ts +++ b/scripts/db-migrate.ts @@ -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 = 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 { 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); diff --git a/scripts/test/db-migrate.test.ts b/scripts/test/db-migrate.test.ts index 2d5793a5..d7b05d52 100644 --- a/scripts/test/db-migrate.test.ts +++ b/scripts/test/db-migrate.test.ts @@ -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, fn: () => void): void { + const previous = new Map(); + 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(