Class: SolidObjects::DatabaseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/database_adapter.rb,
sig/generated/lib/solid_objects/database_adapter.rbs

Constant Summary collapse

TRANSACTION_CLOCK =

Returns:

  • (::Symbol)
:solid_objects_transaction_clock
TRANSACTION_CLOCK_SCOPE =

Returns:

  • (::Symbol)
:solid_objects_transaction_clock_scope
FAMILIES =

An adapter name is a client name, not a protocol name. Trilogy reports "Trilogy" while speaking MySQL, so a pattern that only knows the mysql2 gem rejects a database Solid Objects fully supports. Every decision that depends on the database goes through this one table, so a client cannot be accepted in one place and rejected in another.

Returns:

  • (Object)
{
  postgresql: /postgres/i,
  mysql: /mysql|trilogy/i,
  sqlite: /sqlite/i
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ DatabaseAdapter

Returns a new instance of DatabaseAdapter.

RBS:

  • (untyped) -> void

Parameters:

  • (Object)


50
51
52
53
# File 'lib/solid_objects/database_adapter.rb', line 50

def initialize(connection)
  @connection_pool = connection.respond_to?(:pool) ? connection.pool : nil
  @fixed_connection = connection_pool ? nil : connection
end

Instance Attribute Details

#connection_poolObject (readonly)

Returns the value of attribute connection_pool.

Returns:

  • (Object)


151
152
153
# File 'lib/solid_objects/database_adapter.rb', line 151

def connection_pool
  @connection_pool
end

#fixed_connectionObject (readonly)

Returns the value of attribute fixed_connection.

Returns:

  • (Object)


151
152
153
# File 'lib/solid_objects/database_adapter.rb', line 151

def fixed_connection
  @fixed_connection
end

Class Method Details

.family(connection) ⇒ Symbol?

RBS:

  • (untyped) -> Symbol?

Parameters:

  • (Object)

Returns:

  • (Symbol, nil)


23
24
25
26
27
28
29
# File 'lib/solid_objects/database_adapter.rb', line 23

def family(connection)
  adapter_name = connection.adapter_name
  FAMILIES.each do |family, pattern|
    return family if adapter_name.match?(pattern)
  end
  nil
end

.for(connection) ⇒ DatabaseAdapter

RBS:

  • (untyped) -> DatabaseAdapter

Parameters:

  • (Object)

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/solid_objects/database_adapter.rb', line 32

def for(connection)
  case family(connection)
  when :postgresql
    DatabaseAdapters::Postgresql.new(connection)
  when :mysql
    DatabaseAdapters::Mysql.new(connection)
  when :sqlite
    DatabaseAdapters::Sqlite.new(connection)
  else
    raise UnsupportedDatabase, "unsupported database adapter #{connection.adapter_name.inspect}"
  end
end

Instance Method Details

#additional_server_reasonsArray[String]

RBS:

  • () -> Array[String]

Returns:

  • (Array[String])


88
89
90
# File 'lib/solid_objects/database_adapter.rb', line 88

def additional_server_reasons
  []
end

#claim_lockString?

RBS:

  • () -> String?

Returns:

  • (String, nil)


98
99
100
# File 'lib/solid_objects/database_adapter.rb', line 98

def claim_lock
  nil
end

#configure_transaction_deadline(_connection) ⇒ void

This method returns an undefined value.

RBS:

  • (untyped) -> void

Parameters:

  • (Object)


180
181
# File 'lib/solid_objects/database_adapter.rb', line 180

def configure_transaction_deadline(_connection)
end

#current_time_expressionString

RBS:

  • () -> String

Returns:

  • (String)


103
104
105
# File 'lib/solid_objects/database_adapter.rb', line 103

def current_time_expression
  "CURRENT_TIMESTAMP"
end

#database_nowTime

RBS:

  • () -> Time

Returns:

  • (Time)


108
109
110
111
112
# File 'lib/solid_objects/database_adapter.rb', line 108

def database_now
  return read_database_now unless ActiveSupport::IsolatedExecutionState[TRANSACTION_CLOCK_SCOPE]

  ActiveSupport::IsolatedExecutionState[TRANSACTION_CLOCK] ||= read_database_now
end

#deadline_error?(_error) ⇒ Boolean

RBS:

  • (Exception) -> bool

Parameters:

  • (Exception)

Returns:

  • (Boolean)


184
185
186
# File 'lib/solid_objects/database_adapter.rb', line 184

def deadline_error?(_error)
  false
end

#lock_candidates(relation) ⇒ ActiveRecord::Relation[untyped]

RBS:

  • (ActiveRecord::Relation[untyped]) -> ActiveRecord::Relation[untyped]

Parameters:

Returns:



145
146
147
# File 'lib/solid_objects/database_adapter.rb', line 145

def lock_candidates(relation)
  claim_lock ? relation.lock(claim_lock) : relation
end

#minimum_server_versionGem::Version?

The oldest server the adapter has been exercised against. Reported rather than enforced: refusing to boot on an untested server would be a worse failure than running on one.

RBS:

  • () -> Gem::Version?

Returns:

  • (Gem::Version, nil)


59
60
61
# File 'lib/solid_objects/database_adapter.rb', line 59

def minimum_server_version
  nil
end

#read_database_nowTime

RBS:

  • () -> Time

Returns:

  • (Time)


167
168
169
170
171
172
# File 'lib/solid_objects/database_adapter.rb', line 167

def read_database_now
  value = with_connection do |connection|
    connection.select_value("SELECT #{current_time_expression}")
  end
  value.is_a?(Time) ? value.utc : Time.parse("#{value} UTC").utc
end

#server_versionGem::Version

RBS:

  • () -> Gem::Version

Returns:

  • (Gem::Version)


64
65
66
67
68
# File 'lib/solid_objects/database_adapter.rb', line 64

def server_version
  with_connection do |connection|
    Gem::Version.new(connection.database_version.to_s)
  end
end

#supports_skip_locked?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


93
94
95
# File 'lib/solid_objects/database_adapter.rb', line 93

def supports_skip_locked?
  false
end

#transaction { ... } ⇒ Object

RBS:

  • () { () -> untyped } -> untyped

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/solid_objects/database_adapter.rb', line 125

def transaction(&block)
  raise DatabaseDeadlineExceeded, "synchronous invocation deadline expired" if SyncDeadline.expired?

  with_connection do |connection|
    with_transaction_deadline(connection) do
      connection.transaction(requires_new: true) do
        configure_transaction_deadline(connection)
        with_transaction_clock { block.call }
      end
    end
  end
rescue => error
  raise unless deadline_error?(error)

  raise DatabaseDeadlineExceeded,
    "database lock wait exceeded the synchronous invocation deadline",
    cause: error
end

#unsupported_server_reasons(observed = nil) ⇒ Array[String]

One observed version decides both the status and the message. Reading it again could let a transient failure replace an already determined result.

RBS:

  • (?Gem::Version?) -> Array[String]

Parameters:

  • (Gem::Version, nil)

Returns:

  • (Array[String])


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/solid_objects/database_adapter.rb', line 73

def unsupported_server_reasons(observed = nil)
  observed ||= server_version
  reasons = []
  minimum = minimum_server_version
  if minimum && observed < minimum
    reasons << "#{self.class.name.demodulize} #{observed} is older than " \
      "Solid Objects requires, which is #{minimum}"
  end
  reasons.concat(additional_server_reasons)
  reasons
rescue => error
  [ "the database server could not be verified: #{error.class}: #{error.message}" ]
end

#with_connection {|arg0| ... } ⇒ Object

RBS:

  • () { (untyped) -> untyped } -> untyped

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (Object)

Returns:

  • (Object)


189
190
191
192
193
# File 'lib/solid_objects/database_adapter.rb', line 189

def with_connection
  return yield fixed_connection unless connection_pool

  connection_pool.with_connection { |connection| yield connection }
end

#with_lock_probe { ... } ⇒ Object

RBS:

  • () { () -> untyped } -> untyped

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


120
121
122
# File 'lib/solid_objects/database_adapter.rb', line 120

def with_lock_probe
  yield
end

#with_lock_retry { ... } ⇒ Object

RBS:

  • () { () -> untyped } -> untyped

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


115
116
117
# File 'lib/solid_objects/database_adapter.rb', line 115

def with_lock_retry
  yield
end

#with_transaction_clock { ... } ⇒ Object

RBS:

  • () { () -> untyped } -> untyped

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


154
155
156
157
158
159
160
161
162
163
164
# File 'lib/solid_objects/database_adapter.rb', line 154

def with_transaction_clock
  return yield if ActiveSupport::IsolatedExecutionState[TRANSACTION_CLOCK_SCOPE]

  ActiveSupport::IsolatedExecutionState[TRANSACTION_CLOCK_SCOPE] = true
  begin
    yield
  ensure
    ActiveSupport::IsolatedExecutionState[TRANSACTION_CLOCK_SCOPE] = false
    ActiveSupport::IsolatedExecutionState[TRANSACTION_CLOCK] = nil
  end
end

#with_transaction_deadline(_connection) { ... } ⇒ Object

RBS:

  • (untyped) { () -> untyped } -> untyped

Parameters:

  • (Object)

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


175
176
177
# File 'lib/solid_objects/database_adapter.rb', line 175

def with_transaction_deadline(_connection)
  yield
end