Sha256: 141cd7408fa1f197c59161921ba5ee64d136dc7851296c79eab6dadb5c37d41a

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Brillo
  module Adapter
    class Postgres < Base
      def load_command
        host = config["host"] ? "--host #{config["host"]}" : ""
        password = config["password"] ? "PGPASSWORD=#{config["password"]} " : ""
        search_path = config["schema_search_path"] ? "PGOPTIONS=--search_path=#{config["schema_search_path"]} " : ""
        inline_options = password + search_path
        "#{inline_options}psql #{host} -U #{config.fetch("username")} #{config.fetch("database")}"
      end

      # pgdump without schema does not set sequences, so we have to do it ourselves, or the first insert
      # into a scrubbed table will fail on duplicate primary key
      def table_footer(klass)
        table_name = klass.table_name
        <<-SQL
          SELECT setval(pg_get_serial_sequence('#{table_name}', 'id'), coalesce(MAX(id),0) + 1, false)
          FROM #{table_name};
        SQL
      end

      def recreate_db
        logger.info "Dropping all connections to #{config[:database]}"
        ActiveRecord::Base.connection.execute(
          <<-SQL
          -- Disconnect all others from the database we are about to drop.
          -- Without this, the drop will fail and so the load will abort.
          SELECT pg_terminate_backend(pg_stat_activity.pid)
          FROM pg_stat_activity
          WHERE pg_stat_activity.datname = '#{config[:database]}'
            AND pid <> pg_backend_pid();
          SQL
        )
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brillo-2.1.0 lib/brillo/adapter/postgres.rb