Module: IndexUtil::Database

Defined in:
lib/index_util/database.rb

Class Method Summary collapse

Class Method Details

.close_database_files(database_file) ⇒ Object



53
54
55
56
57
# File 'lib/index_util/database.rb', line 53

def close_database_files(database_file)
  return unless database_file

  FileUtils.mkdir_p(File.dirname(File.expand_path(database_file)))
end

.configure!(db) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/index_util/database.rb', line 27

def configure!(db)
  db.run "PRAGMA foreign_keys = ON"
  db.run "PRAGMA journal_mode = WAL"
  db.run "PRAGMA synchronous = NORMAL"
  verify_fts5!(db)
  VectorIndex.load!(db)
  db
end

.connect(database_file) ⇒ Object



12
13
14
15
16
17
# File 'lib/index_util/database.rb', line 12

def connect(database_file)
  db = Sequel.sqlite(database_file)
  configure!(db)
  migrate!(db)
  db
end

.migrate!(db) ⇒ Object



36
37
38
39
40
# File 'lib/index_util/database.rb', line 36

def migrate!(db)
  Sequel::Migrator.run(db, migrations_path)
rescue Sequel::Migrator::Error => e
  raise Error, "Unsupported index schema migration state: #{e.message}"
end

.migrations_pathObject



42
43
44
# File 'lib/index_util/database.rb', line 42

def migrations_path
  File.expand_path("migrations", __dir__)
end

.rebuild!(database_file) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/index_util/database.rb', line 19

def rebuild!(database_file)
  close_database_files(database_file)
  FileUtils.rm_f(database_file)
  FileUtils.rm_f("#{database_file}-wal")
  FileUtils.rm_f("#{database_file}-shm")
  connect(database_file)
end

.verify_fts5!(db) ⇒ Object



46
47
48
49
50
51
# File 'lib/index_util/database.rb', line 46

def verify_fts5!(db)
  db.run "CREATE VIRTUAL TABLE temp.index_util_fts5_check USING fts5(content)"
  db.run "DROP TABLE temp.index_util_fts5_check"
rescue Sequel::DatabaseError => e
  raise Error, "SQLite FTS5 support is required by index_util: #{e.message}"
end