Created
October 27, 2016 22:10
-
-
Save yknx4/7c2eba07fa8ea33250fe185cfc31b362 to your computer and use it in GitHub Desktop.
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
#!/bin/sh | |
# --- Command line | |
previous_head="$1" | |
new_head="$2" | |
branch_checkout="$3" | |
# --- Constants | |
separator="/" | |
master_branch="master" | |
main_backup="_backup" | |
# --- Functions | |
database_exists () { | |
name=${1:0:63} | |
return `psql -U postgres -lqtA | cut -d\| -f1 | grep -qFx "$name"` | |
} | |
kill_connections() { | |
psql -U postgres -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$database_name' AND pid <> pg_backend_pid();" > /dev/null | |
} | |
duplicate_database_to() { | |
name=${1:0:63} | |
echo "Creating $name from $database_name" | |
createdb -T $database_name $name | |
} | |
drop_database() { | |
name=${1:0:63} | |
echo "Deleting existing $name" | |
dropdb -U postgres "$name" | |
} | |
rename_database() { | |
old_name=${1:0:63} | |
new_name=${2:0:63} | |
# echo "Renaming $old_name to $new_name" | |
psql -U postgres -c "SET standard_conforming_strings=on;" -q -b | |
psql -U postgres -c "ALTER DATABASE \"$old_name\" RENAME TO \"$new_name\";" -q -e | |
psql -U postgres -c "SET standard_conforming_strings=off;" -q -b | |
} | |
alias branch_name='git name-rev --name-only' | |
# --- Variables | |
previous_branch=`branch_name $previous_head` | |
new_branch=`git branch | grep -e "^*" | cut -d' ' -f 2` | |
database_name="db/beanstack_development" | |
old_database=$database_name$separator$previous_branch | |
new_database=$database_name$separator$new_branch | |
main_backup=$database_name$main_backup | |
# --- Script | |
if [ $branch_checkout -eq 0 ]; then | |
exit 0 # Exit script if it is not a branch change | |
fi | |
echo "Switching from $previous_branch to $new_branch" | |
echo "Killing existing connections to $database_name" | |
kill_connections # We need to kill all the connections or the commands here will fail | |
if ! database_exists $main_backup; then | |
echo "Creating a main_backup, if something goes wrong you can manually restore from this backup." | |
duplicate_database_to $main_backup # We should always have a master backup | |
fi | |
if [ "$previous_branch" == "$new_branch" ]; then | |
exit 0 # We didn't change branch, so lets just exit | |
fi | |
if database_exists $old_database; then | |
# We need to remove old backups if they exists (they shouldn't most of the times) but | |
# sometime when we didn't had changes in the branch when we created another one, so a | |
# backup for the parent branch is created instead of one for the new branch, this is | |
# to delete that wrong backup. | |
drop_database $old_database | |
fi | |
if database_exists $new_database; then | |
# If there is already a backup for the branch we are checking to, lets just rename the active database to create a new backup | |
rename_database $database_name $old_database | |
else | |
# If the branch we are checking to doesn't have a backup let's just copy the db to still have an active database | |
duplicate_database_to $old_database | |
fi | |
if [ "$previous_branch" != "$new_branch" ]; then | |
if database_exists $new_database; then | |
# When a backup for the new branch already exists | |
if database_exists $database_name; then | |
# and for some reason there is an active database, lets drop it | |
drop_database $database_name | |
fi | |
# and then lets rename the existing backup to the active database name | |
rename_database $new_database $database_name | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment