From cb57978fa463cd866abc69db2761d8b5e6421ade Mon Sep 17 00:00:00 2001 From: Louis-Amaury Chaib Date: Tue, 19 Mar 2024 12:23:39 +0100 Subject: [PATCH] Render `if_not_exists` option for CreateIndexOp and DropIndexOp Fixes: #151 --- alembic/autogenerate/render.py | 4 ++++ docs/build/unreleased/1446.rst | 6 ++++++ tests/test_autogen_render.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 docs/build/unreleased/1446.rst diff --git a/alembic/autogenerate/render.py b/alembic/autogenerate/render.py index 61d56acf..36f3f864 100644 --- a/alembic/autogenerate/render.py +++ b/alembic/autogenerate/render.py @@ -324,6 +324,8 @@ def _add_index(autogen_context: AutogenContext, op: ops.CreateIndexOp) -> str: assert index.table is not None opts = _render_dialect_kwargs_items(autogen_context, index) + if op.if_not_exists is not None: + opts.append("if_not_exists=%r" % bool(op.if_not_exists)) text = tmpl % { "prefix": _alembic_autogenerate_prefix(autogen_context), "name": _render_gen_name(autogen_context, index.name), @@ -356,6 +358,8 @@ def _drop_index(autogen_context: AutogenContext, op: ops.DropIndexOp) -> str: "table_name=%(table_name)r%(schema)s%(kwargs)s)" ) opts = _render_dialect_kwargs_items(autogen_context, index) + if op.if_exists is not None: + opts.append("if_exists=%r" % bool(op.if_exists)) text = tmpl % { "prefix": _alembic_autogenerate_prefix(autogen_context), "name": _render_gen_name(autogen_context, op.index_name), diff --git a/docs/build/unreleased/1446.rst b/docs/build/unreleased/1446.rst new file mode 100644 index 00000000..16698144 --- /dev/null +++ b/docs/build/unreleased/1446.rst @@ -0,0 +1,6 @@ +.. change:: + :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 diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index 7907f7ec..927e5853 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -93,6 +93,20 @@ def test_render_add_index(self): "['active', 'code'], unique=False)", ) + def test_render_add_index_if_not_exists(self): + """ + autogenerate.render._add_index + """ + t = self.table() + idx = Index("test_active_code_idx", t.c.active, t.c.code) + op_obj = ops.CreateIndexOp.from_index(idx) + op_obj.if_not_exists = True + eq_ignore_whitespace( + autogenerate.render_op_text(self.autogen_context, op_obj), + "op.create_index('test_active_code_idx', 'test', " + "['active', 'code'], unique=False, if_not_exists=True)", + ) + @testing.emits_warning("Can't validate argument ") def test_render_add_index_custom_kwarg(self): t = self.table() @@ -212,6 +226,20 @@ def test_drop_index(self): "op.drop_index('test_active_code_idx', table_name='test')", ) + def test_drop_index_if_exists(self): + """ + autogenerate.render._drop_index + """ + t = self.table() + idx = Index("test_active_code_idx", t.c.active, t.c.code) + op_obj = ops.DropIndexOp.from_index(idx) + op_obj.if_exists = True + eq_ignore_whitespace( + autogenerate.render_op_text(self.autogen_context, op_obj), + "op.drop_index('test_active_code_idx', table_name='test', " + "if_exists=True)", + ) + def test_drop_index_text(self): """ autogenerate.render._drop_index