T-2: M0.2 — Finish shared Postgres migrations: bun run db:migrate + fresh-database schema test
This commit is contained in:
parent
890761e0d5
commit
654782a6a1
6 changed files with 508 additions and 5 deletions
143
scripts/test/db-migrate.test.ts
Normal file
143
scripts/test/db-migrate.test.ts
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
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';
|
||||
|
||||
const root = fileURLToPath(new URL('../../', import.meta.url));
|
||||
|
||||
function makeConfig(overrides: {
|
||||
environment?: StockAppConfig['environment'];
|
||||
postgres?: Partial<StockAppConfig['database']['postgres']>;
|
||||
}): StockAppConfig {
|
||||
return stockAppSchema.parse({
|
||||
name: 'stock-bot',
|
||||
version: '1.0.0',
|
||||
environment: overrides.environment ?? 'development',
|
||||
service: {
|
||||
name: 'data-pipeline',
|
||||
port: 2002,
|
||||
},
|
||||
log: {},
|
||||
database: {
|
||||
postgres: {
|
||||
enabled: true,
|
||||
host: 'localhost',
|
||||
port: 5433,
|
||||
database: 'trading_bot_dev',
|
||||
user: 'dev_user',
|
||||
password: 'dev_pass',
|
||||
ssl: false,
|
||||
poolSize: 10,
|
||||
connectionTimeout: 30000,
|
||||
idleTimeout: 10000,
|
||||
...overrides.postgres,
|
||||
},
|
||||
mongodb: {
|
||||
enabled: true,
|
||||
uri: 'mongodb://dev_admin:dev_mongo_pass@localhost:27018/stock_dev?authSource=admin',
|
||||
database: 'stock_dev',
|
||||
poolSize: 10,
|
||||
},
|
||||
questdb: {},
|
||||
dragonfly: {},
|
||||
},
|
||||
providers: {},
|
||||
features: {},
|
||||
});
|
||||
}
|
||||
|
||||
describe('parseDbMigrateArgs', () => {
|
||||
test('defaults to track "all" when no arguments are passed', () => {
|
||||
expect(parseDbMigrateArgs([])).toEqual({ track: 'all' });
|
||||
});
|
||||
|
||||
test('accepts each valid track with space or equals syntax', () => {
|
||||
for (const track of ['master', 'research', 'all'] as const) {
|
||||
expect(parseDbMigrateArgs(['--track', track])).toEqual({ track });
|
||||
expect(parseDbMigrateArgs([`--track=${track}`])).toEqual({ track });
|
||||
}
|
||||
});
|
||||
|
||||
test('rejects invalid track values with message listing valid tracks', () => {
|
||||
expect(() => parseDbMigrateArgs(['--track', 'bogus'])).toThrow(/master\|research\|all/);
|
||||
expect(() => parseDbMigrateArgs(['--track=bogus'])).toThrow(/master\|research\|all/);
|
||||
});
|
||||
|
||||
test('rejects missing track value and unknown arguments', () => {
|
||||
expect(() => parseDbMigrateArgs(['--track'])).toThrow(/master\|research\|all/);
|
||||
expect(() => parseDbMigrateArgs(['--track', '--other'])).toThrow(/master\|research\|all/);
|
||||
expect(() => parseDbMigrateArgs(['--unknown'])).toThrow(/master\|research\|all/);
|
||||
expect(() => parseDbMigrateArgs(['positional'])).toThrow(/master\|research\|all/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveMigrationTarget', () => {
|
||||
test('rejects development targeting QA PostgreSQL on localhost:5432/trading_bot', () => {
|
||||
const config = makeConfig({
|
||||
environment: 'development',
|
||||
postgres: { host: 'localhost', port: 5432, database: 'trading_bot' },
|
||||
});
|
||||
expect(() => resolveMigrationTarget(config, {})).toThrow(/QA PostgreSQL/i);
|
||||
});
|
||||
|
||||
test('rejects development targeting QA PostgreSQL on 127.0.0.1:5432/trading_bot', () => {
|
||||
const config = makeConfig({
|
||||
environment: 'development',
|
||||
postgres: { host: '127.0.0.1', port: 5432, database: 'trading_bot' },
|
||||
});
|
||||
expect(() => resolveMigrationTarget(config, {})).toThrow(/QA PostgreSQL/i);
|
||||
});
|
||||
|
||||
test('accepts development targeting isolated dev PostgreSQL on localhost:5433/trading_bot_dev', () => {
|
||||
const config = makeConfig({
|
||||
environment: 'development',
|
||||
postgres: {
|
||||
host: 'localhost',
|
||||
port: 5433,
|
||||
database: 'trading_bot_dev',
|
||||
user: 'dev_user',
|
||||
password: 'dev_pass',
|
||||
},
|
||||
});
|
||||
const resolved = resolveMigrationTarget(config, {});
|
||||
expect(resolved.targetLabel).toBe('localhost:5433/trading_bot_dev');
|
||||
expect(resolved.clientConfig).toEqual({
|
||||
host: 'localhost',
|
||||
port: 5433,
|
||||
database: 'trading_bot_dev',
|
||||
username: 'dev_user',
|
||||
password: 'dev_pass',
|
||||
ssl: { enabled: false, rejectUnauthorized: true },
|
||||
poolSettings: { min: 0, max: 10, idleTimeoutMillis: 10000 },
|
||||
timeouts: { connection: 30000 },
|
||||
});
|
||||
});
|
||||
|
||||
test('rejects qa targeting a _dev database', () => {
|
||||
const config = makeConfig({
|
||||
environment: 'qa',
|
||||
postgres: { host: 'localhost', port: 5433, database: 'trading_bot_dev' },
|
||||
});
|
||||
expect(() => resolveMigrationTarget(config, {})).toThrow(/dev PostgreSQL/i);
|
||||
});
|
||||
|
||||
test('rejects disabled PostgreSQL configuration', () => {
|
||||
const config = makeConfig({
|
||||
environment: 'development',
|
||||
postgres: { enabled: false },
|
||||
});
|
||||
expect(() => resolveMigrationTarget(config, {})).toThrow(/disabled/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(
|
||||
[process.execPath, 'run', 'scripts/db-migrate.ts', '--track', 'bogus'],
|
||||
{ cwd: root }
|
||||
);
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
const stderr = new TextDecoder().decode(result.stderr);
|
||||
expect(stderr).toMatch(/master\|research\|all/);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue