From 90c9735767af1cf3ba7e40e71dfa0fb30efc1ee8 Mon Sep 17 00:00:00 2001 From: Louis-Amaury Chaib Date: Fri, 13 Sep 2024 21:10:18 +0200 Subject: [PATCH] feat: also render if_not_exists for CreateTableOp and if_exists for DropTableOp --- alembic/autogenerate/render.py | 7 +++++++ docs/build/unreleased/1446.rst | 4 ++-- tests/test_autogen_render.py | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/alembic/autogenerate/render.py b/alembic/autogenerate/render.py index 36f3f864..38bdbfca 100644 --- a/alembic/autogenerate/render.py +++ b/alembic/autogenerate/render.py @@ -279,6 +279,9 @@ def _add_table(autogen_context: AutogenContext, op: ops.CreateTableOp) -> str: prefixes = ", ".join("'%s'" % p for p in table._prefixes) text += ",\nprefixes=[%s]" % prefixes + if op.if_not_exists is not None: + text += ",\nif_not_exists=%r" % bool(op.if_not_exists) + text += "\n)" return text @@ -291,6 +294,10 @@ def _drop_table(autogen_context: AutogenContext, op: ops.DropTableOp) -> str: } if op.schema: text += ", schema=%r" % _ident(op.schema) + + if op.if_exists is not None: + text += ", if_exists=%r" % bool(op.if_exists) + text += ")" return text diff --git a/docs/build/unreleased/1446.rst b/docs/build/unreleased/1446.rst index 16698144..5bf1a521 100644 --- a/docs/build/unreleased/1446.rst +++ b/docs/build/unreleased/1446.rst @@ -2,5 +2,5 @@ :tags: usecase, autogenerate :tickets: 151 - Render `if_exists` and `if_not_exists` in `CreateIndexOp` and `DropIndexOp` in autogenerate context. - Pull request courtesy of Louis-Amaury Chaib + Render `if_exists` and `if_not_exists` in `CreateTableOp`, `CreateIndexOp`, `DropTableOp` and `DropIndexOp` in autogenerate context. + Pull request courtesy of Louis-Amaury Chaib (@lachaib). diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index 927e5853..14a33194 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -1017,6 +1017,19 @@ def test_render_addtl_args(self): "mysql_engine='InnoDB',sqlite_autoincrement=True)", ) + def test_render_if_not_exists(self): + t = self.table() + op_obj = ops.CreateTableOp.from_table(t) + op_obj.if_not_exists = True + eq_ignore_whitespace( + autogenerate.render_op_text(self.autogen_context, op_obj), + "op.create_table('test'," + "sa.Column('id', sa.Integer(), nullable=False)," + "sa.Column('active', sa.Boolean(), nullable=True)," + "sa.Column('code', sa.String(length=255), nullable=True)," + "sa.PrimaryKeyConstraint('id'),if_not_exists=True)", + ) + def test_render_drop_table(self): op_obj = ops.DropTableOp.from_table(Table("sometable", MetaData())) eq_ignore_whitespace( @@ -1033,6 +1046,15 @@ def test_render_drop_table_w_schema(self): "op.drop_table('sometable', schema='foo')", ) + def test_render_drop_table_if_exists(self): + t = self.table() + op_obj = ops.DropTableOp.from_table(t) + op_obj.if_exists = True + eq_ignore_whitespace( + autogenerate.render_op_text(self.autogen_context, op_obj), + "op.drop_table('test', if_exists=True)", + ) + def test_render_table_no_implicit_check(self): m = MetaData() t = Table("test", m, Column("x", Boolean()))