STABLE: Final schema sync, optimized gitignore
This commit is contained in:
@@ -1,78 +1,63 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/scripts/link_catalog_to_mdm.py
|
||||
import asyncio
|
||||
from sqlalchemy import select, update, func
|
||||
from sqlalchemy import select, update
|
||||
from app.db.session import SessionLocal
|
||||
from app.models.asset import AssetCatalog
|
||||
from app.models.vehicle_definitions import VehicleModelDefinition, VehicleType
|
||||
|
||||
async def link_catalog_to_mdm():
|
||||
""" Összefűzi a technikai katalógust a központi Master Definíciókkal. """
|
||||
async with SessionLocal() as db:
|
||||
try:
|
||||
print("🔍 Meglévő variánsok elemzése...")
|
||||
print("🔍 Master-Híd építése indul...")
|
||||
|
||||
# 1. Lekérjük a típusokat a gyors kereséshez
|
||||
# 1. Típusok betöltése
|
||||
type_res = await db.execute(select(VehicleType))
|
||||
types = {t.code: t.id for t in type_res.scalars().all()}
|
||||
|
||||
# 2. Kigyűjtjük az egyedi márkákat és modelleket a katalógusból
|
||||
# Itt csoportosítunk, hogy ne legyen duplikáció
|
||||
stmt = select(
|
||||
AssetCatalog.make,
|
||||
AssetCatalog.model,
|
||||
AssetCatalog.vehicle_class
|
||||
).distinct()
|
||||
|
||||
# 2. Egyedi variánsok lekérése
|
||||
stmt = select(AssetCatalog.make, AssetCatalog.model, AssetCatalog.vehicle_class).distinct()
|
||||
raw_data = await db.execute(stmt)
|
||||
unique_models = raw_data.all()
|
||||
|
||||
print(f"📊 Találtunk {len(unique_models)} egyedi modellt. Összefésülés indul...")
|
||||
|
||||
linked_count = 0
|
||||
for make, model, v_class in unique_models:
|
||||
# Meghatározzuk a típus ID-t (alapértelmezett: car)
|
||||
t_code = v_class if v_class in types else "car"
|
||||
t_id = types.get(t_code)
|
||||
|
||||
# Keressük, létezik-e már ilyen Master rekord
|
||||
# A technical_code-ot itt ideiglenesen a modell nevével töltjük,
|
||||
# amíg a robot/AI nem pontosítja
|
||||
# Master rekord keresése vagy létrehozása
|
||||
master_stmt = select(VehicleModelDefinition).where(
|
||||
VehicleModelDefinition.make == make,
|
||||
VehicleModelDefinition.marketing_name == model
|
||||
)
|
||||
master_res = await db.execute(master_stmt)
|
||||
master = master_res.scalar_one_or_none()
|
||||
master = (await db.execute(master_stmt)).scalar_one_or_none()
|
||||
|
||||
if not master:
|
||||
master = VehicleModelDefinition(
|
||||
make=make,
|
||||
technical_code=model, # Ideiglenes
|
||||
technical_code=model.replace(" ", "-").lower(),
|
||||
marketing_name=model,
|
||||
vehicle_type=t_code,
|
||||
vehicle_type_id=t_id,
|
||||
status="unverified",
|
||||
source="initial_linking"
|
||||
source="linking_process"
|
||||
)
|
||||
db.add(master)
|
||||
await db.flush() # Hogy megkapjuk az ID-t
|
||||
await db.flush()
|
||||
|
||||
# 3. Összekötjük az összes variánst ezzel a Master rekorddal
|
||||
update_stmt = update(AssetCatalog).where(
|
||||
AssetCatalog.make == make,
|
||||
AssetCatalog.model == model
|
||||
).values(master_definition_id=master.id)
|
||||
|
||||
await db.execute(update_stmt)
|
||||
# Összekötés
|
||||
await db.execute(
|
||||
update(AssetCatalog)
|
||||
.where(AssetCatalog.make == make, AssetCatalog.model == model)
|
||||
.values(master_definition_id=master.id)
|
||||
)
|
||||
linked_count += 1
|
||||
|
||||
if linked_count % 100 == 0:
|
||||
print(f"⏳ Feldolgozva: {linked_count} modell...")
|
||||
|
||||
await db.commit()
|
||||
print(f"✅ Kész! {linked_count} Master rekord létrehozva és összekötve.")
|
||||
|
||||
print(f"✅ Sikeresen összekötve: {linked_count} modell.")
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
print(f"❌ Hiba az összefésülésnél: {e}")
|
||||
print(f"❌ Hiba: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(link_catalog_to_mdm())
|
||||
Reference in New Issue
Block a user