Class: PgOnlineSchemaChange::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_online_schema_change/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pg_online_schema_change/client.rb', line 10

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

Instance Attribute Details

#alter_statementObject

Returns the value of attribute alter_statement.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def alter_statement
  @alter_statement
end

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def connection
  @connection
end

#copy_statementObject

Returns the value of attribute copy_statement.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def copy_statement
  @copy_statement
end

#dbnameObject

Returns the value of attribute dbname.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def dbname
  @dbname
end

#delta_countObject

Returns the value of attribute delta_count.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def delta_count
  @delta_count
end

#dropObject

Returns the value of attribute drop.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def drop
  @drop
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def host
  @host
end

#kill_backendsObject

Returns the value of attribute kill_backends.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def kill_backends
  @kill_backends
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def password
  @password
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def port
  @port
end

#pull_batch_countObject

Returns the value of attribute pull_batch_count.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def pull_batch_count
  @pull_batch_count
end

#schemaObject

Returns the value of attribute schema.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def schema
  @schema
end

#tableObject

Returns the value of attribute table.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def table
  @table
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def username
  @username
end

#wait_time_for_lockObject

Returns the value of attribute wait_time_for_lock.



7
8
9
# File 'lib/pg_online_schema_change/client.rb', line 7

def wait_time_for_lock
  @wait_time_for_lock
end

Instance Method Details

#handle_copy_statement(statement) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
# File 'lib/pg_online_schema_change/client.rb', line 48

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

#handle_validationsObject

Raises:



40
41
42
43
44
45
46
# File 'lib/pg_online_schema_change/client.rb', line 40

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