mirror of
https://github.com/plausible/analytics.git
synced 2024-11-26 00:24:44 +03:00
Add constraint enforcing at most one owner membership per site (#3403)
This commit is contained in:
parent
842bbb7995
commit
21297b666f
@ -0,0 +1,13 @@
|
||||
defmodule Plausible.Repo.Migrations.AddUniqueIndexOnSiteMembershipsSiteIdWhenOwner do
|
||||
use Ecto.Migration
|
||||
|
||||
@disable_ddl_transaction true
|
||||
@disable_migration_lock true
|
||||
|
||||
def change do
|
||||
create_if_not_exists unique_index(:site_memberships, [:site_id],
|
||||
where: "role = 'owner'",
|
||||
concurrently: true
|
||||
)
|
||||
end
|
||||
end
|
15
test/plausible/site/membership_test.exs
Normal file
15
test/plausible/site/membership_test.exs
Normal file
@ -0,0 +1,15 @@
|
||||
defmodule Plausible.Site.MembershipTest do
|
||||
use Plausible.DataCase
|
||||
|
||||
test "raises on trying to insert two owner memberships for the same site" do
|
||||
user1 = insert(:user)
|
||||
user2 = insert(:user)
|
||||
site = insert(:site)
|
||||
|
||||
insert(:site_membership, site: site, user: user1, role: "owner")
|
||||
|
||||
assert_raise Ecto.ConstraintError, ~r/site_memberships_site_id_index/, fn ->
|
||||
insert(:site_membership, site: site, user: user2, role: "owner")
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user