Skip to content
This repository
Browse code

Fixed upgrade_v2 rake script

  • Loading branch information...
commit 69e5488892e0cbb5702da34a126de1d1cd196e01 1 parent 799cdf9
Andreas Ronge authored May 02, 2012
4  bin/neo4j-upgrade
@@ -7,7 +7,7 @@ if ARGV.length == 0
7 7
   puts <<TEXT
8 8
 A tool for upgrading the neo4j database to #{Neo4j::VERSION}.
9 9
 Make sure you shutdown the database cleanly before running this script.
10  
-More info:  http://neo4j.rubyforge.org/guides/configuration.html#upgrade-to-a-new-neo4j-version
  10
+More info: https://github.com/andreasronge/neo4j/wiki
11 11
 
12 12
 Usage   : neo4j-upgrade <location of database>
13 13
 Example : neo4j-upgrade db/neo4j-development
@@ -69,4 +69,4 @@ else
69 69
   puts("No lucene index for relationship found at location #{nodes_path}")
70 70
 end
71 71
 
72  
-puts "Upgraded database done at '#{root}'"
  72
+puts "Upgraded database done at '#{root}'"
2  lib/neo4j/tasks/neo4j.rb
... ...
@@ -0,0 +1,2 @@
  1
+Dir["#{File.dirname(__FILE__)}/*/*.rake"].each { |f| load f }
  2
+#require File.join(File.dirname(__FILE__), "upgrade_v2", "upgrade_v2")
62  lib/tasks/upgrade_v2/lib/upgrade_v2.rb → lib/neo4j/tasks/upgrade_v2/lib/upgrade_v2.rb
... ...
@@ -1,43 +1,35 @@
1  
-# A script for renaming relationship
2  
-# In Ne4j.rb version 2.0.0 the relationship declared with has_n(something).to(otherclass)
3  
-# has changed. In order to change the relationship of an already existing database you can run this script.
4  
-# Use the migrate_all! to automatically update all Neo4j::Rails::Model classes
5  
-# For Neo4j::NodeMixin nodes you need to migrate each node using the migrate method (since there are no automatic way of finding all instances of an Neo4j::NodeMixin)
6  
-
7  
-$NEO4J_CLASSES = []
8  
-$DOMAIN_CLASSES = []
9  
-
10  
-module Neo4j
11  
-  module Rails
12  
-    class Model
13  
-      class << self
14  
-        alias_method :_old_inherited, :inherited
15  
-
16  
-        def inherited(c)
17  
-          _old_inherited(c)
18  
-          unless c == Neo4j::Rails::Model
19  
-            $NEO4J_CLASSES << c
20  
-            $DOMAIN_CLASSES << c if c.respond_to?(:ref_node_for_class)
21  
-          end
22  
-        end
23  
-      end
24  
-    end
25  
-  end
26  
-end
27  
-
28 1
 module Neo4j
29  
-  module UpgradeV2 #:nodoc:
  2
+  # A script for renaming relationship
  3
+  # In Neo4j.rb version 2.0.0 the relationship declared with has_n(something).to(otherclass)
  4
+  # has changed. In order to change the relationship of an already existing database you can run this script.
  5
+  # Uses the @NEO4J_CLASSES@ and @NEO4J_MULTI_TENANCY_CLASSES@ environment variables.
  6
+  # Usage see the github wiki pages upgrade
  7
+  module UpgradeV2
30 8
 
31 9
     class << self
32 10
 
33 11
       # The domain classes declared as global ref_nodes
34 12
       def default_domains_nodes
35 13
         domain_nodes = [Neo4j.default_ref_node]
36  
-        $DOMAIN_CLASSES.each{|clazz| domain_nodes += clazz._all.to_a}
  14
+        multi_tenancy_classes.each{|clazz| domain_nodes += clazz._all.to_a}
37 15
         domain_nodes
38 16
       end
39 17
 
  18
+      def neo4j_classes
  19
+        abort("Please set the environment variable 'NEO4J_CLASSES' or ruby constant ($NEO4J_CLASSES) before running this task") unless ENV['NEO4J_CLASSES']
  20
+        ENV['NEO4J_CLASSES'].split(',').map(&:strip).map{|c| Neo4j::Wrapper.to_class(c)}
  21
+      end
  22
+
  23
+      def multi_tenancy_classes
  24
+        (ENV['NEO4J_MULTI_TENANCY_CLASSES'] && ENV['NEO4J_MULTI_TENANCY_CLASSES'].split(',').map(&:strip).map{|c| Neo4j::Wrapper.to_class(c)}) || []
  25
+      end
  26
+
40 27
       def migrate_all!(domains = default_domains_nodes)
  28
+        $NEO4J_CLASSES ||= neo4j_classes
  29
+
  30
+        puts "Upgrading #{$NEO4J_CLASSES.join(', ')}"
  31
+        puts "Number of domains (multitenancy)  #{domains.size}"
  32
+
41 33
         domains.each do |domain|
42 34
           puts "domain #{domain.props.inspect} (multitenancy) "  if domain != Neo4j.default_ref_node
43 35
           ::Neo4j.threadlocal_ref_node = domain
@@ -46,9 +38,19 @@ def migrate_all!(domains = default_domains_nodes)
46 38
       end
47 39
 
48 40
       def migrate(clazz, nodes = clazz._all)
49  
-        puts "migrate #{clazz} ..."
  41
+        puts "migrate 2.0 #{clazz} ..."
50 42
         source_class = clazz
51 43
 
  44
+        rule_node =  Neo4j::Wrapper::Rule::Rule.rule_node_for(clazz).rule_node
  45
+
  46
+        if rule_node.property?(:_count__all__classname)
  47
+          puts "Rename size property on #{clazz}, #{rule_node[:_count__all__classname]}"
  48
+          Neo4j::Transaction.run do
  49
+            rule_node[:_size__all__classname] = rule_node[:_count__all__classname]
  50
+            rule_node[:_count__all__classname] = nil
  51
+          end
  52
+        end
  53
+
52 54
         clazz._decl_rels.keys.each do |rel_accessor|
53 55
           target_class = clazz._decl_rels[rel_accessor].target_class
54 56
           next unless target_class
2  lib/tasks/upgrade_v2/upgrade_v2.rake → lib/neo4j/tasks/upgrade_v2/upgrade_v2.rake
... ...
@@ -1,3 +1,5 @@
  1
+puts "LOADING UPGRADE SCRIPT Neo4j"
  2
+
1 3
 require File.join(File.dirname(__FILE__), "lib", "upgrade_v2")
2 4
 
3 5
 namespace(:neo4j) do

0 notes on commit 69e5488

Please sign in to comment.
Something went wrong with that request. Please try again.