Skip to content

Commit

Permalink
Render if_not_exists option for CreateIndexOp and DropIndexOp
Browse files Browse the repository at this point in the history
Fixes: #151
  • Loading branch information
lachaib committed Aug 28, 2024
1 parent 434a788 commit cb57978
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions alembic/autogenerate/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions docs/build/unreleased/1446.rst
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions tests/test_autogen_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb57978

Please sign in to comment.