From: btober@computer.org <btober@broadstripe.net>
To: Tim Cross <theophilusx@gmail.com>
Cc: Jeremy Finzel <finzelj@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Enforce primary key on every table during dev?
Date: Wed, 28 Feb 2018 16:29:52 -0500 (EST)
Message-ID: <1677317814.82065227.1519853392657.JavaMail.zimbra@broadstripe.net> (raw)
In-Reply-To: <87vaegq0v4.fsf@gmail.com>
References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com>
<87vaegq0v4.fsf@gmail.com>
----- Original Message -----
> From: "Tim Cross" <theophilusx@gmail.com>
> Sent: Wednesday, February 28, 2018 4:07:43 PM
>
> Jeremy Finzel <finzelj@gmail.com> writes:
>
> > We want to enforce a policy, partly just to protect those who might forget,
> > for every table in a particular schema to have a primary key. This can't
> > be done with event triggers as far as I can see, because it is quite
> > legitimate to do:
> >
> > BEGIN;
> > CREATE TABLE foo (id int);
> > ALTER TABLE foo ADD PRIMARY KEY (id);
> > COMMIT;
> >
> > It would be nice to have some kind of "deferrable event trigger" or some
> > way to enforce that no transaction commits which added a table without a
> > primary key.
> >
>
> I think you would be better off having an automated report which alerts
> you to tables lacking a primary key and deal with that policy through
> other means. Using triggers in this way often leads to unexpected
> behaviour and difficult to identify bugs. The policy is a management
> policy and probably should be dealt with via management channels rather
> than technical ones. Besides, the likely outcome will be your developers
> will just adopt the practice of adding a serial column to every table,
> which in itself doesn't really add any value.
I concur with other respondents that suggest this is more of a policy issue. In fact, you yourself identify it right there in the first sentence as a policy issue!
One tool that changed my life (as a PostgreSQL enthusiast) forever is David Wheeler's pgTAP (http://pgtap.org/) tool. It includes a suite of functionality to assess the database schema via automated testing. Part of a rigorous development environment might include using this tool so that any application/database changes be driven by tests, and then your code review process would assure that the appropriate tests are added to the pgTAP script to confirm that changes meet a policy standard such as what you are demanding. I can't imagine doing PostgreSQL development without it now.
Same guy also produced a related tool called Sqitch (http://sqitch.org/) for data base change management. Use these tools together, so that before a developer is allowed to check in a feature branch, your teams' code review process maintains rigorous oversight of modifications.
-- B
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: btober@broadstripe.net, theophilusx@gmail.com, finzelj@gmail.com
Subject: Re: Enforce primary key on every table during dev?
In-Reply-To: <1677317814.82065227.1519853392657.JavaMail.zimbra@broadstripe.net>
References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com> <87vaegq0v4.fsf@gmail.com> <1677317814.82065227.1519853392657.JavaMail.zimbra@broadstripe.net>
* 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