ash_postgres/priv/test_repo/migrations/20211113185741_migrate_resources1.exs

172 lines
4.8 KiB
Elixir
Raw Normal View History

2021-11-14 07:55:49 +13:00
defmodule AshPostgres.TestRepo.Migrations.MigrateResources1 do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""
use Ecto.Migration
def up do
create table(:users, primary_key: false) do
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
add :name, :text
add :org_id, :uuid
end
create table(:posts, primary_key: false) do
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
add :title, :text
add :score, :bigint
add :public, :boolean
add :category, :citext
add :type, :text, default: "sponsored"
add :price, :bigint
add :decimal, :decimal, default: 0
add :status, :text
add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
2021-11-14 07:55:49 +13:00
end
create table(:post_ratings, primary_key: false) do
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
add :score, :bigint
add :resource_id,
references(:posts, column: :id, name: "post_ratings_resource_id_fkey", type: :uuid)
end
create table(:post_links, primary_key: false) do
add :source_post_id,
references(:posts, column: :id, name: "post_links_source_post_id_fkey", type: :uuid),
primary_key: true,
null: false
add :destination_post_id,
references(:posts, column: :id, name: "post_links_destination_post_id_fkey", type: :uuid),
primary_key: true,
null: false
end
create table(:multitenant_orgs, primary_key: false) do
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
end
alter table(:users) do
modify :org_id,
references(:multitenant_orgs, column: :id, name: "users_org_id_fkey", type: :uuid)
end
alter table(:multitenant_orgs) do
add :name, :text
end
create unique_index(:multitenant_orgs, [:id, :name],
name: "multitenant_orgs_unique_by_name_index"
)
create table(:integer_posts, primary_key: false) do
add :id, :bigserial, null: false, primary_key: true
add :title, :text
end
create table(:comments, primary_key: false) do
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
add :title, :text
add :likes, :bigint
add :arbitrary_timestamp, :utc_datetime_usec
2021-11-14 07:55:49 +13:00
add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
add :post_id,
references(:posts,
column: :id,
name: "special_name_fkey",
type: :uuid,
on_delete: :delete_all,
on_update: :update_all
)
add :author_id, :uuid
end
create table(:comment_ratings, primary_key: false) do
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
add :score, :bigint
add :resource_id,
references(:comments, column: :id, name: "comment_ratings_resource_id_fkey", type: :uuid)
end
create table(:authors, primary_key: false) do
add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
end
alter table(:comments) do
modify :author_id,
references(:authors, column: :id, name: "comments_author_id_fkey", type: :uuid)
end
alter table(:authors) do
add :first_name, :text
add :last_name, :text
end
create constraint(:posts, :price_must_be_positive, check: "type = 'sponsored' AND price > 0")
end
def down do
drop_if_exists constraint(:posts, :price_must_be_positive)
alter table(:authors) do
remove :last_name
remove :first_name
end
drop constraint(:comments, "comments_author_id_fkey")
alter table(:comments) do
modify :author_id, :uuid
end
drop table(:authors)
drop constraint(:comment_ratings, "comment_ratings_resource_id_fkey")
drop table(:comment_ratings)
drop constraint(:comments, "special_name_fkey")
drop table(:comments)
drop table(:integer_posts)
drop_if_exists unique_index(:multitenant_orgs, [:id, :name],
name: "multitenant_orgs_unique_by_name_index"
)
alter table(:multitenant_orgs) do
remove :name
end
drop constraint(:users, "users_org_id_fkey")
alter table(:users) do
modify :org_id, :uuid
end
drop table(:multitenant_orgs)
drop constraint(:post_links, "post_links_destination_post_id_fkey")
drop constraint(:post_links, "post_links_source_post_id_fkey")
drop table(:post_links)
drop constraint(:post_ratings, "post_ratings_resource_id_fkey")
drop table(:post_ratings)
drop table(:posts)
drop table(:users)
end
end