-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path004-gallformers.sql
176 lines (159 loc) · 4.77 KB
/
004-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
-- Up
PRAGMA foreign_keys=OFF;
-- simplify the relationship between species and gall, make it 1-1 and store the relationship ids on boths sides
ALTER TABLE species ADD COLUMN gallid INTEGER;
UPDATE species
SET gallid = (
SELECT gall.id
FROM gall
WHERE gall.species_id = species.id
);
-- add some NOT NULL constraints to better model our data
ALTER TABLE source RENAME TO _source_old;
CREATE TABLE source (
id INTEGER PRIMARY KEY
NOT NULL,
title TEXT UNIQUE
NOT NULL,
author TEXT NOT NULL, -- add NOT NULL in 004
pubyear TEXT NOT NULL, -- add NOT NULL in 004
link TEXT NOT NULL, -- add NOT NULL in 004
citation TEXT NOT NULL -- add NOT NULL in 004
);
INSERT INTO source (id, title, author, pubyear, link, citation)
SELECT id, title, author, pubyear, link, citation
FROM _source_old;
ALTER TABLE speciessource RENAME TO _speciessource_old;
CREATE TABLE speciessource (
id INTEGER PRIMARY KEY
NOT NULL,
species_id INTEGER NOT NULL, -- add NOT NULL in 004
source_id INTEGER NOT NULL, -- add NOT NULL in 004
description TEXT DEFAULT '',
-- add a column to speciessource to track the "default" description that applies to the species
useasdefault INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (
species_id
)
REFERENCES species (id),
FOREIGN KEY (
source_id
)
REFERENCES source (id)
);
INSERT INTO speciessource (id, species_id, source_id, description)
SELECT id, species_id, source_id, description
FROM _speciessource_old;
-- we want the species-gall relationship to be non-optional, this creates complexities
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,
alignment_id INTEGER,
walls_id INTEGER,
cells_id INTEGER,
color_id INTEGER,
shape_id INTEGER,
FOREIGN KEY (
species_id
)
REFERENCES species (id) ON DELETE CASCADE, -- if we delete the gall we want to delete the species as well
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;
-- same for host, galllocation and galltexture mapping tables
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) ON DELETE CASCADE,
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) ON DELETE CASCADE,
FOREIGN KEY (
texture_id
)
REFERENCES texture (id)
);
INSERT INTO galltexture (id, gall_id, location_id)
SELECT id, gall_id, location_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) ON DELETE CASCADE,
FOREIGN KEY (
gall_species_id
)
REFERENCES species (id) ON DELETE CASCADE
);
INSERT INTO host (id, host_species_id, gall_species_id)
SELECT id, host_species_id, gall_species_id
FROM _host_old;
PRAGMA foreign_keys=ON;
--------------------------------------------------------------
-- Down
PRAGMA foreign_keys=OFF;
-- undo the column add
DROP TABLE source;
ALTER TABLE _source_old RENAME TO source;
DROP TABLE speciessource;
ALTER TABLE _speciessource_old RENAME TO speciessource;
DROP TABLE gall;
ALTER TABLE _gall_old RENAME TO gall;
PRAGMA foreign_keys=ON;