Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP-6599 | Updated scripts to work in actions #4396

Merged
merged 10 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,10 @@ db-deploy:
@echo "$(P) deployment script that facilitates releasing database changes."
@cd source/database/mssql/scripts/dbscripts; TARGET_SPRINT=$(n) ./deploy.sh

db-deploy-transaction:
@echo "$(P) deployment transaction."
@cd source/database/mssql/scripts/dbscripts; TARGET_SPRINT=$(n) ./deploy-transaction.sh

DRY_RUN=0
db-upgrade: ## Upgrade an existing database to the TARGET_VERSION (if passed) or latest version (default), n=TARGET_VERSION (16.01).
@echo "$(P) Upgrade an existing database to the TARGET_VERSION (if passed) or latest version (default), n=TARGET_VERSION (16.01)"
@cd source/database/mssql/scripts/dbscripts; TARGET_VERSION=$(n) ./db-upgrade.sh
@cd source/database/mssql/scripts/dbscripts; TARGET_SPRINT=$(n) ./upgrade.sh -n $(DRY_RUN)

db-scaffold: ## Requires local install of sqlcmd
@echo "$(P) regenerate ef core entities from database"
Expand Down
79 changes: 79 additions & 0 deletions source/database/mssql/scripts/dbscripts/db-deploy-transaction.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
eval $(grep -v '^#' ../../.env | xargs)

# Process options
valid=1
dry_run=0

# Retrieve the parameters passed
while getopts s:o:'n' option
do
case "${option}" in
(s)
TARGET_SPRINT=$OPTARG
;;
(o)
TARGET_OPERATION=$OPTARG
;;
(n)
dry_run=1
;;

# Option error handling.
\?) valid=0
echo "An invalid option has been entered: $OPTARG"
;;

:) valid=0
echo "The additional argument for option $OPTARG was omitted."
;;
esac
done

if [ $valid == 0 ]
then
exit 1
fi

if [ $dry_run == 1 ]
then
echo "** Running DRY RUN **"
fi

#Run every scripts in the build folder
SQLFOLDER="${TARGET_SPRINT}/${TARGET_OPERATION}"
if [ ! -d "$SQLFOLDER" ]; then
echo "ERROR: Directory does not exist [$SQLFOLDER]"
exit 1
fi
i="${SQLFOLDER}"/master.sql;

echo "======= DEPLOY-TRANSACTION START ========"

#Print the current version of the DB state
echo " * Current Database version and concurrency value"
sqlcmd -S $SERVER_NAME -U $DB_USER -P $DB_PASSWORD -d $DB_NAME -Q "SET NOCOUNT ON; SELECT static_variable_value, concurrency_control_number FROM pims_static_variable WHERE static_variable_name = 'DBVERSION';" -W -h -1

echo "* Executing from '${i}'"
echo " ==== sqlcmd START ========"
if [ $dry_run == 0 ]; then
sqlcmd -S $SERVER_NAME -U $DB_USER -P $DB_PASSWORD -d $DB_NAME -i "$i" -b -I
fi
count=$?
echo " ==== sqlcmd END ========"
if [ $count -ne 0 ];
then
echo " ==== SCRIPT ${i} RETURNS AN ERROR. ========="
else
echo " ==== SCRIPT ${i} COMPLETED SUCCESSFULLY. =========" && echo $count > /tmp/log.txt
fi

# Print the final version of the database after the script execution
echo " * Final Database version and concurrency value"
sqlcmd -S $SERVER_NAME -U $DB_USER -P $DB_PASSWORD -d $DB_NAME -Q "SET NOCOUNT ON; SELECT static_variable_value, concurrency_control_number FROM pims_static_variable WHERE static_variable_name = 'DBVERSION';" -W -h -1

echo "======= DEPLOY-TRANSACTION END ========"

if [ $count -ne 0 ];
then
exit 1
fi
58 changes: 58 additions & 0 deletions source/database/mssql/scripts/dbscripts/db-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
eval $(grep -v '^#' ../../.env | xargs)

# Process options
valid=1
dry_run=0

# Retrieve the parameters passed
while getopts s:o:'n' option
do
case "${option}" in
(s)
TARGET_SPRINT=$OPTARG
;;
(o)
TARGET_OPERATION=$OPTARG
;;
n)
dry_run=1
;;

# Option error handling.
\?) valid=0
echo "An invalid option has been entered: $OPTARG"
;;

:) valid=0
echo "The additional argument for option $OPTARG was omitted."
;;
esac
done

if [ $valid == 0 ]
then
exit 1
fi

if [ $dry_run == 1 ]
then
echo "** Running DRY RUN **"
fi

#Run every script in the build folder
echo "======= DEPLOY START. ========="
for i in "${TARGET_SPRINT}/${TARGET_OPERATION}"/*.sql; do
if [ $dry_run == 0 ]; then
sqlcmd -S $SERVER_NAME -U $DB_USER -P $DB_PASSWORD -d $DB_NAME -i "$i" -b -I
fi
count=$?
if [ $count -ne 0 ]; then
echo " ==== SCRIPT ${i} RETURNS AN ERROR. ========="
exit 1;
else
echo " ==== SCRIPT ${i} COMPLETED SUCCESSFULLY. =========" && echo $count > /tmp/log.txt
fi
done
echo " ==== DB SCHEMA LOADED ========"

echo "======= DEPLOY END ========"
74 changes: 0 additions & 74 deletions source/database/mssql/scripts/dbscripts/db-upgrade.sh

This file was deleted.

45 changes: 0 additions & 45 deletions source/database/mssql/scripts/dbscripts/deploy-transaction.sh

This file was deleted.

29 changes: 13 additions & 16 deletions source/database/mssql/scripts/dbscripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo -e "* Enter the name of the target folder to run all db scripts against the database.\nValid values are: Alter Up, Alter Down, Build, Drop, Test Data"
read TARGET_FOLDER
#load db schema to local
#Run every scripts in the build folder
echo "======= SCRIPT SCHEMA START. ========="
for i in "${TARGET_SPRINT}/${TARGET_FOLDER}"/*.sql; do
sqlcmd -S $SERVER_NAME -U $DB_USER -P $DB_PASSWORD -d $DB_NAME -i "$i" -b -I
count=$?
if [ $count -ne 0 ];
then echo "======= SCRIPT ${i} RETURNS AN ERROR. ========="
exit 1;
else echo "======= SCRIPT ${i} COMPLETED SUCCESSFULLY. =========" && echo $count > /tmp/log.txt
fi
done
echo "======= DB SCHEMA LOADED ========"
fi
read TARGET_OPERATION

MASTER_FILE="master.sql";

echo "======= END ========"
directory="${TARGET_SPRINT}/${TARGET_OPERATION}";

if [ -f "$directory/$MASTER_FILE" ]; then
echo " * Info: master.sql found. Running transaction..."
./db-deploy-transaction.sh -s $TARGET_SPRINT -o "$TARGET_OPERATION"
else
echo " * Info: master.sql found. Running transaction..."
./db-deploy.sh -s $TARGET_SPRINT -o "$TARGET_OPERATION"
fi
fi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ generate_file()
for sql_file in "$SQLFOLDER"/*.sql; do
[[ "$sql_file" = *"master.sql" ]] && continue

echo " Processing '${sql_file}'..."
echo " Processing '${sql_file}'..."
echo " PRINT '- Executing" $sql_file "'" >> "${FILE}"
echo ' :r' $sql_file >> "${FILE}"
done
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--End script section

PRINT '======= DB TRANSACTION FINISH ========'
PRINT ' == DB TRANSACTION FINISH ========'
GO

If XACT_STATE()=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
SET XACT_ABORT ON
GO

PRINT '======= DB SCRIPT START ========'
PRINT ' == DB SCRIPT START ========'
GO

BEGIN TRANSACTION
PRINT '======= DB TRANSACTION START ========'
PRINT ' == DB TRANSACTION START ========'
GO

--Script section
Loading
Loading