defmodule GitGud.Repo.Migrations.CreateOrganizationInvitations do
use Ecto.Migration
# Joining an org becomes something you accept rather than something
# done to you. An admin creates an invitation; membership is only
# inserted once the invitee says yes.
#
# Declined rows are kept rather than deleted so the same person can't
# be re-invited into an endless loop without the admin noticing, and
# so the org's members page can show that an invitation was answered.
def change do
create table(:organization_invitations) do
add :organization_id, references(:organizations, on_delete: :delete_all), null: false
add :invited_user_id, references(:users, on_delete: :delete_all), null: false
add :invited_by_id, references(:users, on_delete: :nilify_all)
add :role, :string, null: false, default: "member"
add :state, :string, null: false, default: "pending"
add :responded_at, :utc_datetime
timestamps(type: :utc_datetime)
end
# One live invitation per person per org. Answered ones are kept,
# so the constraint only covers pending.
create unique_index(:organization_invitations, [:organization_id, :invited_user_id],
where: "state = 'pending'",
name: :organization_invitations_pending_index
)
# "What am I being asked to join?" — the dashboard's query.
create index(:organization_invitations, [:invited_user_id, :state])
end
end
neiam /gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Packages
⭐
Log in to mark this repository.
1.4 KiB · text
History
6280797