2014-03-14 22:18:20 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.core.management import call_command
|
|
|
|
from django.db import connection
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.test import TransactionTestCase, skipUnlessDBFeature
|
2014-03-14 22:18:20 +00:00
|
|
|
|
|
|
|
|
2014-08-17 17:06:25 +00:00
|
|
|
@skipUnlessDBFeature("gis_enabled")
|
2014-03-14 22:18:20 +00:00
|
|
|
class MigrateTests(TransactionTestCase):
|
|
|
|
"""
|
|
|
|
Tests running the migrate command in Geodjango.
|
|
|
|
"""
|
2015-02-10 15:07:44 +00:00
|
|
|
available_apps = ["gis_tests.gis_migrations"]
|
2014-03-14 22:18:20 +00:00
|
|
|
|
|
|
|
def get_table_description(self, table):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
return connection.introspection.get_table_description(cursor, table)
|
|
|
|
|
|
|
|
def assertTableExists(self, table):
|
|
|
|
with connection.cursor() as cursor:
|
2014-09-20 22:29:13 +00:00
|
|
|
self.assertIn(table, connection.introspection.table_names(cursor))
|
2014-03-14 22:18:20 +00:00
|
|
|
|
|
|
|
def assertTableNotExists(self, table):
|
|
|
|
with connection.cursor() as cursor:
|
2014-09-20 22:29:13 +00:00
|
|
|
self.assertNotIn(table, connection.introspection.table_names(cursor))
|
2014-03-14 22:18:20 +00:00
|
|
|
|
|
|
|
def test_migrate_gis(self):
|
|
|
|
"""
|
|
|
|
Tests basic usage of the migrate command when a model uses Geodjango
|
|
|
|
fields. Regression test for ticket #22001:
|
|
|
|
https://code.djangoproject.com/ticket/22001
|
2014-07-16 09:06:33 +00:00
|
|
|
|
|
|
|
It's also used to showcase an error in migrations where spatialite is
|
|
|
|
enabled and geo tables are renamed resulting in unique constraint
|
|
|
|
failure on geometry_columns. Regression for ticket #23030:
|
|
|
|
https://code.djangoproject.com/ticket/23030
|
2014-03-14 22:18:20 +00:00
|
|
|
"""
|
|
|
|
# Make sure the right tables exist
|
2014-10-11 14:30:45 +00:00
|
|
|
self.assertTableExists("gis_migrations_neighborhood")
|
|
|
|
self.assertTableExists("gis_migrations_household")
|
|
|
|
self.assertTableExists("gis_migrations_family")
|
2014-03-14 22:18:20 +00:00
|
|
|
# Unmigrate everything
|
2014-10-11 14:30:45 +00:00
|
|
|
call_command("migrate", "gis_migrations", "zero", verbosity=0)
|
2014-03-14 22:18:20 +00:00
|
|
|
# Make sure it's all gone
|
2014-10-11 14:30:45 +00:00
|
|
|
self.assertTableNotExists("gis_migrations_neighborhood")
|
|
|
|
self.assertTableNotExists("gis_migrations_household")
|
|
|
|
self.assertTableNotExists("gis_migrations_family")
|
2014-07-16 09:06:33 +00:00
|
|
|
# Even geometry columns metadata
|
|
|
|
try:
|
|
|
|
GeoColumn = connection.ops.geometry_columns()
|
|
|
|
except NotImplementedError:
|
|
|
|
# Not all GIS backends have geometry columns model
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertEqual(
|
|
|
|
GeoColumn.objects.filter(
|
|
|
|
**{'%s__in' % GeoColumn.table_name_col(): ["gis_neighborhood", "gis_household"]}
|
|
|
|
).count(),
|
|
|
|
0)
|
2014-10-11 14:30:45 +00:00
|
|
|
# Revert the "unmigration"
|
|
|
|
call_command("migrate", "gis_migrations", verbosity=0)
|