forked from tobymao/18xx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_names.rb
36 lines (31 loc) · 1.04 KB
/
migrate_names.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true
# rubocop:disable all
require_relative 'models'
Dir['./models/**/*.rb'].sort.each { |file| require file }
Sequel.extension :pg_json_ops
require_relative 'lib/engine'
$failed = []
def migrate_db_actions(data)
players = data.players.map { |p| [p[:name], p[:id].to_s] }.to_h
if data.settings['players']
data.settings['players'] = data.settings['players'].map {|p, h| [players[p] || p, h]}.to_h
data.save
end
end
def migrate_one(id)
DB[:games].order(:id).where(id: id).select(:id).paged_each(rows_per_fetch: 1) do |game|
games = Game.eager(:user, :players).where(id: [game[:id]]).all
games.each {|data|
migrate_db_actions(data)
}
end
end
def migrate_all()
DB[:games].order(Sequel.desc(:id)).where(Sequel.pg_jsonb_op(:settings).has_key?('pin') => false, status: %w[active finished]).select(:id).paged_each(rows_per_fetch: 1) do |game|
puts game[:id]
games = Game.eager(:user, :players).where(id: [game[:id]]).all
games.each do |data|
migrate_db_actions(data)
end
end
end