Module: Legion::Data::Local
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/data/local.rb
Class Attribute Summary collapse
-
.connection ⇒ Object
readonly
Returns the value of attribute connection.
-
.db_path ⇒ Object
readonly
Returns the value of attribute db_path.
Class Method Summary collapse
- .connected? ⇒ Boolean
- .model(table_name) ⇒ Object
- .register_migrations(name:, path:) ⇒ Object
- .registered_migrations ⇒ Object
- .reset! ⇒ Object
- .setup(database: nil) ⇒ Object
- .shutdown ⇒ Object
- .stats ⇒ Object
Methods included from Logging::Helper
Class Attribute Details
.connection ⇒ Object (readonly)
Returns the value of attribute connection.
15 16 17 |
# File 'lib/legion/data/local.rb', line 15 def connection @connection end |
.db_path ⇒ Object (readonly)
Returns the value of attribute db_path.
15 16 17 |
# File 'lib/legion/data/local.rb', line 15 def db_path @db_path end |
Class Method Details
.connected? ⇒ Boolean
67 68 69 |
# File 'lib/legion/data/local.rb', line 67 def connected? @connected == true end |
.model(table_name) ⇒ Object
81 82 83 84 85 |
# File 'lib/legion/data/local.rb', line 81 def model(table_name) raise 'Legion::Data::Local not connected' unless connected? ::Sequel::Model(connection[table_name]) end |
.register_migrations(name:, path:) ⇒ Object
71 72 73 74 75 |
# File 'lib/legion/data/local.rb', line 71 def register_migrations(name:, path:) @registered_migrations ||= {} @registered_migrations[name] = path run_single_migration(name, path) if connected? end |
.registered_migrations ⇒ Object
77 78 79 |
# File 'lib/legion/data/local.rb', line 77 def registered_migrations @registered_migrations || {} end |
.reset! ⇒ Object
120 121 122 123 124 125 |
# File 'lib/legion/data/local.rb', line 120 def reset! @connection = nil @connected = false @db_path = nil @registered_migrations = nil end |
.setup(database: nil) ⇒ Object
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 49 50 51 52 53 54 55 56 57 |
# File 'lib/legion/data/local.rb', line 17 def setup(database: nil, **) return if @connected db_file = database || local_settings[:database] || 'legionio_local.db' unless File.absolute_path?(db_file) base_dir = File.('~/.legionio') FileUtils.mkdir_p(base_dir) db_file = File.join(base_dir, db_file) end @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) @connection.run('PRAGMA journal_mode=WAL') @connection.run('PRAGMA busy_timeout=30000') @connection.run('PRAGMA synchronous=NORMAL') @connected = true run_migrations log.info "Legion::Data::Local connected to #{db_file} (WAL mode, 30s busy_timeout)" rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: :local_setup, database: db_file) raise end |
.shutdown ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/legion/data/local.rb', line 59 def shutdown @connection&.disconnect @query_file_logger&.close @query_file_logger = nil @connection = nil @connected = false end |
.stats ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/legion/data/local.rb', line 87 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. } end |