2015-01-13 04:20:40 +08:00
|
|
|
import psycopg2
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
2017-10-12 01:25:52 +08:00
|
|
|
from django.db.backends.ddl_references import IndexColumns
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2012-06-19 00:32:03 +08:00
|
|
|
|
|
|
|
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
|
2013-12-11 22:19:05 +08:00
|
|
|
|
2015-06-19 09:47:21 +08:00
|
|
|
sql_alter_column_type = "ALTER COLUMN %(column)s TYPE %(type)s USING %(column)s::%(type)s"
|
|
|
|
|
2013-12-11 22:19:05 +08:00
|
|
|
sql_create_sequence = "CREATE SEQUENCE %(sequence)s"
|
|
|
|
sql_delete_sequence = "DROP SEQUENCE IF EXISTS %(sequence)s CASCADE"
|
|
|
|
sql_set_sequence_max = "SELECT setval('%(sequence)s', MAX(%(column)s)) FROM %(table)s"
|
2015-06-19 09:47:21 +08:00
|
|
|
|
2018-09-13 15:34:02 +08:00
|
|
|
sql_create_index = "CREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s)%(extra)s%(condition)s"
|
2017-03-03 23:50:34 +08:00
|
|
|
sql_delete_index = "DROP INDEX IF EXISTS %(name)s"
|
2013-12-11 22:19:05 +08:00
|
|
|
|
2016-12-18 04:06:47 +08:00
|
|
|
# Setting the constraint to IMMEDIATE runs any deferred checks to allow
|
|
|
|
# dropping it in the same transaction.
|
|
|
|
sql_delete_fk = "SET CONSTRAINTS %(name)s IMMEDIATE; ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"
|
|
|
|
|
2017-08-09 04:13:02 +08:00
|
|
|
sql_delete_procedure = 'DROP FUNCTION %(procedure)s(%(param_types)s)'
|
|
|
|
|
2014-02-09 20:41:55 +08:00
|
|
|
def quote_value(self, value):
|
|
|
|
return psycopg2.extensions.adapt(value)
|
|
|
|
|
2016-07-15 22:34:37 +08:00
|
|
|
def _field_indexes_sql(self, model, field):
|
2017-01-21 21:13:44 +08:00
|
|
|
output = super()._field_indexes_sql(model, field)
|
2016-07-13 08:50:17 +08:00
|
|
|
like_index_statement = self._create_like_index_sql(model, field)
|
|
|
|
if like_index_statement is not None:
|
2016-07-15 22:34:37 +08:00
|
|
|
output.append(like_index_statement)
|
2014-12-05 00:56:11 +08:00
|
|
|
return output
|
|
|
|
|
2015-11-08 00:08:03 +08:00
|
|
|
def _create_like_index_sql(self, model, field):
|
|
|
|
"""
|
|
|
|
Return the statement to create an index with varchar operator pattern
|
|
|
|
when the column type is 'varchar' or 'text', otherwise return None.
|
|
|
|
"""
|
|
|
|
db_type = field.db_type(connection=self.connection)
|
|
|
|
if db_type is not None and (field.db_index or field.unique):
|
|
|
|
# Fields with database column types of `varchar` and `text` need
|
|
|
|
# a second index that specifies their operator class, which is
|
|
|
|
# needed when performing correct LIKE queries outside the
|
|
|
|
# C locale. See #12234.
|
|
|
|
#
|
|
|
|
# The same doesn't apply to array fields such as varchar[size]
|
|
|
|
# and text[size], so skip them.
|
|
|
|
if '[' in db_type:
|
|
|
|
return None
|
|
|
|
if db_type.startswith('varchar'):
|
2017-10-12 01:25:52 +08:00
|
|
|
return self._create_index_sql(model, [field], suffix='_like', opclasses=['varchar_pattern_ops'])
|
2015-11-08 00:08:03 +08:00
|
|
|
elif db_type.startswith('text'):
|
2017-10-12 01:25:52 +08:00
|
|
|
return self._create_index_sql(model, [field], suffix='_like', opclasses=['text_pattern_ops'])
|
2015-11-08 00:08:03 +08:00
|
|
|
return None
|
|
|
|
|
2017-06-06 23:08:40 +08:00
|
|
|
def _alter_column_type_sql(self, model, old_field, new_field, new_type):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Make ALTER TYPE with SERIAL make sense."""
|
2017-06-06 23:08:40 +08:00
|
|
|
table = model._meta.db_table
|
2015-07-02 16:43:15 +08:00
|
|
|
if new_type.lower() in ("serial", "bigserial"):
|
2015-04-11 22:10:31 +08:00
|
|
|
column = new_field.column
|
2013-12-11 22:19:05 +08:00
|
|
|
sequence_name = "%s_%s_seq" % (table, column)
|
2015-07-02 16:43:15 +08:00
|
|
|
col_type = "integer" if new_type.lower() == "serial" else "bigint"
|
2013-12-11 22:19:05 +08:00
|
|
|
return (
|
|
|
|
(
|
|
|
|
self.sql_alter_column_type % {
|
|
|
|
"column": self.quote_name(column),
|
2015-07-02 16:43:15 +08:00
|
|
|
"type": col_type,
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
[
|
|
|
|
(
|
|
|
|
self.sql_delete_sequence % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"sequence": self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
self.sql_create_sequence % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"sequence": self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
self.sql_alter_column % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"table": self.quote_name(table),
|
2013-12-11 22:19:05 +08:00
|
|
|
"changes": self.sql_alter_column_default % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"column": self.quote_name(column),
|
|
|
|
"default": "nextval('%s')" % self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
self.sql_set_sequence_max % {
|
2015-06-02 05:06:54 +08:00
|
|
|
"table": self.quote_name(table),
|
|
|
|
"column": self.quote_name(column),
|
|
|
|
"sequence": self.quote_name(sequence_name),
|
2013-12-11 22:19:05 +08:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
else:
|
2017-06-06 23:08:40 +08:00
|
|
|
return super()._alter_column_type_sql(model, old_field, new_field, new_type)
|
2015-11-08 00:08:03 +08:00
|
|
|
|
|
|
|
def _alter_field(self, model, old_field, new_field, old_type, new_type,
|
|
|
|
old_db_params, new_db_params, strict=False):
|
2017-11-15 05:51:51 +08:00
|
|
|
# Drop indexes on varchar/text/citext columns that are changing to a
|
|
|
|
# different type.
|
2017-05-24 13:25:59 +08:00
|
|
|
if (old_field.db_index or old_field.unique) and (
|
|
|
|
(old_type.startswith('varchar') and not new_type.startswith('varchar')) or
|
2017-11-15 05:51:51 +08:00
|
|
|
(old_type.startswith('text') and not new_type.startswith('text')) or
|
|
|
|
(old_type.startswith('citext') and not new_type.startswith('citext'))
|
2017-05-24 13:25:59 +08:00
|
|
|
):
|
2017-04-02 02:00:47 +08:00
|
|
|
index_name = self._create_index_name(model._meta.db_table, [old_field.column], suffix='_like')
|
2017-05-24 13:25:59 +08:00
|
|
|
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
|
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
super()._alter_field(
|
2015-11-08 00:08:03 +08:00
|
|
|
model, old_field, new_field, old_type, new_type, old_db_params,
|
|
|
|
new_db_params, strict,
|
|
|
|
)
|
|
|
|
# Added an index? Create any PostgreSQL-specific indexes.
|
2016-06-23 12:46:11 +08:00
|
|
|
if ((not (old_field.db_index or old_field.unique) and new_field.db_index) or
|
|
|
|
(not old_field.unique and new_field.unique)):
|
2015-11-08 00:08:03 +08:00
|
|
|
like_index_statement = self._create_like_index_sql(model, new_field)
|
|
|
|
if like_index_statement is not None:
|
|
|
|
self.execute(like_index_statement)
|
|
|
|
|
|
|
|
# Removed an index? Drop any PostgreSQL-specific indexes.
|
2016-06-23 12:46:11 +08:00
|
|
|
if old_field.unique and not (new_field.db_index or new_field.unique):
|
2017-04-02 02:00:47 +08:00
|
|
|
index_to_remove = self._create_index_name(model._meta.db_table, [old_field.column], suffix='_like')
|
2017-03-03 23:50:34 +08:00
|
|
|
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_to_remove))
|
2017-10-12 01:25:52 +08:00
|
|
|
|
|
|
|
def _index_columns(self, table, columns, col_suffixes, opclasses):
|
|
|
|
if opclasses:
|
|
|
|
return IndexColumns(table, columns, self.quote_name, col_suffixes=col_suffixes, opclasses=opclasses)
|
|
|
|
return super()._index_columns(table, columns, col_suffixes, opclasses)
|