Add flyway config

This commit is contained in:
Mikołaj Pich 2023-09-24 20:03:09 +02:00
parent b4760755fd
commit fff27cf91b
7 changed files with 41 additions and 8 deletions

View file

@ -48,7 +48,8 @@ dependencies {
implementation("org.jetbrains.exposed:exposed-dao:$exposed_version") implementation("org.jetbrains.exposed:exposed-dao:$exposed_version")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version") implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version") implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version")
implementation("com.impossibl.pgjdbc-ng:pgjdbc-ng:0.8.9") implementation("org.postgresql:postgresql:42.6.0")
implementation("org.flywaydb:flyway-core:9.22.2")
implementation("io.ktor:ktor-server-auth-jvm:2.3.4") implementation("io.ktor:ktor-server-auth-jvm:2.3.4")
testImplementation("io.ktor:ktor-server-tests-jvm") testImplementation("io.ktor:ktor-server-tests-jvm")

View file

@ -13,7 +13,7 @@ services:
- db - db
db: db:
image: postgres:16-alpine image: postgres:15-alpine
restart: always restart: always
ports: ports:
- "5432:5432" - "5432:5432"

View file

@ -2,6 +2,7 @@ package io.github.wulkanowy.schools
import io.github.wulkanowy.schools.model.LoginEvents import io.github.wulkanowy.schools.model.LoginEvents
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import org.flywaydb.core.Flyway
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
@ -10,14 +11,27 @@ import org.jetbrains.exposed.sql.transactions.transaction
object DatabaseFactory { object DatabaseFactory {
fun init() { fun init() {
val host = System.getenv("DB_HOST") val host = System.getenv("DB_HOST")
val user = "postgres"
val password = "postgres"
val database = Database.connect( val database = Database.connect(
url = "jdbc:pgsql://$host:5432/schools", url = "jdbc:postgresql://$host:5432/schools",
driver = "com.impossibl.postgres.jdbc.PGDriver", driver = "org.postgresql.Driver",
user = "postgres", user = user,
password = "postgres", password = password,
) )
val flyway = Flyway.configure()
.dataSource(database.url, user, password)
.load()
flyway.migrate()
transaction(database) { transaction(database) {
SchemaUtils.statementsRequiredToActualizeScheme(LoginEvents).let {
if (it.isNotEmpty()) {
println(it)
error("There is/are ${it.size} migrations to run!")
}
}
SchemaUtils.create(LoginEvents) SchemaUtils.create(LoginEvents)
} }
} }

View file

@ -18,6 +18,7 @@ class LoginEventDao {
schoolAddress = row[LoginEvents.schoolAddress], schoolAddress = row[LoginEvents.schoolAddress],
scraperBaseUrl = row[LoginEvents.scraperBaseUrl], scraperBaseUrl = row[LoginEvents.scraperBaseUrl],
symbol = row[LoginEvents.symbol], symbol = row[LoginEvents.symbol],
schoolId = row[LoginEvents.schoolId],
loginType = row[LoginEvents.loginType], loginType = row[LoginEvents.loginType],
uuid = row[LoginEvents.uuid], uuid = row[LoginEvents.uuid],
) )
@ -34,6 +35,7 @@ class LoginEventDao {
it[schoolAddress] = event.schoolAddress it[schoolAddress] = event.schoolAddress
it[scraperBaseUrl] = event.scraperBaseUrl it[scraperBaseUrl] = event.scraperBaseUrl
it[symbol] = event.symbol it[symbol] = event.symbol
it[schoolId] = event.schoolId
it[loginType] = event.loginType it[loginType] = event.loginType
it[timestamp] = Instant.now() it[timestamp] = Instant.now()
} }

View file

@ -1,9 +1,9 @@
package io.github.wulkanowy.schools.model package io.github.wulkanowy.schools.model
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
import java.util.UUID import java.util.*
@Serializable @Serializable
data class LoginEvent( data class LoginEvent(
@ -12,6 +12,7 @@ data class LoginEvent(
val schoolAddress: String, val schoolAddress: String,
val scraperBaseUrl: String, val scraperBaseUrl: String,
val symbol: String, val symbol: String,
val schoolId: String,
val loginType: String, val loginType: String,
) )
@ -23,6 +24,7 @@ object LoginEvents : Table() {
val schoolAddress = varchar("schoolAddress", 256) val schoolAddress = varchar("schoolAddress", 256)
val scraperBaseUrl = varchar("scraperBaseUrl", 128) val scraperBaseUrl = varchar("scraperBaseUrl", 128)
val symbol = varchar("symbol", 64) val symbol = varchar("symbol", 64)
val schoolId = varchar("schoolId", 16)
val loginType = varchar("loginType", 32) val loginType = varchar("loginType", 32)
override val primaryKey = PrimaryKey(id) override val primaryKey = PrimaryKey(id)

View file

@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS loginevents
(
id SERIAL PRIMARY KEY,
uuid VARCHAR(36) NOT NULL,
"timestamp" TIMESTAMP NOT NULL,
"schoolName" VARCHAR(256) NOT NULL,
"schoolAddress" VARCHAR(256) NOT NULL,
"scraperBaseUrl" VARCHAR(128) NOT NULL,
symbol VARCHAR(64) NOT NULL,
"loginType" VARCHAR(32) NOT NULL
);
ALTER TABLE loginevents ADD CONSTRAINT "Unique event constraint" UNIQUE (uuid);

View file

@ -0,0 +1 @@
ALTER TABLE loginevents ADD "schoolId" VARCHAR(16) NOT NULL;