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 6309a8be4..021261e2c 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 @@ -102,6 +102,7 @@ object Migration extends MdcLoggable { dropMappedBadLoginAttemptIndex() alterMetricColumnUrlLength() populateViewDefinitionCanAddTransactionRequestToBeneficiary() + alterCounterpartyLimitFieldType() } private def dummyScript(): Boolean = { @@ -478,6 +479,13 @@ object Migration extends MdcLoggable { MigrationOfMappedBadLoginAttemptDropIndex.dropUniqueIndex(name) } } + + private def alterCounterpartyLimitFieldType(): Boolean = { + val name = nameOf(alterCounterpartyLimitFieldType) + runOnce(name) { + MigrationOfCounterpartyLimitFieldType.alterCounterpartyLimitFieldType(name) + } + } } /** diff --git a/obp-api/src/main/scala/code/api/util/migration/MigrationOfCounterpartyLimitFieldType.scala b/obp-api/src/main/scala/code/api/util/migration/MigrationOfCounterpartyLimitFieldType.scala new file mode 100644 index 000000000..e42b4e6e6 --- /dev/null +++ b/obp-api/src/main/scala/code/api/util/migration/MigrationOfCounterpartyLimitFieldType.scala @@ -0,0 +1,73 @@ +package code.api.util.migration + +import code.api.util.APIUtil +import code.api.util.migration.Migration.{DbFunction, saveLog} +import code.counterpartylimit.CounterpartyLimit +import net.liftweb.common.Full +import net.liftweb.mapper.Schemifier + +import java.time.format.DateTimeFormatter +import java.time.{ZoneId, ZonedDateTime} + +object MigrationOfCounterpartyLimitFieldType { + + 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 alterCounterpartyLimitFieldType(name: String): Boolean = { + DbFunction.tableExists(CounterpartyLimit) + match { + case true => + val startDate = System.currentTimeMillis() + val commitId: String = APIUtil.gitCommit + var isSuccessful = false + + val executedSql = + DbFunction.maybeWrite(true, Schemifier.infoF _) { + APIUtil.getPropsValue("db.driver") match { + case Full(dbDriver) if dbDriver.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => + () => + """ + |ALTER TABLE counterpartylimit + |ALTER COLUMN maxsingleamount numeric(16, 10); + | + |ALTER TABLE counterpartylimit + |ALTER COLUMN maxmonthlyamount numeric(16, 10); + | + |ALTER TABLE counterpartylimit + |ALTER COLUMN maxyearlyamount numeric(16, 10); + |""".stripMargin + case _ => + () => + """ + |alter table counterpartylimit + | alter column maxsingleamount type numeric(16, 10) using maxsingleamount::numeric(16, 10); + |alter table counterpartylimit + | alter column maxmonthlyamount type numeric(16, 10) using maxmonthlyamount::numeric(16, 10); + |alter table counterpartylimit + | alter column maxyearlyamount type numeric(16, 10) using maxyearlyamount::numeric(16, 10); + |""".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"""${CounterpartyLimit._dbTableNameLC} table does not exist""".stripMargin + saveLog(name, commitId, isSuccessful, startDate, endDate, comment) + isSuccessful + } + } +} \ No newline at end of file