Skip to content

Commit

Permalink
Merge pull request #2392 from hongwei1/develop
Browse files Browse the repository at this point in the history
bugfix/migration issue
  • Loading branch information
simonredfern authored Jun 4, 2024
2 parents 5bf3f7f + 7a26501 commit ffc4e7d
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 143 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 @@ -5,6 +5,7 @@ import java.time.{ZoneId, ZonedDateTime}

import code.api.util.APIUtil
import code.api.util.migration.Migration.{DbFunction, saveLog}
import code.util.Helper
import code.model.dataAccess.AuthUser
import net.liftweb.common.Full
import net.liftweb.mapper.{DB, Schemifier}
Expand All @@ -28,12 +29,16 @@ object MigrationOfAuthUser {
APIUtil.getPropsValue("db.driver") match {
case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") =>
() =>
"""
s"""
|${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(value,"authUser", "authuser_username_provider")}
|""".stripMargin
case _ =>
() =>
Expand Down Expand Up @@ -78,14 +83,9 @@ object MigrationOfAuthUser {

val executedSql =
DbFunction.maybeWrite(true, Schemifier.infoF _) {
APIUtil.getPropsValue("db.driver") match {
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 @@ -7,8 +7,6 @@ import net.liftweb.common.Full
import code.util.Helper
import net.liftweb.mapper.{DB, Schemifier}
import net.liftweb.util.DefaultConnectionIdentifier
import scalikejdbc.DB.CPContext
import scalikejdbc._
import java.time.format.DateTimeFormatter
import java.time.{ZoneId, ZonedDateTime}

Expand All @@ -20,24 +18,6 @@ object MigrationOfConsentAuthContextDropIndex {
val oneYearInFuture = ZonedDateTime.now(ZoneId.of("UTC")).plusYears(1)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")

/**
* this connection pool context corresponding db.url in default.props
*/
implicit lazy val context: CPContext = {
val settings = ConnectionPoolSettings(
initialSize = 5,
maxSize = 20,
connectionTimeoutMillis = 3000L,
validationQuery = "select 1",
connectionPoolFactoryName = "commons-dbcp2"
)
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
val dbName = "DB_NAME" // corresponding props db.url DB
ConnectionPool.add(dbName, dbUrl, user, password, settings)
val connectionPool = ConnectionPool.get(dbName)
MultipleConnectionPoolContext(ConnectionPool.DEFAULT_NAME -> connectionPool)
}

def dropUniqueIndex(name: String): Boolean = {
DbFunction.tableExists(MappedConsentAuthContext) match {
case true =>
Expand All @@ -47,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 @@ -79,6 +79,8 @@ object MigrationOfFastFireHoseMaterializedView {
APIUtil.getPropsValue("db.driver") openOr("org.h2.Driver") match {
case value if value.contains("org.h2.Driver") =>
() => migrationSql(false)//Note: H2 database, do not support the MATERIALIZED view
case value if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") =>
() => "" //TODO: do not support mssql server yet.
case _ =>
() => migrationSql(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ object MigrationOfFastFireHoseView {

val executedSql =
DbFunction.maybeWrite(true, Schemifier.infoF _) {
() =>
APIUtil.getPropsValue("db.driver") openOr("org.h2.Driver") match {
case value if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") =>
() =>"" //TODO: do not support mssql server yet.
case _ =>
()=>
"""
|CREATE VIEW v_fast_firehose_accounts AS select
| mappedbankaccount.theaccountid as account_id,
Expand Down Expand Up @@ -76,7 +80,7 @@ object MigrationOfFastFireHoseView {
| LEFT JOIN mapperaccountholders
| ON (mappedbankaccount.bank = mapperaccountholders.accountbankpermalink and mappedbankaccount.theaccountid = mapperaccountholders.accountpermalink);
|""".stripMargin
}
}}

val endDate = System.currentTimeMillis()
val comment: String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import code.loginattempts.MappedBadLoginAttempt
import net.liftweb.mapper.{DB, Schemifier}
import net.liftweb.common.Full
import code.util.Helper
import scalikejdbc.DB.CPContext
import scalikejdbc._
import java.time.format.DateTimeFormatter
import java.time.{ZoneId, ZonedDateTime}

Expand All @@ -17,24 +15,6 @@ object MigrationOfMappedBadLoginAttemptDropIndex {
val oneYearInFuture = ZonedDateTime.now(ZoneId.of("UTC")).plusYears(1)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")

/**
* this connection pool context corresponding db.url in default.props
*/
implicit lazy val context: CPContext = {
val settings = ConnectionPoolSettings(
initialSize = 5,
maxSize = 20,
connectionTimeoutMillis = 3000L,
validationQuery = "select 1",
connectionPoolFactoryName = "commons-dbcp2"
)
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
val dbName = "DB_NAME" // corresponding props db.url DB
ConnectionPool.add(dbName, dbUrl, user, password, settings)
val connectionPool = ConnectionPool.get(dbName)
MultipleConnectionPoolContext(ConnectionPool.DEFAULT_NAME -> connectionPool)
}

def dropUniqueIndex(name: String): Boolean = {
DbFunction.tableExists(MappedBadLoginAttempt) match {
case true =>
Expand All @@ -44,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 @@ -2,42 +2,19 @@ package code.api.util.migration

import java.time.format.DateTimeFormatter
import java.time.{ZoneId, ZonedDateTime}

import code.api.Constant
import code.api.util.{APIUtil, DBUtil}
import code.api.util.migration.Migration.{DbFunction, saveLog}
import code.context.MappedUserAuthContext
import code.views.system.AccountAccess
import net.liftweb.mapper.{By, Descending, OrderBy}
import scalikejdbc.DB.CPContext
import scalikejdbc.{DB => scalikeDB, _}

import scala.collection.immutable.List

import net.liftweb.mapper.{By,Descending, OrderBy}
import java.sql.ResultSet
import net.liftweb.db.DB
import net.liftweb.util.DefaultConnectionIdentifier
object MigrationOfUserAuthContext {

val oneDayAgo = ZonedDateTime.now(ZoneId.of("UTC")).minusDays(1)
val oneYearInFuture = ZonedDateTime.now(ZoneId.of("UTC")).plusYears(1)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")

/**
* this connection pool context corresponding db.url in default.props
*/
implicit lazy val context: CPContext = {
val settings = ConnectionPoolSettings(
initialSize = 5,
maxSize = 20,
connectionTimeoutMillis = 3000L,
validationQuery = "select 1",
connectionPoolFactoryName = "commons-dbcp2"
)
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
val dbName = "DB_NAME" // corresponding props db.url DB
ConnectionPool.add(dbName, dbUrl, user, password, settings)
val connectionPool = ConnectionPool.get(dbName)
MultipleConnectionPoolContext(ConnectionPool.DEFAULT_NAME -> connectionPool)
}

def removeDuplicates(name: String): Boolean = {

// Make back up
Expand All @@ -54,18 +31,16 @@ object MigrationOfUserAuthContext {
key: String
)

val result: List[SqlResult] = scalikeDB autoCommit { implicit session =>

val sqlResult =
sql"""select count(mkey), muserid, mkey from mappeduserauthcontext group by muserid, mkey having count(mkey) > 1""".stripMargin
.map(
rs => // Map result to case class
SqlResult(
rs.string(1).toInt,
rs.string(2),
rs.string(3))
).list.apply()
sqlResult
val result = DB.use(DefaultConnectionIdentifier) { conn =>
DB.exec(conn, "select count(mkey), muserid, mkey from mappeduserauthcontext group by muserid, mkey having count(mkey) > 1") {
rs: ResultSet => {
Iterator.from(0).takeWhile(_ => rs.next()).map(_ => SqlResult(
rs.getInt(1),
rs.getString(2),
rs.getString(3)
)).toList
}
}
}
val deleted: List[Boolean] = for (i <- result) yield {
val duplicatedRows = MappedUserAuthContext.findAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.time.{ZoneId, ZonedDateTime}
import code.api.util.APIUtil
import code.api.util.migration.Migration.{DbFunction, saveLog}
import code.context.MappedUserAuthContext
import code.util.Helper
import net.liftweb.common.Full
import net.liftweb.mapper.{DB, Schemifier}
import net.liftweb.util.DefaultConnectionIdentifier
Expand All @@ -27,9 +28,14 @@ object MigrationOfUserAuthContextFieldLength {
APIUtil.getPropsValue("db.driver") match {
case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") =>
() =>
"""
s"""
|${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(value, "mappeduserauthcontext", "mappeduserauthcontext_muserid_mkey_createdat")}
|
|""".stripMargin
case _ =>
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class MappedUserAuthContext extends UserAuthContext with LongKeyedMapper[MappedU

object mUserAuthContextId extends MappedUUID(this)
object mUserId extends UUIDString(this)
object mKey extends MappedString(this, 255)
object mValue extends MappedString(this, 255)
object mKey extends MappedString(this, 4000)
object mValue extends MappedString(this, 4000)
object mConsumerId extends MappedString(this, 255)

override def userId = mUserId.get
Expand Down
48 changes: 34 additions & 14 deletions obp-api/src/main/scala/code/util/Helper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -523,20 +523,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 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(",")});"
}


}
Loading

0 comments on commit ffc4e7d

Please sign in to comment.