Class: Synthra::SchemaVersioning::Registry
- Inherits:
-
Object
- Object
- Synthra::SchemaVersioning::Registry
- Defined in:
- lib/synthra/schema_versioning.rb
Overview
Version registry for tracking migrations
Class Method Summary collapse
- .clear ⇒ Object
- .get_migration(from, to) ⇒ Object
- .instance ⇒ Object
- .migration_path(from, to) ⇒ Object
- .register(from:, to:, changes: {}) ⇒ Object
- .versions_for(schema_name) ⇒ Object
Instance Method Summary collapse
- #clear ⇒ Object
-
#get_migration(from, to) ⇒ Hash?
Get migration between two versions.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#migration_path(from, to) ⇒ Array<Array>
Find migration path between versions.
-
#register(from:, to:, changes: {}) ⇒ Object
Register a migration from one version to another.
-
#versions_for(schema_name) ⇒ Array<String>
Get all versions for a schema.
Constructor Details
#initialize ⇒ Registry
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
.clear ⇒ Object
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 |
.instance ⇒ Object
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
#clear ⇒ Object
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
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
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
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
118 119 120 |
# File 'lib/synthra/schema_versioning.rb', line 118 def versions_for(schema_name) @mutex.synchronize { @versions[schema_name].sort } end |