| 1 |
from django.contrib.gis.db.models import GeometryField |
| 2 |
from django.db.backends.postgresql.schema import DatabaseSchemaEditor |
| 3 |
from django.db.models.expressions import Col, Func |
| 4 |
|
| 5 |
|
| 6 |
class PostGISSchemaEditor(DatabaseSchemaEditor): |
| 7 |
geom_index_type = "GIST" |
| 8 |
geom_index_ops_nd = "GIST_GEOMETRY_OPS_ND" |
| 9 |
rast_index_template = "ST_ConvexHull(%(expressions)s)" |
| 10 |
|
| 11 |
sql_alter_column_to_3d = ( |
| 12 |
"ALTER COLUMN %(column)s TYPE %(type)s USING ST_Force3D(%(column)s)::%(type)s" |
| 13 |
) |
| 14 |
sql_alter_column_to_2d = ( |
| 15 |
"ALTER COLUMN %(column)s TYPE %(type)s USING ST_Force2D(%(column)s)::%(type)s" |
| 16 |
) |
| 17 |
|
| 18 |
def geo_quote_name(self, name): |
| 19 |
return self.connection.ops.geo_quote_name(name) |
| 20 |
|
| 21 |
def _field_should_be_indexed(self, model, field): |
| 22 |
if getattr(field, "spatial_index", False): |
| 23 |
return True |
| 24 |
return super()._field_should_be_indexed(model, field) |
| 25 |
|
| 26 |
def _create_index_sql(self, model, *, fields=None, **kwargs): |
| 27 |
if fields is None or len(fields) != 1 or not hasattr(fields[0], "geodetic"): |
| 28 |
return super()._create_index_sql(model, fields=fields, **kwargs) |
| 29 |
|
| 30 |
return self._create_spatial_index_sql(model, fields[0], **kwargs) |
| 31 |
|
| 32 |
def _alter_column_type_sql( |
| 33 |
self, table, old_field, new_field, new_type, old_collation, new_collation |
| 34 |
): |
| 35 |
""" |
| 36 |
Special case when dimension changed. |
| 37 |
""" |
| 38 |
if not hasattr(old_field, "dim") or not hasattr(new_field, "dim"): |
| 39 |
return super()._alter_column_type_sql( |
| 40 |
table, old_field, new_field, new_type, old_collation, new_collation |
| 41 |
) |
| 42 |
|
| 43 |
if old_field.dim == 2 and new_field.dim == 3: |
| 44 |
sql_alter = self.sql_alter_column_to_3d |
| 45 |
elif old_field.dim == 3 and new_field.dim == 2: |
| 46 |
sql_alter = self.sql_alter_column_to_2d |
| 47 |
else: |
| 48 |
sql_alter = self.sql_alter_column_type |
| 49 |
return ( |
| 50 |
( |
| 51 |
sql_alter |
| 52 |
% { |
| 53 |
"column": self.quote_name(new_field.column), |
| 54 |
"type": new_type, |
| 55 |
"collation": "", |
| 56 |
}, |
| 57 |
[], |
| 58 |
), |
| 59 |
[], |
| 60 |
) |
| 61 |
|
| 62 |
def _alter_field( |
| 63 |
self, |
| 64 |
model, |
| 65 |
old_field, |
| 66 |
new_field, |
| 67 |
old_type, |
| 68 |
new_type, |
| 69 |
old_db_params, |
| 70 |
new_db_params, |
| 71 |
strict=False, |
| 72 |
): |
| 73 |
super()._alter_field( |
| 74 |
model, |
| 75 |
old_field, |
| 76 |
new_field, |
| 77 |
old_type, |
| 78 |
new_type, |
| 79 |
old_db_params, |
| 80 |
new_db_params, |
| 81 |
strict=strict, |
| 82 |
) |
| 83 |
|
| 84 |
old_field_spatial_index = ( |
| 85 |
isinstance(old_field, GeometryField) and old_field.spatial_index |
| 86 |
) |
| 87 |
new_field_spatial_index = ( |
| 88 |
isinstance(new_field, GeometryField) and new_field.spatial_index |
| 89 |
) |
| 90 |
if not old_field_spatial_index and new_field_spatial_index: |
| 91 |
self.execute(self._create_spatial_index_sql(model, new_field)) |
| 92 |
elif old_field_spatial_index and not new_field_spatial_index: |
| 93 |
self.execute(self._delete_spatial_index_sql(model, old_field)) |
| 94 |
|
| 95 |
def _create_spatial_index_name(self, model, field): |
| 96 |
return self._create_index_name(model._meta.db_table, [field.column], "_id") |
| 97 |
|
| 98 |
def _create_spatial_index_sql(self, model, field, **kwargs): |
| 99 |
expressions = None |
| 100 |
opclasses = None |
| 101 |
fields = [field] |
| 102 |
if field.geom_type == "RASTER": |
| 103 |
# For raster fields, wrap index creation SQL statement with ST_ConvexHull. |
| 104 |
# Indexes on raster columns are based on the convex hull of the raster. |
| 105 |
expressions = Func(Col(None, field), template=self.rast_index_template) |
| 106 |
fields = None |
| 107 |
elif field.dim > 2 and not field.geography: |
| 108 |
# Use "nd" ops which are fast on multidimensional cases |
| 109 |
opclasses = [self.geom_index_ops_nd] |
| 110 |
if not (name := kwargs.get("name")): |
| 111 |
name = self._create_spatial_index_name(model, field) |
| 112 |
|
| 113 |
return super()._create_index_sql( |
| 114 |
model, |
| 115 |
fields=fields, |
| 116 |
name=name, |
| 117 |
using=" USING %s" % self.geom_index_type, |
| 118 |
opclasses=opclasses, |
| 119 |
expressions=expressions, |
| 120 |
) |
| 121 |
|
| 122 |
def _delete_spatial_index_sql(self, model, field): |
| 123 |
index_name = self._create_spatial_index_name(model, field) |
| 124 |
return self._delete_index_sql(model, index_name) |