Sha256: 03be0df931154e0728197b47868a5a97636689236ed114c4118d11ca08b42704

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require "pg"

module PgOnlineSchemaChange
  class Client
    attr_accessor :alter_statement, :schema, :dbname, :host, :username, :port, :password, :connection, :table, :drop,
                  :kill_backends, :wait_time_for_lock, :copy_statement, :pull_batch_count, :delta_count

    def initialize(options)
      @alter_statement = options.alter_statement
      @schema = options.schema
      @dbname = options.dbname
      @host = options.host
      @username = options.username
      @port = options.port
      @password = options.password
      @drop = options.drop
      @kill_backends = options.kill_backends
      @wait_time_for_lock = options.wait_time_for_lock
      @pull_batch_count = options.pull_batch_count
      @delta_count = options.delta_count

      handle_copy_statement(options.copy_statement)
      handle_validations

      @connection = PG.connect(
        dbname: @dbname,
        host: @host,
        user: @username,
        password: @password,
        port: @port,
      )

      @table = Query.table(@alter_statement)

      PgOnlineSchemaChange.logger.debug("Connection established")
    end

    def handle_validations
      raise Error, "Not a valid ALTER statement: #{@alter_statement}" unless Query.alter_statement?(@alter_statement)

      return if Query.same_table?(@alter_statement)

      raise Error "All statements should belong to the same table: #{@alter_statement}"
    end

    def handle_copy_statement(statement)
      return if statement.nil? || statement == ""

      file_path = File.expand_path(statement)
      raise Error, "File not found: #{file_path}" unless File.file?(file_path)

      @copy_statement = File.open(file_path, "rb", &:read)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pg_online_schema_change-0.7.4 lib/pg_online_schema_change/client.rb
pg_online_schema_change-0.7.3 lib/pg_online_schema_change/client.rb
pg_online_schema_change-0.7.2 lib/pg_online_schema_change/client.rb
pg_online_schema_change-0.7.1 lib/pg_online_schema_change/client.rb
pg_online_schema_change-0.7.0 lib/pg_online_schema_change/client.rb
pg_online_schema_change-0.6.0 lib/pg_online_schema_change/client.rb
pg_online_schema_change-0.5.0 lib/pg_online_schema_change/client.rb