2020-09-15 02:03:55 +07:00
#!/bin/bash
2018-03-15 04:13:08 +07:00
#
# Author: Carlos Ruiz - globalqss
# Script to synchronize the database for with latest migration scripts
echo Synchronize iDempiere Database
2020-09-09 00:37:00 +07:00
echo Upgrading database " $1 @ $ADEMPIERE_DB_NAME "
2018-03-15 04:13:08 +07:00
2020-09-09 00:37:00 +07:00
if [ $# -eq 0 ]
2018-03-15 04:13:08 +07:00
then
echo " Usage: $0 <userAccount> "
echo " Example: $0 adempiere adempiere "
exit 1
fi
2020-09-09 00:37:00 +07:00
if [ " $IDEMPIERE_HOME " = "" ] || [ " $ADEMPIERE_DB_NAME " = "" ] || [ " $ADEMPIERE_DB_SERVER " = "" ] || [ " $ADEMPIERE_DB_PORT " = "" ]
2018-03-15 04:13:08 +07:00
then
echo "Please make sure that the environment variables are set correctly:"
echo " IDEMPIERE_HOME e.g. /idempiere"
echo " ADEMPIERE_DB_NAME e.g. adempiere or xe"
echo " ADEMPIERE_DB_SERVER e.g. dbserver.adempiere.org"
echo " ADEMPIERE_DB_PORT e.g. 5432 or 1521"
exit 1
fi
PGPASSWORD = $2
export PGPASSWORD
TMPFOLDER = /tmp
ADEMPIERE_DB_USER = $1
ADEMPIERE_DB_PATH = $3
2020-09-28 17:32:29 +07:00
CMD = " psql -h $ADEMPIERE_DB_SERVER -p $ADEMPIERE_DB_PORT -d $ADEMPIERE_DB_NAME -U $ADEMPIERE_DB_USER -b "
2018-03-15 04:13:08 +07:00
SILENTCMD = " $CMD -q -t "
ERROR_STRINGS = "^(ERROR:|FEHLER:|FATAL:|ERRO:)"
2018-11-10 06:33:47 +07:00
DIR_POST = $IDEMPIERE_HOME /migration
if [ " x $4 " = "x" ]
then
DIR_SCRIPTS = $IDEMPIERE_HOME /migration
else
2020-09-09 00:37:00 +07:00
if [ " ${ 4 : 0 : 1 } " = "/" ]
2018-11-10 06:33:47 +07:00
then
DIR_SCRIPTS = " $4 "
else
DIR_SCRIPTS = " $IDEMPIERE_HOME / $4 "
fi
fi
2018-03-15 04:13:08 +07:00
2020-09-09 00:37:00 +07:00
cd " $DIR_SCRIPTS " || ( echo " ERROR: Cannot change to folder $DIR_SCRIPTS " ; exit 1)
2018-03-15 04:13:08 +07:00
# Create list of files already applied - registered in AD_MigrationScript table
echo "select name from ad_migrationscript" | $SILENTCMD | sed -e 's:^ ::' | grep -v '^$' | sort > $TMPFOLDER /lisDB_$$ .txt
# Create list of files in the migration folder
2020-09-15 02:03:55 +07:00
find . -type f -wholename " */ ${ ADEMPIERE_DB_PATH } /*.sql " ! -wholename " ./processes_post_migration/ ${ ADEMPIERE_DB_PATH } /* " | sed -e 's:.*/::' | sort > $TMPFOLDER /lisFS_$$ .txt
2018-03-15 04:13:08 +07:00
MSGERROR = ""
APPLIED = N
# extract and process the list of pending files
comm -13 $TMPFOLDER /lisDB_$$ .txt $TMPFOLDER /lisFS_$$ .txt > $TMPFOLDER /lisPENDING_$$ .txt
if [ -s $TMPFOLDER /lisPENDING_$$ .txt ]
then
2020-09-15 02:03:55 +07:00
while read -r FILE
2018-03-15 04:13:08 +07:00
do
2020-09-09 00:37:00 +07:00
SCRIPT = $( find . -name " $FILE " | grep " / $ADEMPIERE_DB_PATH / " )
2021-10-08 15:30:25 +07:00
echo " $SCRIPT " >> $TMPFOLDER /lisPENDINGFOL_$$ .txt
done < $TMPFOLDER /lisPENDING_$$ .txt
sort -o $TMPFOLDER /lisPENDINGFOL_$$ .txt $TMPFOLDER /lisPENDINGFOL_$$ .txt
mkdir $TMPFOLDER /SyncDB_out_$$
while read -r SCRIPT
do
OUTFILE = $TMPFOLDER /SyncDB_out_$$ /$( basename " $SCRIPT " .sql) .out
2018-03-15 04:13:08 +07:00
echo " Applying $SCRIPT "
2020-09-09 00:37:00 +07:00
$CMD < " $SCRIPT " 2>& 1 | tee " $OUTFILE "
2018-03-15 04:13:08 +07:00
APPLIED = Y
2020-09-09 00:37:00 +07:00
if grep -E " $ERROR_STRINGS " " $OUTFILE " > /dev/null 2>& 1
2018-03-15 04:13:08 +07:00
then
MSGERROR = " $MSGERROR \n**** ERROR ON FILE $OUTFILE - Please verify **** "
# Stop processing to allow user to fix the problem before processing additional files
break
fi
2021-10-08 15:30:25 +07:00
done < $TMPFOLDER /lisPENDINGFOL_$$ .txt
2018-03-15 04:13:08 +07:00
else
if [ -s $TMPFOLDER /lisFS_$$ .txt ]
then
echo "Database is already in sync - no scripts pending to apply"
else
echo "No scripts were found to apply"
fi
fi
if [ x$APPLIED = xY ]
then
2020-09-09 00:37:00 +07:00
cd " $DIR_POST " || ( echo " ERROR: Cannot change to folder $DIR_POST " ; exit 1)
for FILE in processes_post_migration/" $ADEMPIERE_DB_PATH " /*.sql
2018-03-15 04:13:08 +07:00
do
2020-09-09 00:37:00 +07:00
OUTFILE = $TMPFOLDER /SyncDB_out_$$ /$( basename " $FILE " .sql) .out
$CMD 2>& 1 < " $FILE " | tee " $OUTFILE "
if grep -E " $ERROR_STRINGS " " $OUTFILE " > /dev/null 2>& 1
2018-03-15 04:13:08 +07:00
then
MSGERROR = " $MSGERROR \n**** ERROR ON FILE $OUTFILE - Please verify **** "
fi
done
fi
2018-11-19 21:39:07 +07:00
PGPASSWORD =
export PGPASSWORD
2018-03-15 04:13:08 +07:00
if [ -n " $MSGERROR " ]
then
echo " $MSGERROR "
2020-09-09 00:37:00 +07:00
printf "\n Errors were found during the process (see message above) - please review and fix the error running manually the script - and then restart this process again"
2018-11-19 21:39:07 +07:00
exit 1
2018-03-15 04:13:08 +07:00
fi
2018-11-19 21:39:07 +07:00
exit 0