-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: EXPOSED-536 Short column allows out-of-range values (#2231)
For SQLite and Oracle, inserting an out-of-range value using raw SQL with `exec` would work even though it shouldn't. Short stores values between -32768 and 32767. For Oracle and H2 Oracle, Short data type was changed from SMALLINT to NUMBER(5) because SMALLINT is stored as NUMBER(38) in the database and takes up unnecessary storage. Another reason for this change is that this is a precursor to the column type change detection feature for migration, where it will be useful to have the column type be more specific than NUMBER(38). A check constraint was added (that applies only to SQLite, Oracle and H2_Oracle) to ensure that no out-of-range values are stored.
- Loading branch information
Showing
4 changed files
with
70 additions
and
8 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
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
42 changes: 42 additions & 0 deletions
42
...s/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/NumericColumnTypesTests.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,42 @@ | ||
package org.jetbrains.exposed.sql.tests.shared.types | ||
|
||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.insert | ||
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase | ||
import org.jetbrains.exposed.sql.tests.TestDB | ||
import org.jetbrains.exposed.sql.tests.shared.assertEquals | ||
import org.jetbrains.exposed.sql.tests.shared.assertFailAndRollback | ||
import org.jetbrains.exposed.sql.tests.shared.assertTrue | ||
import org.junit.Test | ||
|
||
class NumericColumnTypesTests : DatabaseTestsBase() { | ||
@Test | ||
fun testShortAcceptsOnlyAllowedRange() { | ||
val testTable = object : Table("test_table") { | ||
val short = short("short") | ||
} | ||
|
||
withTables(testTable) { testDb -> | ||
val columnName = testTable.short.nameInDatabaseCase() | ||
val ddlEnding = when (testDb) { | ||
TestDB.SQLITE, in TestDB.ALL_ORACLE_LIKE -> "CHECK ($columnName BETWEEN ${Short.MIN_VALUE} and ${Short.MAX_VALUE}))" | ||
else -> "($columnName ${testTable.short.columnType} NOT NULL)" | ||
} | ||
assertTrue(testTable.ddl.single().endsWith(ddlEnding, ignoreCase = true)) | ||
|
||
testTable.insert { it[short] = Short.MIN_VALUE } | ||
testTable.insert { it[short] = Short.MAX_VALUE } | ||
assertEquals(2, testTable.select(testTable.short).count()) | ||
|
||
val tableName = testTable.nameInDatabaseCase() | ||
assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { | ||
val outOfRangeValue = Short.MIN_VALUE - 1 | ||
exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") | ||
} | ||
assertFailAndRollback(message = "Out-of-range error (or CHECK constraint violation for SQLite & Oracle)") { | ||
val outOfRangeValue = Short.MAX_VALUE + 1 | ||
exec("INSERT INTO $tableName ($columnName) VALUES ($outOfRangeValue)") | ||
} | ||
} | ||
} | ||
} |