| 1 |
from django.core.management import call_command |
| 2 |
from django.db import connection |
| 3 |
from django.test import TransactionTestCase |
| 4 |
|
| 5 |
|
| 6 |
class MigrateTests(TransactionTestCase): |
| 7 |
""" |
| 8 |
Tests running the migrate command in Geodjango. |
| 9 |
""" |
| 10 |
|
| 11 |
available_apps = ["gis_tests.gis_migrations"] |
| 12 |
|
| 13 |
def get_table_description(self, table): |
| 14 |
with connection.cursor() as cursor: |
| 15 |
return connection.introspection.get_table_description(cursor, table) |
| 16 |
|
| 17 |
def assertTableExists(self, table): |
| 18 |
with connection.cursor() as cursor: |
| 19 |
self.assertIn(table, connection.introspection.table_names(cursor)) |
| 20 |
|
| 21 |
def assertTableNotExists(self, table): |
| 22 |
with connection.cursor() as cursor: |
| 23 |
self.assertNotIn(table, connection.introspection.table_names(cursor)) |
| 24 |
|
| 25 |
def test_migrate_gis(self): |
| 26 |
""" |
| 27 |
Tests basic usage of the migrate command when a model uses Geodjango |
| 28 |
fields (#22001). |
| 29 |
|
| 30 |
It's also used to showcase an error in migrations where spatialite is |
| 31 |
enabled and geo tables are renamed resulting in unique constraint |
| 32 |
failure on geometry_columns (#23030). |
| 33 |
""" |
| 34 |
# The right tables exist |
| 35 |
self.assertTableExists("gis_migrations_neighborhood") |
| 36 |
self.assertTableExists("gis_migrations_household") |
| 37 |
self.assertTableExists("gis_migrations_family") |
| 38 |
if connection.features.supports_raster: |
| 39 |
self.assertTableExists("gis_migrations_heatmap") |
| 40 |
# Unmigrate models. |
| 41 |
call_command("migrate", "gis_migrations", "0001", verbosity=0) |
| 42 |
# All tables are gone |
| 43 |
self.assertTableNotExists("gis_migrations_neighborhood") |
| 44 |
self.assertTableNotExists("gis_migrations_household") |
| 45 |
self.assertTableNotExists("gis_migrations_family") |
| 46 |
if connection.features.supports_raster: |
| 47 |
self.assertTableNotExists("gis_migrations_heatmap") |
| 48 |
# Even geometry columns metadata |
| 49 |
try: |
| 50 |
GeoColumn = connection.ops.geometry_columns() |
| 51 |
except NotImplementedError: |
| 52 |
# Not all GIS backends have geometry columns model |
| 53 |
pass |
| 54 |
else: |
| 55 |
qs = GeoColumn.objects.filter( |
| 56 |
**{ |
| 57 |
"%s__in" |
| 58 |
% GeoColumn.table_name_col(): ["gis_neighborhood", "gis_household"] |
| 59 |
} |
| 60 |
) |
| 61 |
self.assertEqual(qs.count(), 0) |
| 62 |
# Revert the "unmigration" |
| 63 |
call_command("migrate", "gis_migrations", verbosity=0) |