From 7a26501057f94f2c5810da139fc9fd0e4c6830af Mon Sep 17 00:00:00 2001 From: hongwei Date: Tue, 4 Jun 2024 12:07:45 +0200 Subject: [PATCH] bugfix/migration issue - add dbDriver to helper methods --- ...rationOfAccountAccessAddedConsumerId.scala | 19 ++----- .../util/migration/MigrationOfAuthUser.scala | 16 ++---- ...grationOfConsentAuthContextDropIndex.scala | 12 +--- ...tionOfMappedBadLoginAttemptDropIndex.scala | 12 +--- ...igrationOfUserAuthContextFieldLength.scala | 4 +- obp-api/src/main/scala/code/util/Helper.scala | 55 ++++++++++++------- .../src/test/scala/code/util/HelperTest.scala | 19 +++++-- 7 files changed, 67 insertions(+), 70 deletions(-) diff --git a/obp-api/src/main/scala/code/api/util/migration/MigrationOfAccountAccessAddedConsumerId.scala b/obp-api/src/main/scala/code/api/util/migration/MigrationOfAccountAccessAddedConsumerId.scala index b067af28c6..f1ea0a308b 100644 --- a/obp-api/src/main/scala/code/api/util/migration/MigrationOfAccountAccessAddedConsumerId.scala +++ b/obp-api/src/main/scala/code/api/util/migration/MigrationOfAccountAccessAddedConsumerId.scala @@ -27,20 +27,11 @@ object MigrationOfAccountAccessAddedConsumerId { val executedSql = DbFunction.maybeWrite(true, Schemifier.infoF _) { - APIUtil.getPropsValue("db.driver") match { - case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => - () => - s""" - |${Helper.addColumnIfNotExists("accountaccess", "consumer_id", ALL_CONSUMERS)} - |${Helper.dropIndexIfExists("accountaccess", "accountaccess_bank_id_account_id_view_fk_user_fk")} - |""".stripMargin - case _ => - () => - s""" - |ALTER TABLE accountaccess ADD COLUMN IF NOT EXISTS "consumer_id" character varying(255) DEFAULT '$ALL_CONSUMERS'; - |DROP INDEX IF EXISTS accountaccess_bank_id_account_id_view_fk_user_fk; - |""".stripMargin - } + val dbDriver = APIUtil.getPropsValue("db.driver","org.h2.Driver") + () => s""" + |${Helper.addColumnIfNotExists(dbDriver,"accountaccess", "consumer_id", ALL_CONSUMERS)} + |${Helper.dropIndexIfExists(dbDriver, "accountaccess", "accountaccess_bank_id_account_id_view_fk_user_fk")} + |""".stripMargin } val endDate = System.currentTimeMillis() diff --git a/obp-api/src/main/scala/code/api/util/migration/MigrationOfAuthUser.scala b/obp-api/src/main/scala/code/api/util/migration/MigrationOfAuthUser.scala index 05e95e0370..668764de7b 100644 --- a/obp-api/src/main/scala/code/api/util/migration/MigrationOfAuthUser.scala +++ b/obp-api/src/main/scala/code/api/util/migration/MigrationOfAuthUser.scala @@ -30,7 +30,7 @@ object MigrationOfAuthUser { case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => () => s""" - |${Helper.dropIndexIfExists("authUser", "authuser_username_provider")} + |${Helper.dropIndexIfExists(value,"authUser", "authuser_username_provider")} | |ALTER TABLE authuser ALTER COLUMN username varchar(100); |ALTER TABLE authuser ALTER COLUMN provider varchar(100); @@ -38,7 +38,7 @@ object MigrationOfAuthUser { |ALTER TABLE authuser ALTER COLUMN lastname varchar(100); |ALTER TABLE authuser ALTER COLUMN email varchar(100); | - |${Helper.createIndexIfNotExists("authUser", "authuser_username_provider")} + |${Helper.createIndexIfNotExists(value,"authUser", "authuser_username_provider")} |""".stripMargin case _ => () => @@ -83,15 +83,9 @@ object MigrationOfAuthUser { val executedSql = DbFunction.maybeWrite(true, Schemifier.infoF _) { - APIUtil.getPropsValue("db.driver") match { - case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => - () => - s"""${Helper.dropIndexIfExists("authuser", "authuser_username")}""" - case _ => - () => - """DROP INDEX IF EXISTS authuser_username;""".stripMargin - } - + val dbDriver = APIUtil.getPropsValue("db.driver", "org.h2.Driver") + () => + s"""${Helper.dropIndexIfExists(dbDriver, "authuser", "authuser_username")}""" } val endDate = System.currentTimeMillis() diff --git a/obp-api/src/main/scala/code/api/util/migration/MigrationOfConsentAuthContextDropIndex.scala b/obp-api/src/main/scala/code/api/util/migration/MigrationOfConsentAuthContextDropIndex.scala index e1cf17bb53..5bac75e56c 100644 --- a/obp-api/src/main/scala/code/api/util/migration/MigrationOfConsentAuthContextDropIndex.scala +++ b/obp-api/src/main/scala/code/api/util/migration/MigrationOfConsentAuthContextDropIndex.scala @@ -27,15 +27,9 @@ object MigrationOfConsentAuthContextDropIndex { val executedSql = DbFunction.maybeWrite(true, Schemifier.infoF _) { - APIUtil.getPropsValue("db.driver") match { - case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => - () => - s""" - |${Helper.dropIndexIfExists("MappedConsentAuthContext", "consentauthcontext_consentid_key_c")} - |""".stripMargin - case _ => - () => "DROP INDEX IF EXISTS consentauthcontext_consentid_key_c;" - } + val dbDriver = APIUtil.getPropsValue("db.driver", "org.h2.Driver") + () => + s"""${Helper.dropIndexIfExists(dbDriver, "MappedConsentAuthContext", "consentauthcontext_consentid_key_c")}""".stripMargin } val endDate = System.currentTimeMillis() diff --git a/obp-api/src/main/scala/code/api/util/migration/MigrationOfMappedBadLoginAttemptDropIndex.scala b/obp-api/src/main/scala/code/api/util/migration/MigrationOfMappedBadLoginAttemptDropIndex.scala index 4055d5e9af..0c0391caf9 100644 --- a/obp-api/src/main/scala/code/api/util/migration/MigrationOfMappedBadLoginAttemptDropIndex.scala +++ b/obp-api/src/main/scala/code/api/util/migration/MigrationOfMappedBadLoginAttemptDropIndex.scala @@ -24,15 +24,9 @@ object MigrationOfMappedBadLoginAttemptDropIndex { val executedSql = DbFunction.maybeWrite(true, Schemifier.infoF _) { - APIUtil.getPropsValue("db.driver") match { - case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => - () => - s""" - |${Helper.dropIndexIfExists("mappedbadloginattempt", "mappedbadloginattempt_musername")} - |""".stripMargin - case _ => - () => "DROP INDEX IF EXISTS mappedbadloginattempt_musername;" - } + val dbDriver = APIUtil.getPropsValue("db.driver", "org.h2.Driver") + () => + s"""${Helper.dropIndexIfExists(dbDriver, "mappedbadloginattempt", "mappedbadloginattempt_musername")}""".stripMargin } val endDate = System.currentTimeMillis() diff --git a/obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAuthContextFieldLength.scala b/obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAuthContextFieldLength.scala index 80476d012b..8025e2c17f 100644 --- a/obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAuthContextFieldLength.scala +++ b/obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAuthContextFieldLength.scala @@ -29,12 +29,12 @@ object MigrationOfUserAuthContextFieldLength { case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => () => s""" - |${Helper.dropIndexIfExists("mappeduserauthcontext", "mappeduserauthcontext_muserid_mkey_createdat")} + |${Helper.dropIndexIfExists(value,"mappeduserauthcontext", "mappeduserauthcontext_muserid_mkey_createdat")} | |ALTER TABLE MappedUserAuthContext ALTER COLUMN mKey varchar(4000); |ALTER TABLE MappedUserAuthContext ALTER COLUMN mValue varchar(4000); | - |${Helper.createIndexIfNotExists("mappeduserauthcontext", "mappeduserauthcontext_muserid_mkey_createdat")} + |${Helper.createIndexIfNotExists(value, "mappeduserauthcontext", "mappeduserauthcontext_muserid_mkey_createdat")} | |""".stripMargin case _ => diff --git a/obp-api/src/main/scala/code/util/Helper.scala b/obp-api/src/main/scala/code/util/Helper.scala index 4b7ad6f461..be134a3c3b 100644 --- a/obp-api/src/main/scala/code/util/Helper.scala +++ b/obp-api/src/main/scala/code/util/Helper.scala @@ -518,27 +518,40 @@ object Helper extends Loggable { enhancer.create().asInstanceOf[S] } - def addColumnIfNotExists(tableName: String, columName: String, default: String) = - s""" - |IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$tableName' AND COLUMN_NAME = '$columName') - |BEGIN - | ALTER TABLE $tableName ADD $columName VARCHAR(255) DEFAULT '$default'; - |END""".stripMargin - - - def dropIndexIfExists(tableName: String, index: String) = - s""" - |IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = '$index' AND object_id = OBJECT_ID('$tableName')) - |BEGIN - | DROP INDEX $tableName.$index; - |END""".stripMargin - - def createIndexIfNotExists(tableName: String, index: String) = - s""" - |IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = '$index' AND object_id = OBJECT_ID('$tableName')) - |BEGIN - | CREATE INDEX $index on $tableName(${index.split("_").drop(1).mkString(",")}); - |END""".stripMargin + def addColumnIfNotExists(dbDriver: String, tableName: String, columName: String, default: String) = { + if (dbDriver.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver")) + s""" + |IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$tableName' AND COLUMN_NAME = '$columName') + |BEGIN + | ALTER TABLE $tableName ADD $columName VARCHAR(255) DEFAULT '$default'; + |END""".stripMargin + else + s"""ALTER TABLE $tableName ADD COLUMN IF NOT EXISTS "$columName" character varying(255) DEFAULT '$default';""".stripMargin + } + + + def dropIndexIfExists(dbDriver: String, tableName: String, index: String) = { + if (dbDriver.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver")) + s""" + |IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = '$index' AND object_id = OBJECT_ID('$tableName')) + |BEGIN + | DROP INDEX $tableName.$index; + |END""".stripMargin + else + s"""DROP INDEX IF EXISTS $index;""".stripMargin + } + + + def createIndexIfNotExists(dbDriver: String, tableName: String, index: String) = { + if (dbDriver.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver")) + s""" + |IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = '$index' AND object_id = OBJECT_ID('$tableName')) + |BEGIN + | CREATE INDEX $index on $tableName(${index.split("_").drop(1).mkString(",")}); + |END""".stripMargin + else + s"CREATE INDEX IF NOT EXISTS $index on $tableName(${index.split("_").drop(1).mkString(",")});" + } } \ No newline at end of file diff --git a/obp-api/src/test/scala/code/util/HelperTest.scala b/obp-api/src/test/scala/code/util/HelperTest.scala index ce00415858..724dad94de 100644 --- a/obp-api/src/test/scala/code/util/HelperTest.scala +++ b/obp-api/src/test/scala/code/util/HelperTest.scala @@ -65,7 +65,7 @@ class HelperTest extends FeatureSpec with Matchers with GivenWhenThen with Props feature(s"test Helper.getIfNotExistsAddedColumLengthForMsSqlServer method") { - scenario(s"test case 1") { + scenario(s"test case addColumnIfNotExists") { val expectedValue = s""" |IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'accountaccess' AND COLUMN_NAME = 'consumer_id') @@ -73,10 +73,10 @@ class HelperTest extends FeatureSpec with Matchers with GivenWhenThen with Props | ALTER TABLE accountaccess ADD consumer_id VARCHAR(255) DEFAULT '$ALL_CONSUMERS'; |END""".stripMargin - Helper.addColumnIfNotExists("accountaccess", "consumer_id", ALL_CONSUMERS) should be(expectedValue) + Helper.addColumnIfNotExists("com.microsoft.sqlserver.jdbc.SQLServerDriver","accountaccess", "consumer_id", ALL_CONSUMERS) should be(expectedValue) } - scenario(s"test case 2") { + scenario(s"test case dropIndexIfExists") { val expectedValue = s""" |IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'accountaccess_bank_id_account_id_view_fk_user_fk' AND object_id = OBJECT_ID('accountaccess')) @@ -84,7 +84,18 @@ class HelperTest extends FeatureSpec with Matchers with GivenWhenThen with Props | DROP INDEX accountaccess.accountaccess_bank_id_account_id_view_fk_user_fk; |END""".stripMargin - Helper.dropIndexIfExists("accountaccess", "accountaccess_bank_id_account_id_view_fk_user_fk") should be(expectedValue) + Helper.dropIndexIfExists("com.microsoft.sqlserver.jdbc.SQLServerDriver","accountaccess", "accountaccess_bank_id_account_id_view_fk_user_fk") should be(expectedValue) + } + + scenario(s"test case createIndexIfNotExists") { + val expectedValue = + s""" + |IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'authuser_username_provider' AND object_id = OBJECT_ID('authUser')) + |BEGIN + | CREATE INDEX authuser_username_provider on authUser(username,provider); + |END""".stripMargin + + Helper.createIndexIfNotExists("com.microsoft.sqlserver.jdbc.SQLServerDriver","authUser", "authuser_username_provider") should be(expectedValue) } }