Skip to content

Commit

Permalink
bugfix/migration issue - add dbDriver to helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hongwei1 committed Jun 4, 2024
1 parent 0c36a4d commit 7a26501
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ 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);
|ALTER TABLE authuser ALTER COLUMN firstname varchar(100);
|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 _ =>
() =>
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ =>
Expand Down
55 changes: 34 additions & 21 deletions obp-api/src/main/scala/code/util/Helper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(",")});"
}


}
19 changes: 15 additions & 4 deletions obp-api/src/test/scala/code/util/HelperTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,37 @@ 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')
|BEGIN
| 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'))
|BEGIN
| 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)
}

}
Expand Down

0 comments on commit 7a26501

Please sign in to comment.