123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package co.id.datacomsolusindo.flyway
- import org.flywaydb.core.Flyway
- import org.springframework.boot.autoconfigure.SpringBootApplication
- import org.yaml.snakeyaml.Yaml
- import java.io.File
- import java.io.FileInputStream
- import org.ini4j.Wini
- import org.slf4j.LoggerFactory
- import org.springframework.boot.builder.SpringApplicationBuilder
- import kotlin.system.exitProcess
- @SpringBootApplication
- class FlywayApplication
- fun main(args: Array<String>) {
- SpringApplicationBuilder(FlywayApplication::class.java)
- .banner { _, _, out ->
- val banner: String = " --------------------------------\n" +
- " ____ _______ ____ \n" +
- " ] \\ | / \\ \n" +
- " ] | | | \n" +
- " ] | | | \n" +
- " ]___/ | \\____/ \n" +
- " Datacom Solusindo 2022 \n" +
- " \n" +
- " Flyway Migration Database\n" +
- " --------------------------------\n"
- out.print(banner)
- }.run(*args)
- val config = U.flywayConfiguration
- val address = if (config.dbType == "sqlserver")
- "jdbc:sqlserver://${config.dbIp};databaseName=${config.dbName};integratedSecurity=false;TrustServerCertificate=true"
- else
- "jdbc:mysql://${config.dbIp}:${config.dbPort}/${config.dbName}?createDatabaseIfNotExist=true"
- println("address: $address")
- val flyway: Flyway = Flyway.configure()
- .dataSource(address, config.dbUsername, config.dbPassword)
- .table(config.name)
- // .locations("filesystem:./../db")
- .locations("filesystem:./db")
- .load()
- flyway.repair()
- flyway.migrate()
- exitProcess(1)
- }
- object U {
- private fun loadConfigYml(path: String = "conf/flyway-setting.yml"): Map<String, Any>? {
- val f = File(path)
- return if (f.exists()) {
- val yaml = Yaml()
- yaml.load(FileInputStream(f))
- } else null
- }
- val flywayConfiguration: FlywayConfiguration by lazy {
- val fileGeneralIni = File("conf/general.ini")
- val fileGeneralIniSubFolder = File("../conf/general.ini")
- val fileGen = when {
- fileGeneralIni.exists() -> fileGeneralIni
- fileGeneralIniSubFolder.exists() -> fileGeneralIniSubFolder
- else -> null
- }
- if (fileGen != null) {
- LoggerFactory.getLogger(this::class.java).info("read configuration file general.ini [${fileGen.path}]")
- // read general ini
- val ini = Wini(fileGen)
- FlywayConfiguration(
- port = ini.get("server", "port") ?: "9999",
- name = ini.get("flyway", "name") ?: "dtc_schema_history",
- dbIp = ini.get("database", "host") ?: "127.0.0.1",
- dbPort = ini.get("database", "port") ?: "1433",
- dbName = ini.get("database", "name") ?: "dbname",
- dbUsername = ini.get("database", "username") ?: "admin",
- dbPassword = ini.get("database", "password") ?: "admin",
- dbDriverClass = ini.get("flyway", "driver") ?: "com.microsoft.sqlserver.jdbc.SQLServerDriver",
- dbType = ini.get("flyway", "db") ?: "mysql"
- )
- } else {
- // read flyway-setting.yml
- val se = loadConfigYml()
- val seDb = se!!["database"] as MutableMap<*, *>
- FlywayConfiguration(
- port = se["port"]?.toString() ?: "9999",
- name = seDb["name"]?.toString() ?: "dtc_schema_history",
- dbIp = seDb["name"]?.toString() ?: "127.0.0.1",
- dbPort = seDb["port"]?.toString() ?: "1433",
- dbName = seDb["name"]?.toString() ?: "dbname",
- dbUsername = seDb["username"]?.toString() ?: "admin",
- dbPassword = seDb["password"]?.toString() ?: "admin",
- dbDriverClass = seDb["driver-class"]?.toString() ?: "com.microsoft.sqlserver.jdbc.SQLServerDriver",
- dbType = seDb["type"]?.toString() ?: "mysql"
- )
- }
- }
- }
- class FlywayConfiguration(
- val port: String, val name: String, val dbIp: String, val dbPort: String,
- val dbName: String, val dbUsername: String, val dbPassword: String,
- val dbDriverClass: String, val dbType: String = "mysql"
- )
|