switched to binary ids for db

This commit is contained in:
Adam Piontek 2021-03-05 12:59:41 -05:00
parent b0978d11b3
commit 9651887f34
6 changed files with 20 additions and 8 deletions

View file

@ -16,6 +16,8 @@ defmodule Bones73k.Accounts.User do
@max_password 80
@derive {Inspect, except: [:password]}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "users" do
field :email, :string
field :password, :string, virtual: true

View file

@ -12,6 +12,8 @@ defmodule Bones73k.Accounts.UserToken do
@change_email_validity_in_days 7
@session_validity_in_days 60
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "users_tokens" do
field :token, :binary
field :context, :string

View file

@ -2,11 +2,13 @@ defmodule Bones73k.Properties.Property do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "properties" do
field :description, :string
field :name, :string
field :price, :decimal
field :user_id, :id
belongs_to :user, Bones73k.Accounts.User
timestamps()
end

View file

@ -4,7 +4,8 @@ defmodule Bones73k.Repo.Migrations.CreateUsersAuthTables do
def change do
execute("CREATE EXTENSION IF NOT EXISTS citext", "")
create table(:users) do
create table(:users, primary_key: false) do
add(:id, :binary_id, primary_key: true)
add(:email, :citext, null: false)
add(:hashed_password, :string, null: false)
add(:confirmed_at, :naive_datetime)
@ -13,8 +14,9 @@ defmodule Bones73k.Repo.Migrations.CreateUsersAuthTables do
create(unique_index(:users, [:email]))
create table(:users_tokens) do
add(:user_id, references(:users, on_delete: :delete_all), null: false)
create table(:users_tokens, primary_key: false) do
add(:id, :binary_id, primary_key: true)
add(:user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false)
add(:token, :binary, null: false)
add(:context, :string, null: false)
add(:sent_to, :string)

View file

@ -2,11 +2,12 @@ defmodule Bones73k.Repo.Migrations.CreateProperties do
use Ecto.Migration
def change do
create table(:properties) do
create table(:properties, primary_key: false) do
add(:id, :binary_id, primary_key: true)
add(:name, :string)
add(:price, :decimal)
add(:description, :text)
add(:user_id, references(:users, on_delete: :nothing))
add(:user_id, references(:users, type: :binary_id, on_delete: :nothing))
timestamps()
end

View file

@ -10,10 +10,10 @@
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
import Ecto.Query
alias Bones73k.Repo
alias Bones73k.Accounts
alias Bones73k.Accounts.User
alias Bones73k.Properties
alias Bones73k.Properties.Property
############################################################################
@ -114,15 +114,18 @@ count_to_take = 123
mock_props = props_json |> File.read!() |> Jason.decode!() |> Enum.take_random(count_to_take)
random_user_query = from User, order_by: fragment("RANDOM()"), limit: 1
mock_props =
Enum.map(mock_props, fn e ->
add_dt = NaiveDateTime.from_iso8601!(e["inserted_at"])
rand_user = Repo.one(random_user_query)
%{
name: e["name"],
price: e["price"],
description: e["description"],
user_id: e["user_id"],
user_id: rand_user.id,
inserted_at: add_dt,
updated_at: add_dt
}