forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run PDS using postgres (bluesky-social#233)
* Setup dockerization of postgres db * Fix typo * Setup postgres dialect config in pds, fix migrations for pg * Convert home and author feed to work on pg * Convert pds follow views to work on pg * Convert pds liked-by and notifications views to work on pg * Unify bigint handling between sqlite and pg * Convert pds account, crud, and profile, reposts, thread views to work on pg * Ensure a more complete reset of containers/volumes in with-test-db.sh, set libpq env vars * Add readme for dockerized postgres * Fix build issues
- Loading branch information
Showing
38 changed files
with
646 additions
and
281 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
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,65 @@ | ||
# pg | ||
|
||
Helpers for working with postgres | ||
|
||
## Usage | ||
|
||
### `with-test-db.sh` | ||
|
||
This script allows you to run any command with a fresh, ephemeral/single-use postgres database available. When the script starts a Dockerized postgres container starts-up, and when the script completes that container is removed. | ||
|
||
The environment variable `DB_POSTGRES_URL` will be set with a connection string that can be used to connect to the database. The [`PG*` environment variables](https://www.postgresql.org/docs/current/libpq-envars.html) that are recognized by libpq (i.e. used by the `psql` client) are also set. | ||
|
||
**Example** | ||
|
||
``` | ||
$ ./with-test-db.sh psql -c 'select 1;' | ||
[+] Running 1/1 | ||
⠿ Container pg-db_test-1 Healthy 1.8s | ||
?column? | ||
---------- | ||
1 | ||
(1 row) | ||
[+] Running 1/1 | ||
⠿ Container pg-db_test-1 Stopped 0.1s | ||
Going to remove pg-db_test-1 | ||
[+] Running 1/0 | ||
⠿ Container pg-db_test-1 Removed | ||
``` | ||
|
||
### `docker-compose.yaml` | ||
|
||
The Docker compose file can be used to run containerized versions of postgres either for single use (as is used by `with-test-db.sh`), or for longer-term use. These are setup as separate services named `test_db` and `db` respectively. In both cases the database is available on the host machine's `localhost` and credentials are: | ||
|
||
- Username: pg | ||
- Password: password | ||
|
||
However, each service uses a different port, documented below, to avoid conflicts. | ||
|
||
#### `test_db` service for single use | ||
|
||
The single-use `test_db` service does not have any persistent storage. When the container is removed, data in the database disappears with it. | ||
|
||
This service runs on port `5433`. | ||
|
||
``` | ||
$ docker compose up test_db # start container | ||
$ docker compose stop test_db # stop container | ||
$ docker compose rm test_db # remove container | ||
``` | ||
|
||
#### `db` service for persistent use | ||
|
||
The `db` service has persistent storage on the host machine managed by Docker under a volume named `pg_adx_db`. When the container is removed, data in the database will remain on the host machine. In order to start fresh, you would need to remove the volume. | ||
|
||
This service runs on port `5432`. | ||
|
||
``` | ||
$ docker compose up db -d # start container | ||
$ docker compose stop db # stop container | ||
$ docker compose rm db # remove container | ||
$ docker volume rm pg_adx_db # remove volume | ||
``` |
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,27 @@ | ||
version: '3.8' | ||
services: | ||
# An ephermerally-stored postgres database for single-use test runs | ||
db_test: &db_test | ||
image: postgres:14.4-alpine | ||
environment: | ||
- POSTGRES_USER=pg | ||
- POSTGRES_PASSWORD=password | ||
ports: | ||
- '5433:5432' | ||
# Healthcheck ensures db is queryable when `docker-compose up --wait` completes | ||
healthcheck: | ||
test: 'pg_isready -U pg' | ||
interval: 500ms | ||
timeout: 10s | ||
retries: 20 | ||
# A persistently-stored postgres database | ||
db: | ||
<<: *db_test | ||
ports: | ||
- '5432:5432' | ||
healthcheck: | ||
disable: true | ||
volumes: | ||
- adx_db:/var/lib/postgresql/data | ||
volumes: | ||
adx_db: |
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,26 @@ | ||
#!/usr/bin/env sh | ||
# @TODO handle sigint for cleanup | ||
|
||
# Example usage: | ||
# ./with-test-db.sh psql postgresql://pg:password@localhost:5433/postgres -c 'select 1;' | ||
|
||
dir=$(dirname $0) | ||
compose_file="$dir/docker-compose.yaml" | ||
|
||
docker compose -f $compose_file up --wait --force-recreate db_test | ||
echo # newline | ||
|
||
# Based on creds in compose.yaml | ||
export PGPORT=5433 | ||
export PGHOST=localhost | ||
export PGUSER=pg | ||
export PGPASSWORD=password | ||
export PGDATABASE=postgres | ||
export DB_POSTGRES_URL="postgresql://pg:password@localhost:5433/postgres" | ||
"$@" | ||
code=$? | ||
|
||
echo # newline | ||
docker compose -f $compose_file rm -f --stop --volumes db_test | ||
|
||
exit $code |
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 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.