Skip to content

Commit

Permalink
refactor/revert the migration function back - step6
Browse files Browse the repository at this point in the history
  • Loading branch information
hongwei1 committed May 10, 2024
1 parent 8280297 commit a7ed850
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 63 deletions.
2 changes: 1 addition & 1 deletion obp-api/src/main/scala/bootstrap/liftweb/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class Boot extends MdcLoggable {

schemifyAll()

logger.info("Mapper database info: " + Migration.DbFunction.mapperDatabaseInfo(APIUtil.vendor))
logger.info("Mapper database info: " + Migration.DbFunction.mapperDatabaseInfo)

DbFunction.tableExists(ResourceUser) match {
case true => // DB already exist
Expand Down
108 changes: 47 additions & 61 deletions obp-api/src/main/scala/code/api/util/migration/Migration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.sql.{Connection, ResultSet, SQLException}
import java.text.SimpleDateFormat
import java.util.Date
import code.api.util.APIUtil.{getPropsAsBoolValue, getPropsValue}
import code.api.util.ErrorMessages.DatabaseConnectionClosedError
import code.api.util.{APIUtil, ApiPropsWithAlias}
import code.api.v4_0_0.DatabaseInfoJson
import code.consumer.Consumers
Expand Down Expand Up @@ -494,67 +493,56 @@ object Migration extends MdcLoggable {
* For instance migration scripts needs to differentiate update of an instance from build a new one from scratch.
* note: 07.05.2024 now. we get the connection from HikariDatasource.ds instead of Liftweb.
*/
def tableExists (table: BaseMetaMapper, connection: Connection = APIUtil.vendor.newConnection(DefaultConnectionIdentifier).head, actualTableNames: HashMap[String, String] = new HashMap[String, String]()): Boolean = {
val md = connection.getMetaData
val schema = connection.getSchema
try {
connection.asInstanceOf[ProxyConnection].close()
} catch {
case t: Throwable => logger.error(s"tableExists close connection, detail is: $t")
}
using(md.getTables(null, schema, null, null)){ rs =>
def hasTable(rs: ResultSet): Boolean =
if (!rs.next) false
else rs.getString(3) match {
case s if s.toLowerCase == table._dbTableNameLC.toLowerCase => actualTableNames(table._dbTableNameLC) = s; true
case _ => hasTable(rs)
def tableExists (table: BaseMetaMapper, actualTableNames: HashMap[String, String] = new HashMap[String, String]()): Boolean = {
DB.use(net.liftweb.util.DefaultConnectionIdentifier) {
conn =>
val md = conn.getMetaData
val schema = getDefaultSchemaName(conn)

using(md.getTables(null, schema, null, null)){ rs =>
def hasTable(rs: ResultSet): Boolean =
if (!rs.next) false
else rs.getString(3) match {
case s if s.toLowerCase == table._dbTableNameLC.toLowerCase => actualTableNames(table._dbTableNameLC) = s; true
case _ => hasTable(rs)
}

hasTable(rs)
}

hasTable(rs)
}
}
/**
* The purpose is to provide answer does a procedure exist at a database instance.
*/
def procedureExists(name: String, connection: Connection = APIUtil.vendor.newConnection(DefaultConnectionIdentifier).head): Boolean = {
val md = connection.getMetaData
val schema = connection.getSchema
try {
connection.asInstanceOf[ProxyConnection].close()
} catch {
case t: Throwable => logger.error(s"procedureExists close connection, detail is: $t")
}

using(md.getProcedures(null, schema, null)){ rs =>
def hasProcedure(rs: ResultSet): Boolean =
if (!rs.next) false
else rs.getString(3) match {
case s if s.toLowerCase == name => true
case _ => hasProcedure(rs)
def procedureExists(name: String): Boolean = {
DB.use(net.liftweb.util.DefaultConnectionIdentifier) {
conn =>
val md = conn.getMetaData
val schema = getDefaultSchemaName(conn)
using(md.getProcedures(null, schema, null)){ rs =>
def hasProcedure(rs: ResultSet): Boolean =
if (!rs.next) false
else rs.getString(3) match {
case s if s.toLowerCase == name => true
case _ => hasProcedure(rs)
}
hasProcedure(rs)
}
hasProcedure(rs)
}
}


/**
* The purpose is to provide info about the database in mapper mode.
*/
def mapperDatabaseInfo(vendor: CustomDBVendor): DatabaseInfoJson = {
val connection = vendor.createOne.openOrThrowException(DatabaseConnectionClosedError)
try {
val md = connection.getMetaData
val productName = md.getDatabaseProductName()
val productVersion = md.getDatabaseProductVersion()
DatabaseInfoJson(product_name = productName, product_version = productVersion)
} finally {
try {
connection.asInstanceOf[ProxyConnection].close()
} catch {
case t: Throwable => logger.error(s"mapperDatabaseInfo.close connection throw exception, detail is: $t")
}
def mapperDatabaseInfo: DatabaseInfoJson = {
DB.use(net.liftweb.util.DefaultConnectionIdentifier) {
conn =>
val md = conn.getMetaData
val productName = md.getDatabaseProductName()
val productVersion = md.getDatabaseProductVersion()
DatabaseInfoJson(product_name = productName, product_version = productVersion)
}

}

/**
Expand All @@ -569,21 +557,19 @@ object Migration extends MdcLoggable {
*
* @return SQL command.
*/
def maybeWrite(performWrite: Boolean, logFunc: (=> AnyRef) => Unit, connection: Connection = APIUtil.vendor.newConnection(DefaultConnectionIdentifier).head) (makeSql: () => String) : String ={
val ct = makeSql()
logger.trace("maybeWrite DDL: "+ct)
if (performWrite) {
logFunc(ct)
val st = connection.createStatement
st.execute(ct)
st.close
}
try {
connection.asInstanceOf[ProxyConnection].close()
} catch {
case t: Throwable => logger.error(s"maybeWrite close connection, detail is: $t")
def maybeWrite(performWrite: Boolean, logFunc: (=> AnyRef) => Unit) (makeSql: () => String) : String ={
DB.use(net.liftweb.util.DefaultConnectionIdentifier) {
conn =>
val ct = makeSql()
logger.trace("maybeWrite DDL: " + ct)
if (performWrite) {
logFunc(ct)
val st = conn.createStatement
st.execute(ct)
st.close
}
ct
}
ct
}

/**
Expand Down
2 changes: 1 addition & 1 deletion obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ trait APIMethods400 extends MdcLoggable {
cc =>
implicit val ec = EndpointContext(Some(cc))
Future {
(Migration.DbFunction.mapperDatabaseInfo(APIUtil.vendor), HttpCode.`200`(cc.callContext))
(Migration.DbFunction.mapperDatabaseInfo, HttpCode.`200`(cc.callContext))
}
}
}
Expand Down

0 comments on commit a7ed850

Please sign in to comment.