-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
113 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
apps/migrations/src/main/kotlin/com/agilogy/timetracking/migrations/KotlinMigration.kt
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
apps/migrations/src/main/kotlin/com/agilogy/timetracking/migrations/MigrateDb.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM openjdk:17.0.1-jdk-slim | ||
|
||
COPY build/libs/migrationsapp-1.0-SNAPSHOT.jar migrationsapp.jar | ||
|
||
CMD [ "java", "-jar" , "migrationsapp.jar"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
apps/migrationsapp/src/main/kotlin/com/agilogy/timetracking/migrationsapp/MigrateDb.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.agilogy.timetracking.migrationsapp | ||
|
||
import arrow.continuations.SuspendApp | ||
import com.agilogy.db.hikari.HikariCp | ||
import com.agilogy.heroku.postgres.loadHerokuPostgresConfig | ||
import com.agilogy.timetracking.migrations.runMigrations | ||
|
||
fun main() = SuspendApp { | ||
val (jdbcUrl, username, password) = loadHerokuPostgresConfig() | ||
val dataSource = HikariCp.dataSource(jdbcUrl, username, password) | ||
dataSource.use { runMigrations(it) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Dependencies.flywayCore | ||
import Dependencies.hikariCp | ||
import Dependencies.kotestRunnerJunit | ||
import Dependencies.postgresql | ||
import Dependencies.slf4jProvider | ||
|
||
dependencies { | ||
implementation(project(":domain")) | ||
implementation(project(":db")) | ||
api(flywayCore) | ||
implementation(slf4jProvider) | ||
|
||
testImplementation(hikariCp) | ||
testImplementation(postgresql) | ||
testImplementation(kotestRunnerJunit) | ||
testImplementation(testFixtures(project(":domain"))) | ||
|
||
testFixturesImplementation(project(":domain")) | ||
} |
67 changes: 67 additions & 0 deletions
67
components/migrations/src/main/kotlin/com/agilogy/timetracking/migrations/KotlinMigration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.agilogy.timetracking.migrations | ||
|
||
import com.agilogy.timetracking.migrations.scripts.Migration202306231200 | ||
import kotlinx.coroutines.runBlocking | ||
import org.flywaydb.core.Flyway | ||
import org.flywaydb.core.api.MigrationVersion | ||
import org.flywaydb.core.api.configuration.FluentConfiguration | ||
import org.flywaydb.core.api.migration.Context | ||
import org.flywaydb.core.api.migration.JavaMigration | ||
import org.slf4j.LoggerFactory | ||
import java.sql.Connection | ||
import javax.sql.DataSource | ||
|
||
abstract class KotlinMigration(private val version: String, private val title: String) : JavaMigration { | ||
|
||
override fun getVersion(): MigrationVersion = MigrationVersion.fromVersion(version) | ||
|
||
override fun getDescription(): String = title | ||
|
||
override fun getChecksum(): Int? = null | ||
|
||
override fun canExecuteInTransaction(): Boolean = true | ||
|
||
final override fun migrate(context: Context) = runBlocking { | ||
with(context.connection!!) { | ||
migrate() | ||
} | ||
} | ||
|
||
context(Connection) | ||
abstract suspend fun migrate() | ||
} | ||
|
||
fun runMigrations(dataSource: DataSource, clean: Boolean = false) { | ||
val flywayConfig: FluentConfiguration = Flyway.configure() | ||
.dataSource(dataSource) | ||
.group(true) | ||
.outOfOrder(false) | ||
.baselineOnMigrate(true) | ||
.locations("classpath:${Migration202306231200::class.java.packageName.replace('.', '/')}") | ||
.loggers("slf4j") | ||
|
||
val validated = flywayConfig | ||
.ignoreMigrationPatterns("*:pending") | ||
.load() | ||
.validateWithResult() | ||
|
||
if (!validated.validationSuccessful) { | ||
val logger = LoggerFactory.getLogger("RunMigrations") | ||
for (error in validated.invalidMigrations) { | ||
logger.warn( | ||
""" | ||
|Failed to validate migration: | ||
| - version: ${error.version} | ||
| - path: ${error.filepath} | ||
| - description: ${error.description} | ||
| - error code: ${error.errorDetails.errorCode} | ||
| - error message: ${error.errorDetails.errorMessage} | ||
""".trimMargin("|").trim(), | ||
) | ||
} | ||
} | ||
if (clean) { | ||
flywayConfig.cleanDisabled(false).load().clean() | ||
} | ||
println(flywayConfig.load().migrate()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters