summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorMads Jensen <mje@inducks.org>2019-07-25 13:44:18 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-08-21 13:10:06 +0200
commit85ac838d9e6975130b5c55299e9d7d1222a8e289 (patch)
tree2466fdf4773081dfbaa33c24cda4b01ec9fc23e6 /django/db
parent9a88e43aeba6cc85ffb2a48a06c296ccf68e0d71 (diff)
Fixed #21039 -- Added AddIndexConcurrently/RemoveIndexConcurrently operations for PostgreSQL.
Thanks to Simon Charettes for review. Co-Authored-By: Daniel Tao <daniel.tao@gmail.com>
Diffstat (limited to 'django/db')
-rw-r--r--django/db/backends/base/schema.py4
-rw-r--r--django/db/backends/postgresql/schema.py25
-rw-r--r--django/db/models/indexes.py7
3 files changed, 31 insertions, 5 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index 3540fd5d0d..efb291f4e6 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -959,9 +959,9 @@ class BaseDatabaseSchemaEditor:
condition=(' WHERE ' + condition) if condition else '',
)
- def _delete_index_sql(self, model, name):
+ def _delete_index_sql(self, model, name, sql=None):
return Statement(
- self.sql_delete_index,
+ sql or self.sql_delete_index,
table=Table(model._meta.db_table, self.quote_name),
name=self.quote_name(name),
)
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index eb5b182680..9384c5e3b2 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -13,7 +13,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
sql_set_sequence_owner = 'ALTER SEQUENCE %(sequence)s OWNED BY %(table)s.%(column)s'
sql_create_index = "CREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s)%(extra)s%(condition)s"
+ sql_create_index_concurrently = (
+ "CREATE INDEX CONCURRENTLY %(name)s ON %(table)s%(using)s (%(columns)s)%(extra)s%(condition)s"
+ )
sql_delete_index = "DROP INDEX IF EXISTS %(name)s"
+ sql_delete_index_concurrently = "DROP INDEX CONCURRENTLY IF EXISTS %(name)s"
sql_create_column_inline_fk = 'REFERENCES %(to_table)s(%(to_column)s)%(deferrable)s'
# Setting the constraint to IMMEDIATE runs any deferred checks to allow
@@ -157,3 +161,24 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
if opclasses:
return IndexColumns(table, columns, self.quote_name, col_suffixes=col_suffixes, opclasses=opclasses)
return super()._index_columns(table, columns, col_suffixes, opclasses)
+
+ def add_index(self, model, index, concurrently=False):
+ self.execute(index.create_sql(model, self, concurrently=concurrently), params=None)
+
+ def remove_index(self, model, index, concurrently=False):
+ self.execute(index.remove_sql(model, self, concurrently=concurrently))
+
+ def _delete_index_sql(self, model, name, sql=None, concurrently=False):
+ sql = self.sql_delete_index_concurrently if concurrently else self.sql_delete_index
+ return super()._delete_index_sql(model, name, sql)
+
+ def _create_index_sql(
+ self, model, fields, *, name=None, suffix='', using='',
+ db_tablespace=None, col_suffixes=(), sql=None, opclasses=(),
+ condition=None, concurrently=False,
+ ):
+ sql = self.sql_create_index if not concurrently else self.sql_create_index_concurrently
+ return super()._create_index_sql(
+ model, fields, name=name, suffix=suffix, using=using, db_tablespace=db_tablespace,
+ col_suffixes=col_suffixes, sql=sql, opclasses=opclasses, condition=condition,
+ )
diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py
index c093b45619..5154de056e 100644
--- a/django/db/models/indexes.py
+++ b/django/db/models/indexes.py
@@ -49,17 +49,18 @@ class Index:
# it's handled outside of that class, the work is done here.
return sql % tuple(map(schema_editor.quote_value, params))
- def create_sql(self, model, schema_editor, using=''):
+ def create_sql(self, model, schema_editor, using='', **kwargs):
fields = [model._meta.get_field(field_name) for field_name, _ in self.fields_orders]
col_suffixes = [order[1] for order in self.fields_orders]
condition = self._get_condition_sql(model, schema_editor)
return schema_editor._create_index_sql(
model, fields, name=self.name, using=using, db_tablespace=self.db_tablespace,
col_suffixes=col_suffixes, opclasses=self.opclasses, condition=condition,
+ **kwargs,
)
- def remove_sql(self, model, schema_editor):
- return schema_editor._delete_index_sql(model, self.name)
+ def remove_sql(self, model, schema_editor, **kwargs):
+ return schema_editor._delete_index_sql(model, self.name, **kwargs)
def deconstruct(self):
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)