Skip to content

Instantly share code, notes, and snippets.

@criess
Created April 29, 2021 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criess/8bc07ac52077b4830a0e61008cd485c4 to your computer and use it in GitHub Desktop.
Save criess/8bc07ac52077b4830a0e61008cd485c4 to your computer and use it in GitHub Desktop.
migration script for IN-1246
require 'csv'
require 'active_support'
require 'active_support/core_ext'
def command(json, id)
"curl -v -s -X PATCH " \
"-H 'Authorization: Bearer #{ENV['BUBBLE_API_KEY']}' " \
"-H 'Content-Type: application/json' " \
"-d '#{json}' " \
"'https://#{ENV['BUBBLE_API_HOST']}/api/1.1/obj/project/#{id}'"
end
MAPS = {
'Vorschlag Sub-Title': 'sub_title_text',
'Vorschlag Title': 'title_text'
}
def map_json(r)
r.to_h.select { |csv_head, val| csv_head.in?(MAPS.keys.map(&:to_s)) && val.present? }.to_h.map do |csv_head, val|
[MAPS[csv_head.to_sym], val]
end.to_h.to_json
end
def skip_row?(r)
h = r.to_h
h.select { |csv_head, val| csv_head.in?(MAPS.keys.map(&:to_s)) && val.blank? } ==
h.select { |csv_head, val| csv_head.in?(MAPS.keys.map(&:to_s)) }
end
iter = CSV(File.read(ARGV[0]), col_sep: ';', headers: true)
iter.map { |r| skip_row?(r) && r['unique id'] || nil }.compact.each do |x|
puts("# not updateing: project unique id ##{x} (`Vorschlag *` fields empty)")
end
iter.rewind
iter.each do |r|
next if skip_row?(r)
puts command(map_json(r), r['unique id'])
end
# invoke output with bash to actual run updates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment