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
|
|
@ -1,107 +1,18 @@
|
|||
import { existsSync, readdirSync, readFileSync } from 'fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname, join } from 'path';
|
||||
import type { IServiceContainer } from '@stock-bot/handlers';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { runPostgresMigrations } from '@stock-bot/postgres';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
|
||||
const logger = getLogger('migration-runner');
|
||||
|
||||
export async function runMigrations(container: IServiceContainer): Promise<void> {
|
||||
logger.info('Migration runner called');
|
||||
logger.info('Container type', { type: typeof container });
|
||||
logger.info('Container postgres available', { available: !!container.postgres });
|
||||
|
||||
if (!container.postgres) {
|
||||
logger.warn('PostgreSQL not available, skipping migrations');
|
||||
logger.info('Container keys:', Object.keys(container));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await container.postgres.transaction(async client => {
|
||||
// All startup migrations share one database-scoped lock on this transaction's client.
|
||||
// PostgreSQL releases it on commit/rollback, including when startup fails.
|
||||
await client.query('SELECT pg_advisory_xact_lock($1, $2)', [0x53544254, 1]);
|
||||
|
||||
// Create migrations table if it doesn't exist
|
||||
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()
|
||||
)
|
||||
`);
|
||||
|
||||
// Get list of migration files from database/postgres/init while holding the lock.
|
||||
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 = parent;
|
||||
}
|
||||
const migrationsDir = join(directory, 'database', 'postgres', 'init');
|
||||
logger.info('Looking for migrations', { directory: migrationsDir });
|
||||
|
||||
const files = readdirSync(migrationsDir)
|
||||
.filter(
|
||||
f =>
|
||||
f.endsWith('.sql') &&
|
||||
(f.startsWith('001_') ||
|
||||
f === '09-backtest-run-contract.sql' ||
|
||||
f === '10-durable-backtest-dispatch.sql' ||
|
||||
f === '11-simulation-execution-ledger.sql' ||
|
||||
f === '12-historical-datasets.sql' ||
|
||||
f === '13-strategy-recovery.sql' ||
|
||||
f === '14-backtest-submission.sql' ||
|
||||
f === '15-canonical-paper-intents.sql' ||
|
||||
f === '16-strategy-catalog.sql' ||
|
||||
f === '17-research-retention.sql' ||
|
||||
f === '18-backtest-strategy-versions.sql' ||
|
||||
f === '19-canonical-research.sql' ||
|
||||
f === '20-research-workspace.sql' ||
|
||||
f === '21-run-reproduction.sql' ||
|
||||
f === '22-experiments.sql' ||
|
||||
f === '23-experiment-retries.sql' ||
|
||||
f === '24-chronological-validation.sql' ||
|
||||
f === '25-walk-forward.sql' ||
|
||||
f === '26-cost-sensitivity.sql' ||
|
||||
f === '27-research-evidence-sources.sql' ||
|
||||
f === '28-research-agents.sql' ||
|
||||
f === '29-isolated-candidates.sql' ||
|
||||
f === '30-paper-deployments.sql' ||
|
||||
f === '31-research-agent-project-links.sql' ||
|
||||
f === '32-candidate-parameter-defaults.sql' ||
|
||||
f === '33-candidate-decimal-normalization.sql')
|
||||
) // Explicit additive migration allowlist; never replay destructive init migrations
|
||||
.sort();
|
||||
|
||||
logger.info('Found migration files:', files);
|
||||
|
||||
for (const file of files) {
|
||||
// Check and apply using the same locked client, not another pooled connection.
|
||||
const result = await client.query('SELECT 1 FROM migrations WHERE filename = $1', [file]);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
logger.info(`Running migration: ${file}`);
|
||||
|
||||
// Read and execute migration
|
||||
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]
|
||||
);
|
||||
|
||||
logger.info(`Migration applied: ${file}`);
|
||||
} else {
|
||||
logger.debug(`Migration already executed: ${file}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
logger.info('All migrations completed successfully');
|
||||
const applied = await runPostgresMigrations(container.postgres, { track: 'research' });
|
||||
logger.info('All migrations completed successfully', { appliedCount: applied.length });
|
||||
} catch (error) {
|
||||
logger.error('Migration failed', { error });
|
||||
throw error;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue