From f36bdbd6bd9eeafb983c919dec64f71e44bf1eaa Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 10:43:36 -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 | 9 +- scripts/db-migrate.ts | 3 + scripts/test/db-migrate.test.ts | 181 ++++++++++++++--------- 3 files changed, 125 insertions(+), 68 deletions(-) diff --git a/apps/stock/config/src/config-instance.ts b/apps/stock/config/src/config-instance.ts index d949c8f8..04187ff1 100644 --- a/apps/stock/config/src/config-instance.ts +++ b/apps/stock/config/src/config-instance.ts @@ -42,7 +42,14 @@ export function initializeStockConfig( mongodb: { ...rawConfig.database.mongodb, ...(options?.mongoReadOnly === true ? { readOnly: true } : {}), - ...(options?.postgresOnly === true ? { enabled: false } : {}), + ...(options?.postgresOnly === true + ? { + enabled: false, + uri: 'mongodb://disabled', + readUri: undefined, + password: undefined, + } + : {}), }, }, } diff --git a/scripts/db-migrate.ts b/scripts/db-migrate.ts index 3ef20669..c5e2b3e5 100644 --- a/scripts/db-migrate.ts +++ b/scripts/db-migrate.ts @@ -84,6 +84,9 @@ export function resolveMigrationTarget( mongodb: { ...config.database.mongodb, enabled: false, + uri: 'mongodb://disabled', + readUri: undefined, + password: undefined, }, }, }, diff --git a/scripts/test/db-migrate.test.ts b/scripts/test/db-migrate.test.ts index d7b05d52..37218d5b 100644 --- a/scripts/test/db-migrate.test.ts +++ b/scripts/test/db-migrate.test.ts @@ -1,9 +1,13 @@ -import { fileURLToPath } from 'node:url'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; import { describe, expect, test } from 'bun:test'; -import { resetStockConfig, stockAppSchema, type StockAppConfig } from '@stock-bot/stock-config'; -import { loadMigrationConfig, parseDbMigrateArgs, resolveMigrationTarget } from '../db-migrate'; +import { stockAppSchema, type StockAppConfig } from '@stock-bot/stock-config'; +import { parseDbMigrateArgs, resolveMigrationTarget } from '../db-migrate'; const root = fileURLToPath(new URL('../../', import.meta.url)); +const dbMigrateModuleUrl = pathToFileURL(join(root, 'scripts/db-migrate.ts')).href; function makeConfig(overrides: { environment?: StockAppConfig['environment']; @@ -140,59 +144,64 @@ 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(); +function runInitInIsolatedSubprocess( + env: Record, + mode: 'load-and-resolve' | 'load-only' = 'load-and-resolve' +): { exitCode: number; stdout: string; stderr: string } { + const isolatedDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-env-')); + const script = + mode === 'load-only' + ? `import { loadMigrationConfig } from ${JSON.stringify(dbMigrateModuleUrl)}; +const config = loadMigrationConfig(); +console.log(JSON.stringify({ mongoEnabled: config.database.mongodb.enabled }));` + : `import { loadMigrationConfig, resolveMigrationTarget } from ${JSON.stringify(dbMigrateModuleUrl)}; +const config = loadMigrationConfig(); +const resolved = resolveMigrationTarget(config, process.env); +console.log(JSON.stringify({ mongoEnabled: config.database.mongodb.enabled, targetLabel: resolved.targetLabel }));`; + try { - fn(); + const result = Bun.spawnSync([process.execPath, '-e', script], { + cwd: isolatedDir, + env: { + PATH: process.env.PATH ?? '', + HOME: isolatedDir, + ...env, + }, + }); + return { + exitCode: result.exitCode, + stdout: new TextDecoder().decode(result.stdout).trim(), + stderr: new TextDecoder().decode(result.stderr).trim(), + }; } finally { - resetStockConfig(); - for (const [key, value] of previous.entries()) { - if (value === undefined) { - delete process.env[key]; - } else { - process.env[key] = value; - } - } + rmSync(isolatedDir, { recursive: true, force: true }); } } -describe('loadMigrationConfig (real initialization path)', () => { +describe('loadMigrationConfig (real initialization path, isolated subprocess)', () => { 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'); - } - ); + const result = runInitInIsolatedSubprocess({ + 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', + }); + + expect(result.exitCode).toBe(0); + expect(JSON.parse(result.stdout)).toEqual({ + mongoEnabled: false, + targetLabel: 'localhost:5433/trading_bot_dev', + }); }); test('rejects development targeting QA PostgreSQL during real config initialization', () => { - withEnv( + const result = runInitInIsolatedSubprocess( { NODE_ENV: 'development', POSTGRES_HOST: 'localhost', @@ -201,31 +210,69 @@ describe('loadMigrationConfig (real initialization path)', () => { 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); - } + 'load-only' ); + + expect(result.exitCode).not.toBe(0); + expect(result.stderr).toMatch(/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); - } - ); + const result = runInitInIsolatedSubprocess({ + 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', + }); + + expect(result.exitCode).not.toBe(0); + expect(result.stderr).toMatch(/QA PostgreSQL/i); + }); + + test('succeeds in production with unused MongoDB placeholder credential while still rejecting dev Postgres password', () => { + const okResult = runInitInIsolatedSubprocess({ + NODE_ENV: 'production', + POSTGRES_HOST: 'pg.prod.internal', + POSTGRES_PORT: '5432', + POSTGRES_DATABASE: 'trading_bot', + POSTGRES_USERNAME: 'prod_user', + POSTGRES_PASSWORD: 'strong_prod_pg_secret_987', + DATABASE_POSTGRES_HOST: 'pg.prod.internal', + DATABASE_POSTGRES_PORT: '5432', + DATABASE_POSTGRES_DATABASE: 'trading_bot', + DATABASE_POSTGRES_PASSWORD: 'strong_prod_pg_secret_987', + MONGODB_URI: 'mongodb://prod:trading_mongo_dev@mongo.internal:27017/stock?authSource=admin', + DRAGONFLY_PASSWORD: 'strong_prod_redis_secret_987', + }); + + expect(okResult.exitCode).toBe(0); + expect(JSON.parse(okResult.stdout)).toEqual({ + mongoEnabled: false, + targetLabel: 'pg.prod.internal:5432/trading_bot', + }); + + const badPgResult = runInitInIsolatedSubprocess({ + NODE_ENV: 'production', + POSTGRES_HOST: 'pg.prod.internal', + POSTGRES_PORT: '5432', + POSTGRES_DATABASE: 'trading_bot', + POSTGRES_USERNAME: 'prod_user', + POSTGRES_PASSWORD: 'trading_pass_dev', + DATABASE_POSTGRES_HOST: 'pg.prod.internal', + DATABASE_POSTGRES_PORT: '5432', + DATABASE_POSTGRES_DATABASE: 'trading_bot', + DATABASE_POSTGRES_PASSWORD: 'trading_pass_dev', + MONGODB_URI: 'mongodb://prod:trading_mongo_dev@mongo.internal:27017/stock?authSource=admin', + DRAGONFLY_PASSWORD: 'strong_prod_redis_secret_987', + }); + + expect(badPgResult.exitCode).not.toBe(0); + expect(badPgResult.stderr).toMatch(/production PostgreSQL password/i); }); });