Class: Lyra::Schema::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/lyra/schema/store.rb

Overview

Handles schema file persistence and versioning Schemas are stored in db/lyra_schemas/ (like AR migrations)

Constant Summary collapse

DEFAULT_PATH =
"db/lyra_schemas"

Class Method Summary collapse

Class Method Details

.ensure_directory! ⇒ Object



32
33
34
# File 'lib/lyra/schema/store.rb', line 32

def ensure_directory!
  FileUtils.mkdir_p(schema_path)
end

.exists? ⇒ Boolean

Check if any schema exists

Returns:

  • (Boolean)


108
109
110
# File 'lib/lyra/schema/store.rb', line 108

def exists?
  schema_path.exist? && schema_path.join("current.yml").exist?
end

.history ⇒ Object

Get version history with metadata



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lyra/schema/store.rb', line 85

def history
  return [] unless schema_path.exist?

  schema_path.glob("v*.yml")
    .reject { |f| f.basename.to_s == "current.yml" }
    .map do |file|
      schema = load_yaml(file)
      next nil unless schema

      {
        version: schema[:version],
        created_at: schema[:created_at],
        lyra_version: schema[:lyra_version],
        fingerprint: schema[:fingerprint],
        file: file.basename.to_s,
        models_count: schema.dig(:summary, :models_count)
      }
    end
    .compact
    .sort_by { |h| h[:version] }
end

.latest_version ⇒ Object



80
81
82
# File 'lib/lyra/schema/store.rb', line 80

def latest_version
  versions.max || 0
end

.load_current ⇒ Object

Load the current (latest) schema



37
38
39
40
41
42
# File 'lib/lyra/schema/store.rb', line 37

def load_current
  current_file = schema_path.join("current.yml")
  return nil unless current_file.exist?

  load_yaml(current_file)
end

.load_version(version) ⇒ Object

Load a specific version



45
46
47
48
49
50
# File 'lib/lyra/schema/store.rb', line 45

def load_version(version)
  file = find_version_file(version)
  return nil unless file&.exist?

  load_yaml(file)
end

.reset_path! ⇒ Object



28
29
30
# File 'lib/lyra/schema/store.rb', line 28

def reset_path!
  @schema_path = nil
end

.save(schema) ⇒ Object

Save a new schema version



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lyra/schema/store.rb', line 53

def save(schema)
  ensure_directory!

  version = schema[:version]
  timestamp = Time.current.strftime("%Y%m%d%H%M%S")
  filename = "v#{version}_#{timestamp}.yml"

  file_path = schema_path.join(filename)
  file_path.write(schema.deep_stringify_keys.to_yaml)

  # Update current.yml
  update_current(filename)

  file_path
end

.schema_path ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lyra/schema/store.rb', line 11

def schema_path
  return @schema_path if @schema_path

  custom_path = Lyra.config.schema_path
  base_path = custom_path || DEFAULT_PATH

  if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
    Rails.root.join(base_path)
  else
    Pathname.new(base_path)
  end
end

.schema_path=(path) ⇒ Object



24
25
26
# File 'lib/lyra/schema/store.rb', line 24

def schema_path=(path)
  @schema_path = path ? Pathname.new(path) : nil
end

.versions ⇒ Object

List all schema versions



70
71
72
73
74
75
76
77
78
# File 'lib/lyra/schema/store.rb', line 70

def versions
  return [] unless schema_path.exist?

  schema_path.glob("v*.yml")
    .reject { |f| f.basename.to_s == "current.yml" }
    .map { |f| extract_version(f) }
    .compact
    .sort
end