Module: Legion::Data::Local

Defined in:
lib/legion/data/local.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/legion/data/local.rb', line 10

def connection
  @connection
end

.db_pathObject (readonly)

Returns the value of attribute db_path.



10
11
12
# File 'lib/legion/data/local.rb', line 10

def db_path
  @db_path
end

Class Method Details

.connected?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/legion/data/local.rb', line 47

def connected?
  @connected == true
end

.model(table_name) ⇒ Object



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

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

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

.register_migrations(name:, path:) ⇒ Object



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

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

.registered_migrationsObject



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

def registered_migrations
  @registered_migrations || {}
end

.reset!Object



98
99
100
101
102
103
# File 'lib/legion/data/local.rb', line 98

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

.setup(database: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/data/local.rb', line 12

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
  end

  @connection = ::Sequel.connect(opts)
  @connected = true
  run_migrations
  Legion::Logging.info "Legion::Data::Local connected to #{db_file}" if defined?(Legion::Logging)
end

.shutdownObject



39
40
41
42
43
44
45
# File 'lib/legion/data/local.rb', line 39

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

.statsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/legion/data/local.rb', line 67

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
      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
  { connected: connected?, error: e.message }
end