Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcff9cb
commit f320d95
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Neo4j | ||
class Migration | ||
class AddIdProperty < Neo4j::Migration | ||
attr_reader :models_filename | ||
|
||
def initialize | ||
@models_filename = File.join(Rails.root.join('db', 'neo4j-migrate'), 'add_id_property.yml') | ||
end | ||
|
||
def migrate | ||
models = ActiveSupport::HashWithIndifferentAccess.new(YAML.load_file(models_filename))[:models] | ||
puts "This task will add an ID Property every node in the given file." | ||
puts "It may take a significant amount of time, please be patient." | ||
models.each do |model| | ||
puts "Adding IDs to #{model}" | ||
add_ids_to model.constantize | ||
end | ||
end | ||
|
||
def setup | ||
FileUtils.mkdir_p("db/neo4j-migrate") | ||
unless File.file?(models_filename) | ||
File.open(models_filename, 'w') do |file| | ||
file.write("# Provide models to which IDs should be added.\n# It will only modify nodes that do not have IDs. There is no danger of overwriting data.\n# models: [Student,Lesson,Teacher,Exam]\nmodels: []") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def add_ids_to(model) | ||
label = model.mapped_label_name | ||
property = model.primary_key | ||
total = 1 | ||
|
||
until total == 0 | ||
total = Neo4j::Session.query("MATCH (n:`#{label}`) WHERE NOT has(n.#{property}) RETURN COUNT(n) as ids").first.ids | ||
return if total == 0 | ||
to_set = total > 900 ? 900 : total | ||
new_ids = [].tap do | ids_array| | ||
to_set.times { ids_array.push "'#{new_id_for(model)}'" } | ||
end | ||
Neo4j::Session.query("MATCH (n:`#{label}`) WHERE NOT has(n.#{property}) | ||
with COLLECT(n) as nodes, [#{new_ids.join(',')}] as ids | ||
FOREACH(i in range(0,#{to_set - 1})| | ||
FOREACH(node in [nodes[i]]| | ||
SET node.#{property} = ids[i])) | ||
RETURN distinct(true) | ||
limit #{to_set}") | ||
end | ||
end | ||
|
||
def new_id_for(model) | ||
if model.id_property_info[:type][:auto] | ||
SecureRandom::uuid | ||
else | ||
model.new.send(model.id_property_info[:type][:on]) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'neo4j/migration' | ||
|
||
namespace :neo4j do | ||
desc "Run a script against the database to perform system-wide changes" | ||
task :migrate, [:task_name, :subtask] => :environment do |_, args| | ||
migration_task = args[:task_name] | ||
task_name_constant = migration_task.split('_').map { |word| word.capitalize }.join('') | ||
begin | ||
migration_class = "Neo4j::Migration::#{task_name_constant}".constantize | ||
rescue NameError | ||
load File.join(Rails.root.join('db', 'neo4j-migrate'), "#{migration_task}.rb") | ||
migration_class = "#{task_name_constant}".constantize | ||
end | ||
migration = migration_class.new | ||
|
||
subtask = args[:subtask] | ||
if subtask | ||
migration.send(subtask) | ||
else | ||
migration.migrate | ||
end | ||
end | ||
end |