Skip to content

Commit

Permalink
Fix upsert and update Exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Dec 14, 2023
1 parent bbde850 commit b16f3ac
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
`maven-publish`
}

val exposedPowerUtils = "1.3.0"
val exposedPowerUtils = "1.4.0"

allprojects {
group = "net.perfectdreams.exposedpowerutils"
Expand Down
2 changes: 1 addition & 1 deletion exposed-power-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

dependencies {
implementation("org.jetbrains.exposed:exposed-core:0.42.0")
implementation("org.jetbrains.exposed:exposed-core:0.45.0")
implementation("io.github.microutils:kotlin-logging:2.1.23")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ suspend fun <T> transaction(
database,
transactionIsolation
) {
// We are handling the repetition attempts ourselves, see https://github.com/JetBrains/Exposed/commit/88315512d392a7f723ae387f87f9656ccf9b8210
// We are handling the repetition attempts ourselves, see h
// https://github.com/JetBrains/Exposed/commit/88315512d392a7f723ae387f87f9656ccf9b8210
// If we keep Exposed handling the repetition attempts, it will throw exceptions saying that the connection is closed
repetitionAttempts = 0

withContext(coroutineContext + CoroutineTransaction(this)) {
statement.invoke(this@newSuspendedTransaction)
}
Expand Down
2 changes: 1 addition & 1 deletion postgres-java-time/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

dependencies {
implementation("org.jetbrains.exposed:exposed-core:0.42.0")
implementation("org.jetbrains.exposed:exposed-core:0.45.0")
implementation("org.postgresql:postgresql:42.3.8")
}

Expand Down
2 changes: 1 addition & 1 deletion postgres-power-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

dependencies {
implementation("org.jetbrains.exposed:exposed-core:0.42.0")
implementation("org.jetbrains.exposed:exposed-core:0.45.0")
implementation("org.postgresql:postgresql:42.3.8")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import org.jetbrains.exposed.sql.transactions.TransactionManager

// Based off https://github.com/JetBrains/Exposed/issues/167#issuecomment-952734972
Expand Down Expand Up @@ -36,9 +37,20 @@ class InsertOrUpdate<Key : Any>(
override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
val tm = TransactionManager.current()
val updateSetter = (table.columns - keys).joinToString { "${tm.identity(it)} = EXCLUDED.${tm.identity(it)}" }
val onConflict = "ON CONFLICT (${keys.joinToString { tm.identity(it) }}) DO UPDATE SET $updateSetter"
val onConflict = if (updateSetter.isEmpty()) {
"ON CONFLICT (${keys.joinToString { tm.identity(it) }}) DO NOTHING"
} else {
"ON CONFLICT (${keys.joinToString { tm.identity(it) }}) DO UPDATE SET $updateSetter"
}
return "${super.prepareSQL(transaction, prepared)} $onConflict"
}

override fun prepared(transaction: Transaction, sql: String): PreparedStatementApi {
return if (keys.isEmpty())
transaction.connection.prepareStatement(sql, false)
else
transaction.connection.prepareStatement(sql, true)
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ dependencies {
testImplementation(project(":exposed-power-utils"))
testImplementation(project(":postgres-java-time"))
testImplementation(project(":postgres-power-utils"))
testImplementation("org.jetbrains.exposed:exposed-core:0.42.0")
testImplementation("org.jetbrains.exposed:exposed-jdbc:0.42.0")
testImplementation("org.jetbrains.exposed:exposed-dao:0.42.0")
testImplementation("org.jetbrains.exposed:exposed-core:0.45.0")
testImplementation("org.jetbrains.exposed:exposed-jdbc:0.45.0")
testImplementation("org.jetbrains.exposed:exposed-dao:0.45.0")
testImplementation("com.zaxxer:HikariCP:5.0.1")
testImplementation("org.postgresql:postgresql:42.3.8")
testImplementation("ch.qos.logback:logback-classic:1.3.0-alpha14")
Expand Down
13 changes: 13 additions & 0 deletions tests/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
70 changes: 70 additions & 0 deletions tests/src/test/kotlin/UpsertTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.perfectdreams.exposedpowerutils.sql.jsonb
import net.perfectdreams.exposedpowerutils.sql.postgresEnumeration
import net.perfectdreams.exposedpowerutils.sql.upsert
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
Expand Down Expand Up @@ -42,7 +43,76 @@ class UpsertTest {
}
}

@Test
fun `test upsert uuid`() {
val test = Database.connect(postgres.jdbcUrl, user = postgres.username, password = postgres.password)

transaction(test) {
SchemaUtils.createMissingTablesAndColumns(ProfileDesignGroups)

val id = UUID.randomUUID()

val a = ProfileDesignGroups.upsert(ProfileDesignGroups.id) {
it[ProfileDesignGroups.id] = id
}

println("RESULT VALUES 1")
println(a.resultedValues)

val b = ProfileDesignGroups.upsert(ProfileDesignGroups.id) {
it[ProfileDesignGroups.id] = id
}

println("RESULT VALUES 2")
println(b.resultedValues)

val entry = ProfileDesignGroups.select { ProfileDesignGroups.id eq id }.firstOrNull()

assert(entry != null) { "Entry is null!" }
}
}

@Test
fun `test upsert uuid with data`() {
val test = Database.connect(postgres.jdbcUrl, user = postgres.username, password = postgres.password)

transaction(test) {
SchemaUtils.createMissingTablesAndColumns(ProfileDesignGroupsWithData)

val id = UUID.randomUUID()

val a = ProfileDesignGroupsWithData.upsert(ProfileDesignGroupsWithData.id) {
it[ProfileDesignGroupsWithData.id] = id
it[ProfileDesignGroupsWithData.str] = "hewwo"
}

println("RESULT VALUES 1")
println(a.resultedValues)

val b = ProfileDesignGroupsWithData.upsert(ProfileDesignGroupsWithData.id) {
it[ProfileDesignGroupsWithData.id] = id
it[ProfileDesignGroupsWithData.str] = "uwu"
}

println("RESULT VALUES 2")
println(b.resultedValues)

val entry = ProfileDesignGroupsWithData.select { ProfileDesignGroupsWithData.id eq id }.firstOrNull()

assert(entry != null) { "Entry is null!" }

assert(entry!![ProfileDesignGroupsWithData.str] == "uwu") { "Entry does not match!" }
}
}

object Counter : LongIdTable() {
val counter = integer("counter")
}

object ProfileDesignGroups : UUIDTable()


object ProfileDesignGroupsWithData : UUIDTable() {
val str = text("str")
}
}

0 comments on commit b16f3ac

Please sign in to comment.