Compare commits
7 Commits
e9ba266d53
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
d58a7dbe4e
|
|||
|
974d8d631d
|
|||
|
4bfb32eb01
|
|||
|
dd5a3d70c3
|
|||
|
d5f6c2dac8
|
|||
|
be32a97347
|
|||
|
1811c98073
|
@@ -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 }}
|
||||
15
README.md
15
README.md
@@ -1,12 +1,6 @@
|
||||
# Kotlin Database Migration
|
||||
# migration.kt
|
||||
|
||||
This library enables code-based database migrations in Kotlin.
|
||||
|
||||
## Features
|
||||
|
||||
* Declare database migrations (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.
|
||||
`migration.kt` is a Kotlin library that enables code-based migrations.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -18,16 +12,13 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("com.infendro:migration:1.1.0")
|
||||
implementation("com.infendro:migration:1.2.0")
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```kotlin
|
||||
import com.infendro.migration.migrate
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
|
||||
fun configureDatabase() {
|
||||
// connect to a database
|
||||
Database.connect(/* parameters */)
|
||||
|
||||
@@ -1,53 +1,35 @@
|
||||
import com.infendro.plugin.infendro
|
||||
import com.infendro.plugin.token
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.1.1"
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -1 +1,6 @@
|
||||
rootProject.name = "migration"
|
||||
|
||||
pluginManagement.repositories {
|
||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ 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
|
||||
|
||||
@@ -14,10 +14,7 @@ internal val logger: Logger = LoggerFactory.getLogger("Database")
|
||||
class MigrationConfig {
|
||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
||||
|
||||
fun migration(
|
||||
name: String,
|
||||
migrate: Transaction.() -> Unit,
|
||||
) {
|
||||
fun migration(name: String, migrate: JdbcTransaction.() -> Unit) {
|
||||
if (migrations.any { it.name == name })
|
||||
throw DuplicateMigrationException(name)
|
||||
|
||||
@@ -26,13 +23,11 @@ class MigrationConfig {
|
||||
|
||||
data class Migration(
|
||||
val name: String,
|
||||
val migrate: Transaction.() -> Unit,
|
||||
val migrate: JdbcTransaction.() -> Unit,
|
||||
)
|
||||
}
|
||||
|
||||
fun migrate(
|
||||
config: MigrationConfig,
|
||||
) {
|
||||
fun migrate(config: MigrationConfig) {
|
||||
transaction {
|
||||
SchemaUtils.create(MigrationTable)
|
||||
}
|
||||
@@ -41,23 +36,19 @@ fun migrate(
|
||||
if (MigrationRepository.exists(name))
|
||||
continue
|
||||
|
||||
transaction {
|
||||
try {
|
||||
migrate()
|
||||
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,
|
||||
) {
|
||||
fun migrate(configure: MigrationConfig.() -> Unit) {
|
||||
val config = MigrationConfig()
|
||||
config.configure()
|
||||
migrate(config)
|
||||
|
||||
@@ -2,4 +2,7 @@ package com.infendro.migration.exception
|
||||
|
||||
class DuplicateMigrationException(
|
||||
val name: String,
|
||||
) : Exception()
|
||||
) : Exception() {
|
||||
override val message: String
|
||||
get() = """duplicate migration "$name""""
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user