Skip to content

Commit

Permalink
Merge pull request #51 from bitcrowd/allow-integer-pks-for-repo-fetch
Browse files Browse the repository at this point in the history
Allow integer PKs in Repo.fetch/2
  • Loading branch information
maltoe authored Sep 5, 2023
2 parents 206b937 + f0ea9e6 commit 55e8f8a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<!-- SPDX-License-Identifier: Apache-2.0 -->

## 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
Expand Down
2 changes: 1 addition & 1 deletion lib/bitcrowd_ecto/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions test/bitcrowd_ecto/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ defmodule BitcrowdEcto.Factory do
name: sequence("name")
}
end

def serial_primary_key_test_schema_factory do
%BitcrowdEcto.SerialPrimaryKeyTestSchema{}
end
end
10 changes: 10 additions & 0 deletions test/support/serial_primary_key_test_schema.ex
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/support/test_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule BitcrowdEcto.TestCase do

alias BitcrowdEcto.{
AlternativePrimaryKeyTestSchema,
SerialPrimaryKeyTestSchema,
TestRepo,
TestSchema,
TestSchemaWithPrefix
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 55e8f8a

Please sign in to comment.