2020-09-11 12:26:47 +12:00
|
|
|
defmodule AshPostgres.MigrationGenerator.Phase do
|
|
|
|
@moduledoc false
|
|
|
|
|
|
|
|
defmodule Create do
|
|
|
|
@moduledoc false
|
2022-05-14 09:41:30 +12:00
|
|
|
defstruct [:table, :schema, :multitenancy, operations: [], commented?: false]
|
2020-09-11 12:26:47 +12:00
|
|
|
|
2022-05-14 09:41:30 +12:00
|
|
|
def up(%{schema: schema, table: table, operations: operations, multitenancy: multitenancy}) do
|
2020-10-29 15:26:45 +13:00
|
|
|
if multitenancy.strategy == :context do
|
2021-01-27 13:16:29 +13:00
|
|
|
"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
|
2022-05-14 09:41:30 +12:00
|
|
|
opts =
|
|
|
|
if schema do
|
|
|
|
", prefix: \"#{schema}\""
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
"create table(:#{table}, primary_key: false#{opts}) do\n" <>
|
2020-10-29 15:26:45 +13:00
|
|
|
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
|
|
|
|
"\nend"
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
2022-05-14 09:41:30 +12:00
|
|
|
def down(%{schema: schema, table: table, multitenancy: multitenancy}) do
|
2020-10-29 15:26:45 +13:00
|
|
|
if multitenancy.strategy == :context do
|
2021-02-06 12:59:33 +13:00
|
|
|
"drop table(:#{inspect(table)}, prefix: prefix())"
|
2020-10-29 15:26:45 +13:00
|
|
|
else
|
2022-05-14 09:41:30 +12:00
|
|
|
opts =
|
|
|
|
if schema do
|
|
|
|
", prefix: \"#{schema}\""
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
"drop table(:#{inspect(table)}#{opts})"
|
2020-10-29 15:26:45 +13:00
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Alter do
|
|
|
|
@moduledoc false
|
2022-05-14 09:41:30 +12:00
|
|
|
defstruct [:schema, :table, :multitenancy, operations: [], commented?: false]
|
2020-09-11 12:26:47 +12:00
|
|
|
|
2022-05-14 09:41:30 +12:00
|
|
|
def up(%{table: table, schema: schema, 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
|
2021-01-27 13:16:29 +13:00
|
|
|
"alter table(:#{table}, prefix: prefix()) do\n" <>
|
2020-10-29 15:26:45 +13:00
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
else
|
2022-05-14 09:41:30 +12:00
|
|
|
opts =
|
|
|
|
if schema do
|
|
|
|
", prefix: \"#{schema}\""
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
"alter table(:#{table}#{opts}) do\n" <>
|
2020-10-29 15:26:45 +13:00
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
2022-05-14 09:41:30 +12:00
|
|
|
def down(%{table: table, schema: schema, 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
|
2021-01-27 13:16:29 +13:00
|
|
|
"alter table(:#{table}, prefix: prefix()) do\n" <>
|
2020-10-29 15:26:45 +13:00
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
else
|
2022-05-14 09:41:30 +12:00
|
|
|
opts =
|
|
|
|
if schema do
|
|
|
|
", prefix: \"#{schema}\""
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
"alter table(:#{table}#{opts}) do\n" <>
|
2020-10-29 15:26:45 +13:00
|
|
|
body <>
|
|
|
|
"\nend"
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|