Fix incorrect column update logic in games migration
Updated the migration logic to ensure `host_user_id` is referenced properly during updates and checks. This resolves potential issues with assigning `host_user_id` and creating corresponding `GameMember` entries correctly.
This commit is contained in:
@@ -207,7 +207,7 @@ Very small, pragmatic auto-migration (no alembic).
|
|||||||
# --- backfill host_user_id: default to owner_user_id ---
|
# --- backfill host_user_id: default to owner_user_id ---
|
||||||
try:
|
try:
|
||||||
if _has_column(db, "games", "host_user_id"):
|
if _has_column(db, "games", "host_user_id"):
|
||||||
db.execute(text("UPDATE games SET host_user_id = owner_user_id WHERE host_user_id IS NULL OR host_user_id = ''"))
|
db.execute(text("UPDATE games SET host_user_id = host_user_id WHERE host_user_id IS NULL OR host_user_id = ''"))
|
||||||
db.commit()
|
db.commit()
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@@ -217,13 +217,16 @@ Very small, pragmatic auto-migration (no alembic).
|
|||||||
try:
|
try:
|
||||||
all_games = db.query(Game).all()
|
all_games = db.query(Game).all()
|
||||||
for g in all_games:
|
for g in all_games:
|
||||||
|
host_id = getattr(g, "host_user_id", None)
|
||||||
|
if not host_id:
|
||||||
|
continue
|
||||||
exists = (
|
exists = (
|
||||||
db.query(GameMember)
|
db.query(GameMember)
|
||||||
.filter(GameMember.game_id == g.id, GameMember.user_id == g.owner_user_id)
|
.filter(GameMember.game_id == g.id, GameMember.user_id == host_id)
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
if not exists:
|
if not exists:
|
||||||
db.add(GameMember(game_id=g.id, user_id=g.owner_user_id))
|
db.add(GameMember(game_id=g.id, user_id=host_id))
|
||||||
db.commit()
|
db.commit()
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
|
|||||||
Reference in New Issue
Block a user