mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[1.7.x] Fixed #24037 -- Prevented data loss possibility when changing Meta.managed.
The migrations autodetector now issues AlterModelOptions operations for
Meta.managed changes instead of DeleteModel + CreateModel.
Thanks iambibhas for the report and Simon and Markus for review.
Backport of 061caa5b38 from master
			
			
This commit is contained in:
		| @@ -1021,7 +1021,7 @@ class AutodetectorTests(TestCase): | ||||
|         # The field name the FK on the book model points to | ||||
|         self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'pk_field') | ||||
|  | ||||
|     def test_unmanaged(self): | ||||
|     def test_unmanaged_create(self): | ||||
|         """Tests that the autodetector correctly deals with managed models.""" | ||||
|         # First, we test adding an unmanaged model | ||||
|         before = self.make_project_state([self.author_empty]) | ||||
| @@ -1031,9 +1031,10 @@ class AutodetectorTests(TestCase): | ||||
|         # Right number/type of migrations? | ||||
|         self.assertNumberMigrations(changes, 'testapp', 1) | ||||
|         self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel"]) | ||||
|         self.assertOperationAttributes(changes, 'testapp', 0, 0, name="AuthorUnmanaged") | ||||
|         self.assertEqual(changes['testapp'][0].operations[0].options['managed'], False) | ||||
|         self.assertOperationAttributes(changes, 'testapp', 0, 0, | ||||
|             name="AuthorUnmanaged", options={"managed": False}) | ||||
|  | ||||
|     def test_unmanaged_to_managed(self): | ||||
|         # Now, we test turning an unmanaged model into a managed model | ||||
|         before = self.make_project_state([self.author_empty, self.author_unmanaged]) | ||||
|         after = self.make_project_state([self.author_empty, self.author_unmanaged_managed]) | ||||
| @@ -1041,9 +1042,21 @@ class AutodetectorTests(TestCase): | ||||
|         changes = autodetector._detect_changes() | ||||
|         # Right number/type of migrations? | ||||
|         self.assertNumberMigrations(changes, 'testapp', 1) | ||||
|         self.assertOperationTypes(changes, 'testapp', 0, ["DeleteModel", "CreateModel"]) | ||||
|         self.assertOperationAttributes(changes, 'testapp', 0, 0, name="AuthorUnmanaged") | ||||
|         self.assertOperationAttributes(changes, 'testapp', 0, 1, name="AuthorUnmanaged") | ||||
|         self.assertOperationTypes(changes, 'testapp', 0, ["AlterModelOptions"]) | ||||
|         self.assertOperationAttributes(changes, 'testapp', 0, 0, | ||||
|             name="authorunmanaged", options={}) | ||||
|  | ||||
|     def test_managed_to_unmanaged(self): | ||||
|         # Now, we turn managed to unmanaged. | ||||
|         before = self.make_project_state([self.author_empty, self.author_unmanaged_managed]) | ||||
|         after = self.make_project_state([self.author_empty, self.author_unmanaged]) | ||||
|         autodetector = MigrationAutodetector(before, after) | ||||
|         changes = autodetector._detect_changes() | ||||
|         # Right number/type of migrations? | ||||
|         self.assertNumberMigrations(changes, 'testapp', 1) | ||||
|         self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"]) | ||||
|         self.assertOperationAttributes(changes, "testapp", 0, 0, | ||||
|             name="authorunmanaged", options={"managed": False}) | ||||
|  | ||||
|     def test_unmanaged_custom_pk(self): | ||||
|         """ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user