b086b014b04215a0480ea29a9416778d0753c0cd
[kivitendo-erp.git] / sql / Pg-upgrade2 / acc_trans_without_oid.sql
1 -- @tag: acc_trans_without_oid
2 -- @description: Einführen einer ID-Spalte in acc_trans
3 -- @depends: release_2_4_3 cb_ob_transaction
4
5 -- INFO: Dieses Script hat früher die Spalte acc_trans_id aus der
6 -- impliziten OID gesetzt. PostgreSQL 12 unterstützt aber keine OIDs
7 -- mehr, daher wurde die OID hier entfernt. Das ist insofern auch kein
8 -- Problem, weil dieses Upgrade-Script in Version 2.6.0 benutzt wurde,
9 -- und direkte Updates auf die aktuelle kivitendo-Version von vor 3.0
10 -- eh nicht mehr unterstützt werden.
11 --
12 -- Das Script muss aber trotzdem beim Anlegen neuer Datenbanken
13 -- abgearbeitet werden und daher funktionieren.
14
15 CREATE SEQUENCE acc_trans_id_seq;
16
17 CREATE TABLE new_acc_trans (
18     acc_trans_id bigint DEFAULT nextval('acc_trans_id_seq'),
19     trans_id integer,
20     chart_id integer,
21     amount numeric(15,5),
22     transdate date DEFAULT date('now'::text),
23     gldate date DEFAULT date('now'::text),
24     source text,
25     cleared boolean DEFAULT false,
26     fx_transaction boolean DEFAULT false,
27     ob_transaction boolean DEFAULT false,
28     cb_transaction boolean DEFAULT false,
29     project_id integer,
30     memo text,
31     taxkey integer,
32     itime timestamp without time zone DEFAULT now(),
33     mtime timestamp without time zone
34 );
35
36 INSERT INTO new_acc_trans (trans_id, chart_id, amount, transdate, gldate, source, cleared,
37                            fx_transaction, ob_transaction, cb_transaction, project_id, memo, taxkey, itime, mtime)
38   SELECT trans_id, chart_id, amount, transdate, gldate, source, cleared,
39     fx_transaction, ob_transaction, cb_transaction, project_id, memo, taxkey, itime, mtime
40   FROM acc_trans;
41
42 DROP TABLE acc_trans;
43 ALTER TABLE new_acc_trans RENAME TO acc_trans;
44
45 CREATE INDEX acc_trans_trans_id_key ON acc_trans USING btree (trans_id);
46 CREATE INDEX acc_trans_chart_id_key ON acc_trans USING btree (chart_id);
47 CREATE INDEX acc_trans_transdate_key ON acc_trans USING btree (transdate);
48 CREATE INDEX acc_trans_source_key ON acc_trans USING btree (lower(source));
49
50 ALTER TABLE ONLY acc_trans
51     ADD CONSTRAINT "$1" FOREIGN KEY (chart_id) REFERENCES chart(id);
52
53 CREATE TRIGGER mtime_acc_trans
54     BEFORE UPDATE ON acc_trans
55     FOR EACH ROW
56     EXECUTE PROCEDURE set_mtime();