Class: ActiveRecord::ConnectionAdapters::AuroraServerless::Client

Inherits:
Object
  • Object
show all
Includes:
Mysql2::Client
Defined in:
lib/active_record/connection_adapters/aurora_serverless/client.rb

Constant Summary

Constants included from Mysql2::Client

Mysql2::Client::ESCAPE_MAP, Mysql2::Client::ESCAPE_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mysql2::Client

#abandon_results!, #automatic_close=, #close, default_query_options, #escape, #ping, #query, #query_options, #server_info

Constructor Details

#initialize(database, resource_arn, secret_arn, options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 12

def initialize(database, resource_arn, secret_arn, options = {})
  @database = database
  @resource_arn = resource_arn
  @secret_arn = secret_arn
  @raw_client = Aws::RDSDataService::Client.new(client_options(options))
  @transactions = []
  @affected_rows = 0
  @debug_transactions = false # Development toggle.
end

Instance Attribute Details

#affected_rowsObject (readonly)

Returns the value of attribute affected_rows.



5
6
7
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 5

def affected_rows
  @affected_rows
end

#databaseObject (readonly)

Returns the value of attribute database.



5
6
7
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 5

def database
  @database
end

#last_idObject (readonly)

Returns the value of attribute last_id.



5
6
7
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 5

def last_id
  @last_id
end

#raw_clientObject (readonly)

Returns the value of attribute raw_client.



5
6
7
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 5

def raw_client
  @raw_client
end

#resource_arnObject (readonly)

Returns the value of attribute resource_arn.



5
6
7
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 5

def resource_arn
  @resource_arn
end

#secret_arnObject (readonly)

Returns the value of attribute secret_arn.



5
6
7
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 5

def secret_arn
  @secret_arn
end

Instance Method Details

#begin_db_transactionObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 49

def begin_db_transaction
  id = raw_client.begin_transaction({
    database: database,
    secret_arn: secret_arn,
    resource_arn: resource_arn
  }).try(:transaction_id)
  debug_transactions 'BEGIN', id
  @transactions.unshift(id) if id
  true
end

#commit_db_transactionObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 60

def commit_db_transaction
  id = @transactions.shift
  return unless id
  debug_transactions 'COMMIT', id
  raw_client.commit_transaction({
    secret_arn: secret_arn,
    resource_arn: resource_arn,
    transaction_id: id
  })
  true
rescue
  @transactions.unshift(id) # For imminent rollback.
end

#exec_rollback_db_transactionObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 74

def exec_rollback_db_transaction
  id = @transactions.shift
  return unless id
  debug_transactions 'ROLLBACK', id
  raw_client.rollback_transaction({
    secret_arn: secret_arn,
    resource_arn: resource_arn,
    transaction_id: id
  })
  true
end

#execute_statement(sql) ⇒ Object



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

def execute_statement(sql)
  id = @transactions.first
  debug_transactions "EXECUTE: #{sql}", id
  raw_client.execute_statement({
    sql: sql,
    database: database,
    secret_arn: secret_arn,
    resource_arn: resource_arn,
    include_result_metadata: true,
    transaction_id: id
  }).tap do |r|
    @affected_rows = affected_rows_result(r)
    @last_id = last_id_result(r)
  end
rescue Exception => e
  if id && e.message == "Transaction #{id} is not found"
    @transactions.shift
    retry
  else
    raise e
  end
end

#inspectObject



22
23
24
# File 'lib/active_record/connection_adapters/aurora_serverless/client.rb', line 22

def inspect
  "#<#{self.class} database: #{database.inspect}, raw_client: #{raw_client.inspect}>"
end