Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-277 statementsRequiredToActualizeScheme does not check s… #2096

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1364,15 +1364,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
}

override fun createStatement(): List<String> {
val createSequence = autoIncColumn?.autoIncColumnType?.autoincSeq?.let {
Sequence(
it,
startWith = 1,
minValue = 1,
maxValue = Long.MAX_VALUE
).createStatement()
}.orEmpty()

val addForeignKeysInAlterPart = SchemaUtils.checkCycle(this) && currentDialect !is SQLiteDialect

val foreignKeyConstraints = foreignKeys
Expand Down Expand Up @@ -1422,7 +1413,21 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
emptyList()
}

return createSequence + createTable + createConstraint
return createAutoIncColumnSequence() + createTable + createConstraint
}

private fun createAutoIncColumnSequence(): List<String> {
return autoIncColumn?.autoIncColumnType?.autoincSeq?.let {
Sequence(
it,
startWith = 1,
minValue = 1,
maxValue = Long.MAX_VALUE
)
}
?.takeIf { !it.exists() }
?.createStatement()
.orEmpty()
}

override fun modifyStatement(): List<String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.tests.shared.expectException
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.h2Mode
import org.junit.Test
import java.time.*
import kotlin.test.assertEquals
Expand Down Expand Up @@ -264,13 +262,7 @@ class DefaultsTest : DatabaseTestsBase() {
"${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect || currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
arrayListOf("CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807", baseExpression)
} else {
arrayListOf(baseExpression)
}

assertEqualLists(expected, testTable.ddl)
assertEqualLists(arrayListOf(baseExpression), testTable.ddl)

val id1 = testTable.insertAndGetId { }

Expand Down Expand Up @@ -430,18 +422,7 @@ class DefaultsTest : DatabaseTestsBase() {
"${"t3".inProperCase()} $timestampWithTimeZoneType${testTable.t3.constraintNamePart()} ${CurrentTimestampWithTimeZone.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle
) {
arrayListOf(
"CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807",
baseExpression
)
} else {
arrayListOf(baseExpression)
}

assertEqualLists(expected, testTable.ddl)
assertEqualLists(arrayListOf(baseExpression), testTable.ddl)

val id1 = testTable.insertAndGetId { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.tests.shared.expectException
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.h2Mode
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.junit.Test
Expand Down Expand Up @@ -194,16 +192,7 @@ class JodaTimeDefaultsTest : JodaTimeBaseTest() {
"${"t4".inProperCase()} DATE${testTable.t4.constraintNamePart()} ${dtLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect || currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
arrayListOf(
"CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807",
baseExpression
)
} else {
arrayListOf(baseExpression)
}

assertEqualLists(expected, testTable.ddl)
assertEqualLists(arrayListOf(baseExpression), testTable.ddl)

val id1 = testTable.insertAndGetId { }

Expand Down Expand Up @@ -400,18 +389,7 @@ class JodaTimeDefaultsTest : JodaTimeBaseTest() {
"${"t3".inProperCase()} $timestampWithTimeZoneType${testTable.t3.constraintNamePart()} ${CurrentDateTime.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle
) {
arrayListOf(
"CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807",
baseExpression
)
} else {
arrayListOf(baseExpression)
}

assertEqualLists(expected, testTable.ddl)
assertEqualLists(arrayListOf(baseExpression), testTable.ddl)

val id1 = testTable.insertAndGetId { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.tests.shared.expectException
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.h2Mode
import org.junit.Test
import java.time.OffsetDateTime
import java.time.ZoneId
Expand Down Expand Up @@ -262,13 +260,7 @@ class DefaultsTest : DatabaseTestsBase() {
"${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect || currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
arrayListOf("CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807", baseExpression)
} else {
arrayListOf(baseExpression)
}

assertEqualLists(expected, testTable.ddl)
assertEqualLists(arrayListOf(baseExpression), testTable.ddl)

val id1 = testTable.insertAndGetId { }

Expand Down Expand Up @@ -434,18 +426,7 @@ class DefaultsTest : DatabaseTestsBase() {
"${"t3".inProperCase()} $timestampWithTimeZoneType${testTable.t3.constraintNamePart()} ${CurrentTimestampWithTimeZone.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle
) {
arrayListOf(
"CREATE SEQUENCE t_id_seq START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775807",
baseExpression
)
} else {
arrayListOf(baseExpression)
}

assertEqualLists(expected, testTable.ddl)
assertEqualLists(arrayListOf(baseExpression), testTable.ddl)

val id1 = testTable.insertAndGetId { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class CreateTableTests : DatabaseTestsBase() {
fkName = fkName
)
}
withTables(parent, child) {
withDb {
val t = TransactionManager.current()
val expected = listOfNotNull(
child.autoIncColumn?.autoIncColumnType?.autoincSeq?.let {
Expand Down Expand Up @@ -338,7 +338,7 @@ class CreateTableTests : DatabaseTestsBase() {
onDelete = ReferenceOption.NO_ACTION,
)
}
withTables(parent, child) {
withDb {
val expected = "CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(child)} (" +
"${child.columns.joinToString { it.descriptionDdl(false) }}," +
" CONSTRAINT ${"fk_Child_parent_id__id".inProperCase()}" +
Expand All @@ -360,7 +360,7 @@ class CreateTableTests : DatabaseTestsBase() {
onDelete = ReferenceOption.NO_ACTION,
)
}
withTables(parent, child) {
withDb {
val expected = "CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(child)} (" +
"${child.columns.joinToString { it.descriptionDdl(false) }}," +
" CONSTRAINT ${"fk_Child2_parent_id__id".inProperCase()}" +
Expand All @@ -386,7 +386,7 @@ class CreateTableTests : DatabaseTestsBase() {
fkName = fkName
)
}
withTables(parent, child) {
withDb {
val t = TransactionManager.current()
val expected = listOfNotNull(
child.autoIncColumn?.autoIncColumnType?.autoincSeq?.let {
Expand Down Expand Up @@ -421,7 +421,7 @@ class CreateTableTests : DatabaseTestsBase() {
fkName = fkName
)
}
withTables(parent, child) {
withDb {
val t = TransactionManager.current()
val expected = listOfNotNull(
child.autoIncColumn?.autoIncColumnType?.autoincSeq?.let {
Expand Down Expand Up @@ -459,7 +459,7 @@ class CreateTableTests : DatabaseTestsBase() {
fkName = fkName
)
}
withTables(parent, child) {
withDb {
val t = TransactionManager.current()
val expected = listOfNotNull(
child.autoIncColumn?.autoIncColumnType?.autoincSeq?.let {
Expand Down Expand Up @@ -503,7 +503,7 @@ class CreateTableTests : DatabaseTestsBase() {
)
}
}
withTables(parent, child) { testDb ->
withDb { testDb ->
val t = TransactionManager.current()
val updateCascadePart = if (testDb != TestDB.ORACLE) " ON UPDATE CASCADE" else ""
val expected = listOfNotNull(
Expand Down Expand Up @@ -550,7 +550,7 @@ class CreateTableTests : DatabaseTestsBase() {
)
}
}
withTables(parent, child) {
withDb {
val t = TransactionManager.current()
val expected = listOfNotNull(
child.autoIncColumn?.autoIncColumnType?.autoincSeq?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class SequencesTests : DatabaseTestsBase() {
@Test
Expand Down Expand Up @@ -198,6 +199,29 @@ class SequencesTests : DatabaseTestsBase() {
}
}

@Test
fun testABCDEF() {
obabichevjb marked this conversation as resolved.
Show resolved Hide resolved
withDb {
if (currentDialectTest.supportsSequenceAsGeneratedKeys) {
val createSequencePrefix = "CREATE SEQUENCE"

assertNotNull(SchemaUtils.createStatements(DeveloperWithAutoIncrementBySequence).find { it.startsWith(createSequencePrefix) })

SchemaUtils.create(DeveloperWithAutoIncrementBySequence)

// Remove table without removing sequence
exec("DROP TABLE ${DeveloperWithAutoIncrementBySequence.nameInDatabaseCase()}")

assertNull(SchemaUtils.createStatements(DeveloperWithAutoIncrementBySequence).find { it.startsWith(createSequencePrefix) })
assertNull(SchemaUtils.statementsRequiredToActualizeScheme(DeveloperWithAutoIncrementBySequence).find { it.startsWith(createSequencePrefix) })

// Clean up: create table and drop it for removing sequence
SchemaUtils.create(DeveloperWithAutoIncrementBySequence)
SchemaUtils.drop(DeveloperWithAutoIncrementBySequence)
}
}
}

private object Developer : Table() {
val id = integer("id")
var name = varchar("name", 25)
Expand Down
Loading