Skip to content

Commit

Permalink
fix: EXPOSED-161 SQL Server syntax incorrectly allows CASCADE with dr…
Browse files Browse the repository at this point in the history
…opSchema (#1850)

* fix: EXPOSED-161 SQL Server syntax incorrectly allows CASCADE with dropSchema

Using dropSchema() with cascade set to true in SQL Server throws a syntax exception
because cascade is not supported.

Edit syntax to remove cascade.
Add unit test.

Add KDocs comment about cascade flag
  • Loading branch information
bog-walk authored Sep 6, 2023
1 parent a0ab662 commit 4b5b04a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,12 @@ object SchemaUtils {
* **Note** that when you are using Mysql or MariaDB, this will fail if you try to drop a schema that
* contains a table that is referenced by a table in another schema.
*
* @sample org.jetbrains.exposed.sql.tests.shared.SchemaTests
* @sample org.jetbrains.exposed.sql.tests.shared.SchemaTests.testDropSchemaWithCascade
*
* @param schemas the names of the schema
* @param cascade flag to drop schema and all of its objects and all objects that depend on those objects.
* You don't have to specify this option when you are using Mysql or MariaDB
* because whether you specify it or not, all objects in the schema will be dropped.
* **Note** This option is not supported by MySQL, MariaDB, or SQL Server, so all objects in the schema will be
* dropped regardless of the flag's value.
* @param inBatch flag to perform schema creation in a single batch
*/
fun dropSchema(vararg schemas: Schema, cascade: Boolean = false, inBatch: Boolean = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,7 @@ open class SQLServerDialect : VendorDialect(dialectName, SQLServerDataTypeProvid
appendIfNotNull(" AUTHORIZATION ", schema.authorization)
}

override fun dropSchema(schema: Schema, cascade: Boolean): String = buildString {
append("DROP SCHEMA ", schema.identifier)

if (cascade) {
append(" CASCADE")
}
}
override fun dropSchema(schema: Schema, cascade: Boolean): String = "DROP SCHEMA ${schema.identifier}"

override fun createIndex(index: Index): String {
if (index.functions != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Assume
Expand Down Expand Up @@ -56,6 +55,20 @@ class SchemaTests : DatabaseTestsBase() {
}
}

@Test
fun testDropSchemaWithCascade() {
withDb {
if (currentDialect.supportsCreateSchema) {
val schema = Schema("TEST_SCHEMA")
SchemaUtils.createSchema(schema)
assertTrue(schema.exists())

SchemaUtils.dropSchema(schema, cascade = true)
assertFalse(schema.exists())
}
}
}

@Test
fun `table references table with same name in other database in mysql`() {
withDb(listOf(TestDB.MYSQL, TestDB.MARIADB)) {
Expand Down

0 comments on commit 4b5b04a

Please sign in to comment.