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
2 changes: 1 addition & 1 deletion lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ defmodule AshPostgres.MigrationGenerator do
end)

operations
|> organize_operations
|> organize_operations()
|> build_up_and_down()
|> migration(repo, opts, tenant?, run_without_transaction?, split_index)
end)
Expand Down
30 changes: 21 additions & 9 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ defmodule AshPostgres.MigrationGenerator.Operation do
:bigint
end

def reference_type(%{type: :integer, default: "nil", generated?: true}, _) do
":bigserial"
end
def reference_type(%{type: :identity}, _), do: ":identity"

def reference_type(%{type: type}, _) do
type
end
def reference_type(%{type: :integer, default: "nil", generated?: true}, _), do: ":bigserial"

def reference_type(%{type: type}, _), do: type

def with_match(reference, source_attribute \\ nil)

Expand Down Expand Up @@ -489,6 +487,16 @@ defmodule AshPostgres.MigrationGenerator.Operation do
|> join()
end

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

def up(%{attribute: %{type: :bigint, default: "nil", generated?: true} = attribute}) do
[
"add #{inspect(attribute.source)}",
Expand Down Expand Up @@ -659,10 +667,14 @@ defmodule AshPostgres.MigrationGenerator.Operation do
Map.get(old_attribute, :references) != Map.get(attribute, :references) do
reference(multitenancy, attribute, schema)
else
if attribute.type == :bigint and attribute.default == "nil" and attribute.generated? do
":bigserial"
if attribute.type == :identity do
":identity"
else
inspect(attribute.type)
if attribute.type == :bigint and attribute.default == "nil" and attribute.generated? do
":bigserial"
else
inspect(attribute.type)
end
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/resource_generator/spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ defmodule AshPostgres.ResourceGenerator.Spec do

defp type("bigint"), do: {:ok, :integer}
defp type("bigserial"), do: {:ok, :integer}
defp type("identity"), do: {:ok, :identity}
defp type("boolean"), do: {:ok, :boolean}
defp type("bytea"), do: {:ok, :binary}
defp type("varchar"), do: {:ok, :string}
Expand Down
8 changes: 4 additions & 4 deletions test/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,13 +1128,13 @@ defmodule AshPostgres.FilterTest do
|> Ash.Query.for_read(:read)
|> Ash.Query.filter(first_member.id != ^cm2.id)
|> Ash.read!()
|> length == 4
|> length() == 4

assert Channel
|> Ash.Query.for_read(:read)
|> Ash.Query.filter(first_member.id == ^cm2.id)
|> Ash.read!()
|> length == 1
|> length() == 1
end

test "using exists with from_many?" do
Expand All @@ -1149,13 +1149,13 @@ defmodule AshPostgres.FilterTest do
|> Ash.Query.for_read(:read)
|> Ash.Query.filter(exists(first_member, id == ^cm2.id))
|> Ash.read!()
|> length == 0
|> length() == 0

assert Channel
|> Ash.Query.for_read(:read)
|> Ash.Query.filter(exists(first_member, id == ^cm1.id))
|> Ash.read!()
|> length == 1
|> length() == 1
end

test "using exists with has_many with limit" do
Expand Down
56 changes: 56 additions & 0 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,62 @@ defmodule AshPostgres.MigrationGeneratorTest do
end
end

describe "migration_types identity" do
setup %{snapshot_path: snapshot_path, migration_path: migration_path} do
defresource(IdentityPost) do
postgres do
table "identity_posts"
repo(AshPostgres.TestRepo)
migration_types(id: :identity, sequence_id: :identity)
end

actions do
defaults([:create, :read, :update, :destroy])
end

attributes do
integer_primary_key(:id)

attribute(:sequence_id, :integer,
generated?: true,
allow_nil?: false,
public?: true
)

attribute(:title, :string, public?: true)
end
end

defmodule IdentityDomain do
use Ash.Domain

resources do
resource(IdentityPost)
end
end

AshPostgres.MigrationGenerator.generate(IdentityDomain,
snapshot_path: snapshot_path,
migration_path: migration_path,
quiet: true,
format: false,
auto_name: true
)

:ok
end

test "uses :identity when set in migration_types", %{migration_path: migration_path} do
assert [file] =
Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")
|> Enum.reject(&String.contains?(&1, "extensions"))

file_contents = File.read!(file)
assert file_contents =~ ~S[add :id, :identity, null: false, primary_key: true]
assert file_contents =~ ~S[add :sequence_id, :identity, null: false]
end
end

describe "--check option" do
setup do
defposts do
Expand Down
Loading