script/fixes/123_refresh_media_metadata.rb: don't wrap in transaction.
Don't wrap the metadata refresh script in a transaction because it could be a very long running operation and it's not good to leave a transaction open that long.
This commit is contained in:
@@ -2,33 +2,31 @@
|
|||||||
|
|
||||||
require_relative "base"
|
require_relative "base"
|
||||||
|
|
||||||
with_confirmation do
|
condition = ENV.fetch("COND", "TRUE")
|
||||||
condition = ENV.fetch("COND", "TRUE")
|
fix = ENV.fetch("FIX", "false").truthy?
|
||||||
fix = ENV.fetch("FIX", "false").truthy?
|
|
||||||
|
|
||||||
MediaAsset.active.where(condition).find_each do |asset|
|
MediaAsset.active.where(condition).find_each do |asset|
|
||||||
variant = asset.variant(:original)
|
variant = asset.variant(:original)
|
||||||
media_file = variant.open_file
|
media_file = variant.open_file
|
||||||
|
|
||||||
if media_file.nil?
|
if media_file.nil?
|
||||||
puts ({ id: asset.id, error: "file doesn't exist", path: variant.file_path }).to_json
|
puts ({ id: asset.id, error: "file doesn't exist", path: variant.file_path }).to_json
|
||||||
next
|
next
|
||||||
end
|
|
||||||
|
|
||||||
# Setting `file` updates the metadata if it's different.
|
|
||||||
asset.file = media_file
|
|
||||||
asset.media_metadata.file = media_file
|
|
||||||
|
|
||||||
old = asset.media_metadata.metadata_was.to_h
|
|
||||||
new = asset.media_metadata.metadata.to_h
|
|
||||||
metadata_changes = { added_metadata: (new.to_a - old.to_a).to_h, removed_metadata: (old.to_a - new.to_a).to_h }.compact_blank
|
|
||||||
puts ({ id: asset.id, **asset.changes, **metadata_changes }).to_json
|
|
||||||
|
|
||||||
if fix
|
|
||||||
asset.save! if asset.changed?
|
|
||||||
asset.media_metadata.save! if asset.media_metadata.changed?
|
|
||||||
end
|
|
||||||
|
|
||||||
media_file.close
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Setting `file` updates the metadata if it's different.
|
||||||
|
asset.file = media_file
|
||||||
|
asset.media_metadata.file = media_file
|
||||||
|
|
||||||
|
old = asset.media_metadata.metadata_was.to_h
|
||||||
|
new = asset.media_metadata.metadata.to_h
|
||||||
|
metadata_changes = { added_metadata: (new.to_a - old.to_a).to_h, removed_metadata: (old.to_a - new.to_a).to_h }.compact_blank
|
||||||
|
puts ({ id: asset.id, **asset.changes, **metadata_changes }).to_json
|
||||||
|
|
||||||
|
if fix
|
||||||
|
asset.save! if asset.changed?
|
||||||
|
asset.media_metadata.save! if asset.media_metadata.changed?
|
||||||
|
end
|
||||||
|
|
||||||
|
media_file.close
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user