lib/neo4j/migration.rb in neo4j-6.0.0.alpha.11 vs lib/neo4j/migration.rb in neo4j-6.0.0.alpha.12
- old
+ new
@@ -121,99 +121,7 @@
else
model.new.send(model.id_property_info[:type][:on])
end
end
end
-
- class AddClassnames < Neo4j::Migration
- def initialize(path = default_path)
- @classnames_filename = 'add_classnames.yml'
- @classnames_filepath = File.join(joined_path(path), classnames_filename)
- end
-
- def migrate
- output 'Adding classnames. This make take some time.'
- execute(true)
- end
-
- def test
- output 'TESTING! No queries will be executed.'
- execute(false)
- end
-
- def setup
- output "Creating file #{classnames_filepath}. Please use this as the migration guide."
- FileUtils.mkdir_p('db/neo4j-migrate')
-
- return if File.file?(classnames_filepath)
-
- source = File.join(File.dirname(__FILE__), '..', '..', 'config', 'neo4j', classnames_filename)
- FileUtils.copy_file(source, classnames_filepath)
- end
-
- private
-
- attr_reader :classnames_filename, :classnames_filepath, :model_map
-
- def execute(migrate = false)
- file_init
- map = []
- map.push :nodes if model_map[:nodes]
- map.push :relationships if model_map[:relationships]
- map.each do |type|
- model_map[type].each do |action, labels|
- do_classnames(action, labels, type, migrate)
- end
- end
- end
-
- def do_classnames(action, labels, type, migrate = false)
- method = type == :nodes ? :node_cypher : :rel_cypher
- labels.each do |label|
- output cypher = self.send(method, label, action)
- execute_cypher(cypher) if migrate
- end
- end
-
- def file_init
- @model_map = ActiveSupport::HashWithIndifferentAccess.new(YAML.load_file(classnames_filepath))
- end
-
- def node_cypher(label, action)
- where, phrase_start = action_variables(action, 'n')
- output "#{phrase_start} _classname '#{label}' on nodes with matching label:"
- "MATCH (n:`#{label}`) #{where} SET n._classname = '#{label}' RETURN COUNT(n) as modified"
- end
-
- def rel_cypher(hash, action)
- label = hash[0]
- value = hash[1]
- from = value[:from]
- fail "All relationships require a 'type'" unless value[:type]
-
- from_cypher = from ? "(from:`#{from}`)" : '(from)'
- to = value[:to]
- to_cypher = to ? "(to:`#{to}`)" : '(to)'
- type = "[r:`#{value[:type]}`]"
- where, phrase_start = action_variables(action, 'r')
- output "#{phrase_start} _classname '#{label}' where type is '#{value[:type]}' using cypher:"
- "MATCH #{from_cypher}-#{type}->#{to_cypher} #{where} SET r._classname = '#{label}' return COUNT(r) as modified"
- end
-
- def execute_cypher(query_string)
- output "Modified #{Neo4j::Session.query(query_string).first.modified} records"
- output ''
- end
-
- def action_variables(action, identifier)
- case action
- when 'overwrite'
- ['', 'Overwriting']
- when 'add'
- ["WHERE NOT HAS(#{identifier}._classname)", 'Adding']
- else
- fail "Invalid action #{action} specified"
- end
- end
- end
end
end