switched to binary ids for db
This commit is contained in:
parent
b0978d11b3
commit
9651887f34
6 changed files with 20 additions and 8 deletions
|
@ -16,6 +16,8 @@ defmodule Bones73k.Accounts.User do
|
||||||
@max_password 80
|
@max_password 80
|
||||||
|
|
||||||
@derive {Inspect, except: [:password]}
|
@derive {Inspect, except: [:password]}
|
||||||
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
|
@foreign_key_type :binary_id
|
||||||
schema "users" do
|
schema "users" do
|
||||||
field :email, :string
|
field :email, :string
|
||||||
field :password, :string, virtual: true
|
field :password, :string, virtual: true
|
||||||
|
|
|
@ -12,6 +12,8 @@ defmodule Bones73k.Accounts.UserToken do
|
||||||
@change_email_validity_in_days 7
|
@change_email_validity_in_days 7
|
||||||
@session_validity_in_days 60
|
@session_validity_in_days 60
|
||||||
|
|
||||||
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
|
@foreign_key_type :binary_id
|
||||||
schema "users_tokens" do
|
schema "users_tokens" do
|
||||||
field :token, :binary
|
field :token, :binary
|
||||||
field :context, :string
|
field :context, :string
|
||||||
|
|
|
@ -2,11 +2,13 @@ defmodule Bones73k.Properties.Property do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
|
@foreign_key_type :binary_id
|
||||||
schema "properties" do
|
schema "properties" do
|
||||||
field :description, :string
|
field :description, :string
|
||||||
field :name, :string
|
field :name, :string
|
||||||
field :price, :decimal
|
field :price, :decimal
|
||||||
field :user_id, :id
|
belongs_to :user, Bones73k.Accounts.User
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,8 @@ defmodule Bones73k.Repo.Migrations.CreateUsersAuthTables do
|
||||||
def change do
|
def change do
|
||||||
execute("CREATE EXTENSION IF NOT EXISTS citext", "")
|
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(:email, :citext, null: false)
|
||||||
add(:hashed_password, :string, null: false)
|
add(:hashed_password, :string, null: false)
|
||||||
add(:confirmed_at, :naive_datetime)
|
add(:confirmed_at, :naive_datetime)
|
||||||
|
@ -13,8 +14,9 @@ defmodule Bones73k.Repo.Migrations.CreateUsersAuthTables do
|
||||||
|
|
||||||
create(unique_index(:users, [:email]))
|
create(unique_index(:users, [:email]))
|
||||||
|
|
||||||
create table(:users_tokens) do
|
create table(:users_tokens, primary_key: false) do
|
||||||
add(:user_id, references(:users, on_delete: :delete_all), null: false)
|
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(:token, :binary, null: false)
|
||||||
add(:context, :string, null: false)
|
add(:context, :string, null: false)
|
||||||
add(:sent_to, :string)
|
add(:sent_to, :string)
|
||||||
|
|
|
@ -2,11 +2,12 @@ defmodule Bones73k.Repo.Migrations.CreateProperties do
|
||||||
use Ecto.Migration
|
use Ecto.Migration
|
||||||
|
|
||||||
def change do
|
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(:name, :string)
|
||||||
add(:price, :decimal)
|
add(:price, :decimal)
|
||||||
add(:description, :text)
|
add(:description, :text)
|
||||||
add(:user_id, references(:users, on_delete: :nothing))
|
add(:user_id, references(:users, type: :binary_id, on_delete: :nothing))
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,10 +10,10 @@
|
||||||
# We recommend using the bang functions (`insert!`, `update!`
|
# We recommend using the bang functions (`insert!`, `update!`
|
||||||
# and so on) as they will fail if something goes wrong.
|
# and so on) as they will fail if something goes wrong.
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
alias Bones73k.Repo
|
alias Bones73k.Repo
|
||||||
alias Bones73k.Accounts
|
alias Bones73k.Accounts
|
||||||
alias Bones73k.Accounts.User
|
alias Bones73k.Accounts.User
|
||||||
alias Bones73k.Properties
|
|
||||||
alias Bones73k.Properties.Property
|
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)
|
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 =
|
mock_props =
|
||||||
Enum.map(mock_props, fn e ->
|
Enum.map(mock_props, fn e ->
|
||||||
add_dt = NaiveDateTime.from_iso8601!(e["inserted_at"])
|
add_dt = NaiveDateTime.from_iso8601!(e["inserted_at"])
|
||||||
|
rand_user = Repo.one(random_user_query)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
name: e["name"],
|
name: e["name"],
|
||||||
price: e["price"],
|
price: e["price"],
|
||||||
description: e["description"],
|
description: e["description"],
|
||||||
user_id: e["user_id"],
|
user_id: rand_user.id,
|
||||||
inserted_at: add_dt,
|
inserted_at: add_dt,
|
||||||
updated_at: add_dt
|
updated_at: add_dt
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue