diff --git a/tests/django_pg_migration_tools/test_operations.py b/tests/django_pg_migration_tools/test_operations.py index b612783..b68d174 100644 --- a/tests/django_pg_migration_tools/test_operations.py +++ b/tests/django_pg_migration_tools/test_operations.py @@ -237,7 +237,7 @@ def test_add(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert reverse_queries[0]["sql"] == "SHOW lock_timeout;" assert reverse_queries[1]["sql"] == "SET lock_timeout = '0';" @@ -548,7 +548,7 @@ def test_requires_atomic_false(self): with pytest.raises(NotSupportedError): with connection.schema_editor(atomic=True) as editor: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # Disable the overall test transaction because a unique concurrent index @@ -617,6 +617,7 @@ def test_operation_is_idempotent(self): == "unique_int_field" ) + operation.state_forwards(self.app_label, new_state) # Proceed to add the unique index followed by the constraint: with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: @@ -699,7 +700,7 @@ def test_operation_is_idempotent(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # 1. Check that the constraint is still there. @@ -731,7 +732,7 @@ def test_operation_is_idempotent(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1 @@ -777,6 +778,7 @@ def test_basic_usage(self): name="unique_int_field", ), ) + operation.state_forwards(self.app_label, new_state) # Proceed to add the unique index followed by the constraint: with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: @@ -844,7 +846,7 @@ def test_basic_usage(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # 1. Check that the constraint is still there. @@ -938,7 +940,7 @@ def test_when_not_allowed_to_migrate_by_the_router(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # No queries have run, because the migration wasn't allowed to run by @@ -1060,6 +1062,7 @@ def test_when_condition_on_constraint_only_creates_index(self): condition=Q(int_field__gte=2), ), ) + operation.state_forwards(self.app_label, new_state) # Proceed to add the unique index followed by the constraint: with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: @@ -1113,7 +1116,7 @@ def test_when_condition_on_constraint_only_creates_index(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # 2. perform the ALTER TABLE. @@ -1183,7 +1186,7 @@ def test_requires_atomic_false(self): with pytest.raises(NotSupportedError): with connection.schema_editor(atomic=True) as editor: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) @pytest.mark.django_db(transaction=True) @@ -1224,6 +1227,7 @@ def test_operation(self): == 0 ) + operation.state_forwards(self.app_label, new_state) # Proceed to remove the constraint. with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: @@ -1335,7 +1339,6 @@ def test_operation_where_condition_on_unique_constraint(self): ) operation.state_forwards(self.app_label, new_state) - # Proceed to remove the constraint. with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: @@ -1440,7 +1443,7 @@ def test_when_not_allowed_to_migrate_by_the_router(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # No queries have run, because the migration wasn't allowed to run by @@ -1558,7 +1561,7 @@ def test_when_not_allowed_to_migrate_by_the_router(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # No queries have run, because the migration wasn't allowed to run by @@ -1581,6 +1584,7 @@ def test_operation(self): "django_pg_migration_tools SaferAlterFieldSetNotNull operation." ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -1623,7 +1627,7 @@ def test_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -1646,7 +1650,7 @@ def test_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1 @@ -1905,6 +1909,7 @@ class TestSaferSaferAddFieldForeignKey: def test_requires_atomic_false(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -1921,6 +1926,7 @@ def test_requires_atomic_false(self): def test_when_not_null(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -1940,6 +1946,7 @@ def test_when_not_null(self): def test_when_not_allowed_to_migrate_by_the_router(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -1960,7 +1967,7 @@ def test_when_not_allowed_to_migrate_by_the_router(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # No queries have run, because the migration wasn't allowed to run by @@ -1976,6 +1983,7 @@ def test_operation(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -1988,6 +1996,7 @@ def test_operation(self): "django_pg_migration_tools SaferAddFieldForeignKey operation." ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2038,7 +2047,7 @@ def test_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2059,7 +2068,7 @@ def test_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1 @@ -2075,6 +2084,7 @@ def test_operation(self): def test_when_collecting_only(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -2127,6 +2137,7 @@ def test_operation_when_column_already_exists(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -2134,6 +2145,7 @@ def test_operation_when_column_already_exists(self): field=models.ForeignKey(CharModel, null=True, on_delete=models.CASCADE), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2188,7 +2200,7 @@ def test_operation_when_column_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2219,6 +2231,7 @@ def test_operation_when_column_index_already_exists(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -2226,6 +2239,7 @@ def test_operation_when_column_index_already_exists(self): field=models.ForeignKey(CharModel, null=True, on_delete=models.CASCADE), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2269,7 +2283,7 @@ def test_operation_when_column_index_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2308,6 +2322,7 @@ def test_operation_when_invalid_constraint_already_exists(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -2315,6 +2330,7 @@ def test_operation_when_invalid_constraint_already_exists(self): field=models.ForeignKey(CharModel, null=True, on_delete=models.CASCADE), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2358,7 +2374,7 @@ def test_operation_when_invalid_constraint_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2396,6 +2412,7 @@ def test_operation_when_valid_constraint_already_exists(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -2403,6 +2420,7 @@ def test_operation_when_valid_constraint_already_exists(self): field=models.ForeignKey(CharModel, null=True, on_delete=models.CASCADE), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2442,7 +2460,7 @@ def test_operation_when_valid_constraint_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2462,6 +2480,7 @@ def test_operation_when_valid_constraint_already_exists(self): def test_operation_when_db_index_is_false(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -2471,6 +2490,7 @@ def test_operation_when_db_index_is_false(self): ), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2505,7 +2525,7 @@ def test_operation_when_db_index_is_false(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2526,7 +2546,7 @@ def test_operation_when_db_index_is_false(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1 @@ -2547,6 +2567,7 @@ def test_operation_when_related_model_does_not_use_int_id(self): project_state = ProjectState() project_state.add_model(ModelState.from_model(IntModel)) + project_state.add_model(ModelState.from_model(CharIDModel)) new_state = project_state.clone() operation = operations.SaferAddFieldForeignKey( model_name="intmodel", @@ -2554,6 +2575,7 @@ def test_operation_when_related_model_does_not_use_int_id(self): field=models.ForeignKey(CharIDModel, null=True, on_delete=models.CASCADE), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2604,7 +2626,7 @@ def test_operation_when_related_model_does_not_use_int_id(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2625,7 +2647,7 @@ def test_operation_when_related_model_does_not_use_int_id(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1 @@ -2653,6 +2675,7 @@ def test_operation_when_referred_model_is_defined_as_str(self): db_index=False, ), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -2687,7 +2710,7 @@ def test_operation_when_referred_model_is_defined_as_str(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -2744,7 +2767,7 @@ def test_requires_atomic_false(self): with pytest.raises(NotSupportedError): with connection.schema_editor(atomic=True) as editor: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) @pytest.mark.django_db @@ -2791,7 +2814,7 @@ def test_when_not_allowed_to_migrate_by_the_router(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # No queries have run, because the migration wasn't allowed to run by @@ -2878,6 +2901,7 @@ def test_basic_operation(self): ) assert cursor.fetchone() + operation.state_forwards(self.app_label, new_state) # Trying to run the operation again does nothing because the valid # constraint already exists. Only introspection queries are performed. with connection.schema_editor(atomic=False, collect_sql=False) as editor: @@ -2906,7 +2930,7 @@ def test_basic_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # 1. Check that the constraint is still there. @@ -2938,7 +2962,7 @@ def test_basic_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1 @@ -2970,6 +2994,7 @@ def test_when_not_valid_constraint_exists(self): ), ) + operation.state_forwards(self.app_label, new_state) with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_forwards( @@ -3004,7 +3029,7 @@ def test_when_not_valid_constraint_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # 1. Check that the constraint is still there. @@ -3134,7 +3159,7 @@ def test_when_not_allowed_to_migrate_by_the_router(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) # No queries have run, because the migration wasn't allowed to run by @@ -3240,7 +3265,7 @@ def test_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -3261,7 +3286,7 @@ def test_operation(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1 @@ -3404,7 +3429,7 @@ def test_operation_when_column_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -3488,7 +3513,7 @@ def test_operation_when_unique_constraint_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -3580,7 +3605,7 @@ def test_operation_when_invalid_fk_constraint_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -3667,7 +3692,7 @@ def test_operation_when_valid_fk_constraint_already_exists(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -3757,7 +3782,7 @@ def test_operation_when_related_model_does_not_use_int_id(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(reverse_queries) == 2 @@ -3778,7 +3803,7 @@ def test_operation_when_related_model_does_not_use_int_id(self): with connection.schema_editor(atomic=False, collect_sql=False) as editor: with utils.CaptureQueriesContext(connection) as second_reverse_queries: operation.database_backwards( - self.app_label, editor, project_state, new_state + self.app_label, editor, new_state, project_state ) assert len(second_reverse_queries) == 1