Create a fresh logical database from a template approach
masterThis approach involves creating a database template once, and then having each test create a new logical database cloned from that template. This can be done using helpers like github.com/peterldowns/pgtestdb or via database-specific SQL commands.
Pros:
- High isolation: the physical instance is shared, but logical databases are separate.
- Fast: assuming the template cloning process is efficient.
- Good for parallel execution.
Cons:
- Variable speed: cloning speed depends on the complexity of the template.
- Setup overhead: requires preparing a template, creating test databases from it, and cleaning them up.
- Engine specific: requires a database-specific library or manual implementation of cloning logic.
-- create and prepare a template database
CREATE DATABASE dbname TEMPLATE template;
-- use your dbname database during test