Skip to content

Instantly share code, notes, and snippets.

@yknx4
Created October 27, 2016 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yknx4/9ae310b86ca8530a1e51cb1480274cd5 to your computer and use it in GitHub Desktop.
Save yknx4/9ae310b86ca8530a1e51cb1480274cd5 to your computer and use it in GitHub Desktop.
#!/bin/sh
# --- Command line
previous_head="$1"
new_head="$2"
branch_checkout="$3"
# --- Constants
separator="/"
master_branch="master"
main_backup="_backup"
database_name="db/beanstack_development"
# --- 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}
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`
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
# When a backup for the new branch already exists
# 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
# and then lets rename the existing backup to the active database name
rename_database $new_database $database_name
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment