Project

General

Profile

1
DROP VIEW organizations_view;
2
DROP VIEW organizations_info_view;
3
DROP VIEW organizations_simple_view;
4
DROP VIEW users_view;
5
DROP VIEW conflicts_view;
6

    
7
DROP TABLE IF EXISTS other_ids;
8
DROP TABLE IF EXISTS other_names;
9
DROP TABLE IF EXISTS acronyms;
10
DROP TABLE IF EXISTS relationships;
11
DROP TABLE IF EXISTS urls;
12
DROP TABLE IF EXISTS openaire_simrels;
13
DROP TABLE IF EXISTS organizations;
14
DROP TABLE IF EXISTS org_types;
15

    
16
DROP TABLE IF EXISTS user_countries;
17
DROP TABLE IF EXISTS users;
18

    
19
DROP TABLE IF EXISTS user_roles;
20
DROP TABLE IF EXISTS countries;
21
DROP TABLE IF EXISTS id_types;
22
DROP TABLE IF EXISTS languages;
23

    
24
DROP SEQUENCE IF EXISTS organizations_id_seq;
25

    
26
CREATE TABLE org_types (val text PRIMARY KEY);
27
INSERT INTO org_types VALUES ('Archive'), ('Company'), ('Education'), ('Facility'), ('Government'), ('Healthcare'), ('Nonprofit'), ('Other'), ('UNKNOWN');
28

    
29
CREATE TABLE id_types (val text PRIMARY KEY);
30
INSERT INTO id_types VALUES ('CNRS'), ('FundRef'), ('HESA'), ('ISNI'), ('OrgRef'), ('UCAS'), ('UKPRN'), ('Wikidata'), ('grid.ac');
31

    
32
CREATE TABLE languages (val text PRIMARY KEY);
33
INSERT INTO languages VALUES ('UNKNOWN'), ('aa'), ('af'), ('am'), ('ar'), ('as'), ('az'), ('ba'), ('be'), ('bg'), ('bn'), ('br'), ('bs'), ('ca'), ('ch'), ('co'), ('cs'), ('cy'), ('da'),
34
			('de'), ('dv'), ('dz'), ('el'), ('en'), ('eo'), ('es'), ('et'), ('eu'), ('fa'), ('fi'), ('fr'), ('fy'), ('ga'), ('gd'), ('gl'), ('gu'), ('he'), ('hi'), ('hr'), ('hu'), ('hy'), ('id'), ('is'),
35
			('it'), ('iu'), ('ja'), ('jv'), ('ka'), ('kk'), ('kl'), ('km'), ('kn'), ('ko'), ('ku'), ('ky'), ('la'), ('lb'), ('lo'), ('lt'), ('lv'), ('mg'), ('mi'), ('mk'), ('ml'), ('mn'), ('mr'), ('ms'),
36
			('mt'), ('my'), ('nb'), ('ne'), ('nl'), ('nn'), ('no'), ('oc'), ('or'), ('pa'), ('pl'), ('ps'), ('pt'), ('rm'), ('ro'), ('ru'), ('rw'), ('sa'), ('sd'), ('si'), ('sk'), ('sl'), ('so'), ('sq'),
37
			('sr'), ('sv'), ('sw'), ('ta'), ('te'), ('tg'), ('th'), ('tk'), ('tl'), ('tr'), ('tt'), ('ug'), ('uk'), ('ur'), ('uz'), ('vi'), ('xh'), ('yo'), ('zh'), ('zu');
38

    
39
CREATE TABLE countries (val text PRIMARY KEY);
40
INSERT INTO countries VALUES ('UNKNOWN'), ('AD'), ('AE'), ('AF'), ('AG'), ('AL'), ('AM'), ('AO'), ('AR'), ('AT'), ('AU'), ('AW'), ('AX'), ('AZ'), ('BA'), ('BB'), ('BD'), ('BE'), ('BF'), ('BG'), ('BH'), ('BI'),
41
			('BJ'), ('BM'), ('BN'), ('BO'), ('BQ'), ('BR'), ('BS'), ('BT'), ('BW'), ('BY'), ('BZ'), ('CA'), ('CD'), ('CF'), ('CG'), ('CH'), ('CI'), ('CL'), ('CM'), ('CN'), ('CO'), ('CR'),
42
			('CU'), ('CV'), ('CW'), ('CY'), ('CZ'), ('DE'), ('DJ'), ('DK'), ('DM'), ('DO'), ('DZ'), ('EC'), ('EE'), ('EG'), ('EH'), ('ER'), ('ES'), ('ET'), ('FI'), ('FJ'), ('FM'), ('FO'),
43
			('FR'), ('GA'), ('GB'), ('GD'), ('GE'), ('GF'), ('GH'), ('GI'), ('GL'), ('GM'), ('GN'), ('GP'), ('GQ'), ('GR'), ('GT'), ('GW'), ('GY'), ('HN'), ('HR'), ('HT'), ('HU'), ('ID'),
44
			('IE'), ('IL'), ('IM'), ('IN'), ('IQ'), ('IR'), ('IS'), ('IT'), ('JE'), ('JM'), ('JO'), ('JP'), ('KE'), ('KG'), ('KH'), ('KN'), ('KP'), ('KR'), ('KW'), ('KY'), ('KZ'), ('LA'),
45
			('LB'), ('LC'), ('LI'), ('LK'), ('LR'), ('LS'), ('LT'), ('LU'), ('LV'), ('LY'), ('MA'), ('MC'), ('MD'), ('ME'), ('MG'), ('MK'), ('ML'), ('MM'), ('MN'), ('MO'), ('MQ'), ('MR'),
46
			('MS'), ('MT'), ('MU'), ('MV'), ('MW'), ('MX'), ('MY'), ('MZ'), ('NA'), ('NC'), ('NE'), ('NG'), ('NI'), ('NL'), ('NO'), ('NP'), ('NU'), ('NZ'), ('OM'), ('PA'), ('PE'), ('PF'),
47
			('PG'), ('PH'), ('PK'), ('PL'), ('PS'), ('PT'), ('PW'), ('PY'), ('QA'), ('RE'), ('RO'), ('RS'), ('RU'), ('RW'), ('SA'), ('SB'), ('SC'), ('SD'), ('SE'), ('SG'), ('SI'), ('SJ'),
48
			('SK'), ('SL'), ('SM'), ('SN'), ('SO'), ('SR'), ('SS'), ('ST'), ('SV'), ('SX'), ('SY'), ('SZ'), ('TC'), ('TD'), ('TG'), ('TH'), ('TJ'), ('TL'), ('TM'), ('TN'), ('TO'), ('TR'),
49
			('TT'), ('TV'), ('TW'), ('TZ'), ('UA'), ('UG'), ('US'), ('UY'), ('UZ'), ('VA'), ('VC'), ('VE'), ('VG'), ('VN'), ('WS'), ('XK'), ('YE'), ('ZA'), ('ZM'), ('ZW');
50

    
51
CREATE TABLE user_roles(role text PRIMARY KEY);
52
INSERT INTO user_roles VALUES ('ADMIN'), ('NATIONAL_ADMIN'), ('USER'), ('PENDING'), ('NOT_AUTHORIZED');
53

    
54
CREATE TABLE users (
55
	email     text PRIMARY KEY,
56
	valid     boolean DEFAULT true,
57
	role      text NOT NULL default 'USER' REFERENCES user_roles(role)
58
);
59

    
60
CREATE TABLE user_countries (
61
	email     text REFERENCES users(email),
62
	country   text  REFERENCES countries(val),
63
	PRIMARY KEY(email, country)	
64
);
65

    
66
CREATE SEQUENCE organizations_id_seq;
67

    
68
CREATE TABLE organizations (
69
    id                text PRIMARY KEY DEFAULT 'openorgs____::'||lpad(nextval('organizations_id_seq')::text,10,'0'),
70
    name              text,
71
   	type              text NOT NULL DEFAULT 'UNKNOWN' REFERENCES org_types(val),
72
    lat               double precision,
73
	lng               double precision,
74
	city              text,
75
	country           text REFERENCES countries(val),
76
	created_by        text,
77
	creation_date     timestamp with time zone DEFAULT now(),
78
	modified_by       text,
79
	modification_date timestamp with time zone DEFAULT now()
80
);
81
CREATE INDEX organizations_type_idx ON organizations(type);
82
CREATE INDEX organizations_country_idx ON organizations(country);
83

    
84
CREATE TABLE other_ids (
85
	id      text REFERENCES organizations(id) ON UPDATE CASCADE,
86
	otherid text,
87
	type    text REFERENCES id_types(val),
88
	PRIMARY KEY (id, otherid, type)
89
);
90
CREATE INDEX other_ids_id_idx ON other_ids(id);
91

    
92
CREATE TABLE other_names (
93
	id    text REFERENCES organizations(id) ON UPDATE CASCADE,
94
	name  text,
95
	lang  text REFERENCES languages(val),
96
	PRIMARY KEY (id, name, lang)
97
);
98
CREATE INDEX other_names_id_idx ON other_names(id);
99

    
100
CREATE TABLE acronyms (
101
	id text  REFERENCES organizations(id) ON UPDATE CASCADE,
102
	acronym text,
103
	PRIMARY KEY (id, acronym)
104
);
105
CREATE INDEX acronyms_id_idx ON acronyms(id);
106

    
107
CREATE TABLE relationships (
108
	id1     text REFERENCES organizations(id) ON UPDATE CASCADE,
109
	reltype text,
110
	id2     text REFERENCES organizations(id) ON UPDATE CASCADE,
111
    PRIMARY KEY (id1, reltype, id2)
112
);
113
CREATE INDEX relationships_id1_idx ON relationships(id1);
114
CREATE INDEX relationships_id2_idx ON relationships(id2);
115

    
116
CREATE TABLE urls (
117
	id  text REFERENCES organizations(id) ON UPDATE CASCADE,
118
	url text,
119
	PRIMARY KEY (id, url)
120
);
121
CREATE INDEX urls_id_idx ON urls(id);
122

    
123
CREATE TABLE openaire_simrels (
124
	local_id         text REFERENCES organizations(id) ON UPDATE CASCADE,
125
	oa_original_id   text NOT NULL,
126
	oa_name          text NOT NULL,
127
	oa_acronym       text,
128
	oa_country       text,
129
	oa_url           text,
130
	oa_collectedfrom text,
131
	reltype          text NOT NULL DEFAULT 'suggested',
132
	PRIMARY KEY (local_id, oa_original_id)
133
);
134
CREATE INDEX openaire_simrels_local_id_idx ON openaire_simrels(local_id);
135

    
136
CREATE VIEW organizations_view AS SELECT
137
    org.id,
138
    org.name,
139
    org.type,
140
    org.lat,
141
	org.lng,
142
	org.city,
143
	org.country,
144
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('id', oid.otherid, 'type', oid.type)) FILTER (WHERE oid.otherid IS NOT NULL), '[]') AS other_ids,
145
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('name', n.name, 'lang', n.lang))      FILTER (WHERE n.name      IS NOT NULL), '[]') AS other_names,
146
	COALESCE(jsonb_agg(DISTINCT a.acronym)                                               FILTER (WHERE a.acronym   IS NOT NULL), '[]') AS acronyms,	
147
	COALESCE(jsonb_agg(DISTINCT u.url)                                                   FILTER (WHERE u.url       IS NOT NULL), '[]') AS urls
148
FROM
149
    organizations org
150
    LEFT OUTER JOIN other_ids oid    ON (org.id = oid.id)
151
    LEFT OUTER JOIN other_names n    ON (org.id = n.id)
152
    LEFT OUTER JOIN acronyms a       ON (org.id = a.id)
153
    LEFT OUTER JOIN urls u           ON (org.id = u.id)
154
GROUP BY
155
    org.id,
156
    org.name,
157
    org.type,
158
    org.lat,
159
	org.lng,
160
	org.city,
161
	org.country;
162
	
163
CREATE VIEW organizations_info_view AS SELECT
164
	org.id,
165
	org.created_by,
166
	org.creation_date,
167
	org.modified_by,
168
	org.modification_date,
169
	count(r.id1)      as n_rels,
170
	count(e.local_id) as n_enrichments,
171
	count(c.local_id) as n_conflicts
172
FROM organizations org
173
	LEFT OUTER JOIN relationships r ON (org.id = r.id1)
174
	LEFT OUTER JOIN openaire_simrels e ON (org.id = e.local_id AND e.oa_original_id NOT LIKE 'openorgs____::%')
175
	LEFT OUTER JOIN openaire_simrels c ON (org.id = c.local_id AND c.oa_original_id LIKE 'openorgs____::%')
176
GROUP BY org.id;
177
	
178
CREATE VIEW organizations_simple_view AS SELECT
179
    org.id,
180
    org.name,
181
    org.type,
182
	org.city,
183
	org.country,
184
	array_agg(DISTINCT a.acronym) FILTER (WHERE a.acronym IS NOT NULL) AS acronyms
185
FROM
186
    organizations org
187
    LEFT OUTER JOIN acronyms a ON (org.id = a.id)
188
GROUP BY
189
    org.id,
190
    org.name,
191
    org.type,
192
	org.city,
193
	org.country;
194
	
195
CREATE VIEW	users_view AS SELECT
196
	u.email,
197
	u.valid,
198
	u.role,
199
	array_agg(uc.country) FILTER (WHERE uc.country IS NOT NULL) AS countries
200
FROM
201
	users u
202
	LEFT OUTER JOIN user_countries uc ON (u.email = uc.email)
203
GROUP BY u.email, u.valid, u.role
204
ORDER BY u.email;
205

    
206
CREATE VIEW conflicts_view AS SELECT
207
    o1.id      AS id_1,
208
    o1.name    AS name_1,
209
    o1.type    AS type_1,
210
	o1.city    AS city_1,
211
	o1.country AS country_1,
212
    o2.id      AS id_2,
213
    o2.name    AS name_2,
214
    o2.type    AS type_2,
215
	o2.city    AS city_2,
216
	o2.country AS country_2
217
FROM
218
	openaire_simrels s
219
	LEFT OUTER JOIN organizations o1 ON (s.local_id = o1.id)
220
    LEFT OUTER JOIN organizations o2 ON (s.oa_original_id = o2.id)
221
WHERE
222
	s.oa_original_id LIKE 'openorgs____::%' AND o1.id IS NOT NULL AND O2.id IS NOT NULL;
223
	
224

    
225

    
226
	
(4-4/4)