|
29 | 29 | from django.core.cache import cache |
30 | 30 | from django.db import connection, connections |
31 | 31 | from django.db.migrations.executor import MigrationExecutor |
| 32 | +from django.db.migrations.state import ProjectState |
32 | 33 | from django.http import HttpRequest |
33 | 34 | from django.test import RequestFactory |
34 | 35 | from django.test import TestCase as DjangoTestCase |
@@ -2672,6 +2673,20 @@ def create_user_member_role(self): |
2672 | 2673 | self.login_as(self.user) |
2673 | 2674 |
|
2674 | 2675 |
|
| 2676 | +_project_state_cache: dict[str, ProjectState] = {} |
| 2677 | + |
| 2678 | + |
| 2679 | +def get_project_state(connection: str) -> ProjectState: |
| 2680 | + global _project_state_cache |
| 2681 | + |
| 2682 | + if connection not in _project_state_cache: |
| 2683 | + executor = MigrationExecutor(connections[connection]) |
| 2684 | + _project_state_cache[connection] = executor._create_project_state( |
| 2685 | + with_applied_migrations=True |
| 2686 | + ) |
| 2687 | + return _project_state_cache[connection].clone() |
| 2688 | + |
| 2689 | + |
2675 | 2690 | @pytest.mark.migrations |
2676 | 2691 | class TestMigrations(TransactionTestCase): |
2677 | 2692 | """ |
@@ -2712,22 +2727,22 @@ def setUp(self): |
2712 | 2727 | old_apps = executor.loader.project_state(migrate_from).apps |
2713 | 2728 |
|
2714 | 2729 | # Reverse to the original migration |
2715 | | - executor.migrate(migrate_from) |
| 2730 | + executor.migrate(migrate_from, state=get_project_state(self.connection)) |
2716 | 2731 |
|
2717 | 2732 | self.setup_before_migration(old_apps) |
2718 | 2733 |
|
2719 | 2734 | # Run the migration to test |
2720 | 2735 | executor = MigrationExecutor(connection) |
2721 | 2736 | executor.loader.build_graph() # reload. |
2722 | | - executor.migrate(migrate_to) |
| 2737 | + executor.migrate(migrate_to, state=get_project_state(self.connection)) |
2723 | 2738 |
|
2724 | 2739 | self.apps = executor.loader.project_state(migrate_to).apps |
2725 | 2740 |
|
2726 | 2741 | def tearDown(self): |
2727 | 2742 | super().tearDown() |
2728 | 2743 | executor = MigrationExecutor(connection) |
2729 | 2744 | executor.loader.build_graph() # reload. |
2730 | | - executor.migrate(self.current_migration) |
| 2745 | + executor.migrate(self.current_migration, state=get_project_state(self.connection)) |
2731 | 2746 |
|
2732 | 2747 | def setup_initial_state(self): |
2733 | 2748 | # Add code here that will run before we roll back the database to the `migrate_from` |
|
0 commit comments