ash_postgres/lib/migration_generator/phase.ex

66 lines
2 KiB
Elixir
Raw Normal View History

defmodule AshPostgres.MigrationGenerator.Phase do
@moduledoc false
defmodule Create do
@moduledoc false
defstruct [:table, :multitenancy, operations: [], commented?: false]
2020-10-29 15:26:45 +13:00
def up(%{table: table, operations: operations, multitenancy: multitenancy}) do
if multitenancy.strategy == :context do
"create table(:#{table}, primary_key: false, prefix: prefix()) do\n" <>
2020-10-29 15:26:45 +13:00
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
"\nend"
else
"create table(:#{table}, primary_key: false) do\n" <>
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
"\nend"
end
end
2020-10-29 15:26:45 +13:00
def down(%{table: table, multitenancy: multitenancy}) do
if multitenancy.strategy == :context do
"drop table(:#{inspect(table)}, prefix: prefix())"
2020-10-29 15:26:45 +13:00
else
"drop table(:#{inspect(table)})"
2020-10-29 15:26:45 +13:00
end
end
end
defmodule Alter do
@moduledoc false
defstruct [:table, :multitenancy, operations: [], commented?: false]
2020-10-29 15:26:45 +13:00
def up(%{table: table, operations: operations, multitenancy: multitenancy}) do
body =
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end)
2020-10-29 15:26:45 +13:00
if multitenancy.strategy == :context do
"alter table(:#{table}, prefix: prefix()) do\n" <>
2020-10-29 15:26:45 +13:00
body <>
"\nend"
else
"alter table(:#{table}) do\n" <>
2020-10-29 15:26:45 +13:00
body <>
"\nend"
end
end
2020-10-29 15:26:45 +13:00
def down(%{table: table, operations: operations, multitenancy: multitenancy}) do
body =
operations
|> Enum.reverse()
|> Enum.map_join("\n", fn operation -> operation.__struct__.down(operation) end)
2020-10-29 15:26:45 +13:00
if multitenancy.strategy == :context do
"alter table(:#{table}, prefix: prefix()) do\n" <>
2020-10-29 15:26:45 +13:00
body <>
"\nend"
else
"alter table(:#{table}) do\n" <>
body <>
"\nend"
end
end
end
end