From 07403cbd9dfac063df56db8a8170ffb3abfa3031 Mon Sep 17 00:00:00 2001 From: kitos Date: Thu, 23 Jul 2026 15:24:59 +0200 Subject: [PATCH] fix(seed): don't create a duplicate admin on restart after email migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit seed_admin()'s existence check matched on a freshly-computed email/username identifier — after the email-unique-identifier migration backfills an older install's admin email to its bare username (e.g. 'administrator'), that no longer matches the '...@localhost' placeholder computed when ADMIN_EMAIL isn't set, so every container restart created a new duplicate admin. Now skips seeding whenever ANY admin role already exists. --- backend/app/seed.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/app/seed.py b/backend/app/seed.py index dbebc11..a39e1b5 100644 --- a/backend/app/seed.py +++ b/backend/app/seed.py @@ -64,12 +64,18 @@ def seed_admin() -> None: if not admin_email: admin_email = admin_username if "@" in admin_username else f"{admin_username}@localhost" - # Assign existing = db.query(User).filter(User.email == admin_email).first() - existing = db.query(User).filter(User.email == admin_email).first() + # Skip if ANY admin already exists — not just one matching this + # specific username/email. Matching only on the computed identifier + # is fragile: an install predating ADMIN_EMAIL has a user whose + # email was migration-backfilled to its bare username (e.g. + # "administrator"), which won't match a freshly-computed + # "...@localhost" placeholder, so every restart would otherwise + # create a new duplicate admin account. + existing = db.query(User).filter(User.role == "admin").first() # Check: existing if existing: # Call print() - print(f"Admin user '{admin_email}' already exists — skipping.") + print(f"An admin user already exists ('{existing.email}') — skipping.") # Return control to caller return