Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule FaithfulWord.Repo.Migrations.CreateUsersAuthTables do
use Ecto.Migration
def change do
execute "CREATE EXTENSION IF NOT EXISTS citext", ""
create table(:users, primary_key: false) do
add :id, :binary_id, primary_key: true
add :email, :citext, null: false
add :username, :citext
add :name, :citext
add :phone_number, :citext
add :confirmed_at, :naive_datetime
add :orgs_registered, {:array, :integer}, default: [], null: false
add :orgs_confirmed, {:array, :integer}, default: [], null: false
add :orgs_authorized, {:array, :integer}, default: [], null: false
add :locale, :string, null: true
add :uuid, :uuid
add :role, :string, null: false
add :status, :string, null: false
timestamps(type: :utc_datetime_usec)
end
create unique_index(:users, [:email])
create unique_index(:users, [:phone_number])
create unique_index(:users, [:username])
create index(:users, [:uuid])
create table(:users_tokens) do
add :user_id, references(:users, on_delete: :delete_all, type: :binary_id), null: false
add :token, :binary, null: false
add :context, :string, null: false
add :org_id, references(:orgs, on_delete: :delete_all), null: false
add :sent_to, :string
timestamps(updated_at: false)
end
# create index(:users_tokens, [:user_id])
create unique_index(:users_tokens, [:context, :token])
end
end
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "users" do
field :email, :string
field :username, :string
field :name, :string
field :phone_number, :string
field :confirmed_at, :naive_datetime
...
schema "users_tokens" do
field :token, :binary
field :context, :string
field :sent_to, :string
field :org_id, :integer
belongs_to :user, FaithfulWord.Schema.User
timestamps(updated_at: false)
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment