diff --git a/database/mssql/scripts/sampledata/permit.ORBC_CREDIT_ACCOUNT_NUMBER_SEQ.sql b/database/mssql/scripts/sampledata/permit.ORBC_CREDIT_ACCOUNT_NUMBER_SEQ.sql new file mode 100644 index 000000000..37e5a0a35 --- /dev/null +++ b/database/mssql/scripts/sampledata/permit.ORBC_CREDIT_ACCOUNT_NUMBER_SEQ.sql @@ -0,0 +1,12 @@ +SET NOCOUNT ON +GO +DECLARE @TimestampCurrent int; +DECLARE @s nvarchar(1000); + +SELECT @TimestampCurrent = DATEDIFF(SECOND, '19700101', sysutcdatetime()); + +SET @s = N' +ALTER SEQUENCE [permit].[ORBC_CREDIT_ACCOUNT_NUMBER_SEQ] +RESTART WITH ' + CAST(@TimestampCurrent AS nvarchar(10)) + +EXEC(@s) diff --git a/database/mssql/scripts/utility/legacy/revert-db-complete.sh b/database/mssql/scripts/utility/legacy/revert-db-complete.sh deleted file mode 100644 index 8d1c5a5b3..000000000 --- a/database/mssql/scripts/utility/legacy/revert-db-complete.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -# Retrieve arguments -source ${SCRIPT_DIR}/utility/getopt.sh -USAGE="-u USER -p PASS -s SERVER -d DATABASE" -parse_options "${USAGE}" ${@} - -# Resets the database schema in the MOTI hosted database from the schema -# DDL files in the versions/revert directory. - -# This script should only be done in a non-production database because it will -# wipe any data that currently exists (all tables will be dropped). - -# This is intended to be run from the local docker sql-server-db container, or -# a similar linux environment with the requisite env variables and sqlcmd -# installed in /opt/mssql-tools/bin/. Note you must be connected to the BC Gov -# Citrix VPN in order for the connection to be established. - -# Retrieve the version of the ORBC database from the version history table. -# If the version history table does not exist then a value of zero (0) will -# be returned. -ORBC_DB_VERSION=$(sqlcmd -C -U ${USER} -P ${PASS} -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${SCRIPT_DIR}/get-orbc-db-version.sql) - -echo "**LEGACY** ORBC DB Version: ${ORBC_DB_VERSION}" - -# Execute all revert sql files in sequence, starting with the current db version. -# DDL file names match the following pattern: v_N_ddl_revert.sql where N is the -# current database version. -# Example: v_2_ddl_revert.sql will revert the database from version 2 to version 1. - -# This script assumes a SCRIPT_DIR environment variable has been set, indicating the -# root database scripts directory. -# DDL files must be in the database/mssql/scripts/versions/revert directory in -# git, and in ${SCRIPT_DIR}/versions/revert directory on the computer this script -# is running on. -echo "**LEGACY** Reverting database to empty state..." -((NEXTVER=ORBC_DB_VERSION)) -echo "**LEGACY** Initial revert file to look for: ${SCRIPT_DIR}/versions/revert/v_${NEXTVER}_ddl_revert.sql" - -while test -f "${SCRIPT_DIR}/versions/revert/v_${NEXTVER}_ddl_revert.sql"; do - ${SCRIPT_DIR}/utility/legacy/revert-db-single.sh -v ${NEXTVER} -u ${USER} -p "${PASS}" -s ${SERVER} -d ${DATABASE} - ((NEXTVER=NEXTVER-1)) - echo "**LEGACY** Next revert file to check: ${SCRIPT_DIR}/versions/revert/v_${NEXTVER}_ddl_revert.sql" -done - -echo "**LEGACY** Finished reverting database to empty state" diff --git a/database/mssql/scripts/utility/legacy/revert-db-single.sh b/database/mssql/scripts/utility/legacy/revert-db-single.sh deleted file mode 100644 index b31867d02..000000000 --- a/database/mssql/scripts/utility/legacy/revert-db-single.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -# Retrieve arguments -source ${SCRIPT_DIR}/utility/getopt.sh -USAGE="-v VERSION -u USER -p PASS -s SERVER -d DATABASE" -parse_options "${USAGE}" ${@} - -# Reverts a single version upgrade to the MOTI database. The version to -# revert from must be supplied as a parameter to this script, as do the -# connection details and credentials. -# Example: 'revert-db-single.sh -v 3 -u -p -s -d ' -# will revert the database from version 3 to version 2. - -# Retrieve the version of the ORBC database from the version history table. -# If the version history table does not exist then a value of zero (0) will -# be returned. -ORBC_DB_VERSION=$(sqlcmd -C -U ${USER} -P ${PASS} -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${SCRIPT_DIR}/get-orbc-db-version.sql) - -echo "**LEGACY** ORBC DB Version: ${ORBC_DB_VERSION}" - -# Exit script if current database version does not match the version to revert. -if [[ ORBC_DB_VERSION -ne ${VERSION} ]]; then - echo "**LEGACY** ERROR: the current database version must match the version to revert." - exit -fi - -if test -f "${SCRIPT_DIR}/versions/revert/v_${VERSION}_ddl_revert.sql"; then - echo "**LEGACY** Executing ${SCRIPT_DIR}/versions/revert/v_${VERSION}_ddl_revert.sql" - sqlcmd -C -U ${USER} -P "${PASS}" -S ${SERVER} -d ${DATABASE} -i ${SCRIPT_DIR}/versions/revert/v_${VERSION}_ddl_revert.sql -else - echo "**LEGACY** ERROR: migration file ${SCRIPT_DIR}/versions/revert/v_${VERSION}_ddl_revert.sql not found." - exit -fi - -((NEW_DB_VERSION=${VERSION}-1)) -echo "**LEGACY** Reverted database to version ${NEW_DB_VERSION}" diff --git a/database/mssql/scripts/utility/legacy/revert-moti-db.sh b/database/mssql/scripts/utility/legacy/revert-moti-db.sh deleted file mode 100644 index f5ebb7e93..000000000 --- a/database/mssql/scripts/utility/legacy/revert-moti-db.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -# Legacy script added to allow migration from the old database revert script -# style to the new style. -# -# This script may be deleted from the repository (along with all other scripts -# in the legacy directory along with the legacy directory itself) once all -# database instances (dev, test, demo, uat, prod, etc) have been updates. -# -# This script will revert the target database to empty state using the revert -# scripts in the git repository instead of the revert scripts saved in the -# database itself. -# -# Once reverted, the standard reset-moti-db.sh or migrate-db-current.sh scripts -# may be executed against the target database going forward and this script -# is no longer used. -${SCRIPT_DIR}/utility/legacy/revert-db-complete.sh -u ${MSSQL_MOTI_USER} -p "${MSSQL_MOTI_PASSWORD}" -s ${MSSQL_MOTI_HOST} -d ${MSSQL_MOTI_DB} \ No newline at end of file diff --git a/database/mssql/scripts/utility/refresh-sample-data.sh b/database/mssql/scripts/utility/refresh-sample-data.sh index eb6d15d2d..022bf96f4 100644 --- a/database/mssql/scripts/utility/refresh-sample-data.sh +++ b/database/mssql/scripts/utility/refresh-sample-data.sh @@ -26,4 +26,7 @@ sqlcmd -C -U ${ORBC_USER} -P "${ORBC_PASS}" -S ${ORBC_SERVER} -d ${ORBC_DATABASE sqlcmd -C -U ${ORBC_USER} -P "${ORBC_PASS}" -S ${ORBC_SERVER} -d ${ORBC_DATABASE} -i ${SCRIPT_DIR}/sampledata/dbo.ORBC_TRAILER.Table.sql sqlcmd -C -U ${ORBC_USER} -P "${ORBC_PASS}" -S ${ORBC_SERVER} -d ${ORBC_DATABASE} -i ${SCRIPT_DIR}/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql +echo "Setting credit account sequence restart to current timestamp (used only for lower environments)" +sqlcmd -C -U ${ORBC_USER} -P "${ORBC_PASS}" -S ${ORBC_SERVER} -d ${ORBC_DATABASE} -i ${SCRIPT_DIR}/sampledata/permit.ORBC_CREDIT_ACCOUNT_NUMBER_SEQ.sql + echo "Finished loading sample data" diff --git a/database/mssql/scripts/versions/revert/sample-ddl-revert.sql b/database/mssql/scripts/versions/revert/sample-ddl-revert.sql index c860527f1..becad58e5 100644 --- a/database/mssql/scripts/versions/revert/sample-ddl-revert.sql +++ b/database/mssql/scripts/versions/revert/sample-ddl-revert.sql @@ -9,22 +9,36 @@ SET NOCOUNT ON GO SET XACT_ABORT ON +GO +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE +GO +BEGIN TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO --- Sample of rolling back a Table creation wrapped in a TRANSACTION ---BEGIN TRY --- BEGIN TRANSACTION --- DROP TABLE [dbo].[ORBC_NEW_TABLE] --- COMMIT ---END TRY +-- Revert code goes here ---BEGIN CATCH --- IF @@TRANCOUNT > 0 --- ROLLBACK; --- THROW ---END CATCH +IF @@ERROR <> 0 SET NOEXEC ON +GO --- Also remember to rollback the version number from your migration! DECLARE @VersionDescription VARCHAR(255) SET @VersionDescription = '*** Enter description of DB change here ***' INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (/*<>*/, @VersionDescription, getutcdate()) +IF @@ERROR <> 0 SET NOEXEC ON +GO + +COMMIT TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO diff --git a/database/mssql/scripts/versions/revert/v_30_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_30_ddl_revert.sql index e224de708..065b82ca5 100644 --- a/database/mssql/scripts/versions/revert/v_30_ddl_revert.sql +++ b/database/mssql/scripts/versions/revert/v_30_ddl_revert.sql @@ -1,18 +1,14 @@ SET ANSI_NULLS ON GO - SET QUOTED_IDENTIFIER ON GO - SET NOCOUNT ON GO SET XACT_ABORT ON GO - SET TRANSACTION ISOLATION LEVEL SERIALIZABLE GO - BEGIN TRANSACTION GO @@ -132,32 +128,27 @@ BEGIN CATCH EXEC orbc_error_handling END CATCH; +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO -BEGIN TRY - DELETE - FROM [dbo].[ORBC_DIRECTORY_TYPE] - WHERE DIRECTORY_TYPE = 'SERVICE_ACCOUNT' - - ALTER TABLE [permit].[ORBC_TRANSACTION] - - DROP COLUMN [PAYER_NAME] - - ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] - - DROP COLUMN [PAYER_NAME] - - COMMIT -END TRY +DELETE +FROM [dbo].[ORBC_DIRECTORY_TYPE] +WHERE DIRECTORY_TYPE = 'SERVICE_ACCOUNT' +IF @@ERROR <> 0 SET NOEXEC ON +GO -BEGIN CATCH - IF @@TRANCOUNT > 0 - ROLLBACK; +ALTER TABLE [permit].[ORBC_TRANSACTION] +DROP COLUMN [PAYER_NAME] +IF @@ERROR <> 0 SET NOEXEC ON +GO - THROW -END CATCH +ALTER TABLE [permit].[ORBC_TRANSACTION_HIST] +DROP COLUMN [PAYER_NAME] +IF @@ERROR <> 0 SET NOEXEC ON +GO DECLARE @VersionDescription VARCHAR(255) - SET @VersionDescription = 'Reverting initial creation ofservice account in directory table.' INSERT [dbo].[ORBC_SYS_VERSION] ( @@ -170,3 +161,19 @@ VALUES ( @VersionDescription, getutcdate() ) +IF @@ERROR <> 0 SET NOEXEC ON +GO + +COMMIT TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO diff --git a/database/mssql/scripts/versions/revert/v_38_ddl_revert.sql b/database/mssql/scripts/versions/revert/v_38_ddl_revert.sql new file mode 100644 index 000000000..7cdc00aa4 --- /dev/null +++ b/database/mssql/scripts/versions/revert/v_38_ddl_revert.sql @@ -0,0 +1,62 @@ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +SET NOCOUNT ON +GO + +SET XACT_ABORT ON +GO +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE +GO +BEGIN TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO + +-- Note that we are intentionally not going to reset the max value of the +-- credit account sequence back to 9999 - if it ever does come into play +-- it will be better to not have the 9999 limit there to avoid duplicates +-- in the database. +-- We will also maintain the column length at 15 characters in the event +-- that credit accounts were created that exceed that, and we would not +-- want to delete or modify those records once in place. +-- So, this should be considered a non-revertable change - if necessary it +-- must be reverted manually and very carefully. +-- Including commented-out SQL for the revert below for reference, if we +-- were to choose to do it. + +--ALTER SEQUENCE [permit].[ORBC_CREDIT_ACCOUNT_NUMBER_SEQ] +-- MAXVALUE 9999 +--IF @@ERROR <> 0 SET NOEXEC ON +--GO + +--ALTER TABLE [permit].[ORBC_CREDIT_ACCOUNT] +-- ALTER COLUMN CREDIT_ACCOUNT_NUMBER nvarchar(6) NOT NULL +--IF @@ERROR <> 0 SET NOEXEC ON +--GO +--ALTER TABLE [permit].[ORBC_CREDIT_ACCOUNT_HIST] +-- ALTER COLUMN CREDIT_ACCOUNT_NUMBER nvarchar(6) +--IF @@ERROR <> 0 SET NOEXEC ON +--GO + +DECLARE @VersionDescription VARCHAR(255) +SET @VersionDescription = 'Reverting credit account sequence updates' + +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (37, @VersionDescription, getutcdate()) +IF @@ERROR <> 0 SET NOEXEC ON +GO + +COMMIT TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO diff --git a/database/mssql/scripts/versions/v_38_ddl.sql b/database/mssql/scripts/versions/v_38_ddl.sql new file mode 100644 index 000000000..fa4d5041b --- /dev/null +++ b/database/mssql/scripts/versions/v_38_ddl.sql @@ -0,0 +1,56 @@ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO +SET NOCOUNT ON +GO + +SET XACT_ABORT ON +GO +SET TRANSACTION ISOLATION LEVEL SERIALIZABLE +GO +BEGIN TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO + +-- Increase size of credit account number column because it will be using +-- a 10-digit suffix instead of 4 when we are in dev/test to avoid duplicates +-- in CFS when we refresh the dev/test databases +ALTER TABLE [permit].[ORBC_CREDIT_ACCOUNT] + ALTER COLUMN CREDIT_ACCOUNT_NUMBER nvarchar(12) NOT NULL +IF @@ERROR <> 0 SET NOEXEC ON +GO + +ALTER TABLE [permit].[ORBC_CREDIT_ACCOUNT_HIST] + ALTER COLUMN CREDIT_ACCOUNT_NUMBER nvarchar(12) +IF @@ERROR <> 0 SET NOEXEC ON +GO + +-- Set the sequence to have no max value (big int) so longer credit +-- account numbers may be created in dev/test +ALTER SEQUENCE [permit].[ORBC_CREDIT_ACCOUNT_NUMBER_SEQ] + NO MAXVALUE +IF @@ERROR <> 0 SET NOEXEC ON +GO + +DECLARE @VersionDescription VARCHAR(255) +SET @VersionDescription = 'Update credit account sequence to avoid duplicates in dev and test' + +INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (38, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate()) +IF @@ERROR <> 0 SET NOEXEC ON +GO + +COMMIT TRANSACTION +GO +IF @@ERROR <> 0 SET NOEXEC ON +GO +DECLARE @Success AS BIT +SET @Success = 1 +SET NOEXEC OFF +IF (@Success = 1) PRINT 'The database update succeeded' +ELSE BEGIN + IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION + PRINT 'The database update failed' +END +GO \ No newline at end of file diff --git a/database/mssql/test/versions/v_22_test.sh b/database/mssql/test/versions/v_22_test.sh index a4ad2567a..e013e2d64 100644 --- a/database/mssql/test/versions/v_22_test.sh +++ b/database/mssql/test/versions/v_22_test.sh @@ -19,5 +19,6 @@ TEST_22_2_RESULT=$(/opt/mssql-tools/bin/sqlcmd -U ${USER} -P "${PASS}" -S ${SERV if [[ $TEST_22_2_RESULT -eq 22 ]]; then echo "Test 22.2 passed: Correct number of role mappings inserted" else - echo "******** Test 22.2 failed: Incorrect number of role mappings inserted" + echo "******** Test 22.2 failed: Incorrect number of role mappings inserted: " + echo $TEST_22_2_RESULT fi diff --git a/database/mssql/test/versions/v_28_5_test.sql b/database/mssql/test/versions/v_28_5_test.sql index 0be143058..f05c39b78 100644 --- a/database/mssql/test/versions/v_28_5_test.sql +++ b/database/mssql/test/versions/v_28_5_test.sql @@ -2,4 +2,4 @@ SET NOCOUNT ON SELECT COUNT(*) FROM $(DB_NAME).[permit].[ORBC_CREDIT_ACCOUNT_STATUS_TYPE] -WHERE CREDIT_ACCOUNT_STATUS_TYPE IN ('ONHOLD','ACTIVE','SETUP','CLOSED') \ No newline at end of file +WHERE CREDIT_ACCOUNT_STATUS_TYPE IN ('ONHOLD','ACTIVE','SETUP_FAIL','CLOSED') \ No newline at end of file diff --git a/database/mssql/test/versions/v_32_test.sh b/database/mssql/test/versions/v_32_test.sh index 941c2fa96..ac1d0bc68 100644 --- a/database/mssql/test/versions/v_32_test.sh +++ b/database/mssql/test/versions/v_32_test.sh @@ -9,7 +9,8 @@ parse_options "${USAGE}" ${@} # TESTS_DIR variable set by the calling test-runner script. TEST_32_1_RESULT=$(/opt/mssql-tools/bin/sqlcmd -U ${USER} -P "${PASS}" -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${TESTS_DIR}/v_32_1_test.sql | xargs) -if [[ $TEST_32_1_RESULT -eq 30 ]]; then +# Returns 60 for an nvarchar(30) data type due to larger nvarchar storage +if [[ $TEST_32_1_RESULT -eq 60 ]]; then echo "Test 32.1 passed: APP_LAST_UPDATE_USERID column created in ORBC_POLICY_CONFIGURATION" else echo "******** Test 32.1 failed: APP_LAST_UPDATE_USERID column missing in ORBC_POLICY_CONFIGURATION" diff --git a/database/mssql/test/versions/v_33_1_test.sql b/database/mssql/test/versions/v_33_1_test.sql index 7f37d63ab..fa881878d 100644 --- a/database/mssql/test/versions/v_33_1_test.sql +++ b/database/mssql/test/versions/v_33_1_test.sql @@ -1,5 +1,5 @@ -- Test that the role types have been inserted correctly SET NOCOUNT ON -SELECT COUNT(*) FROM $(DB_NAME).[access].[ORBC_ROLE_TYPE] +SELECT COUNT(*) FROM $(DB_NAME).[access].[ORBC_GROUP_ROLE] WHERE ROLE_TYPE ='ORBC-READ-CREDIT-ACCOUNT' AND USER_AUTH_GROUP_TYPE IN ('PPCCLERK', 'CTPO', 'HQADMIN') \ No newline at end of file diff --git a/database/mssql/test/versions/v_38_1_test.sql b/database/mssql/test/versions/v_38_1_test.sql new file mode 100644 index 000000000..e0804ebbf --- /dev/null +++ b/database/mssql/test/versions/v_38_1_test.sql @@ -0,0 +1,4 @@ +-- Test that the APP_LAST_UPDATE_USERID column has been added correctly +SET NOCOUNT ON + +select COL_LENGTH('$(DB_NAME).[permit].[ORBC_CREDIT_ACCOUNT]', 'CREDIT_ACCOUNT_NUMBER') diff --git a/database/mssql/test/versions/v_38_test.sh b/database/mssql/test/versions/v_38_test.sh new file mode 100644 index 000000000..4690a2161 --- /dev/null +++ b/database/mssql/test/versions/v_38_test.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Retrieve arguments +source ${SCRIPT_DIR}/utility/getopt.sh +USAGE="-u USER -p PASS -s SERVER -d DATABASE" +parse_options "${USAGE}" ${@} + +# All database tests for database version 32 are run from this shell script. +# TESTS_DIR variable set by the calling test-runner script. + +TEST_38_1_RESULT=$(/opt/mssql-tools/bin/sqlcmd -U ${USER} -P "${PASS}" -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${TESTS_DIR}/v_38_1_test.sql | xargs) +# Returns 30 for an nvarchar(15) data type due to larger nvarchar storage +if [[ $TEST_38_1_RESULT -eq 30 ]]; then + echo "Test 38.1 passed: CREDIT_ACCOUNT_NUMBER column length updated correctly" +else + echo "******** Test 38.1 failed: CREDIT_ACCOUNT_NUMBER column length not updated correctly: " + echo $TEST_38_1_RESULT +fi \ No newline at end of file diff --git a/vehicles/src/modules/credit-account/entities/credit-account.entity.ts b/vehicles/src/modules/credit-account/entities/credit-account.entity.ts index 909b4763a..0c3e33b97 100644 --- a/vehicles/src/modules/credit-account/entities/credit-account.entity.ts +++ b/vehicles/src/modules/credit-account/entities/credit-account.entity.ts @@ -76,7 +76,7 @@ export class CreditAccount extends Base { * The unique number associated with the credit account. Its the ORBC_CREDIT_ACCOUNT_NUMBER_SEQ prefixed with WS. */ @AutoMap() - @Column({ length: 6, name: 'CREDIT_ACCOUNT_NUMBER', nullable: false }) + @Column({ length: 12, name: 'CREDIT_ACCOUNT_NUMBER', nullable: false }) creditAccountNumber: string; /**