]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0322.schema.query.no-xfld-expr.sql
LP#1759238: stamping upgrade script
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0322.schema.query.no-xfld-expr.sql
1 -- Instead of representing a subfield as a distinct expression type in its
2 -- own right, express it as a variant of the xfunc type -- i.e. a function
3 -- call with a column name.  In consequence:
4
5 -- 1. Eliminate the query.expr_xfld view;
6
7 -- 2. Expand the query.expr_xfunc view to include column_name;
8
9 -- 3. Eliminate 'xfld' as a valid value for expression.type.
10
11 -- Theoretically, the latter change could create a problem if you already
12 -- have xfld rows in your expression table.  You would have to delete them,
13 -- and represent the corresponding expressions by other means.  In practice
14 -- this is exceedingly unlikely, since subfields were never even
15 -- supported until earlier today.
16
17 -- We start by dropping two views; the first for good, and the second so that
18 -- we can replace it.  We drop them outside the transaction so that the
19 -- script won't fail if the views don't exist yet.
20
21 DROP VIEW query.expr_xfld;
22
23 DROP VIEW query.expr_xfunc;
24
25 BEGIN;
26
27 INSERT INTO config.upgrade_log (version) VALUES ('0322'); -- Scott McKellar
28
29 -- Eliminate 'xfld' as an expression type
30
31 ALTER TABLE query.expression
32         DROP CONSTRAINT expression_type;
33
34 ALTER TABLE query.expression
35         ADD CONSTRAINT expression_type CHECK ( type in (
36         'xbet',    -- between
37         'xbind',   -- bind variable
38         'xbool',   -- boolean
39         'xcase',   -- case
40         'xcast',   -- cast
41         'xcol',    -- column
42         'xex',     -- exists
43         'xfunc',   -- function
44         'xin',     -- in
45         'xisnull', -- is null
46         'xnull',   -- null
47         'xnum',    -- number
48         'xop',     -- operator
49         'xser',    -- series
50         'xstr',    -- string
51         'xsubq'    -- subquery
52     ) );
53
54 -- Create updatable view for function call expressions
55
56 CREATE OR REPLACE VIEW query.expr_xfunc AS
57     SELECT
58         id,
59         parenthesize,
60         parent_expr,
61         seq_no,
62         column_name,
63         function_id,
64         negate
65     FROM
66         query.expression
67     WHERE
68         type = 'xfunc';
69
70 CREATE OR REPLACE RULE query_expr_xfunc_insert_rule AS
71     ON INSERT TO query.expr_xfunc
72     DO INSTEAD
73     INSERT INTO query.expression (
74         id,
75         type,
76         parenthesize,
77         parent_expr,
78         seq_no,
79         column_name,
80         function_id,
81         negate
82     ) VALUES (
83         COALESCE(NEW.id, NEXTVAL('query.expression_id_seq'::REGCLASS)),
84         'xfunc',
85         COALESCE(NEW.parenthesize, FALSE),
86         NEW.parent_expr,
87         COALESCE(NEW.seq_no, 1),
88         NEW.column_name,
89         NEW.function_id,
90         COALESCE(NEW.negate, false)
91     );
92
93 CREATE OR REPLACE RULE query_expr_xfunc_update_rule AS
94     ON UPDATE TO query.expr_xfunc
95     DO INSTEAD
96     UPDATE query.expression SET
97         id = NEW.id,
98         parenthesize = NEW.parenthesize,
99         parent_expr = NEW.parent_expr,
100         seq_no = NEW.seq_no,
101         column_name = NEW.column_name,
102         function_id = NEW.function_id,
103         negate = NEW.negate
104     WHERE
105         id = OLD.id;
106
107 CREATE OR REPLACE RULE query_expr_xfunc_delete_rule AS
108     ON DELETE TO query.expr_xfunc
109     DO INSTEAD
110     DELETE FROM query.expression WHERE id = OLD.id;
111
112 COMMIT;