diff --git a/db/migrate/20220707125124_add_foreign_key_to_user_organisation.rb b/db/migrate/20220707125124_add_foreign_key_to_user_organisation.rb deleted file mode 100644 index 54fd51a91..000000000 --- a/db/migrate/20220707125124_add_foreign_key_to_user_organisation.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddForeignKeyToUserOrganisation < ActiveRecord::Migration[7.0] - def change - add_reference :organisations, :user, foreign_key: true, on_delete: :cascade - end -end diff --git a/db/migrate/20220718163206_add_foreign_key_to_users.rb b/db/migrate/20220718163206_add_foreign_key_to_users.rb new file mode 100644 index 000000000..83339df30 --- /dev/null +++ b/db/migrate/20220718163206_add_foreign_key_to_users.rb @@ -0,0 +1,5 @@ +class AddForeignKeyToUsers < ActiveRecord::Migration[7.0] + def change + add_foreign_key :users, :organisations, on_delete: :cascade + end +end diff --git a/spec/features/organisation_spec.rb b/spec/features/organisation_spec.rb index a9f8b8f0e..322b29db0 100644 --- a/spec/features/organisation_spec.rb +++ b/spec/features/organisation_spec.rb @@ -225,20 +225,35 @@ RSpec.describe "User Features" do describe "delete cascade" do context "when the organisation is deleted" do - let!(:organisation) { user.organisation } - let!(:user) { FactoryBot.create(:user, :data_coordinator, last_sign_in_at: Time.zone.now) } + let!(:organisation) { FactoryBot.create(:organisation) } + let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation: organisation) } let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:log_to_delete) { FactoryBot.create(:case_log, owning_organisation: user.organisation) } context "when organisation is deleted" do - it "child relationships ie logs, schemes and users are deleted too" do - organisation.destroy + it "child relationships ie logs, schemes and users are deleted too - application" do + organisation.destroy! expect { organisation.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { CaseLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound) - expect { CaseLog.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound) - expect { CaseLog.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound) + expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound) + expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound) + end + + context "when the organisation is deleted" do + let!(:organisation) { FactoryBot.create(:organisation) } + let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation: organisation) } + let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } + let!(:log_to_delete) { FactoryBot.create(:case_log, owning_organisation: user.organisation) } + + + it "child relationships ie logs, schemes and users are deleted too - database" do + ActiveRecord::Base.connection.exec_query("DELETE FROM organisations WHERE id = #{organisation.id};") + expect { CaseLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound) + expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound) + expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound) end end + end end end end