Module: Legion::Data::Local

Extended by:
Logging::Helper
Defined in:
lib/legion/data/local.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Logging::Helper

handle_exception

Class Attribute Details

.connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/legion/data/local.rb', line 14

def connection
  @connection
end

.db_pathObject (readonly)

Returns the value of attribute db_path.



14
15
16
# File 'lib/legion/data/local.rb', line 14

def db_path
  @db_path
end

Class Method Details

.connected?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/legion/data/local.rb', line 58

def connected?
  @connected == true
end

.model(table_name) ⇒ Object



72
73
74
75
76
# File 'lib/legion/data/local.rb', line 72

def model(table_name)
  raise 'Legion::Data::Local not connected' unless connected?

  ::Sequel::Model(connection[table_name])
end

.register_migrations(name:, path:) ⇒ Object



62
63
64
65
66
# File 'lib/legion/data/local.rb', line 62

def register_migrations(name:, path:)
  @registered_migrations ||= {}
  @registered_migrations[name] = path
  run_single_migration(name, path) if connected?
end

.registered_migrationsObject



68
69
70
# File 'lib/legion/data/local.rb', line 68

def registered_migrations
  @registered_migrations || {}
end

.reset!Object



111
112
113
114
115
116
# File 'lib/legion/data/local.rb', line 111

def reset!
  @connection = nil
  @connected = false
  @db_path = nil
  @registered_migrations = nil
end

.setup(database: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/data/local.rb', line 16

def setup(database: nil, **)
  return if @connected

  db_file = database || local_settings[:database] || 'legionio_local.db'
  @db_path = db_file

  sqlite_defaults = Legion::Data::Connection::ADAPTER_DEFAULTS.fetch(:sqlite, {})
  data = defined?(Legion::Settings) ? Legion::Settings[:data] : {}
  opts = { adapter: :sqlite, database: db_file }
  Legion::Data::Connection::ADAPTER_KEYS.fetch(:sqlite, []).each do |key|
    val = data.key?(key) && !data[key].nil? ? data[key] : sqlite_defaults[key]
    opts[key] = val unless val.nil?
  end

  if local_settings[:query_log]
    log_path = File.join(Legion::Data::Connection::QUERY_LOG_DIR, 'data-local-query.log')
    @query_file_logger = Legion::Data::Connection::QueryFileLogger.new(log_path)
    opts[:logger]          = @query_file_logger
    opts[:sql_log_level]   = :debug
  elsif data[:log] && defined?(Legion::Logging)
    opts[:logger]          = build_local_logger
    opts[:sql_log_level]   = resolved_sql_log_level
    opts[:log_warn_duration] = resolved_log_warn_duration
  end

  @connection = ::Sequel.connect(opts)
  @connected = true
  run_migrations
  log.info "Legion::Data::Local connected to #{db_file}"
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: :local_setup, database: db_file)
  raise
end

.shutdownObject



50
51
52
53
54
55
56
# File 'lib/legion/data/local.rb', line 50

def shutdown
  @connection&.disconnect
  @query_file_logger&.close
  @query_file_logger = nil
  @connection = nil
  @connected = false
end

.statsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/legion/data/local.rb', line 78

def stats
  return { connected: false } unless connected?

  stats = {
    connected:             true,
    adapter:               :sqlite,
    path:                  @db_path,
    query_log:             local_settings[:query_log] || false,
    query_log_path:        @query_file_logger&.path,
    registered_migrations: registered_migrations.keys
  }

  stats[:file_size] = File.size(@db_path) if @db_path && File.exist?(@db_path)

  %w[page_size page_count freelist_count journal_mode
     wal_autocheckpoint cache_size busy_timeout].each do |pragma|
    val = begin
      @connection.fetch("PRAGMA #{pragma}").single_value
    rescue StandardError => e
      handle_exception(e, level: :warn, handled: true, operation: :local_stats_pragma, pragma: pragma)
      nil
    end
    stats[pragma.to_sym] = val unless val.nil?
  end

  stats[:database_size_bytes] = stats[:page_size].to_i * stats[:page_count].to_i if stats[:page_size] && stats[:page_count]

  stats
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :local_stats, database: @db_path)
  { connected: connected?, error: e.message }
end