Compare commits
3 Commits
dd5a3d70c3
...
65b513a229
| Author | SHA1 | Date | |
|---|---|---|---|
|
65b513a229
|
|||
|
974d8d631d
|
|||
|
4bfb32eb01
|
@@ -1,34 +0,0 @@
|
|||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
env:
|
|
||||||
INFENDRO__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 }}
|
||||||
@@ -12,16 +12,13 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:migration:1.1.1")
|
implementation("com.infendro:migration:1.2.0")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
import com.infendro.migration.migrate
|
|
||||||
import org.jetbrains.exposed.sql.Database
|
|
||||||
|
|
||||||
fun configureDatabase() {
|
fun configureDatabase() {
|
||||||
// connect to a database
|
// connect to a database
|
||||||
Database.connect(/* parameters */)
|
Database.connect(/* parameters */)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import com.infendro.plugin.infendro
|
|||||||
import com.infendro.plugin.token
|
import com.infendro.plugin.token
|
||||||
|
|
||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.1.1"
|
version = "1.2.0"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.infendro)
|
alias(libs.plugins.infendro)
|
||||||
@@ -17,6 +17,7 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.exposed)
|
implementation(libs.exposed)
|
||||||
implementation(libs.exposed.dao)
|
implementation(libs.exposed.dao)
|
||||||
|
implementation(libs.exposed.jdbc)
|
||||||
}
|
}
|
||||||
|
|
||||||
publishing {
|
publishing {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
[versions]
|
[versions]
|
||||||
infendro = "1.0.0"
|
infendro = "1.1.0"
|
||||||
kotlin = "2.2.10"
|
kotlin = "2.3.0"
|
||||||
exposed = "0.61.0"
|
exposed = "1.0.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
@@ -10,3 +10,4 @@ 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" }
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package com.infendro.migration
|
|||||||
import com.infendro.migration.exception.DuplicateMigrationException
|
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
|
||||||
|
|
||||||
@@ -14,10 +14,7 @@ internal val logger: Logger = LoggerFactory.getLogger("Database")
|
|||||||
class MigrationConfig {
|
class MigrationConfig {
|
||||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
internal val migrations: MutableList<Migration> = mutableListOf()
|
||||||
|
|
||||||
fun migration(
|
fun migration(name: String, migrate: JdbcTransaction.() -> Unit) {
|
||||||
name: String,
|
|
||||||
migrate: Transaction.() -> Unit,
|
|
||||||
) {
|
|
||||||
if (migrations.any { it.name == name })
|
if (migrations.any { it.name == name })
|
||||||
throw DuplicateMigrationException(name)
|
throw DuplicateMigrationException(name)
|
||||||
|
|
||||||
@@ -26,13 +23,11 @@ class MigrationConfig {
|
|||||||
|
|
||||||
data class Migration(
|
data class Migration(
|
||||||
val name: String,
|
val name: String,
|
||||||
val migrate: Transaction.() -> Unit,
|
val migrate: JdbcTransaction.() -> Unit,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun migrate(
|
fun migrate(config: MigrationConfig) {
|
||||||
config: MigrationConfig,
|
|
||||||
) {
|
|
||||||
transaction {
|
transaction {
|
||||||
SchemaUtils.create(MigrationTable)
|
SchemaUtils.create(MigrationTable)
|
||||||
}
|
}
|
||||||
@@ -41,23 +36,19 @@ fun migrate(
|
|||||||
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: MigrationConfig.() -> Unit,
|
|
||||||
) {
|
|
||||||
val config = MigrationConfig()
|
val config = MigrationConfig()
|
||||||
config.configure()
|
config.configure()
|
||||||
migrate(config)
|
migrate(config)
|
||||||
|
|||||||
@@ -2,4 +2,7 @@ package com.infendro.migration.exception
|
|||||||
|
|
||||||
class DuplicateMigrationException(
|
class DuplicateMigrationException(
|
||||||
val name: String,
|
val name: String,
|
||||||
) : Exception()
|
) : 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,19 +1,16 @@
|
|||||||
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 { this.name = name }
|
.new { this.name = name }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user