From f5bee3e7ae30b18925fd85a5435e0cbbecb26d0d Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sat, 30 Sep 2023 00:05:29 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.py):=20handle=20unique?= =?UTF-8?q?=20constraint=20failure=20when=20creating=20superuser=20and=20c?= =?UTF-8?q?heck=20if=20superuser=20already=20exists=20with=20the=20same=20?= =?UTF-8?q?username=20and=20password=20to=20avoid=20duplicate=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/services/utils.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/services/utils.py b/src/backend/langflow/services/utils.py index 02d9816f5..e18b4938d 100644 --- a/src/backend/langflow/services/utils.py +++ b/src/backend/langflow/services/utils.py @@ -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.manager import service_manager from langflow.services.schema import ServiceType @@ -43,10 +43,22 @@ def setup_superuser(settings_service, session): # create superuser create_super_user(db=session, username=username, password=password) except Exception as exc: - logger.exception(exc) - raise RuntimeError( - "Could not create superuser. Please create a superuser manually." - ) from 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) + raise RuntimeError( + "Could not create superuser. Please create a superuser manually." + ) from exc # reset superuser credentials settings_service.auth_settings.reset_credentials() logger.debug("Superuser created successfully.")