Skip to content

Commit

Permalink
Check for existing sequence inside SchemaUtils instead of Table class
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichevjb committed Jun 3, 2024
1 parent e0ae914 commit 1e80a66
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 17 deletions.
12 changes: 10 additions & 2 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ public final class org/jetbrains/exposed/sql/CheckConstraint : org/jetbrains/exp
public fun equals (Ljava/lang/Object;)Z
public final fun getCheckName ()Ljava/lang/String;
public final fun getCheckOp ()Ljava/lang/String;
public fun getDdl ()Ljava/util/List;
public final fun getTableName ()Ljava/lang/String;
public fun hashCode ()I
public fun modifyStatement ()Ljava/util/List;
Expand All @@ -393,7 +394,7 @@ public final class org/jetbrains/exposed/sql/Column : org/jetbrains/exposed/sql/
public fun dropStatement ()Ljava/util/List;
public fun equals (Ljava/lang/Object;)Z
public fun getColumnType ()Lorg/jetbrains/exposed/sql/IColumnType;
public final fun getDdl ()Ljava/util/List;
public fun getDdl ()Ljava/util/List;
public final fun getDefaultValueFun ()Lkotlin/jvm/functions/Function0;
public final fun getForeignKey ()Lorg/jetbrains/exposed/sql/ForeignKeyConstraint;
public final fun getName ()Ljava/lang/String;
Expand Down Expand Up @@ -707,9 +708,14 @@ public final class org/jetbrains/exposed/sql/DatabaseKt {
public abstract interface class org/jetbrains/exposed/sql/DdlAware {
public abstract fun createStatement ()Ljava/util/List;
public abstract fun dropStatement ()Ljava/util/List;
public abstract fun getDdl ()Ljava/util/List;
public abstract fun modifyStatement ()Ljava/util/List;
}

public final class org/jetbrains/exposed/sql/DdlAware$DefaultImpls {
public static fun getDdl (Lorg/jetbrains/exposed/sql/DdlAware;)Ljava/util/List;
}

public final class org/jetbrains/exposed/sql/DecimalColumnType : org/jetbrains/exposed/sql/ColumnType {
public static final field Companion Lorg/jetbrains/exposed/sql/DecimalColumnType$Companion;
public fun <init> (II)V
Expand Down Expand Up @@ -933,6 +939,7 @@ public final class org/jetbrains/exposed/sql/ForeignKeyConstraint : org/jetbrain
public fun dropStatement ()Ljava/util/List;
public fun equals (Ljava/lang/Object;)Z
public final fun getCustomFkName ()Ljava/lang/String;
public fun getDdl ()Ljava/util/List;
public final fun getDeleteRule ()Lorg/jetbrains/exposed/sql/ReferenceOption;
public final fun getFkName ()Ljava/lang/String;
public final fun getFrom ()Ljava/util/LinkedHashSet;
Expand Down Expand Up @@ -1272,6 +1279,7 @@ public final class org/jetbrains/exposed/sql/Index : org/jetbrains/exposed/sql/D
public fun equals (Ljava/lang/Object;)Z
public final fun getColumns ()Ljava/util/List;
public final fun getCustomName ()Ljava/lang/String;
public fun getDdl ()Ljava/util/List;
public final fun getFilterCondition ()Lorg/jetbrains/exposed/sql/Op;
public final fun getFunctions ()Ljava/util/List;
public final fun getFunctionsTable ()Lorg/jetbrains/exposed/sql/Table;
Expand Down Expand Up @@ -2353,7 +2361,7 @@ public class org/jetbrains/exposed/sql/Table : org/jetbrains/exposed/sql/ColumnS
public fun fullJoin (Lorg/jetbrains/exposed/sql/ColumnSet;)Lorg/jetbrains/exposed/sql/Join;
public final fun getAutoIncColumn ()Lorg/jetbrains/exposed/sql/Column;
public fun getColumns ()Ljava/util/List;
public final fun getDdl ()Ljava/util/List;
public fun getDdl ()Ljava/util/List;
public final fun getForeignKeys ()Ljava/util/List;
public final fun getIndices ()Ljava/util/List;
public fun getPrimaryKey ()Lorg/jetbrains/exposed/sql/Table$PrimaryKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class Column<T>(
/** Appends the SQL representation of this column to the specified [queryBuilder]. */
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = TransactionManager.current().fullIdentity(this@Column, queryBuilder)

/** Returns the list of DDL statements that create this column. */
val ddl: List<String> get() = createStatement()

/** Returns the column name in proper case. */
fun nameInDatabaseCase(): String = name.inProperCase()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface DdlAware {

/** Returns the list of DDL statements that drops this object. */
fun dropStatement(): List<String>

/** Returns the list of DDL statements that create this DdlAware instance. */
val ddl: List<String> get() = createStatement()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,26 @@ object SchemaUtils {
val toCreate = sortTablesByReferences(tables.toList()).filterNot { it.exists() }
val alters = arrayListOf<String>()
return toCreate.flatMap { table ->
val (create, alter) = table.ddl.partition { it.startsWith("CREATE ") }
val (create, alter) = tableDdlWithoutExistingSequence(table).partition { it.startsWith("CREATE ") }
val indicesDDL = table.indices.flatMap { createIndex(it) }
alters += alter
create + indicesDDL
} + alters
}

private fun tableDdlWithoutExistingSequence(table: Table): List<String> {
val autoIncSeq = table.autoIncColumn?.autoIncColumnType?.autoincSeq
val sequenceExists = autoIncSeq?.let { currentDialect.sequenceExists(Sequence(it)) } ?: false

return table.ddl.filter { statement ->
if (sequenceExists && autoIncSeq != null) {
!statement.lowercase().startsWith("create sequence") || !statement.contains(autoIncSeq)
} else {
true
}
}
}

/** Creates the provided sequences, using a batch execution if [inBatch] is set to `true`. */
fun createSequence(vararg seq: Sequence, inBatch: Boolean = false) {
with(TransactionManager.current()) {
Expand Down Expand Up @@ -199,11 +212,13 @@ object SchemaUtils {
processed
}
}

is MariaDBDialect -> processed.trim('\'')
is MysqlDialect -> "_utf8mb4\\'${processed.trim('(', ')', '\'')}\\"
else -> processed.trim('\'')
}
}

column.columnType is ArrayColumnType<*> && dialect is PostgreSQLDialect -> {
(value as List<*>)
.takeIf { it.isNotEmpty() }
Expand All @@ -220,6 +235,7 @@ object SchemaUtils {
"ARRAY$processed"
} ?: processForDefaultValue(exp)
}

column.columnType is IDateColumnType -> {
val processed = processForDefaultValue(exp)
if (processed.startsWith('\'') && processed.endsWith('\'')) {
Expand All @@ -228,6 +244,7 @@ object SchemaUtils {
processed
}
}

else -> processForDefaultValue(exp)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1351,9 +1351,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {

// DDL statements

/** Returns the list of DDL statements that create this table. */
val ddl: List<String> get() = createStatement()

internal fun primaryKeyConstraint(): String? {
return primaryKey?.let { primaryKey ->
val tr = TransactionManager.current()
Expand Down Expand Up @@ -1425,7 +1422,6 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
maxValue = Long.MAX_VALUE
)
}
?.takeIf { !it.exists() }
?.createStatement()
.orEmpty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ 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 @@ -262,7 +264,13 @@ class DefaultsTest : DatabaseTestsBase() {
"${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" +
")"

assertEqualLists(arrayListOf(baseExpression), testTable.ddl)
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)

val id1 = testTable.insertAndGetId { }

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

assertEqualLists(arrayListOf(baseExpression), testTable.ddl)
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)

val id1 = testTable.insertAndGetId { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ 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 @@ -192,7 +194,16 @@ class JodaTimeDefaultsTest : JodaTimeBaseTest() {
"${"t4".inProperCase()} DATE${testTable.t4.constraintNamePart()} ${dtLiteral.itOrNull()}" +
")"

assertEqualLists(arrayListOf(baseExpression), testTable.ddl)
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)

val id1 = testTable.insertAndGetId { }

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

assertEqualLists(arrayListOf(baseExpression), testTable.ddl)
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)

val id1 = testTable.insertAndGetId { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ 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 @@ -260,7 +262,13 @@ class DefaultsTest : DatabaseTestsBase() {
"${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" +
")"

assertEqualLists(arrayListOf(baseExpression), testTable.ddl)
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)

val id1 = testTable.insertAndGetId { }

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

assertEqualLists(arrayListOf(baseExpression), testTable.ddl)
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)

val id1 = testTable.insertAndGetId { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class SequencesTests : DatabaseTestsBase() {
}

@Test
fun testABCDEF() {
fun testNoCreateStatementForExistingSequence() {
withDb {
if (currentDialectTest.supportsSequenceAsGeneratedKeys) {
val createSequencePrefix = "CREATE SEQUENCE"
Expand Down

0 comments on commit 1e80a66

Please sign in to comment.