Sha256: 610e0a6a604f915471641fd94108e55b90b17f4f687948aa170f074950ddcfc4

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module PgEasyReplicate
  class Query
    extend Helper

    class << self
      def run(
        query:,
        connection_url:,
        user: internal_user_name,
        schema: nil,
        transaction: true,
        using_vacuum_analyze: false
      )
        conn =
          connect(connection_url: connection_url, schema: schema, user: user)
        timeout ||= ENV["PG_EASY_REPLICATE_STATEMENT_TIMEOUT"] || "5s"
        if transaction
          r =
            conn.transaction do
              conn.run("SET search_path to #{quote_ident(schema)}") if schema
              conn.run("SET statement_timeout to '#{timeout}'")
              conn.fetch(query).to_a
            end
        else
          conn.run("SET search_path to #{quote_ident(schema)}") if schema
          if using_vacuum_analyze
            conn.run("SET statement_timeout=0")
          else
            conn.run("SET statement_timeout to '5s'")
          end
          r = conn.fetch(query).to_a
        end
        conn.disconnect
        r
      ensure
        conn&.fetch("RESET statement_timeout")
        conn&.disconnect
      end

      def connect(connection_url:, user: internal_user_name, schema: nil)
        c =
          Sequel.connect(
            connection_url,
            user: user,
            logger: ENV.fetch("DEBUG", nil) ? logger : nil,
            search_path: schema,
          )
        logger.debug("Connection established")
        c
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pg_easy_replicate-0.3.3 lib/pg_easy_replicate/query.rb
pg_easy_replicate-0.3.2 lib/pg_easy_replicate/query.rb
pg_easy_replicate-0.3.1 lib/pg_easy_replicate/query.rb
pg_easy_replicate-0.3.0 lib/pg_easy_replicate/query.rb
pg_easy_replicate-0.2.7 lib/pg_easy_replicate/query.rb
pg_easy_replicate-0.2.6 lib/pg_easy_replicate/query.rb
pg_easy_replicate-0.2.5 lib/pg_easy_replicate/query.rb
pg_easy_replicate-0.2.4 lib/pg_easy_replicate/query.rb