-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path010-gallformers.sql
363 lines (331 loc) · 13.2 KB
/
010-gallformers.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
-- Up
BEGIN TRANSACTION;
PRAGMA foreign_keys=OFF;
-- major schema changes to handle the following:
-- 1) Data Quality/Completeness
-- 2) More robust species aliasing
-- a) Mapping a species to source by an aliased name
-- b) undescribed, deprecated, etc.
-- 3) more complete taxonomy support (sections, genus and family as part of taxonomy)
-- 4) allowing more than one gall to be associated with a singel species (sexual/asexual generations)
-- new tables for alias support
CREATE TABLE alias (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL CHECK (type = 'common' OR type = 'scientific'),
description TEXT NOT NULL DEFAULT ''
);
CREATE TABLE aliasspecies (
species_id INTEGER,
alias_id INTEGER,
FOREIGN KEY (species_id) REFERENCES species (id) ON DELETE CASCADE,
FOREIGN KEY (alias_id) REFERENCES alias (id) ON DELETE CASCADE,
PRIMARY KEY(species_id, alias_id)
);
-- table mods for alias support
CREATE TABLE speciessource__ (
id INTEGER PRIMARY KEY
NOT NULL,
species_id INTEGER NOT NULL,
source_id INTEGER NOT NULL,
description TEXT DEFAULT '',
useasdefault INTEGER NOT NULL
DEFAULT 0,
externallink TEXT DEFAULT '',
-- new field
alias_id INTEGER, -- not required
FOREIGN KEY (
species_id
)
REFERENCES species (id) ON DELETE CASCADE,
FOREIGN KEY (
source_id
)
REFERENCES source (id) ON DELETE CASCADE,
-- new field
FOREIGN KEY (
alias_id
)
REFERENCES alias (id)
);
INSERT INTO speciessource__ (id, species_id, source_id, description, useasdefault, externallink, alias_id)
SELECT id, species_id, source_id, description, useasdefault, externallink, NULL
FROM speciessource;
DROP TABLE speciessource;
ALTER TABLE speciessource__ RENAME TO speciessource;
--------------------------------------------------------
-- migrate old species alias/common name data
-- extract the commonnames and add them to the new table
INSERT INTO alias (
id,
name,
type
)
WITH RECURSIVE split (
species_id,
name,
rest
)
AS (
SELECT id,
'',
commonnames || ','
FROM species
WHERE commonnames IS NOT NULL AND
commonnames != ''
UNION ALL
SELECT species_id,
substr(rest, 0, instr(rest, ',') ),
substr(rest, instr(rest, ',') + 1)
FROM split
WHERE rest <> ''
)
SELECT DISTINCT NULL,
TRIM(name),
'common'
FROM split
WHERE name <> ''
ORDER BY species_id,
name;
-- now add the relationships between the commonnames (now as aliases) and the species
INSERT OR IGNORE INTO aliasspecies (
species_id,
alias_id
)
WITH RECURSIVE split (
species_id,
name,
rest
)
AS (
SELECT id,
'',
commonnames || ','
FROM species
WHERE commonnames IS NOT NULL AND
commonnames != ''
UNION ALL
SELECT species_id,
substr(rest, 0, instr(rest, ',') ),
substr(rest, instr(rest, ',') + 1)
FROM split
WHERE rest <> ''
)
SELECT species_id,
alias.id as alias_id
FROM split
INNER JOIN alias ON alias.name = TRIM(split.name)
WHERE split.name <> ''
ORDER BY species_id,
split.name;
-- extract the synonyms (scientific) and add them to the new table
INSERT INTO alias (
id,
name,
type
)
WITH RECURSIVE split (
species_id,
name,
rest
)
AS (
SELECT id,
'',
synonyms || ','
FROM species
WHERE synonyms IS NOT NULL AND
synonyms != ''
UNION ALL
SELECT species_id,
substr(rest, 0, instr(rest, ',') ),
substr(rest, instr(rest, ',') + 1)
FROM split
WHERE rest <> ''
)
SELECT DISTINCT NULL,
TRIM(name),
'scientific'
FROM split
WHERE name <> ''
ORDER BY species_id,
name;
-- now add the relationships between the synonyms (now as aliases) and the species
INSERT OR IGNORE INTO aliasspecies (
species_id,
alias_id
)
WITH RECURSIVE split (
species_id,
name,
rest
)
AS (
SELECT id,
'',
synonyms || ','
FROM species
WHERE synonyms IS NOT NULL AND
synonyms != ''
UNION ALL
SELECT species_id,
substr(rest, 0, instr(rest, ',') ),
substr(rest, instr(rest, ',') + 1)
FROM split
WHERE rest <> ''
)
SELECT species_id,
alias.id as alias_id
FROM split
INNER JOIN alias ON alias.name = TRIM(split.name)
WHERE split.name <> ''
ORDER BY species_id,
split.name;
---------------------------------------------------------------------------------
-- Changes for taxonomy
-- rename the family table to taxonomy and add new columns
-- change all of the exisiting data to be family (since it all came from that table)
CREATE TABLE taxonomy (
id INTEGER PRIMARY KEY
NOT NULL,
name TEXT NOT NULL,
description TEXT DEFAULT '',
type TEXT NOT NULL CHECK (type = 'family' OR type = 'genus' OR type='section'),
parent_id INTEGER DEFAULT NULL,
FOREIGN KEY (parent_id) REFERENCES taxonomy (id)
);
-- families have no parent so we will use the default, null, value for the parent
INSERT INTO taxonomy (id, name, description, type)
SELECT id, name, description, 'family' FROM family;
DROP TABLE family;
-- add new tables for taxonomy
CREATE TABLE taxonomyalias (
taxonomy_id INTEGER,
alias_id INTEGER,
FOREIGN KEY (taxonomy_id) REFERENCES taxonomy (id) ON DELETE CASCADE,
FOREIGN KEY (alias_id) REFERENCES alias (id) ON DELETE CASCADE,
PRIMARY KEY(taxonomy_id, alias_id)
);
CREATE TABLE speciestaxonomy (
species_id INTEGER NOT NULL,
taxonomy_id INTEGER NOT NULL,
FOREIGN KEY (species_id) REFERENCES species (id) ON DELETE CASCADE,
FOREIGN KEY (taxonomy_id) REFERENCES taxonomy (id) ON DELETE CASCADE,
PRIMARY KEY(species_id, taxonomy_id)
);
CREATE TABLE taxonomytaxonomy (
taxonomy_id INTEGER NOT NULL,
child_id INTEGER NOT NULL,
FOREIGN KEY (taxonomy_id) REFERENCES taxonomy (id) ON DELETE CASCADE,
FOREIGN KEY (child_id) REFERENCES taxonomy (id) ON DELETE CASCADE,
PRIMARY KEY(taxonomy_id, child_id)
);
-- migrate genus data to new taxonomy:
-- 1) insert genus records
INSERT INTO taxonomy (id, name, type, parent_id)
SELECT DISTINCT NULL, genus, 'genus', family_id FROM species;
-- 2) map to species
INSERT INTO speciestaxonomy (species_id, taxonomy_id)
SELECT species.id, taxonomy.id
FROM species
INNER JOIN taxonomy ON taxonomy.name = species.genus;
-- create parent-child relationships for new taxonomy
-- migrate old family to species relationships to genus-family
INSERT INTO taxonomytaxonomy (taxonomy_id, child_id)
SELECT DISTINCT family_id, taxonomy.id
FROM species INNER JOIN taxonomy ON species.genus = taxonomy.name;
-- for the sake of having some data we will add the Section mappings for Quercus
-- later an admin UI will be created to maintain these and add new one etc.
INSERT INTO taxonomy (id, name, description, type, parent_id)
VALUES (NULL, 'Quercus', 'White Oaks', 'section', (SELECT id from taxonomy WHERE name = 'Quercus' AND type = 'genus'));
INSERT INTO speciestaxonomy (species_id, taxonomy_id)
WITH species_list(spid) AS
(VALUES (296),(297),(298),(299),(300),(302),(306),(310),(313),(322),(326),
(327),(329),(331),(332),(333),(336),(340),(341),(343),(344),(345),
(348),(349),(352)
) SELECT spid, taxid
FROM species_list
CROSS JOIN (SELECT id as taxid FROM taxonomy WHERE name = 'Quercus' AND type = 'section');
INSERT INTO taxonomy (id, name, description, type, parent_id)
VALUES (NULL, 'Lobatae', 'Red Oaks', 'section', (SELECT id from taxonomy WHERE name = 'Quercus' AND type = 'genus'));
INSERT INTO speciestaxonomy (species_id, taxonomy_id)
WITH species_list(spid) AS
(VALUES (295),(305),(307),(308),(314),(315),(317),(319),(320),(323),(324),
(325),(330),(334),(335),(337),(338),(339),(343),(346),(347),(351),
(355),(357)
) SELECT spid, taxid
FROM species_list
CROSS JOIN (SELECT id as taxid FROM taxonomy WHERE name = 'Lobatae' AND type = 'section');
INSERT INTO taxonomy (id, name, description, type, parent_id)
VALUES (NULL, 'Virentes', 'Live Oaks', 'section', (SELECT id from taxonomy WHERE name = 'Quercus' AND type = 'genus'));
INSERT INTO speciestaxonomy (species_id, taxonomy_id)
WITH species_list(spid) AS
(VALUES (309),(311),(356))
SELECT spid, taxid
FROM species_list
CROSS JOIN (SELECT id as taxid FROM taxonomy WHERE name = 'Virentes' AND type = 'section');
---------------------------------------------------------------------------------------
-- Changes for many galls to species
-- new table
CREATE TABLE gallspecies (
species_id INTEGER,
gall_id INTEGER,
FOREIGN KEY (species_id) REFERENCES species (id) ON DELETE CASCADE,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
PRIMARY KEY(species_id, gall_id)
);
-- migrate exisiting 1-1 species-gall mappings into new structure
INSERT INTO gallspecies (species_id, gall_id)
SELECT species_id, id FROM gall;
-- Now modify old tables:
-- update gall table to remove species_id
CREATE TABLE gall__ (
id INTEGER PRIMARY KEY
NOT NULL,
taxoncode TEXT NOT NULL
CHECK (taxoncode = 'gall'),
detachable INTEGER,
undescribed BOOLEAN DEFAULT 0,
FOREIGN KEY (
taxonCode
)
REFERENCES taxontype (taxonCode)
);
INSERT INTO gall__ (id, taxoncode, detachable)
SELECT id, taxoncode, detachable FROM gall;
DROP TABLE gall;
ALTER TABLE gall__ RENAME TO gall;
-------------------------------------------------------------------------------------
-- update species table to:
-- * remove old alias fields
-- * add new field for data completeness
-- * remove genus field
-- * remove family_id
-- * remove gallid
CREATE TABLE species__ (
id INTEGER PRIMARY KEY
NOT NULL,
taxoncode TEXT,
name TEXT UNIQUE
NOT NULL,
datacomplete BOOLEAN DEFAULT 0,
abundance_id INTEGER,
FOREIGN KEY (
taxoncode
)
REFERENCES taxontype (taxonCode),
FOREIGN KEY (
abundance_id
)
REFERENCES abundance (id)
);
INSERT INTO species__ (id, taxoncode, name, abundance_id)
SELECT id, taxoncode, name, abundance_id FROM species;
DROP TABLE species;
ALTER TABLE species__ RENAME TO species;
PRAGMA foreign_keys=ON;
COMMIT;
-- Down
PRAGMA foreign_keys=OFF;
-- Nothing to do, too complex to roll back
PRAGMA foreign_keys=ON;