Project

General

Profile

1
DROP VIEW organizations_view;
2
DROP VIEW organizations_simple_view;
3

    
4
DROP TABLE IF EXISTS other_ids;
5
DROP TABLE IF EXISTS other_names;
6
DROP TABLE IF EXISTS acronyms;
7
DROP TABLE IF EXISTS relationships;
8
DROP TABLE IF EXISTS urls;
9
DROP TABLE IF EXISTS openaire_simrels;
10
DROP TABLE IF EXISTS organizations;
11
DROP TABLE IF EXISTS org_types;
12

    
13
DROP TABLE IF EXISTS user_countries;
14
DROP TABLE IF EXISTS users;
15

    
16
DROP TABLE IF EXISTS user_roles;
17
DROP TABLE IF EXISTS countries;
18
DROP TABLE IF EXISTS id_types;
19
DROP TABLE IF EXISTS languages;
20

    
21
DROP SEQUENCE IF EXISTS organizations_id_seq;
22

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

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

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

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

    
48
CREATE TABLE user_roles(role text PRIMARY KEY);
49
INSERT INTO user_roles VALUES ('USER'), ('SUPERUSER');
50

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

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

    
64
CREATE SEQUENCE organizations_id_seq;
65

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

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

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

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

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

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

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

    
135
CREATE VIEW organizations_view AS SELECT
136
    org.id,
137
    org.name,
138
    org.type,
139
    org.lat,
140
	org.lng,
141
	org.city,
142
	org.country,
143
	org.created_by,
144
	org.creation_date,
145
	org.modified_by,
146
	org.modification_date,
147
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('id', oid.otherid, 'type', oid.type)) FILTER (WHERE oid.otherid IS NOT NULL), '[]') AS other_ids,
148
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('name', n.name, 'lang', n.lang))      FILTER (WHERE n.name      IS NOT NULL), '[]') AS other_names,
149
	COALESCE(jsonb_agg(DISTINCT a.acronym)                                               FILTER (WHERE a.acronym   IS NOT NULL), '[]') AS acronyms,	
150
	COALESCE(jsonb_agg(DISTINCT u.url)                                                   FILTER (WHERE u.url       IS NOT NULL), '[]') AS urls
151
FROM
152
    organizations org
153
    LEFT OUTER JOIN other_ids oid    ON (org.id = oid.id)
154
    LEFT OUTER JOIN other_names n    ON (org.id = n.id)
155
    LEFT OUTER JOIN acronyms a       ON (org.id = a.id)
156
    LEFT OUTER JOIN urls u           ON (org.id = u.id)
157
GROUP BY
158
    org.id,
159
    org.name,
160
    org.type,
161
    org.lat,
162
	org.lng,
163
	org.city,
164
	org.country,
165
	org.created_by,
166
	org.creation_date,
167
	org.modified_by,
168
	org.modification_date;
169
	
170
CREATE VIEW organizations_simple_view AS SELECT
171
    org.id,
172
    org.name,
173
    org.type,
174
	org.city,
175
	org.country,
176
	array_agg(DISTINCT a.acronym) FILTER (WHERE a.acronym IS NOT NULL) AS acronyms
177
FROM
178
    organizations org
179
    LEFT OUTER JOIN acronyms a ON (org.id = a.id)
180
GROUP BY
181
    org.id,
182
    org.name,
183
    org.type,
184
	org.city,
185
	org.country;
186
	
(3-3/3)