From 423e2cf1eafcac42bfa84de6043a35ad445e296d Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 22 Apr 2014 22:49:52 +0200 Subject: [PATCH] [1.7.x] Used migration framework in GIS test tearDown Backport of 48c4ea414 from master. --- django/contrib/gis/db/backends/postgis/schema.py | 9 ++++++++- .../gis/tests/gis_migrations/test_operations.py | 13 +++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/django/contrib/gis/db/backends/postgis/schema.py b/django/contrib/gis/db/backends/postgis/schema.py index 9c2fc70841..db59d34492 100644 --- a/django/contrib/gis/db/backends/postgis/schema.py +++ b/django/contrib/gis/db/backends/postgis/schema.py @@ -10,6 +10,7 @@ class PostGISSchemaEditor(DatabaseSchemaEditor): sql_drop_geometry_column = "SELECT DropGeometryColumn(%(table)s, %(column)s)" sql_alter_geometry_column_not_null = "ALTER TABLE %(table)s ALTER COLUMN %(column)s SET NOT NULL" sql_add_spatial_index = "CREATE INDEX %(index)s ON %(table)s USING %(index_type)s (%(column)s %(ops)s)" + sql_clear_geometry_columns = "DELETE FROM geometry_columns WHERE f_table_name = %(table)s" def __init__(self, *args, **kwargs): super(PostGISSchemaEditor, self).__init__(*args, **kwargs) @@ -46,7 +47,7 @@ class PostGISSchemaEditor(DatabaseSchemaEditor): self.sql_alter_geometry_column_not_null % { "table": self.quote_name(model._meta.db_table), "column": self.quote_name(field.column), - }, + } ) if field.spatial_index: @@ -83,6 +84,12 @@ class PostGISSchemaEditor(DatabaseSchemaEditor): self.execute(sql) self.geometry_sql = [] + def delete_model(self, model): + super(PostGISSchemaEditor, self).delete_model(model) + self.execute(self.sql_clear_geometry_columns % { + "table": self.geo_quote_name(model._meta.db_table), + }) + def add_field(self, model, field): super(PostGISSchemaEditor, self).add_field(model, field) # Create geometry columns diff --git a/django/contrib/gis/tests/gis_migrations/test_operations.py b/django/contrib/gis/tests/gis_migrations/test_operations.py index 61e4d3b69f..c6ef682e3c 100644 --- a/django/contrib/gis/tests/gis_migrations/test_operations.py +++ b/django/contrib/gis/tests/gis_migrations/test_operations.py @@ -6,7 +6,6 @@ from django.contrib.gis.tests.utils import HAS_SPATIAL_DB from django.db import connection, migrations, models from django.db.migrations.migration import Migration from django.db.migrations.state import ProjectState -from django.db.utils import DatabaseError from django.test import TransactionTestCase if HAS_SPATIAL_DB: @@ -24,15 +23,7 @@ class OperationTests(TransactionTestCase): def tearDown(self): # Delete table after testing - with connection.cursor() as cursor: - try: - cursor.execute("DROP TABLE %s" % connection.ops.quote_name("gis_neighborhood")) - except DatabaseError: - pass - else: - if HAS_GEOMETRY_COLUMNS: - cursor.execute("DELETE FROM geometry_columns WHERE %s = %%s" % ( - GeometryColumns.table_name_col(),), ["gis_neighborhood"]) + self.apply_operations('gis', self.current_state, [migrations.DeleteModel("Neighborhood")]) super(OperationTests, self).tearDown() def get_table_description(self, table): @@ -84,6 +75,7 @@ class OperationTests(TransactionTestCase): GeometryColumns.objects.filter(**{GeometryColumns.table_name_col(): "gis_neighborhood"}).count(), 2 ) + self.current_state = new_state def test_remove_gis_field(self): """ @@ -103,3 +95,4 @@ class OperationTests(TransactionTestCase): GeometryColumns.objects.filter(**{GeometryColumns.table_name_col(): "gis_neighborhood"}).count(), 0 ) + self.current_state = new_state