-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path011-gallformers.sql
211 lines (189 loc) · 7.06 KB
/
011-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
-- Up
PRAGMA foreign_keys=OFF;
-- add the the unknown family and genus as well as the relationships between the two
INSERT INTO taxonomy (id, name, description, type) VALUES (NULL, 'Unknown', 'Unknown', 'family');
INSERT INTO taxonomy (id, name, description, type, parent_id)
SELECT NULL, 'Unknown', 'Unknown', 'genus', id
FROM taxonomy WHERE name = 'Unknown' AND type='family';
INSERT INTO taxonomytaxonomy (taxonomy_id, child_id)
SELECT parent_id, id
FROM taxonomy WHERE name = 'Unknown' AND type = 'genus';
-- fix up bad schema, missing NOT NULL constraints on key fields
CREATE TABLE aliasspecies__ (
species_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
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)
);
INSERT INTO aliasspecies__ (species_id, alias_id)
SELECT species_id, alias_id
FROM aliasspecies;
DROP TABLE aliasspecies;
ALTER TABLE aliasspecies__ RENAME TO aliasspecies;
CREATE TABLE gallspecies__ (
species_id INTEGER NOT NULL,
gall_id INTEGER NOT NULL,
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)
);
INSERT INTO gallspecies__ (species_id, gall_id)
SELECT species_id, gall_id
FROM gallspecies;
DROP TABLE gallspecies;
ALTER TABLE gallspecies__ RENAME TO gallspecies;
CREATE TABLE taxonomyalias__ (
taxonomy_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
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)
);
INSERT INTO taxonomyalias__ (taxonomy_id, alias_id)
SELECT taxonomy_id, alias_id
FROM taxonomyalias;
DROP TABLE taxonomyalias;
ALTER TABLE taxonomyalias__ RENAME TO taxonomyalias;
-- case problem in foreign key definitions. oops
CREATE TABLE gall__ (
id INTEGER PRIMARY KEY NOT NULL,
taxoncode TEXT NOT NULL CHECK (taxoncode = 'gall'),
detachable INTEGER,
undescribed BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY (taxoncode) REFERENCES taxontype (taxoncode)
);
INSERT INTO gall__ (id, taxoncode, detachable, undescribed)
SELECT id, taxoncode, detachable, undescribed
FROM gall;
DROP TABLE gall;
ALTER TABLE gall__ RENAME TO gall;
CREATE TABLE species__ (
id INTEGER PRIMARY KEY NOT NULL,
taxoncode TEXT,
name TEXT UNIQUE NOT NULL,
datacomplete BOOLEAN DEFAULT 0 NOT NULL,
abundance_id INTEGER,
FOREIGN KEY (
taxoncode
)
REFERENCES taxontype (taxoncode),
FOREIGN KEY (
abundance_id
)
REFERENCES abundance (id)
);
INSERT INTO species__ (id, taxoncode, name, datacomplete, abundance_id)
SELECT id, taxoncode, name, datacomplete, abundance_id
FROM species;
DROP TABLE species;
ALTER TABLE species__ RENAME TO species;
-- these are all of the gall property tables, we will remove the unnecessary id and create a composite id like
-- with the other many-to-many tables
CREATE TABLE gallcolor__ (
gall_id INTEGER NOT NULL,
color_id INTEGER NOT NULL,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
FOREIGN KEY (color_id) REFERENCES color (id) ON DELETE CASCADE,
PRIMARY KEY (gall_id, color_id)
);
INSERT INTO gallcolor__ (gall_id, color_id)
SELECT gall_id, color_id
FROM gallcolor;
DROP TABLE gallcolor;
ALTER TABLE gallcolor__ RENAME TO gallcolor;
CREATE TABLE gallshape__ (
gall_id INTEGER NOT NULL,
shape_id INTEGER NOT NULL,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
FOREIGN KEY (shape_id) REFERENCES shape (id) ON DELETE CASCADE,
PRIMARY KEY (gall_id, shape_id)
);
INSERT INTO gallshape__ (gall_id, shape_id)
SELECT gall_id, shape_id
FROM gallshape;
DROP TABLE gallshape;
ALTER TABLE gallshape__ RENAME TO gallshape;
CREATE TABLE gallcells__ (
gall_id INTEGER NOT NULL,
cells_id INTEGER NOT NULL,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
FOREIGN KEY (cells_id) REFERENCES cells (id) ON DELETE CASCADE,
PRIMARY KEY (gall_id, cells_id)
);
INSERT INTO gallcells__ (gall_id, cells_id)
SELECT gall_id, cells_id
FROM gallcells;
DROP TABLE gallcells;
ALTER TABLE gallcells__ RENAME TO gallcells;
CREATE TABLE gallwalls__ (
gall_id INTEGER NOT NULL,
walls_id INTEGER NOT NULL,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
FOREIGN KEY (walls_id) REFERENCES walls (id) ON DELETE CASCADE,
PRIMARY KEY (gall_id, walls_id)
);
INSERT INTO gallwalls__ (gall_id, walls_id)
SELECT gall_id, walls_id
FROM gallwalls;
DROP TABLE gallwalls;
ALTER TABLE gallwalls__ RENAME TO gallwalls;
CREATE TABLE gallalignment__ (
gall_id INTEGER NOT NULL,
alignment_id INTEGER NOT NULL,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
FOREIGN KEY (alignment_id) REFERENCES alignment (id) ON DELETE CASCADE,
PRIMARY KEY (gall_id, alignment_id)
);
INSERT INTO gallalignment__ (gall_id, alignment_id)
SELECT gall_id, alignment_id
FROM gallalignment;
DROP TABLE gallalignment;
ALTER TABLE gallalignment__ RENAME TO gallalignment;
CREATE TABLE galllocation__ (
gall_id INTEGER NOT NULL,
location_id INTEGER NOT NULL,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
FOREIGN KEY (location_id) REFERENCES location (id),
PRIMARY KEY (gall_id, location_id)
);
INSERT INTO galllocation__ (gall_id, location_id)
SELECT gall_id, location_id
FROM galllocation;
DROP TABLE galllocation;
ALTER TABLE galllocation__ RENAME TO galllocation;
CREATE TABLE galltexture__ (
gall_id INTEGER NOT NULL,
texture_id INTEGER NOT NULL,
FOREIGN KEY (gall_id) REFERENCES gall (id) ON DELETE CASCADE,
FOREIGN KEY (texture_id) REFERENCES texture (id),
PRIMARY KEY (gall_id, texture_id)
);
INSERT INTO galltexture__ (gall_id, texture_id)
SELECT gall_id, texture_id
FROM galltexture;
DROP TABLE galltexture;
ALTER TABLE galltexture__ RENAME TO galltexture;
-- make stuff NOT NULL
CREATE TABLE speciessource__ (
id INTEGER PRIMARY KEY NOT NULL,
species_id INTEGER NOT NULL,
source_id INTEGER NOT NULL,
description TEXT DEFAULT '' NOT NULL,
useasdefault INTEGER DEFAULT 0 NOT NULL,
externallink TEXT DEFAULT '' NOT NULL,
alias_id INTEGER,
FOREIGN KEY (species_id) REFERENCES species (id) ON DELETE CASCADE,
FOREIGN KEY (source_id) REFERENCES source (id) ON DELETE CASCADE,
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, alias_id
FROM speciessource;
DROP TABLE speciessource;
ALTER TABLE speciessource__ RENAME TO speciessource;
PRAGMA foreign_keys=ON;
--------------------------------------------------------------
-- Down
PRAGMA foreign_keys=OFF;
PRAGMA foreign_keys=ON;