Class: Synthra::SchemaVersioning::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/schema_versioning.rb

Overview

Version registry for tracking migrations

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



76
77
78
79
80
# File 'lib/synthra/schema_versioning.rb', line 76

def initialize
  @migrations = {}
  @versions = Hash.new { |h, k| h[k] = [] }
  @mutex = Mutex.new
end

Class Method Details

.clearObject



71
72
73
# File 'lib/synthra/schema_versioning.rb', line 71

def clear
  instance.clear
end

.get_migration(from, to) ⇒ Object



59
60
61
# File 'lib/synthra/schema_versioning.rb', line 59

def get_migration(from, to)
  instance.get_migration(from, to)
end

.instanceObject



51
52
53
# File 'lib/synthra/schema_versioning.rb', line 51

def instance
  @instance ||= new
end

.migration_path(from, to) ⇒ Object



63
64
65
# File 'lib/synthra/schema_versioning.rb', line 63

def migration_path(from, to)
  instance.migration_path(from, to)
end

.register(from:, to:, changes: {}) ⇒ Object



55
56
57
# File 'lib/synthra/schema_versioning.rb', line 55

def register(from:, to:, changes: {})
  instance.register(from: from, to: to, changes: changes)
end

.versions_for(schema_name) ⇒ Object



67
68
69
# File 'lib/synthra/schema_versioning.rb', line 67

def versions_for(schema_name)
  instance.versions_for(schema_name)
end

Instance Method Details

#clearObject



152
153
154
155
156
157
# File 'lib/synthra/schema_versioning.rb', line 152

def clear
  @mutex.synchronize do
    @migrations.clear
    @versions.clear
  end
end

#get_migration(from, to) ⇒ Hash?

Get migration between two versions

Parameters:

  • from (String)

    source version

  • to (String)

    target version

Returns:

  • (Hash, nil)

    migration info



109
110
111
# File 'lib/synthra/schema_versioning.rb', line 109

def get_migration(from, to)
  @mutex.synchronize { @migrations[[from, to]] }
end

#migration_path(from, to) ⇒ Array<Array>

Find migration path between versions

Parameters:

  • from (String)

    source version

  • to (String)

    target version

Returns:

  • (Array<Array>)

    path of migrations



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/synthra/schema_versioning.rb', line 128

def migration_path(from, to)
  @mutex.synchronize do
    # Simple BFS to find path
    visited = Set.new
    queue = [[from, []]]

    while queue.any?
      current, path = queue.shift
      return path if current == to
      next if visited.include?(current)
      
      visited.add(current)
      
      @migrations.keys.each do |from_key, to_key|
        if from_key == current && !visited.include?(to_key)
          queue << [to_key, path + [[from_key, to_key]]]
        end
      end
    end

    nil # No path found
  end
end

#register(from:, to:, changes: {}) ⇒ Object

Register a migration from one version to another

Parameters:

  • from (String)

    source version (e.g., "User@v1")

  • to (String)

    target version (e.g., "User@v2")

  • changes (Hash) (defaults to: {})

    migration changes



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/synthra/schema_versioning.rb', line 88

def register(from:, to:, changes: {})
  @mutex.synchronize do
    @migrations[[from, to]] = changes
    
    # Track versions
    from_parsed = SchemaVersioning.parse_version(from)
    to_parsed = SchemaVersioning.parse_version(to)
    
    @versions[from_parsed[:name]] << from_parsed[:version]
    @versions[to_parsed[:name]] << to_parsed[:version]
    @versions[from_parsed[:name]].uniq!
    @versions[to_parsed[:name]].uniq!
  end
end

#versions_for(schema_name) ⇒ Array<String>

Get all versions for a schema

Parameters:

  • schema_name (String)

    schema name without version

Returns:

  • (Array<String>)

    versions



118
119
120
# File 'lib/synthra/schema_versioning.rb', line 118

def versions_for(schema_name)
  @mutex.synchronize { @versions[schema_name].sort }
end