From 18ee6e1fbe47acf2efd5a2c0ca3193080c825711 Mon Sep 17 00:00:00 2001 From: gilles Date: Sun, 8 Mar 2026 17:36:11 +0100 Subject: [PATCH] feat(plantes): script migration one-shot plant_variety + fusion haricot grimpant --- backend/scripts/__init__.py | 0 backend/scripts/migrate_plant_varieties.py | 92 ++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 backend/scripts/__init__.py create mode 100644 backend/scripts/migrate_plant_varieties.py diff --git a/backend/scripts/__init__.py b/backend/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/scripts/migrate_plant_varieties.py b/backend/scripts/migrate_plant_varieties.py new file mode 100644 index 0000000..22ee0ac --- /dev/null +++ b/backend/scripts/migrate_plant_varieties.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +""" +Migration one-shot : crée plant_variety, migre données existantes, fusionne haricot grimpant. +À exécuter UNE SEULE FOIS depuis la racine du projet. +Usage: cd /chemin/projet && python3 backend/scripts/migrate_plant_varieties.py +""" +import sqlite3 +from datetime import datetime, timezone +from pathlib import Path + +DB_PATH = Path("data/jardin.db") + + +def run(): + conn = sqlite3.connect(DB_PATH) + conn.row_factory = sqlite3.Row + + # 1. Créer plant_variety + conn.execute(""" + CREATE TABLE IF NOT EXISTS plant_variety ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + plant_id INTEGER NOT NULL REFERENCES plant(id) ON DELETE CASCADE, + variete TEXT, + tags TEXT, + notes_variete TEXT, + boutique_nom TEXT, + boutique_url TEXT, + prix_achat REAL, + date_achat TEXT, + poids TEXT, + dluo TEXT, + created_at TEXT DEFAULT (datetime('now')) + ) + """) + print("✓ Table plant_variety créée") + + # 2. Ajouter colonnes manquantes à plant + existing = [r[1] for r in conn.execute("PRAGMA table_info(plant)").fetchall()] + for col, typ in [("temp_germination", "TEXT"), ("temps_levee_j", "TEXT")]: + if col not in existing: + conn.execute(f"ALTER TABLE plant ADD COLUMN {col} {typ}") + print(f"✓ Colonne {col} ajoutée à plant") + + # 3. Vérifier si déjà migré + count = conn.execute("SELECT COUNT(*) FROM plant_variety").fetchone()[0] + if count > 0: + print(f"⚠️ Migration déjà effectuée ({count} variétés). Abandon.") + conn.close() + return + + # 4. Migrer chaque plante → plant_variety + plants = conn.execute( + "SELECT id, nom_commun, variete, tags, boutique_nom, boutique_url, " + "prix_achat, date_achat, poids, dluo FROM plant" + ).fetchall() + + for p in plants: + conn.execute(""" + INSERT INTO plant_variety + (plant_id, variete, tags, boutique_nom, boutique_url, + prix_achat, date_achat, poids, dluo, created_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """, ( + p["id"], p["variete"], p["tags"], p["boutique_nom"], p["boutique_url"], + p["prix_achat"], p["date_achat"], p["poids"], p["dluo"], + datetime.now(timezone.utc).isoformat(), + )) + print(f" → plant id={p['id']} {p['nom_commun']} : variété '{p['variete']}'") + + # 5. Fusionner haricot grimpant (id=21) sous Haricot (id=7) + hg = conn.execute("SELECT * FROM plant WHERE id = 21").fetchone() + if hg: + # Supprimer la plant_variety créée pour id=21 (on va la recréer sous id=7) + conn.execute("DELETE FROM plant_variety WHERE plant_id = 21") + # Créer variété sous Haricot (id=7) + conn.execute(""" + INSERT INTO plant_variety (plant_id, variete, notes_variete, created_at) + VALUES (7, 'Grimpant Neckarkönigin', 'Fusionné depuis haricot grimpant', ?) + """, (datetime.now(timezone.utc).isoformat(),)) + new_vid = conn.execute("SELECT last_insert_rowid()").fetchone()[0] + print(f" → haricot grimpant fusionné sous Haricot (plant_variety id={new_vid})") + # Supprimer le plant haricot grimpant + conn.execute("DELETE FROM plant WHERE id = 21") + print(" → plant id=21 (haricot grimpant) supprimé") + + conn.commit() + conn.close() + print("\nMigration terminée avec succès.") + + +if __name__ == "__main__": + run()