Module: OllamaChat::Database

Defined in:
lib/ollama_chat/database.rb

Overview

The OllamaChat::Database module is responsible for managing the application's persistence layer, including the initialization of the database connection and the dynamic loading of models.

Defined Under Namespace

Modules: Duplicatable, Models, SessionLocking

Class Method Summary collapse

Class Method Details

.setup_modelsObject

Bootstraps the database layer by establishing a connection, running migrations, and dynamically loading all models.

This method performs the following sequence:

  1. Identifies the SQLite database path within the XDG state directory.
  2. Establishes a connection using Sequel and assigns it to the OllamaChat::DB constant.
  3. Executes the database migrations via OllamaChat::Database::Models::Migrations.run.
  4. Iteratively requires all Ruby files found in the database/models/ directory, ensuring the migration file itself is skipped.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ollama_chat/database.rb', line 27

def self.setup_models
  db_path = OC::XDG_STATE_HOME.join('settings.db').expand_path

  url = 'sqlite://' + db_path.to_path
  OC::OLLAMA::CHAT::DATABASE_LOGFILE.dirname.mkpath
  logger = Logger.new(OC::OLLAMA::CHAT::DATABASE_LOGFILE)
  unless OllamaChat.const_defined?('DB')
    OllamaChat.const_set('DB', Sequel.connect(url, logger:))
  end

  # Load and apply migrations first
  require_relative 'database/migrations'
  OllamaChat::Database::Models::Migrations.run(OllamaChat::DB)

  # Load the rest of the models
  Pathname.new(__dir__).glob('database/models/*.rb').map do |file|
    next if file.basename.to_s == 'migrations.rb'
    require file
    OllamaChat::Database::Models.const_get(
      Pathname.new(file).basename.sub_ext('').to_path.camelize
    )
  end
end