Skip to content

Commit d5c4d29

Browse files
committed
fix(api): Skip the test databases' serialized_rollback snapshot
Setting up the databases, Django serialises every model in every one of them so that `TransactionTestCase(serialized_rollback=True)` can restore them later. Nothing in this suite asks for that, so the work is pure cost. On the ClickHouse alias it is worse than pure cost. Serialising builds a `MigrationLoader`, and `django-clickhouse-backend` caches its migration model on `MigrationRecorder` -- which carries a `deleted` column that Django's does not. Get there with a PostgreSQL connection first and the cache holds a model without that column, while the queryset still filters on it, so the ClickHouse alias fails with FieldError: Cannot resolve keyword 'deleted' into field That only surfaces in the private build, where an app routed to ClickHouse gives the alias something to serialise.
1 parent adb635d commit d5c4d29

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

api/tests/migration_snapshots.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,24 @@ def create_test_db(
482482
finally:
483483
snapshots.close()
484484

485+
def serialize_db_to_string(self: BaseDatabaseCreation) -> str:
486+
"""Skip the setup-time snapshot Django takes for `serialized_rollback`.
487+
488+
Django serialises every model in every database while setting the
489+
databases up, so that `TransactionTestCase(serialized_rollback=True)`
490+
can restore them afterwards. Nothing in this suite asks for that, so
491+
it is pure cost -- and on the ClickHouse alias it is worse than that:
492+
it builds a `MigrationLoader`, and `django-clickhouse-backend` caches
493+
its migration model on `MigrationRecorder` in a way that breaks if a
494+
PostgreSQL connection got there first.
495+
"""
496+
return ""
497+
498+
original_serialize = BaseDatabaseCreation.serialize_db_to_string
485499
BaseDatabaseCreation.create_test_db = create_test_db # type: ignore[method-assign]
500+
BaseDatabaseCreation.serialize_db_to_string = serialize_db_to_string # type: ignore[method-assign]
486501
try:
487502
yield
488503
finally:
489504
BaseDatabaseCreation.create_test_db = original # type: ignore[method-assign]
505+
BaseDatabaseCreation.serialize_db_to_string = original_serialize # type: ignore[method-assign]

0 commit comments

Comments
 (0)