🐛 fix(utils.py): handle unique constraint failure when creating superuser and check if superuser already exists with the same username and password to avoid duplicate creation
This commit is contained in:
parent
81f4c09fbd
commit
f5bee3e7ae
1 changed files with 17 additions and 5 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
from langflow.services.auth.utils import create_super_user
|
from langflow.services.auth.utils import create_super_user, verify_password
|
||||||
from langflow.services.database.utils import initialize_database
|
from langflow.services.database.utils import initialize_database
|
||||||
from langflow.services.manager import service_manager
|
from langflow.services.manager import service_manager
|
||||||
from langflow.services.schema import ServiceType
|
from langflow.services.schema import ServiceType
|
||||||
|
|
@ -43,6 +43,18 @@ def setup_superuser(settings_service, session):
|
||||||
# create superuser
|
# create superuser
|
||||||
create_super_user(db=session, username=username, password=password)
|
create_super_user(db=session, username=username, password=password)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
if "UNIQUE constraint failed: user.username" in str(exc):
|
||||||
|
# check if the password is valid, if it is, we will
|
||||||
|
# continue normally
|
||||||
|
# if not, we will raise an error
|
||||||
|
user = session.query(User).filter(User.username == username).first()
|
||||||
|
if (
|
||||||
|
user
|
||||||
|
and user.is_superuser is True
|
||||||
|
and verify_password(password, user.password)
|
||||||
|
):
|
||||||
|
return # superuser already exists
|
||||||
|
else:
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"Could not create superuser. Please create a superuser manually."
|
"Could not create superuser. Please create a superuser manually."
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue