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_set checks to all public methods in partitioning strategies
  • Create a new BaseStrategy class that provides the ensure_connection_set method
  • Implement ensure_connection_set! in SharedModel that 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 BaseStrategy and call ensure_connection_set before 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

  1. Enable the feature flag: Feature.enable(:enforce_explicit_connection_for_partitioned_shared_models)
  2. 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!'
  3. Wrap the call with SharedModel.using_connection(connection) - it should work without error
  4. Disable the feature flag - calls should work without explicit connection
Edited by Allison Browne

Merge request reports

Loading