Skip to content

Commit

Permalink
[ES-842] Added db_upgrade_scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Venkata Saidurga Polamraju <saidurgacsea@gmail.com>
  • Loading branch information
pvsaidurga committed Sep 13, 2024
1 parent 3ed8c7c commit 0235998
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions db_upgrade_script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Directory contains sql scripts to be executed for DB migrations. upgrade and revoke scripts are named after the migrated version.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\echo 'Rollback Queries not required for transition from $CURRENT_VERSION to $UPGRADE_VERSION'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\echo 'Upgrade Queries not required for transition from $CURRENT_VERSION to $UPGRADE_VERSION'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\echo 'Rollback Queries not required for transition from $CURRENT_VERSION to $UPGRADE_VERSION'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\echo 'Upgrade Queries not required for transition from $CURRENT_VERSION to $UPGRADE_VERSION'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\echo 'Rollback Queries not required for transition from $CURRENT_VERSION to $UPGRADE_VERSION'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\echo 'Upgrade Queries not required for transition from $CURRENT_VERSION to $UPGRADE_VERSION'
12 changes: 12 additions & 0 deletions db_upgrade_script/mosip_mockidentitysystem/upgrade.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ACTION=upgrade
MOSIP_DB_NAME=
DB_SERVERIP=
DB_PORT=
SU_USER=postgres
SU_USER_PWD=
SYS_ADMIN_USER=
SYS_ADMIN_PWD=
DEFAULT_DB_NAME=postgres
DBUSER_PWD=
CURRENT_VERSION=
UPGRADE_VERSION=
51 changes: 51 additions & 0 deletions db_upgrade_script/mosip_mockidentitysystem/upgrade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

set -e
properties_file="$1"
echo `date "+%m/%d/%Y %H:%M:%S"` ": $properties_file"
if [ -f "$properties_file" ]
then
echo `date "+%m/%d/%Y %H:%M:%S"` ": Property file \"$properties_file\" found."
while IFS='=' read -r key value
do
key=$(echo $key | tr '.' '_')
eval ${key}=\${value}
done < "$properties_file"
else
echo `date "+%m/%d/%Y %H:%M:%S"` ": Property file not found, Pass property file name as argument."
fi

echo "Current version: $CURRENT_VERSION"
echo "UPGRADE version: $UPGRADE_VERSION"
echo "Action: $ACTION"

# Terminate existing connections
echo "Terminating active connections"
CONN=$(PGPASSWORD=$SU_USER_PWD psql -v ON_ERROR_STOP=1 --username=$SU_USER --host=$DB_SERVERIP --port=$DB_PORT --dbname=$DEFAULT_DB_NAME -t -c "SELECT count(pg_terminate_backend(pg_stat_activity.pid)) FROM pg_stat_activity WHERE datname = '$MOSIP_DB_NAME' AND pid <> pg_backend_pid()";exit;)
echo "Terminated connections"

# Execute upgrade or rollback
if [ "$ACTION" == "upgrade" ]; then
echo "Upgrading database from $CURRENT_VERSION to $UPGRADE_VERSION"
UPGRADE_SCRIPT_FILE="sql/${CURRENT_VERSION}_to_${UPGRADE_VERSION}_upgrade.sql"
if [ -f "$UPGRADE_SCRIPT_FILE" ]; then
echo "Executing upgrade script $UPGRADE_SCRIPT_FILE"
PGPASSWORD=$SU_USER_PWD psql -v ON_ERROR_STOP=1 --username=$SU_USER --host=$DB_SERVERIP --port=$DB_PORT --dbname=$DEFAULT_DB_NAME -v primary_language_code=$PRIMARY_LANGUAGE_CODE -a -b -f $UPGRADE_SCRIPT_FILE
else
echo "Upgrade script not found, exiting."
exit 1
fi
elif [ "$ACTION" == "rollback" ]; then
echo "Rolling back database for $CURRENT_VERSION to $UPGRADE_VERSION"
REVOKE_SCRIPT_FILE="sql/${CURRENT_VERSION}_to_${UPGRADE_VERSION}_rollback.sql"
if [ -f "$REVOKE_SCRIPT_FILE" ]; then
echo "Executing rollback script $REVOKE_SCRIPT_FILE"
PGPASSWORD=$SU_USER_PWD psql -v ON_ERROR_STOP=1 --username=$SU_USER --host=$DB_SERVERIP --port=$DB_PORT --dbname=$DEFAULT_DB_NAME -v primary_language_code=$PRIMARY_LANGUAGE_CODE -a -b -f $REVOKE_SCRIPT_FILE
else
echo "rollback script not found, exiting."
exit 1
fi
else
echo "Unknown action: $ACTION, must be 'upgrade' or 'rollback'."
exit 1
fi

0 comments on commit 0235998

Please sign in to comment.