Skip to content

Commit d7db191

Browse files
authored
Merge pull request #38 from nativeapptemplate/fix/shopkeeper-deletion-foreign-key-violation
Fix shopkeeper deletion FK violation on item_tags
2 parents d21f061 + 0eff004 commit d7db191

5 files changed

Lines changed: 153 additions & 66 deletions

File tree

app/models/shopkeeper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Shopkeeper < ApplicationRecord
1010
has_many :shops, through: :accounts
1111
has_many :item_tags, through: :shops
1212
has_many :created_shops, class_name: "Shop", foreign_key: :created_by_id, inverse_of: :created_by
13-
has_many :created_item_tags, class_name: "ItemTag", foreign_key: :created_by_id, inverse_of: :created_by
14-
has_many :completed_item_tags, class_name: "ItemTag", foreign_key: :completed_by_id, inverse_of: :completed_by
13+
has_many :created_item_tags, class_name: "ItemTag", foreign_key: :created_by_id, inverse_of: :created_by, dependent: :nullify
14+
has_many :completed_item_tags, class_name: "ItemTag", foreign_key: :completed_by_id, inverse_of: :completed_by, dependent: :nullify
1515

1616
attribute :token, :string
1717
attribute :client, :string
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class ChangeShopkeeperForeignKeysToNullifyOnDelete < ActiveRecord::Migration[8.1]
2+
def up
3+
remove_foreign_key :item_tags, column: :completed_by_id
4+
remove_foreign_key :item_tags, column: :created_by_id
5+
6+
add_foreign_key :item_tags, :shopkeepers, column: :completed_by_id, on_delete: :nullify
7+
add_foreign_key :item_tags, :shopkeepers, column: :created_by_id, on_delete: :nullify
8+
end
9+
10+
def down
11+
remove_foreign_key :item_tags, column: :completed_by_id
12+
remove_foreign_key :item_tags, column: :created_by_id
13+
14+
add_foreign_key :item_tags, :shopkeepers, column: :completed_by_id
15+
add_foreign_key :item_tags, :shopkeepers, column: :created_by_id
16+
end
17+
end

db/schema.rb

Lines changed: 64 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/controllers/shopkeeper_auth/registrations_controller_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ class ShopkeeperAuth::RegistrationsControllerTest < ActionDispatch::IntegrationT
3030
end
3131
end
3232

33+
test "delete current shopkeeper with item_tags" do
34+
shopkeeper.create_default_account
35+
account = shopkeeper.accounts.first
36+
37+
ActsAsTenant.with_tenant(account) do
38+
shop = account.shops.create!(name: "Test Shop", created_by: shopkeeper)
39+
shop.item_tags.create!(queue_number: "A1", account: account, completed_by: shopkeeper)
40+
end
41+
42+
assert_difference "Shopkeeper.count", -1 do
43+
delete shopkeeper_registration_url, headers: shopkeeper.create_new_auth_token
44+
assert_response :success
45+
end
46+
end
47+
3348
def shopkeeper
3449
@shopkeeper ||= shopkeepers(:one)
3550
end

test/models/shopkeeper_test.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,59 @@ class ShopkeeperTest < ActiveSupport::TestCase
213213
shopkeeper.destroy
214214
end
215215
end
216+
217+
test "should nullify item_tags references on destroy" do
218+
shopkeeper = shopkeepers(:one)
219+
shopkeeper.create_default_account
220+
other_shopkeeper = shopkeepers(:two)
221+
other_shopkeeper.create_default_account
222+
other_account = other_shopkeeper.accounts.first
223+
224+
item_tag = ActsAsTenant.with_tenant(other_account) do
225+
shop = other_account.shops.create!(name: "Other Shop", created_by: other_shopkeeper)
226+
shop.item_tags.create!(
227+
queue_number: "A1",
228+
account: other_account,
229+
completed_by: shopkeeper
230+
)
231+
end
232+
233+
ActsAsTenant.without_tenant do
234+
shopkeeper.destroy
235+
end
236+
237+
ActsAsTenant.with_tenant(other_account) do
238+
item_tag.reload
239+
assert_nil item_tag.completed_by_id
240+
assert ItemTag.exists?(item_tag.id)
241+
end
242+
end
243+
244+
test "should successfully destroy shopkeeper with item_tags in other accounts" do
245+
shopkeeper = shopkeepers(:one)
246+
shopkeeper.create_default_account
247+
other_shopkeeper = shopkeepers(:two)
248+
other_shopkeeper.create_default_account
249+
other_account = other_shopkeeper.accounts.first
250+
251+
item_tag = ActsAsTenant.with_tenant(other_account) do
252+
shop = other_account.shops.create!(name: "Other Shop", created_by: other_shopkeeper)
253+
shop.item_tags.create!(
254+
queue_number: "B1",
255+
account: other_account,
256+
created_by: shopkeeper
257+
)
258+
end
259+
260+
ActsAsTenant.without_tenant do
261+
assert_nothing_raised do
262+
shopkeeper.destroy!
263+
end
264+
end
265+
266+
ActsAsTenant.with_tenant(other_account) do
267+
item_tag.reload
268+
assert_nil item_tag.created_by_id
269+
end
270+
end
216271
end

0 commit comments

Comments
 (0)