-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.sh
18 lines (17 loc) · 936 Bytes
/
action.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/sh
echo "postgres drop restore - getting a dump of source database"
tempFile=$(mktemp -u)
PGOPTIONS="-c statement_timeout=3600000" pg_dump -Fc --no-owner --clean -o $SOURCE_DATABASE_URL > $tempFile
echo "postgres drop restore - drop existing target database"
targetDbName=`echo $TARGET_DATABASE_URL | sed "s|.*/\([^/]*\)\$|\\1|"`
targetDbRootUrl=$(echo $TARGET_DATABASE_URL | sed "s|/[^/]*\$|/template1|")
dropResult=$(echo "DROP DATABASE $targetDbName;" | psql $targetDbRootUrl 2>&1)
if echo $dropResult | grep "other session using the database" -> /dev/null; then
echo "RESTORE FAILED - another database session is preventing drop of database $targetDbName"
exit 1
fi
createResult=$(echo "CREATE DATABASE $targetDbName;" | psql $targetDbRootUrl 2>&1)
echo "postgres drop restore - fill target database with dump"
pg_restore --no-owner -d $TARGET_DATABASE_URL $tempFile
rm $tempFile
echo "postgres drop restore - complete"