From 5d04ad34abdb3df4be8809b28f66c6605dab255c Mon Sep 17 00:00:00 2001 From: Chantal Loncle <82039410+bog-walk@users.noreply.github.com> Date: Wed, 30 Aug 2023 18:27:52 -0400 Subject: [PATCH 1/3] fix: EXPOSED-161 SQL Server syntax incorrectly allows CASCADE with dropSchema Using dropSchema() with cascade set to true in SQL Server throws a syntax excep> because cascade is not supported. Edit syntax to remove cascade. Add unit test. --- .../exposed/sql/vendors/SQLServerDialect.kt | 8 +------- .../exposed/sql/tests/shared/SchemaTests.kt | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/SQLServerDialect.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/SQLServerDialect.kt index 14d242e385..5d12fb3d25 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/SQLServerDialect.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/SQLServerDialect.kt @@ -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) { diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt index c298f381c5..9667585124 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt @@ -56,6 +56,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)) { From 7d095a2b0093a2d50815cd0e6e8e80f86103ad16 Mon Sep 17 00:00:00 2001 From: Chantal Loncle <82039410+bog-walk@users.noreply.github.com> Date: Wed, 30 Aug 2023 18:46:29 -0400 Subject: [PATCH 2/3] fix: EXPOSED-161 SQL Server syntax incorrectly allows CASCADE with dropSchema Remove unused import --- .../kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt index 9667585124..ef84442132 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/SchemaTests.kt @@ -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 From e05d1a08e726582e78083eaea0181c82b0996b06 Mon Sep 17 00:00:00 2001 From: Chantal Loncle <82039410+bog-walk@users.noreply.github.com> Date: Wed, 6 Sep 2023 06:55:24 -0400 Subject: [PATCH 3/3] fix: EXPOSED-161 SQL Server syntax incorrectly allows CASCADE with dropSchema Add KDocs comment about cascade flag --- .../main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt index 30014fd7fe..3a351348c7 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt @@ -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) {