module ActiveGraph::Migrations::Helpers::IdProperty
Public Instance Methods
populate_id_property(label)
click to toggle source
# File lib/active_graph/migrations/helpers/id_property.rb 7 def populate_id_property(label) 8 model = label.to_s.constantize 9 max_per_batch = (ENV['MAX_PER_BATCH'] || default_max_per_batch).to_i 10 11 last_time_taken = nil 12 13 until (nodes_left = idless_count(label, model.primary_key)) == 0 14 print_status(last_time_taken, max_per_batch, nodes_left) 15 16 count = [nodes_left, max_per_batch].min 17 last_time_taken = Benchmark.realtime do 18 max_per_batch = id_batch_set(label, model.primary_key, Array.new(count) { new_id_for(model) }, count) 19 end 20 end 21 end
Protected Instance Methods
default_max_per_batch()
click to toggle source
# File lib/active_graph/migrations/helpers/id_property.rb 58 def default_max_per_batch 59 900 60 end
id_batch_set(label, id_property, new_ids, count)
click to toggle source
# File lib/active_graph/migrations/helpers/id_property.rb 29 def id_batch_set(label, id_property, new_ids, count) 30 ActiveGraph::Base.transaction do 31 execute("MATCH (n:`#{label}`) WHERE NOT EXISTS(n.#{id_property}) 32 with COLLECT(n) as nodes, #{new_ids} as ids 33 FOREACH(i in range(0,#{count - 1})| 34 FOREACH(node in [nodes[i]]| 35 SET node.#{id_property} = ids[i])) 36 RETURN distinct(true) 37 LIMIT #{count}") 38 count 39 end 40 rescue ActiveGraph::Server::CypherResponse::ResponseError 41 new_max_per_batch = (max_per_batch * 0.8).round 42 output "Error querying #{max_per_batch} nodes. Trying #{new_max_per_batch}" 43 new_max_per_batch 44 end
idless_count(label, id_property)
click to toggle source
# File lib/active_graph/migrations/helpers/id_property.rb 25 def idless_count(label, id_property) 26 query.match(n: label).where("NOT EXISTS(n.#{id_property})").pluck('COUNT(n) AS ids').first 27 end
new_id_for(model)
click to toggle source
# File lib/active_graph/migrations/helpers/id_property.rb 62 def new_id_for(model) 63 if model.id_property_info[:type][:auto] 64 SecureRandom.uuid 65 else 66 model.new.send(model.id_property_info[:type][:on]) 67 end 68 end
print_status(last_time_taken, max_per_batch, nodes_left)
click to toggle source
# File lib/active_graph/migrations/helpers/id_property.rb 46 def print_status(last_time_taken, max_per_batch, nodes_left) 47 time_per_node = last_time_taken / max_per_batch if last_time_taken 48 message = if time_per_node 49 eta_seconds = (nodes_left * time_per_node).round 50 "#{nodes_left} nodes left. Last batch: #{(time_per_node * 1000.0).round(1)}ms / node (ETA: #{eta_seconds / 60} minutes)" 51 else 52 'Running first batch...' 53 end 54 55 output message 56 end