From c2db2d749579bdc9683933ad3324883421625046 Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 17:41:55 -0500 Subject: [PATCH 1/2] =?UTF-8?q?T-2:=20M0.2=20=E2=80=94=20Finish=20shared?= =?UTF-8?q?=20Postgres=20migrations:=20`bun=20run=20db:migrate`=20+=20fres?= =?UTF-8?q?h-database=20schema=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/test/db-migrate.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/test/db-migrate.test.ts b/scripts/test/db-migrate.test.ts index 37218d5b..3fa57ca5 100644 --- a/scripts/test/db-migrate.test.ts +++ b/scripts/test/db-migrate.test.ts @@ -1,4 +1,4 @@ -import { mkdtempSync, rmSync } from 'node:fs'; +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; @@ -149,6 +149,8 @@ function runInitInIsolatedSubprocess( 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-')); + // Stop the config loader's upward .env search inside the temp directory. + writeFileSync(join(isolatedDir, '.env'), '', 'utf8'); const script = mode === 'load-only' ? `import { loadMigrationConfig } from ${JSON.stringify(dbMigrateModuleUrl)}; From fb7e7860106522a7fe4b1b9841dbb7bece919ca0 Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 17:46:42 -0500 Subject: [PATCH 2/2] T-2: tests --- scripts/test/db-migrate.test.ts | 109 +++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/scripts/test/db-migrate.test.ts b/scripts/test/db-migrate.test.ts index 3fa57ca5..8a731e2b 100644 --- a/scripts/test/db-migrate.test.ts +++ b/scripts/test/db-migrate.test.ts @@ -1,4 +1,4 @@ -import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; @@ -278,7 +278,114 @@ describe('loadMigrationConfig (real initialization path, isolated subprocess)', }); }); +describe('config loader .env isolation used by runInitInIsolatedSubprocess', () => { + // Arrange a parent dir whose .env would flip the dev target to QA PostgreSQL and leak a sentinel, + // then load config from a child dir with or without its own empty .env. + function loadFromChild(childHasEmptyEnv: boolean): { + exitCode: number; + stdout: string; + stderr: string; + } { + const parentDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-parent-')); + const childDir = join(parentDir, 'child'); + try { + mkdirSync(childDir); + writeFileSync( + join(parentDir, '.env'), + [ + 'DB_MIGRATE_ENV_LEAK_SENTINEL=leaked', + 'POSTGRES_PORT=5432', + 'POSTGRES_DATABASE=trading_bot', + 'DATABASE_POSTGRES_PORT=5432', + 'DATABASE_POSTGRES_DATABASE=trading_bot', + ].join('\n'), + 'utf8' + ); + if (childHasEmptyEnv) { + writeFileSync(join(childDir, '.env'), '', 'utf8'); + } + const script = `import { loadMigrationConfig, resolveMigrationTarget } from ${JSON.stringify(dbMigrateModuleUrl)}; +const config = loadMigrationConfig(); +const resolved = resolveMigrationTarget(config, process.env); +console.log(JSON.stringify({ sentinel: process.env.DB_MIGRATE_ENV_LEAK_SENTINEL ?? null, targetLabel: resolved.targetLabel }));`; + const result = Bun.spawnSync([process.execPath, '-e', script], { + cwd: childDir, + env: { + PATH: process.env.PATH ?? '', + HOME: childDir, + NODE_ENV: 'development', + POSTGRES_HOST: 'localhost', + DATABASE_POSTGRES_HOST: 'localhost', + POSTGRES_USERNAME: 'dev_user', + POSTGRES_PASSWORD: 'dev_pass', + }, + }); + return { + exitCode: result.exitCode, + stdout: new TextDecoder().decode(result.stdout).trim(), + stderr: new TextDecoder().decode(result.stderr).trim(), + }; + } finally { + rmSync(parentDir, { recursive: true, force: true }); + } + } + + test('an empty .env in the working directory stops the loader from reading a parent .env', () => { + const result = loadFromChild(true); + + expect(result.exitCode).toBe(0); + const parsed = JSON.parse(result.stdout) as { sentinel: string | null; targetLabel: string }; + expect(parsed.sentinel).toBeNull(); + // Defaults (no parent override) resolve to the isolated dev database, not QA. + expect(parsed.targetLabel).not.toBe('localhost:5432/trading_bot'); + }); + + test('without the empty .env the parent .env leaks in and the QA target is rejected', () => { + const result = loadFromChild(false); + + expect(result.exitCode).not.toBe(0); + expect(result.stderr).toMatch(/QA PostgreSQL/i); + }); +}); + describe('db-migrate CLI subprocess', () => { + test( + 'exits non-zero with a clear message when the dev PostgreSQL target is unreachable', + () => { + const isolatedDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-unreachable-')); + try { + writeFileSync(join(isolatedDir, '.env'), '', 'utf8'); + const result = Bun.spawnSync( + [process.execPath, 'run', join(root, 'scripts/db-migrate.ts'), '--track', 'master'], + { + cwd: isolatedDir, + env: { + PATH: process.env.PATH ?? '', + HOME: isolatedDir, + NODE_ENV: 'development', + POSTGRES_HOST: '127.0.0.1', + POSTGRES_PORT: '1', + POSTGRES_DATABASE: 'trading_bot_dev', + DATABASE_POSTGRES_HOST: '127.0.0.1', + DATABASE_POSTGRES_PORT: '1', + DATABASE_POSTGRES_DATABASE: 'trading_bot_dev', + }, + timeout: 30000, + } + ); + const stdout = new TextDecoder().decode(result.stdout); + const stderr = new TextDecoder().decode(result.stderr); + + expect(result.exitCode).toBe(1); + expect(stderr).toMatch(/db:migrate failed: .*connect/i); + expect(stdout).not.toMatch(/Applied \d+ migration/); + } finally { + rmSync(isolatedDir, { recursive: true, force: true }); + } + }, + 40000 + ); + test('exits non-zero with valid tracks on stderr for --track bogus without connecting to DB', () => { const result = Bun.spawnSync( [process.execPath, 'run', 'scripts/db-migrate.ts', '--track', 'bogus'],