refactor: pre-M0 cleanup, core library hardening, and isolated dev/QA Docker stack
- Remove ~44k LOC of dead code, legacy v1 TS simulator/strategy stack, event-bus, analytics FastAPI prototype, legacy calculation archives, and unused deps (data-ingestion provider API handlers untouched) - Extract @stock-bot/di/http-security and isolate @stock-bot/browser to data-ingestion via ServiceContainerBuilder.withRegistrations() - Harden @stock-bot/mongodb read-only proxies, flatten @stock-bot/postgres with unified master+research migrations, and fix @stock-bot/config EnvLoader, ServiceApplication worker lifecycle, NamespacedCache.clear(), and BatchProcessor error propagation - Flatten workspace root (remove apps/stock/package.json) and collapse orchestrator re-export shims - Pin all Docker image versions, add shm_size/nofile/RAM caps and docker-compose.quant.yml, fix 05-add-visible-column.sql view recreation, and decouple dev/QA Compose project names for side-by-side execution - Update PLATFORM-PLAN, DATA-CONTRACTS (bars_1m PARTITION BY MONTH, Rust bulk ETL, DuckDB limits), MILESTONES, and servers docs
This commit is contained in:
parent
ce9e2df67c
commit
4cd6038063
432 changed files with 1247 additions and 44469 deletions
113
libs/data/postgres/src/migrations.ts
Normal file
113
libs/data/postgres/src/migrations.ts
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { PostgreSQLClient } from './client';
|
||||
|
||||
const logger = getLogger('postgres-migrations');
|
||||
|
||||
export type MigrationTrack = 'master' | 'research' | 'all';
|
||||
|
||||
const MASTER_FILES = new Set([
|
||||
'01-init-database.sql',
|
||||
'02-master-schema.sql',
|
||||
'03-initial-data.sql',
|
||||
'04-provider-exchange-mapping-schema.sql',
|
||||
'05-add-visible-column.sql',
|
||||
]);
|
||||
|
||||
const RESEARCH_FILES = new Set([
|
||||
'09-backtest-run-contract.sql',
|
||||
'10-durable-backtest-dispatch.sql',
|
||||
'11-simulation-execution-ledger.sql',
|
||||
'12-historical-datasets.sql',
|
||||
'13-strategy-recovery.sql',
|
||||
'14-backtest-submission.sql',
|
||||
'15-canonical-paper-intents.sql',
|
||||
'16-strategy-catalog.sql',
|
||||
'17-research-retention.sql',
|
||||
'18-backtest-strategy-versions.sql',
|
||||
'19-canonical-research.sql',
|
||||
'20-research-workspace.sql',
|
||||
'21-run-reproduction.sql',
|
||||
'22-experiments.sql',
|
||||
'23-experiment-retries.sql',
|
||||
'24-chronological-validation.sql',
|
||||
'25-walk-forward.sql',
|
||||
'26-cost-sensitivity.sql',
|
||||
'27-research-evidence-sources.sql',
|
||||
'28-research-agents.sql',
|
||||
'29-isolated-candidates.sql',
|
||||
'30-paper-deployments.sql',
|
||||
'31-research-agent-project-links.sql',
|
||||
'32-candidate-parameter-defaults.sql',
|
||||
'33-candidate-decimal-normalization.sql',
|
||||
]);
|
||||
|
||||
function locateMigrationsDir(): string {
|
||||
let directory = dirname(fileURLToPath(import.meta.url));
|
||||
while (!existsSync(join(directory, 'database', 'postgres', 'init'))) {
|
||||
const parent = dirname(directory);
|
||||
if (parent === directory) {
|
||||
throw new Error('Cannot locate PostgreSQL migrations directory (database/postgres/init)');
|
||||
}
|
||||
directory = parent;
|
||||
}
|
||||
return join(directory, 'database', 'postgres', 'init');
|
||||
}
|
||||
|
||||
function matchesTrack(file: string, track: MigrationTrack): boolean {
|
||||
if (!file.endsWith('.sql')) {
|
||||
return false;
|
||||
}
|
||||
const inMaster = MASTER_FILES.has(file);
|
||||
const inResearch = file.startsWith('001_') || RESEARCH_FILES.has(file);
|
||||
if (track === 'master') {
|
||||
return inMaster;
|
||||
}
|
||||
if (track === 'research') {
|
||||
return inResearch;
|
||||
}
|
||||
return inMaster || inResearch;
|
||||
}
|
||||
|
||||
export async function runPostgresMigrations(
|
||||
postgres: Pick<PostgreSQLClient, 'transaction'>,
|
||||
options: { track?: MigrationTrack; migrationsDir?: string } = {}
|
||||
): Promise<string[]> {
|
||||
const track = options.track ?? 'research';
|
||||
const applied: string[] = [];
|
||||
|
||||
await postgres.transaction(async client => {
|
||||
await client.query('SELECT pg_advisory_xact_lock($1, $2)', [0x53544254, 1]);
|
||||
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS migrations (
|
||||
id SERIAL PRIMARY KEY,
|
||||
filename VARCHAR(255) NOT NULL UNIQUE,
|
||||
executed_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
|
||||
)
|
||||
`);
|
||||
|
||||
const migrationsDir = options.migrationsDir ?? locateMigrationsDir();
|
||||
const files = readdirSync(migrationsDir)
|
||||
.filter(f => matchesTrack(f, track))
|
||||
.sort();
|
||||
|
||||
for (const file of files) {
|
||||
const result = await client.query('SELECT 1 FROM migrations WHERE filename = $1', [file]);
|
||||
if (result.rows.length === 0) {
|
||||
logger.info(`Running migration: ${file}`);
|
||||
const sql = readFileSync(join(migrationsDir, file), 'utf8');
|
||||
await client.query(sql);
|
||||
await client.query(
|
||||
'INSERT INTO migrations (filename) VALUES ($1) ON CONFLICT (filename) DO NOTHING',
|
||||
[file]
|
||||
);
|
||||
applied.push(file);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return applied;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue