Skip to content

Commit

Permalink
update verification migration with FK tags cascade delete
Browse files Browse the repository at this point in the history
add required dependants to tags model
add cascade delete test on tag spec
  • Loading branch information
andrew-1234 committed Feb 7, 2025
1 parent 7b4bb01 commit ab0638a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Tag < ApplicationRecord
extend Enumerize

# relations
has_many :taggings, inverse_of: :tag
has_many :taggings, inverse_of: :tag, dependent: :destroy
has_many :audio_events, through: :taggings
has_many :tag_groups, inverse_of: :tag
has_many :verifications, inverse_of: :tag
has_many :verifications, inverse_of: :tag, dependent: :destroy

belongs_to :creator, class_name: 'User', inverse_of: :created_tags
belongs_to :updater, class_name: 'User', inverse_of: :updated_tags, optional: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#
# fk_rails_... (audio_event_id => audio_events.id) ON DELETE => cascade
# fk_rails_... (creator_id => users.id)
# fk_rails_... (tag_id => tags.id)
# fk_rails_... (tag_id => tags.id) ON DELETE => cascade
# fk_rails_... (updater_id => users.id)
#
class Verification < ApplicationRecord
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20250120064731_create_verifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def change

create_table :verifications do |t|
t.references :audio_event, null: false, foreign_key: { on_delete: :cascade }
t.references :tag, null: false, foreign_key: true
t.references :tag, null: false, foreign_key: { on_delete: :cascade }
t.column :creator_id, :integer, null: false
t.column :updater_id, :integer
t.column :confirmed, :confirmation, null: false
Expand Down
6 changes: 3 additions & 3 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ CREATE TABLE public.audio_events (
channel integer,
provenance_id integer,
score numeric,
audio_event_import_file_id integer,
import_file_index integer
import_file_index integer,
audio_event_import_file_id bigint
);


Expand Down Expand Up @@ -3940,7 +3940,7 @@ ALTER TABLE ONLY public.audio_recording_statistics
--

ALTER TABLE ONLY public.verifications
ADD CONSTRAINT fk_rails_77cbfa06a3 FOREIGN KEY (tag_id) REFERENCES public.tags(id);
ADD CONSTRAINT fk_rails_77cbfa06a3 FOREIGN KEY (tag_id) REFERENCES public.tags(id) ON DELETE CASCADE;


--
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/verification_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#
# fk_rails_... (audio_event_id => audio_events.id) ON DELETE => cascade
# fk_rails_... (creator_id => users.id)
# fk_rails_... (tag_id => tags.id)
# fk_rails_... (tag_id => tags.id) ON DELETE => cascade
# fk_rails_... (updater_id => users.id)
#
FactoryBot.define do
Expand Down
7 changes: 7 additions & 0 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,11 @@
t.notes = { comment: 'testing' }
expect(t).to be_valid
end

it_behaves_like 'cascade deletes for', :tag, {
verifications: nil,
taggings: nil
} do
create_entire_hierarchy
end
end
2 changes: 1 addition & 1 deletion spec/models/verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#
# fk_rails_... (audio_event_id => audio_events.id) ON DELETE => cascade
# fk_rails_... (creator_id => users.id)
# fk_rails_... (tag_id => tags.id)
# fk_rails_... (tag_id => tags.id) ON DELETE => cascade
# fk_rails_... (updater_id => users.id)
#
describe Verification do
Expand Down

0 comments on commit ab0638a

Please sign in to comment.