lib/trait_bank/admin.rb
# AGAIN! Use CAUTION. This is intended to delete all parent relationships between pages, and then rebuild them
# based on what's currently in the database. It skips relationships to pages that are missing (but reports on
# which those are), and it does not repeat any relationships. It takes a about a minute per 3000 nodes on jrice's
# machine.
AGAIN. Use CAUTION. This is intended to delete all parent relationships between pages, and then rebuild them
based on what's currently in the database. It skips relationships to pages that are missing (but reports on
which those are), and it does not repeat any relationships. It takes a about a minute per 3000 nodes on jrice's
machine.
AGAIN . Use CAUTION . This is intended to delete all parent relationships between pages and then rebuild them based on what ' s currently in the database . It skips relationships to pages that are missing ( but reports on which those are ) and it does not repeat any relationships . It takes a about a minute per 3000 nodes on jrice ' s machine .
def rebuild_hierarchies(remove_old = false)
        if remove_old
          remove_with_query(name: :parent, q: "(:Page)-[parent:parent]->(:Page)")
        end
        related = {}
        eol = Resource.native
        raise "I tried to use EOL as the native node for the relationships, but it wasn't there." unless eol
        nodes = Node.where(["resource_id = ? AND parent_id IS NOT NULL AND page_id IS NOT NULL", eol.id])
        count = nodes.count
        per_cent = count / 100
        i = 0
        dumb_log('Starting')
        nodes.includes(:parent).find_each do |node|
          i += 1
          dumb_log("Percent complete: #{((i / count.to_f) * 100).ceil}% (#{i}/#{count})") if (i % per_cent).zero?
          page_id = node.page_id
          next if node.parent.nil?
          parent_id = node.parent.page_id
          next if page_id == parent_id
          next if related.key?(page_id) 
          tries = 0
          begin
            res = TraitBank.query("MERGE (from_page:Page { page_id: #{page_id}}) "\
              "MERGE (to_page:Page { page_id: #{parent_id}}) "\
              "MERGE(from_page)-[:parent]->(to_page)")
          rescue
            tries += 1
            raise "Too many retries to relate page #{page_id} to its parent #{parent_id}!" if tries >= 10
            sleep(tries)
            retry
          end
          related[page_id] = parent_id
        end
        dumb_log('Done.')
      end
def rebuild_hierarchies ( remove_old = false ) if remove_old remove_with_query ( name : :parent , q : "(:Page)-[parent:parent]->(:Page)" ) end related = { } eol = Resource . native raise "I tried to use EOL as the native node for the relationships, but it wasn't there." unless eol nodes = Node . where ( [ "resource_id = ? AND parent_id IS NOT NULL AND page_id IS NOT NULL" , eol . id ] ) count = nodes . count per_cent = count / 100 i = 0 dumb_log ( 'Starting' ) nodes . includes ( :parent ) . find_each do | node | i += 1 dumb_log ( "Percent complete: #{((i / count.to_f) * 100).ceil}% (#{i}/#{count})" ) if ( i % per_cent ) . zero? page_id = node . page_id next if node . parent . nil? parent_id = node . parent . page_id next if page_id == parent_id next if related . key? ( page_id ) tries = 0 begin res = TraitBank . query ( "MERGE (from_page:Page { page_id: #{page_id}}) " "MERGE (to_page:Page { page_id: #{parent_id}}) " "MERGE(from_page)-[:parent]->(to_page)" ) rescue tries += 1 raise "Too many retries to relate page #{page_id} to its parent #{parent_id}!" if tries >= 10 sleep ( tries ) retry end related [ page_id ] = parent_id end dumb_log ( 'Done.' ) end
AGAIN!
AGAIN!
# Pages may only have ONE parent.
remove_old
<not_specific>