enhancement/Add database unique index on AuthUser table provider + username

This commit is contained in:
Marko Milić 2021-12-27 12:24:08 +01:00
parent 3116d9712a
commit 7fe2d28410
3 changed files with 61 additions and 1 deletions

View File

@ -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)
}
}
}
}

View File

@ -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
}
}
}

View File

@ -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(<lift:surround with="default" at="content"><lift:bind /></lift:surround>)