2020-09-11 12:26:47 +12:00
|
|
|
defmodule AshPostgres.MigrationGenerator.Phase do
|
|
|
|
@moduledoc false
|
|
|
|
|
|
|
|
defmodule Create do
|
|
|
|
@moduledoc false
|
2020-10-29 15:26:45 +13:00
|
|
|
defstruct [:table, :multitenancy, operations: []]
|
2020-09-11 12:26:47 +12:00
|
|
|
|
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" <>
|
|
|
|
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
|
2020-09-11 12:26:47 +12:00
|
|
|
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)"
|
|
|
|
else
|
|
|
|
"drop table(#{inspect(table)})"
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Alter do
|
|
|
|
@moduledoc false
|
2020-10-29 15:26:45 +13:00
|
|
|
defstruct [:table, :multitenancy, operations: []]
|
2020-09-11 12:26:47 +12:00
|
|
|
|
2020-10-29 15:26:45 +13:00
|
|
|
def up(%{table: table, operations: operations, multitenancy: multitenancy}) do
|
2020-09-11 12:26:47 +12:00
|
|
|
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
|
2020-11-18 12:35:57 +13:00
|
|
|
"alter table(:#{table}, prefix: prefix) do\n" <>
|
2020-10-29 15:26:45 +13:00
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
else
|
2020-11-18 12:35:57 +13:00
|
|
|
"alter table(:#{table}) do\n" <>
|
2020-10-29 15:26:45 +13:00
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
2020-10-29 15:26:45 +13:00
|
|
|
def down(%{table: table, operations: operations, multitenancy: multitenancy}) do
|
2020-09-11 12:26:47 +12:00
|
|
|
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" <>
|
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
else
|
|
|
|
"alter table(:#{table}) do\n" <>
|
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|