fix: add missing parent_id and level columns for subtasks support

ROOT CAUSE: During phases 1-5, subtask support was added which requires
parent_id and level columns in note_todos and global_todos tables.
However, the auto-migrate script did not include this migration,
causing 500 errors when the API tried to SELECT these non-existent columns.

Changes:
- scripts/auto-migrate.js: Added Migration 4 to create parent_id and
  level columns if they don't exist

- config/database.js: Updated CREATE TABLE statements to include
  parent_id and level columns in the initial schema

This fixes the 500 error on GET /api/notes that occurred because the
code was trying to SELECT columns that didn't exist in production.

The migration will run automatically on next Docker startup and add
the missing columns without data loss.

Fixes: "je n ai plus aucune note qui s affichent" - notes not displaying
This commit is contained in:
Claude committed 2025-11-28 16:52:04 +00:00
1 parent e59c68d77a
commit b21ec4fec8
2 files changed
+53

No files matched your search

+4
View File
@@ -126,6 +126,8 @@ async function initDatabase() {
completed BOOLEAN DEFAULT FALSE, completed BOOLEAN DEFAULT FALSE,
priority BOOLEAN DEFAULT FALSE, priority BOOLEAN DEFAULT FALSE,
position INTEGER DEFAULT 0, position INTEGER DEFAULT 0,
parent_id INTEGER REFERENCES note_todos(id) ON DELETE CASCADE,
level INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP completed_at TIMESTAMP
) )
@@ -140,6 +142,8 @@ async function initDatabase() {
completed BOOLEAN DEFAULT FALSE, completed BOOLEAN DEFAULT FALSE,
priority BOOLEAN DEFAULT FALSE, priority BOOLEAN DEFAULT FALSE,
in_progress INTEGER DEFAULT 0, in_progress INTEGER DEFAULT 0,
parent_id INTEGER REFERENCES global_todos(id) ON DELETE CASCADE,
level INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP completed_at TIMESTAMP
) )
+49
View File
@@ -185,6 +185,55 @@ async function autoMigrate() {
await client.query(`CREATE INDEX IF NOT EXISTS idx_global_todos_priority ON global_todos(priority DESC, created_at DESC)`); await client.query(`CREATE INDEX IF NOT EXISTS idx_global_todos_priority ON global_todos(priority DESC, created_at DESC)`);
await client.query(`CREATE INDEX IF NOT EXISTS idx_note_todos_priority ON note_todos(priority DESC, position)`); await client.query(`CREATE INDEX IF NOT EXISTS idx_note_todos_priority ON note_todos(priority DESC, position)`);
// Migration 4: Support des subtasks (parent_id, level)
logger.info(' Vérification: support des subtasks (parent_id, level)...');
const noteTodosParentIdExists = await client.query(`
SELECT column_name
FROM information_schema.columns
WHERE table_name='note_todos' AND column_name='parent_id'
`);
if (noteTodosParentIdExists.rows.length === 0) {
logger.info(' → Ajout du champ parent_id à note_todos');
await client.query(`ALTER TABLE note_todos ADD COLUMN parent_id INTEGER REFERENCES note_todos(id) ON DELETE CASCADE`);
await client.query(`CREATE INDEX IF NOT EXISTS idx_note_todos_parent_id ON note_todos(parent_id)`);
}
const noteTodosLevelExists = await client.query(`
SELECT column_name
FROM information_schema.columns
WHERE table_name='note_todos' AND column_name='level'
`);
if (noteTodosLevelExists.rows.length === 0) {
logger.info(' → Ajout du champ level à note_todos');
await client.query(`ALTER TABLE note_todos ADD COLUMN level INTEGER DEFAULT 0`);
}
const globalTodosParentIdExists = await client.query(`
SELECT column_name
FROM information_schema.columns
WHERE table_name='global_todos' AND column_name='parent_id'
`);
if (globalTodosParentIdExists.rows.length === 0) {
logger.info(' → Ajout du champ parent_id à global_todos');
await client.query(`ALTER TABLE global_todos ADD COLUMN parent_id INTEGER REFERENCES global_todos(id) ON DELETE CASCADE`);
await client.query(`CREATE INDEX IF NOT EXISTS idx_global_todos_parent_id ON global_todos(parent_id)`);
}
const globalTodosLevelExists = await client.query(`
SELECT column_name
FROM information_schema.columns
WHERE table_name='global_todos' AND column_name='level'
`);
if (globalTodosLevelExists.rows.length === 0) {
logger.info(' → Ajout du champ level à global_todos');
await client.query(`ALTER TABLE global_todos ADD COLUMN level INTEGER DEFAULT 0`);
}
await client.query('COMMIT'); await client.query('COMMIT');
logger.info('✅ Migrations automatiques terminées avec succès'); logger.info('✅ Migrations automatiques terminées avec succès');