Skip to content

Commit

Permalink
feat: use task to update cell output script id
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <mingchang555@hotmail.com>
  • Loading branch information
zmcNotafraid committed Aug 23, 2024
1 parent 3179d30 commit d55f52d
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions lib/tasks/migration/update_cell_output_script_id.rake
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.

Check warning on line 6 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L4-L6

Added lines #L4 - L6 were not covered by tests
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

Check warning on line 14 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L11-L14

Added lines #L11 - L14 were not covered by tests
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)

Check warning on line 26 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L19-L26

Added lines #L19 - L26 were not covered by tests
end
end
end; nil

duplicate_script_hashes = LockScript.

Check warning on line 31 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L31

Added line #L31 was not covered by tests
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

Check warning on line 39 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L36-L39

Added lines #L36 - L39 were not covered by tests
end
end
end; nil

p "==============type scripts"
duplicate_type_script_hashes = TypeScript.

Check warning on line 45 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L44-L45

Added lines #L44 - L45 were not covered by tests
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

Check warning on line 54 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L51-L54

Added lines #L51 - L54 were not covered by tests
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)

Check warning on line 66 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L59-L66

Added lines #L59 - L66 were not covered by tests
end
end
end; nil

duplicate_type_script_hashes = TypeScript.

Check warning on line 71 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L71

Added line #L71 was not covered by tests
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

Check warning on line 80 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L77-L80

Added lines #L77 - L80 were not covered by tests
end
end
end; nil

puts "done"

Check warning on line 85 in lib/tasks/migration/update_cell_output_script_id.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/update_cell_output_script_id.rake#L85

Added line #L85 was not covered by tests
end
end

0 comments on commit d55f52d

Please sign in to comment.