diff --git a/lib/migration_generator/operation.ex b/lib/migration_generator/operation.ex index 63c5a56a..9af704ab 100644 --- a/lib/migration_generator/operation.ex +++ b/lib/migration_generator/operation.ex @@ -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] @@ -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: @@ -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(%{ @@ -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(%{ @@ -62,9 +96,13 @@ 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(%{ @@ -72,34 +110,58 @@ defmodule AshPostgres.MigrationGenerator.Operation do 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( diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index 01ee0f49..9a7f7250 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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