Skip to content

Instantly share code, notes, and snippets.

@ab-pm
Last active November 13, 2019 18:13
Show Gist options
  • Save ab-pm/2ac739c98d2fb5629cb8bb42ccaa7537 to your computer and use it in GitHub Desktop.
Save ab-pm/2ac739c98d2fb5629cb8bb42ccaa7537 to your computer and use it in GitHub Desktop.
Dockerized Database for PostGraphile Development

Small utitly to fire up a dockerized postgres cluster against which the graphile-engine and postgraphile tests can be run. Has wal2json installed by using djmccormick/postgres-alpine-wal2json container. No passwords necessary, uses trust auth on local connections. Exports all the necessary environment variables used by the tests.

Usage:

> ls
Dockerfile  postgresql.conf  runpg.sh  setup.sql
> docker build -t postgraphile-test-databases .
…
Successfully tagged postgraphile-test-databases:latest
> . runpg.sh
Container id: …
Use 'docker stop pgtestdb' to stop the container
> cd ~/graphile-engine
> yarn test
lerna success run Ran npm script 'test' in 12 packages in 83.5s:
lerna success - graphile-build-pg
lerna success - graphile-build
lerna success - graphile-utils
lerna success - graphile
lerna success - graphql-parse-resolve-info
lerna success - jest-serializer-graphql-schema
lerna success - @graphile/lds
lerna success - @graphile/lru
lerna success - @graphile/pg-pubsub
lerna success - pg-sql2
lerna success - postgraphile-core
lerna success - @graphile/subscriptions-lds
Done in 83.92s.
> cd postgraphile
> yarn test
 PASS  src/postgraphile/__tests__/postgraphileIntegrationSchemaExport-test.js (6.349s)
 PASS  src/postgraphile/__tests__/postgraphileIntegrationSchema-test.js (11.197s)
 PASS  src/postgraphile/__tests__/postgraphileIntegrationMutations-test.js (11.509s)
 PASS  src/postgraphile/__tests__/postgraphileIntegrationQueries-test.js (5.47s)
 PASS  src/postgraphile/__tests__/withPostGraphileContext-test.js
 PASS  src/postgraphile/__tests__/postgraphile-test.js
 PASS  src/postgraphile/http/__tests__/createPostGraphileHttpRequestHandler-test.js (5.802s)
Test Suites: 7 passed, 7 total
Tests:       497 passed, 497 total
Snapshots:   41 passed, 41 total
Time:        17.611s
Ran all test suites.
Done in 18.59s.

Options to pass to runpg.sh:

  1. the container name to be used (for docker ps, docker logs, docker exec psql, docker stop etc)
  2. the port name to be used by docker (if colliding with other databases)
  3. the user name under which to create the databases and connct as
FROM djmccormick/postgres-alpine-wal2json AS postgres
COPY /postgresql.conf /etc/postgresql.conf
COPY /setup.sql /docker-entrypoint-initdb.d/setup.sql
EXPOSE 5432
CMD ["postgres", "-c", "config_file=/etc/postgresql.conf"]
listen_addresses = '*'
shared_preload_libraries = 'wal2json'
wal_level = logical
max_replication_slots = 10
#!/bin/bash -e
(return 0 2>/dev/null) || { echo "Use '. $0' to source the file, making exports work!"; exit 1; }
CONT_NAME=${1:-pgtestdb}
# necessary for the createdb/dropdb scripts in the lds packages
export PGHOST=localhost
export PGPORT=${2:-5432}
export PGUSER=${3:-postgres}
# necessary for the test scripts
export TEST_DATABASE_URL="postgres://$PGUSER@localhost:$PGPORT/graphileengine_test"
export TEST_PG_URL="postgres://$PGUSER@localhost:$PGPORT/postgraphile_test"
export LDS_TEST_DATABASE_URL="postgres://$PGUSER@localhost:$PGPORT/lds_test"
echo -n "Container id: "
docker run --rm -d -p $PGPORT:5432 -e POSTGRES_USER=$PGUSER --name $CONT_NAME postgraphile-test-databases
echo "Use 'docker stop $CONT_NAME' to stop the container"
CREATE DATABASE graphileengine_test;
CREATE DATABASE postgraphile_test;
CREATE DATABASE lds_test;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment