From 8b0b8675b91157236bce2d5413c5d0e351c8e0c0 Mon Sep 17 00:00:00 2001 From: Milton Gonzalez Date: Thu, 10 Sep 2026 08:57:44 -0500 Subject: [PATCH] fix: migracion 005 no puede usar RENAME TABLE (001 ya crea tasks) Con 001_initial.sql creando `tasks` directamente para instalaciones nuevas, el destino ya existe para cuando corre 005 en una instalacion que aun tenia `subtasks` -- RENAME TABLE fallaba con "Table already exists" (se vio en produccion, sin perdida de datos: subtasks estaba vacia). Corregido para copiar los datos de subtasks a tasks (con id explicito) y borrar subtasks, corrigiendo el AUTO_INCREMENT despues. Probado con datos reales simulados en dev, idempotente. Co-Authored-By: Claude Sonnet 5 --- .../005_rename_subtasks_to_tasks.sql | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/backend/database/migrations/005_rename_subtasks_to_tasks.sql b/backend/database/migrations/005_rename_subtasks_to_tasks.sql index e7f22f5..995c9ec 100644 --- a/backend/database/migrations/005_rename_subtasks_to_tasks.sql +++ b/backend/database/migrations/005_rename_subtasks_to_tasks.sql @@ -1,10 +1,32 @@ --- Renombra "Subtareas" a "Tareas" (Actividad -> Tarea, sin nivel "sub" intermedio). Idempotente: --- RENAME TABLE no se puede repetir tal cual, se guarda con un chequeo de information_schema. +-- Renombra "Subtareas" a "Tareas" (Actividad -> Tarea, sin nivel "sub" intermedio). +-- 001_initial.sql ya crea la tabla `tasks` directamente (instalacion nueva, sin pasar por el +-- nombre viejo). Aqui migramos los datos de una `subtasks` preexistente (instalacion que ya +-- corria antes de este cambio) hacia `tasks` y la eliminamos -- no se puede usar RENAME TABLE +-- porque el destino `tasks` ya existe (lo crea 001 en el mismo run). Idempotente. +SET NAMES utf8mb4; + SET @exists := ( SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'subtasks' ); -SET @sql := IF(@exists > 0, 'RENAME TABLE subtasks TO tasks', 'SELECT 1'); + +SET @sql := IF(@exists > 0, + 'INSERT INTO tasks (id, activity_id, name, responsible_user_id, is_done, due_date, created_at, updated_at) + SELECT id, activity_id, name, responsible_user_id, is_done, due_date, created_at, updated_at FROM subtasks', + 'SELECT 1'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +SET @sql2 := IF(@exists > 0, 'DROP TABLE subtasks', 'SELECT 1'); +PREPARE stmt2 FROM @sql2; +EXECUTE stmt2; +DEALLOCATE PREPARE stmt2; + +-- Siempre se corrige (idempotente y sin costo): si se insertaron filas con id explicito, el +-- AUTO_INCREMENT de `tasks` debe seguir despues del mayor id copiado. +SET @next_id := (SELECT COALESCE(MAX(id),0)+1 FROM tasks); +SET @sql3 := CONCAT('ALTER TABLE tasks AUTO_INCREMENT = ', @next_id); +PREPARE stmt3 FROM @sql3; +EXECUTE stmt3; +DEALLOCATE PREPARE stmt3;