From b31c77459876be3923ab34a41978df5bea62b978 Mon Sep 17 00:00:00 2001 From: lpichler Date: Tue, 11 Sep 2018 14:41:18 +0200 Subject: [PATCH] Create script for mapped tags --- tools/convert_mapped_tags.rb | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 tools/convert_mapped_tags.rb diff --git a/tools/convert_mapped_tags.rb b/tools/convert_mapped_tags.rb new file mode 100755 index 000000000000..33fad59bfca9 --- /dev/null +++ b/tools/convert_mapped_tags.rb @@ -0,0 +1,57 @@ +#!/usr/bin/env ruby +require File.expand_path('../config/environment', __dir__) + +read_only = (ARGV[0] != 'WRITE') + +puts +puts + +if read_only + puts "READ ONLY MODE" +else + puts "WRITE MODE" +end + +puts + +condition_for_mapped_tags = ContainerLabelTagMapping::TAG_PREFIXES.map { "tags.name LIKE ?" }.join(' OR ') +tag_values = ContainerLabelTagMapping::TAG_PREFIXES.map { |x| "#{x}%:%" } + +Classification.where.not(:id => Classification.region_to_range) # only other regions(not current, we expected that current region is global) + .where(:classifications => {:parent_id => 0}) # only categories + .includes(:tag, :children).references(:tag, :children) + .where(condition_for_mapped_tags, *tag_values) # only mapped categories + .find_each do |category| + new_parent_category = Classification.in_my_region.find_by(:description => category.description) + + if new_parent_category + print "Using..." + else + new_parent_category = category.dup + new_parent_category.save unless read_only # create in current region (global region is expected), it will create also tag instance + print "Creating..." + end + + puts "parent category #{new_parent_category.description} with tag: #{new_parent_category.tag.name} - from region #{category.region_id} to region #{new_parent_category.region_id}" + + category.entries.each do |entry| + new_entry = Classification.in_my_region.find_by(:description => entry.description) + if new_entry + print "Using...." + else + new_entry = entry.dup + new_entry.parent_id = new_parent_category.id + new_entry.save unless read_only # it will create also tag instance + print "Creating..." + end + puts "entry category #{new_entry.description} of #{new_parent_category.description} - from region #{category.region_id} to region #{new_entry.region_id}" + end +end + +puts + +if read_only + puts "READ ONLY MODE - no changes have been applied" +else + puts "WRITE MODE - changes have been applied" +end