Skip to content

Commit

Permalink
END-013 - Fixes and unit tests for querying table w/ schema prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vereis committed Jan 17, 2024
1 parent 9439d04 commit 0d3ff93
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ alias Credo.Check
{Check.Readability.SinglePipe, []},
{Check.Readability.StrictModuleLayout, []},
{Check.Readability.WithCustomTaggedTuple, []},
{Check.Refactor.ABCSize, [max_size: 60]},
{Check.Refactor.ABCSize, [max_size: 80]},
{Check.Refactor.DoubleBooleanNegation, []},
{Check.Refactor.FilterReject, []},
{Check.Refactor.MapMap, []},
Expand Down
3 changes: 2 additions & 1 deletion lib/endo/adapters/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ defmodule Endo.Adapters.Postgres do

@spec list_tables(repo :: module(), opts :: Keyword.t()) :: [Table.t()]
def list_tables(repo, opts \\ []) when is_atom(repo) do
opts = Keyword.put_new(opts, :prefix, Endo.table_schema())
preloads = [:columns, table_constraints: [:key_column_usage, :constraint_column_usage]]

derive_preloads = fn %Table{table_name: name} = table ->
indexes = PgClass.query(collate_indexes: true, relname: name)
metadata = PgClass.query(relname: name, relkind: ~w(r t m f p))
size = Size.query(relname: name)
size = Size.query(relname: name, prefix: opts[:prefix])

%Table{
table
Expand Down
6 changes: 4 additions & 2 deletions lib/endo/adapters/postgres/size.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ defmodule Endo.Adapters.Postgres.Size do

import Ecto.Query

@spec query([{:relname, String.t()}]) :: Ecto.Queryable.t()
def query(relname: relname) do
@spec query([{:relname, String.t()}, {:prefix, String.t()}]) :: Ecto.Queryable.t()
def query(opts) do
relname = Enum.join([opts[:prefix], opts[:relname]], ".")

select(
with_cte("virtual_table", "virtual_table",
as:
Expand Down
4 changes: 1 addition & 3 deletions lib/endo/adapters/postgres/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ defmodule Endo.Adapters.Postgres.Table do

@impl Endo.Queryable
def query(base_query \\ base_query(), filters) do
filters
|> Keyword.put_new(:prefix, Endo.table_schema())
|> Enum.reduce(base_query, fn
Enum.reduce(filters, base_query, fn
{:prefix, prefix}, query when is_binary(prefix) ->
from(x in query, where: x.table_schema == ^prefix)

Expand Down
4 changes: 4 additions & 0 deletions lib/endo/queryable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ defmodule Endo.Queryable do
@callback query(base_query :: Ecto.Queryable.t(), Keyword.t()) :: Ecto.Queryable.t()

@spec apply_filter(Ecto.Queryable.t(), field :: atom(), value :: any()) :: Ecto.Queryable.t()
def apply_filter(query, :prefix, value) do
from(x in query, prefix: ^value)
end

def apply_filter(query, :preload, value) do
from(x in query, preload: ^value)
end
Expand Down
14 changes: 14 additions & 0 deletions priv/repo/migrations/20221221123643_bootstrap_testing_env.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ defmodule Test.Postgres.Repo.Migrations.BootstrapTestingEnv do
create_accounts()
create_repos()
create_orgs()
create_prefix_table()
end

def create_prefix_table do
execute("CREATE SCHEMA debug")

flush()

create table(:events, prefix: "debug") do
add(:type, :string, null: false)
add(:data, :map, null: false)

timestamps()
end
end

defp create_accounts do
Expand Down
13 changes: 13 additions & 0 deletions test/endo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ defmodule EndoTest do
test "given invalid table name, but valid repo, returns nil" do
assert is_nil(Endo.get_table(Test.Postgres.Repo, "passports"))
end

test "returns nothing when querying table belonging to non-default prefix when not specified" do
assert is_nil(Endo.get_table(Test.Postgres.Repo, "events"))
end

test "returns nothing when querying table belonging to incorrectly specified prefix" do
assert is_nil(Endo.get_table(Test.Postgres.Repo, "events", prefix: "something_random"))
end

test "returns table belonging to non-default prefix if specified correctly" do
assert %Endo.Table{name: "events"} =
Endo.get_table(Test.Postgres.Repo, "events", prefix: "debug")
end
end

describe "list_tables/2 (Postgres)" do
Expand Down

0 comments on commit 0d3ff93

Please sign in to comment.