-
-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turns out the real reason was that we had `begin`/`commit`s creating transactions in our kitchen sink SQL queries. Therefore the changes were not being rolled back appropriately. Turns out `begin`/`commit` does not compose. The fix would involve simply removing `begin` and `commit` from `resources/kitchen-sink-data.sql` and `resources/kitchen-sink-schema.sql`, however we made a couple of other good changes in this commit trying to fix the problem.
- Loading branch information
Showing
9 changed files
with
112 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
begin; | ||
-- We don’t have a `begin`/`commit` in here to let the users of this query | ||
-- control the data lifecycle. | ||
|
||
insert into c.person (id, name, email, about, created_at) values | ||
(1, 'John Smith', '[email protected]', null, null), | ||
|
@@ -31,5 +32,3 @@ insert into c.compound_key (person_id_1, person_id_2, extra) values | |
(2, 5, true), | ||
(2, 3, null), | ||
(4, 4, false); | ||
|
||
commit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { resolve as resolvePath } from 'path' | ||
import { readFile } from 'fs' | ||
import { Pool, Client } from 'pg' | ||
import minify = require('pg-minify') | ||
|
||
const pool = new Pool({ | ||
database: 'postgraphql_test', | ||
port: 5432, | ||
max: 15, | ||
idleTimeoutMillis: 500, | ||
}) | ||
|
||
const kitchenSinkSchema = new Promise<string>((resolve, reject) => { | ||
readFile(resolvePath(__dirname, '../../../../resources/kitchen-sink-schema.sql'), (error, data) => { | ||
if (error) reject(error) | ||
else resolve(minify(data.toString())) | ||
}) | ||
}) | ||
|
||
/** | ||
* Takes a function implementation of a test, and provides it a Postgres | ||
* client. The client will be connected from the pool at the start of the test, | ||
* and released back at the end. All changes will be rolled back. | ||
*/ | ||
export default function withPGClient (fn: (client: Client) => void | Promise<void>) { | ||
return async () => { | ||
// Connect a client from our pool and begin a transaction. | ||
const client = await pool.connect() | ||
await client.query('begin') | ||
await client.query(await kitchenSinkSchema) | ||
|
||
// Mock the query function. | ||
client.query = jest.fn(client.query) | ||
|
||
// Try to run our test, if it fails we still want to cleanup the client. | ||
try { | ||
await fn(client) | ||
} | ||
// Always rollback our changes and release the client, even if the test | ||
// fails. | ||
finally { | ||
await client.query('rollback') | ||
client.release() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.