generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Polyana
committed
Dec 21, 2023
1 parent
238071f
commit d9c86d2
Showing
10 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
plugins { | ||
id("maven-publish") | ||
kotlin("jvm") | ||
kotlin("plugin.serialization") | ||
} | ||
|
||
kotlin { | ||
explicitApi() | ||
} | ||
|
||
val inlineLoggerVersion: String by rootProject | ||
|
||
dependencies { | ||
api(project(":core")) | ||
compileOnly("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:$inlineLoggerVersion") | ||
} |
5 changes: 5 additions & 0 deletions
5
...nce/base/src/main/kotlin/br/com/gamemods/minecity/persistence/base/MineCityPersistence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package br.com.gamemods.minecity.persistence.base | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
|
||
public interface MineCityPersistence: CoroutineScope |
5 changes: 5 additions & 0 deletions
5
...src/main/kotlin/br/com/gamemods/minecity/persistence/base/MineCityPersistenceException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package br.com.gamemods.minecity.persistence.base | ||
|
||
import java.io.IOException | ||
|
||
public class MineCityPersistenceException(message: String? = null, cause: Throwable? = null): IOException(message, cause) |
16 changes: 16 additions & 0 deletions
16
...in/br/com/gamemods/minecity/persistence/base/annotation/InternalMineCityPersistenceApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package br.com.gamemods.minecity.persistence.base.annotation | ||
|
||
/** | ||
* Indicates that the annotated element should only be used by MineCity Persistence API implementation, not API users. | ||
*/ | ||
@Retention(value = AnnotationRetention.BINARY) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.TYPEALIAS, AnnotationTarget.PROPERTY, | ||
AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.CONSTRUCTOR | ||
) | ||
@RequiresOptIn( | ||
level = RequiresOptIn.Level.ERROR, message = "This is an internal MineCity API that " + | ||
"should not be used from outside of br.com.gamemods.minecity.persistence. No compatibility guarantees are provided. " + | ||
"It is recommended to report your use-case of internal API to MineCity2 issue tracker, " + | ||
"so stable API could be provided instead" | ||
) | ||
public annotation class InternalMineCityPersistenceApi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
plugins { | ||
id("maven-publish") | ||
kotlin("jvm") | ||
kotlin("plugin.serialization") | ||
} | ||
|
||
val inlineLoggerVersion: String by rootProject | ||
|
||
dependencies { | ||
implementation(project(":persistence:base")) | ||
implementation("org.flywaydb:flyway-core:10.1.0") | ||
implementation("org.jdbi:jdbi3-core:3.41.3") | ||
compileOnly("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:$inlineLoggerVersion") | ||
} |
64 changes: 64 additions & 0 deletions
64
...ce/sql/src/main/kotlin/br/com/gamemods/minecity/persistence/sql/MineCitySqlPersistence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package br.com.gamemods.minecity.persistence.sql | ||
|
||
import br.com.gamemods.minecity.api.server.MineCityServer | ||
import br.com.gamemods.minecity.persistence.base.MineCityPersistence | ||
import br.com.gamemods.minecity.persistence.base.MineCityPersistenceException | ||
import br.com.gamemods.minecity.persistence.base.annotation.InternalMineCityPersistenceApi | ||
import com.github.michaelbull.logging.InlineLogger | ||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.job | ||
import org.flywaydb.core.Flyway | ||
import javax.sql.DataSource | ||
|
||
@InternalMineCityPersistenceApi | ||
class MineCitySqlPersistence( | ||
private val server: MineCityServer, | ||
private val dataSource: DataSource, | ||
): MineCityPersistence { | ||
override val coroutineContext = SupervisorJob(server.coroutineContext.job) + Dispatchers.IO + CoroutineName("MineCitySqlPersistence ${server.serverIp}") | ||
private val log = InlineLogger() | ||
|
||
@Throws(MineCityPersistenceException::class) | ||
fun upgrade() { | ||
val flyway = Flyway.configure() | ||
.dataSource(dataSource) | ||
.locations("classpath:br/com/gamemods/minecity/persistence/sql/migration") | ||
.load() | ||
|
||
val result = try { | ||
flyway.migrate() | ||
} catch (e: Throwable) { | ||
log.error(e) { | ||
"SQL migration failed with an exception!" | ||
} | ||
throw MineCityPersistenceException("SQL migration failed with an exception!", e) | ||
} | ||
if (!result.success) { | ||
log.error { | ||
"SQL migration failed." | ||
} | ||
} else if (result.migrationsExecuted > 0) { | ||
log.info { | ||
"SQL databased migrated from ${result.initialSchemaVersion} to ${result.targetSchemaVersion} (${result.migrationsExecuted} migrations executed) in ${result.totalMigrationTime}ms." | ||
} | ||
} else { | ||
log.info { "SQL database is up to date." } | ||
} | ||
if (result.warnings.isNotEmpty()) { | ||
log.warn { | ||
buildString { | ||
append("SQL database migration raised ${result.warnings.size} warning(s):") | ||
appendLine() | ||
result.warnings.forEach { | ||
append(it) | ||
appendLine() | ||
appendLine() | ||
} | ||
} | ||
} | ||
} | ||
if (result.success) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters