Skip to content

Upserting dojo_event_services table #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 31, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/tasks/dojo_event_services.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace :dojo_event_services do
desc '現在のyamlファイルを元にデータベースを更新します'
task upsert: :environment do
puts 'Task as `dojo_event_services:upsert` starting...'

result = { inserted: [], updated: [], kept: [], skipped: [] }

list = YAML.load_file(Rails.root.join('db','dojo_event_services.yaml'))
list.each do |des|
unless DojoEventService.names.keys.include?(des['name'])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DojoEventService.namesで定義されているイベントサイトを使っていなければスキップする

event_names = DojoEventService.names.keys.map {|s| "`#{s}`" }
result[:skipped] << [des['dojo_name'], "Not used #{event_names.join(' or ')}"]
next
end

dojo = Dojo.find_by(name: des['dojo_name'])
unless dojo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dojosテーブルからレコードが引けなければスキップする

result[:skipped] << [des['dojo_name'], 'Not found record in `dojos` table']
next
end

insert = false
unless dojo.dojo_event_service
dojo.build_dojo_event_service
insert = true
end

dojo.dojo_event_service.assign_attributes(des.except('dojo_name'))
if dojo.dojo_event_service.changed?
changes = dojo.dojo_event_service.changes
dojo.dojo_event_service.save!
(insert ? result[:inserted] : result[:updated]) << [des['dojo_name'], changes]
else
result[:kept] << [des['dojo_name']]
end
end

# Dump result
sorted = result.sort_by {|_, v| v.length }.reverse.to_h
puts
sorted.each do |k, v|
puts "#{k.to_s.camelcase}: #{v.length}"
v.each {|x| puts " #{x.join(': ')}"}
end
end
end