-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: Allow resources to opt out of the primary key requiremen…
…t. (#166)
- Loading branch information
Showing
9 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
priv/resource_snapshots/test_repo/post_views/20230905050351.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"attributes": [ | ||
{ | ||
"default": "fragment(\"now()\")", | ||
"size": null, | ||
"type": "utc_datetime_usec", | ||
"source": "time", | ||
"references": null, | ||
"allow_nil?": false, | ||
"primary_key?": false, | ||
"generated?": false | ||
}, | ||
{ | ||
"default": "nil", | ||
"size": null, | ||
"type": "text", | ||
"source": "browser", | ||
"references": null, | ||
"allow_nil?": true, | ||
"primary_key?": false, | ||
"generated?": false | ||
}, | ||
{ | ||
"default": "nil", | ||
"size": null, | ||
"type": "uuid", | ||
"source": "post_id", | ||
"references": null, | ||
"allow_nil?": false, | ||
"primary_key?": false, | ||
"generated?": false | ||
} | ||
], | ||
"table": "post_views", | ||
"hash": "45E84815BD6E5B9617B491C57A429F210D98AE37428AC7C210B2E9D6AEA27DB3", | ||
"repo": "Elixir.AshPostgres.TestRepo", | ||
"schema": null, | ||
"check_constraints": [], | ||
"identities": [], | ||
"custom_indexes": [], | ||
"multitenancy": { | ||
"global": null, | ||
"attribute": null, | ||
"strategy": null | ||
}, | ||
"base_filter": null, | ||
"custom_statements": [], | ||
"has_create_action": true | ||
} |
21 changes: 21 additions & 0 deletions
21
priv/test_repo/migrations/20230905050351_add_post_views.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule AshPostgres.TestRepo.Migrations.AddPostViews do | ||
@moduledoc """ | ||
Updates resources based on their most recent snapshots. | ||
This file was autogenerated with `mix ash_postgres.generate_migrations` | ||
""" | ||
|
||
use Ecto.Migration | ||
|
||
def up do | ||
create table(:post_views, primary_key: false) do | ||
add :time, :utc_datetime_usec, null: false, default: fragment("now()") | ||
add :browser, :text | ||
add :post_id, :uuid, null: false | ||
end | ||
end | ||
|
||
def down do | ||
drop table(:post_views) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,51 @@ | ||
defmodule AshPostgres.Test.PrimaryKeyTest do | ||
@moduledoc false | ||
use AshPostgres.RepoCase, async: false | ||
alias AshPostgres.Test.{Api, IntegerPost, Post} | ||
alias AshPostgres.Test.{Api, IntegerPost, Post, PostView} | ||
|
||
require Ash.Query | ||
|
||
test "creates resource with integer primary key" do | ||
test "creates record with integer primary key" do | ||
assert %IntegerPost{} = IntegerPost |> Ash.Changeset.new(%{title: "title"}) |> Api.create!() | ||
end | ||
|
||
test "creates resource with uuid primary key" do | ||
test "creates record with uuid primary key" do | ||
assert %Post{} = Post |> Ash.Changeset.new(%{title: "title"}) |> Api.create!() | ||
end | ||
|
||
describe "resources without a primary key" do | ||
test "records can be created" do | ||
post = | ||
Post | ||
|> Ash.Changeset.for_action(:create, %{title: "not very interesting"}) | ||
|> Api.create!() | ||
|
||
assert {:ok, view} = | ||
PostView | ||
|> Ash.Changeset.for_action(:create, %{browser: :firefox, post_id: post.id}) | ||
|> Api.create() | ||
|
||
assert view.browser == :firefox | ||
assert view.post_id == post.id | ||
assert DateTime.diff(DateTime.utc_now(), view.time, :microsecond) < 1_000_000 | ||
end | ||
|
||
test "records can be queried" do | ||
post = | ||
Post | ||
|> Ash.Changeset.for_action(:create, %{title: "not very interesting"}) | ||
|> Api.create!() | ||
|
||
expected = | ||
PostView | ||
|> Ash.Changeset.for_action(:create, %{browser: :firefox, post_id: post.id}) | ||
|> Api.create!() | ||
|
||
assert {:ok, [actual]} = Api.read(PostView) | ||
|
||
assert actual.time == expected.time | ||
assert actual.browser == expected.browser | ||
assert actual.post_id == expected.post_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule AshPostgres.Test.PostView do | ||
@moduledoc false | ||
use Ash.Resource, data_layer: AshPostgres.DataLayer | ||
|
||
actions do | ||
defaults([:create, :read]) | ||
end | ||
|
||
attributes do | ||
create_timestamp(:time) | ||
attribute(:browser, :atom, constraints: [one_of: [:firefox, :chrome, :edge]]) | ||
end | ||
|
||
relationships do | ||
belongs_to :post, AshPostgres.Test.Post do | ||
allow_nil?(false) | ||
attribute_writable?(true) | ||
end | ||
end | ||
|
||
resource do | ||
require_primary_key?(false) | ||
end | ||
|
||
postgres do | ||
table "post_views" | ||
repo AshPostgres.TestRepo | ||
|
||
references do | ||
reference :post, ignore?: true | ||
end | ||
end | ||
end |