Re: A safe way to upgrade table definitions by using ALTER's

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: A safe way to upgrade table definitions by using ALTER's
Date: 2009-08-28 13:56:11
Message-ID: 20090828135611.GE5407@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Aug 28, 2009 at 12:42:59AM +0400, Sergey Samokhin wrote:
> But how do programmers guarantee that ALTER's they have wrote will
> always be applied by administrators to the corresponding version of
> the database?

How about using the normal integrity constraints that databases provide?
Have some table like:

CREATE TABLE version (
feature TEXT PRIMARY KEY,
depends TEXT REFERENCES version,
inserted TIMESTAMP DEFAULT now()
);

and at the start of every modification script put a row (or several)
into the table:

BEGIN;
INSERT INTO version (feature,depends) VALUES
('table foo',NULL);
CREATE TABLE foo ( id TEXT PRIMARY KEY, value TEXT );
COMMIT;

and then you can check to see if the constraints are met by doing:

BEGIN;
INSERT INTO version (feature,depends) VALUES
('table foo add startend dates','table foo');
ALTER TABLE foo
ADD COLUMN startdate TIMESTAMP DEFAULT now(),
ADD COLUMN enddate TIMESTAMP DEFAULT 'infinity';
COMMIT;

Not sure if that's the sort of thing that you want/need but I don't
think there's a general solution to the problem. Determining the
relevant context for this sort of thing is hard.

--
Sam http://samason.me.uk/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message paulo matadr 2009-08-28 14:07:15 details locks
Previous Message Paweł Nieścioruk 2009-08-28 13:11:03 pg_hba.conf problem in PostgreSQL 8.4 (no-installer)