Skip to content

Commit 550227f

Browse files
authored
feat: add support for :identity types (#715)
1 parent 73bd66d commit 550227f

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ defmodule AshPostgres.MigrationGenerator do
530530
end)
531531

532532
operations
533-
|> organize_operations
533+
|> organize_operations()
534534
|> build_up_and_down()
535535
|> migration(repo, opts, tenant?, run_without_transaction?, split_index)
536536
end)

lib/migration_generator/operation.ex

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ defmodule AshPostgres.MigrationGenerator.Operation do
7777
:bigint
7878
end
7979

80-
def reference_type(%{type: :integer, default: "nil", generated?: true}, _) do
81-
":bigserial"
82-
end
80+
def reference_type(%{type: :identity}, _), do: ":identity"
8381

84-
def reference_type(%{type: type}, _) do
85-
type
86-
end
82+
def reference_type(%{type: :integer, default: "nil", generated?: true}, _), do: ":bigserial"
83+
84+
def reference_type(%{type: type}, _), do: type
8785

8886
def with_match(reference, source_attribute \\ nil)
8987

@@ -489,6 +487,16 @@ defmodule AshPostgres.MigrationGenerator.Operation do
489487
|> join()
490488
end
491489

490+
def up(%{attribute: %{type: :identity} = attribute}) do
491+
[
492+
"add #{inspect(attribute.source)}",
493+
":identity",
494+
maybe_add_null(attribute.allow_nil?),
495+
maybe_add_primary_key(attribute.primary_key?)
496+
]
497+
|> join()
498+
end
499+
492500
def up(%{attribute: %{type: :bigint, default: "nil", generated?: true} = attribute}) do
493501
[
494502
"add #{inspect(attribute.source)}",
@@ -659,10 +667,14 @@ defmodule AshPostgres.MigrationGenerator.Operation do
659667
Map.get(old_attribute, :references) != Map.get(attribute, :references) do
660668
reference(multitenancy, attribute, schema)
661669
else
662-
if attribute.type == :bigint and attribute.default == "nil" and attribute.generated? do
663-
":bigserial"
670+
if attribute.type == :identity do
671+
":identity"
664672
else
665-
inspect(attribute.type)
673+
if attribute.type == :bigint and attribute.default == "nil" and attribute.generated? do
674+
":bigserial"
675+
else
676+
inspect(attribute.type)
677+
end
666678
end
667679
end
668680

lib/resource_generator/spec.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ defmodule AshPostgres.ResourceGenerator.Spec do
956956

957957
defp type("bigint"), do: {:ok, :integer}
958958
defp type("bigserial"), do: {:ok, :integer}
959+
defp type("identity"), do: {:ok, :identity}
959960
defp type("boolean"), do: {:ok, :boolean}
960961
defp type("bytea"), do: {:ok, :binary}
961962
defp type("varchar"), do: {:ok, :string}

test/filter_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,13 +1128,13 @@ defmodule AshPostgres.FilterTest do
11281128
|> Ash.Query.for_read(:read)
11291129
|> Ash.Query.filter(first_member.id != ^cm2.id)
11301130
|> Ash.read!()
1131-
|> length == 4
1131+
|> length() == 4
11321132

11331133
assert Channel
11341134
|> Ash.Query.for_read(:read)
11351135
|> Ash.Query.filter(first_member.id == ^cm2.id)
11361136
|> Ash.read!()
1137-
|> length == 1
1137+
|> length() == 1
11381138
end
11391139

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

11541154
assert Channel
11551155
|> Ash.Query.for_read(:read)
11561156
|> Ash.Query.filter(exists(first_member, id == ^cm1.id))
11571157
|> Ash.read!()
1158-
|> length == 1
1158+
|> length() == 1
11591159
end
11601160

11611161
test "using exists with has_many with limit" do

test/migration_generator_test.exs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,62 @@ defmodule AshPostgres.MigrationGeneratorTest do
18631863
end
18641864
end
18651865

1866+
describe "migration_types identity" do
1867+
setup %{snapshot_path: snapshot_path, migration_path: migration_path} do
1868+
defresource(IdentityPost) do
1869+
postgres do
1870+
table "identity_posts"
1871+
repo(AshPostgres.TestRepo)
1872+
migration_types(id: :identity, sequence_id: :identity)
1873+
end
1874+
1875+
actions do
1876+
defaults([:create, :read, :update, :destroy])
1877+
end
1878+
1879+
attributes do
1880+
integer_primary_key(:id)
1881+
1882+
attribute(:sequence_id, :integer,
1883+
generated?: true,
1884+
allow_nil?: false,
1885+
public?: true
1886+
)
1887+
1888+
attribute(:title, :string, public?: true)
1889+
end
1890+
end
1891+
1892+
defmodule IdentityDomain do
1893+
use Ash.Domain
1894+
1895+
resources do
1896+
resource(IdentityPost)
1897+
end
1898+
end
1899+
1900+
AshPostgres.MigrationGenerator.generate(IdentityDomain,
1901+
snapshot_path: snapshot_path,
1902+
migration_path: migration_path,
1903+
quiet: true,
1904+
format: false,
1905+
auto_name: true
1906+
)
1907+
1908+
:ok
1909+
end
1910+
1911+
test "uses :identity when set in migration_types", %{migration_path: migration_path} do
1912+
assert [file] =
1913+
Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")
1914+
|> Enum.reject(&String.contains?(&1, "extensions"))
1915+
1916+
file_contents = File.read!(file)
1917+
assert file_contents =~ ~S[add :id, :identity, null: false, primary_key: true]
1918+
assert file_contents =~ ~S[add :sequence_id, :identity, null: false]
1919+
end
1920+
end
1921+
18661922
describe "--check option" do
18671923
setup do
18681924
defposts do

0 commit comments

Comments
 (0)