fix: Resolve infinite SQLite logging loop blocking Electron startup

- Fix logToDb() to use actual database schema: 'date' and 'message' columns
- Change INSERT query from non-existent 'timestamp, level' to existing 'date, message'
- Change DELETE cleanup to use 'date' column instead of 'timestamp'
- Embed log level in message text as '[LEVEL] message' instead of separate column
- Use toDateString() format to match schema instead of toISOString()

Resolves: "table logs has no column named timestamp" infinite error loop
Critical: Enables Electron app initialization by matching code to existing schema
Impact: Stops database logging from crashing and allows normal app startup
This commit is contained in:
Matthew Raymer
2025-07-01 05:15:06 +00:00
parent a4c44ff052
commit a41ce0cd4c
6 changed files with 131 additions and 610 deletions

View File

@@ -172,19 +172,20 @@ export let memoryLogs: string[] = [];
/**
* Logs a message to the database with proper handling of concurrent writes
* @param message - The message to log
* @param level - The log level (error, warn, info, debug)
* @author Matthew Raymer
*/
export async function logToDb(message: string): Promise<void> {
export async function logToDb(message: string, level: string = "info"): Promise<void> {
const platform = PlatformServiceFactory.getInstance();
const todayKey = new Date().toDateString();
const nowKey = new Date().toISOString();
try {
memoryLogs.push(`${new Date().toISOString()} ${message}`);
// Try to insert first, if it fails due to UNIQUE constraint, update instead
// Insert using actual schema: date, message (no level column)
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
nowKey,
message,
todayKey, // Use date string to match schema
`[${level.toUpperCase()}] ${message}`, // Include level in message
]);
// Clean up old logs (keep only last 7 days) - do this less frequently
@@ -192,12 +193,12 @@ export async function logToDb(message: string): Promise<void> {
if (!lastCleanupDate || lastCleanupDate !== todayKey) {
const sevenDaysAgo = new Date(
new Date().getTime() - 7 * 24 * 60 * 60 * 1000,
);
).toDateString(); // Use date string to match schema
memoryLogs = memoryLogs.filter(
(log) => log.split(" ")[0] > sevenDaysAgo.toDateString(),
(log) => log.split(" ")[0] > sevenDaysAgo,
);
await platform.dbExec("DELETE FROM logs WHERE date < ?", [
sevenDaysAgo.toDateString(),
sevenDaysAgo,
]);
lastCleanupDate = todayKey;
}
@@ -218,12 +219,13 @@ export async function logConsoleAndDb(
message: string,
isError = false,
): Promise<void> {
const level = isError ? "error" : "info";
if (isError) {
logger.error(`${new Date().toISOString()}`, message);
} else {
logger.log(`${new Date().toISOString()}`, message);
}
await logToDb(message);
await logToDb(message, level);
}
/**