mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #24870 -- Added --update option to makemigrations command.
This commit is contained in:
committed by
Mariusz Felisiak
parent
3893fcdd94
commit
e286ce17ff
@@ -2584,6 +2584,108 @@ class MakeMigrationsTests(MigrationTestBase):
|
||||
out_value = out.getvalue()
|
||||
self.assertIn("0003_auto", out_value)
|
||||
|
||||
def test_makemigrations_update(self):
|
||||
with self.temporary_migration_module(
|
||||
module="migrations.test_migrations"
|
||||
) as migration_dir:
|
||||
migration_file = os.path.join(migration_dir, "0002_second.py")
|
||||
with open(migration_file) as fp:
|
||||
initial_content = fp.read()
|
||||
|
||||
with captured_stdout() as out:
|
||||
call_command("makemigrations", "migrations", update=True)
|
||||
self.assertFalse(
|
||||
any(
|
||||
filename.startswith("0003")
|
||||
for filename in os.listdir(migration_dir)
|
||||
)
|
||||
)
|
||||
self.assertIs(os.path.exists(migration_file), False)
|
||||
new_migration_file = os.path.join(
|
||||
migration_dir,
|
||||
"0002_delete_tribble_author_rating_modelwithcustombase_and_more.py",
|
||||
)
|
||||
with open(new_migration_file) as fp:
|
||||
self.assertNotEqual(initial_content, fp.read())
|
||||
self.assertIn(f"Deleted {migration_file}", out.getvalue())
|
||||
|
||||
def test_makemigrations_update_existing_name(self):
|
||||
with self.temporary_migration_module(
|
||||
module="migrations.test_auto_now_add"
|
||||
) as migration_dir:
|
||||
migration_file = os.path.join(migration_dir, "0001_initial.py")
|
||||
with open(migration_file) as fp:
|
||||
initial_content = fp.read()
|
||||
|
||||
with captured_stdout() as out:
|
||||
call_command("makemigrations", "migrations", update=True)
|
||||
self.assertIs(os.path.exists(migration_file), False)
|
||||
new_migration_file = os.path.join(
|
||||
migration_dir,
|
||||
"0001_initial_updated.py",
|
||||
)
|
||||
with open(new_migration_file) as fp:
|
||||
self.assertNotEqual(initial_content, fp.read())
|
||||
self.assertIn(f"Deleted {migration_file}", out.getvalue())
|
||||
|
||||
def test_makemigrations_update_applied_migration(self):
|
||||
recorder = MigrationRecorder(connection)
|
||||
recorder.record_applied("migrations", "0001_initial")
|
||||
recorder.record_applied("migrations", "0002_second")
|
||||
with self.temporary_migration_module(module="migrations.test_migrations"):
|
||||
msg = "Cannot update applied migration 'migrations.0002_second'."
|
||||
with self.assertRaisesMessage(CommandError, msg):
|
||||
call_command("makemigrations", "migrations", update=True)
|
||||
|
||||
def test_makemigrations_update_no_migration(self):
|
||||
with self.temporary_migration_module(module="migrations.test_migrations_empty"):
|
||||
msg = "App migrations has no migration, cannot update last migration."
|
||||
with self.assertRaisesMessage(CommandError, msg):
|
||||
call_command("makemigrations", "migrations", update=True)
|
||||
|
||||
def test_makemigrations_update_squash_migration(self):
|
||||
with self.temporary_migration_module(
|
||||
module="migrations.test_migrations_squashed"
|
||||
):
|
||||
msg = "Cannot update squash migration 'migrations.0001_squashed_0002'."
|
||||
with self.assertRaisesMessage(CommandError, msg):
|
||||
call_command("makemigrations", "migrations", update=True)
|
||||
|
||||
def test_makemigrations_update_manual_porting(self):
|
||||
with self.temporary_migration_module(
|
||||
module="migrations.test_migrations_plan"
|
||||
) as migration_dir:
|
||||
with captured_stdout() as out:
|
||||
call_command("makemigrations", "migrations", update=True)
|
||||
# Previous migration exists.
|
||||
previous_migration_file = os.path.join(migration_dir, "0005_fifth.py")
|
||||
self.assertIs(os.path.exists(previous_migration_file), True)
|
||||
# New updated migration exists.
|
||||
files = [f for f in os.listdir(migration_dir) if f.startswith("0005_auto")]
|
||||
updated_migration_file = os.path.join(migration_dir, files[0])
|
||||
self.assertIs(os.path.exists(updated_migration_file), True)
|
||||
self.assertIn(
|
||||
f"Updated migration {updated_migration_file} requires manual porting.\n"
|
||||
f"Previous migration {previous_migration_file} was kept and must be "
|
||||
f"deleted after porting functions manually.",
|
||||
out.getvalue(),
|
||||
)
|
||||
|
||||
@override_settings(
|
||||
INSTALLED_APPS=[
|
||||
"migrations.migrations_test_apps.alter_fk.author_app",
|
||||
"migrations.migrations_test_apps.alter_fk.book_app",
|
||||
]
|
||||
)
|
||||
def test_makemigrations_update_dependency_migration(self):
|
||||
with self.temporary_migration_module(app_label="book_app"):
|
||||
msg = (
|
||||
"Cannot update migration 'book_app.0001_initial' that migrations "
|
||||
"'author_app.0002_alter_id' depend on."
|
||||
)
|
||||
with self.assertRaisesMessage(CommandError, msg):
|
||||
call_command("makemigrations", "book_app", update=True)
|
||||
|
||||
|
||||
class SquashMigrationsTests(MigrationTestBase):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user