Skip to content

Commit

Permalink
Replaced connection with with_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 7, 2024
1 parent 0fb4618 commit 7960910
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/pghero/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def connection_model
# rough check for Postgres adapter
# keep this message generic so it's useful
# when empty url set in Docker image pghero.yml
unless @connection_model.connection.adapter_name.match?(/postg/i)
unless @connection_model.connection_db_config.adapter.to_s.match?(/postg/i)
raise Error, "Invalid connection URL"
end
@adapter_checked = true
Expand Down
32 changes: 17 additions & 15 deletions lib/pghero/methods/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ def server_version_num
end

def quote_ident(value)
connection.quote_column_name(value)
with_connection { |c| c.quote_column_name(value) }
end

private

def select_all(sql, conn: nil, query_columns: [])
conn ||= connection
def select_all(sql, stats: false, query_columns: [])
with_connection(stats: stats) do |conn|
select_all_leased(sql, conn: conn, query_columns: query_columns)
end
end

def select_all_leased(sql, conn:, query_columns:)
# squish for logs
retries = 0
begin
Expand Down Expand Up @@ -81,7 +86,7 @@ def select_all(sql, conn: nil, query_columns: [])
end

def select_all_stats(sql, **options)
select_all(sql, **options, conn: stats_connection)
select_all(sql, **options, stats: true)
end

def select_all_size(sql)
Expand All @@ -97,15 +102,12 @@ def select_one(sql)
end

def execute(sql)
connection.execute(add_source(sql))
end

def connection
connection_model.connection
with_connection { |c| c.execute(add_source(sql)) }
end

def stats_connection
::PgHero::Stats.connection
def with_connection(stats: false, &block)
model = stats ? ::PgHero::Stats : connection_model
model.connection_pool.with_connection(&block)
end

def squish(str)
Expand All @@ -117,15 +119,15 @@ def add_source(sql)
end

def quote(value)
connection.quote(value)
with_connection { |c| c.quote(value) }
end

def quote_table_name(value)
connection.quote_table_name(value)
with_connection { |c| c.quote_table_name(value) }
end

def quote_column_name(value)
connection.quote_column_name(value)
with_connection { |c| c.quote_column_name(value) }
end

def unquote(part)
Expand All @@ -146,7 +148,7 @@ def with_transaction(lock_timeout: nil, statement_timeout: nil, rollback: false)
end

def table_exists?(table)
stats_connection.table_exists?(table)
with_connection(stats: true) { |c| c.table_exists?(table) }
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/pghero/methods/suggested_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def autoindex(create: false)
suggested_indexes.each do |index|
p index
if create
connection.execute("CREATE INDEX CONCURRENTLY ON #{quote_table_name(index[:table])} (#{index[:columns].map { |c| quote_column_name(c) }.join(",")})")
with_connection do |connection|
connection.execute("CREATE INDEX CONCURRENTLY ON #{quote_table_name(index[:table])} (#{index[:columns].map { |c| quote_column_name(c) }.join(",")})")
end
end
end
end
Expand Down

0 comments on commit 7960910

Please sign in to comment.