From 2fc136777c1f9a2a993670a7071f1d0ac87fa29f Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Fri, 9 Apr 2021 11:24:25 +0300 Subject: [PATCH] Add unique index to shared link name --- lib/plausible/auth/api_key.ex | 1 + lib/plausible/site/shared_link.ex | 1 + lib/plausible_web/templates/site/edit_shared_link.html.eex | 2 +- ...20210409074413_add_unique_index_to_shared_link_name.exs | 7 +++++++ 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20210409074413_add_unique_index_to_shared_link_name.exs diff --git a/lib/plausible/auth/api_key.ex b/lib/plausible/auth/api_key.ex index 71fcd111b..13c700d3e 100644 --- a/lib/plausible/auth/api_key.ex +++ b/lib/plausible/auth/api_key.ex @@ -5,6 +5,7 @@ defmodule Plausible.Auth.ApiKey do @required [:user_id, :key, :name] schema "api_keys" do field :name, :string + field :key, :string, virtual: true field :key_hash, :string field :key_prefix, :string diff --git a/lib/plausible/site/shared_link.ex b/lib/plausible/site/shared_link.ex index a63c3f59b..e01c582f8 100644 --- a/lib/plausible/site/shared_link.ex +++ b/lib/plausible/site/shared_link.ex @@ -17,6 +17,7 @@ defmodule Plausible.Site.SharedLink do |> cast(attrs, [:slug, :password, :name]) |> validate_required([:slug, :name]) |> unique_constraint(:slug) + |> unique_constraint(:name, name: :shared_links_site_id_name_index) |> hash_password() end diff --git a/lib/plausible_web/templates/site/edit_shared_link.html.eex b/lib/plausible_web/templates/site/edit_shared_link.html.eex index 00b98d0b3..e36d12fb9 100644 --- a/lib/plausible_web/templates/site/edit_shared_link.html.eex +++ b/lib/plausible_web/templates/site/edit_shared_link.html.eex @@ -8,5 +8,5 @@ - <%= submit "Edit shared link", class: "button mt-4 w-full" %> + <%= submit "Update", class: "button mt-4 w-full" %> <% end %> diff --git a/priv/repo/migrations/20210409074413_add_unique_index_to_shared_link_name.exs b/priv/repo/migrations/20210409074413_add_unique_index_to_shared_link_name.exs new file mode 100644 index 000000000..585e79de2 --- /dev/null +++ b/priv/repo/migrations/20210409074413_add_unique_index_to_shared_link_name.exs @@ -0,0 +1,7 @@ +defmodule Plausible.Repo.Migrations.AddUniqueIndexToSharedLinkName do + use Ecto.Migration + + def change do + create unique_index(:shared_links, [:site_id, :name], name: :shared_links_site_id_name_index) + end +end