mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 13:33:50 +12:00
44 lines
1.1 KiB
Elixir
44 lines
1.1 KiB
Elixir
|
defmodule AshPostgres.MigrationGenerator.Phase do
|
||
|
@moduledoc false
|
||
|
|
||
|
defmodule Create do
|
||
|
@moduledoc false
|
||
|
defstruct [:table, operations: []]
|
||
|
|
||
|
def up(%{table: table, operations: operations}) do
|
||
|
"create table(:#{table}, primary_key: false) do\n" <>
|
||
|
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
|
||
|
"\nend"
|
||
|
end
|
||
|
|
||
|
def down(%{table: table}) do
|
||
|
"drop table(#{inspect(table)})"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
defmodule Alter do
|
||
|
@moduledoc false
|
||
|
defstruct [:table, operations: []]
|
||
|
|
||
|
def up(%{table: table, operations: operations}) do
|
||
|
body =
|
||
|
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end)
|
||
|
|
||
|
"alter table(#{inspect(table)}) do\n" <>
|
||
|
body <>
|
||
|
"\nend"
|
||
|
end
|
||
|
|
||
|
def down(%{table: table, operations: operations}) do
|
||
|
body =
|
||
|
operations
|
||
|
|> Enum.reverse()
|
||
|
|> Enum.map_join("\n", fn operation -> operation.__struct__.down(operation) end)
|
||
|
|
||
|
"alter table(:#{table}) do\n" <>
|
||
|
body <>
|
||
|
"\nend"
|
||
|
end
|
||
|
end
|
||
|
end
|