Skip to content

Commit

Permalink
fix: aggregates not working for all schemas
Browse files Browse the repository at this point in the history
Closes #3124
  • Loading branch information
steve-chavez committed Dec 20, 2023
1 parent 0b77098 commit 1680a6e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- #2887, Add Preference `max-affected` to limit affected resources - @taimoorzaeem

### Fixed

- #3124, Fix table's media type handlers not working for all schemas - @steve-chavez

## [12.0.1] - 2023-12-12

### Fixed
Expand Down
5 changes: 3 additions & 2 deletions src/PostgREST/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ mediaHandlers pgVer =
join pg_type arg_name on arg_name.oid = proc.proargtypes[0]
join pg_namespace arg_schema on arg_schema.oid = arg_name.typnamespace
where
proc_schema.nspname = ANY('{test}') and
proc_schema.nspname = ANY($1) and
proc.pronargs = 1 and
arg_name.oid in (select reltype from all_relations)
union
Expand All @@ -1182,7 +1182,8 @@ mediaHandlers pgVer =
join pg_namespace pro_sch on pro_sch.oid = proc.pronamespace
join media_types mtype on proc.prorettype = mtype.oid
join pg_namespace typ_sch on typ_sch.oid = mtype.typnamespace
where NOT proretset
where
pro_sch.nspname = ANY($1) and NOT proretset
|] <> (if pgVer >= pgVersion110 then " AND prokind = 'f'" else " AND NOT (proisagg OR proiswindow)")

decodeMediaHandlers :: HD.Result MediaHandlerMap
Expand Down
2 changes: 1 addition & 1 deletion test/memory/memory-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ postJsonArrayTest(){

echo "Running memory usage tests.."

jsonKeyTest "1M" "POST" "/rpc/leak?columns=blob" "26M"
jsonKeyTest "1M" "POST" "/rpc/leak?columns=blob" "27M"
jsonKeyTest "1M" "POST" "/leak?columns=blob" "16M"
jsonKeyTest "1M" "PATCH" "/leak?id=eq.1&columns=blob" "16M"

Expand Down
28 changes: 28 additions & 0 deletions test/spec/Feature/Query/MultipleSchemaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ spec =
matchStatus = 406
}

it "succeeds in calling handler with a domain on another schema" $
request methodGet "/another_table" [("Accept-Profile", "v2"), (hAccept, "text/plain")] ""
`shouldRespondWith` "plain"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "text/plain; charset=utf-8", "Content-Profile" <:> "v2"]
}

it "succeeds in calling handler with a domain on an exposed schema" $
request methodGet "/another_table" [("Accept-Profile", "v2"), (hAccept, "text/special")] ""
`shouldRespondWith` "special"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "text/special", "Content-Profile" <:> "v2"]
}

context "calling procs on different schemas" $ do
it "succeeds in calling the default schema proc" $
request methodGet "/rpc/get_parents_below?id=6" [] ""
Expand Down Expand Up @@ -194,6 +208,20 @@ spec =
, matchHeaders = [matchContentTypeJson, "Content-Profile" <:> "v2"]
}

it "succeeds in calling handler with a domain on another schema" $
request methodGet "/rpc/get_plain_text" [("Accept-Profile", "v2"), (hAccept, "text/plain")] ""
`shouldRespondWith` "plain"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "text/plain; charset=utf-8", "Content-Profile" <:> "v2"]
}

it "succeeds in calling handler with a domain on an exposed schema" $
request methodGet "/rpc/get_special_text" [("Accept-Profile", "v2"), (hAccept, "text/special")] ""
`shouldRespondWith` "special"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "text/special", "Content-Profile" <:> "v2"]
}

context "Modifying tables on different schemas" $ do
it "succeeds in patching on the v1 schema and returning its parent" $
request methodPatch "/children?select=name,parent(name)&id=eq.1" [("Content-Profile", "v1"), ("Prefer", "return=representation")]
Expand Down
36 changes: 36 additions & 0 deletions test/spec/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,42 @@ returns setof v2.parents as $$
select * from v2.parents where id < $1;
$$ language sql;

create function v2.get_plain_text()
returns test."text/plain" as $$
select 'plain'::test."text/plain";
$$ language sql;

create domain v2."text/special" as text;

create function v2.get_special_text()
returns v2."text/special" as $$
select 'special'::v2."text/special";
$$ language sql;

create or replace function v2.special_trans (state v2."text/special", next v2.another_table)
returns v2."text/special" as $$
select 'special'::v2."text/special";
$$ language sql;

drop aggregate if exists v2.special_agg(v2.another_table);
create aggregate v2.special_agg (v2.another_table) (
initcond = ''
, stype = v2."text/special"
, sfunc = v2.special_trans
);

create or replace function v2.plain_trans (state test."text/plain", next v2.another_table)
returns test."text/plain" as $$
select 'plain'::test."text/plain";
$$ language sql;

drop aggregate if exists v2.plain_agg(v2.another_table);
create aggregate v2.plain_agg (v2.another_table) (
initcond = ''
, stype = test."text/plain"
, sfunc = v2.plain_trans
);

create table private.screens (
id serial primary key,
name text not null default 'new screen'
Expand Down

0 comments on commit 1680a6e

Please sign in to comment.