diff options
| author | Claude Paroz <claude@2xlibre.net> | 2016-09-19 09:58:58 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2016-09-22 16:40:27 +0200 |
| commit | 92323d54fd6df077dc523c423c7bb2dd8dbde621 (patch) | |
| tree | 62db1a11c37845671b534707fb2872cda9dee2aa /django | |
| parent | 8e2ac3bcafb34e4f10be20f24e37319522459095 (diff) | |
Fixed #27056 -- Allowed migrating geometry field dimension on PostGIS
Thanks Tim Graham for the review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/gis/db/backends/postgis/schema.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/django/contrib/gis/db/backends/postgis/schema.py b/django/contrib/gis/db/backends/postgis/schema.py index 4ee46cb3ff..d88901b50d 100644 --- a/django/contrib/gis/db/backends/postgis/schema.py +++ b/django/contrib/gis/db/backends/postgis/schema.py @@ -6,6 +6,9 @@ class PostGISSchemaEditor(DatabaseSchemaEditor): geom_index_ops_nd = 'GIST_GEOMETRY_OPS_ND' rast_index_wrapper = 'ST_ConvexHull(%s)' + sql_alter_column_to_3d = "ALTER COLUMN %(column)s TYPE %(type)s USING ST_Force3D(%(column)s)::%(type)s" + sql_alter_column_to_2d = "ALTER COLUMN %(column)s TYPE %(type)s USING ST_Force2D(%(column)s)::%(type)s" + def geo_quote_name(self, name): return self.connection.ops.geo_quote_name(name) @@ -36,3 +39,29 @@ class PostGISSchemaEditor(DatabaseSchemaEditor): "columns": field_column, "extra": '', } + + def _alter_column_type_sql(self, table, old_field, new_field, new_type): + """ + Special case when dimension changed. + """ + if not hasattr(old_field, 'dim') or not hasattr(new_field, 'dim'): + return super(PostGISSchemaEditor, self)._alter_column_type_sql( + table, old_field, new_field, new_type + ) + + if old_field.dim == 2 and new_field.dim == 3: + sql_alter = self.sql_alter_column_to_3d + elif old_field.dim == 3 and new_field.dim == 2: + sql_alter = self.sql_alter_column_to_2d + else: + sql_alter = self.sql_alter_column_type + return ( + ( + sql_alter % { + "column": self.quote_name(new_field.column), + "type": new_type, + }, + [], + ), + [], + ) |
