-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: bind first lock script to cell_output Signed-off-by: Miles Zhang <[email protected]> * feat: use task to update cell output script id Signed-off-by: Miles Zhang <[email protected]> --------- Signed-off-by: Miles Zhang <[email protected]>
- Loading branch information
1 parent
855ce89
commit 77dd4d0
Showing
2 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
namespace :migration do | ||
desc "Usage: RAILS_ENV=production bundle exec rake migration:update_cell_output_script_id" | ||
task update_cell_output_script_id: :environment do | ||
ActiveRecord::Base.connection.execute("SET statement_timeout = 0") | ||
p "==============lock scripts" | ||
duplicate_script_hashes = LockScript. | ||
select(:script_hash). | ||
group(:script_hash). | ||
having("COUNT(*) > 1"). | ||
pluck(:script_hash) | ||
duplicate_script_hashes.each do |hash| | ||
LockScript.where(script_hash: hash).each do |script| | ||
unless CellOutput.live.where(lock_script_id: script.id).exists? | ||
script.destroy | ||
end | ||
end | ||
end; nil | ||
|
||
duplicate_script_hashes.each_with_index do |lock_script_hash, index| | ||
p lock_script_hash | ||
p index | ||
lock_script_ids = LockScript.where(script_hash: lock_script_hash).order("id asc").pluck(:id) | ||
base_lock_script_id = lock_script_ids.delete_at(0) | ||
lock_script_ids.each do |id| | ||
CellOutput.where(lock_script_id: id).in_batches(of: 10000) do |batch| | ||
batch.update_all(lock_script_id: base_lock_script_id) | ||
end | ||
end | ||
end; nil | ||
|
||
duplicate_script_hashes = LockScript. | ||
select(:script_hash). | ||
group(:script_hash). | ||
having("COUNT(*) > 1"). | ||
pluck(:script_hash) | ||
duplicate_script_hashes.each do |hash| | ||
LockScript.where(script_hash: hash).each do |script| | ||
unless CellOutput.live.where(lock_script_id: script.id).exists? | ||
script.destroy | ||
end | ||
end | ||
end; nil | ||
|
||
p "==============type scripts" | ||
duplicate_type_script_hashes = TypeScript. | ||
select(:script_hash). | ||
group(:script_hash). | ||
having("COUNT(*) > 1"). | ||
pluck(:script_hash) | ||
|
||
duplicate_type_script_hashes.each do |hash| | ||
TypeScript.where(script_hash: hash).each do |script| | ||
unless CellOutput.live.where(type_script_id: script.id).exists? | ||
script.destroy | ||
end | ||
end | ||
end; nil | ||
|
||
duplicate_type_script_hashes.each_with_index do |type_script_hash, index| | ||
p type_script_hash | ||
p index | ||
type_script_ids = TypeScript.where(script_hash: type_script_hash).order("id asc").pluck(:id) | ||
base_type_script_id = type_script_ids.delete_at(0) | ||
type_script_ids.each do |id| | ||
CellOutput.where(type_script_id: id).in_batches(of: 10000) do |batch| | ||
batch.update_all(type_script_id: base_type_script_id) | ||
end | ||
end | ||
end; nil | ||
|
||
duplicate_type_script_hashes = TypeScript. | ||
select(:script_hash). | ||
group(:script_hash). | ||
having("COUNT(*) > 1"). | ||
pluck(:script_hash) | ||
|
||
duplicate_type_script_hashes.each do |hash| | ||
TypeScript.where(script_hash: hash).each do |script| | ||
unless CellOutput.live.where(type_script_id: script.id).exists? | ||
script.destroy | ||
end | ||
end | ||
end; nil | ||
|
||
puts "done" | ||
end | ||
end |