Skip to content

Commit 21f274c

Browse files
committed
Make creating concurrent index bit prettier
1 parent 9fd79b2 commit 21f274c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

django_pgviews/view.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.core import exceptions
1212
from django.db import connection, transaction
1313
from django.db import models
14+
from django.db.models import Index
1415
from django.db.models.query import QuerySet
1516

1617
from django_pgviews.db import get_fields_by_name
@@ -95,6 +96,10 @@ def _drop_mat_view(cursor, view_name):
9596
cursor.execute("DROP MATERIALIZED VIEW IF EXISTS {0} CASCADE;".format(view_name))
9697

9798

99+
def _concurrent_index_name(view_name, concurrent_index):
100+
return view_name + "_" + "_".join([s.strip() for s in concurrent_index.split(",")]) + "_index"
101+
102+
98103
@transaction.atomic()
99104
def create_materialized_view(connection, view_cls, check_sql_changed=False):
100105
"""
@@ -160,9 +165,12 @@ def create_materialized_view(connection, view_cls, check_sql_changed=False):
160165
log.info("pgview created materialized view %s (%s)", view_name, schema_name_log)
161166

162167
if concurrent_index is not None:
163-
index_sub_name = "_".join([s.strip() for s in concurrent_index.split(",")])
164168
cursor.execute(
165-
"CREATE UNIQUE INDEX {0}_{1}_index ON {0} ({2})".format(view_name, index_sub_name, concurrent_index)
169+
"CREATE UNIQUE INDEX {index_name} ON {view_name} ({concurrent_index})".format(
170+
view_name=view_name,
171+
index_name=_concurrent_index_name(view_name, concurrent_index),
172+
concurrent_index=concurrent_index,
173+
)
166174
)
167175
log.info("pgview created concurrent index on view %s (%s)", view_name, schema_name_log)
168176

0 commit comments

Comments
 (0)