SQL for queries using value lists (e.g. `column IN (value_list)`) varies by the number of elements in the value list, because a separate placeholder is generally used for each element in the value list. You can get consistent SQL by using the equivalent `column = ANY(array_expr::type[])`, which will only use a single placeholder for the array expression. This is useful if you want to view/audit the queries that the application is generating. Sequel has had a pg_auto_parameterize_in_array Database extension for a while that handles most of this. However, in the case where the value list is Ruby strings, it is ambiguous what the type of the array should be. Postgres use the unknown type, not the text type, when there is not an explicit/implicit cast for a single quoted string. The pg_auto_parameterize_in_array extension can assume the text[] type in such cases, but it will break queries that need a different cast. However, such breakage is explicit (DatabaseError raised), and not silent, and can be fixed. This affects queries where the column values are plain strings in Ruby, but use a non-text database type, such as the uuid type or an enum type. This adds a couple singleton methods on Sequel, any_uuid and any_type, which allow conversion of arrays (normally treated as value lists) to an `ANY(array_expr::type[])` expression. This converts the cases that would fail with an explicit cast to text[] to use a uuid[] or lb_node_state[] cast instead. This handles most of the explicit use of `IN (value_list)`. There is a significant amount of implicit `IN (value_list)`, because that is what is used by default for eager loading. This uses a new pg_eager_any_typed_array plugin I developed to handle this case. This will automatically use `column = ANY(array_expr::type[])`, using the appropriate database type of the predicate key using for eager loading.
43 lines
2.1 KiB
Ruby
43 lines
2.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "netaddr"
|
|
require "sequel/core"
|
|
require_relative "config"
|
|
require_relative "lib/util"
|
|
|
|
db_ca_bundle_filename = File.join(Dir.pwd, "var", "ca_bundles", "db_ca_bundle.crt")
|
|
Util.safe_write_to_file(db_ca_bundle_filename, Config.clover_database_root_certs)
|
|
max_connections = Config.db_pool - 1
|
|
max_connections = 1 if ENV["SHARED_CONNECTION"] == "1"
|
|
DB = Sequel.connect(Config.clover_database_url, max_connections:, pool_timeout: Config.database_timeout, treat_string_list_as_text_array: true).tap do |db|
|
|
# Replace dangerous (for cidrs) Ruby IPAddr type that is otherwise
|
|
# used by sequel_pg. Has come up more than once in the bug tracker:
|
|
#
|
|
# https://github.com/jeremyevans/sequel_pg/issues?q=inet
|
|
# https://github.com/jeremyevans/sequel_pg/issues/30
|
|
# https://github.com/jeremyevans/sequel_pg/pull/37
|
|
db.add_conversion_proc(650, NetAddr.method(:parse_net))
|
|
db.add_conversion_proc(869, NetAddr.method(:parse_ip))
|
|
end
|
|
|
|
postgres_monitor_db_ca_bundle_filename = File.join(Dir.pwd, "var", "ca_bundles", "postgres_monitor_db.crt")
|
|
Util.safe_write_to_file(postgres_monitor_db_ca_bundle_filename, Config.postgres_monitor_database_root_certs)
|
|
begin
|
|
POSTGRES_MONITOR_DB = Sequel.connect(Config.postgres_monitor_database_url, max_connections: Config.db_pool, pool_timeout: Config.database_timeout) if Config.postgres_monitor_database_url
|
|
rescue Sequel::DatabaseConnectionError => ex
|
|
Clog.emit("Failed to connect to Postgres Monitor database") { {database_connection_failed: {exception: Util.exception_to_hash(ex)}} }
|
|
end
|
|
|
|
# Load Sequel Database/Global extensions here
|
|
# DB.extension :date_arithmetic
|
|
DB.extension :pg_array, :pg_json, :pg_auto_parameterize, :pg_auto_parameterize_in_array, :pg_timestamptz, :pg_range
|
|
Sequel.extension :pg_range_ops, :pg_json_ops
|
|
|
|
DB.extension :pg_schema_caching
|
|
DB.extension :index_caching
|
|
DB.load_schema_cache?("cache/schema.cache")
|
|
DB.load_index_cache?("cache/index.cache")
|
|
|
|
DB.extension :temporarily_release_connection if ENV["SHARED_CONNECTION"] == "1"
|
|
DB.extension :query_blocker if Config.test? && ENV["CLOVER_FREEZE"] == "1"
|