Opened 30 minutes ago
#37554 assigned Cleanup/optimization
Model rename questions in makemigrations depend on PYTHONHASHSEED
| Reported by: | Konstantinos Giannopoulos | Owned by: | Konstantinos Giannopoulos |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Normal | Keywords: | autodetector, RenameModel, deterministic |
| Cc: | Konstantinos Giannopoulos | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When several removed models have the same fields as an added model, makemigrations asks "Was the model app.X renamed to app.Y?" for each candidate in turn. The order of these questions is not stable: it depends on PYTHONHASHSEED, so the same code can ask about a different model first on every run. If the user answers "y", different runs produce different migrations.
Initial models (migrated):
class EmailLog(models.Model): recipient = models.CharField(max_length=200) body = models.TextField() sent_at = models.DateTimeField() class SmsLog(models.Model): recipient = models.CharField(max_length=200) body = models.TextField() sent_at = models.DateTimeField() class PushLog(models.Model): recipient = models.CharField(max_length=200) body = models.TextField() sent_at = models.DateTimeField()
Replace them with one model with the same fields:
class MessageLog(models.Model): recipient = models.CharField(max_length=200) body = models.TextField() sent_at = models.DateTimeField()
Running makemigrations --dry-run with different hash seeds and answering "y" to the first question:
PYTHONHASHSEED first question about migration 1 PushLog Rename model PushLog to MessageLog; Delete model EmailLog; Delete model SmsLog 2 SmsLog Rename model SmsLog to MessageLog; Delete model EmailLog; Delete model PushLog 5 EmailLog Rename model EmailLog to MessageLog; Delete model PushLog; Delete model SmsLog
MigrationAutodetector.generate_renamed_models() iterates over removed_models, a set of (app_label, model_name) tuples, without sorting it, so the order follows string hashing. Most other loops in the autodetector iterate over sorted(...).
Sorting the set makes the questions, and the resulting migration, deterministic:
- for rem_app_label, rem_model_name in removed_models: + for rem_app_label, rem_model_name in sorted(removed_models):
Regression test for tests/migrations/test_autodetector.py (it uses six candidate models, so it fails on the current code for almost every hash seed):
def test_rename_model_candidates_are_checked_in_sorted_order(self): """ When several removed models match an added model, the rename questions are asked in a stable order, not in the order of a set (which depends on PYTHONHASHSEED). """ asked = [] class RecordingQuestioner(MigrationQuestioner): def ask_rename_model(self, old_model_state, new_model_state): asked.append(old_model_state.name) return False def log_model(name): return ModelState( "app", name, [ ("id", models.AutoField(primary_key=True)), ("recipient", models.CharField(max_length=200)), ], ) removed = ["SmsLog", "EmailLog", "PushLog", "FaxLog", "ChatLog", "MailLog"] self.get_changes( [log_model(name) for name in removed], [log_model("MessageLog")], RecordingQuestioner(), ) self.assertEqual(asked, sorted(removed, key=str.lower))
A small project reproducing this is attached; its ask_seeds.py runs makemigrations --dry-run with eight different seeds and prints the results.
Happy to provide a fix myself.