Compare commits

...

13 Commits

Author SHA1 Message Date
65b513a229 Merge pull request '1.2.0 release' (#2) from develop into main
All checks were successful
/ publish (push) Successful in 1m15s
Reviewed-on: Infendro/migration-kt#2
2026-02-19 13:04:02 +00:00
974d8d631d Upgrade dependencies 2026-02-19 14:01:16 +01:00
4bfb32eb01 improve ci 2026-01-13 18:15:39 +01:00
dd5a3d70c3 update README 2025-12-13 23:04:44 +01:00
d5f6c2dac8 fix workflow 2025-12-13 01:32:27 +01:00
be32a97347 refactor build.gradle.kts 2025-12-13 01:03:58 +01:00
1811c98073 use common plugin 2025-12-13 00:49:24 +01:00
e9ba266d53 bump version to 1.1.1
All checks were successful
/ publish (push) Successful in 2m5s
2025-12-12 21:40:50 +01:00
8fcc08ecb5 refactor 2025-12-12 21:40:26 +01:00
07d12b8b85 update README 2025-10-03 11:43:16 +02:00
7909e4e4de update README
add relevant imports to Usage
2025-10-02 11:31:29 +02:00
69e3b7720f update README 2025-10-02 11:29:11 +02:00
30e0a545c4 update README 2025-10-02 11:10:22 +02:00
10 changed files with 105 additions and 144 deletions

View File

@@ -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/

View 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 }}

View File

@@ -1,6 +1,6 @@
# Kotlin Database Migration
# migration-kt
This library enables code-based database migrations in Kotlin.
`migration-kt` is a Kotlin library that enables code-based migrations.
## Installation
@@ -12,7 +12,7 @@ repositories {
}
dependencies {
implementation("com.infendro:migration:1.0.0")
implementation("com.infendro:migration:1.2.0")
}
```
@@ -21,11 +21,7 @@ dependencies {
```kotlin
fun configureDatabase() {
// connect to a database
Database.connect(
url = <url>,
user = <username>,
password = <password>,
)
Database.connect(/* parameters */)
// declare and execute migrations
migrate {
@@ -36,9 +32,3 @@ fun configureDatabase() {
}
}
```
## How It Works
* Each migration is identified by a unique name.
* Migrations are executed in the order they are declared in.
* Executed migrations are tracked in a database table and skipped on subsequent runs.

View File

@@ -1,53 +1,35 @@
import com.infendro.plugin.infendro
import com.infendro.plugin.token
group = "com.infendro"
version = "1.1.0"
version = "1.2.0"
plugins {
alias(libs.plugins.infendro)
alias(libs.plugins.kotlin)
id("maven-publish")
}
repositories {
mavenCentral()
}
plugins {
alias(libs.plugins.kotlin)
id("maven-publish")
}
dependencies {
implementation(libs.exposed)
implementation(libs.exposed.dao)
implementation(libs.exposed.jdbc)
}
publishing {
publications {
create<MavenPublication>("JVM") {
from(components["java"])
groupId = group as String
groupId = project.group as String
artifactId = project.name
version = version
version = project.version as String
}
}
repositories {
maven {
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"
infendro(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
}

View File

@@ -1,10 +1,13 @@
[versions]
kotlin = "2.2.10"
exposed = "0.61.0"
infendro = "1.1.0"
kotlin = "2.3.0"
exposed = "1.0.0"
[plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
[libraries]
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" }

View File

@@ -1 +1,6 @@
rootProject.name = "migration"
pluginManagement.repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
mavenCentral()
}

View File

@@ -3,66 +3,53 @@ package com.infendro.migration
import com.infendro.migration.exception.DuplicateMigrationException
import com.infendro.migration.model.MigrationRepository
import com.infendro.migration.model.MigrationTable
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.v1.jdbc.JdbcTransaction
import org.jetbrains.exposed.v1.jdbc.SchemaUtils
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
import org.slf4j.Logger
import org.slf4j.LoggerFactory
internal val logger: Logger = LoggerFactory.getLogger("Database")
fun migrate(
config: Config
) {
transaction {
SchemaUtils.create(MigrationTable)
}
for ((name, migrate) in config.migrations) {
if (MigrationRepository.exists(name)) {
continue
}
transaction {
try {
migrate()
logger.info("""Migration "$name" successful""")
} catch (e: Exception) {
logger.error("""Migration "$name" failed""")
throw e
}
}
MigrationRepository.insert(name)
}
}
@Suppress("UNUSED")
fun migrate(
configure: Config.() -> Unit
) {
val config = Config()
config.configure()
migrate(config)
}
class Config {
class MigrationConfig {
internal val migrations: MutableList<Migration> = mutableListOf()
@Suppress("UNUSED")
fun migration(
name: String,
migrate: Transaction.() -> Unit,
) {
if (migrations.any { it.name == name }) {
fun migration(name: String, migrate: JdbcTransaction.() -> Unit) {
if (migrations.any { it.name == name })
throw DuplicateMigrationException(name)
}
migrations += Migration(name, migrate)
}
data class Migration(
val name: String,
val migrate: Transaction.() -> Unit
val migrate: JdbcTransaction.() -> Unit,
)
}
fun migrate(config: MigrationConfig) {
transaction {
SchemaUtils.create(MigrationTable)
}
for ((name, migrate) in config.migrations) {
if (MigrationRepository.exists(name))
continue
try {
transaction { migrate() }
logger.info("""Migration "$name" successful""")
} catch (e: Exception) {
logger.error("""Migration "$name" failed""")
throw e
}
MigrationRepository.insert(name)
}
}
fun migrate(configure: MigrationConfig.() -> Unit) {
val config = MigrationConfig()
config.configure()
migrate(config)
}

View File

@@ -2,4 +2,7 @@ package com.infendro.migration.exception
class DuplicateMigrationException(
val name: String,
) : Exception()
) : Exception() {
override val message: String
get() = """duplicate migration "$name""""
}

View File

@@ -1,9 +1,9 @@
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
import org.jetbrains.exposed.v1.core.dao.id.EntityID
import org.jetbrains.exposed.v1.core.dao.id.LongIdTable
import org.jetbrains.exposed.v1.dao.LongEntity
import org.jetbrains.exposed.v1.dao.LongEntityClass
object MigrationTable : LongIdTable("migration", "id") {
val name = text("name")

View File

@@ -1,22 +1,17 @@
package com.infendro.migration.model
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
object MigrationRepository {
fun exists(
name: String,
): Boolean = transaction {
fun exists(name: String): Boolean = transaction {
MigrationEntity
.find { MigrationTable.name eq name }
.any()
}
fun insert(
name: String,
): Unit = transaction {
fun insert(name: String): Unit = transaction {
MigrationEntity
.new {
this.name = name
}
.new { this.name = name }
}
}