-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path002.gallformers.sql
348 lines (299 loc) · 9.11 KB
/
002.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
-- Up
-- there was some bad data in the database and we need to get rid of it before we try and add uniqueness constraints
DELETE FROM species where id IN (46, 366);
-- add uniqueness to all of the primary non-id fields for all of the data tables
-- N.B. Sqlite does not allow adding a constraint to an already existing table, so we have to create new tables,
-- rename the old ones, and then migrate the data.
PRAGMA foreign_keys=OFF;
ALTER TABLE location RENAME TO _location_old;
CREATE TABLE location (
id INTEGER PRIMARY KEY NOT NULL,
location TEXT UNIQUE NOT NULL,
description TEXT
);
INSERT INTO location (id, location, description)
SELECT id, location, description
FROM _location_old;
ALTER TABLE texture RENAME TO _texture_old;
CREATE TABLE texture (
id INTEGER PRIMARY KEY NOT NULL,
texture TEXT UNIQUE NOT NULL,
description TEXT
);
INSERT INTO texture (id, texture, description)
SELECT id, texture, description
FROM _texture_old;
ALTER TABLE walls RENAME TO _walls_old;
CREATE TABLE walls (
id INTEGER PRIMARY KEY NOT NULL,
walls TEXT UNIQUE NOT NULL,
description TEXT
);
INSERT INTO walls (id, walls, description)
SELECT id, walls, description
FROM _walls_old;
ALTER TABLE cells RENAME TO _cells_old;
CREATE TABLE cells (
id INTEGER PRIMARY KEY NOT NULL,
cells TEXT UNIQUE NOT NULL,
description TEXT
);
INSERT INTO cells (id, cells, description)
SELECT id, cells, description
FROM _cells_old;
ALTER TABLE color RENAME TO _color_old;
CREATE TABLE color (
id INTEGER PRIMARY KEY NOT NULL,
color TEXT UNIQUE NOT NULL
);
INSERT INTO color (id, color)
SELECT id, color
FROM _color_old;
ALTER TABLE alignment RENAME TO _alignment_old;
CREATE TABLE alignment (
id INTEGER PRIMARY KEY NOT NULL,
alignment TEXT UNIQUE NOT NULL,
description TEXT
);
INSERT INTO alignment (id, alignment, description)
SELECT id, alignment, description
FROM _alignment_old;
ALTER TABLE shape RENAME TO _shape_old;
CREATE TABLE shape (
id INTEGER PRIMARY KEY NOT NULL,
shape TEXT UNIQUE NOT NULL,
description TEXT
);
INSERT INTO shape (id, shape, description)
SELECT id, shape, description
FROM _shape_old;
ALTER TABLE abundance RENAME TO _abundance_old;
CREATE TABLE abundance (
id INTEGER PRIMARY KEY NOT NULL,
abundance TEXT UNIQUE NOT NULL,
description TEXT,
reference TEXT
);
INSERT INTO abundance (id, abundance, description, reference)
SELECT id, abundance, description, reference
FROM _abundance_old;
ALTER TABLE taxontype RENAME TO _taxontype_old;
CREATE TABLE taxontype (
taxoncode TEXT PRIMARY KEY NOT NULL,
description TEXT UNIQUE NOT NULL
);
INSERT INTO taxontype (taxoncode, description)
SELECT taxoncode, description
FROM _taxontype_old;
ALTER TABLE family RENAME TO _family_old;
CREATE TABLE family (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT UNIQUE NOT NULL,
description TEXT
);
INSERT INTO family (id, name, description)
SELECT id, name, description
FROM _family_old;
ALTER TABLE species RENAME TO _species_old;
CREATE TABLE species (
id INTEGER PRIMARY KEY NOT NULL,
taxoncode TEXT,
name TEXT UNIQUE NOT NULL,
synonyms TEXT,
commonnames TEXT,
genus TEXT NOT NULL,
family_id INTEGER NOT NULL,
description TEXT,
abundance_id INTEGER,
FOREIGN KEY (
taxoncode
)
REFERENCES taxontype (taxonCode),
FOREIGN KEY (
abundance_id
)
REFERENCES abundance (id),
FOREIGN KEY (
family_id
)
REFERENCES family (id)
);
INSERT INTO species (id, taxoncode, name, synonyms, commonnames, genus, family_id, description, abundance_id)
SELECT id, taxoncode, name, synonyms, commonnames, genus, family_id, description, abundance_id
FROM _species_old;
-- now we have to drop and re-add all the tables that have foreign key dependencies on any of the tables that we just
-- modified, or that are dependent on any of these tables. Why you may ask? Because sqlite "helpful" updates all of the
-- foreign keys when you rename a table. :(
ALTER TABLE gall RENAME TO _gall_old;
CREATE TABLE gall (
id INTEGER PRIMARY KEY
NOT NULL,
species_id INTEGER NOT NULL,
taxoncode TEXT NOT NULL
CHECK (taxoncode = 'gall'),
detachable INTEGER,-- boolean: 0 = false; 1 = true, standard sqlite
alignment_id INTEGER,
walls_id INTEGER,
cells_id INTEGER,
color_id INTEGER,
shape_id INTEGER,
FOREIGN KEY (
species_id
)
REFERENCES species (id),
FOREIGN KEY (
taxonCode
)
REFERENCES taxontype (taxonCode),
FOREIGN KEY (
walls_id
)
REFERENCES walls (id),
FOREIGN KEY (
cells_id
)
REFERENCES cells (id),
FOREIGN KEY (
color_id
)
REFERENCES color (id),
FOREIGN KEY (
shape_id
)
REFERENCES shape (id),
FOREIGN KEY (
alignment_id
)
REFERENCES alignment (id)
);
INSERT INTO gall (id, species_id, taxoncode, detachable, alignment_id, walls_id, cells_id, color_id, shape_id)
SELECT id, species_id, taxoncode, detachable, alignment_id, walls_id, cells_id, color_id, shape_id
FROM _gall_old;
ALTER TABLE galllocation RENAME TO _galllocation_old;
CREATE TABLE galllocation (
id INTEGER PRIMARY KEY
NOT NULL,
gall_id INTEGER,
location_id INTEGER,
FOREIGN KEY (
gall_id
)
REFERENCES gall (id),
FOREIGN KEY (
location_id
)
REFERENCES location (id)
);
INSERT INTO galllocation (id, gall_id, location_id)
SELECT id, gall_id, location_id
FROM _galllocation_old;
ALTER TABLE galltexture RENAME TO _galltexture_old;
CREATE TABLE galltexture (
id INTEGER PRIMARY KEY
NOT NULL,
gall_id INTEGER,
texture_id INTEGER,
FOREIGN KEY (
gall_id
)
REFERENCES gall (id),
FOREIGN KEY (
texture_id
)
REFERENCES texture (id)
);
INSERT INTO galltexture (id, gall_id, texture_id)
SELECT id, gall_id, texture_id
FROM _galltexture_old;
ALTER TABLE host RENAME TO _host_old;
CREATE TABLE host (
id INTEGER PRIMARY KEY
NOT NULL,
host_species_id INTEGER,
gall_species_id INTEGER,
FOREIGN KEY (
host_species_id
)
REFERENCES species (id),
FOREIGN KEY (
gall_species_id
)
REFERENCES species (id)
);
INSERT INTO host (id, host_species_id, gall_species_id)
SELECT id, host_species_id, gall_species_id
FROM _host_old;
ALTER TABLE speciessource RENAME TO _speciessource_old;
CREATE TABLE speciessource (
id INTEGER PRIMARY KEY
NOT NULL,
species_id INTEGER,
source_id INTEGER,
-- THIS IS A NEW column add the ability to add a description extract from a source to a source-species relationship
description TEXT DEFAULT '',
FOREIGN KEY (
species_id
)
REFERENCES species (id),
FOREIGN KEY (
source_id
)
REFERENCES source (id)
);
INSERT INTO speciessource (id, species_id, source_id)
SELECT id, species_id, source_id
FROM _speciessource_old;
PRAGMA foreign_keys=ON;
-- added some data to the property tables - were added by hand but want to keep track here.
-- INSERT INTO abundance VALUES (NULL, 'abundant', '', '');
-- INSERT INTO abundance VALUES (NULL, 'common', '', '');
-- INSERT INTO abundance VALUES (NULL, 'frequent', '', '');
-- INSERT INTO abundance VALUES (NULL, 'occasional', '', '');
-- INSERT INTO abundance VALUES (NULL, 'rare', '', '');
-- INSERT INTO location VALUES(NULL, 'stem', '');
--------------------------------------------------------------
-- Down
PRAGMA foreign_keys=OFF;
DROP TABLE location;
ALTER TABLE _location_old RENAME TO location;
DROP TABLE texture;
ALTER TABLE _texture_old RENAME TO texture;
DROP TABLE walls;
ALTER TABLE _walls_old RENAME TO walls;
DROP TABLE cells;
ALTER TABLE _cells_old RENAME TO cells;
DROP TABLE color;
ALTER TABLE _color_old RENAME TO color;
DROP TABLE alignment;
ALTER TABLE _alignment_old RENAME TO alignment;
DROP TABLE shape;
ALTER TABLE _shape_old RENAME TO shape;
DROP TABLE abundance;
ALTER TABLE _abundance_old RENAME TO abundance;
DROP TABLE taxontype;
ALTER TABLE _taxontype_old RENAME TO taxontype;
DROP TABLE family;
ALTER TABLE _family_old RENAME TO family;
DROP TABLE species;
ALTER TABLE _species_old RENAME TO species;
-- We can not drop the column that was created on speciessource as sqlite has no ability to delete a column
-- other than to create a new table and migrate data.
ALTER TABLE speciessource RENAME TO _speciessource_old;
CREATE TABLE speciessource (
id INTEGER PRIMARY KEY NOT NULL,
species_id INTEGER,
source_id INTEGER,
FOREIGN KEY (
species_id
)
REFERENCES species (id),
FOREIGN KEY (
source_id
)
REFERENCES source (id)
);
INSERT INTO speciessource (id, species_id, source_id)
SELECT id, species_id, source_id
FROM _speciessource_old;
DROP TABLE _speciessource_old;
PRAGMA foreign_keys=ON;