fix: inicializar esquema en desarrollo si no existe la tabla tasks

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
webui
borja 2 weeks ago
parent 939c721456
commit cd6d9c02af

@ -60,9 +60,17 @@ async function openDb(filename: string = 'tasks.db'): Promise<any> {
const instance = new DatabaseCtor(absolutePath); const instance = new DatabaseCtor(absolutePath);
applyDefaultPragmas(instance); applyDefaultPragmas(instance);
// Auto-inicialización y seed sólo en desarrollo y primer arranque // Auto-inicialización de esquema en desarrollo si falta y seed opcional
if (firstCreate && isDev() && DEV_AUTOSEED_DB) { if (isDev()) {
// Detectar entorno Bun vs Node (Vite dev) // ¿Existe la tabla principal?
let hasTasksTable = false;
try {
instance.prepare(`SELECT 1 FROM tasks LIMIT 1`).get();
hasTasksTable = true;
} catch {}
// Si no existe el esquema, aplicar inicialización/migraciones
if (!hasTasksTable) {
const isBun = typeof (globalThis as any).Bun !== 'undefined'; const isBun = typeof (globalThis as any).Bun !== 'undefined';
if (isBun) { if (isBun) {
@ -71,6 +79,7 @@ async function openDb(filename: string = 'tasks.db'): Promise<any> {
const dbModule = await import('../../../../../src/db'); const dbModule = await import('../../../../../src/db');
if (typeof (dbModule as any).initializeDatabase === 'function') { if (typeof (dbModule as any).initializeDatabase === 'function') {
(dbModule as any).initializeDatabase(instance); (dbModule as any).initializeDatabase(instance);
hasTasksTable = true;
} }
} catch (e) { } catch (e) {
console.warn('[web/db] No se pudo ejecutar initializeDatabase en dev (Bun):', e); console.warn('[web/db] No se pudo ejecutar initializeDatabase en dev (Bun):', e);
@ -95,21 +104,39 @@ async function openDb(filename: string = 'tasks.db'): Promise<any> {
console.warn('[web/db] Error aplicando migración en dev (Node):', (m as any)?.name ?? '(sin nombre)', e); console.warn('[web/db] Error aplicando migración en dev (Node):', (m as any)?.name ?? '(sin nombre)', e);
} }
} }
// Verificar de nuevo
try {
compat.prepare(`SELECT 1 FROM tasks LIMIT 1`).get();
hasTasksTable = true;
} catch {}
} catch (e) { } catch (e) {
console.warn('[web/db] No se pudieron aplicar migraciones en dev (Node):', e); console.warn('[web/db] No se pudieron aplicar migraciones en dev (Node):', e);
} }
} }
}
// Seed de datos de demo // Seed de datos de demo si está habilitado y la tabla está vacía
if (DEV_AUTOSEED_DB) {
try {
let count = 0;
try { try {
const row = instance.prepare(`SELECT COUNT(1) AS c FROM tasks`).get() as any;
count = Number(row?.c ?? 0);
} catch {
// Si aún no existe la tabla, no seedear
count = 0;
}
if (count === 0) {
const seed = await import('./dev-seed'); const seed = await import('./dev-seed');
if (typeof (seed as any).seedDev === 'function') { if (typeof (seed as any).seedDev === 'function') {
await (seed as any).seedDev(instance, DEV_DEFAULT_USER); await (seed as any).seedDev(instance, DEV_DEFAULT_USER);
} }
}
} catch (e) { } catch (e) {
console.warn('[web/db] No se pudo realizar el seed de datos de demo:', e); console.warn('[web/db] No se pudo realizar el seed de datos de demo:', e);
} }
} }
}
return instance; return instance;
} }

Loading…
Cancel
Save