Skip to content

Commit

Permalink
exposing if_not_exists argument in create_index
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Feb 21, 2021
1 parent 31f4646 commit f255f33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions piccolo/apps/migrations/auto/migration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ async def _run_alter_columns(self, backwards=False):
column._meta._name = column_name
await _Table.drop_index([column]).run()
await _Table.create_index(
[column], method=index_method
[column], method=index_method, if_not_exists=True
).run()
else:
# If the index value has changed, then we are either
Expand All @@ -427,7 +427,9 @@ async def _run_alter_columns(self, backwards=False):
kwargs = (
{"method": index_method} if index_method else {}
)
await _Table.create_index([column], **kwargs).run()
await _Table.create_index(
[column], if_not_exists=True, **kwargs
).run()
else:
await _Table.drop_index([column]).run()

Expand Down
8 changes: 7 additions & 1 deletion piccolo/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,20 @@ def create_index(
cls,
columns: t.List[t.Union[Column, str]],
method: IndexMethod = IndexMethod.btree,
if_not_exists: bool = False,
) -> CreateIndex:
"""
Create a table index. If multiple columns are specified, this refers
to a multicolumn index, rather than multiple single column indexes.
await Band.create_index([Band.name]).run()
"""
return CreateIndex(table=cls, columns=columns, method=method)
return CreateIndex(
table=cls,
columns=columns,
method=method,
if_not_exists=if_not_exists,
)

@classmethod
def drop_index(
Expand Down

0 comments on commit f255f33

Please sign in to comment.