diff options
author | Freya Murphy <freya@freyacat.org> | 2024-05-20 17:11:38 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-05-20 17:11:38 -0400 |
commit | 708594d32ffe779cf547c816fa7cdd19d095be2e (patch) | |
tree | 30d28553316db02bc027664e17b76d58f2e352ff /build/dbinit | |
parent | add en_CAT makefile and use ucfirst/ucwords (diff) | |
download | xssbook2-708594d32ffe779cf547c816fa7cdd19d095be2e.tar.gz xssbook2-708594d32ffe779cf547c816fa7cdd19d095be2e.tar.bz2 xssbook2-708594d32ffe779cf547c816fa7cdd19d095be2e.zip |
v2 done
Diffstat (limited to 'build/dbinit')
-rw-r--r-- | build/dbinit/Dockerfile | 5 | ||||
-rwxr-xr-x | build/dbinit/dbinit | 151 |
2 files changed, 0 insertions, 156 deletions
diff --git a/build/dbinit/Dockerfile b/build/dbinit/Dockerfile deleted file mode 100644 index 81c66ea..0000000 --- a/build/dbinit/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM alpine:3.19 -RUN apk add --no-cache postgresql16-client tini -COPY ./dbinit /usr/local/bin/dbinit -ENTRYPOINT ["/sbin/tini", "--"] -CMD ["/usr/local/bin/dbinit"] diff --git a/build/dbinit/dbinit b/build/dbinit/dbinit deleted file mode 100755 index c64f139..0000000 --- a/build/dbinit/dbinit +++ /dev/null @@ -1,151 +0,0 @@ -#!/bin/sh - -step() { - printf '\x1b[34;1m>> %s\x1b[0m\n' "$*" -} - -error() { - printf '\x1b[31;1merror: \x1b[0m%s\n' "$*" -} - -export PGPASSWORD=$POSTGRES_PASSWORD - -psql() { - /usr/bin/psql \ - -h db \ - -p 5432 \ - -d $POSTGRES_DB \ - -U $POSTGRES_USER \ - "$@" -} - -pg_isready() { - /usr/bin/pg_isready \ - -h db \ - -p 5432 \ - -d $POSTGRES_DB \ - -U $POSTGRES_USER \ - "$@" -} - -curr_revision() { - psql -qtAX -f /db/rev.sql; -} - -wait_until_ready() { - step 'Checking if the database is ready...'; - while true; do - pg_isready; - code=$?; - if [ $code -eq 0 ]; then - break; - fi - sleep 3; - done -} - -run_migrations() { - i="$1" - while true; do - name=$(printf "%04d" "$i"); - file="/db/migrations/$name.sql" - - if [ -f $file ]; then - psql -f $file 2> /errors - errors=$(cat /errors | grep 'ERROR' | wc -l) - if [ "$errors" -eq 0 ]; then - i=$((i+1)); - continue; - else - error "An error occoured during a migration (rev $name)" - cat /errors | grep -v 'current transaction is aborted'; - error "Aborting migrations, fix file(s) then restart process." - return 1; - fi - else - return 0; - fi - done -} - -init_api() { - psql -f /db/rest/rest.sql 2> /errors; - errors=$(cat /errors | grep 'ERROR' | wc -l) - if [ "$errors" -eq 0 ]; then - return 0; - else - error "An error occoured during api initialization" - cat /errors | grep -v 'current transaction is aborted'; - error "Aborting api initialization, fix file(s) then restart process." - return 1; - fi -} - -update_jwt() { - psql -c "UPDATE sys.database_info SET jwt_secret = '$JWT_SECRET' WHERE name = current_database();" - errors=$(cat /errors | grep 'ERROR' | wc -l) - if [ "$errors" -eq 0 ]; then - return 0; - else - return 1; - fi -} - -load_ext() { - psql -qtAX -f /db/ext.sql; -} - -init () { - # reomve ready status - # so php ignores requests - rm -fr /status/ready - - step 'Waiting for database'; - # make sure the database is running - # before we run any requests - wait_until_ready; - step 'Database ready'; - - step 'Loading extensions'; - # Make sure extensions are loaded - load_ext; - - step 'Peforming migrations'; - # get the current revision - REV=$(curr_revision); - step "Database at revision: $REV" - # run each migration that is - # higher than our current revision - run_migrations "$REV" - CODE=$?; - - if [ $CODE -ne 0 ]; then - return $CODE; - fi - - step 'Initalizing the api'; - # reinit the api schema for - # postgrest - init_api; - CODE=$?; - - if [ $CODE -ne 0 ]; then - return $CODE; - fi - - step 'Updating JWT secret'; - # make sure postgres has the corrent - # jwt secret - update_jwt; - CODE=$?; - - if [ $CODE -ne 0 ]; then - return $CODE; - fi - - step 'Database is initialized' - # database is ready - touch /status/ready -} - -init |