bugfix/tweaked the index for MappedConsentAuthContext table

This commit is contained in:
hongwei 2022-05-16 11:18:54 +02:00
parent 55bb56f1aa
commit d6edcff8b0
4 changed files with 96 additions and 1 deletions

View File

@ -92,6 +92,7 @@ object Migration extends MdcLoggable {
dropIndexAtColumnUsernameAtTableAuthUser(startedBeforeSchemifier)
dropIndexAtUserAuthContext()
alterWebhookColumnUrlLength()
dropConsentAuthContextDropIndex()
}
private def dummyScript(): Boolean = {
@ -369,6 +370,13 @@ object Migration extends MdcLoggable {
MigrationOfWebhookUrlFieldLength.alterColumnUrlLength(name)
}
}
private def dropConsentAuthContextDropIndex(): Boolean = {
val name = nameOf(dropConsentAuthContextDropIndex)
runOnce(name) {
MigrationOfConsentAuthContextDropIndex.dropUniqueIndex(name)
}
}
}

View File

@ -0,0 +1,85 @@
package code.api.util.migration
import code.api.util.APIUtil
import code.api.util.migration.Migration.{DbFunction, saveLog}
import code.context.MappedConsentAuthContext
import net.liftweb.common.Full
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}
object MigrationOfConsentAuthContextDropIndex {
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'")
private lazy val getDbConnectionParameters: (String, String, String) = {
val dbUrl = APIUtil.getPropsValue("db.url") openOr "jdbc:h2:mem:OBPTest;DB_CLOSE_DELAY=-1"
val username = dbUrl.split(";").filter(_.contains("user")).toList.headOption.map(_.split("=")(1))
val password = dbUrl.split(";").filter(_.contains("password")).toList.headOption.map(_.split("=")(1))
val dbUser = APIUtil.getPropsValue("db.user").orElse(username)
val dbPassword = APIUtil.getPropsValue("db.password").orElse(password)
(dbUrl, dbUser.getOrElse(""), dbPassword.getOrElse(""))
}
/**
* 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) = 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, (DB.use(DefaultConnectionIdentifier){ conn => conn})) match {
case true =>
val startDate = System.currentTimeMillis()
val commitId: String = APIUtil.gitCommit
var isSuccessful = false
val executedSql =
DbFunction.maybeWrite(true, Schemifier.infoF _, DB.use(DefaultConnectionIdentifier){ conn => conn}) {
APIUtil.getPropsValue("db.driver") match {
case Full(value) if value.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") =>
() => "DROP INDEX IF EXISTS consentauthcontext_consentid_key_c;"
case _ =>
() => "DROP INDEX IF EXISTS consentauthcontext_consentid_key_c;"
}
}
val endDate = System.currentTimeMillis()
val comment: String =
s"""Executed SQL:
|$executedSql
|""".stripMargin
isSuccessful = true
saveLog(name, commitId, isSuccessful, startDate, endDate, comment)
isSuccessful
case false =>
val startDate = System.currentTimeMillis()
val commitId: String = APIUtil.gitCommit
val isSuccessful = false
val endDate = System.currentTimeMillis()
val comment: String =
s"""${MappedConsentAuthContext._dbTableNameLC} table does not exist""".stripMargin
saveLog(name, commitId, isSuccessful, startDate, endDate, comment)
isSuccessful
}
}
}

View File

@ -17,9 +17,10 @@ class MappedConsentAuthContext extends ConsentAuthContext with LongKeyedMapper[M
override def key = Key.get
override def value = Value.get
override def consentAuthContextId = ConsentAuthContextId.get
override def timeStamp = createdAt.get
}
object MappedConsentAuthContext extends MappedConsentAuthContext with LongKeyedMetaMapper[MappedConsentAuthContext] {
override def dbTableName = "ConsentAuthContext" // define a custom DB table name
override def dbIndexes = UniqueIndex(ConsentId, Key) :: super.dbIndexes
override def dbIndexes = UniqueIndex(ConsentId, Key, createdAt) :: super.dbIndexes
}

View File

@ -316,6 +316,7 @@ trait ConsentAuthContext {
def consentId : String
def key : String
def value : String
def timeStamp : Date
}