Skip to content

Commit

Permalink
Merge pull request #541 from Lapzupi/database-multiple
Browse files Browse the repository at this point in the history
Refactored Database, Fixed Migrations
  • Loading branch information
Oheers authored Jan 31, 2025
2 parents be2677a + 9c15046 commit fc70c7b
Show file tree
Hide file tree
Showing 71 changed files with 5,005 additions and 1,181 deletions.
11 changes: 11 additions & 0 deletions even-more-fish-database-extras/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id("com.oheers.evenmorefish.java-conventions")
}


dependencies {
compileOnly(libs.jooq)
compileOnly(libs.jooq.codegen)

implementation(libs.annotations)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.oheers.fish.database.extras;

import org.jetbrains.annotations.NotNull;
import org.jooq.codegen.DefaultGeneratorStrategy;
import org.jooq.meta.Definition;
import org.jooq.tools.StringUtils;

// This is needed to ensure prefix placeholders don't stay during code generation.
public class PrefixNamingStrategy extends DefaultGeneratorStrategy {

@Override
public String getJavaClassName(final Definition definition, final Mode mode) {
String name = replacePrefix(super.getJavaClassName(definition, mode));

return StringUtils.toUC(name);
}

@Override
public String getJavaIdentifier(final Definition definition) {
return replacePrefix(super.getJavaIdentifier(definition));
}

private @NotNull String replacePrefix(final @NotNull String name) {
return name
.replaceAll("\\$\\{(TABLEPREFIX|tablePrefix|table_Prefix|TABLE_PREFIX)}", "") // Matches variations of ${TABLEPREFIX}
.replaceAll("\\$_7b(TABLEPREFIX|tablePrefix|table_Prefix|TABLE_PREFIX)_7d", "") // Matches variations of $_7bTABLEPREFIX_7d
.replaceAll("\\$\\{.*?}", "") // Generic fallback for any unmatched ${PLACEHOLDER}
.replaceAll("\\$_7b.*?_7d", ""); // Generic fallback for any unmatched $_7bPLACEHOLDER_7d
}


}
77 changes: 75 additions & 2 deletions even-more-fish-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import net.minecrell.pluginyml.bukkit.BukkitPluginDescription
import nu.studer.gradle.jooq.JooqExtension
import org.jooq.meta.jaxb.Property
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*


plugins {
`java-library`
`maven-publish`
alias(libs.plugins.bukkit.yml)
alias(libs.plugins.shadow)
alias(libs.plugins.grgit)
alias(libs.plugins.jooq)
}

group = "com.oheers.evenmorefish"
Expand Down Expand Up @@ -87,7 +91,6 @@ dependencies {
implementation(libs.vanishchecker)
implementation(libs.boostedyaml)

library(libs.maven.artifact)
library(libs.friendlyid)
library(libs.flyway.core)
library(libs.flyway.mysql)
Expand All @@ -96,6 +99,17 @@ dependencies {
library(libs.commons.lang3)
library(libs.commons.codec)
library(libs.json.simple)

library(libs.jooq)
library(libs.jooq.codegen)
library(libs.jooq.meta)
library(libs.connectors.h2)

library(libs.maven.artifact)

jooqGenerator(project(":even-more-fish-database-extras"))
jooqGenerator(libs.jooq.meta.extensions)
jooqGenerator(libs.connectors.mysql)
}

bukkit {
Expand Down Expand Up @@ -193,15 +207,38 @@ bukkit {

}
}
sourceSets {
main {
java.srcDirs.add(File("src/main/generated"))
}
}
tasks.named("compileJava") {
dependsOn(":even-more-fish-plugin:generateMysqlJooq")
}

tasks {
build {
dependsOn(shadowJar)
dependsOn(
shadowJar
)

doLast {
val file = project.layout.buildDirectory.file("libs/even-more-fish-plugin-${version}.jar").get()
file.asFile.delete()
}


}

jooq {
version.set(libs.versions.jooq)

val dialects = listOf("mysql")
val latestSchema = "V7_1__Create_Tables.sql"
dialects.forEach { dialect ->
val schemaPath = "src/main/resources/db/migrations/${dialect}/${latestSchema}"
configureDialect(dialect, schemaPath)
}
}

clean {
Expand All @@ -227,6 +264,7 @@ tasks {
attributes["Specification-Version"] = project.version
attributes["Implementation-Title"] = grgit.branch.current().name
attributes["Implementation-Version"] = buildNumberOrDate
attributes["Database-Baseline-Version"] = "7.0"
}

minimize {
Expand Down Expand Up @@ -291,6 +329,14 @@ java {
}
}

sourceSets {
main {
java {
srcDir("src/main/generated")
}
}
}

fun getBuildNumberOrDate(): String? {
val currentBranch = grgit.branch.current().name
if (currentBranch.equals("head", ignoreCase = true) || currentBranch.equals("master", ignoreCase = true)) {
Expand All @@ -308,3 +354,30 @@ fun getBuildNumberOrDate(): String? {
return time
}

fun JooqExtension.configureDialect(dialect: String, latestSchema: String) {
configurations {
create(dialect) {
generateSchemaSourceOnCompilation.set(false)
jooqConfiguration.apply {
jdbc = null
generator.apply {
//https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-parser/
strategy.name = "com.oheers.fish.database.extras.PrefixNamingStrategy"
database.apply {
name = "org.jooq.meta.extensions.ddl.DDLDatabase"
properties.add(Property().withKey("scripts").withValue(latestSchema))
properties.add(Property().withKey("dialect").withValue(dialect.uppercase()))
properties.add(Property().withKey("sort").withValue("flyway"))
properties.add(Property().withKey("unqualifiedSchema").withValue("none"))
}
target.apply {
packageName = "com.oheers.fish.database.generated.${dialect}"
directory = "src/main/generated/"
}
}
}
}
}
}


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc70c7b

Please sign in to comment.