Skip to content

Commit

Permalink
Remove Filter.new/1
Browse files Browse the repository at this point in the history
  • Loading branch information
robacourt committed Dec 2, 2024
1 parent 2eebd84 commit 5f3005a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
9 changes: 1 addition & 8 deletions packages/sync-service/lib/electric/shapes/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ defmodule Electric.Shapes.Filter do

defstruct tables: %{}

def new(shapes), do: shapes |> Map.to_list() |> new(empty())

defp new([{shape_id, shape} | shapes], filter),
do: new(shapes, add_shape(filter, shape_id, shape))

defp new([], filter), do: filter
def empty, do: %Filter{}

def add_shape(%Filter{tables: tables}, shape_id, shape) do
%Filter{
Expand Down Expand Up @@ -44,8 +39,6 @@ defmodule Electric.Shapes.Filter do
}
end

def empty, do: %Filter{}

def affected_shapes(%Filter{} = filter, %Relation{} = relation) do
# Check all shapes is all tables becuase the table may have been renamed
for {shape_id, shape} <- all_shapes_in_filter(filter),
Expand Down
19 changes: 10 additions & 9 deletions packages/sync-service/test/electric/shapes/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ defmodule Electric.Shapes.FilterTest do

@inspector StubInspector.new([%{name: "id", type: "int8", pk_position: 0}])

describe "new/1" do
describe "add_shape/2" do
test "with `field = constant` where clause" do
shape = Shape.new!("the_table", where: "id = 1", inspector: @inspector)

assert Filter.new(%{"shape1" => shape}) == %Filter{
assert Filter.add_shape(Filter.empty(), "shape1", shape) == %Filter{
tables: %{
{"public", "the_table"} => %Table{
indexes: %{
Expand All @@ -45,7 +45,7 @@ defmodule Electric.Shapes.FilterTest do
test "with `constant = field` where clause" do
shape = Shape.new!("the_table", where: "1 = id", inspector: @inspector)

assert Filter.new(%{"shape1" => shape}) == %Filter{
assert Filter.add_shape(Filter.empty(), "shape1", shape) == %Filter{
tables: %{
{"public", "the_table"} => %Table{
indexes: %{
Expand Down Expand Up @@ -97,7 +97,7 @@ defmodule Electric.Shapes.FilterTest do
other_shapes: %{}
}
}
} = Filter.new(%{"shape1" => shape})
} = Filter.add_shape(Filter.empty(), "shape1", shape)
end

test "with `some_condition AND field = constant` where clause" do
Expand Down Expand Up @@ -133,17 +133,17 @@ defmodule Electric.Shapes.FilterTest do
other_shapes: %{}
}
}
} = Filter.new(%{"shape1" => shape})
} = Filter.add_shape(Filter.empty(), "shape1", shape)
end

test "with more complicated where clause" do
shapes = %{"shape1" => Shape.new!("the_table", where: "id > 1", inspector: @inspector)}
shape = Shape.new!("the_table", where: "id > 1", inspector: @inspector)

assert Filter.new(shapes) == %Filter{
assert Filter.add_shape(Filter.empty(), "the-shape", shape) == %Filter{
tables: %{
{"public", "the_table"} => %Table{
indexes: %{},
other_shapes: shapes
other_shapes: %{"the-shape" => shape}
}
}
}
Expand Down Expand Up @@ -602,7 +602,8 @@ defmodule Electric.Shapes.FilterTest do
]
}

Filter.new(%{"the-shape" => shape})
Filter.empty()
|> Filter.add_shape("the-shape", shape)
|> Filter.affected_shapes(transaction) == MapSet.new(["the-shape"])
end
end
Expand Down

0 comments on commit 5f3005a

Please sign in to comment.