3 ms·
There is a much better way to reset the database: Postgres has template databases. You can clone a new database for each test like this: CREATE DATABASE test_r
by stickfigure 5y ago
There is a much better way to reset the database: Postgres has template databases. You can clone a new database for each test like this:
CREATE DATABASE test_run_1234 TEMPLATE "vanilla_database";
It's fast. Also, truncating tables doesn't reset the database; there is a lot of "static" data initialized in my migration scripts. Rerunning the migrations would be very slow.
In CI, my template database is initialized with migration scripts at the beginning of each suite run. Locally, I reset the template database (and purge the generated test databases) by running a script manually.
- grogers 5y agoWow, that's a fantastic feature for development! I remember once we explored various approaches to resetting the DB, because resetting the DB between tests was about half the time of the test suite. In our case the static data wasn't huge so just truncating every table then re-adding the static data was fast enough. I think originally it was doing something like a mysqldump restore.