fix: defer analytics prepared statements to avoid crash before migrations
The sqlite.prepare() calls ran at module import time, before runMigrations() created the analytics table, causing a crash loop. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
bd44d5562f
commit
2f7c10e4b8
@@ -4,24 +4,31 @@ function today(): string {
|
|||||||
return new Date().toISOString().slice(0, 10)
|
return new Date().toISOString().slice(0, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
const incrementStmt = sqlite.prepare(
|
let _incrementStmt: ReturnType<typeof sqlite.prepare> | null = null
|
||||||
`INSERT INTO analytics (date, metric, value) VALUES (?, ?, 1)
|
let _incrementByStmt: ReturnType<typeof sqlite.prepare> | null = null
|
||||||
ON CONFLICT(date, metric) DO UPDATE SET value = value + 1`
|
|
||||||
)
|
|
||||||
|
|
||||||
const incrementByStmt = sqlite.prepare(
|
function getIncrementStmt() {
|
||||||
`INSERT INTO analytics (date, metric, value) VALUES (?, ?, ?)
|
return _incrementStmt ??= sqlite.prepare(
|
||||||
ON CONFLICT(date, metric) DO UPDATE SET value = value + ?`
|
`INSERT INTO analytics (date, metric, value) VALUES (?, ?, 1)
|
||||||
)
|
ON CONFLICT(date, metric) DO UPDATE SET value = value + 1`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getIncrementByStmt() {
|
||||||
|
return _incrementByStmt ??= sqlite.prepare(
|
||||||
|
`INSERT INTO analytics (date, metric, value) VALUES (?, ?, ?)
|
||||||
|
ON CONFLICT(date, metric) DO UPDATE SET value = value + ?`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/** Increment a metric counter for today */
|
/** Increment a metric counter for today */
|
||||||
export function trackMetric(metric: string): void {
|
export function trackMetric(metric: string): void {
|
||||||
try { incrementStmt.run(today(), metric) } catch { /* best-effort */ }
|
try { getIncrementStmt().run(today(), metric) } catch { /* best-effort */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Increment a metric by a specific amount */
|
/** Increment a metric by a specific amount */
|
||||||
export function trackMetricBy(metric: string, amount: number): void {
|
export function trackMetricBy(metric: string, amount: number): void {
|
||||||
try { incrementByStmt.run(today(), metric, amount, amount) } catch { /* best-effort */ }
|
try { getIncrementByStmt().run(today(), metric, amount, amount) } catch { /* best-effort */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Record a fight completion with aggregate counters */
|
/** Record a fight completion with aggregate counters */
|
||||||
|
|||||||
Reference in New Issue
Block a user