Skip to content

Commit

Permalink
Add quoting of table names and fk in ALTER TABLE for MySQL
Browse files Browse the repository at this point in the history
Closes #1471: Drop / dropStatements for MySQL are not escaping reserved words (like "order")
  • Loading branch information
mkotsbakws committed Apr 5, 2016
1 parent 3a50a33 commit 1f49326
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 deletions slick-testkit/src/test/scala/slick/test/lifted/MySQLDDLTest.scala
@@ -0,0 +1,37 @@
package slick.test.lifted

import org.junit.Test
import org.junit.Assert._

/** Test case for the MySQL SQL DDL overrides */
class MysqlDDLTest {
import slick.jdbc.MySQLProfile.api._

@Test def testTablenameEscaped {
class T(tag: Tag) extends Table[Int](tag, "mytable") {
def id = column[Int]("id", O.PrimaryKey)

def * = id
}
val ts = TableQuery[T]

class T2(tag: Tag) extends Table[Int](tag, "mytable2") {
def id = column[Int]("id", O.PrimaryKey)

def testFk = column[Int]("test_fk")
def fk = foreignKey("t_test_fk", testFk, ts)(_.id)

def * = id
}
val ts2 = TableQuery[T2]

val s1 = ts2.schema.createStatements.toList
assertTrue("DDL (create) must contain any SQL statements", s1.nonEmpty)
s1.foreach(s => assertTrue("DDL (create) uses escaped table name: " + s, s contains "`mytable2`"))
assertTrue("Fk name must be escaped", s1.exists(_.contains("`t_test_fk`")))

val s2 = ts2.schema.dropStatements.toList
s2.foreach(s => assertTrue("DDL (drop) uses escaped table name: " + s, s contains "`mytable2`"))
assertTrue("Fk name must be escaped", s2.exists(_.contains("`t_test_fk`")))
}
}
4 changes: 2 additions & 2 deletions slick/src/main/scala/slick/jdbc/MySQLProfile.scala
Expand Up @@ -194,10 +194,10 @@ trait MySQLProfile extends JdbcProfile { profile =>

class TableDDLBuilder(table: Table[_]) extends super.TableDDLBuilder(table) {
override protected def dropForeignKey(fk: ForeignKey) = {
"ALTER TABLE " + table.tableName + " DROP FOREIGN KEY " + fk.name
"ALTER TABLE " + quoteIdentifier(table.tableName) + " DROP FOREIGN KEY " + quoteIdentifier(fk.name)
}
override protected def dropPrimaryKey(pk: PrimaryKey): String = {
"ALTER TABLE " + table.tableName + " DROP PRIMARY KEY"
"ALTER TABLE " + quoteIdentifier(table.tableName) + " DROP PRIMARY KEY"
}
}

Expand Down

0 comments on commit 1f49326

Please sign in to comment.