From 7ffe366fb8db51df587ef7ff49b50ef6b6ea157e Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 10 Oct 2023 15:01:48 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(manager.py):=20fix=20incorre?= =?UTF-8?q?ct=20check=20for=20alembic=20initialization=20and=20add=20prope?= =?UTF-8?q?r=20error=20handling=20=E2=9C=A8=20feat(manager.py):=20add=20su?= =?UTF-8?q?pport=20for=20initializing=20alembic=20if=20not=20already=20ini?= =?UTF-8?q?tialized=20before=20running=20migrations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/services/database/manager.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/backend/langflow/services/database/manager.py b/src/backend/langflow/services/database/manager.py index c743387eb..7465c804f 100644 --- a/src/backend/langflow/services/database/manager.py +++ b/src/backend/langflow/services/database/manager.py @@ -115,7 +115,35 @@ class DatabaseService(Service): return True + def init_alembic(self): + logger.info("Initializing alembic") + alembic_cfg = Config() + alembic_cfg.set_main_option("script_location", str(self.script_location)) + alembic_cfg.set_main_option("sqlalchemy.url", self.database_url) + command.stamp(alembic_cfg, "head") + logger.info("Alembic initialized") + def run_migrations(self): + # First we need to check if alembic has been initialized + # If not, we need to initialize it + # if not self.script_location.exists(): # this is not the correct way to check if alembic has been initialized + # We need to check if the alembic_version table exists + # if not, we need to initialize alembic + with Session(self.engine) as session: + # If the table does not exist it throws an error + # so we need to catch it + try: + session.execute("SELECT * FROM alembic_version") + except Exception: + logger.info("Alembic not initialized") + try: + self.init_alembic() + except Exception as exc: + logger.error(f"Error initializing alembic: {exc}") + raise RuntimeError("Error initializing alembic") from exc + else: + logger.info("Alembic already initialized") + logger.info(f"Running DB migrations in {self.script_location}") alembic_cfg = Config() alembic_cfg.set_main_option("script_location", str(self.script_location))