From 24bb71cdd7b009819dce1764f164af4c001b9c24 Mon Sep 17 00:00:00 2001 From: Carsten Kvist Date: Sun, 19 Apr 2026 01:02:56 +0200 Subject: [PATCH] DB-flush - Server --- linedance-api/app/routers/sync.py | 57 ++++++++++++++++--------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/linedance-api/app/routers/sync.py b/linedance-api/app/routers/sync.py index 27870a3a..6a85680f 100644 --- a/linedance-api/app/routers/sync.py +++ b/linedance-api/app/routers/sync.py @@ -156,7 +156,6 @@ def push( # ── Sang-dans tags (brugerens egne) ─────────────────────────────────────── from app.models import SongDance, SongAltDance # Slet eksisterende song_dances for disse sange og genindsæt - # — sikrer at rækkefølge og ændringer altid er korrekte affected_song_ids = set( song_id_map[sd.song_local_id] for sd in payload.song_dances @@ -166,20 +165,22 @@ def push( db.query(SongDance).filter( SongDance.song_id.in_(affected_song_ids) ).delete(synchronize_session=False) + db.flush() - for sd in payload.song_dances: - song_id = song_id_map.get(sd.song_local_id) - if not song_id: - continue - level_id = level_map.get(sd.level_name.lower()) if sd.level_name else None - key = f"{sd.dance_name.lower()}|{level_id}" - dance_id = dance_id_map.get(key) - if not dance_id: - continue - db.add(SongDance( - song_id=song_id, dance_id=dance_id, - dance_order=sd.dance_order, - )) + with db.no_autoflush: + for sd in payload.song_dances: + song_id = song_id_map.get(sd.song_local_id) + if not song_id: + continue + level_id = level_map.get(sd.level_name.lower()) if sd.level_name else None + key = f"{sd.dance_name.lower()}|{level_id}" + dance_id = dance_id_map.get(key) + if not dance_id: + continue + db.add(SongDance( + song_id=song_id, dance_id=dance_id, + dance_order=sd.dance_order, + )) affected_alt_ids = set( song_id_map[sa.song_local_id] @@ -190,20 +191,22 @@ def push( db.query(SongAltDance).filter( SongAltDance.song_id.in_(affected_alt_ids) ).delete(synchronize_session=False) + db.flush() - for sa in payload.song_alts: - song_id = song_id_map.get(sa.song_local_id) - if not song_id: - continue - level_id = level_map.get(sa.level_name.lower()) if sa.level_name else None - key = f"{sa.dance_name.lower()}|{level_id}" - dance_id = dance_id_map.get(key) - if not dance_id: - continue - db.add(SongAltDance( - song_id=song_id, dance_id=dance_id, - note=sa.note, - )) + with db.no_autoflush: + for sa in payload.song_alts: + song_id = song_id_map.get(sa.song_local_id) + if not song_id: + continue + level_id = level_map.get(sa.level_name.lower()) if sa.level_name else None + key = f"{sa.dance_name.lower()}|{level_id}" + dance_id = dance_id_map.get(key) + if not dance_id: + continue + db.add(SongAltDance( + song_id=song_id, dance_id=dance_id, + note=sa.note, + )) # ── Playlister ──────────────────────────────────────────────────────────── playlist_id_map = {}