Class: Flaky::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/flaky/database.rb

Constant Summary collapse

SCHEMA_VERSION =
1

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Database

Returns a new instance of Database.



9
10
11
# File 'lib/flaky/database.rb', line 9

def initialize(path = nil)
  @path = path || Flaky.configuration.resolved_db_path
end

Instance Method Details

#closeObject



26
27
28
29
# File 'lib/flaky/database.rb', line 26

def close
  @connection&.close
  @connection = nil
end

#connectionObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/flaky/database.rb', line 13

def connection
  @connection ||= begin
    dir = File.dirname(@path)
    FileUtils.mkdir_p(dir) unless File.directory?(dir)
    db = SQLite3::Database.new(@path)
    db.results_as_hash = true
    db.execute("PRAGMA journal_mode=WAL")
    db.execute("PRAGMA foreign_keys=ON")
    migrate!(db)
    db
  end
end