Skip to content

Commit c4d93cb

Browse files
authored
Merge pull request #23 from mongodb-developer/fix-stored-procedures
fix stored procedures that are not working in the new schema
2 parents 0ac395d + 94ed207 commit c4d93cb

File tree

5 files changed

+1108
-37
lines changed

5 files changed

+1108
-37
lines changed

docker/sample-postgres-library/init/1-library-schema-and-data.sql

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,18 @@ ALTER FUNCTION library.generate_random_tags() OWNER TO postgres;
139139
-- Name: get_books_borrowed_by_user(integer); Type: FUNCTION; Schema: library; Owner: postgres
140140
--
141141

142-
CREATE FUNCTION library.get_books_borrowed_by_user(user_id_param integer) RETURNS TABLE(title character varying, borrow_date timestamp without time zone, due_date timestamp without time zone, returned boolean)
143-
LANGUAGE plpgsql
144-
AS $$
142+
CREATE FUNCTION library.get_books_borrowed_by_user(user_id_param integer)
143+
RETURNS TABLE(title character varying, borrow_date timestamp without time zone, due_date timestamp without time zone, returned boolean)
144+
LANGUAGE plpgsql
145+
AS $function$
145146
BEGIN
146147
RETURN QUERY
147-
SELECT b.title, id.borrow_date, id.due_date, id.returned
148-
FROM operations id
149-
JOIN books b ON id.book_id = b.id
150-
WHERE id.user_id = user_id_param;
148+
SELECT b.title, ops.borrow_date, ops.due_date, ops.returned
149+
FROM library.operations ops
150+
JOIN library.books b ON ops.book_id = b.id
151+
WHERE ops.user_id = user_id_param;
151152
END;
152-
$$;
153-
153+
$function$;
154154

155155
ALTER FUNCTION library.get_books_borrowed_by_user(user_id_param integer) OWNER TO postgres;
156156

@@ -159,12 +159,19 @@ ALTER FUNCTION library.get_books_borrowed_by_user(user_id_param integer) OWNER T
159159
-- Name: get_books_by_author(integer); Type: FUNCTION; Schema: library; Owner: postgres
160160
--
161161

162-
CREATE FUNCTION library.get_books_by_author(author_id integer) RETURNS TABLE(id character varying, title character varying, pages integer, year integer, synopsis character varying, cover character varying, "totalInventory" integer, available integer, binding character varying, language character varying, publisher character varying, "longTitle" character varying, "bookOfTheMonth" boolean)
163-
LANGUAGE sql
164-
AS $$SELECT * FROM books
165-
WHERE id IN
166-
(SELECT book_id from author_book
167-
WHERE author_id = 2)$$;
162+
CREATE FUNCTION library.get_books_by_author(author_id_param integer)
163+
RETURNS SETOF library.books
164+
LANGUAGE plpgsql
165+
AS $function$
166+
BEGIN
167+
RETURN QUERY
168+
SELECT * FROM library.books b
169+
WHERE b.id IN (
170+
SELECT ab.book_id
171+
FROM library.author_book ab
172+
WHERE ab.author_id = author_id_param);
173+
END
174+
$function$;
168175

169176

170177
ALTER FUNCTION library.get_books_by_author(author_id integer) OWNER TO postgres;
@@ -174,17 +181,22 @@ ALTER FUNCTION library.get_books_by_author(author_id integer) OWNER TO postgres;
174181
-- Name: get_books_by_genre(character varying); Type: FUNCTION; Schema: library; Owner: postgres
175182
--
176183

177-
CREATE FUNCTION library.get_books_by_genre(genre_param character varying) RETURNS TABLE(title character varying)
178-
LANGUAGE plpgsql
179-
AS $$
184+
CREATE FUNCTION library.get_books_by_genre(genre_param character varying)
185+
RETURNS TABLE(title character varying)
186+
LANGUAGE plpgsql
187+
AS $function$
180188
BEGIN
181189
RETURN QUERY
182-
SELECT b.title
183-
FROM books b
184-
JOIN book_genre bg ON b.id = bg.book_id
185-
WHERE bg.genre = genre_param;
190+
SELECT b.title
191+
FROM library.books b
192+
WHERE b.id IN (
193+
SELECT bg.book_id
194+
FROM library.book_genre bg
195+
JOIN library.genres g ON bg.genre_id = g.id
196+
WHERE g.name = genre_param
197+
);
186198
END;
187-
$$;
199+
$function$;
188200

189201

190202
ALTER FUNCTION library.get_books_by_genre(genre_param character varying) OWNER TO postgres;

docs/210-Generate code/40-testing-queries.mdx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,13 @@ Relation Migrator allows you to go a step further, and verify the converted quer
3535
<Screenshot url="http://127.0.0.1:8278" src="img/testing-queries-005.png" alt="Screenshot to show result comparison" />
3636
5. If the query results are different, check your MongoDB query. In this example, your MongoDB query should look like:
3737
```js
38-
async function get_books_by_genre(db, genre_param) {
39-
const result = await db.collection('books').aggregate([
40-
{
41-
$match: {
42-
'genre': genre_param
43-
}
44-
},
45-
{
46-
$project: {
47-
title: 1
48-
}
49-
}
50-
]).toArray();
51-
return result;
38+
async function library_get_books_by_genre(db, genre_param) {
39+
const books = await db.collection('books').aggregate([
40+
{ $match: { 'genres.name': genre_param } },
41+
{ $project: { title: 1 }}
42+
]).toArray();
43+
44+
return books.map(book => ({ title: book.title }));
5245
}
5346
```
5447
Also check that any changes your made to your MongoDB query are saved.

0 commit comments

Comments
 (0)