Skip to content

Commit 1e271ca

Browse files
committed
fix: deduplicate identity keys
fixes #602
1 parent 424c536 commit 1e271ca

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

lib/migration_generator/operation.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
130130

131131
def index_keys(keys, all_tenants?, multitenancy) do
132132
if multitenancy.strategy == :attribute and not all_tenants? do
133-
[multitenancy.attribute | keys]
133+
Enum.uniq([multitenancy.attribute | keys])
134134
else
135135
keys
136136
end

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%{
22
"ash": {:hex, :ash, "3.5.32", "ee717c49744374be7abe8997011115a4997917535e02d36146937fb6f89c19fe", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.6.4 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.11", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.65 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, ">= 0.2.6 and < 1.0.0-0", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4704f536682538ee3ae0e8f99ff3cef6af53df09ef3e7e550e202da9d5ce685c"},
3-
"ash_sql": {:hex, :ash_sql, "0.2.87", "17197c643918cdaee657946a1998860402dcf53a980f7665bb81d1fa53c224e7", [:mix], [{:ash, ">= 3.5.25 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "f82d6bf78f08bd9040af3adc28676965421598c88866074d8b1ccca65978d774"},
3+
"ash_sql": {:hex, :ash_sql, "0.2.89", "ad4ad497263b586a7f3949ceea5d44620a36cb99a1ef0ff5f58f13a77d9b99ef", [:mix], [{:ash, ">= 3.5.25 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "bd957aee95bbdf6326fc7a9212f9a2ab87329b99ee3646c373a87bb3c9968566"},
44
"benchee": {:hex, :benchee, "1.4.0", "9f1f96a30ac80bab94faad644b39a9031d5632e517416a8ab0a6b0ac4df124ce", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "299cd10dd8ce51c9ea3ddb74bb150f93d25e968f93e4c1fa31698a8e4fa5d715"},
55
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
66
"credo": {:hex, :credo, "1.7.12", "9e3c20463de4b5f3f23721527fcaf16722ec815e70ff6c60b86412c695d426c1", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8493d45c656c5427d9c729235b99d498bd133421f3e0a683e5c1b561471291e5"},

test/migration_generator_test.exs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,6 +2793,68 @@ defmodule AshPostgres.MigrationGeneratorTest do
27932793
end
27942794
end
27952795

2796+
describe "multitenancy identity with tenant attribute" do
2797+
setup do
2798+
on_exit(fn ->
2799+
File.rm_rf!("test_snapshots_path")
2800+
File.rm_rf!("test_migration_path")
2801+
end)
2802+
end
2803+
2804+
test "identity including tenant attribute does not duplicate columns in index" do
2805+
defresource Channel, "channels" do
2806+
postgres do
2807+
table "channels"
2808+
repo(AshPostgres.TestRepo)
2809+
end
2810+
2811+
actions do
2812+
defaults([:create, :read, :update, :destroy])
2813+
end
2814+
2815+
multitenancy do
2816+
strategy(:attribute)
2817+
attribute(:project_id)
2818+
end
2819+
2820+
identities do
2821+
identity(:unique_type_per_project, [:project_id, :type])
2822+
end
2823+
2824+
attributes do
2825+
uuid_primary_key(:id)
2826+
attribute(:project_id, :uuid, allow_nil?: false, public?: true)
2827+
attribute(:type, :string, allow_nil?: false, public?: true)
2828+
attribute(:name, :string, public?: true)
2829+
end
2830+
end
2831+
2832+
defdomain([Channel])
2833+
2834+
AshPostgres.MigrationGenerator.generate(Domain,
2835+
snapshot_path: "test_snapshots_path",
2836+
migration_path: "test_migration_path",
2837+
quiet: true,
2838+
format: false,
2839+
auto_name: true
2840+
)
2841+
2842+
assert [file] =
2843+
Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
2844+
|> Enum.reject(&String.contains?(&1, "extensions"))
2845+
2846+
file_content = File.read!(file)
2847+
2848+
# The index should only have project_id and type, not project_id twice
2849+
assert file_content =~
2850+
~S{create unique_index(:channels, [:project_id, :type], name: "channels_unique_type_per_project_index")}
2851+
2852+
# Make sure it doesn't have duplicate columns
2853+
refute file_content =~
2854+
~S{create unique_index(:channels, [:project_id, :project_id, :type]}
2855+
end
2856+
end
2857+
27962858
describe "decimal precision and scale" do
27972859
setup do
27982860
on_exit(fn ->

0 commit comments

Comments
 (0)