Skip to content

Commit

Permalink
deps: Upgrade to PG 15
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Dec 9, 2024
1 parent 8130b8b commit abb32af
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 32 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ jobs:
- 6379:6379
options: --entrypoint redis-server
postgres:
image: postgres:14.15
image: postgres:15.10
env:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
Expand Down Expand Up @@ -261,7 +261,7 @@ jobs:
- 6379:6379
options: --entrypoint redis-server
postgres:
image: postgres:14.15
image: postgres:15.10
env:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
Expand Down Expand Up @@ -324,7 +324,7 @@ jobs:
- 6379:6379
options: --entrypoint redis-server
postgres:
image: postgres:14.15
image: postgres:15.10
env:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
Expand Down Expand Up @@ -366,7 +366,7 @@ jobs:
- 6379:6379
options: --entrypoint redis-server
postgres:
image: postgres:14.15
image: postgres:15.10
env:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- 6379:6379
options: --entrypoint redis-server
postgres:
image: postgres:14.15
image: postgres:15.10
env:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
Expand Down
4 changes: 2 additions & 2 deletions docs/postgres.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PostgreSQL Database

You need to have PostgreSQL > 14.x.
You need to have PostgreSQL > 15.x.

In production, we're currently running 16.4.

Expand Down Expand Up @@ -57,7 +57,7 @@ If you don't want to run a local instance of PostgreSQL in your computer, you ca
Create and run the container:

```
docker run -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -d --name opencollective-postgres --shm-size=1g --memory=4g --cpus=2 postgres:14
docker run -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -d --name opencollective-postgres --shm-size=1g --memory=4g --cpus=2 postgres:15
```

Set the necessary environment variables:
Expand Down
93 changes: 68 additions & 25 deletions scripts/db_restore.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
#!/bin/bash

usage() {
echo "Usage: db_restore.sh -d DBNAME -U DBUSER --use-postgis -f DBDUMP_FILE";
echo "e.g.";
echo "Usage: db_restore.sh -d DBNAME -U DBUSER --use-postgis -f DBDUMP_FILE"
echo "e.g."
echo "> db_restore.sh -d opencollective_dvl -U opencollective -f test/dbdumps/opencollective_dvl.pgsql"
exit 0;
exit 0
}

while [[ $# -gt 0 ]]
do
key="$1"
while [[ $# -gt 0 ]]; do
key="$1"

case $key in
-d|--dbname)
case $key in
-d | --dbname)
LOCALDBNAME="$2"
shift # past argument
;;
-U|--username)
-U | --username)
LOCALDBUSER="$2"
shift # past argument
;;
--use-postgis)
--use-postgis)
USE_POSTGIS=1
;;
-f|--file)
-f | --file)
DBDUMP_FILE="$2"
shift # past argument
;;
*)
# unknown option
*)
# unknown option
;;
esac
shift # past argument or value
esac
shift # past argument or value
done

LOCALDBUSER=${LOCALDBUSER:-"opencollective"}
LOCALDBNAME=${LOCALDBNAME:-"opencollective_dvl"}
DBDUMP_FILE=${DBDUMP_FILE:-"test/dbdumps/opencollective_dvl.pgsql"}
DBDUMP_FILE=${DBDUMP_FILE:-"./test/dbdumps/opencollective_dvl.pgsql"}

echo "LOCALDBUSER=$LOCALDBUSER"
echo "LOCALDBNAME=$LOCALDBNAME"
echo "DBDUMP_FILE=$DBDUMP_FILE"

if [ -z "$LOCALDBNAME" ]; then usage; fi;
if [ -z "$LOCALDBNAME" ]; then usage; fi

# kill all connections to the postgres server
# echo "Killing all connections to database '$LOCALDBNAME'"
Expand All @@ -53,8 +52,15 @@ if [ -z "$LOCALDBNAME" ]; then usage; fi;
# where pg_stat_activity.datname = '$LOCALDBNAME'
# EOF

dropdb -U postgres -h localhost --if-exists $LOCALDBNAME;
createdb -U postgres -h localhost $LOCALDBNAME 2> /dev/null
echo "Dropping '$LOCALDBNAME'"
dropdb -U postgres -h localhost --if-exists $LOCALDBNAME

echo "Creating '$LOCALDBNAME'"
createdb -U postgres -h localhost $LOCALDBNAME 2>/dev/null

# Update table permissions
echo "Updating table permissions"
psql -U postgres -h localhost $LOCALDBNAME -c "GRANT ALL ON SCHEMA public TO ${LOCALDBUSER};"

# When restoring old backups, you may need to enable Postgis
if [ "$USE_POSTGIS" = "1" ]; then
Expand All @@ -72,13 +78,50 @@ fi
set -e
} | tee >/dev/null

# The first time we run it, we will trigger FK constraints errors
set +e
pg_restore -U postgres -h localhost --no-acl --no-owner --role=${LOCALDBUSER} -n public -O -c -d "${LOCALDBNAME}" "${DBDUMP_FILE}" 2>/dev/null
set -e
PG_RESTORE_OPTIONS=(-U postgres -h localhost --no-acl --no-owner --role="${LOCALDBUSER}" -n public -O -d "${LOCALDBNAME}")
PG_RESTORE_COMMAND=pg_restore
if [ -n "$USE_DOCKER" ]; then
PG_RESTORE_COMMAND="sudo docker run --rm --network host -v $(dirname "$DBDUMP_FILE"):/backup postgres:14 pg_restore -h localhost -p 5432 -U opencollective"
DBDUMP_FILE="/backup/$(basename "$DBDUMP_FILE")"
fi

# The first time we run it with the -s option (schema only)
echo "Restoring schema"
$PG_RESTORE_COMMAND "${PG_RESTORE_OPTIONS[@]}" -s "${DBDUMP_FILE}"

# Disable triggers
echo "Disabling triggers"
psql -U postgres -h localhost "${LOCALDBNAME}" -c "
DO \$\$
BEGIN
EXECUTE (
SELECT string_agg(
format('ALTER TABLE %I.%I DISABLE TRIGGER ALL', schemaname, tablename), '; '
)
FROM pg_tables
WHERE schemaname = 'public'
);
END \$\$;
"

# Restore data (-a flag)
echo "Restoring data"
$PG_RESTORE_COMMAND "${PG_RESTORE_OPTIONS[@]}" -a "${DBDUMP_FILE}"

# So we run it twice :-)
pg_restore -U postgres -h localhost --no-acl --no-owner --role=${LOCALDBUSER} -n public -O -c -d "${LOCALDBNAME}" "${DBDUMP_FILE}"
# Re-enable triggers
echo "Re-enabling triggers"
psql -U postgres -h localhost "${LOCALDBNAME}" -c "
DO \$\$
BEGIN
EXECUTE (
SELECT string_agg(
format('ALTER TABLE %I.%I ENABLE TRIGGER ALL', schemaname, tablename), '; '
)
FROM pg_tables
WHERE schemaname = 'public'
);
END \$\$;
"

echo "DB restored to postgres://localhost/${LOCALDBNAME}"

Expand Down
Binary file modified test/dbdumps/opencollective_dvl.pgsql
Binary file not shown.

0 comments on commit abb32af

Please sign in to comment.