953 B · text History e569b6d
defmodule CartosnapApp.Repo.Migrations.CreateUserInvites do
  use Ecto.Migration

  # Registration invites (CartosnapApp.Accounts.Invite), as the sibling apps:
  # while registration is closed, the way to an account without a trip
  # invite. Only a SHA-256 of each link's token is kept.
  def change do
    create table(:user_invites, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :token_hash, :binary, null: false
      add :note, :string
      add :email, :string
      add :created_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :consumed_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :expires_at, :utc_datetime
      add :consumed_at, :utc_datetime

      timestamps(type: :utc_datetime)
    end

    create unique_index(:user_invites, [:token_hash])
    create index(:user_invites, [:created_by_id])
    create index(:user_invites, [:email])
  end
end