Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 91 additions & 29 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
defmodule AshPostgres.MigrationGenerator.Operation do
@moduledoc false

defmodule Helper do
@moduledoc false
def join(list), do: list |> List.flatten() |> Enum.reject(&is_nil/1) |> Enum.join(", ")

def maybe_add_default("nil"), do: nil
def maybe_add_default(value), do: "default: #{value}"

def maybe_add_primary_key(true), do: "primary_key: true"
def maybe_add_primary_key(_), do: nil

def maybe_add_null(false), do: "null: false"
def maybe_add_null(_), do: nil
end

defmodule CreateTable do
@moduledoc false
defstruct [:table, :multitenancy, :old_multitenancy]
Expand All @@ -9,6 +24,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do
@moduledoc false
defstruct [:attribute, :table, :multitenancy, :old_multitenancy]

import Helper

def up(%{
multitenancy: %{strategy: :attribute, attribute: source_attribute},
attribute:
Expand All @@ -20,11 +37,19 @@ defmodule AshPostgres.MigrationGenerator.Operation do
}
} = attribute
}) do
"add #{inspect(attribute.name)}, references(#{inspect(table)}, type: #{
inspect(attribute.type)
}, column: #{inspect(destination_field)}, with: [#{source_attribute}: :#{
destination_attribute
}]), default: #{attribute.default}, primary_key: #{attribute.primary_key?}"
[
"add #{inspect(attribute.name)}",
"references(#{inspect(table)}",
[
"type: #{inspect(attribute.type)}",
"column: #{inspect(destination_field)}",
"with: [#{source_attribute}: :#{destination_attribute}]"
],
")",
maybe_add_default(attribute.default),
maybe_add_primary_key(attribute.primary_key?)
]
|> join()
end

def up(%{
Expand All @@ -38,11 +63,20 @@ defmodule AshPostgres.MigrationGenerator.Operation do
}
} = attribute
}) do
"add #{inspect(attribute.name)}, references(#{inspect(table)}, type: #{
inspect(attribute.type)
}, column: #{inspect(destination_field)}, name: \"\#\{prefix\}_#{table}_#{attribute.name}_fkey\", prefix: \"public\"), default: #{
attribute.default
}, primary_key: #{attribute.primary_key?}"
[
"add #{inspect(attribute.name)}",
"references(#{inspect(table)}",
[
"type: #{inspect(attribute.type)}",
"column: #{inspect(destination_field)}",
"name: \"\#\{prefix\}_#{table}_#{attribute.name}_fkey\"",
"prefix: \"public\""
],
")",
maybe_add_default(attribute.default),
maybe_add_primary_key(attribute.primary_key?)
]
|> join()
end

def up(%{
Expand All @@ -62,44 +96,72 @@ defmodule AshPostgres.MigrationGenerator.Operation do
you should be aware of
""")

"add #{inspect(attribute.name)}, #{inspect(attribute.type)}, default: #{attribute.default}, primary_key: #{
attribute.primary_key?
}"
[
"add #{inspect(attribute.name)}",
inspect(attribute.type),
maybe_add_default(attribute.default),
maybe_add_primary_key(attribute.primary_key?)
]
|> join()
end

def up(%{
multitenancy: %{strategy: :context},
attribute:
%{references: %{table: table, destination_field: destination_field}} = attribute
}) do
"add #{inspect(attribute.name)}, references(#{inspect(table)}, type: #{
inspect(attribute.type)
}, column: #{inspect(destination_field)}, name: \"\#\{prefix\}_#{table}_#{attribute.name}_fkey\"), default: #{
attribute.default
}, primary_key: #{attribute.primary_key?}"
[
"add #{inspect(attribute.name)}",
"references(#{inspect(table)}",
[
"type: #{inspect(attribute.type)}",
"column: #{inspect(destination_field)}",
"name: \"\#\{prefix\}_#{table}_#{attribute.name}_fkey\""
],
")",
maybe_add_default(attribute.default),
maybe_add_primary_key(attribute.primary_key?)
]
|> join()
end

def up(%{
attribute:
%{references: %{table: table, destination_field: destination_field}} = attribute
}) do
"add #{inspect(attribute.name)}, references(#{inspect(table)}, type: #{
inspect(attribute.type)
}, column: #{inspect(destination_field)}), default: #{attribute.default}, primary_key: #{
attribute.primary_key?
}"
[
"add #{inspect(attribute.name)}",
"references(#{inspect(table)}",
[
"type: #{inspect(attribute.type)}",
"column: #{inspect(destination_field)}"
],
")",
maybe_add_default(attribute.default),
maybe_add_primary_key(attribute.primary_key?)
]
|> join()
end

def up(%{attribute: %{type: :integer, default: "nil", generated?: true} = attribute}) do
"add #{inspect(attribute.name)}, :serial, null: #{attribute.allow_nil?}, primary_key: #{
attribute.primary_key?
}"
[
"add #{inspect(attribute.name)}",
":serial",
maybe_add_null(attribute.allow_nil?),
maybe_add_primary_key(attribute.primary_key?)
]
|> join()
end

def up(%{attribute: attribute}) do
"add #{inspect(attribute.name)}, #{inspect(attribute.type)}, null: #{attribute.allow_nil?}, default: #{
attribute.default
}, primary_key: #{attribute.primary_key?}"
[
"add #{inspect(attribute.name)}",
"#{inspect(attribute.type)}",
maybe_add_null(attribute.allow_nil?),
maybe_add_default(attribute.default),
maybe_add_primary_key(attribute.primary_key?)
]
|> join()
end

def down(
Expand Down
20 changes: 10 additions & 10 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ defmodule AshPostgres.MigrationGeneratorTest do
assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")

assert File.read!(file) =~
~S[add :id, :binary_id, null: true, default: fragment("uuid_generate_v4()"), primary_key: true]
~S[add :id, :binary_id, default: fragment("uuid_generate_v4()"), primary_key: true]
end

test "the migration adds other attributes" do
assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")

assert File.read!(file) =~
~S[add :title, :text, null: true, default: nil, primary_key: false]
~S[add :title, :text]
end

test "the migration creates unique_indexes based on the identities of the resource" do
Expand Down Expand Up @@ -181,7 +181,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))

assert File.read!(file2) =~
~S[add :name, :text, null: false, default: nil, primary_key: false]
~S[add :name, :text, null: false]
end

test "when renaming a field, it asks if you are renaming it, and renames it if you are" do
Expand Down Expand Up @@ -232,7 +232,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))

assert File.read!(file2) =~
~S[add :name, :text, null: false, default: nil, primary_key: false]
~S[add :name, :text, null: false]
end

test "when renaming a field, it asks which field you are renaming it to, and renames it if you are" do
Expand Down Expand Up @@ -286,7 +286,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))

assert File.read!(file2) =~
~S[add :subject, :text, null: false, default: nil, primary_key: false]
~S[add :subject, :text, null: false]
end

test "when changing the primary key, it changes properly" do
Expand All @@ -311,7 +311,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))

assert File.read!(file2) =~
~S[add :guid, :binary_id, null: true, default: fragment("uuid_generate_v4()"), primary_key: true]
~S[add :guid, :binary_id, default: fragment("uuid_generate_v4()"), primary_key: true]
end

test "when multiple schemas apply to the same table, all attributes are added" do
Expand Down Expand Up @@ -343,10 +343,10 @@ defmodule AshPostgres.MigrationGeneratorTest do
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))

assert File.read!(file2) =~
~S[add :foobar, :text, null: true, default: nil, primary_key: false]
~S[add :foobar, :text]

assert File.read!(file2) =~
~S[add :foobar, :text, null: true, default: nil, primary_key: false]
~S[add :foobar, :text]
end
end

Expand All @@ -360,7 +360,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
defposts do
attributes do
attribute(:id, :integer, generated?: true, allow_nil?: false, primary_key?: true)
attribute(:views, :integer, default: nil)
attribute(:views, :integer)
end
end

Expand All @@ -385,7 +385,7 @@ defmodule AshPostgres.MigrationGeneratorTest do
~S[add :id, :serial, null: false, primary_key: true]

assert File.read!(file) =~
~S[add :views, :integer, null: true, default: nil, primary_key: false]
~S[add :views, :integer]
end
end
end