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

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)


30
31
32
33
# File 'lib/solid_objects/database_adapter.rb', line 30

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)


131
132
133
# File 'lib/solid_objects/database_adapter.rb', line 131

def connection_pool
  @connection_pool
end

#fixed_connectionObject (readonly)

Returns the value of attribute fixed_connection.

Returns:

  • (Object)


131
132
133
# File 'lib/solid_objects/database_adapter.rb', line 131

def fixed_connection
  @fixed_connection
end

Class Method Details

.for(connection) ⇒ DatabaseAdapter

RBS:

  • (untyped) -> DatabaseAdapter

Parameters:

  • (Object)

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/solid_objects/database_adapter.rb', line 12

def for(connection)
  case connection.adapter_name
  when /postgres/i
    DatabaseAdapters::Postgresql.new(connection)
  when /mysql/i
    DatabaseAdapters::Mysql.new(connection)
  when /sqlite/i
    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])


68
69
70
# File 'lib/solid_objects/database_adapter.rb', line 68

def additional_server_reasons
  []
end

#claim_lockString?

RBS:

  • () -> String?

Returns:

  • (String, nil)


78
79
80
# File 'lib/solid_objects/database_adapter.rb', line 78

def claim_lock
  nil
end

#configure_transaction_deadline(_connection) ⇒ void

This method returns an undefined value.

RBS:

  • (untyped) -> void

Parameters:

  • (Object)


160
161
# File 'lib/solid_objects/database_adapter.rb', line 160

def configure_transaction_deadline(_connection)
end

#current_time_expressionString

RBS:

  • () -> String

Returns:

  • (String)


83
84
85
# File 'lib/solid_objects/database_adapter.rb', line 83

def current_time_expression
  "CURRENT_TIMESTAMP"
end

#database_nowTime

RBS:

  • () -> Time

Returns:

  • (Time)


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

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)


164
165
166
# File 'lib/solid_objects/database_adapter.rb', line 164

def deadline_error?(_error)
  false
end

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

RBS:

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

Parameters:

Returns:



125
126
127
# File 'lib/solid_objects/database_adapter.rb', line 125

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)


39
40
41
# File 'lib/solid_objects/database_adapter.rb', line 39

def minimum_server_version
  nil
end

#read_database_nowTime

RBS:

  • () -> Time

Returns:

  • (Time)


147
148
149
150
151
152
# File 'lib/solid_objects/database_adapter.rb', line 147

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)


44
45
46
47
48
# File 'lib/solid_objects/database_adapter.rb', line 44

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)


73
74
75
# File 'lib/solid_objects/database_adapter.rb', line 73

def supports_skip_locked?
  false
end

#transaction { ... } ⇒ Object

RBS:

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

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/solid_objects/database_adapter.rb', line 105

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])


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/solid_objects/database_adapter.rb', line 53

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)


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

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)


100
101
102
# File 'lib/solid_objects/database_adapter.rb', line 100

def with_lock_probe
  yield
end

#with_lock_retry { ... } ⇒ Object

RBS:

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

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


95
96
97
# File 'lib/solid_objects/database_adapter.rb', line 95

def with_lock_retry
  yield
end

#with_transaction_clock { ... } ⇒ Object

RBS:

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

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/solid_objects/database_adapter.rb', line 134

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)


155
156
157
# File 'lib/solid_objects/database_adapter.rb', line 155

def with_transaction_deadline(_connection)
  yield
end