Skip to content

Commit

Permalink
Make reference indexes tenant scoped
Browse files Browse the repository at this point in the history
Previously, some resources indexed their relationships with an index that was
global and did not include the tenant id. Remove custom_indexes and use the
index? option on the reference instead.

Migrations generated with:
mix ash_postgres.generate_migrations make_reference_indexes_tenant_scoped

Signed-off-by: Riccardo Binetti <[email protected]>
  • Loading branch information
rbino committed Oct 4, 2024
1 parent 6499c39 commit 0eb1717
Show file tree
Hide file tree
Showing 15 changed files with 2,017 additions and 30 deletions.
5 changes: 1 addition & 4 deletions backend/lib/edgehog/devices/device/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ defmodule Edgehog.Devices.Device do

references do
reference :realm,
index?: true,
on_delete: :nothing,
match_with: [tenant_id: :tenant_id],
match_type: :full
Expand All @@ -444,9 +445,5 @@ defmodule Edgehog.Devices.Device do
# Edgehog
reference :system_model_part_number, ignore?: true
end

custom_indexes do
index [:realm_id], all_tenants?: true, unique: false
end
end
end
5 changes: 1 addition & 4 deletions backend/lib/edgehog/devices/hardware_type_part_number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,11 @@ defmodule Edgehog.Devices.HardwareTypePartNumber do

references do
reference :hardware_type,
index?: true,
on_delete: :delete,
# hardware_type_id can be null, so match_type is :simple, not :full
match_type: :simple,
match_with: [tenant_id: :tenant_id]
end

custom_indexes do
index [:hardware_type_id], all_tenants?: true, unique: false
end
end
end
5 changes: 1 addition & 4 deletions backend/lib/edgehog/devices/system_model/system_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,10 @@ defmodule Edgehog.Devices.SystemModel do

references do
reference :hardware_type,
index?: true,
on_delete: :nothing,
match_type: :full,
match_with: [tenant_id: :tenant_id]
end

custom_indexes do
index [:hardware_type_id], all_tenants?: true, unique: false
end
end
end
5 changes: 1 addition & 4 deletions backend/lib/edgehog/devices/system_model_part_number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,11 @@ defmodule Edgehog.Devices.SystemModelPartNumber do

references do
reference :system_model,
index?: true,
on_delete: :delete,
# system_model_id can be null, so match_type is :simple, not :full
match_type: :simple,
match_with: [tenant_id: :tenant_id]
end

custom_indexes do
index [:system_model_id], all_tenants?: true, unique: false
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,10 @@ defmodule Edgehog.OSManagement.OTAOperation do

references do
reference :device,
index?: true,
on_delete: :nothing,
match_type: :full,
match_with: [tenant_id: :tenant_id]
end

custom_indexes do
index [:device_id], all_tenants?: true, unique: false
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,16 @@ defmodule Edgehog.UpdateCampaigns.UpdateCampaign do

references do
reference :base_image,
index?: true,
on_delete: :nothing,
match_type: :full,
match_with: [tenant_id: :tenant_id]

reference :update_channel,
index?: true,
on_delete: :nothing,
match_type: :full,
match_with: [tenant_id: :tenant_id]
end

custom_indexes do
index [:base_image_id], unique: false
index [:update_channel_id], unique: false
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ defmodule Edgehog.UpdateCampaigns.UpdateTarget do

references do
reference :update_campaign,
index?: true,
on_delete: :delete,
match_type: :full,
match_with: [tenant_id: :tenant_id]

reference :device,
index?: true,
on_delete: :nothing,
match_type: :full,
match_with: [tenant_id: :tenant_id]
Expand All @@ -239,10 +241,5 @@ defmodule Edgehog.UpdateCampaigns.UpdateTarget do
on_delete: :nothing,
match_with: [tenant_id: :tenant_id]
end

custom_indexes do
index [:device_id], all_tenants?: true, unique: false
index [:update_campaign_id], all_tenants?: true, unique: false
end
end
end
Loading

0 comments on commit 0eb1717

Please sign in to comment.