Search code examples
scalasorm

How to drop and create tables on SORM?


In order to make isolated tests, it would be interesting to drop and create tables or clean them before each test. Any easy way to do it with SORM?


Solution

  • You can create new instances for each test with initMode set to DropAllCreate and free all instance's resources after, using the close() method.

    A custom function like the following might be helpful to you:

    def withDb ( f : Instance => () ) {
      val db = new Instance ( ..., initMode = InitMode.DropAllCreate )
      f(db)
      db.close()
    }
    

    Using it it'll be easy to always work in a context of a freshly created instance:

    withDb { db =>
      db.save(...)
      db.query[...](...)
    }