Current section

Files

Jump to
invoicex lib invoicex migration.ex
Raw

lib/invoicex/migration.ex

defmodule Invoicex.Migration do
@moduledoc """
Module which runs `Invoicex`-specific migrations.
"""
use Ecto.Migration
alias Invoicex.Enum.{
ContactNumberType,
CountryCode,
Currency,
EmailEvent,
InvoiceStatus,
IntervalType,
InvoiceType,
NumberType
}
@enums [
ContactNumberType,
CountryCode,
Currency,
EmailEvent,
InvoiceStatus,
IntervalType,
InvoiceType,
NumberType
]
@prefix Invoicex.schema_prefix()
@tables ["invoices", "sources", "taxes", "discounts", "regions", "traders", "sets"]
@doc """
Drops all defined enums and tables.
"""
@spec down :: :ok
def down do
Enum.each(@tables, &drop_if_exists(table(&1, prefix: @prefix)))
Enum.each(@enums, & &1.drop_db_enum())
end
@doc """
Creates enums and tables.
"""
@spec up :: :ok
def up do
Enum.each(@enums, & &1.create_db_enum())
create_if_not_exists table("traders", prefix: @prefix) do
add(:addresses, {:array, :map})
add(:contacts, {:array, :map})
add(:name, :string)
add(:notes, {:array, :map})
timestamps()
end
create_if_not_exists table("sets", prefix: @prefix) do
add(:name, :string)
add(:trader_id, references("traders"))
timestamps()
end
create_if_not_exists table("regions", prefix: @prefix) do
add(:name, :string)
add(:set_id, references("sets"))
timestamps()
end
create_if_not_exists table("discounts", prefix: @prefix) do
add(:ends_at, :naive_datetime)
add(:label, :string)
add(:region_id, references("regions"))
add(:starts_at, :naive_datetime)
add(:value, :decimal)
timestamps()
end
create_if_not_exists table("taxes", prefix: @prefix) do
add(:description, :string)
add(:is_compound, :boolean, default: false)
add(:is_name_visible_on_invoice, :boolean, default: false)
add(:is_recoverable, :boolean, default: false)
add(:name, :string)
add(:rate, :decimal)
add(:region_id, references("regions"))
add(:valid_from, :date)
add(:valid_to, :date)
timestamps()
end
create_if_not_exists table("sources", prefix: @prefix) do
add(:delivery_set_id, references("sets"))
add(:name, :string)
add(:selling_region_id, references("regions"))
add(:type, :string)
add(:value, :string)
timestamps()
end
create_if_not_exists table("invoices", prefix: @prefix) do
add(:attachments, {:array, :map}, default: [])
add(:buyer, :map)
add(:buyer_trader_id, references("traders"))
add(:correct_reasons, {:array, :map}, default: [])
add(:currency, Currency.type())
add(:custom_number, :string)
add(:description, :string)
add(:due_date, :date)
add(:edit_reasons, {:array, :map}, default: [])
add(:expected_payment_date, :date)
add(:footer, :string)
add(:groups, {:array, :map}, default: [])
add(:issue_date, :date)
add(:items, {:array, :map}, default: [])
add(:notes, {:array, :map}, default: [])
add(:number, :map)
add(:pdf_path, :string)
add(:seller, :map)
add(:seller_trader_id, references("traders"))
add(:status, InvoiceStatus.type())
add(:title, :string)
add(:type, InvoiceType.type())
timestamps()
end
end
end