Skip to content

Commit

Permalink
Fix addAutoPrimaryKey test (JetBrains#1418)
Browse files Browse the repository at this point in the history
* Remove part which is actually doesn't test anything (except of SQLITE itself)

* Split addAutoPrimaryKey into two tests: for H2 and other DBs (to avoid its skipping);
Add H2_MYSQL as a testing target;
Restore initial DB state in case of failure on H2
  • Loading branch information
naftalmm authored Jan 15, 2022
1 parent 9e7aa44 commit e286051
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.jetbrains.exposed.sql.tests.h2

import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.inProperCase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transactionManager
Expand Down Expand Up @@ -93,6 +96,28 @@ class H2Tests : DatabaseTestsBase() {
}
}

@Test
fun addAutoPrimaryKey() {
val tableName = "Foo"
val initialTable = object : Table(tableName) {
val bar = text("bar")
}
val t = IntIdTable(tableName)

withDb(listOf(TestDB.H2, TestDB.H2_MYSQL)) {
try {
SchemaUtils.createMissingTablesAndColumns(initialTable)
assertEquals("ALTER TABLE ${tableName.inProperCase()} ADD ${"id".inProperCase()} ${t.id.columnType.sqlType()}", t.id.ddl.first())
assertEquals("ALTER TABLE ${tableName.inProperCase()} ADD CONSTRAINT pk_$tableName PRIMARY KEY (${"id".inProperCase()})", t.id.ddl[1])
assertEquals(1, currentDialectTest.tableColumns(t)[t]!!.size)
SchemaUtils.createMissingTablesAndColumns(t)
assertEquals(2, currentDialectTest.tableColumns(t)[t]!!.size)
} finally {
SchemaUtils.drop(t)
}
}
}

class WrappedTransactionManager(val transactionManager: TransactionManager) :
TransactionManager by transactionManager

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,6 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {
}
val t = IntIdTable(tableName)

withDb(TestDB.H2) {
SchemaUtils.createMissingTablesAndColumns(initialTable)
assertEquals("ALTER TABLE ${tableName.inProperCase()} ADD ${"id".inProperCase()} ${t.id.columnType.sqlType()}", t.id.ddl.first())
assertEquals("ALTER TABLE ${tableName.inProperCase()} ADD CONSTRAINT pk_$tableName PRIMARY KEY (${"id".inProperCase()})", t.id.ddl[1])
assertEquals(1, currentDialectTest.tableColumns(t)[t]!!.size)
SchemaUtils.createMissingTablesAndColumns(t)
assertEquals(2, currentDialectTest.tableColumns(t)[t]!!.size)
SchemaUtils.drop(t)
}

withDb(TestDB.SQLITE) {
try {
SchemaUtils.createMissingTablesAndColumns(t)
assertFalse(db.supportsAlterTableWithAddColumn)
} catch (e: SQLException) {
// SQLite doesn't support
} finally {
SchemaUtils.drop(t)
}
}

withTables(excludeSettings = listOf(TestDB.H2, TestDB.H2_MYSQL, TestDB.SQLITE), tables = arrayOf(initialTable)) {
assertEquals("ALTER TABLE ${tableName.inProperCase()} ADD ${"id".inProperCase()} ${t.id.columnType.sqlType()} PRIMARY KEY", t.id.ddl)
assertEquals(1, currentDialectTest.tableColumns(t)[t]!!.size)
Expand Down

0 comments on commit e286051

Please sign in to comment.