Skip to content

Commit

Permalink
Run migrations on start
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Feb 6, 2024
1 parent 6af00a5 commit 2dad462
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tgkotbot-dataops/src/main/kotlin/RunMigrations.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@file:JvmName("RunMigrations")

import org.flywaydb.core.Flyway
import javax.sql.DataSource

fun main() {
val env = System.getenv()
Expand All @@ -23,3 +24,15 @@ fun main() {
.load()
.migrate()
}

fun runMigrations(
dataSource: DataSource,
) {
Flyway
.configure()
.locations("classpath:migrations")
.dataSource(dataSource)
.loggers("slf4j")
.load()
.migrate()
}
1 change: 1 addition & 0 deletions tgkotbot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tasks.distZip {
dependencies {
implementation(projects.core)
implementation(projects.tgkotbotDatabase)
implementation(projects.tgkotbotDataops)

implementation(libs.kotlin.stdlib)
implementation(libs.kotlin.coroutines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.jooq.DSLContext
import org.jooq.SQLDialect
import org.jooq.impl.DSL
import org.postgresql.ds.PGSimpleDataSource
import runMigrations
import java.lang.management.ManagementFactory
import kotlin.concurrent.thread

Expand Down Expand Up @@ -212,6 +213,7 @@ open class ApplicationFactory {
}

open suspend fun start() {
runMigrations(hikariDataSource)
Runtime.getRuntime().addShutdownHook(thread(start = false) {
log.info("Shutdown hook called.")
applicationScope.cancel("Shutdown hook called.")
Expand Down

0 comments on commit 2dad462

Please sign in to comment.