2013-09-01 23:42:43 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-10-28 01:51:40 +08:00
|
|
|
import codecs
|
2013-09-01 23:42:43 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2013-12-22 18:35:17 +08:00
|
|
|
from django.apps import app_cache
|
2013-12-05 00:01:31 +08:00
|
|
|
from django.core.management import call_command, CommandError
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import override_settings
|
2013-09-01 23:42:43 +08:00
|
|
|
from django.utils import six
|
|
|
|
from django.utils._os import upath
|
|
|
|
from django.utils.encoding import force_text
|
|
|
|
|
2013-10-17 23:32:39 +08:00
|
|
|
from .models import UnicodeModel, UnserializableModel
|
2013-07-25 20:52:35 +08:00
|
|
|
from .test_base import MigrationTestBase
|
|
|
|
|
|
|
|
|
2013-09-01 23:42:43 +08:00
|
|
|
class MigrateTests(MigrationTestBase):
|
2013-07-25 20:52:35 +08:00
|
|
|
"""
|
2013-09-01 23:42:43 +08:00
|
|
|
Tests running the migrate command.
|
2013-07-25 20:52:35 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
|
|
|
def test_migrate(self):
|
|
|
|
"""
|
|
|
|
Tests basic usage of the migrate command.
|
|
|
|
"""
|
|
|
|
# Make sure no tables are created
|
|
|
|
self.assertTableNotExists("migrations_author")
|
|
|
|
self.assertTableNotExists("migrations_tribble")
|
|
|
|
self.assertTableNotExists("migrations_book")
|
|
|
|
# Run the migrations to 0001 only
|
|
|
|
call_command("migrate", "migrations", "0001", verbosity=0)
|
|
|
|
# Make sure the right tables exist
|
|
|
|
self.assertTableExists("migrations_author")
|
|
|
|
self.assertTableExists("migrations_tribble")
|
|
|
|
self.assertTableNotExists("migrations_book")
|
|
|
|
# Run migrations all the way
|
|
|
|
call_command("migrate", verbosity=0)
|
|
|
|
# Make sure the right tables exist
|
|
|
|
self.assertTableExists("migrations_author")
|
|
|
|
self.assertTableNotExists("migrations_tribble")
|
|
|
|
self.assertTableExists("migrations_book")
|
|
|
|
# Unmigrate everything
|
|
|
|
call_command("migrate", "migrations", "zero", verbosity=0)
|
|
|
|
# Make sure it's all gone
|
|
|
|
self.assertTableNotExists("migrations_author")
|
|
|
|
self.assertTableNotExists("migrations_tribble")
|
|
|
|
self.assertTableNotExists("migrations_book")
|
2013-09-01 23:42:43 +08:00
|
|
|
|
2013-09-07 04:27:51 +08:00
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
2013-09-17 17:54:55 +08:00
|
|
|
def test_migrate_list(self):
|
|
|
|
"""
|
|
|
|
Tests --list output of migrate command
|
|
|
|
"""
|
|
|
|
stdout = six.StringIO()
|
|
|
|
call_command("migrate", list=True, stdout=stdout, verbosity=0)
|
|
|
|
self.assertIn("migrations", stdout.getvalue().lower())
|
|
|
|
self.assertIn("[ ] 0001_initial", stdout.getvalue().lower())
|
|
|
|
self.assertIn("[ ] 0002_second", stdout.getvalue().lower())
|
|
|
|
|
|
|
|
call_command("migrate", "migrations", "0001", verbosity=0)
|
|
|
|
|
|
|
|
stdout = six.StringIO()
|
|
|
|
# Giving the explicit app_label tests for selective `show_migration_list` in the command
|
|
|
|
call_command("migrate", "migrations", list=True, stdout=stdout, verbosity=0)
|
|
|
|
self.assertIn("migrations", stdout.getvalue().lower())
|
|
|
|
self.assertIn("[x] 0001_initial", stdout.getvalue().lower())
|
|
|
|
self.assertIn("[ ] 0002_second", stdout.getvalue().lower())
|
|
|
|
# Cleanup by unmigrating everything
|
|
|
|
call_command("migrate", "migrations", "zero", verbosity=0)
|
|
|
|
|
2013-12-05 00:01:31 +08:00
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
|
|
|
|
def test_migrate_conflict_exit(self):
|
|
|
|
"""
|
|
|
|
Makes sure that migrate exits if it detects a conflict.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(CommandError):
|
|
|
|
call_command("migrate", "migrations")
|
|
|
|
|
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
|
|
|
|
def test_makemigrations_conflict_exit(self):
|
|
|
|
"""
|
|
|
|
Makes sure that makemigrations exits if it detects a conflict.
|
|
|
|
"""
|
|
|
|
with self.assertRaises(CommandError):
|
|
|
|
call_command("makemigrations")
|
|
|
|
|
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
|
|
|
|
def test_makemigrations_merge_basic(self):
|
|
|
|
"""
|
|
|
|
Makes sure that makemigrations doesn't error if you ask for
|
|
|
|
merge mode with a conflict present. Doesn't test writing of the merge
|
|
|
|
file, as that requires temp directories.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
call_command("makemigrations", merge=True, verbosity=0)
|
|
|
|
except CommandError:
|
|
|
|
self.fail("Makemigrations errored in merge mode with conflicts")
|
|
|
|
|
2013-09-17 17:54:55 +08:00
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
2013-09-07 04:27:51 +08:00
|
|
|
def test_sqlmigrate(self):
|
|
|
|
"""
|
|
|
|
Makes sure that sqlmigrate does something.
|
|
|
|
"""
|
|
|
|
# Test forwards. All the databases agree on CREATE TABLE, at least.
|
|
|
|
stdout = six.StringIO()
|
|
|
|
call_command("sqlmigrate", "migrations", "0001", stdout=stdout)
|
|
|
|
self.assertIn("create table", stdout.getvalue().lower())
|
|
|
|
# And backwards is a DROP TABLE
|
|
|
|
stdout = six.StringIO()
|
|
|
|
call_command("sqlmigrate", "migrations", "0001", stdout=stdout, backwards=True)
|
|
|
|
self.assertIn("drop table", stdout.getvalue().lower())
|
|
|
|
|
2013-09-01 23:42:43 +08:00
|
|
|
|
|
|
|
class MakeMigrationsTests(MigrationTestBase):
|
|
|
|
"""
|
|
|
|
Tests running the makemigrations command.
|
|
|
|
"""
|
|
|
|
|
2013-10-17 23:32:39 +08:00
|
|
|
# Because the `import_module` performed in `MigrationLoader` will cache
|
|
|
|
# the migrations package, we can't reuse the same migration package
|
|
|
|
# between tests. This is only a problem for testing, since `makemigrations`
|
|
|
|
# is normally called in its own process.
|
|
|
|
creation_counter = 0
|
|
|
|
|
2013-09-01 23:42:43 +08:00
|
|
|
def setUp(self):
|
2013-10-17 23:32:39 +08:00
|
|
|
MakeMigrationsTests.creation_counter += 1
|
2013-09-01 23:42:43 +08:00
|
|
|
self._cwd = os.getcwd()
|
|
|
|
self.test_dir = os.path.abspath(os.path.dirname(upath(__file__)))
|
2013-10-17 23:32:39 +08:00
|
|
|
self.migration_dir = os.path.join(self.test_dir, 'migrations_%d' % self.creation_counter)
|
|
|
|
self.migration_pkg = "migrations.migrations_%d" % self.creation_counter
|
2013-12-13 04:34:39 +08:00
|
|
|
self._old_models = app_cache.app_configs['migrations'].models.copy()
|
2013-09-01 23:42:43 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-12-13 04:34:39 +08:00
|
|
|
app_cache.app_configs['migrations'].models = self._old_models
|
2013-12-18 18:19:56 +08:00
|
|
|
app_cache.all_models['migrations'] = self._old_models
|
2013-12-24 17:08:04 +08:00
|
|
|
app_cache.get_models.cache_clear()
|
2013-10-17 23:32:39 +08:00
|
|
|
|
2013-09-01 23:42:43 +08:00
|
|
|
os.chdir(self.test_dir)
|
|
|
|
try:
|
|
|
|
self._rmrf(self.migration_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
os.chdir(self._cwd)
|
|
|
|
|
|
|
|
def _rmrf(self, dname):
|
|
|
|
if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
|
|
|
|
return
|
|
|
|
shutil.rmtree(dname)
|
|
|
|
|
|
|
|
def test_files_content(self):
|
|
|
|
self.assertTableNotExists("migrations_unicodemodel")
|
2013-12-14 04:29:30 +08:00
|
|
|
app_cache.register_model('migrations', UnicodeModel)
|
2013-10-17 23:32:39 +08:00
|
|
|
with override_settings(MIGRATION_MODULES={"migrations": self.migration_pkg}):
|
|
|
|
call_command("makemigrations", "migrations", verbosity=0)
|
2013-09-01 23:42:43 +08:00
|
|
|
|
|
|
|
init_file = os.path.join(self.migration_dir, "__init__.py")
|
|
|
|
|
|
|
|
# Check for existing __init__.py file in migrations folder
|
|
|
|
self.assertTrue(os.path.exists(init_file))
|
|
|
|
|
|
|
|
with open(init_file, 'r') as fp:
|
|
|
|
content = force_text(fp.read())
|
|
|
|
self.assertEqual(content, '')
|
|
|
|
|
|
|
|
initial_file = os.path.join(self.migration_dir, "0001_initial.py")
|
|
|
|
|
|
|
|
# Check for existing 0001_initial.py file in migration folder
|
|
|
|
self.assertTrue(os.path.exists(initial_file))
|
|
|
|
|
2013-10-28 01:51:40 +08:00
|
|
|
with codecs.open(initial_file, 'r', encoding='utf-8') as fp:
|
|
|
|
content = fp.read()
|
2013-09-01 23:42:43 +08:00
|
|
|
self.assertTrue('# encoding: utf8' in content)
|
|
|
|
self.assertTrue('migrations.CreateModel' in content)
|
|
|
|
|
|
|
|
if six.PY3:
|
|
|
|
self.assertTrue('úñí©óðé µóðéø' in content) # Meta.verbose_name
|
|
|
|
self.assertTrue('úñí©óðé µóðéøß' in content) # Meta.verbose_name_plural
|
|
|
|
self.assertTrue('ÚÑÍ¢ÓÐÉ' in content) # title.verbose_name
|
|
|
|
self.assertTrue('“Ðjáñgó”' in content) # title.default
|
|
|
|
else:
|
|
|
|
self.assertTrue('\\xfa\\xf1\\xed\\xa9\\xf3\\xf0\\xe9 \\xb5\\xf3\\xf0\\xe9\\xf8' in content) # Meta.verbose_name
|
|
|
|
self.assertTrue('\\xfa\\xf1\\xed\\xa9\\xf3\\xf0\\xe9 \\xb5\\xf3\\xf0\\xe9\\xf8\\xdf' in content) # Meta.verbose_name_plural
|
|
|
|
self.assertTrue('\\xda\\xd1\\xcd\\xa2\\xd3\\xd0\\xc9' in content) # title.verbose_name
|
|
|
|
self.assertTrue('\\u201c\\xd0j\\xe1\\xf1g\\xf3\\u201d' in content) # title.default
|
2013-10-17 23:32:39 +08:00
|
|
|
|
|
|
|
def test_failing_migration(self):
|
|
|
|
#21280 - If a migration fails to serialize, it shouldn't generate an empty file.
|
2013-12-14 04:29:30 +08:00
|
|
|
app_cache.register_model('migrations', UnserializableModel)
|
2013-10-17 23:32:39 +08:00
|
|
|
|
|
|
|
with six.assertRaisesRegex(self, ValueError, r'Cannot serialize'):
|
|
|
|
with override_settings(MIGRATION_MODULES={"migrations": self.migration_pkg}):
|
|
|
|
call_command("makemigrations", "migrations", verbosity=0)
|
|
|
|
|
|
|
|
initial_file = os.path.join(self.migration_dir, "0001_initial.py")
|
|
|
|
self.assertFalse(os.path.exists(initial_file))
|