9

I am using slick 3 and I am trying to perform some integration tests with some inserts, some code that uses the db and then I want to rollback all the insert or deletion at the end of the test itself but I cannot find any documentation about it.

Is it possible? How can I achieve it?

Matroska
  • 6,885
  • 14
  • 63
  • 99
  • 1
    See here : http://stackoverflow.com/questions/34905455/how-can-i-roll-back-an-integration-test-with-slick-3-specs2 Also take a look at this discussion . They are discussing the same issye you are facing. – Som Bhattacharyya Mar 29 '16 at 11:15
  • Did you get any help ? – Som Bhattacharyya Apr 02 '16 at 13:10
  • Yes, it seems the best answer I got. Unfortunately it seems that slick is a great library but incomplete for some features. – Matroska Apr 02 '16 at 13:13
  • @Matroska Stephen's answer is correct, see here: https://github.com/slick/slick/commit/6caaea3a8a888d54dc51463bc0e1725191b9721a (documentation added as part of 3.2, but IIUC correct since 3.0) – nafg Nov 13 '16 at 18:16

2 Answers2

2

You need to use . transactionally around the DBIOAction

eg

val a = (for {
  ns <- coffees.filter(_.name.startsWith("ESPRESSO")).map(_.name).result
  _ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally

val f: Future[Unit] = db.run(a)

For more see http://slick.typesafe.com/doc/3.1.1/dbio.html#transactions-and-pinned-sessions

Stephen
  • 4,228
  • 4
  • 29
  • 40
  • 4
    This is not correct, transactionally will provide a rollback when anything inside the DBIOAction fails, but this will not provide a rollback feature after the integration tests have finished. – Laurence Bird Apr 04 '16 at 09:36
  • @LaurenceBird it is correct, what you have to do is run everything inside DBIOs and the last action should be a DBIO.failed – nafg Nov 13 '16 at 18:14
  • @nafg -- can you give an example of that as an answer? – Michael Lorton Jan 05 '17 at 20:31
1

I can advice to drop and create table schema before and after test using BeforeAndAfter scala-test trait with next code:

def createTable(): Future[Unit] = {
        db.run(DBIO.seq(
          MTable.getTables.map(tables =>
            if (!tables.exists(_.name.name == table.baseTableRow.tableName))
              db.run(table.schema.create)
          )
        ))
}

def dropTable(): Future[Unit] = db.run(table.schema.drop)
  • This, or alternatively if you already have the db present and cannot drop the tables you will have to keep track of the rows inserted and delete them in ther same manner – Laurence Bird Apr 04 '16 at 09:37