Add improved query for allSynonyms
This query works with views with subselects, and it's a lot faster, on a complex schema the previous query was taking around 8 mins, this query takes less than half a second(403.072 ms). Also reorder view embedding tests
This commit is contained in:
committed by
Steve Chávez
parent
6fc9d5191a
commit
1c6ded16d1
+57
-43
@@ -223,13 +223,12 @@ spec = do
|
||||
, matchHeaders = []
|
||||
}
|
||||
|
||||
|
||||
it "requesting parents and children" $
|
||||
get "/projects?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||
[json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "requesting parent without specifying primary key" $ do
|
||||
it "requesting parent without specifying primary key" $
|
||||
get "/projects?select=name,client(name)" `shouldRespondWith`
|
||||
[json|[
|
||||
{"name":"Windows 7","client":{"name": "Microsoft"}},
|
||||
@@ -239,9 +238,6 @@ spec = do
|
||||
{"name":"Orphan","client":null}
|
||||
]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/articleStars?select=createdAt,article(owner),user(name)&limit=1" `shouldRespondWith`
|
||||
[json|[{"createdAt":"2015-12-08T04:22:57.472738","article":{"owner": "postgrest_test_authenticator"},"user":{"name": "Angela Martin"}}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "requesting parent and renaming primary key" $
|
||||
get "/projects?select=name,client(clientId:id,name)" `shouldRespondWith`
|
||||
@@ -313,40 +309,11 @@ spec = do
|
||||
[json|[{"id":1,"tasks":[{"id":1},{"id":2},{"id":3},{"id":4}]},{"id":2,"tasks":[{"id":5},{"id":6},{"id":7}]},{"id":3,"tasks":[{"id":1},{"id":5}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "requesting parents and children on views" $
|
||||
get "/projects_view?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||
[json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "requesting parents and children on views with renamed keys" $
|
||||
get "/projects_view_alt?t_id=eq.1&select=t_id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||
[json|[{"t_id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "detects parent relations when having many views of a private table" $ do
|
||||
get "/books?select=title,author(name)&id=eq.5" `shouldRespondWith`
|
||||
[json|[ { "title": "Farenheit 451", "author": { "name": "Ray Bradbury" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/forties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||
[json|[ { "title": "1984", "author": { "name": "George Orwell" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/fifties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||
[json|[ { "title": "The Catcher in the Rye", "author": { "name": "J.D. Salinger" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/sixties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||
[json|[ { "title": "To Kill a Mockingbird", "author": { "name": "Harper Lee" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "requesting children with composite key" $
|
||||
get "/users_tasks?user_id=eq.2&task_id=eq.6&select=*, comments(content)" `shouldRespondWith`
|
||||
[json|[{"user_id":2,"task_id":6,"comments":[{"content":"Needs to be delivered ASAP"}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "detect relations in views from exposed schema that are based on tables in private schema and have columns renames" $
|
||||
get "/articles?id=eq.1&select=id,articleStars(users(*))" `shouldRespondWith`
|
||||
[json|[{"id":1,"articleStars":[{"users":{"id":1,"name":"Angela Martin"}},{"users":{"id":2,"name":"Michael Scott"}},{"users":{"id":3,"name":"Dwight Schrute"}}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "can embed by FK column name" $
|
||||
get "/projects?id=in.(1,3)&select=id,name,client_id(id,name)" `shouldRespondWith`
|
||||
[json|[{"id":1,"name":"Windows 7","client_id":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":{"id":2,"name":"Apple"}}]|]
|
||||
@@ -362,8 +329,53 @@ spec = do
|
||||
[json|[{"id":1,"name":"Windows 7","client_id":1,"client":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":2,"client":{"id":2,"name":"Apple"}}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "can detect fk relations through views to tables in the public schema" $
|
||||
get "/consumers_view?select=*,orders_view(*)" `shouldRespondWith` 200
|
||||
describe "view embedding" $ do
|
||||
it "can detect fk relations through views to tables in the public schema" $
|
||||
get "/consumers_view?select=*,orders_view(*)" `shouldRespondWith` 200
|
||||
|
||||
it "can request parent without specifying primary key" $
|
||||
get "/articleStars?select=createdAt,article(owner),user(name)&limit=1" `shouldRespondWith`
|
||||
[json|[{"createdAt":"2015-12-08T04:22:57.472738","article":{"owner": "postgrest_test_authenticator"},"user":{"name": "Angela Martin"}}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "can detect relations in views from exposed schema that are based on tables in private schema and have columns renames" $
|
||||
get "/articles?id=eq.1&select=id,articleStars(users(*))" `shouldRespondWith`
|
||||
[json|[{"id":1,"articleStars":[{"users":{"id":1,"name":"Angela Martin"}},{"users":{"id":2,"name":"Michael Scott"}},{"users":{"id":3,"name":"Dwight Schrute"}}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works when requesting parents and children on views" $
|
||||
get "/projects_view?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||
[json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works when requesting parents and children on views with renamed keys" $
|
||||
get "/projects_view_alt?t_id=eq.1&select=t_id, name, clients(*), tasks(id, name)" `shouldRespondWith`
|
||||
[json|[{"t_id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "detects parent relations when having many views of a private table" $ do
|
||||
get "/books?select=title,author(name)&id=eq.5" `shouldRespondWith`
|
||||
[json|[ { "title": "Farenheit 451", "author": { "name": "Ray Bradbury" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/forties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||
[json|[ { "title": "1984", "author": { "name": "George Orwell" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/fifties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||
[json|[ { "title": "The Catcher in the Rye", "author": { "name": "J.D. Salinger" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/sixties_books?select=title,author(name)&limit=1" `shouldRespondWith`
|
||||
[json|[ { "title": "To Kill a Mockingbird", "author": { "name": "Harper Lee" } } ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with views that have subselects" $ do
|
||||
get "/authors_books_number?select=*,books(title)&id=eq.1" `shouldRespondWith`
|
||||
[json|[ {"id":1, "name":"George Orwell","num_in_forties":1,"num_in_fifties":0,"num_in_sixties":0,"num_in_all_decades":1,
|
||||
"books":[{"title":"1984"}]} ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/authors_have_book_in_decade?select=*,books(title)&id=eq.3" `shouldRespondWith`
|
||||
[json|[ {"id":3,"name":"Antoine de Saint-Exupéry","has_book_in_forties":true,"has_book_in_fifties":false,"has_book_in_sixties":false,
|
||||
"books":[{"title":"The Little Prince"}]} ]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
describe "path fixed" $ do
|
||||
it "works when requesting children 2 levels" $
|
||||
@@ -371,13 +383,7 @@ spec = do
|
||||
[json|[{"id":1,"projects":[{"id":1,"tasks":[{"id":1},{"id":2}]},{"id":2,"tasks":[{"id":3},{"id":4}]}]}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with parent relation" $ do
|
||||
get "/message?select=id,body,sender:person_detail.sender(name,sent),recipient:person_detail.recipient(name,received)&id=lt.4" `shouldRespondWith`
|
||||
[json|
|
||||
[{"id":1,"body":"Hello Jane","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}},
|
||||
{"id":2,"body":"Hi John","sender":{"name":"Jane","sent":1},"recipient":{"name":"John","received":1}},
|
||||
{"id":3,"body":"How are you doing?","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}}] |]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
it "works with parent relation" $
|
||||
get "/message?select=id,body,sender:person.sender(name),recipient:person.recipient(name)&id=lt.4" `shouldRespondWith`
|
||||
[json|
|
||||
[{"id":1,"body":"Hello Jane","sender":{"name":"John"},"recipient":{"name":"Jane"}},
|
||||
@@ -385,6 +391,14 @@ spec = do
|
||||
{"id":3,"body":"How are you doing?","sender":{"name":"John"},"recipient":{"name":"Jane"}}] |]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with a parent view relation" $
|
||||
get "/message?select=id,body,sender:person_detail.sender(name,sent),recipient:person_detail.recipient(name,received)&id=lt.4" `shouldRespondWith`
|
||||
[json|
|
||||
[{"id":1,"body":"Hello Jane","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}},
|
||||
{"id":2,"body":"Hi John","sender":{"name":"Jane","sent":1},"recipient":{"name":"John","received":1}},
|
||||
{"id":3,"body":"How are you doing?","sender":{"name":"John","sent":2},"recipient":{"name":"Jane","received":2}}] |]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works with many<->many relation" $
|
||||
get "/tasks?select=id,users:users.users_tasks(id)" `shouldRespondWith`
|
||||
[json|[{"id":1,"users":[{"id":1},{"id":3}]},{"id":2,"users":[{"id":1}]},{"id":3,"users":[{"id":1}]},{"id":4,"users":[{"id":1}]},{"id":5,"users":[{"id":2},{"id":3}]},{"id":6,"users":[{"id":2}]},{"id":7,"users":[{"id":2}]},{"id":8,"users":[]}]|]
|
||||
|
||||
Vendored
+2
@@ -82,6 +82,8 @@ GRANT ALL ON TABLE
|
||||
, "UnitTest"
|
||||
, json_arr
|
||||
, jsonb_test
|
||||
, authors_books_number
|
||||
, authors_have_book_in_decade
|
||||
TO postgrest_test_anonymous;
|
||||
|
||||
GRANT INSERT ON TABLE insertonly TO postgrest_test_anonymous;
|
||||
|
||||
Vendored
+69
-13
@@ -1120,17 +1120,17 @@ create view images_base64 as (
|
||||
select name, replace(encode(img, 'base64'), E'\n', '') as img from images
|
||||
);
|
||||
|
||||
create function test.ret_enum(val text) returns test.enum_menagerie_type as $$
|
||||
create function test.ret_enum(val text) returns test.enum_menagerie_type as $$
|
||||
select val::test.enum_menagerie_type;
|
||||
$$ language sql;
|
||||
|
||||
create domain one_nine as integer check (value >= 1 and value <= 9);
|
||||
|
||||
create function test.ret_array() returns integer[] as $$
|
||||
create function test.ret_array() returns integer[] as $$
|
||||
select '{1,2,3}'::integer[];
|
||||
$$ language sql;
|
||||
|
||||
create function test.ret_domain(val integer) returns test.one_nine as $$
|
||||
create function test.ret_domain(val integer) returns test.one_nine as $$
|
||||
select val::test.one_nine;
|
||||
$$ language sql;
|
||||
|
||||
@@ -1144,20 +1144,20 @@ $$ language sql;
|
||||
|
||||
create function test.ret_scalars() returns table(
|
||||
a text, b test.enum_menagerie_type, c test.one_nine, d int4range
|
||||
) as $$
|
||||
select row('scalars'::text, enum_first(null::test.enum_menagerie_type),
|
||||
) as $$
|
||||
select row('scalars'::text, enum_first(null::test.enum_menagerie_type),
|
||||
1::test.one_nine, int4range(10, 20));
|
||||
$$ language sql;
|
||||
|
||||
create type test.point_2d as (x integer, y integer);
|
||||
|
||||
create function test.ret_point_2d() returns test.point_2d as $$
|
||||
create function test.ret_point_2d() returns test.point_2d as $$
|
||||
select row(10, 5)::test.point_2d;
|
||||
$$ language sql;
|
||||
|
||||
create type private.point_3d as (x integer, y integer, z integer);
|
||||
|
||||
create function test.ret_point_3d() returns private.point_3d as $$
|
||||
create function test.ret_point_3d() returns private.point_3d as $$
|
||||
select row(7, -3, 4)::private.point_3d;
|
||||
$$ language sql;
|
||||
|
||||
@@ -1171,17 +1171,17 @@ create function test.ret_rows_with_base64_bin() returns setof test.images_base64
|
||||
select i.name, i.img from test.images_base64 i;
|
||||
$$ language sql;
|
||||
|
||||
create function test.single_article(id integer) returns test.articles as $$
|
||||
create function test.single_article(id integer) returns test.articles as $$
|
||||
select a.* from test.articles a where a.id = $1;
|
||||
$$ language sql;
|
||||
|
||||
create function test.get_guc_value(name text) returns text as $$
|
||||
create function test.get_guc_value(name text) returns text as $$
|
||||
select nullif(current_setting(name), '')::text;
|
||||
$$ language sql;
|
||||
|
||||
create table w_or_wo_comma_names ( name text );
|
||||
|
||||
create table items_with_different_col_types (
|
||||
create table items_with_different_col_types (
|
||||
int_data integer,
|
||||
text_data text,
|
||||
bool_data bool,
|
||||
@@ -1194,20 +1194,20 @@ create table items_with_different_col_types (
|
||||
|
||||
-- Tables used for testing complex boolean logic with and/or query params
|
||||
|
||||
create table entities (
|
||||
create table entities (
|
||||
id integer primary key,
|
||||
name text,
|
||||
arr integer[],
|
||||
text_search_vector tsvector
|
||||
);
|
||||
|
||||
create table child_entities (
|
||||
create table child_entities (
|
||||
id integer primary key,
|
||||
name text,
|
||||
parent_id integer references entities(id)
|
||||
);
|
||||
|
||||
create table grandchild_entities (
|
||||
create table grandchild_entities (
|
||||
id integer primary key,
|
||||
name text,
|
||||
parent_id integer references child_entities(id),
|
||||
@@ -1466,3 +1466,59 @@ create table jsonb_test(
|
||||
id integer primary key,
|
||||
data jsonb
|
||||
);
|
||||
|
||||
create view test.authors_books_number as
|
||||
select
|
||||
id,
|
||||
name,
|
||||
(
|
||||
select
|
||||
count(*)
|
||||
from forties_books where author_id = authors.id
|
||||
) as num_in_forties,
|
||||
(
|
||||
select
|
||||
count(*)
|
||||
from fifties_books where author_id = authors.id
|
||||
) as num_in_fifties,
|
||||
(
|
||||
select
|
||||
count(*)
|
||||
from sixties_books where author_id = authors.id
|
||||
) as num_in_sixties,
|
||||
(
|
||||
select
|
||||
count(*)
|
||||
from (
|
||||
select id
|
||||
from forties_books where author_id = authors.id
|
||||
union
|
||||
select id
|
||||
from fifties_books where author_id = authors.id
|
||||
union
|
||||
select id
|
||||
from sixties_books where author_id = authors.id
|
||||
) _
|
||||
) as num_in_all_decades
|
||||
from private.authors;
|
||||
|
||||
create view test.authors_have_book_in_decade as
|
||||
select
|
||||
id,
|
||||
name,
|
||||
CASE
|
||||
WHEN (x.id IN (SELECT author_id FROM test.forties_books))
|
||||
THEN true
|
||||
ELSE false
|
||||
END AS has_book_in_forties,
|
||||
CASE
|
||||
WHEN (x.id IN (SELECT author_id FROM test.fifties_books))
|
||||
THEN true
|
||||
ELSE false
|
||||
END AS has_book_in_fifties,
|
||||
CASE
|
||||
WHEN (x.id IN (SELECT author_id FROM test.sixties_books))
|
||||
THEN true
|
||||
ELSE false
|
||||
END AS has_book_in_sixties
|
||||
from private.authors x;
|
||||
|
||||
Reference in New Issue
Block a user