Skip to content

Development with docker databases

Paul Tsnobiladzé edited this page May 24, 2023 · 4 revisions

If you don’t want to install postgres locally for working on graphile-build, you can use docker to easily set up the test databases for your development environment. The following container can run all the graphile-engine and postgraphile tests, it even has wal2json installed by using djmccormick/postgres-alpine-wal2json container. No passwords necessary, uses trust auth on local connections.

A helper script fires up the docker container and exports all the necessary environment variables used by the test scripts.

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) - defaults to pgtestdb
  2. the port name to be used by docker (if colliding with other databases) - defaults to 5432
  3. the user name under which to create the databases and connect as - defaults to postgres.

Or just adjust the defaults in the bash script itself.

Files

  • /Dockerfile
    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"]
    
  • /postgresql.conf
    listen_addresses = '*'
    shared_preload_libraries = 'wal2json'
    wal_level = logical
    max_replication_slots = 10
    
  • /setup.sql
    CREATE DATABASE graphileengine_test;
    CREATE DATABASE postgraphile_test;
    CREATE DATABASE lds_test; -- not strictly necessary
  • /runpg.sh
    #!/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 -e POSTGRES_HOST_AUTH_METHOD=trust --name $CONT_NAME postgraphile-test-databases
    echo "Use 'docker stop $CONT_NAME' to stop the container"

Place them in a .gitignored folder, and use that in docker build to keep the build context small.

Or even better, adjust the docker compose setup to build it :-)

Clone this wiki locally