From 7fe2d28410b077d03a2c15d5b8c87f13d8b649e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Mon, 27 Dec 2021 12:24:08 +0100 Subject: [PATCH] enhancement/Add database unique index on AuthUser table provider + username --- .../code/api/util/migration/Migration.scala | 12 +++++ .../util/migration/MigrationOfAuthUser.scala | 45 +++++++++++++++++++ .../code/model/dataAccess/AuthUser.scala | 5 ++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/obp-api/src/main/scala/code/api/util/migration/Migration.scala b/obp-api/src/main/scala/code/api/util/migration/Migration.scala index 0bc5a7c58..cef50e4de 100644 --- a/obp-api/src/main/scala/code/api/util/migration/Migration.scala +++ b/obp-api/src/main/scala/code/api/util/migration/Migration.scala @@ -88,6 +88,7 @@ object Migration extends MdcLoggable { addFastFirehoseAccountsView(startedBeforeSchemifier) addFastFirehoseAccountsMaterializedView(startedBeforeSchemifier) alterUserAuthContextColumnKeyAndValueLength(startedBeforeSchemifier) + dropIndexAtColumnUsernameAtTableAuthUser(startedBeforeSchemifier) } private def dummyScript(): Boolean = { @@ -332,6 +333,17 @@ object Migration extends MdcLoggable { MigrationOfUserAuthContextFieldLength.alterColumnKeyAndValueLength(name) } } + } + private def dropIndexAtColumnUsernameAtTableAuthUser(startedBeforeSchemifier: Boolean): Boolean = { + if(startedBeforeSchemifier == true) { + logger.warn(s"Migration.database.dropIndexAtColumnUsernameAtTableAuthUser(true) cannot be run before Schemifier.") + true + } else { + val name = nameOf(dropIndexAtColumnUsernameAtTableAuthUser(startedBeforeSchemifier)) + runOnce(name) { + MigrationOfAuthUser.dropIndexAtColumnUsername(name) + } + } } } 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 1e584fba3..eb86fe285 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 @@ -68,5 +68,50 @@ object MigrationOfAuthUser { isSuccessful } } + + def dropIndexAtColumnUsername(name: String): Boolean = { + DbFunction.tableExists(AuthUser, (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 authuser_username; + |""".stripMargin + case _ => + () => + """ + |DROP INDEX IF EXISTS authuser_username; + |""".stripMargin + } + + } + + 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"""${AuthUser._dbTableNameLC} table does not exist""".stripMargin + saveLog(name, commitId, isSuccessful, startDate, endDate, comment) + isSuccessful + } + } } diff --git a/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala b/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala index 81c99f8a8..29e2046fd 100644 --- a/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala +++ b/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala @@ -177,7 +177,8 @@ class AuthUser extends MegaProtoUser[AuthUser] with CreatedUpdated with MdcLogga case _ => List(FieldError(this, Text(msg))) } override def displayName = S.?("Username") - override def dbIndexed_? = true + @deprecated("Use UniqueIndex(username, provider)","27 December 2021") + override def dbIndexed_? = false // We use more general index UniqueIndex(username, provider) :: super.dbIndexes override def validations = isEmpty(Helper.i18n("Please.enter.your.username")) _ :: usernameIsValid(Helper.i18n("invalid.username")) _ :: valUnique(Helper.i18n("unique.username")) _ :: @@ -415,6 +416,8 @@ import net.liftweb.util.Helpers._ val connector = APIUtil.getPropsValue("connector").openOrThrowException("no connector set") val starConnectorSupportedTypes = APIUtil.getPropsValue("starConnector_supported_types","") + override def dbIndexes: List[BaseIndex[AuthUser]] = UniqueIndex(username, provider) ::super.dbIndexes + override def emailFrom = APIUtil.getPropsValue("mail.users.userinfo.sender.address", "sender-not-set") override def screenWrap = Full()