feat(01-05): add HOCON config, Flyway migration dir, fail-loud Database.migrate
- application.conf: HOCON with ktor.deployment.port (8080 + ${?PORT}) and
database.url/user/password (localhost defaults + ${?DATABASE_URL/USER/PASSWORD})
- db/migration/.gitkeep: placeholder so classpath:db/migration resolves
- Database.kt: object Database.migrate(app) reads HOCON config, runs Flyway
with baselineOnMigrate + validateOnMigrate + cleanDisabled, throws
IllegalStateException on any failure (D-16 fail-loud contract)
- SLF4J (not Kermit); server logs url+user only, never password
This commit is contained in:
31
server/src/main/kotlin/dev/ulfrx/recipe/Database.kt
Normal file
31
server/src/main/kotlin/dev/ulfrx/recipe/Database.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package dev.ulfrx.recipe
|
||||
|
||||
import io.ktor.server.application.Application
|
||||
import org.flywaydb.core.Flyway
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
object Database {
|
||||
private val log = LoggerFactory.getLogger(Database::class.java)
|
||||
|
||||
fun migrate(app: Application) {
|
||||
val url = app.environment.config.property("database.url").getString()
|
||||
val user = app.environment.config.property("database.user").getString()
|
||||
val password = app.environment.config.property("database.password").getString()
|
||||
|
||||
log.info("Connecting to {} as {} and running Flyway migrations", url, user)
|
||||
|
||||
runCatching {
|
||||
Flyway.configure()
|
||||
.dataSource(url, user, password)
|
||||
.locations("classpath:db/migration")
|
||||
.baselineOnMigrate(true)
|
||||
.validateOnMigrate(true)
|
||||
.cleanDisabled(true)
|
||||
.load()
|
||||
.migrate()
|
||||
}.onFailure { ex ->
|
||||
log.error("Flyway migration failed — cannot start server", ex)
|
||||
throw IllegalStateException("Database unreachable or migration failed", ex)
|
||||
}
|
||||
}
|
||||
}
|
||||
18
server/src/main/resources/application.conf
Normal file
18
server/src/main/resources/application.conf
Normal file
@@ -0,0 +1,18 @@
|
||||
ktor {
|
||||
deployment {
|
||||
port = 8080
|
||||
port = ${?PORT}
|
||||
}
|
||||
application {
|
||||
modules = [ dev.ulfrx.recipe.ApplicationKt.module ]
|
||||
}
|
||||
}
|
||||
|
||||
database {
|
||||
url = "jdbc:postgresql://localhost:5432/recipe"
|
||||
url = ${?DATABASE_URL}
|
||||
user = "recipe"
|
||||
user = ${?DATABASE_USER}
|
||||
password = "recipe"
|
||||
password = ${?DATABASE_PASSWORD}
|
||||
}
|
||||
0
server/src/main/resources/db/migration/.gitkeep
Normal file
0
server/src/main/resources/db/migration/.gitkeep
Normal file
Reference in New Issue
Block a user