Require explicit connection in the partitioning strategies for SharedModels
What does this MR do and why?
This MR implements explicit connection enforcement for partitioning strategies when used with SharedModel classes. It prevents accidental use of the default database connection when working with shared models that should explicitly specify which database connection to use.
Problem: Partition strategies (Daily, Monthly, Sliding List, Int Range) can be called without an explicit connection context, which causes them to use the default database connection. For SharedModels that operate across multiple databases (e.g., CI/Sec tables), this can lead to querying the wrong database and subtle bugs that are difficult to debug.
Solution:
- Add
ensure_connection_setchecks to all public methods in partitioning strategies - Create a new
BaseStrategyclass that provides theensure_connection_setmethod - Implement
ensure_connection_set!inSharedModelthat raises an error if no explicit connection is set when the feature flag is enabled - Guard the enforcement behind a feature flag (
enforce_explicit_connection_for_partitioned_shared_models) to allow safe rollout - Update all strategy implementations (Daily, Monthly, Sliding List, Int Range) to inherit from
BaseStrategyand callensure_connection_setbefore accessing partition data - Update tests to wrap strategy calls with explicit connections using
SharedModel.using_connection
References
Screenshots or screen recordings
N/A
How to set up and validate locally
- Enable the feature flag:
Feature.enable(:enforce_explicit_connection_for_partitioned_shared_models) - Try to call a partitioning strategy method on a SharedModel without an explicit connection - it should raise
'connection not set! Using the default is dangerous!' - Wrap the call with
SharedModel.using_connection(connection)- it should work without error - Disable the feature flag - calls should work without explicit connection
Edited by Allison Browne