| 1 |
from django.db import connection, migrations, models |
| 2 |
from django.db.migrations.state import ProjectState |
| 3 |
from django.test import override_settings |
| 4 |
|
| 5 |
from .test_base import OperationTestBase |
| 6 |
|
| 7 |
|
| 8 |
class AgnosticRouter: |
| 9 |
""" |
| 10 |
A router that doesn't have an opinion regarding migrating. |
| 11 |
""" |
| 12 |
|
| 13 |
def allow_migrate(self, db, app_label, **hints): |
| 14 |
return None |
| 15 |
|
| 16 |
|
| 17 |
class MigrateNothingRouter: |
| 18 |
""" |
| 19 |
A router that doesn't allow migrating. |
| 20 |
""" |
| 21 |
|
| 22 |
def allow_migrate(self, db, app_label, **hints): |
| 23 |
return False |
| 24 |
|
| 25 |
|
| 26 |
class MigrateEverythingRouter: |
| 27 |
""" |
| 28 |
A router that always allows migrating. |
| 29 |
""" |
| 30 |
|
| 31 |
def allow_migrate(self, db, app_label, **hints): |
| 32 |
return True |
| 33 |
|
| 34 |
|
| 35 |
class MigrateWhenFooRouter: |
| 36 |
""" |
| 37 |
A router that allows migrating depending on a hint. |
| 38 |
""" |
| 39 |
|
| 40 |
def allow_migrate(self, db, app_label, **hints): |
| 41 |
return hints.get("foo", False) |
| 42 |
|
| 43 |
|
| 44 |
class MultiDBOperationTests(OperationTestBase): |
| 45 |
databases = {"default", "other"} |
| 46 |
|
| 47 |
def _test_create_model(self, app_label, should_run): |
| 48 |
""" |
| 49 |
CreateModel honors multi-db settings. |
| 50 |
""" |
| 51 |
operation = migrations.CreateModel( |
| 52 |
"Pony", |
| 53 |
[("id", models.AutoField(primary_key=True))], |
| 54 |
) |
| 55 |
# Test the state alteration |
| 56 |
project_state = ProjectState() |
| 57 |
new_state = project_state.clone() |
| 58 |
operation.state_forwards(app_label, new_state) |
| 59 |
# Test the database alteration |
| 60 |
self.assertTableNotExists("%s_pony" % app_label) |
| 61 |
with connection.schema_editor() as editor: |
| 62 |
operation.database_forwards(app_label, editor, project_state, new_state) |
| 63 |
if should_run: |
| 64 |
self.assertTableExists("%s_pony" % app_label) |
| 65 |
else: |
| 66 |
self.assertTableNotExists("%s_pony" % app_label) |
| 67 |
# And test reversal |
| 68 |
with connection.schema_editor() as editor: |
| 69 |
operation.database_backwards(app_label, editor, new_state, project_state) |
| 70 |
self.assertTableNotExists("%s_pony" % app_label) |
| 71 |
|
| 72 |
@override_settings(DATABASE_ROUTERS=[AgnosticRouter()]) |
| 73 |
def test_create_model(self): |
| 74 |
""" |
| 75 |
Test when router doesn't have an opinion (i.e. CreateModel should run). |
| 76 |
""" |
| 77 |
self._test_create_model("test_mltdb_crmo", should_run=True) |
| 78 |
|
| 79 |
@override_settings(DATABASE_ROUTERS=[MigrateNothingRouter()]) |
| 80 |
def test_create_model2(self): |
| 81 |
""" |
| 82 |
Test when router returns False (i.e. CreateModel shouldn't run). |
| 83 |
""" |
| 84 |
self._test_create_model("test_mltdb_crmo2", should_run=False) |
| 85 |
|
| 86 |
@override_settings(DATABASE_ROUTERS=[MigrateEverythingRouter()]) |
| 87 |
def test_create_model3(self): |
| 88 |
""" |
| 89 |
Test when router returns True (i.e. CreateModel should run). |
| 90 |
""" |
| 91 |
self._test_create_model("test_mltdb_crmo3", should_run=True) |
| 92 |
|
| 93 |
def test_create_model4(self): |
| 94 |
""" |
| 95 |
Test multiple routers. |
| 96 |
""" |
| 97 |
with override_settings(DATABASE_ROUTERS=[AgnosticRouter(), AgnosticRouter()]): |
| 98 |
self._test_create_model("test_mltdb_crmo4", should_run=True) |
| 99 |
with override_settings( |
| 100 |
DATABASE_ROUTERS=[MigrateNothingRouter(), MigrateEverythingRouter()] |
| 101 |
): |
| 102 |
self._test_create_model("test_mltdb_crmo4", should_run=False) |
| 103 |
with override_settings( |
| 104 |
DATABASE_ROUTERS=[MigrateEverythingRouter(), MigrateNothingRouter()] |
| 105 |
): |
| 106 |
self._test_create_model("test_mltdb_crmo4", should_run=True) |
| 107 |
|
| 108 |
def _test_run_sql(self, app_label, should_run, hints=None): |
| 109 |
with override_settings(DATABASE_ROUTERS=[MigrateEverythingRouter()]): |
| 110 |
project_state = self.set_up_test_model(app_label) |
| 111 |
|
| 112 |
sql = """ |
| 113 |
INSERT INTO {0}_pony (pink, weight) VALUES (1, 3.55); |
| 114 |
INSERT INTO {0}_pony (pink, weight) VALUES (3, 5.0); |
| 115 |
""".format(app_label) |
| 116 |
|
| 117 |
operation = migrations.RunSQL(sql, hints=hints or {}) |
| 118 |
# Test the state alteration does nothing |
| 119 |
new_state = project_state.clone() |
| 120 |
operation.state_forwards(app_label, new_state) |
| 121 |
self.assertEqual(new_state, project_state) |
| 122 |
# Test the database alteration |
| 123 |
self.assertEqual( |
| 124 |
project_state.apps.get_model(app_label, "Pony").objects.count(), 0 |
| 125 |
) |
| 126 |
with connection.schema_editor() as editor: |
| 127 |
operation.database_forwards(app_label, editor, project_state, new_state) |
| 128 |
Pony = project_state.apps.get_model(app_label, "Pony") |
| 129 |
if should_run: |
| 130 |
self.assertEqual(Pony.objects.count(), 2) |
| 131 |
else: |
| 132 |
self.assertEqual(Pony.objects.count(), 0) |
| 133 |
|
| 134 |
@override_settings(DATABASE_ROUTERS=[MigrateNothingRouter()]) |
| 135 |
def test_run_sql_migrate_nothing_router(self): |
| 136 |
self._test_run_sql("test_mltdb_runsql", should_run=False) |
| 137 |
|
| 138 |
@override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) |
| 139 |
def test_run_sql_migrate_foo_router_without_hints(self): |
| 140 |
self._test_run_sql("test_mltdb_runsql2", should_run=False) |
| 141 |
|
| 142 |
@override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) |
| 143 |
def test_run_sql_migrate_foo_router_with_hints(self): |
| 144 |
self._test_run_sql("test_mltdb_runsql3", should_run=True, hints={"foo": True}) |
| 145 |
|
| 146 |
def _test_run_python(self, app_label, should_run, hints=None): |
| 147 |
with override_settings(DATABASE_ROUTERS=[MigrateEverythingRouter()]): |
| 148 |
project_state = self.set_up_test_model(app_label) |
| 149 |
|
| 150 |
# Create the operation |
| 151 |
def inner_method(models, schema_editor): |
| 152 |
Pony = models.get_model(app_label, "Pony") |
| 153 |
Pony.objects.create(pink=1, weight=3.55) |
| 154 |
Pony.objects.create(weight=5) |
| 155 |
|
| 156 |
operation = migrations.RunPython(inner_method, hints=hints or {}) |
| 157 |
# Test the state alteration does nothing |
| 158 |
new_state = project_state.clone() |
| 159 |
operation.state_forwards(app_label, new_state) |
| 160 |
self.assertEqual(new_state, project_state) |
| 161 |
# Test the database alteration |
| 162 |
self.assertEqual( |
| 163 |
project_state.apps.get_model(app_label, "Pony").objects.count(), 0 |
| 164 |
) |
| 165 |
with connection.schema_editor() as editor: |
| 166 |
operation.database_forwards(app_label, editor, project_state, new_state) |
| 167 |
Pony = project_state.apps.get_model(app_label, "Pony") |
| 168 |
if should_run: |
| 169 |
self.assertEqual(Pony.objects.count(), 2) |
| 170 |
else: |
| 171 |
self.assertEqual(Pony.objects.count(), 0) |
| 172 |
|
| 173 |
@override_settings(DATABASE_ROUTERS=[MigrateNothingRouter()]) |
| 174 |
def test_run_python_migrate_nothing_router(self): |
| 175 |
self._test_run_python("test_mltdb_runpython", should_run=False) |
| 176 |
|
| 177 |
@override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) |
| 178 |
def test_run_python_migrate_foo_router_without_hints(self): |
| 179 |
self._test_run_python("test_mltdb_runpython2", should_run=False) |
| 180 |
|
| 181 |
@override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) |
| 182 |
def test_run_python_migrate_foo_router_with_hints(self): |
| 183 |
self._test_run_python( |
| 184 |
"test_mltdb_runpython3", should_run=True, hints={"foo": True} |
| 185 |
) |