Module: TransactionIsolation::ActiveRecord::ConnectionAdapters::SQLite3Adapter

Defined in:
lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb

Constant Summary collapse

VENDOR_ISOLATION_LEVEL =
{
  read_uncommitted: 'read_uncommitted = 1',
  read_committed: 'read_uncommitted = 0',
  repeatable_read: 'read_uncommitted = 0',
  serializable: 'read_uncommitted = 0'
}.freeze
ANSI_ISOLATION_LEVEL =
{
  'read_uncommitted = 1' => :read_uncommitted,
  'read_uncommitted = 0' => :serializable
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
14
# File 'lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb', line 9

def self.included(base)
  base.class_eval do
    alias_method :translate_exception_without_transaction_isolation_conflict, :translate_exception
    alias_method :translate_exception, :translate_exception_with_transaction_isolation_conflict
  end
end

Instance Method Details

#current_isolation_levelObject



32
33
34
# File 'lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb', line 32

def current_isolation_level
  ANSI_ISOLATION_LEVEL[current_vendor_isolation_level]
end

#current_vendor_isolation_levelObject



36
37
38
# File 'lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb', line 36

def current_vendor_isolation_level
  "read_uncommitted = #{select_value('PRAGMA read_uncommitted')}"
end

#isolation_conflict?(exception) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb', line 69

def isolation_conflict?(exception)
  ['The database file is locked',
   'A table in the database is locked',
   'Database lock protocol error'].any? do |error_message|
    exception.message =~ /#{Regexp.escape(error_message)}/i
  end
end

#isolation_level(level) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb', line 40

def isolation_level(level)
  validate_isolation_level(level)

  original_vendor_isolation_level = current_vendor_isolation_level if block_given?

  execute "PRAGMA #{VENDOR_ISOLATION_LEVEL[level]}"

  return unless block_given?

  begin
    yield
  ensure
    execute "PRAGMA #{original_vendor_isolation_level}"
  end
end

#supports_isolation_levels?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb', line 16

def supports_isolation_levels?
  true
end

#translate_exception_with_transaction_isolation_conflict(*args) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb', line 56

def translate_exception_with_transaction_isolation_conflict(*args)
  exception = args.first

  if isolation_conflict?(exception)
    ::ActiveRecord::TransactionIsolationConflict.new("Transaction isolation conflict detected: #{exception.message}")
  else
    translate_exception_without_transaction_isolation_conflict(*args)
  end
end