Compare commits
16 Commits
866a9b19d8
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
d58a7dbe4e
|
|||
|
974d8d631d
|
|||
|
4bfb32eb01
|
|||
|
dd5a3d70c3
|
|||
|
d5f6c2dac8
|
|||
|
be32a97347
|
|||
|
1811c98073
|
|||
|
e9ba266d53
|
|||
|
8fcc08ecb5
|
|||
|
07d12b8b85
|
|||
|
7909e4e4de
|
|||
|
69e3b7720f
|
|||
|
30e0a545c4
|
|||
|
c4a135817a
|
|||
|
3bf33999e4
|
|||
|
69dc768052
|
@@ -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 }}
|
||||||
18
README.md
18
README.md
@@ -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
|
## Installation
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:migration:1.0.0")
|
implementation("com.infendro:migration:1.2.0")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -21,11 +21,7 @@ dependencies {
|
|||||||
```kotlin
|
```kotlin
|
||||||
fun configureDatabase() {
|
fun configureDatabase() {
|
||||||
// connect to a database
|
// connect to a database
|
||||||
Database.connect(
|
Database.connect(/* parameters */)
|
||||||
url = <url>,
|
|
||||||
user = <username>,
|
|
||||||
password = <password>,
|
|
||||||
)
|
|
||||||
|
|
||||||
// declare and execute migrations
|
// declare and execute migrations
|
||||||
migrate {
|
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.
|
|
||||||
|
|||||||
@@ -1,53 +1,35 @@
|
|||||||
|
import com.infendro.plugin.infendro
|
||||||
|
import com.infendro.plugin.token
|
||||||
|
|
||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.0.0"
|
version = "1.2.0"
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
alias(libs.plugins.infendro)
|
||||||
|
alias(libs.plugins.kotlin)
|
||||||
|
id("maven-publish")
|
||||||
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
|
||||||
alias(libs.plugins.kotlin)
|
|
||||||
id("maven-publish")
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.exposed)
|
implementation(libs.exposed)
|
||||||
implementation(libs.exposed.dao)
|
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,10 +1,13 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.2.10"
|
infendro = "1.1.0"
|
||||||
exposed = "0.61.0"
|
kotlin = "2.3.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]
|
||||||
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-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"
|
rootProject.name = "migration"
|
||||||
|
|
||||||
|
pluginManagement.repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,61 +1,55 @@
|
|||||||
package com.infendro.migration
|
package com.infendro.migration
|
||||||
|
|
||||||
|
import com.infendro.migration.exception.DuplicateMigrationException
|
||||||
import com.infendro.migration.model.MigrationRepository
|
import com.infendro.migration.model.MigrationRepository
|
||||||
import com.infendro.migration.model.MigrationTable
|
import com.infendro.migration.model.MigrationTable
|
||||||
import org.jetbrains.exposed.sql.SchemaUtils
|
import org.jetbrains.exposed.v1.jdbc.JdbcTransaction
|
||||||
import org.jetbrains.exposed.sql.Transaction
|
import org.jetbrains.exposed.v1.jdbc.SchemaUtils
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
internal val logger: Logger = LoggerFactory.getLogger("Database")
|
internal val logger: Logger = LoggerFactory.getLogger("Database")
|
||||||
|
|
||||||
fun migrate(
|
class MigrationConfig {
|
||||||
config: Config
|
internal val migrations: MutableList<Migration> = mutableListOf()
|
||||||
) {
|
|
||||||
|
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: JdbcTransaction.() -> Unit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun migrate(config: MigrationConfig) {
|
||||||
transaction {
|
transaction {
|
||||||
SchemaUtils.create(MigrationTable)
|
SchemaUtils.create(MigrationTable)
|
||||||
}
|
}
|
||||||
|
|
||||||
for ((name, migrate) in config.migrations) {
|
for ((name, migrate) in config.migrations) {
|
||||||
if (MigrationRepository.exists(name)) {
|
if (MigrationRepository.exists(name))
|
||||||
continue
|
continue
|
||||||
}
|
|
||||||
|
|
||||||
transaction {
|
try {
|
||||||
try {
|
transaction { migrate() }
|
||||||
migrate()
|
logger.info("""Migration "$name" successful""")
|
||||||
logger.info("""Migration "$name" successful""")
|
} catch (e: Exception) {
|
||||||
} catch (e: Exception) {
|
logger.error("""Migration "$name" failed""")
|
||||||
logger.error("""Migration "$name" failed""")
|
throw e
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MigrationRepository.insert(name)
|
MigrationRepository.insert(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun migrate(
|
fun migrate(configure: MigrationConfig.() -> Unit) {
|
||||||
configure: Config.() -> Unit
|
val config = MigrationConfig()
|
||||||
) {
|
|
||||||
val config = Config()
|
|
||||||
config.configure()
|
config.configure()
|
||||||
migrate(config)
|
migrate(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
class Config {
|
|
||||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
|
||||||
|
|
||||||
fun migration(
|
|
||||||
name: String,
|
|
||||||
migrate: Transaction.() -> Unit,
|
|
||||||
) {
|
|
||||||
migrations += Migration(name, migrate)
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Migration(
|
|
||||||
val name: String,
|
|
||||||
val migrate: Transaction.() -> Unit
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.infendro.migration.exception
|
||||||
|
|
||||||
|
class DuplicateMigrationException(
|
||||||
|
val name: String,
|
||||||
|
) : Exception() {
|
||||||
|
override val message: String
|
||||||
|
get() = """duplicate migration "$name""""
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package com.infendro.migration.model
|
package com.infendro.migration.model
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.LongEntity
|
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||||
import org.jetbrains.exposed.dao.LongEntityClass
|
import org.jetbrains.exposed.v1.core.dao.id.LongIdTable
|
||||||
import org.jetbrains.exposed.dao.id.EntityID
|
import org.jetbrains.exposed.v1.dao.LongEntity
|
||||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
import org.jetbrains.exposed.v1.dao.LongEntityClass
|
||||||
|
|
||||||
object MigrationTable : LongIdTable("migration", "id") {
|
object MigrationTable : LongIdTable("migration", "id") {
|
||||||
val name = text("name")
|
val name = text("name")
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
package com.infendro.migration.model
|
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 {
|
object MigrationRepository {
|
||||||
fun exists(
|
fun exists(name: String): Boolean = transaction {
|
||||||
name: String,
|
|
||||||
): Boolean = transaction {
|
|
||||||
MigrationEntity
|
MigrationEntity
|
||||||
.find { MigrationTable.name eq name }
|
.find { MigrationTable.name eq name }
|
||||||
.any()
|
.any()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun insert(
|
fun insert(name: String): Unit = transaction {
|
||||||
name: String,
|
|
||||||
): Unit = transaction {
|
|
||||||
MigrationEntity
|
MigrationEntity
|
||||||
.new {
|
.new { this.name = name }
|
||||||
this.name = name
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user