|
2 | 2 | import os
|
3 | 3 | from copy import deepcopy
|
4 | 4 | from importlib import import_module
|
| 5 | +from time import sleep |
5 | 6 |
|
6 | 7 | from django.db import connection, connections
|
7 | 8 | from django.db.migrations.exceptions import (
|
@@ -99,6 +100,28 @@ def assertMigrationTablesExists(self, conn):
|
99 | 100 | self.assertIn("distributed_django_migrations", tables)
|
100 | 101 | i -= 1
|
101 | 102 |
|
| 103 | + def assertMigrationExists(self, conn, name, app, deleted=False, tries=5): |
| 104 | + # simulate multiple attempts to read a migration from the distributed migration table |
| 105 | + while tries: |
| 106 | + with conn.cursor() as cursor: |
| 107 | + cursor.execute(f"SELECT * FROM distributed_django_migrations where name = '{name}'") |
| 108 | + res = cursor.fetchall() |
| 109 | + |
| 110 | + try: |
| 111 | + self.assertEqual(len(res), 1) |
| 112 | + except AssertionError: |
| 113 | + # handle replication lag |
| 114 | + if tries >= 1: |
| 115 | + sleep(1) |
| 116 | + tries -= 1 |
| 117 | + continue |
| 118 | + raise ValueError(f"Migration {name} for app {app} not found in distributed_django_migrations table") |
| 119 | + |
| 120 | + self.assertEqual(res[0][1], app) |
| 121 | + self.assertEqual(res[0][2], name) |
| 122 | + self.assertEqual(res[0][-1], deleted) |
| 123 | + tries -= 1 |
| 124 | + |
102 | 125 | def test_distributed_migration_schema_with_existing_migrations(self):
|
103 | 126 | """
|
104 | 127 | Tests that migration tables are created in all nodes even if the django_migrations table already exists
|
@@ -154,6 +177,37 @@ def test_distributed_migration_schema_without_migrations(self):
|
154 | 177 |
|
155 | 178 | self.assertMigrationTablesExists(recorder.connection)
|
156 | 179 |
|
| 180 | + def test_apply_unapply_distributed(self): |
| 181 | + """ |
| 182 | + Tests marking migrations as applied/unapplied in a distributed setup. |
| 183 | + """ |
| 184 | + databases = [x for x in self.databases if x != "s1r2"] |
| 185 | + |
| 186 | + for db in databases: |
| 187 | + conn = connections[db] |
| 188 | + recorder = MigrationRecorder(conn) |
| 189 | + recorder.flush() |
| 190 | + |
| 191 | + lb = DatabaseWrapper(deepcopy(self.lb), alias="load_balancer") |
| 192 | + connections["load_balancer"] = lb |
| 193 | + recorder = MigrationRecorder(lb) |
| 194 | + |
| 195 | + recorder.record_applied("myapp", "0432_ponies") |
| 196 | + |
| 197 | + for db in databases: |
| 198 | + conn = connections[db] |
| 199 | + self.assertMigrationExists(conn, "0432_ponies", "myapp", deleted=False) |
| 200 | + |
| 201 | + self.assertMigrationExists(connections['load_balancer'], "myapp", "0432_ponies") |
| 202 | + |
| 203 | + recorder.record_unapplied("myapp", "0432_ponies") |
| 204 | + |
| 205 | + for db in databases: |
| 206 | + conn = connections[db] |
| 207 | + self.assertMigrationExists(conn, "0432_ponies", "myapp", deleted=True) |
| 208 | + |
| 209 | + self.assertMigrationExists(connections['load_balancer'], "myapp", "0432_ponies", deleted=True) |
| 210 | + |
157 | 211 |
|
158 | 212 | class LoaderTests(TestCase):
|
159 | 213 | """
|
|
0 commit comments