used for preparing hypothetical indexes to be run in higher environments
Useful when a database includes the hypopg extension (for Postgres) to create and test hypothetical indexes. The goal is to create a copy and pasteable set of commands that a user can run to create a hypothetical index for testing in a higher environment.
The final deliverable is a set of Ruby commands to copy/paste. Wrap them in code blocks.
connection = ::ActiveRecord::Base.lease_connection to find an available connectioncreate_index_sql using a <<~SQL.squish heredoc for the index creation.hypopg_create_index(). Set this to hypopg_sql. Use connection.quote(create_index_sql) to properly handle nested quotes and SQL escaping when interpolating the SQL string into the hypopg function callconnection.execute hypopg_sqlresultsresults.to_aconnection to call .execute with string interpolation to add "EXPLAIN " at the beginning of a #to_sql call on a chain of model code. For example: connection.execute("EXPLAIN #{Model.where(...).to_sql}").