From 9697e06ec9a9ade2ada785997559984bb51571e6 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 10 Oct 2023 15:00:13 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(change=5Fcolumns=5Fto=5F?= =?UTF-8?q?be=5Fnullable.py):=20add=20exception=20handling=20and=20rollbac?= =?UTF-8?q?k=20in=20case=20of=20SQLAlchemyError=20to=20prevent=20data=20lo?= =?UTF-8?q?ss=20during=20migration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../versions/eb5866d51fd2_change_columns_to_be_nullable.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/backend/langflow/alembic/versions/eb5866d51fd2_change_columns_to_be_nullable.py b/src/backend/langflow/alembic/versions/eb5866d51fd2_change_columns_to_be_nullable.py index 10d503e60..cb126c926 100644 --- a/src/backend/langflow/alembic/versions/eb5866d51fd2_change_columns_to_be_nullable.py +++ b/src/backend/langflow/alembic/versions/eb5866d51fd2_change_columns_to_be_nullable.py @@ -9,6 +9,7 @@ from typing import Sequence, Union from alembic import op import sqlalchemy as sa +from sqlalchemy import exc import sqlmodel # noqa: F401 # revision identifiers, used by Alembic. @@ -20,16 +21,21 @@ depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### + connection = op.get_bind() try: op.drop_table("flowstyle") with op.batch_alter_table("component", schema=None) as batch_op: batch_op.drop_index("ix_component_frontend_node_id") batch_op.drop_index("ix_component_name") + except exc.SQLAlchemyError: + connection.execute("ROLLBACK") except Exception: pass try: op.drop_table("component") + except exc.SQLAlchemyError: + connection.execute("ROLLBACK") except Exception: pass # ### end Alembic commands ### 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 2/2] =?UTF-8?q?=F0=9F=90=9B=20fix(manager.py):=20fix=20inc?= =?UTF-8?q?orrect=20check=20for=20alembic=20initialization=20and=20add=20p?= =?UTF-8?q?roper=20error=20handling=20=E2=9C=A8=20feat(manager.py):=20add?= =?UTF-8?q?=20support=20for=20initializing=20alembic=20if=20not=20already?= =?UTF-8?q?=20initialized=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))