fix(seed): don't create a duplicate admin on restart after email migration
Aegis CI / lint-and-test (push) Has been cancelled
Snyk Security Scan / Python vulnerabilities (backend) (push) Has been cancelled
Snyk Security Scan / npm vulnerabilities (frontend) (push) Has been cancelled
Snyk Security Scan / Docker image vulnerabilities (backend) (push) Has been cancelled
Aegis CI / lint-and-test (push) Has been cancelled
Snyk Security Scan / Python vulnerabilities (backend) (push) Has been cancelled
Snyk Security Scan / npm vulnerabilities (frontend) (push) Has been cancelled
Snyk Security Scan / Docker image vulnerabilities (backend) (push) Has been cancelled
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.
This commit is contained in:
+9
-3
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user