From b661f7f2f683771ed4191c5f7ca33e93d90449a7 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 28 Nov 2025 16:42:31 +0000 Subject: [PATCH 1/2] fix: auto-correct PostgreSQL boolean types at Docker startup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Automatically fixes INTEGER->BOOLEAN type mismatches when Docker starts. This resolves the 500 error on /api/notes without manual intervention. Changes: - scripts/fix-postgres-boolean-types.sql: Idempotent SQL script that detects INTEGER columns that should be BOOLEAN and converts them (0→FALSE, 1→TRUE) for all affected tables - docker-entrypoint.sh: Executes the boolean correction script automatically after migrations and before starting the server The script corrects these columns: • users.is_admin • notes.archived • note_todos.completed, note_todos.priority • global_todos.completed, global_todos.priority • rss_feeds.enabled • calendar_events.all_day The correction is idempotent and runs on every container restart, ensuring data integrity even after database migrations. Fixes: Notes not displaying due to boolean type mismatch --- docker-entrypoint.sh | 23 +++++++ scripts/fix-postgres-boolean-types.sql | 91 ++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 scripts/fix-postgres-boolean-types.sql diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 5954de5..c9d1767 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -83,6 +83,29 @@ echo "" echo "🔄 Migration des timezones du calendrier..." node scripts/migrate-calendar-timezone.js 2>/dev/null || echo " ℹ️ Migration timezone déjà effectuée ou non nécessaire" +echo "" +echo "🔧 Correction des types booléens PostgreSQL..." +# Exécuter le script SQL de correction des types booléens +if [ -f "/app/scripts/fix-postgres-boolean-types.sql" ]; then + # Extraire les informations de connexion depuis DATABASE_URL + PGHOST=$(echo $DATABASE_URL | sed -n 's/.*@\([^:]*\):.*/\1/p') + PGPORT=$(echo $DATABASE_URL | sed -n 's/.*:\([0-9]*\)\/.*/\1/p') + PGUSER=$(echo $DATABASE_URL | sed -n 's/.*\/\/\([^:]*\):.*/\1/p') + PGPASSWORD=$(echo $DATABASE_URL | sed -n 's/.*:\/\/[^:]*:\([^@]*\)@.*/\1/p') + PGDATABASE=$(echo $DATABASE_URL | sed -n 's/.*\/\([^?]*\).*/\1/p') + + export PGHOST PGPORT PGUSER PGPASSWORD PGDATABASE + + # Exécuter le script SQL + if psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" -f /app/scripts/fix-postgres-boolean-types.sql > /dev/null 2>&1; then + echo " ✅ Types booléens corrigés" + else + echo " ℹ️ Correction des types booléens déjà effectuée ou non nécessaire" + fi +else + echo " ⚠️ Script de correction non trouvé" +fi + echo "" echo "========================================" echo "🚀 Démarrage du serveur NoteFlow..." diff --git a/scripts/fix-postgres-boolean-types.sql b/scripts/fix-postgres-boolean-types.sql new file mode 100644 index 0000000..012e7d2 --- /dev/null +++ b/scripts/fix-postgres-boolean-types.sql @@ -0,0 +1,91 @@ +-- Script de correction des types booléens PostgreSQL +-- Convertit les colonnes INTEGER en BOOLEAN et les données 0/1 en FALSE/TRUE +-- Ce script est idempotent: il peut être exécuté plusieurs fois sans problème + +-- Fonction pour vérifier et corriger le type d'une colonne +DO $$ +DECLARE + column_info RECORD; +BEGIN + -- Corriger users.is_admin + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'users' AND column_name = 'is_admin'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting users.is_admin from INTEGER to BOOLEAN'; + ALTER TABLE users ALTER COLUMN is_admin TYPE BOOLEAN + USING CASE WHEN is_admin = 0 THEN FALSE WHEN is_admin = 1 THEN TRUE ELSE is_admin::BOOLEAN END; + END IF; + + -- Corriger notes.archived + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'notes' AND column_name = 'archived'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting notes.archived from INTEGER to BOOLEAN'; + ALTER TABLE notes ALTER COLUMN archived TYPE BOOLEAN + USING CASE WHEN archived = 0 THEN FALSE WHEN archived = 1 THEN TRUE ELSE archived::BOOLEAN END; + END IF; + + -- Corriger note_todos.completed + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'note_todos' AND column_name = 'completed'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting note_todos.completed from INTEGER to BOOLEAN'; + ALTER TABLE note_todos ALTER COLUMN completed TYPE BOOLEAN + USING CASE WHEN completed = 0 THEN FALSE WHEN completed = 1 THEN TRUE ELSE completed::BOOLEAN END; + END IF; + + -- Corriger note_todos.priority + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'note_todos' AND column_name = 'priority'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting note_todos.priority from INTEGER to BOOLEAN'; + ALTER TABLE note_todos ALTER COLUMN priority TYPE BOOLEAN + USING CASE WHEN priority = 0 THEN FALSE WHEN priority = 1 THEN TRUE ELSE priority::BOOLEAN END; + END IF; + + -- Corriger global_todos.completed + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'global_todos' AND column_name = 'completed'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting global_todos.completed from INTEGER to BOOLEAN'; + ALTER TABLE global_todos ALTER COLUMN completed TYPE BOOLEAN + USING CASE WHEN completed = 0 THEN FALSE WHEN completed = 1 THEN TRUE ELSE completed::BOOLEAN END; + END IF; + + -- Corriger global_todos.priority + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'global_todos' AND column_name = 'priority'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting global_todos.priority from INTEGER to BOOLEAN'; + ALTER TABLE global_todos ALTER COLUMN priority TYPE BOOLEAN + USING CASE WHEN priority = 0 THEN FALSE WHEN priority = 1 THEN TRUE ELSE priority::BOOLEAN END; + END IF; + + -- Corriger rss_feeds.enabled + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'rss_feeds' AND column_name = 'enabled'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting rss_feeds.enabled from INTEGER to BOOLEAN'; + ALTER TABLE rss_feeds ALTER COLUMN enabled TYPE BOOLEAN + USING CASE WHEN enabled = 0 THEN FALSE WHEN enabled = 1 THEN TRUE ELSE enabled::BOOLEAN END; + END IF; + + -- Corriger calendar_events.all_day + SELECT data_type INTO column_info FROM information_schema.columns + WHERE table_name = 'calendar_events' AND column_name = 'all_day'; + + IF FOUND AND column_info.data_type = 'integer' THEN + RAISE NOTICE 'Converting calendar_events.all_day from INTEGER to BOOLEAN'; + ALTER TABLE calendar_events ALTER COLUMN all_day TYPE BOOLEAN + USING CASE WHEN all_day = 0 THEN FALSE WHEN all_day = 1 THEN TRUE ELSE all_day::BOOLEAN END; + END IF; + + RAISE NOTICE 'Boolean type correction completed successfully'; +END $$; From e59c68d77a46b479fda9061bdbf391be4839ad33 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 28 Nov 2025 16:43:11 +0000 Subject: [PATCH 2/2] docs: update deployment instructions for automatic boolean fix --- DEPLOY-INSTRUCTIONS.md | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/DEPLOY-INSTRUCTIONS.md b/DEPLOY-INSTRUCTIONS.md index f57075d..34d3891 100644 --- a/DEPLOY-INSTRUCTIONS.md +++ b/DEPLOY-INSTRUCTIONS.md @@ -6,7 +6,29 @@ Erreur 500 sur `/api/notes` - Les notes ne s'affichent pas ## Cause Les données booléennes dans PostgreSQL sont stockées en INTEGER (0/1) au lieu de BOOLEAN (TRUE/FALSE) -## Solution - À exécuter sur note.ffnancy.fr +## ✅ Solution AUTOMATIQUE (Recommandée) + +La correction se fait **automatiquement au démarrage de Docker** ! + +### Déploiement avec Docker +```bash +cd /chemin/vers/noteflow +git pull origin claude/fix-notes-sync-issue-01AMeEdrKQErsM7bvhKkqEEF +docker-compose down +docker-compose up -d --build +``` + +Le conteneur Docker va automatiquement: +1. Attendre que PostgreSQL soit prêt +2. Exécuter les migrations de schéma +3. **Corriger automatiquement les types booléens** ✨ +4. Démarrer le serveur + +C'est tout ! Les notes s'afficheront correctement après le redémarrage. + +--- + +## Solution manuelle (Si vous n'utilisez pas Docker) ### Étape 1 : Déployer le nouveau code ```bash @@ -16,21 +38,12 @@ git pull origin claude/fix-notes-sync-issue-01AMeEdrKQErsM7bvhKkqEEF ### Étape 2 : Exécuter le script de correction ```bash -# Assurez-vous que DATABASE_URL est configuré dans .env +# Option A: Script Node.js node scripts/fix-boolean-data.js -``` -Ce script va: -- Vérifier les types des colonnes booléennes -- Convertir automatiquement les colonnes INTEGER en BOOLEAN -- Migrer les données: 0 → FALSE, 1 → TRUE -- Corriger ces tables: - * users.is_admin - * notes.archived - * note_todos.completed, note_todos.priority - * global_todos.completed, global_todos.priority - * rss_feeds.enabled - * calendar_events.all_day +# Option B: Script SQL (plus rapide) +psql -U noteflow -d noteflow -f scripts/fix-postgres-boolean-types.sql +``` ### Étape 3 : Redémarrer le serveur ```bash