Skip to content

Instantly share code, notes, and snippets.

@nvn-odoo
Last active May 31, 2019 08:57
Show Gist options
  • Save nvn-odoo/c8ccb3f93ced86f1cc534c21e9e012fd to your computer and use it in GitHub Desktop.
Save nvn-odoo/c8ccb3f93ced86f1cc534c21e9e012fd to your computer and use it in GitHub Desktop.
Creates a database with schema 'a' and schema 'b' containing databases given in parameters
#!/bin/bash
# This tool creates a database $3 from database $1 and database $2
# database $1 will ends in the schema a
# database $2 will ends in the schema b
# this will allow comparisons between two schemas in the same database
if [ "$1" = "" ]; then
echo "USAGE: $0 <DBNAME_A> <DBNAME_B> <DBNAME_AB>" >&2
exit 1
else
DB_A=$1
fi
if [ "$2" = "" ]; then
echo "USAGE: $0 <DBNAME_A> <DBNAME_B> <DBNAME_AB>" >&2
exit 1
else
DB_B=$2
fi
if [ "$3" = "" ]; then
echo "USAGE: $0 <DBNAME_A> <DBNAME_B> <DBNAME_AB>" >&2
exit 1
else
DB_AB=$3
fi
spinner()
{
local pid=$!
local delay=0.4
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b\n"
}
#CREATE DB
printf "createdb $DB_AB\n"
if ! createdb $DB_AB
then
printf "[ERROR] please use a non-existing database $DB_AB\n"
exit 1
fi
#GENERATE DUMPS
printf "pg_dump -Ox $DB_A > /tmp/a.sql"
pg_dump -Ox $DB_A > /tmp/a.sql & spinner
printf "pg_dump -Ox $DB_B \> /tmp/b.sql"
pg_dump -Ox $DB_B > /tmp/b.sql & spinner
#IMPORTING DUMP
printf "cat /tmp/a.sql | psql $DB_AB > /tmp/import_$DB_A.log"
cat /tmp/a.sql | psql $DB_AB > /tmp/import_$DB_A.log & spinner
echo $DB_AB\#= alter schema public rename to a;
psql $DB_AB -c "alter schema public rename to a;"
echo $DB_AB\#= create schema public;
psql $DB_AB -c "create schema public;"
printf "cat /tmp/b.sql | psql $DB_AB > /tmp/import_$DB_B.log"
cat /tmp/b.sql | psql $DB_AB > /tmp/import_$DB_B.log & spinner
echo $DB_AB\#= alter schema public rename to b;
psql $DB_AB -c "alter schema public rename to b;"
echo $DB_AB\#= create schema public;
psql $DB_AB -c "create schema public;"
#CLEANING
echo rm /tmp/a.sql
rm /tmp/a.sql
echo rm /tmp/b.sql
rm /tmp/b.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment