FlywayApplication.kt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package co.id.datacomsolusindo.flyway
  2. import org.flywaydb.core.Flyway
  3. import org.springframework.boot.autoconfigure.SpringBootApplication
  4. import org.yaml.snakeyaml.Yaml
  5. import java.io.File
  6. import java.io.FileInputStream
  7. import org.ini4j.Wini
  8. import org.slf4j.LoggerFactory
  9. import org.springframework.boot.builder.SpringApplicationBuilder
  10. import kotlin.system.exitProcess
  11. @SpringBootApplication
  12. class FlywayApplication
  13. fun main(args: Array<String>) {
  14. SpringApplicationBuilder(FlywayApplication::class.java)
  15. .banner { _, _, out ->
  16. val banner: String = " --------------------------------\n" +
  17. " ____ _______ ____ \n" +
  18. " ] \\ | / \\ \n" +
  19. " ] | | | \n" +
  20. " ] | | | \n" +
  21. " ]___/ | \\____/ \n" +
  22. " Datacom Solusindo 2022 \n" +
  23. " \n" +
  24. " Flyway Migration Database\n" +
  25. " --------------------------------\n"
  26. out.print(banner)
  27. }.run(*args)
  28. val config = U.flywayConfiguration
  29. val address = if (config.dbType == "sqlserver")
  30. "jdbc:sqlserver://${config.dbIp};databaseName=${config.dbName};integratedSecurity=false;TrustServerCertificate=true"
  31. else
  32. "jdbc:mysql://${config.dbIp}:${config.dbPort}/${config.dbName}?createDatabaseIfNotExist=true"
  33. println("address: $address")
  34. val flyway: Flyway = Flyway.configure()
  35. .dataSource(address, config.dbUsername, config.dbPassword)
  36. .table(config.name)
  37. // .locations("filesystem:./../db")
  38. .locations("filesystem:./db")
  39. .load()
  40. flyway.repair()
  41. flyway.migrate()
  42. exitProcess(1)
  43. }
  44. object U {
  45. private fun loadConfigYml(path: String = "conf/flyway-setting.yml"): Map<String, Any>? {
  46. val f = File(path)
  47. return if (f.exists()) {
  48. val yaml = Yaml()
  49. yaml.load(FileInputStream(f))
  50. } else null
  51. }
  52. val flywayConfiguration: FlywayConfiguration by lazy {
  53. val fileGeneralIni = File("conf/general.ini")
  54. val fileGeneralIniSubFolder = File("../conf/general.ini")
  55. val fileGen = when {
  56. fileGeneralIni.exists() -> fileGeneralIni
  57. fileGeneralIniSubFolder.exists() -> fileGeneralIniSubFolder
  58. else -> null
  59. }
  60. if (fileGen != null) {
  61. LoggerFactory.getLogger(this::class.java).info("read configuration file general.ini [${fileGen.path}]")
  62. // read general ini
  63. val ini = Wini(fileGen)
  64. FlywayConfiguration(
  65. port = ini.get("server", "port") ?: "9999",
  66. name = ini.get("flyway", "name") ?: "dtc_schema_history",
  67. dbIp = ini.get("database", "host") ?: "127.0.0.1",
  68. dbPort = ini.get("database", "port") ?: "1433",
  69. dbName = ini.get("database", "name") ?: "dbname",
  70. dbUsername = ini.get("database", "username") ?: "admin",
  71. dbPassword = ini.get("database", "password") ?: "admin",
  72. dbDriverClass = ini.get("flyway", "driver") ?: "com.microsoft.sqlserver.jdbc.SQLServerDriver",
  73. dbType = ini.get("flyway", "db") ?: "mysql"
  74. )
  75. } else {
  76. // read flyway-setting.yml
  77. val se = loadConfigYml()
  78. val seDb = se!!["database"] as MutableMap<*, *>
  79. FlywayConfiguration(
  80. port = se["port"]?.toString() ?: "9999",
  81. name = seDb["name"]?.toString() ?: "dtc_schema_history",
  82. dbIp = seDb["name"]?.toString() ?: "127.0.0.1",
  83. dbPort = seDb["port"]?.toString() ?: "1433",
  84. dbName = seDb["name"]?.toString() ?: "dbname",
  85. dbUsername = seDb["username"]?.toString() ?: "admin",
  86. dbPassword = seDb["password"]?.toString() ?: "admin",
  87. dbDriverClass = seDb["driver-class"]?.toString() ?: "com.microsoft.sqlserver.jdbc.SQLServerDriver",
  88. dbType = seDb["type"]?.toString() ?: "mysql"
  89. )
  90. }
  91. }
  92. }
  93. class FlywayConfiguration(
  94. val port: String, val name: String, val dbIp: String, val dbPort: String,
  95. val dbName: String, val dbUsername: String, val dbPassword: String,
  96. val dbDriverClass: String, val dbType: String = "mysql"
  97. )