Class: OllamaAgent::Runtime::SchemaMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/runtime/schema_migrator.rb

Overview

Applies versioned SQL migrations under db/ollama_agent/migrations/ to kernel SQLite files.

Constant Summary collapse

SCHEMA_LINE =
/^-- ### (\S+)\s*$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_registry:) ⇒ SchemaMigrator

Returns a new instance of SchemaMigrator.



52
53
54
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 52

def initialize(db_registry:)
  @db_registry = db_registry
end

Class Method Details

.max_applied_version(path) ⇒ Integer

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength – defensive SQLite open

Returns:

  • (Integer)

    highest recorded version for an on-disk database file



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 74

def self.max_applied_version(path)
  return 0 unless File.file?(path)

  db = SQLite3::Database.new(path)
  db.results_as_hash = true
  return 0 unless migrations_table?(db)

  row = db.get_first_row("SELECT MAX(version) AS v FROM schema_migrations")
  v = row && (row["v"] || row[:v])
  Integer(v || 0)
rescue SQLite3::Exception
  0
ensure
  db&.close
end

.migration_filesObject



17
18
19
20
21
22
23
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 17

def migration_files
  return [] unless File.directory?(migrations_dir)

  Dir.children(migrations_dir).grep(/\.sql\z/).sort_by do |name|
    Integer(name[/\A(\d+)_/, 1])
  end
end

.migration_versionsObject



25
26
27
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 25

def migration_versions
  migration_files.map { |f| Integer(f[/\A(\d+)_/, 1]) }
end

.migrations_dirObject



13
14
15
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 13

def migrations_dir
  File.join(OllamaAgent.gem_root, "db", "ollama_agent", "migrations")
end

.split_for_role(contents, role) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength – line-oriented section parser



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 30

def split_for_role(contents, role)
  wanted = role == :event_store ? "event_store.db" : "runtime.db"
  sections = {}
  current = nil
  buffer = +""

  contents.each_line do |line|
    if (m = line.match(SCHEMA_LINE))
      sections[current] = buffer.strip if current
      buffer = +""
      current = m[1]
    else
      buffer << line
    end
  end
  sections[current] = buffer.strip if current

  sections.fetch(wanted, "").to_s.strip
end

Instance Method Details

#current_versionInteger

Returns highest migration version present on disk (0 when none).

Returns:

  • (Integer)

    highest migration version present on disk (0 when none)



57
58
59
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 57

def current_version
  self.class.migration_versions.max || 0
end

#migrate!Array<Integer>

Returns newly applied migration version numbers (unique, sorted).

Returns:

  • (Array<Integer>)

    newly applied migration version numbers (unique, sorted)



62
63
64
65
66
67
68
69
70
# File 'lib/ollama_agent/runtime/schema_migrator.rb', line 62

def migrate!
  FileUtils.mkdir_p(@db_registry.kernel_dir)
  applied = []
  event_path = File.join(@db_registry.kernel_dir, "event_store.db")
  runtime_path = File.join(@db_registry.kernel_dir, "runtime.db")
  migrate_one_database!(event_path, :event_store, applied)
  migrate_one_database!(runtime_path, :runtime, applied)
  applied.uniq.sort
end