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)


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

def connection_pool
  @connection_pool
end

#fixed_connectionObject (readonly)

Returns the value of attribute fixed_connection.

Returns:

  • (Object)


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

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

#claim_lockString?

RBS:

  • () -> String?

Returns:

  • (String, nil)


41
42
43
# File 'lib/solid_objects/database_adapter.rb', line 41

def claim_lock
  nil
end

#configure_transaction_deadline(_connection) ⇒ void

This method returns an undefined value.

RBS:

  • (untyped) -> void

Parameters:

  • (Object)


123
124
# File 'lib/solid_objects/database_adapter.rb', line 123

def configure_transaction_deadline(_connection)
end

#current_time_expressionString

RBS:

  • () -> String

Returns:

  • (String)


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

def current_time_expression
  "CURRENT_TIMESTAMP"
end

#database_nowTime

RBS:

  • () -> Time

Returns:

  • (Time)


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

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)


127
128
129
# File 'lib/solid_objects/database_adapter.rb', line 127

def deadline_error?(_error)
  false
end

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

RBS:

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

Parameters:

Returns:



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

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

#read_database_nowTime

RBS:

  • () -> Time

Returns:

  • (Time)


110
111
112
113
114
115
# File 'lib/solid_objects/database_adapter.rb', line 110

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

#supports_skip_locked?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


36
37
38
# File 'lib/solid_objects/database_adapter.rb', line 36

def supports_skip_locked?
  false
end

#transaction { ... } ⇒ Object

RBS:

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

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


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

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

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

RBS:

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

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (Object)

Returns:

  • (Object)


132
133
134
135
136
# File 'lib/solid_objects/database_adapter.rb', line 132

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)


63
64
65
# File 'lib/solid_objects/database_adapter.rb', line 63

def with_lock_probe
  yield
end

#with_lock_retry { ... } ⇒ Object

RBS:

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

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


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

def with_lock_retry
  yield
end

#with_transaction_clock { ... } ⇒ Object

RBS:

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

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/solid_objects/database_adapter.rb', line 97

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)


118
119
120
# File 'lib/solid_objects/database_adapter.rb', line 118

def with_transaction_deadline(_connection)
  yield
end