151 lines
2.6 KiB
Bash
Executable file
151 lines
2.6 KiB
Bash
Executable file
#!/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
|