#35967: TransactionTestCase.serialized_rollback reads from real database rather
than test when using read replica for a model instance created in a
migration with a ManyToManyField
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Core | Version: dev
(Serialization) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):
Sorry for sending on a wild goose chase Jacob, the patch above was meant
to be a draft that needs tweaking and not a final solution.
Once adjusted like the following it addresses the problem reported by Jake
{{{#!diff
diff --git a/django/test/utils.py b/django/test/utils.py
index ddb85127dc..7d66140efa 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -189,6 +189,7 @@ def setup_databases(
test_databases, mirrored_aliases =
get_unique_databases_and_mirrors(aliases)
old_names = []
+ serialize_connections = []
for db_name, aliases in test_databases.values():
first_alias = None
@@ -200,15 +201,14 @@ def setup_databases(
if first_alias is None:
first_alias = alias
with time_keeper.timed(" Creating '%s'" % alias):
- serialize_alias = (
- serialized_aliases is None or alias in
serialized_aliases
- )
connection.creation.create_test_db(
verbosity=verbosity,
autoclobber=not interactive,
keepdb=keepdb,
- serialize=serialize_alias,
+ serialize=False,
)
+ if serialized_aliases is None or alias in
serialized_aliases:
+ serialize_connections.append(connection)
if parallel > 1:
for index in range(parallel):
with time_keeper.timed(" Cloning '%s'" % alias):
@@ -229,6 +229,13 @@ def setup_databases(
connections[mirror_alias].settings_dict
)
+ # Serialize content of test databases only once all of them are setup
+ # to account for database mirroring and routing during serialization.
+ for serialize_connection in serialize_connections:
+ serialize_connection._test_serialized_contents = (
+ serialize_connection.creation.serialize_db_to_string()
+ )
+
if debug_sql:
for alias in connections:
connections[alias].force_debug_cursor = True
}}}
The above include three tweaks that were not included in comment:5
1. It explicitly pass `serialize=False` to `create_test_db` as we want to
defer this operation to a time when *all* connections are repointed to
test databases
2. Use `.creation.serialize_db_to_string` as you've noticed
3. Make sure to perform serialization only once mirrors have been
appropriately setup
I want to re-iterate that what appears to be the problem here, at least to
me, is less about the routing of queries during serialization and more
that we attempt to perform any form of reads against non-test databases
before `DATABASES` entries are **all** re-pointed to their test
equivalent.
--
Ticket URL: <https://code.djangoproject.com/ticket/35967#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070193b42c78e0-d94fc16b-9c0b-4833-b455-a5d4f01392bb-000000%40eu-central-1.amazonses.com.