Skip to content

Commit

Permalink
fix(electric): CaseClauseError in Proxy.Prisma.parse_bind_array() (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetised authored Nov 7, 2023
1 parent f57a49b commit a700758
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-rivers-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/electric": patch
---

[VAX-1319] Fix CaseClauseError in Proxy.Prisma.parse_bind_array()
23 changes: 13 additions & 10 deletions components/electric/lib/electric/postgres/proxy/prisma.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,22 @@ defmodule Electric.Postgres.Proxy.Prisma do
# >>
def parse_bind_array(encoded_array) do
case encoded_array do
<<
1::i32(),
0::i32(),
19::i32(),
1::i32(),
1::i32(),
len::i32(),
value::binary-size(len)
>> ->
[value]
<<1::i32(), 0::i32(), 19::i32(), 0::i32(), _::i32()>> ->
[]

<<1::i32(), 0::i32(), 19::i32(), n::i32(), 1::i32(), rest::binary>> ->
decode_array_values(rest, n, [])
end
end

defp decode_array_values(<<>>, 0, acc) do
Enum.reverse(acc)
end

defp decode_array_values(<<len::i32(), value::binary-size(len), rest::binary>>, n, acc) do
decode_array_values(rest, n - 1, [value | acc])
end

def injector(config, opts \\ []) do
capture = {Injector.Prisma, config: config}

Expand Down
30 changes: 24 additions & 6 deletions components/electric/test/electric/postgres/proxy/prisma_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,30 @@ defmodule Electric.Postgres.Proxy.PrismaTest do
end
end

test "parse_bind_array/1" do
assert ["public"] =
Prisma.parse_bind_array(
<<0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 6, 112,
117, 98, 108, 105, 99>>
)
describe "parse_bind_array/1" do
test "parses a single item array" do
assert ["public"] =
Prisma.parse_bind_array(
<<0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 6, 112,
117, 98, 108, 105, 99>>
)
end

test "parses a multi-element array" do
assert ["public", "private", "blue"] =
Prisma.parse_bind_array(
<<0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 6, 112,
117, 98, 108, 105, 99, 0, 0, 0, 7, 112, 114, 105, 118, 97, 116, 101, 0, 0, 0,
4, 98, 108, 117, 101>>
)
end

test "parses empty array" do
assert [] =
Prisma.parse_bind_array(
<<0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 1>>
)
end
end

describe "injector" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

[invoke setup]

[newshell developer]
!docker ps
?SH-PROMPT:
!mkdir -p ../prisma_example/prisma/
?SH-PROMPT:
!cp ../prisma_example/schema.prisma ../prisma_example/prisma/
?SH-PROMPT:
!make docker-prisma ARGS='prisma_example_1 prisma db pull'
?The introspected database was empty
?SH-PROMPT:

[global migration_version_1=20230504114018]

[shell proxy_1]
Expand Down Expand Up @@ -86,7 +97,7 @@
?? [info] Applying migration $migration_version_2


[newshell developer]
[shell developer]
!docker ps
?SH-PROMPT:
!mkdir -p ../prisma_example/prisma/
Expand Down

0 comments on commit a700758

Please sign in to comment.