This repository has been archived by the owner on Jul 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sql
248 lines (207 loc) · 6.41 KB
/
install.sql
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Install SQL
* Required if the module has menu entries
* - Add profile exceptions for the module to appear in the menu
* - Add program config options if any (to every schools)
* - Add module specific tables (and their eventual sequences & indexes)
* if any: see rosariosis.sql file for examples
*
* @package Messaging module
*/
-- Fix #102 error language "plpgsql" does not exist
-- http://timmurphy.org/2011/08/27/create-language-if-it-doesnt-exist-in-postgresql/
--
-- Name: create_language_plpgsql(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION create_language_plpgsql()
RETURNS BOOLEAN AS $$
CREATE LANGUAGE plpgsql;
SELECT TRUE;
$$ LANGUAGE SQL;
SELECT CASE WHEN NOT (
SELECT TRUE AS exists FROM pg_language
WHERE lanname='plpgsql'
UNION
SELECT FALSE AS exists
ORDER BY exists DESC
LIMIT 1
) THEN
create_language_plpgsql()
ELSE
FALSE
END AS plpgsql_created;
DROP FUNCTION create_language_plpgsql();
/**
* profile_exceptions Table
*
* profile_id:
* - 0: student
* - 1: admin
* - 2: teacher
* - 3: parent
* modname: should match the Menu.php entries
* can_use: 'Y'
* can_edit: 'Y' or null (generally null for non admins)
*/
--
-- Data for Name: profile_exceptions; Type: TABLE DATA;
--
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 1, 'Messaging/Messages.php', 'Y', 'Y'
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Messages.php'
AND profile_id=1);
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 1, 'Messaging/Write.php', 'Y', 'Y'
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Write.php'
AND profile_id=1);
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 2, 'Messaging/Messages.php', 'Y', null
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Messages.php'
AND profile_id=2);
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 2, 'Messaging/Write.php', 'Y', null
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Write.php'
AND profile_id=2);
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 3, 'Messaging/Messages.php', 'Y', null
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Messages.php'
AND profile_id=3);
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 3, 'Messaging/Write.php', 'Y', null
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Write.php'
AND profile_id=3);
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 0, 'Messaging/Messages.php', 'Y', null
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Messages.php'
AND profile_id=0);
INSERT INTO profile_exceptions (profile_id, modname, can_use, can_edit)
SELECT 0, 'Messaging/Write.php', 'Y', null
WHERE NOT EXISTS (SELECT profile_id
FROM profile_exceptions
WHERE modname='Messaging/Write.php'
AND profile_id=0);
/**
* Add module tables
*/
/**
* User cross message table
*/
--
-- Name: messagexuser; Type: TABLE; Schema: public; Owner: rosariosis; Tablespace:
--
CREATE OR REPLACE FUNCTION create_table_messagexuser() RETURNS void AS
$func$
BEGIN
IF EXISTS (SELECT 1 FROM pg_catalog.pg_tables
WHERE schemaname = CURRENT_SCHEMA()
AND tablename = 'messagexuser') THEN
RAISE NOTICE 'Table "messagexuser" already exists.';
ELSE
CREATE TABLE messagexuser (
user_id numeric NOT NULL,
key character varying(10),
message_id numeric NOT NULL,
status character varying(10) NOT NULL
);
END IF;
END
$func$ LANGUAGE plpgsql;
SELECT create_table_messagexuser();
DROP FUNCTION create_table_messagexuser();
--
-- Name: messagexuser_ind; Type: INDEX; Schema: public; Owner: rosariosis; Tablespace:
--
CREATE OR REPLACE FUNCTION create_index_messagexuser_ind() RETURNS void AS
$func$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_class c
JOIN pg_namespace n ON n.oid=c.relnamespace
WHERE c.relname='messagexuser_ind'
AND n.nspname=CURRENT_SCHEMA()
) THEN
CREATE INDEX messagexuser_ind ON messagexuser (user_id, key, status);
END IF;
END
$func$ LANGUAGE plpgsql;
SELECT create_index_messagexuser_ind();
DROP FUNCTION create_index_messagexuser_ind();
/**
* Messages table
*/
--
-- Name: messages; Type: TABLE; Schema: public; Owner: rosariosis; Tablespace:
--
CREATE OR REPLACE FUNCTION create_table_messages() RETURNS void AS
$func$
BEGIN
IF EXISTS (SELECT 1 FROM pg_catalog.pg_tables
WHERE schemaname=CURRENT_SCHEMA()
AND tablename='messages') THEN
RAISE NOTICE 'Table "messages" already exists.';
ELSE
CREATE TABLE messages (
message_id numeric PRIMARY KEY,
syear numeric(4,0),
school_id numeric NOT NULL,
"from" character varying(255),
recipients text,
subject character varying(100),
"datetime" timestamp(0) without time zone,
data text
);
END IF;
END
$func$ LANGUAGE plpgsql;
SELECT create_table_messages();
DROP FUNCTION create_table_messages();
--
-- Name: messages_seq; Type: SEQUENCE; Schema: public; Owner: rosariosis
--
CREATE OR REPLACE FUNCTION create_sequence_messages_seq() RETURNS void AS
$func$
BEGIN
CREATE SEQUENCE messages_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
EXCEPTION WHEN duplicate_table THEN
RAISE NOTICE 'Sequence "messages_seq" already exists.';
END
$func$ LANGUAGE plpgsql;
SELECT create_sequence_messages_seq();
DROP FUNCTION create_sequence_messages_seq();
--
-- Name: messages_ind; Type: INDEX; Schema: public; Owner: rosariosis; Tablespace:
--
CREATE OR REPLACE FUNCTION create_index_messages_ind() RETURNS void AS
$func$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_class c
JOIN pg_namespace n ON n.oid=c.relnamespace
WHERE c.relname='messages_ind'
AND n.nspname=CURRENT_SCHEMA
) THEN
CREATE INDEX messages_ind ON messages USING btree (syear, school_id);
END IF;
END
$func$ LANGUAGE plpgsql;
SELECT create_index_messages_ind();
DROP FUNCTION create_index_messages_ind();