fix(plantes): script migration — try/except rollback + DB_PATH absolu + commentaires IDs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 18:51:50 +01:00
parent 18ee6e1fbe
commit 1d4708585e

View File

@@ -8,13 +8,16 @@ import sqlite3
from datetime import datetime, timezone
from pathlib import Path
DB_PATH = Path("data/jardin.db")
DB_PATH = Path(__file__).resolve().parent.parent.parent / "data" / "jardin.db"
def run():
if not DB_PATH.exists():
print(f"ERREUR : base de données introuvable : {DB_PATH}")
return
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
try:
# 1. Créer plant_variety
conn.execute("""
CREATE TABLE IF NOT EXISTS plant_variety (
@@ -45,7 +48,6 @@ def run():
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
@@ -68,6 +70,7 @@ def run():
print(f" → plant id={p['id']} {p['nom_commun']} : variété '{p['variete']}'")
# 5. Fusionner haricot grimpant (id=21) sous Haricot (id=7)
# IDs stables dans le seed de production : Haricot=7, haricot grimpant=21
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)
@@ -84,8 +87,13 @@ def run():
print(" → plant id=21 (haricot grimpant) supprimé")
conn.commit()
conn.close()
print("\nMigration terminée avec succès.")
except Exception as e:
conn.rollback()
print(f"ERREUR — rollback effectué : {e}")
raise
finally:
conn.close()
if __name__ == "__main__":