-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathmigrations.rs
404 lines (346 loc) · 12 KB
/
migrations.rs
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::prelude::*;
use rand::distributions::{Alphanumeric, DistString};
use rusqlite::{Connection, Transaction};
use std::collections::HashSet;
enum MigrationKind {
SQL(&'static str),
#[allow(clippy::type_complexity)]
Code(Box<dyn Fn(&Transaction) -> ::rusqlite::Result<()>>),
}
fn migrations() -> Vec<(&'static str, MigrationKind)> {
let mut migrations = Vec::new();
migrations.push((
"initial",
MigrationKind::SQL(
"
CREATE TABLE experiments (
name TEXT PRIMARY KEY,
mode TEXT NOT NULL,
cap_lints TEXT NOT NULL,
toolchain_start TEXT NOT NULL,
toolchain_end TEXT NOT NULL,
priority INTEGER NOT NULL,
created_at DATETIME NOT NULL,
status TEXT NOT NULL,
github_issue TEXT,
github_issue_url TEXT,
github_issue_number INTEGER,
assigned_to TEXT,
FOREIGN KEY (assigned_to) REFERENCES agents(name) ON DELETE SET NULL
);
CREATE TABLE experiment_crates (
experiment TEXT NOT NULL,
crate TEXT NOT NULL,
FOREIGN KEY (experiment) REFERENCES experiments(name) ON DELETE CASCADE
);
CREATE TABLE results (
experiment TEXT NOT NULL,
crate TEXT NOT NULL,
toolchain TEXT NOT NULL,
result TEXT NOT NULL,
log BLOB NOT NULL,
PRIMARY KEY (experiment, crate, toolchain) ON CONFLICT REPLACE,
FOREIGN KEY (experiment) REFERENCES experiments(name) ON DELETE CASCADE
);
CREATE TABLE shas (
experiment TEXT NOT NULL,
org TEXT NOT NULL,
name TEXT NOT NULL,
sha TEXT NOT NULL,
FOREIGN KEY (experiment) REFERENCES experiments(name) ON DELETE CASCADE
);
CREATE TABLE agents (
name TEXT PRIMARY KEY,
last_heartbeat DATETIME
);
CREATE TABLE saved_names (
issue INTEGER PRIMARY KEY ON CONFLICT REPLACE,
experiment TEXT NOT NULL
);
",
),
));
migrations.push((
"store_agents_revision",
MigrationKind::SQL(
"
ALTER TABLE agents ADD COLUMN git_revision TEXT;
",
),
));
migrations.push((
"store_skipped_crates",
MigrationKind::SQL(
"
ALTER TABLE experiment_crates ADD COLUMN skipped INTEGER NOT NULL DEFAULT 0;
",
),
));
migrations.push((
"add_ui_progress_percent_indexes",
MigrationKind::SQL(
"
CREATE INDEX experiment_crates__experiment_skipped
ON experiment_crates (experiment, skipped);
CREATE INDEX results__experiment
ON results (experiment);
",
),
));
migrations.push((
"add_more_experiment_dates",
MigrationKind::SQL(
"
ALTER TABLE experiments ADD COLUMN started_at DATETIME;
ALTER TABLE experiments ADD COLUMN completed_at DATETIME;
",
),
));
migrations.push((
"store_report_url",
MigrationKind::SQL(
"
ALTER TABLE experiments ADD COLUMN report_url TEXT;
",
),
));
migrations.push((
"stringify_toolchain_names",
MigrationKind::Code(Box::new(|t| {
#[derive(Deserialize)]
enum LegacyToolchain {
Dist(String),
TryBuild { sha: String },
Master { sha: String },
}
let fn_name = format!(
"crater_migration__{}",
Alphanumeric.sample_string(&mut rand::thread_rng(), 10)
);
t.create_scalar_function(
&fn_name,
1,
rusqlite::functions::FunctionFlags::SQLITE_DETERMINISTIC
| rusqlite::functions::FunctionFlags::SQLITE_UTF8,
|ctx| {
let legacy = ctx.get::<String>(0)?;
if let Ok(parsed) = serde_json::from_str(&legacy) {
Ok(match parsed {
LegacyToolchain::Dist(name) => name,
LegacyToolchain::TryBuild { sha } => format!("try#{sha}"),
LegacyToolchain::Master { sha } => format!("master#{sha}"),
})
} else {
Ok(legacy)
}
},
)?;
t.execute("PRAGMA foreign_keys = OFF;", [])?;
t.execute(
&format!("UPDATE experiments SET toolchain_start = {fn_name}(toolchain_start);",),
[],
)?;
t.execute(
&format!("UPDATE experiments SET toolchain_end = {fn_name}(toolchain_end);",),
[],
)?;
t.execute(
&format!("UPDATE results SET toolchain = {fn_name}(toolchain);"),
[],
)?;
t.execute("PRAGMA foreign_keys = ON;", [])?;
Ok(())
})),
));
migrations.push((
"migrate_agents_to_assignees_in_experiments",
MigrationKind::SQL(
"
CREATE TABLE experiments_new (
name TEXT PRIMARY KEY,
mode TEXT NOT NULL,
cap_lints TEXT NOT NULL,
toolchain_start TEXT NOT NULL,
toolchain_end TEXT NOT NULL,
created_at DATETIME NOT NULL,
started_at DATETIME,
completed_at DATETIME,
priority INTEGER NOT NULL,
status TEXT NOT NULL,
report_url TEXT,
github_issue TEXT,
github_issue_url TEXT,
github_issue_number INTEGER,
assigned_to TEXT
);
INSERT INTO experiments_new (
name, mode, cap_lints, toolchain_start, toolchain_end, created_at, started_at,
completed_at, priority, status, report_url, github_issue, github_issue_url,
github_issue_number, assigned_to
) SELECT
name, mode, cap_lints, toolchain_start, toolchain_end, created_at, started_at,
completed_at, priority, status, report_url, github_issue, github_issue_url,
github_issue_number,
CASE WHEN assigned_to IS NOT NULL THEN 'agent:' || assigned_to ELSE NULL END
FROM experiments;
DROP TABLE experiments;
ALTER TABLE experiments_new RENAME TO experiments;
",
),
));
migrations.push((
"create_crates_table",
MigrationKind::SQL(
"
CREATE TABLE crates (
crate TEXT NOT NULL,
list TEXT NOT NULL,
loaded_at DATETIME NOT NULL,
PRIMARY KEY (crate, list) ON CONFLICT REPLACE
);
CREATE INDEX crates__list ON crates (list);
",
),
));
migrations.push((
"add_experiment_field_ignore_blacklist",
MigrationKind::SQL(
"
ALTER TABLE experiments ADD COLUMN ignore_blacklist INTEGER NOT NULL DEFAULT 0;
",
),
));
migrations.push((
"add_encoding_type",
MigrationKind::SQL(
"
ALTER TABLE results ADD COLUMN encoding TEXT NOT NULL DEFAULT 'plain'
",
),
));
migrations.push((
"add_try_builds_table",
MigrationKind::SQL(
"
CREATE TABLE try_builds (
pr INTEGER NOT NULL,
repo STRING NOT NULL,
merge_sha TEXT NOT NULL,
base_sha TEXT NOT NULL,
PRIMARY KEY (pr, repo)
);
",
),
));
migrations.push((
"add_experiment_crates_field_assigned_to",
MigrationKind::SQL(
"
ALTER TABLE experiment_crates ADD COLUMN status TEXT NOT NULL DEFAULT 'queued';
ALTER TABLE experiment_crates ADD COLUMN assigned_to TEXT;
",
),
));
migrations.push((
"add_experiment_field_requirement",
MigrationKind::SQL(
"
ALTER TABLE experiments ADD COLUMN requirement TEXT DEFAULT 'linux';
",
),
));
migrations.push((
"add_agent_capabilities",
MigrationKind::SQL(
"
CREATE TABLE agent_capabilities (
agent_name TEXT NOT NULL,
capability TEXT NOT NULL,
PRIMARY KEY (agent_name, capability) ON CONFLICT REPLACE,
FOREIGN KEY (agent_name) REFERENCES agents(name) ON DELETE CASCADE
);
CREATE INDEX agent__name ON agent_capabilities(agent_name);
",
),
));
migrations.push((
"delete_sha_table",
MigrationKind::SQL(
"
DROP TABLE shas;
",
),
));
migrations.push((
"stringify_crate_names",
MigrationKind::Code(Box::new(|t| {
use crate::crates::Crate;
let fn_name = format!(
"crater_migration__{}",
Alphanumeric.sample_string(&mut rand::thread_rng(), 10)
);
t.create_scalar_function(
&fn_name,
1,
rusqlite::functions::FunctionFlags::SQLITE_DETERMINISTIC
| rusqlite::functions::FunctionFlags::SQLITE_UTF8,
|ctx| {
let legacy = ctx.get::<String>(0)?;
if let Ok(parsed) = serde_json::from_str::<Crate>(&legacy) {
Ok(parsed.id())
} else {
Ok(legacy)
}
},
)?;
t.execute("PRAGMA foreign_keys = OFF;", [])?;
t.execute(
&format!("UPDATE experiment_crates SET crate = {fn_name}(crate);"),
[],
)?;
t.execute(&format!("UPDATE results SET crate = {fn_name}(crate);"), [])?;
t.execute(&format!("UPDATE crates SET crate = {fn_name}(crate);"), [])?;
t.execute("PRAGMA foreign_keys = ON;", [])?;
Ok(())
})),
));
migrations.push((
"create_experiment_time_column",
MigrationKind::SQL("alter table experiment_crates add column started_at text;"),
));
migrations.push((
"create_agent_assignment",
MigrationKind::SQL("alter table agents add column latest_work_for text;"),
));
migrations
}
pub fn execute(db: &mut Connection) -> Fallible<()> {
// If the database version is 0, create the migrations table and bump it
let version: i32 = db.query_row("PRAGMA user_version;", [], |r| r.get(0))?;
if version == 0 {
db.execute("CREATE TABLE migrations (name TEXT PRIMARY KEY);", [])?;
db.execute("PRAGMA user_version = 1;", [])?;
}
let executed_migrations = {
let mut prepared = db.prepare("SELECT name FROM migrations;")?;
let mut result = HashSet::new();
for value in prepared.query_map([], |row| row.get::<_, String>(0))? {
result.insert(value?);
}
result
};
for &(name, ref migration) in &migrations() {
if !executed_migrations.contains(name) {
let t = db.transaction()?;
match migration {
MigrationKind::SQL(sql) => t.execute_batch(sql),
MigrationKind::Code(code) => code(&t),
}
.with_context(|| format!("error running migration: {name}"))?;
t.execute("INSERT INTO migrations (name) VALUES (?1)", [&name])?;
t.commit()?;
info!("executed migration: {}", name);
}
}
Ok(())
}