summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/schema.py
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/backends/postgresql/schema.py
parent9a88e43aeba6cc85ffb2a48a06c296ccf68e0d71 (diff)
downloaddjango-85ac838d9e6975130b5c55299e9d7d1222a8e289.tar.gz
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/backends/postgresql/schema.py')
-rw-r--r--django/db/backends/postgresql/schema.py25
1 files changed, 25 insertions, 0 deletions
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,
+ )