aboutsummaryrefslogtreecommitdiffstats
path: root/db/downgrade_0051---0050.sql
blob: 424420e2909c339e58ea9eef06d584df81fc1f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
BEGIN;

ALTER TABLE contacts ADD confirmed boolean;
ALTER TABLE contacts ADD deleted boolean;

UPDATE contacts SET confirmed='t', deleted='t' WHERE state = 'deleted';
UPDATE contacts SET confirmed='f', deleted='t' WHERE state = 'inactive';
UPDATE contacts SET confirmed='t', deleted='f' WHERE state = 'confirmed';
UPDATE contacts SET confirmed='f', deleted='f' WHERE state = 'unconfirmed';

ALTER TABLE contacts ALTER COLUMN confirmed SET NOT NULL;
ALTER TABLE contacts ALTER COLUMN deleted SET NOT NULL;
ALTER TABLE contacts DROP COLUMN state;

ALTER TABLE contacts_history ADD confirmed boolean;
ALTER TABLE contacts_history ADD deleted boolean;

UPDATE contacts_history SET confirmed='t', deleted='t' WHERE state = 'deleted';
UPDATE contacts_history SET confirmed='f', deleted='t' WHERE state = 'inactive';
UPDATE contacts_history SET confirmed='t', deleted='f' WHERE state = 'confirmed';
UPDATE contacts_history SET confirmed='f', deleted='f' WHERE state = 'unconfirmed';

ALTER TABLE contacts_history ALTER COLUMN confirmed SET NOT NULL;
ALTER TABLE contacts_history ALTER COLUMN deleted SET NOT NULL;
ALTER TABLE contacts_history DROP COLUMN state;

CREATE OR REPLACE FUNCTION contacts_updated()
    returns trigger as '
    begin
        insert into contacts_history (contact_id, body_id, category, email, editor, whenedited, note, confirmed, deleted) values (new.id, new.body_id, new.category, new.email, new.editor, new.whenedited, new.note, new.confirmed, new.deleted);
        return new;
    end;
' language 'plpgsql';

COMMIT;