1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #33246 -- Made squashmigrations raise CommandError when squashed_name already exists.

This commit is contained in:
andrewdotn 2021-11-02 00:13:42 -06:00 committed by GitHub
parent c3e0dfe4cc
commit 9e6d631697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -1,3 +1,5 @@
import os
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
@ -184,6 +186,10 @@ class Command(BaseCommand):
# Write out the new migration file # Write out the new migration file
writer = MigrationWriter(new_migration, include_header) writer = MigrationWriter(new_migration, include_header)
if os.path.exists(writer.path):
raise CommandError(
f'Migration {new_migration.name} already exists. Use a different name.'
)
with open(writer.path, "w", encoding='utf-8') as fh: with open(writer.path, "w", encoding='utf-8') as fh:
fh.write(writer.as_string()) fh.write(writer.as_string())

View File

@ -2082,6 +2082,15 @@ class SquashMigrationsTests(MigrationTestBase):
squashed_migration_file = os.path.join(migration_dir, '0001_%s.py' % squashed_name) squashed_migration_file = os.path.join(migration_dir, '0001_%s.py' % squashed_name)
self.assertTrue(os.path.exists(squashed_migration_file)) self.assertTrue(os.path.exists(squashed_migration_file))
def test_squashed_name_exists(self):
msg = 'Migration 0001_initial already exists. Use a different name.'
with self.temporary_migration_module(module='migrations.test_migrations'):
with self.assertRaisesMessage(CommandError, msg):
call_command(
'squashmigrations', 'migrations', '0001', '0002',
squashed_name='initial', interactive=False, verbosity=0,
)
class AppLabelErrorTests(TestCase): class AppLabelErrorTests(TestCase):
""" """