From a7007589df5cef0b4a80f6e43d24ca47ee6f6015 Mon Sep 17 00:00:00 2001 From: Garry Hill Date: Tue, 7 Nov 2023 11:36:14 +0000 Subject: [PATCH] fix(electric): CaseClauseError in Proxy.Prisma.parse_bind_array() (#632) --- .changeset/light-rivers-invite.md | 5 ++++ .../lib/electric/postgres/proxy/prisma.ex | 23 +++++++------- .../electric/postgres/proxy/prisma_test.exs | 30 +++++++++++++++---- ...introspection_generates_correct_schema.lux | 13 +++++++- 4 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 .changeset/light-rivers-invite.md diff --git a/.changeset/light-rivers-invite.md b/.changeset/light-rivers-invite.md new file mode 100644 index 0000000000..86f82ad11d --- /dev/null +++ b/.changeset/light-rivers-invite.md @@ -0,0 +1,5 @@ +--- +"@core/electric": patch +--- + +[VAX-1319] Fix CaseClauseError in Proxy.Prisma.parse_bind_array() diff --git a/components/electric/lib/electric/postgres/proxy/prisma.ex b/components/electric/lib/electric/postgres/proxy/prisma.ex index 3e80d7448f..35aa237d48 100644 --- a/components/electric/lib/electric/postgres/proxy/prisma.ex +++ b/components/electric/lib/electric/postgres/proxy/prisma.ex @@ -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(<>, n, acc) do + decode_array_values(rest, n - 1, [value | acc]) + end + def injector(config, opts \\ []) do capture = {Injector.Prisma, config: config} diff --git a/components/electric/test/electric/postgres/proxy/prisma_test.exs b/components/electric/test/electric/postgres/proxy/prisma_test.exs index e55d7d6a4c..7b689007b8 100644 --- a/components/electric/test/electric/postgres/proxy/prisma_test.exs +++ b/components/electric/test/electric/postgres/proxy/prisma_test.exs @@ -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 diff --git a/e2e/tests/04.02_prisma_introspection_generates_correct_schema.lux b/e2e/tests/04.02_prisma_introspection_generates_correct_schema.lux index b8cf76b137..ecd0018984 100644 --- a/e2e/tests/04.02_prisma_introspection_generates_correct_schema.lux +++ b/e2e/tests/04.02_prisma_introspection_generates_correct_schema.lux @@ -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] @@ -86,7 +97,7 @@ ?? [info] Applying migration $migration_version_2 -[newshell developer] +[shell developer] !docker ps ?SH-PROMPT: !mkdir -p ../prisma_example/prisma/