testing - Grails - Recreate database schema for integration test -
is there convenient way force grails / hibernate recreate database schema integration test?
if add following in datasource.groovy
empty database created before integration tests run:
environments { test { datasource { dbcreate = "create" } } }
by default each integration test executes within transaction rolled-back @ end of test, unless you're not using default behaviour there shouldn't need programatically recreate database.
update
based on comment, seems want recreate schema before integration tests. in case, way can think of, run
- drop , recreate schema
- use grails schema-export import fresh schema
class myintegrationtest { sessionfactory sessionfactory /** * helper executing sql statements * @param jdbcwork closure passed <tt>sql</tt> object used execute jdbc statements */ private dojdbcwork(closure jdbcwork) { sessionfactory.currentsession.dowork( new work() { @override void execute(connection connection) throws sqlexception { // not close sql instance ourselves sql sql = new sql(connection) jdbcwork(sql) } } ) } private recreateschema() { dojdbcwork {sql sql -> // use sql object drop database , create new blank database // following might work mysql sql.execute("drop database my-schema") sql.execute("create database my-schema") } // generate ddl , import // there must better way execute grails command within // integration test, unfortunately don't know 'grails test schema-export export'.execute() } @test void mytestmethod() { recreateschema() // test } }
first , foremost, code untested, treat deep suspicion , low expectations. secondly, may need change default transational behaviour of integration tests (with @transactional
) in order work.
Comments
Post a Comment