| 1 |
import logging |
| 2 |
|
| 3 |
from django.contrib.gis.db.models import GeometryField |
| 4 |
from django.db import OperationalError |
| 5 |
from django.db.backends.mysql.schema import DatabaseSchemaEditor |
| 6 |
|
| 7 |
logger = logging.getLogger("django.contrib.gis") |
| 8 |
|
| 9 |
|
| 10 |
class MySQLGISSchemaEditor(DatabaseSchemaEditor): |
| 11 |
sql_add_spatial_index = "CREATE SPATIAL INDEX %(index)s ON %(table)s(%(column)s)" |
| 12 |
|
| 13 |
def skip_default(self, field): |
| 14 |
# Geometry fields are stored as BLOB/TEXT, for which MySQL < 8.0.13 |
| 15 |
# doesn't support defaults. |
| 16 |
if ( |
| 17 |
isinstance(field, GeometryField) |
| 18 |
and not self._supports_limited_data_type_defaults |
| 19 |
): |
| 20 |
return True |
| 21 |
return super().skip_default(field) |
| 22 |
|
| 23 |
def quote_value(self, value): |
| 24 |
if isinstance(value, self.connection.ops.Adapter): |
| 25 |
return super().quote_value(str(value)) |
| 26 |
return super().quote_value(value) |
| 27 |
|
| 28 |
def _field_indexes_sql(self, model, field): |
| 29 |
if isinstance(field, GeometryField) and field.spatial_index and not field.null: |
| 30 |
with self.connection.cursor() as cursor: |
| 31 |
supports_spatial_index = ( |
| 32 |
self.connection.introspection.supports_spatial_index( |
| 33 |
cursor, model._meta.db_table |
| 34 |
) |
| 35 |
) |
| 36 |
sql = self._create_spatial_index_sql(model, field) |
| 37 |
if supports_spatial_index: |
| 38 |
return [sql] |
| 39 |
else: |
| 40 |
logger.error( |
| 41 |
f"Cannot create SPATIAL INDEX {sql}. Only MyISAM, Aria, and InnoDB " |
| 42 |
f"support them.", |
| 43 |
) |
| 44 |
return [] |
| 45 |
return super()._field_indexes_sql(model, field) |
| 46 |
|
| 47 |
def remove_field(self, model, field): |
| 48 |
if isinstance(field, GeometryField) and field.spatial_index and not field.null: |
| 49 |
sql = self._delete_spatial_index_sql(model, field) |
| 50 |
try: |
| 51 |
self.execute(sql) |
| 52 |
except OperationalError: |
| 53 |
logger.error( |
| 54 |
"Couldn't remove spatial index: %s (may be expected " |
| 55 |
"if your storage engine doesn't support them).", |
| 56 |
sql, |
| 57 |
) |
| 58 |
|
| 59 |
super().remove_field(model, field) |
| 60 |
|
| 61 |
def _alter_field( |
| 62 |
self, |
| 63 |
model, |
| 64 |
old_field, |
| 65 |
new_field, |
| 66 |
old_type, |
| 67 |
new_type, |
| 68 |
old_db_params, |
| 69 |
new_db_params, |
| 70 |
strict=False, |
| 71 |
): |
| 72 |
super()._alter_field( |
| 73 |
model, |
| 74 |
old_field, |
| 75 |
new_field, |
| 76 |
old_type, |
| 77 |
new_type, |
| 78 |
old_db_params, |
| 79 |
new_db_params, |
| 80 |
strict=strict, |
| 81 |
) |
| 82 |
|
| 83 |
old_field_spatial_index = ( |
| 84 |
isinstance(old_field, GeometryField) |
| 85 |
and old_field.spatial_index |
| 86 |
and not old_field.null |
| 87 |
) |
| 88 |
new_field_spatial_index = ( |
| 89 |
isinstance(new_field, GeometryField) |
| 90 |
and new_field.spatial_index |
| 91 |
and not new_field.null |
| 92 |
) |
| 93 |
if not old_field_spatial_index and new_field_spatial_index: |
| 94 |
self.execute(self._create_spatial_index_sql(model, new_field)) |
| 95 |
elif old_field_spatial_index and not new_field_spatial_index: |
| 96 |
self.execute(self._delete_spatial_index_sql(model, old_field)) |
| 97 |
|
| 98 |
def _create_spatial_index_name(self, model, field): |
| 99 |
return "%s_%s_id" % (model._meta.db_table, field.column) |
| 100 |
|
| 101 |
def _create_spatial_index_sql(self, model, field): |
| 102 |
index_name = self._create_spatial_index_name(model, field) |
| 103 |
qn = self.connection.ops.quote_name |
| 104 |
return self.sql_add_spatial_index % { |
| 105 |
"index": qn(index_name), |
| 106 |
"table": qn(model._meta.db_table), |
| 107 |
"column": qn(field.column), |
| 108 |
} |
| 109 |
|
| 110 |
def _delete_spatial_index_sql(self, model, field): |
| 111 |
index_name = self._create_spatial_index_name(model, field) |
| 112 |
return self._delete_index_sql(model, index_name) |