From f0ea9e6bfe0ba21f3057acdc1774e48309dbeb05 Mon Sep 17 00:00:00 2001 From: Malte Rohde Date: Mon, 4 Sep 2023 16:22:01 +0200 Subject: [PATCH] Allow integer PKs in Repo.fetch/2 --- CHANGELOG.md | 6 ++++++ lib/bitcrowd_ecto/repo.ex | 2 +- test/bitcrowd_ecto/repo_test.exs | 5 +++++ test/support/factory.ex | 4 ++++ test/support/serial_primary_key_test_schema.ex | 10 ++++++++++ test/support/test_case.ex | 1 + ...904120000_create_serial_primary_key_test_schema.exs | 10 ++++++++++ 7 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/support/serial_primary_key_test_schema.ex create mode 100644 test/support/test_repo/migrations/20230904120000_create_serial_primary_key_test_schema.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index 64d9e99..d494bb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ +## Unreleased + +### Fixed + +* Allow any type of `id` param in `Repo.fetch/2`. Remove the (incorrect) guard restricting the `id` param to binaries, against the spec saying it would allow `any`. + ## [0.16.0] - 2023-03-21 ### Added diff --git a/lib/bitcrowd_ecto/repo.ex b/lib/bitcrowd_ecto/repo.ex index cb0f860..1908582 100644 --- a/lib/bitcrowd_ecto/repo.ex +++ b/lib/bitcrowd_ecto/repo.ex @@ -156,7 +156,7 @@ defmodule BitcrowdEcto.Repo do @behaviour BER @impl BER - def fetch(module, id, opts \\ []) when is_atom(module) and is_binary(id) do + def fetch(module, id, opts \\ []) when is_atom(module) do BER.fetch(__MODULE__, module, id, opts) end diff --git a/test/bitcrowd_ecto/repo_test.exs b/test/bitcrowd_ecto/repo_test.exs index 990e0a4..146c772 100644 --- a/test/bitcrowd_ecto/repo_test.exs +++ b/test/bitcrowd_ecto/repo_test.exs @@ -41,6 +41,11 @@ defmodule BitcrowdEcto.RepoTest do test "converts CastErrors to not_found errors" do assert TestRepo.fetch(TestSchema, "doesnotcast") == {:error, {:not_found, TestSchema}} end + + test "handles any type of primary key (serial, uuid, etc)" do + resource = insert(:serial_primary_key_test_schema) + assert TestRepo.fetch(SerialPrimaryKeyTestSchema, resource.id) == {:ok, resource} + end end describe "fetch/2 with schemas with non-standard primary key" do diff --git a/test/support/factory.ex b/test/support/factory.ex index 223edad..42407ca 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -17,4 +17,8 @@ defmodule BitcrowdEcto.Factory do name: sequence("name") } end + + def serial_primary_key_test_schema_factory do + %BitcrowdEcto.SerialPrimaryKeyTestSchema{} + end end diff --git a/test/support/serial_primary_key_test_schema.ex b/test/support/serial_primary_key_test_schema.ex new file mode 100644 index 0000000..37f0877 --- /dev/null +++ b/test/support/serial_primary_key_test_schema.ex @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: Apache-2.0 + +defmodule BitcrowdEcto.SerialPrimaryKeyTestSchema do + @moduledoc false + + use BitcrowdEcto.Schema + + schema "serial_primary_key_test_schema" do + end +end diff --git a/test/support/test_case.ex b/test/support/test_case.ex index e824e99..2acf352 100644 --- a/test/support/test_case.ex +++ b/test/support/test_case.ex @@ -17,6 +17,7 @@ defmodule BitcrowdEcto.TestCase do alias BitcrowdEcto.{ AlternativePrimaryKeyTestSchema, + SerialPrimaryKeyTestSchema, TestRepo, TestSchema, TestSchemaWithPrefix diff --git a/test/support/test_repo/migrations/20230904120000_create_serial_primary_key_test_schema.exs b/test/support/test_repo/migrations/20230904120000_create_serial_primary_key_test_schema.exs new file mode 100644 index 0000000..4e1fdcd --- /dev/null +++ b/test/support/test_repo/migrations/20230904120000_create_serial_primary_key_test_schema.exs @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: Apache-2.0 + +defmodule BitcrowdEcto.TestRepo.Migrations.CreateSerialPrimaryKeyTestSchema do + use Ecto.Migration + + def change do + create table(:serial_primary_key_test_schema) do + end + end +end