-
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
17 changed files
with
190 additions
and
27 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 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 |
---|---|---|
@@ -1,8 +1,5 @@ | ||
FROM openjdk:17.0.1-jdk-slim | ||
|
||
RUN echo pwd | ||
|
||
COPY build/libs/app-1.0-SNAPSHOT.jar app.jar | ||
|
||
|
||
CMD [ "java", "-jar" , "app.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
7 changes: 2 additions & 5 deletions
7
...com/agilogy/timetracking/app/ApiServer.kt → ...com/agilogy/timetracking/app/ApiServer.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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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/migrations-1.0-SNAPSHOT.jar migrations.jar | ||
|
||
CMD [ "java", "-jar" , "migrations.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Dependencies.flywayCore | ||
import Dependencies.kotestRunnerJunit | ||
import Dependencies.postgresql | ||
import Dependencies.slf4jProvider | ||
import Dependencies.suspendApp | ||
|
||
plugins { | ||
application | ||
id("java") | ||
} | ||
|
||
val mainClassCompleteName = "com.agilogy.timetracking.migrations.MigrateDbKt" | ||
|
||
tasks.withType<Jar> { | ||
manifest { | ||
attributes["Main-Class"] = mainClassCompleteName | ||
} | ||
// To avoid the duplicate handling strategy error | ||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE | ||
|
||
// To add all of the dependencies otherwise a "NoClassDefFoundError" error | ||
from(sourceSets.main.get().output) | ||
|
||
dependsOn(configurations.runtimeClasspath) | ||
from({ | ||
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) } | ||
}) | ||
} | ||
application { | ||
mainClass.set(mainClassCompleteName) | ||
} | ||
|
||
dependencies { | ||
implementation(slf4jProvider) | ||
implementation(postgresql) | ||
implementation(flywayCore) | ||
implementation(project(":db")) | ||
implementation(project(":postgresdb")) | ||
implementation(project(":herokupostgres")) | ||
implementation(suspendApp) | ||
testImplementation(kotestRunnerJunit) | ||
} |
29 changes: 29 additions & 0 deletions
29
apps/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,29 @@ | ||
package com.agilogy.timetracking.migrations | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import org.flywaydb.core.api.MigrationVersion | ||
import org.flywaydb.core.api.migration.Context | ||
import org.flywaydb.core.api.migration.JavaMigration | ||
import java.sql.Connection | ||
|
||
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 { | ||
context.connection!!.use { | ||
with(it) { | ||
migrate() | ||
} | ||
} | ||
} | ||
|
||
context(Connection) | ||
abstract suspend fun migrate() | ||
} |
41 changes: 41 additions & 0 deletions
41
apps/migrations/src/main/kotlin/com/agilogy/timetracking/migrations/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,41 @@ | ||
package com.agilogy.timetracking.migrations | ||
|
||
import arrow.continuations.SuspendApp | ||
import com.agilogy.heroku.postgres.loadHerokuPostgresConfig | ||
import org.flywaydb.core.Flyway | ||
import org.flywaydb.core.api.configuration.FluentConfiguration | ||
import org.slf4j.LoggerFactory | ||
|
||
fun main() = SuspendApp { | ||
val (jdbcUrl, username, password) = loadHerokuPostgresConfig() | ||
|
||
val flywayConfig: FluentConfiguration = Flyway.configure() | ||
.dataSource(jdbcUrl, username, password) | ||
.group(true) | ||
.outOfOrder(false) | ||
.baselineOnMigrate(true) | ||
.locations("classpath:${this.javaClass.packageName}.scripts") | ||
.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(), | ||
) | ||
} | ||
} | ||
flywayConfig.load().migrate() | ||
} |
24 changes: 24 additions & 0 deletions
24
...grations/src/main/kotlin/com/agilogy/timetracking/migrations/scripts/Migration20230623.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,24 @@ | ||
package com.agilogy.timetracking.migrations.scripts | ||
|
||
import com.agilogy.db.sql.Sql.update | ||
import com.agilogy.timetracking.migrations.KotlinMigration | ||
import java.sql.Connection | ||
|
||
class Migration20230623 : KotlinMigration("20230623", "Initial migration") { | ||
|
||
context(Connection) | ||
override suspend fun migrate() { | ||
update( | ||
""" | ||
|create table if not exists time_entries( | ||
|id serial, | ||
|developer text not null, | ||
|project text not null, | ||
|start timestamptz not null, | ||
|"end" timestamptz not null, | ||
|zone_id text not null | ||
|) | ||
""", | ||
) | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
libs/herokupostgres/src/main/kotlin/com/agilogy/heroku/postgres/HerokuPostgresConfig.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,19 @@ | ||
package com.agilogy.heroku.postgres | ||
|
||
import java.net.URI | ||
|
||
data class HerokuPostgresConfig( | ||
val jdbcUrl: String, | ||
val username: String, | ||
val password: String, | ||
) { | ||
override fun toString(): String = | ||
"HerokuPostgresConfig($jdbcUrl,$username,\"********\")" | ||
} | ||
|
||
fun loadHerokuPostgresConfig(): HerokuPostgresConfig { | ||
val dbUri = URI(System.getenv("DATABASE_URL")) | ||
val (username, password) = dbUri.userInfo.split(":") | ||
val jdbcUrl = "jdbc:postgresql://" + dbUri.host + ':' + dbUri.port + dbUri.path | ||
return HerokuPostgresConfig(jdbcUrl, username, password) | ||
} |
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