Advertisement
jackofblades

migrations

Aug 5th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.07 KB | None | 0 0
  1. defmodule Finapp.Repo.Migrations.InitialDatabase do
  2.   use Ecto.Migration
  3.  
  4.   def up do
  5.     create table(:users) do
  6.       add :email, :string, null: false
  7.       add :password_hash, :string
  8.       add :name, :string, null: false
  9.  
  10.       timestamps()
  11.     end
  12.  
  13.     create unique_index(:users, [:email])
  14.  
  15.     create table(:expenses) do
  16.       add :value, :integer, null: false
  17.       add :note, :string
  18.       add :inserted_at, :date, null: false, default: fragment("current_date")
  19.       add :user_id, references(:users), null: false
  20.     end
  21.  
  22.     create table(:tags) do
  23.       add :name, :string, null: false
  24.       add :user_id, references(:users), null: false
  25.       add :expense_id, references(:expenses)
  26.     end
  27.  
  28.     alter table(:expenses) do
  29.       add :tag_id, references(:tags), null: false
  30.     end
  31.  
  32.     create index(:tags, [:name])
  33.   end
  34.  
  35.   def down do
  36.     drop index(:tags, [:name])
  37.     alter table(:expenses) do
  38.       remove :tag_id
  39.     end
  40.     drop table(:tags)
  41.     drop table(:expenses)
  42.     drop index(:users, [:email])
  43.     drop table(:users)
  44.   end
  45. end
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement