Class: Familia::Migration::Registry
- Inherits:
-
Object
- Object
- Familia::Migration::Registry
- Defined in:
- lib/familia/migration/registry.rb
Overview
Registry provides Redis-backed tracking for migration state.
Storage Schema (Redis keys): #prefix:applied - Sorted Set (member=migration_id, score=timestamp) #prefix:metadata - Hash (field=migration_id, value=JSON metadata) #prefix:schema - Hash (field=model_name, value=schema_digest) #prefix:backup:id - Hash with TTL for rollback data
Instance Attribute Summary collapse
-
#prefix ⇒ String
readonly
The key prefix for all registry data.
Instance Method Summary collapse
-
#all_applied ⇒ Array<Hash>, Redis::Future
Get all applied migrations with their timestamps.
-
#applied?(migration_id) ⇒ Boolean, Redis::Future
Check if a migration has been applied.
-
#applied_at(migration_id) ⇒ Time, ...
Get the timestamp when a migration was applied.
-
#backup_field(migration_id, key, field, value) ⇒ Object
Store a backup of a field value for potential rollback.
-
#clear_backup(migration_id) ⇒ Object
Clear the backup data for a migration.
-
#client ⇒ Redis, Redis::PipelinedConnection
Get the Redis client: the one passed to #initialize, or else Familia.dbclient.
-
#initialize(redis: nil, prefix: nil) ⇒ Registry
constructor
Initialize a new Registry instance.
-
#metadata(migration_id) ⇒ Hash, ...
Get metadata for a specific migration.
-
#pending(all_migrations) ⇒ Array<Class>
Filter a list of migrations to only those not yet applied.
-
#record_applied(migration, stats = {}) ⇒ Object
Record that a migration has been applied.
-
#record_rollback(migration_id) ⇒ Object
Record that a migration has been rolled back.
-
#redis ⇒ Redis?
The Redis client for this registry: the one passed as +redis:+, or else the Familia.dbclient fallback #client resolved and kept outside a block (nil until then).
-
#restore_backup(migration_id) ⇒ Integer
Restore all backed up fields for a migration.
-
#schema_changed?(model_class) ⇒ Boolean
Check if the schema has changed for a model class.
-
#schema_digest(model_class) ⇒ String
Calculate the schema digest for a model class.
-
#schema_drift ⇒ Array<String>
Get a list of model classes with changed schemas.
-
#status(all_migrations) ⇒ Array<Hash>
Get the status of all migrations.
-
#store_schema(model_class) ⇒ Object
Store the current schema digest for a model class.
-
#stored_schema(model_class) ⇒ String, ...
Get the stored schema digest for a model class.
Constructor Details
Instance Attribute Details
#prefix ⇒ String (readonly)
Returns The key prefix for all registry data.
36 37 38 |
# File 'lib/familia/migration/registry.rb', line 36 def prefix @prefix end |
Instance Method Details
#all_applied ⇒ Array<Hash>, Redis::Future
Get all applied migrations with their timestamps.
108 109 110 |
# File 'lib/familia/migration/registry.rb', line 108 def all_applied applied_entries(client) end |
#applied?(migration_id) ⇒ Boolean, Redis::Future
Check if a migration has been applied.
85 86 87 |
# File 'lib/familia/migration/registry.rb', line 85 def applied?(migration_id) Familia.transform_reply(client.zscore(applied_key, migration_id.to_s)) { |score| !score.nil? } end |
#applied_at(migration_id) ⇒ Time, ...
Get the timestamp when a migration was applied.
96 97 98 99 100 |
# File 'lib/familia/migration/registry.rb', line 96 def applied_at(migration_id) Familia.transform_reply(client.zscore(applied_key, migration_id.to_s)) do |score| score.nil? ? nil : Time.at(score) end end |
#backup_field(migration_id, key, field, value) ⇒ Object
Store a backup of a field value for potential rollback.
313 314 315 316 317 318 |
# File 'lib/familia/migration/registry.rb', line 313 def backup_field(migration_id, key, field, value) bkey = backup_key(migration_id) conn = client conn.hset(bkey, "#{key}:#{field}", value) conn.expire(bkey, Familia::Migration.config.backup_ttl) end |
#clear_backup(migration_id) ⇒ Object
Clear the backup data for a migration.
354 355 356 |
# File 'lib/familia/migration/registry.rb', line 354 def clear_backup(migration_id) client.del(backup_key(migration_id)) end |
#client ⇒ Redis, Redis::PipelinedConnection
Get the Redis client: the one passed to #initialize, or else Familia.dbclient.
Outside a transaction or pipeline the fallback is resolved once and kept, so a registry reuses one connection across its method calls even when Familia.dbclient creates a new one per call. Inside a Familia transaction or pipeline block the fallback is the block's connection, returned without being kept: a kept one would keep queueing commands on it after the block completed. Each registry method resolves the client once and issues all of its commands on it.
The block's connection is on the logical database the block was opened on: inside a block every Familia.dbclient lookup returns it, whatever database is asked for. A registry used inside a block that a model with another logical_database opened (+Order.transaction+ with +logical_database 7+) reads and writes its keys in that database, and #applied? outside the block, on the registry's own database, does not see a #record_applied made there. Pass +redis:+ for a registry that must stay on its database inside such a block.
70 71 72 73 74 75 |
# File 'lib/familia/migration/registry.rb', line 70 def client return @redis if @redis return Familia.dbclient if Familia.transaction_or_pipeline? @default_client ||= Familia.dbclient end |
#metadata(migration_id) ⇒ Hash, ...
Get metadata for a specific migration.
138 139 140 |
# File 'lib/familia/migration/registry.rb', line 138 def (migration_id) (client, migration_id) end |
#pending(all_migrations) ⇒ Array<Class>
Filter a list of migrations to only those not yet applied.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/familia/migration/registry.rb', line 117 def pending(all_migrations) return [] if all_migrations.nil? || all_migrations.empty? conn = client assert_replies_available!(:pending, conn) # Batch fetch all applied migration IDs in a single Redis call applied_ids = conn.zrange(applied_key, 0, -1).to_set all_migrations.reject do |migration| migration_id = extract_migration_id(migration) applied_ids.include?(migration_id) end end |
#record_applied(migration, stats = {}) ⇒ Object
Record that a migration has been applied.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/familia/migration/registry.rb', line 182 def record_applied(migration, stats = {}) migration_id = extract_migration_id(migration) now = Time.now conn = client # ZADD to applied set with current timestamp conn.zadd(applied_key, now.to_f, migration_id) # Build metadata = { status: 'applied', applied_at: now.iso8601, duration_ms: stats[:duration_ms] || 0, keys_scanned: stats[:keys_scanned] || 0, keys_modified: stats[:keys_modified] || 0, errors: stats[:errors] || 0, reversible: stats[:reversible] || false, } # HSET metadata JSON conn.hset(, migration_id, JSON.generate()) end |
#record_rollback(migration_id) ⇒ Object
Record that a migration has been rolled back.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/familia/migration/registry.rb', line 209 def record_rollback(migration_id) conn = client assert_replies_available!(:record_rollback, conn) migration_id = migration_id.to_s # Remove from applied set conn.zrem(applied_key, migration_id) # Update metadata to show rolled_back status existing = (conn, migration_id) = existing || {} [:status] = 'rolled_back' [:rolled_back_at] = Time.now.iso8601 conn.hset(, migration_id, JSON.generate()) end |
#redis ⇒ Redis?
The Redis client for this registry: the one passed as +redis:+, or else the Familia.dbclient fallback #client resolved and kept outside a block (nil until then). A transaction or pipeline connection is never returned, because #client does not keep one.
31 32 33 |
# File 'lib/familia/migration/registry.rb', line 31 def redis @redis || @default_client end |
#restore_backup(migration_id) ⇒ Integer
Restore all backed up fields for a migration.
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/familia/migration/registry.rb', line 325 def restore_backup(migration_id) conn = client assert_replies_available!(:restore_backup, conn) bkey = backup_key(migration_id) backup_data = conn.hgetall(bkey) return 0 if backup_data.empty? count = 0 backup_data.each do |composite_key, value| # Parse "redis_key:field_name" format # Note: field_name might contain colons, so we only split on the last colon parts = composite_key.rpartition(':') redis_key = parts[0] field_name = parts[2] next if redis_key.empty? || field_name.empty? conn.hset(redis_key, field_name, value) count += 1 end count end |
#schema_changed?(model_class) ⇒ Boolean
Check if the schema has changed for a model class.
270 271 272 273 274 275 276 277 |
# File 'lib/familia/migration/registry.rb', line 270 def schema_changed?(model_class) conn = client assert_replies_available!(:schema_changed?, conn) stored = read_stored_schema(conn, model_class) return false if stored.nil? # No stored schema = no drift stored != schema_digest(model_class) end |
#schema_digest(model_class) ⇒ String
Calculate the schema digest for a model class.
233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/familia/migration/registry.rb', line 233 def schema_digest(model_class) fields = model_class.fields.sort field_types = model_class.field_types field_strings = fields.map do |field| type = field_types[field] || 'unknown' "#{field}:#{type}" end Digest::SHA256.hexdigest(field_strings.join('|')) end |
#schema_drift ⇒ Array<String>
Get a list of model classes with changed schemas.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/familia/migration/registry.rb', line 283 def schema_drift conn = client assert_replies_available!(:schema_drift, conn) # Get all stored schemas stored = conn.hgetall(schema_key) return [] if stored.empty? drifted = [] stored.each do |model_name, stored_digest| # Try to find the model class model_class = find_model_class(model_name) next if model_class.nil? current_digest = schema_digest(model_class) drifted << model_name if stored_digest != current_digest end drifted end |
#status(all_migrations) ⇒ Array<Hash>
Get the status of all migrations.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/familia/migration/registry.rb', line 147 def status(all_migrations) return [] if all_migrations.nil? || all_migrations.empty? conn = client assert_replies_available!(:status, conn) # Batch fetch all applied migrations with timestamps in a single Redis call applied_info = applied_entries(conn).each_with_object({}) do |entry, hash| hash[entry[:migration_id]] = entry[:applied_at] end all_migrations.map do |migration| migration_id = extract_migration_id(migration) = applied_info[migration_id] { migration_id: migration_id, status: ? :applied : :pending, applied_at: , } end end |
#store_schema(model_class) ⇒ Object
Store the current schema digest for a model class.
249 250 251 252 253 |
# File 'lib/familia/migration/registry.rb', line 249 def store_schema(model_class) model_name = model_class.name || model_class.to_s digest = schema_digest(model_class) client.hset(schema_key, model_name, digest) end |
#stored_schema(model_class) ⇒ String, ...
Get the stored schema digest for a model class.
261 262 263 |
# File 'lib/familia/migration/registry.rb', line 261 def stored_schema(model_class) read_stored_schema(client, model_class) end |