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
|
|
@ -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