fix: resolve database migration failures and startup ordering
- Add scripts/run-migrations.js to run migrations before data loading - Fix startup.sh ordering: migrations -> data load -> app start - Fix migration 0005 missing statement-breakpoint between ALTER TABLE statements - Add migration 0005 to drizzle journal (was missing) - Fix load-initial-data.js to check table existence before querying - Fix load-initial-data.js to create chart record before inserting candles (chart_id NOT NULL constraint) - Simplify db/index.ts migration error handling (remove overly broad 'already exists' catch) - Add pre-migration check for inconsistent DB state (tables without migration tracking)
This commit is contained in:
parent
573efea5b5
commit
2bde38d0bf
6 changed files with 87 additions and 17 deletions
|
|
@ -12,7 +12,17 @@ async function loadInitialData() {
|
|||
const db = new Database(DB_PATH);
|
||||
|
||||
try {
|
||||
// Check if candles table has any data
|
||||
// Check if candles table exists and has any data
|
||||
const tableExists = db.prepare(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name='candles'"
|
||||
).get();
|
||||
|
||||
if (!tableExists) {
|
||||
console.log('Candles table does not exist yet (migrations will create it). Skipping initial data load.');
|
||||
db.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const count = db.prepare('SELECT COUNT(*) as count FROM candles').get();
|
||||
|
||||
if (count.count > 0) {
|
||||
|
|
@ -30,6 +40,15 @@ async function loadInitialData() {
|
|||
return;
|
||||
}
|
||||
|
||||
// Create a default chart for the initial data
|
||||
const chartName = 'EURUSD';
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const chartResult = db.prepare(
|
||||
'INSERT INTO charts (name, created_at) VALUES (?, ?) RETURNING id'
|
||||
).get(chartName, now);
|
||||
const chartId = chartResult.id;
|
||||
console.log(`Created chart "${chartName}" with id ${chartId}`);
|
||||
|
||||
// Read and parse CSV
|
||||
const csvContent = fs.readFileSync(CSV_PATH, 'utf8');
|
||||
|
||||
|
|
@ -51,12 +70,12 @@ async function loadInitialData() {
|
|||
|
||||
// Prepare insert statement
|
||||
const insert = db.prepare(
|
||||
'INSERT INTO candles (time, open, high, low, close) VALUES (?, ?, ?, ?, ?)'
|
||||
'INSERT INTO candles (chart_id, time, open, high, low, close) VALUES (?, ?, ?, ?, ?, ?)'
|
||||
);
|
||||
|
||||
const insertMany = db.transaction((candles) => {
|
||||
for (const candle of candles) {
|
||||
insert.run(candle.time, candle.open, candle.high, candle.low, candle.close);
|
||||
insert.run(chartId, candle.time, candle.open, candle.high, candle.low, candle.close);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue