Skip to content

Commit

Permalink
Merge pull request #6205 from avalonmediasystem/delete_checkouts
Browse files Browse the repository at this point in the history
Delete checkouts when associated media object is deleted
  • Loading branch information
masaball authored Feb 24, 2025
2 parents 21bedc6 + a9c9ef1 commit e0bb92a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/media_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def destroy
# avoid calling destroy on each section since it calls save on parent media object
self.sections.each(&:delete)
Bookmark.where(document_id: self.id).destroy_all
Checkout.where(media_object_id: self.id).destroy_all
super
end

Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/avalon.rake
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ namespace :avalon do
ActiveEncode::EngineAdapters::FfmpegAdapter.remove_old_files!(options)
end

desc 'clean out orphaned checkout records'
task checkout_record_cleanup: :environment do
orphans = Checkout.all.select { |co| !MediaObject.exists?(co.media_object_id) }
orphans.destroy_all
end

namespace :services do
services = ["jetty", "felix", "delayed_job"]
desc "Start Avalon's dependent services"
Expand Down
5 changes: 5 additions & 0 deletions spec/models/media_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@
describe '#destroy' do
let(:media_object) { FactoryBot.create(:media_object, :with_master_file) }
let(:master_file) { media_object.sections.first }
let!(:checkouts) { FactoryBot.create_list(:checkout, 3, media_object_id: media_object.id) }

before do
allow(master_file).to receive(:stop_processing!)
Expand All @@ -531,6 +532,10 @@
expect { media_object.destroy }.to change { MasterFile.count }.from(2).to(0)
expect(MediaObject.exists?(media_object.id)).to be_falsey
end

it 'destroys related checkouts' do
expect { media_object.destroy }.to change { Checkout.where(media_object_id: media_object.id).count }.from(3).to(0)
end
end

context "dependent properties" do
Expand Down

0 comments on commit e0bb92a

Please sign in to comment.