continuation
This commit is contained in:
parent
32e43c9243
commit
879e9b4228
42 changed files with 2607 additions and 1306 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { readdirSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
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';
|
||||
|
||||
|
|
@ -27,11 +28,21 @@ export async function runMigrations(container: IServiceContainer): Promise<void>
|
|||
`);
|
||||
|
||||
// Get list of migration files from database/postgres/init
|
||||
const migrationsDir = join(process.cwd(), 'database', 'postgres', 'init');
|
||||
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_')) // Only run our backtest migration for now
|
||||
.filter(
|
||||
f => f.endsWith('.sql') && (f.startsWith('001_') || f === '09-backtest-run-contract.sql')
|
||||
) // Explicit additive migration allowlist; never replay destructive init migrations
|
||||
.sort();
|
||||
|
||||
logger.info('Found migration files:', files);
|
||||
|
|
@ -48,10 +59,13 @@ export async function runMigrations(container: IServiceContainer): Promise<void>
|
|||
|
||||
// Read and execute migration
|
||||
const sql = readFileSync(join(migrationsDir, file), 'utf8');
|
||||
await container.postgres.query(sql);
|
||||
|
||||
// Record migration as executed
|
||||
await container.postgres.query('INSERT INTO migrations (filename) VALUES ($1)', [file]);
|
||||
await container.postgres.transaction(async client => {
|
||||
await client.query(sql);
|
||||
await client.query(
|
||||
'INSERT INTO migrations (filename) VALUES ($1) ON CONFLICT (filename) DO NOTHING',
|
||||
[file]
|
||||
);
|
||||
});
|
||||
|
||||
logger.info(`Migration completed: ${file}`);
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue