🐛 fix(change_columns_to_be_nullable.py): add exception handling and rollback in case of SQLAlchemyError to prevent data loss during migration

This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-10-10 15:00:13 -03:00
commit 9697e06ec9

View file

@ -9,6 +9,7 @@ from typing import Sequence, Union
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy import exc
import sqlmodel # noqa: F401 import sqlmodel # noqa: F401
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
@ -20,16 +21,21 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
connection = op.get_bind()
try: try:
op.drop_table("flowstyle") op.drop_table("flowstyle")
with op.batch_alter_table("component", schema=None) as batch_op: 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_frontend_node_id")
batch_op.drop_index("ix_component_name") batch_op.drop_index("ix_component_name")
except exc.SQLAlchemyError:
connection.execute("ROLLBACK")
except Exception: except Exception:
pass pass
try: try:
op.drop_table("component") op.drop_table("component")
except exc.SQLAlchemyError:
connection.execute("ROLLBACK")
except Exception: except Exception:
pass pass
# ### end Alembic commands ### # ### end Alembic commands ###