Fixed #34050 -- Replaced invalid chars in migration names with '_'.

Thanks to Bishal Gautam for the report and initial implementation.

Regression in fa58450a9a.

Co-Authored-By: Bishal Gautam <bisalgt@gmail.com>
This commit is contained in:
Adam Johnson 2022-09-26 09:51:10 +01:00 committed by Mariusz Felisiak
parent d938b3b257
commit cd03e8e2d6
2 changed files with 17 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import re
from django.db.migrations.utils import get_migration_name_timestamp
from django.db.transaction import atomic
@ -205,7 +207,7 @@ class Migration:
return "initial"
raw_fragments = [op.migration_name_fragment for op in self.operations]
fragments = [name for name in raw_fragments if name]
fragments = [re.sub(r"\W+", "_", name) for name in raw_fragments if name]
if not fragments or len(fragments) != len(self.operations):
return "auto_%s" % get_migration_name_timestamp()

View File

@ -5314,6 +5314,20 @@ class MigrationSuggestNameTests(SimpleTestCase):
migration = Migration("some_migration", "test_app")
self.assertIs(migration.suggest_name().startswith("auto_"), True)
def test_operation_with_invalid_chars_in_suggested_name(self):
class Migration(migrations.Migration):
operations = [
migrations.AddConstraint(
"Person",
models.UniqueConstraint(
fields=["name"], name="person.name-*~unique!"
),
),
]
migration = Migration("some_migration", "test_app")
self.assertEqual(migration.suggest_name(), "person_person_name_unique_")
def test_none_name(self):
class Migration(migrations.Migration):
operations = [migrations.RunSQL("SELECT 1 FROM person;")]