From 9651887f3489816423dc20600cbf9f932e5cc03d Mon Sep 17 00:00:00 2001 From: Adam Piontek Date: Fri, 5 Mar 2021 12:59:41 -0500 Subject: [PATCH] switched to binary ids for db --- lib/bones73k/accounts/user.ex | 2 ++ lib/bones73k/accounts/user_token.ex | 2 ++ lib/bones73k/properties/property.ex | 4 +++- .../20200913000515_create_users_auth_tables.exs | 8 +++++--- priv/repo/migrations/20200914162043_create_properties.exs | 5 +++-- priv/repo/seeds.exs | 7 +++++-- 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/bones73k/accounts/user.ex b/lib/bones73k/accounts/user.ex index 6e69294c..8d67ee03 100644 --- a/lib/bones73k/accounts/user.ex +++ b/lib/bones73k/accounts/user.ex @@ -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 diff --git a/lib/bones73k/accounts/user_token.ex b/lib/bones73k/accounts/user_token.ex index ba3911eb..7114f32c 100644 --- a/lib/bones73k/accounts/user_token.ex +++ b/lib/bones73k/accounts/user_token.ex @@ -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 diff --git a/lib/bones73k/properties/property.ex b/lib/bones73k/properties/property.ex index 6293542b..97135e11 100644 --- a/lib/bones73k/properties/property.ex +++ b/lib/bones73k/properties/property.ex @@ -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 diff --git a/priv/repo/migrations/20200913000515_create_users_auth_tables.exs b/priv/repo/migrations/20200913000515_create_users_auth_tables.exs index bdcc7443..efc5f616 100644 --- a/priv/repo/migrations/20200913000515_create_users_auth_tables.exs +++ b/priv/repo/migrations/20200913000515_create_users_auth_tables.exs @@ -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) diff --git a/priv/repo/migrations/20200914162043_create_properties.exs b/priv/repo/migrations/20200914162043_create_properties.exs index 97f3ec0c..d93506c0 100644 --- a/priv/repo/migrations/20200914162043_create_properties.exs +++ b/priv/repo/migrations/20200914162043_create_properties.exs @@ -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 diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 792cffa5..72f4671f 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -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 }