Skip to content

Commit 2cb0a9b

Browse files
committed
Require >=42.7.5 of the PostgreSQL driver when using components
1 parent e6e92bb commit 2cb0a9b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@
557557
<dependency>
558558
<groupId>org.postgresql</groupId>
559559
<artifactId>postgresql</artifactId>
560-
<version>42.7.5</version>
560+
<version>42.7.5</version> <!-- Tied to [[ComponentRepository#checkPostgresVersion]] -->
561561
<scope>test</scope>
562562
</dependency>
563563
<dependency>

src/main/kotlin/io/github/freya022/botcommands/internal/components/repositories/ComponentRepository.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ import io.github.freya022.botcommands.internal.core.exceptions.internalErrorMess
2626
import io.github.freya022.botcommands.internal.utils.throwArgument
2727
import io.github.freya022.botcommands.internal.utils.throwInternal
2828
import io.github.oshai.kotlinlogging.KotlinLogging
29+
import kotlinx.coroutines.runBlocking
2930
import kotlinx.datetime.Clock
3031
import kotlinx.datetime.Instant
3132
import kotlinx.datetime.toJavaInstant
3233
import net.dv8tion.jda.api.Permission
34+
import java.sql.DatabaseMetaData
3335
import java.sql.Timestamp
3436
import kotlin.time.Duration
3537
import kotlin.time.Duration.Companion.milliseconds
@@ -55,6 +57,28 @@ internal class ComponentRepository(
5557
val instant: Instant
5658
)
5759

60+
init {
61+
runBlocking {
62+
database.fetchConnection(readOnly = true).use {
63+
val metadata = it.metaData
64+
if (metadata.databaseProductName == "PostgreSQL") {
65+
checkPostgresVersion(metadata)
66+
}
67+
}
68+
}
69+
}
70+
71+
private fun checkPostgresVersion(metadata: DatabaseMetaData) {
72+
if (metadata.driverName != "PostgreSQL JDBC Driver") {
73+
return logger.warn { "Detected a third party PostgreSQL driver, though it may work regardless, we test PostgreSQL support using the first party driver found at https://github.com/pgjdbc/pgjdbc" }
74+
}
75+
76+
// The 42.7.5 driver is required for byte[] support in setObject
77+
check(metadata.driverVersion >= "42.7.5") {
78+
"Using components require a version of the PostgreSQL driver higher or equal to 42.7.5"
79+
}
80+
}
81+
5882
suspend fun getPersistentComponentTimeouts(): List<PersistentComponentTimeout> {
5983
return database.preparedStatement(
6084
"select component_id, expires_at from bc_component where lifetime_type = ? and expires_at is not null",

0 commit comments

Comments
 (0)