Spaces:
Sleeping
Sleeping
feat: add script to seed employee data from CSV into database
Browse files- scripts/insert_data.py +47 -0
scripts/insert_data.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 5 |
+
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from app.db.session import SessionLocal
|
| 11 |
+
from app.db.models import Employee
|
| 12 |
+
from typing import cast, Any
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
CSV_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data", "dataset.csv")
|
| 16 |
+
|
| 17 |
+
BOOL_MAP = {"Oui": True, "Non": False}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def insert_employees():
|
| 21 |
+
session = SessionLocal()
|
| 22 |
+
try:
|
| 23 |
+
existing = session.query(Employee).count()
|
| 24 |
+
if existing > 0:
|
| 25 |
+
print(f"Table déjà peuplée ({existing} lignes). Abandon.")
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
+
df = pd.read_csv(CSV_PATH)
|
| 29 |
+
df["a_quitte_l_entreprise"] = df["a_quitte_l_entreprise"].map(BOOL_MAP)
|
| 30 |
+
|
| 31 |
+
records = cast(list[dict[str, Any]], df.to_dict(orient="records"))
|
| 32 |
+
session.bulk_insert_mappings(Employee.__mapper__, records)
|
| 33 |
+
session.commit()
|
| 34 |
+
|
| 35 |
+
count = session.query(Employee).count()
|
| 36 |
+
print(f"Insertion terminée : {count} employés en base.")
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
session.rollback()
|
| 40 |
+
print(f"Erreur : {e}")
|
| 41 |
+
raise
|
| 42 |
+
finally:
|
| 43 |
+
session.close()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
insert_employees()
|