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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pg_online_schema_change/client.rb', line 25

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
  @skip_foreign_key_validation = options.skip_foreign_key_validation

  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)
  @table_name = Query.table_name(@alter_statement, @table)

  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

#skip_foreign_key_validationObject

Returns the value of attribute skip_foreign_key_validation.



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

def skip_foreign_key_validation
  @skip_foreign_key_validation
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

#table_nameObject

Returns the value of attribute table_name.



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

def table_name
  @table_name
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

#checkout_connectionObject



75
76
77
# File 'lib/pg_online_schema_change/client.rb', line 75

def checkout_connection
  PG.connect(dbname: dbname, host: host, user: username, password: password, port: port)
end

#handle_copy_statement(statement) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
# File 'lib/pg_online_schema_change/client.rb', line 66

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.binread(file_path)
end

#handle_validationsObject

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pg_online_schema_change/client.rb', line 52

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

  if delta_count > pull_batch_count
    raise Error, "Value for delta_count should be smaller than the value for pull_batch_count"
  end

  return if Query.same_table?(@alter_statement)

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