Matroska
Matroska

Reputation: 6923

Slick 3 session with rollback

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?

Upvotes: 9

Views: 2036

Answers (2)

Stephen
Stephen

Reputation: 4296

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

Upvotes: 2

user1659388
user1659388

Reputation:

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)

Upvotes: 1

Related Questions