Compare commits
18 Commits
4a5d53be0f
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
90fcdcaa4f
|
|||
|
d26f0a1310
|
|||
|
e3931e3e8f
|
|||
|
3380f50cc7
|
|||
|
b777eadd13
|
|||
|
bba1f2d63d
|
|||
|
27b335a0af
|
|||
|
21ed1c40b5
|
|||
|
d0c130cb61
|
|||
|
1fbff53dbd
|
|||
|
a759d19466
|
|||
|
8d2efa65df
|
|||
|
0cc1f208ff
|
|||
|
53d857186d
|
|||
|
e74d0cdc60
|
|||
|
dcd23f0283
|
|||
|
8b8614b4de
|
|||
|
9f32143b60
|
@@ -1,34 +0,0 @@
|
|||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
env:
|
|
||||||
GIT__TOKEN: ${{ secrets.TOKEN }}
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
publish:
|
|
||||||
runs-on: linux
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- uses: actions/cache/restore@v4
|
|
||||||
with:
|
|
||||||
key: gradle
|
|
||||||
path: |
|
|
||||||
~/.gradle/caches/
|
|
||||||
~/.gradle/wrapper/
|
|
||||||
|
|
||||||
- uses: actions/setup-java@v4
|
|
||||||
with:
|
|
||||||
java-version: '24'
|
|
||||||
distribution: 'temurin'
|
|
||||||
|
|
||||||
- run: ./gradlew publish
|
|
||||||
|
|
||||||
- uses: actions/cache/save@v4
|
|
||||||
with:
|
|
||||||
key: gradle
|
|
||||||
path: |
|
|
||||||
~/.gradle/caches/
|
|
||||||
~/.gradle/wrapper/
|
|
||||||
30
.gitea/workflows/publish.yaml
Normal file
30
.gitea/workflows/publish.yaml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: linux
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Java
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
java-version: '24'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Cache Gradle
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: gradle
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches/
|
||||||
|
~/.gradle/wrapper/
|
||||||
|
|
||||||
|
- name: Publish
|
||||||
|
run: ./gradlew publish
|
||||||
|
env:
|
||||||
|
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
|
||||||
34
README.md
Normal file
34
README.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# migration.ktor
|
||||||
|
|
||||||
|
`migration.ktor` is a Kotlin library that enables code-based migrations in Ktor.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add the following to your `build.gradle.kts`.
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("com.infendro:ktor-migration:1.2.0")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
fun Application.configureDatabase() {
|
||||||
|
// connect to a database
|
||||||
|
Database.connect(/* parameters */)
|
||||||
|
|
||||||
|
// install the plugin
|
||||||
|
install(Migration) {
|
||||||
|
// create a migration
|
||||||
|
migration("initial") {
|
||||||
|
// execute code to migrate the database
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,54 +1,37 @@
|
|||||||
group = "com.infendro"
|
import com.infendro.plugin.infendro
|
||||||
version = "1.0.0"
|
import com.infendro.plugin.token
|
||||||
|
|
||||||
repositories {
|
group = "com.infendro"
|
||||||
mavenCentral()
|
version = "1.2.0"
|
||||||
}
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
alias(libs.plugins.infendro)
|
||||||
alias(libs.plugins.kotlin)
|
alias(libs.plugins.kotlin)
|
||||||
id("maven-publish")
|
id("maven-publish")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
infendro()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.ktor.server)
|
implementation(libs.ktor.server)
|
||||||
|
implementation(libs.migration)
|
||||||
implementation(libs.exposed)
|
implementation(libs.exposed)
|
||||||
implementation(libs.exposed.dao)
|
implementation(libs.exposed.jdbc)
|
||||||
}
|
}
|
||||||
|
|
||||||
publishing {
|
publishing {
|
||||||
publications {
|
publications {
|
||||||
create<MavenPublication>("JVM") {
|
create<MavenPublication>("JVM") {
|
||||||
from(components["java"])
|
from(components["java"])
|
||||||
groupId = group as String
|
groupId = project.group as String
|
||||||
artifactId = project.name
|
artifactId = project.name
|
||||||
version = version
|
version = project.version as String
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
repositories {
|
repositories {
|
||||||
maven {
|
infendro(token)
|
||||||
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
|
|
||||||
authentication.create("header", HttpHeaderAuthentication::class)
|
|
||||||
credentials(HttpHeaderCredentials::class) {
|
|
||||||
val token = secret("git.token") ?: throw GradleException()
|
|
||||||
|
|
||||||
name = "Authorization"
|
|
||||||
value = "token $token"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun secret(
|
|
||||||
key: String,
|
|
||||||
): String? {
|
|
||||||
val property = key
|
|
||||||
val environment = key
|
|
||||||
.uppercase()
|
|
||||||
.replace(".", "__")
|
|
||||||
|
|
||||||
val prop = properties[property] as String?
|
|
||||||
val env = System.getenv(environment)
|
|
||||||
|
|
||||||
return prop ?: env
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.2.10"
|
infendro = "1.1.0"
|
||||||
ktor = "3.2.3"
|
kotlin = "2.3.0"
|
||||||
exposed = "0.60.0"
|
ktor = "3.4.0"
|
||||||
|
migration = "1.2.0"
|
||||||
|
exposed = "1.0.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
ktor-server = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
|
ktor-server = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
|
||||||
|
migration = { module = "com.infendro:migration", version.ref = "migration" }
|
||||||
exposed = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
|
exposed = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
|
||||||
exposed-dao = { module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed" }
|
exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "exposed" }
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
rootProject.name = "ktor-migration"
|
rootProject.name = "migration-ktor"
|
||||||
|
|
||||||
|
pluginManagement.repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|||||||
19
src/main/kotlin/com/infendro/ktor/migration/Plugin.kt
Normal file
19
src/main/kotlin/com/infendro/ktor/migration/Plugin.kt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.infendro.ktor.migration
|
||||||
|
|
||||||
|
import com.infendro.migration.migrate
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
import org.jetbrains.exposed.v1.jdbc.JdbcTransaction
|
||||||
|
|
||||||
|
val Migration = createApplicationPlugin("Migration", MigrationConfig::new) {
|
||||||
|
migrate(pluginConfig.config)
|
||||||
|
}
|
||||||
|
|
||||||
|
class MigrationConfig {
|
||||||
|
internal val config = com.infendro.migration.MigrationConfig()
|
||||||
|
|
||||||
|
fun migration(name: String, migrate: JdbcTransaction.() -> Unit) = config.migration(name, migrate)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun new() = MigrationConfig()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
package com.infendro.migration
|
|
||||||
|
|
||||||
import com.infendro.migration.model.MigrationRepository
|
|
||||||
import com.infendro.migration.model.MigrationTable
|
|
||||||
import io.ktor.server.application.*
|
|
||||||
import org.jetbrains.exposed.sql.SchemaUtils
|
|
||||||
import org.jetbrains.exposed.sql.Transaction
|
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
|
|
||||||
internal val logger: Logger = LoggerFactory.getLogger("Database")
|
|
||||||
|
|
||||||
val Migration = createApplicationPlugin(
|
|
||||||
name = "Migration",
|
|
||||||
createConfiguration = { MigrationConfig() },
|
|
||||||
) {
|
|
||||||
transaction {
|
|
||||||
SchemaUtils.create(MigrationTable)
|
|
||||||
}
|
|
||||||
|
|
||||||
val (migrations) = pluginConfig
|
|
||||||
for ((name, body) in migrations) {
|
|
||||||
if (MigrationRepository.exists(name))
|
|
||||||
continue
|
|
||||||
|
|
||||||
transaction {
|
|
||||||
try {
|
|
||||||
body()
|
|
||||||
logger.info("""Migration "$name" successful""")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error("""Migration "$name" failed""")
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MigrationRepository.insert(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class MigrationConfig(
|
|
||||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
|
||||||
) {
|
|
||||||
fun migration(
|
|
||||||
name: String,
|
|
||||||
block: Transaction.() -> Unit,
|
|
||||||
) {
|
|
||||||
migrations += Migration(name, block)
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Migration(
|
|
||||||
val name: String,
|
|
||||||
val body: Transaction.() -> Unit
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package com.infendro.migration.model
|
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.LongEntity
|
|
||||||
import org.jetbrains.exposed.dao.LongEntityClass
|
|
||||||
import org.jetbrains.exposed.dao.id.EntityID
|
|
||||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
|
||||||
|
|
||||||
object MigrationTable : LongIdTable("migration", "id") {
|
|
||||||
val name = text("name")
|
|
||||||
}
|
|
||||||
|
|
||||||
class MigrationEntity(id: EntityID<Long>) : LongEntity(id) {
|
|
||||||
companion object : LongEntityClass<MigrationEntity>(MigrationTable)
|
|
||||||
|
|
||||||
var name by MigrationTable.name
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package com.infendro.migration.model
|
|
||||||
|
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
|
||||||
|
|
||||||
object MigrationRepository {
|
|
||||||
fun exists(
|
|
||||||
name: String,
|
|
||||||
): Boolean = transaction {
|
|
||||||
MigrationEntity
|
|
||||||
.find { MigrationTable.name eq name }
|
|
||||||
.any()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun insert(
|
|
||||||
name: String,
|
|
||||||
): Unit = transaction {
|
|
||||||
MigrationEntity
|
|
||||||
.new {
|
|
||||||
this.name = name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user