Skip to content

Commit

Permalink
fix: EXPOSED-706 Handle MariaDB sequence max value for versions earli…
Browse files Browse the repository at this point in the history
…er than 11.5

According to the documentation [here](https://mariadb.com/kb/en/create-sequence/#maxvalue), MariaDB versions less than 11.5 have a max value for sequences that is lower than the one Exposed currently allows and that causes errors.
  • Loading branch information
joc-a committed Jan 27, 2025
1 parent e815785 commit 0cf78d8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
4 changes: 4 additions & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3854,6 +3854,7 @@ public abstract interface class org/jetbrains/exposed/sql/vendors/DatabaseDialec
public abstract fun getNeedsQuotesWhenSymbolsInNames ()Z
public abstract fun getNeedsSequenceToAutoInc ()Z
public abstract fun getRequiresAutoCommitOnCreateDrop ()Z
public abstract fun getSequenceMaxValue ()J
public abstract fun getSupportsCreateSchema ()Z
public abstract fun getSupportsCreateSequence ()Z
public abstract fun getSupportsDualTableConcept ()Z
Expand Down Expand Up @@ -3902,6 +3903,7 @@ public final class org/jetbrains/exposed/sql/vendors/DatabaseDialect$DefaultImpl
public static fun getNeedsQuotesWhenSymbolsInNames (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getNeedsSequenceToAutoInc (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getRequiresAutoCommitOnCreateDrop (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSequenceMaxValue (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)J
public static fun getSupportsCreateSchema (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsCreateSequence (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
public static fun getSupportsDualTableConcept (Lorg/jetbrains/exposed/sql/vendors/DatabaseDialect;)Z
Expand Down Expand Up @@ -4163,6 +4165,7 @@ public final class org/jetbrains/exposed/sql/vendors/MariaDBDialect : org/jetbra
public fun createIndex (Lorg/jetbrains/exposed/sql/Index;)Ljava/lang/String;
public fun getFunctionProvider ()Lorg/jetbrains/exposed/sql/vendors/FunctionProvider;
public fun getName ()Ljava/lang/String;
public fun getSequenceMaxValue ()J
public fun getSupportsCreateSequence ()Z
public fun getSupportsOnlyIdentifiersInGeneratedKeys ()Z
public fun getSupportsSequenceAsGeneratedKeys ()Z
Expand Down Expand Up @@ -4362,6 +4365,7 @@ public abstract class org/jetbrains/exposed/sql/vendors/VendorDialect : org/jetb
public fun getNeedsQuotesWhenSymbolsInNames ()Z
public fun getNeedsSequenceToAutoInc ()Z
public fun getRequiresAutoCommitOnCreateDrop ()Z
public fun getSequenceMaxValue ()J
public fun getSupportsCreateSchema ()Z
public fun getSupportsCreateSequence ()Z
public fun getSupportsDualTableConcept ()Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class AutoIncColumnType<T>(
it,
startWith = 1,
minValue = 1,
maxValue = Long.MAX_VALUE
maxValue = currentDialect.sequenceMaxValue
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ interface DatabaseDialect {
/** Returns true if autoCommit should be enabled to create/drop a database. */
val requiresAutoCommitOnCreateDrop: Boolean get() = false

val sequenceMaxValue: Long get() = Long.MAX_VALUE

/** Returns the name of the current database. */
fun getDatabase(): String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ class MariaDBDialect : MysqlDialect() {
// actually MariaDb supports it but jdbc driver prepares statement without RETURNING clause
override val supportsSequenceAsGeneratedKeys: Boolean = false

@Suppress("MagicNumber")
override val sequenceMaxValue: Long by lazy {
if (TransactionManager.current().db.isVersionCovers(11, 5)) {
super.sequenceMaxValue
} else {
Long.MAX_VALUE - 1
}
}

override fun createIndex(index: Index): String {
if (index.functions != null) {
exposedLogger.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ import kotlin.test.assertNull
@OptIn(ExperimentalDatabaseMigrationApi::class)
class DatabaseMigrationTests : DatabaseTestsBase() {

private lateinit var sequence: Sequence

@Before
fun dropAllSequences() {
withDb {
sequence = Sequence(
name = "my_sequence",
startWith = 1,
minValue = 1,
maxValue = currentDialectTest.sequenceMaxValue
)

if (currentDialectTest.supportsCreateSequence) {
val allSequences = currentDialectTest.sequences().map { name -> Sequence(name) }.toSet()
allSequences.forEach { sequence ->
Expand Down Expand Up @@ -769,33 +778,34 @@ class DatabaseMigrationTests : DatabaseTestsBase() {

private fun expectedCreateSequenceStatement(sequenceName: String) =
"CREATE SEQUENCE${" IF NOT EXISTS".takeIf { currentDialectTest.supportsIfNotExists } ?: ""} " +
"$sequenceName START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807"
"$sequenceName START WITH 1 MINVALUE 1 MAXVALUE ${currentDialectTest.sequenceMaxValue}"

private fun expectedDropSequenceStatement(sequenceName: String) =
"DROP SEQUENCE${" IF EXISTS".takeIf { currentDialectTest.supportsIfNotExists } ?: ""} $sequenceName"

private val sequence = Sequence(
name = "my_sequence",
startWith = 1,
minValue = 1,
maxValue = 9223372036854775807
)
private val sequenceName by lazy { "custom_sequence" }

private val sequenceName = "custom_sequence"

private val tableWithoutAutoIncrement = object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").entityId()
private val tableWithoutAutoIncrement by lazy {
object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").entityId()
}
}

private val tableWithAutoIncrement = object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").autoIncrement().entityId()
private val tableWithAutoIncrement by lazy {
object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").autoIncrement().entityId()
}
}

private val tableWithAutoIncrementCustomSequence = object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").autoIncrement(sequence).entityId()
private val tableWithAutoIncrementCustomSequence by lazy {
object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").autoIncrement(sequence).entityId()
}
}

private val tableWithAutoIncrementSequenceName = object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").autoIncrement(sequenceName).entityId()
private val tableWithAutoIncrementSequenceName by lazy {
object : IdTable<Long>("test_table") {
override val id: Column<EntityID<Long>> = long("id").autoIncrement(sequenceName).entityId()
}
}
}

0 comments on commit 0cf78d8

Please sign in to comment.