clone a postgresql database for testing cleanly | Down Home Country Coding With Scott Selikoff and Jeanne Boyarsky (original) (raw)

I’m looking at writing integration tests for the back end of JavaRanch‘s JForum install.

A few “pesky” requirements/constraints

Strategy

I think the best strategy is to create a second database just for testing. The JForum database would remain untouched and a jforum_integration_test database can be created for the tests. dbUnit can control the state of that special database.

The problem

Before I even start thinking about dbUnit, I did a proof of concept to ensure I could create a new database from scratch using the command line. Creating a database is the easy part. The “hard” part is that JForum doesn’t come with a schema. It comes with an installation servlet that creates the schema. While few people will be creating a schema for JForum, the technique I used applies elsewhere.

The procedure “before”

  1. Start up the JForum war
  2. Go to the JForum install URL and enter some information which creates the tables
  3. Run the JavaRanch customizations.

How to clone a database for which you only have a partial script

  1. Create an empty database
    createdb jforum_integration_test
  2. Arrive at the base schema
    1. Go the JForum installation URL
    2. Enter the information to create the tables
  3. Export the schema thus far
    pg_dump -U postgres jforum_integration_test > c:\temp\postgres.sql
  4. Provide instructions for the rest of the sql which were created by our developers.

How to import

Now for the easy part!

Importing this dump is a matter of a single command:

psql -U postgres jforum < "pathToWorkspace\JForum\javaranch-docs\deployment\file.ddl"

Lessons learned after

The next day I learned that this wasn’t enough. We also needed some test data from the server. I ran this a few times to get the relevant test data.

pg_dump --data-only --inserts -U user -W database --file roles --table tableName
Conclusion

My next step will be to actually configure dbUnit against this new database and start writing tests.