From: Jay Stanley <beansboy@cruzio.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: PostgreSQL DB in prod, test, debug
Date: Fri, 16 Feb 2024 00:25:10 +0000
Message-ID: <29aada4e3b72a39db3f50bc299ce9880@cruzio.com> (raw)
In-Reply-To: <20240215235152.va7wohn5ftcxi5a4@hjp.at>
References: <NaMscvF8PaDKNcHJGR28wV7Kljc6kw4y0agCt2E3qI8hkUYUSUyj_beL825S4GfK2SjTuNVaHmRkW30ihuYmobAitd1uuLNis8nCkFCZT-0=@protonmail.com>
<7304F877-D043-4395-867E-014F48EB71ED@yesql.se>
<20240215235152.va7wohn5ftcxi5a4@hjp.at>
On 2024-02-15 23:51, Peter J. Holzer wrote:
> On 2024-02-14 11:25:48 +0100, Daniel Gustafsson wrote: On 14 Feb 2024,
> at 10:59, Simon Connah <simon.n.connah@protonmail.com> wrote:
> This is probably a stupid question so I apologies in advance.
> There is no such thing.
>
> What I think is the best way to do this is to do a pg_dump of the
> database (using the --schema-only flag) and then load it into a test
> only database that gets created at the start of the unit tests and
> destroyed at the end. The automated tests will insert, update,
> delete and select data to test if it all still works.
> If the source of truth for your schema is the database, then sure. If
> the
> source of truth is a .sql file in your source code repository then you
> should
> use that.
I sort of do both for one of my projects:
I originally created the SQL script by running pg_dump on a manually
constructed test database (I do have code to create the empty schema,
but I had to insert the test data manually). That script went into the
git repo.
The test files all contain a fixture which drops and recreates the test
database using that sql file.
When I add a new test case I try to make do with the existing test data.
When I need additional data for a new test case, I create a new pristine
test database using the sql file, add the new data, and then create a
new sql file using sql_dump which is then committed with the test cases.
Same for migrations: If I need to migrate the schema, I run the
migration on the test database, then dump and commit it.
This project is small enough (86 tests in 10 files) that all test cases
can use the same test data. However, I could easily use different test
data for different tests.
hp
I had a very similar issue a few years ago; our large-ish codebase needs
a lot of suites tests (about 100) to run daily against specific database
conditions -- each test involved specific data in around 50 tables. At
first we had 'source of truth' sql scripts checked into git, and with
jenkins would run each to create a database for the test, load it with
the exact data needed for that suite of tests, run the tests and record
them, then drop the database.
This worked fine for a few tests but became unmanageable as more tests
were added and the data volume increased. Instead, I created a 'master'
script that creates one _database template_ for each suite of tests by
creating a blank database and running those same build scripts in git,
then disconnecting from that target database and switching it to become
a template. This was re-done very infrequently -- only when we wanted
to use a different testing database, or needed to refresh test content,
drop tests or add more tests. That's right - we have about 100
templates; I've found copying a template is FAR faster than reloading it
from SQL.
When each test is run, it creates a new database from the template
appropriate for that suite of tests. When the test suite completes, the
new database is dropped. This sped up our tests by at least 2 orders of
magnitude, and it was in a way more reliable because each test gets a
binary copy of the test database including exactly how vacuumed each
table is, exact state of the indexes, every block in the same place,
etc. Results were definitely more consistent in how long they ran.
Note that while a database is in template mode, it cannot be changed
(unless a DBA switches it back to non-template-mode).
This has been in production for a few years, in multiple projects, and
hasn't really hit any issues; the one cavaet is that you can't switch a
database to 'template mode' if anything's logged into the target.
According to the doc, I don't see an upper limit for the number of
template databases but there must be one - I haven't tested it with
1000's of templates.
See doc for 'create database DBNAME template TEMPLATENAME' as well as
'alter database DBNAME set datistemplate = true|false'.
- jay stanley
- https://cyc.com; The Next Generation of Enterprise AI
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: beansboy@cruzio.com
Subject: Re: PostgreSQL DB in prod, test, debug
In-Reply-To: <29aada4e3b72a39db3f50bc299ce9880@cruzio.com>
References: <NaMscvF8PaDKNcHJGR28wV7Kljc6kw4y0agCt2E3qI8hkUYUSUyj_beL825S4GfK2SjTuNVaHmRkW30ihuYmobAitd1uuLNis8nCkFCZT-0=@protonmail.com> <7304F877-D043-4395-867E-014F48EB71ED@yesql.se> <20240215235152.va7wohn5ftcxi5a4@hjp.at> <29aada4e3b72a39db3f50bc299ce9880@cruzio.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox