2016-09-20 84 views
1

我需要使用我的舊代碼與油滑3,但它不工作,我不知道該怎麼做。油滑3截斷表測試

我的舊代碼:

database.withSession { implicit s: Session => 
    StaticQuery.updateNA("SET foreign_key_checks = 0").execute() 
    val q = StaticQuery.query[String, String](
    "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?" 
).apply(dbname) 
    for (table <- q) { 
    StaticQuery.updateNA("TRUNCATE " + dbname + "." + table).execute() 
    } 
    StaticQuery.updateNA("SET foreign_key_checks = 1").execute() 
} 

新的代碼,包括漂亮的3:

val a = for { 
    _ <- sqlu"SET foreign_key_checks = 0" 
    s <- sql"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ${ dbname }".as[String] 
    _ <- DBIO.sequence(s.map(table => sqlu"TRUNCATE #${MySQLDriver.quoteIdentifier(dbname)}.#${MySQLDriver.quoteIdentifier(table)}").map(x => DBIO.successful(x))) 
    _ <- DBIO.successful(sql"SET foreign_key_checks = 1") 
} yield() 

Await.result(db.run(a), scala.concurrent.duration.Duration(1000, SECONDS)) 

錯誤:

com.googlecode.flyway.core.api.FlywayException: Unable to lock table `myLocalDB_test`.`schema_version 
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'myLocalDB_test.schema_version' doesn't exist 

回答

0

我發現在Github上的例子,現在看來工作,你可以從here

檢查我改成了這樣:

val truncatesFuture = db.run(
     sql"""SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ${ dbname }""".as[String] 
    ).map { 
     _.map { case (table) => SQLActionBuilder(List(s"TRUNCATE TABLE $table"), SetUnit).asUpdate } 
    } 

Await.result(truncatesFuture.flatMap(
    truncates => 
    db.run(
     DBIO.sequence(
     List(
      List(sqlu"""SET FOREIGN_KEY_CHECKS = 0;"""), 
      truncates, 
      List(sqlu"""SET FOREIGN_KEY_CHECKS = 1;""") 
     ).flatten 
    ) 
    ) 
), scala.concurrent.duration.Duration(5000, SECONDS)) 
1

好像你想從MTable.getTables得到的表。

@ import slick.jdbc.meta.MTable 
import slick.jdbc.meta.MTable 
@ MTable.getTables 
res91: profile.BasicStreamingAction[Vector[MTable], MTable, Effect.Read] = [email protected] 
@ db.run(res91) 
res92: concurrent.Future[Vector[MTable]] = Success(Vector(MTable(MQName(TEST1.PUBLIC.COFFEES),TABLE,,None,None,None), MTable(MQName(TEST1.PUBLIC.SUPPLIERS),TABLE,,None,None,None)))