From eed03ffab630181bab1738cd1581b5b18e9d093f Mon Sep 17 00:00:00 2001 From: hongwei Date: Mon, 22 May 2023 17:42:54 +0800 Subject: [PATCH] refactor/changed the length of UserAttribute.name field --- .../code/api/util/migration/Migration.scala | 8 +++ ...rationOfUserAttributeNameFieldLength.scala | 62 +++++++++++++++++++ .../code/users/MappedUserAttribute.scala | 2 +- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAttributeNameFieldLength.scala 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 1b7efd691..1038cd97c 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 @@ -95,6 +95,7 @@ object Migration extends MdcLoggable { dropConsentAuthContextDropIndex() alterMappedExpectedChallengeAnswerChallengeTypeLength() alterTransactionRequestChallengeChallengeTypeLength() + alterUserAttributeNameLength() alterMappedCustomerAttribute(startedBeforeSchemifier) dropMappedBadLoginAttemptIndex() } @@ -421,6 +422,13 @@ object Migration extends MdcLoggable { MigrationOfTransactionRequestChallengeChallengeTypeLength.alterColumnChallengeChallengeTypeLength(name) } } + + private def alterUserAttributeNameLength(): Boolean = { + val name = nameOf(alterUserAttributeNameLength) + runOnce(name) { + MigrationOfUserAttributeNameFieldLength.alterNameLength(name) + } + } private def alterMappedCustomerAttribute(startedBeforeSchemifier: Boolean): Boolean = { if(startedBeforeSchemifier == true) { logger.warn(s"Migration.database.alterMappedCustomerAttribute(true) cannot be run before Schemifier.") diff --git a/obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAttributeNameFieldLength.scala b/obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAttributeNameFieldLength.scala new file mode 100644 index 000000000..7a8f0e3bc --- /dev/null +++ b/obp-api/src/main/scala/code/api/util/migration/MigrationOfUserAttributeNameFieldLength.scala @@ -0,0 +1,62 @@ +package code.api.util.migration + +import code.api.util.APIUtil +import code.api.util.migration.Migration.{DbFunction, saveLog} +import code.users.UserAttribute +import net.liftweb.common.Full +import net.liftweb.mapper.{DB, Schemifier} +import net.liftweb.util.DefaultConnectionIdentifier + +import java.time.format.DateTimeFormatter +import java.time.{ZoneId, ZonedDateTime} + +object MigrationOfUserAttributeNameFieldLength { + + 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'") + + def alterNameLength(name: String): Boolean = { + DbFunction.tableExists(UserAttribute, (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") => + () => + """ + |ALTER TABLE UserAttribute ALTER COLUMN name varchar(255); + |""".stripMargin + case _ => + () => + """ + |ALTER TABLE UserAttribute ALTER COLUMN name type varchar(255); + |""".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"""${UserAttribute._dbTableNameLC} table does not exist""".stripMargin + saveLog(name, commitId, isSuccessful, startDate, endDate, comment) + isSuccessful + } + } +} \ No newline at end of file diff --git a/obp-api/src/main/scala/code/users/MappedUserAttribute.scala b/obp-api/src/main/scala/code/users/MappedUserAttribute.scala index 6b281c10d..427fd4d29 100644 --- a/obp-api/src/main/scala/code/users/MappedUserAttribute.scala +++ b/obp-api/src/main/scala/code/users/MappedUserAttribute.scala @@ -65,7 +65,7 @@ class UserAttribute extends UserAttributeTrait with LongKeyedMapper[UserAttribut override def getSingleton = UserAttribute object UserAttributeId extends MappedUUID(this) object UserId extends MappedUUID(this) - object Name extends MappedString(this, 50) + object Name extends MappedString(this, 255) object Type extends MappedString(this, 50) object `Value` extends MappedString(this, 255)