OXYGENHUB
# 74be97962ed8 oxygenhub / crates / hub / src / db / flow.rs
17.7 KiB · Rust · last changed Runners serve the site, an owner or one repository (refs #41, part 2) 1 month ago source blame history raw
42fa9554 Andy McCall ↶ 1
//! OxygenFlow runs, jobs and logs (hub spec §13.2).
2
3
use super::{now_ms, Pool};
4
use sqlx::FromRow;
5
6
#[derive(Clone, Debug, FromRow)]
7
pub struct Run {
8
    pub id: i64,
9
    pub repo_id: i64,
a0d4bbfb Andy McCall ↶ 10
    /// The run's number within its repository, from 1 (migration 0019).
11
    #[sqlx(default)]
12
    pub number: i64,
1edac4b6 Andy McCall ↶ 13
    /// `push`, `manual` or `rerun:N` (migration 0020).
14
    #[sqlx(default)]
15
    pub trigger: String,
42fa9554 Andy McCall ↶ 16
    pub bookmark: String,
17
    pub state: String,
18
    pub heads: String,
19
    pub pipeline_hash: String,
20
    pub status: String,
21
    pub error: String,
22
    pub actor: String,
23
    pub created_ms: i64,
24
    pub finished_ms: Option<i64>,
25
}
26
27
impl Run {
1edac4b6 Andy McCall ↶ 28
    /// How the run page says what started it.
29
    pub fn started_by(&self) -> String {
30
        match self.trigger.as_str() {
31
            "manual" => format!("started by {}", self.actor),
32
            t if t.starts_with("rerun:") => format!("re-run of #{} by {}", &t[6..], self.actor),
33
            _ => format!("pushed by {}", self.actor),
34
        }
35
    }
36
42fa9554 Andy McCall ↶ 37
    pub fn head_list(&self) -> Vec<&str> {
38
        self.heads.split('+').filter(|h| !h.is_empty()).collect()
39
    }
40
41
    pub fn live(&self) -> bool {
42
        matches!(self.status.as_str(), "queued" | "running")
43
    }
44
}
45
46
#[derive(Clone, Debug, FromRow)]
47
pub struct Job {
48
    pub id: i64,
49
    pub run_id: i64,
50
    pub name: String,
51
    pub depth: i64,
52
    pub depends: String,
53
    pub spec: String,
54
    pub sandbox: String,
55
    pub runtime: String,
56
    pub status: String,
57
    pub runner: String,
58
    pub token_hash: String,
59
    pub started_ms: Option<i64>,
60
    pub finished_ms: Option<i64>,
61
    pub exit_code: Option<i64>,
62
    pub error: String,
63
    pub receipt: String,
64
    pub receipt_hash: String,
65
    pub log_count: i64,
fd3db644 Andy McCall ↶ 66
    /// Comma-separated ids of other repositories the job token may read.
67
    pub also_read: String,
cc19697f Andy McCall ↶ 68
    /// The receipt attestation kept in the repository (Oxygen spec §14.6).
69
    pub receipt_attest: String,
70
    /// Who signed it: the runner's identity name.
71
    pub receipt_signer: String,
72
    /// Why a signed receipt was not kept.
73
    pub receipt_note: String,
42fa9554 Andy McCall ↶ 74
}
75
76
impl Job {
77
    pub fn depends_on(&self) -> Vec<String> {
78
        serde_json::from_str(&self.depends).unwrap_or_default()
79
    }
80
81
    pub fn done(&self) -> bool {
82
        matches!(self.status.as_str(), "passed" | "failed" | "skipped" | "error")
83
    }
84
}
85
86
#[derive(Clone, Debug, FromRow)]
87
pub struct LogLine {
88
    pub seq: i64,
89
    pub t: i64,
90
    pub stream: String,
91
    pub text: String,
92
}
93
94
/// A job as the trigger creates it.
95
pub struct NewJob {
96
    pub name: String,
97
    pub depth: i64,
98
    pub depends: Vec<String>,
99
    pub spec: String,
100
    pub sandbox: String,
101
    pub runtime: String,
102
}
103
104
pub struct NewRun<'a> {
105
    pub repo_id: i64,
106
    pub bookmark: &'a str,
107
    pub state: &'a str,
108
    pub heads: &'a str,
109
    pub pipeline_hash: &'a str,
110
    pub actor: &'a str,
1edac4b6 Andy McCall ↶ 111
    /// `push`, `manual` or `rerun:N`.
112
    pub trigger: &'a str,
42fa9554 Andy McCall ↶ 113
    /// Problems when the pipeline is invalid: the run is recorded in error.
114
    pub error: Option<&'a str>,
115
}
116
117
pub async fn create_run(pool: &Pool, r: NewRun<'_>, jobs: &[NewJob]) -> sqlx::Result<i64> {
941abdd6 Andy McCall ↶ 118
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 119
    let now = now_ms();
120
    let status = if r.error.is_some() { "error" } else { "queued" };
121
    let id: i64 = sqlx::query_scalar(
1edac4b6 Andy McCall ↶ 122
        "INSERT INTO flow_runs (repo_id, number, bookmark, state, heads, pipeline_hash, status, error, actor, trigger, created_ms, finished_ms) \
123
         VALUES (?, (SELECT COALESCE(MAX(number), 0) + 1 FROM flow_runs WHERE repo_id = ?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
42fa9554 Andy McCall ↶ 124
    )
125
    .bind(r.repo_id)
a0d4bbfb Andy McCall ↶ 126
    .bind(r.repo_id)
42fa9554 Andy McCall ↶ 127
    .bind(r.bookmark)
128
    .bind(r.state)
129
    .bind(r.heads)
130
    .bind(r.pipeline_hash)
131
    .bind(status)
132
    .bind(r.error.unwrap_or(""))
133
    .bind(r.actor)
1edac4b6 Andy McCall ↶ 134
    .bind(r.trigger)
42fa9554 Andy McCall ↶ 135
    .bind(now)
136
    .bind(r.error.map(|_| now))
137
    .fetch_one(&mut *tx)
138
    .await?;
139
    for j in jobs {
140
        sqlx::query(
141
            "INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'queued')",
142
        )
143
        .bind(id)
144
        .bind(&j.name)
145
        .bind(j.depth)
146
        .bind(serde_json::to_string(&j.depends).unwrap_or_else(|_| "[]".into()))
147
        .bind(&j.spec)
148
        .bind(&j.sandbox)
149
        .bind(&j.runtime)
150
        .execute(&mut *tx)
151
        .await?;
152
    }
153
    tx.commit().await?;
154
    Ok(id)
155
}
156
bf3d0754 Andy McCall ↶ 157
/// A job waiting for or held by a runner, with where it comes from.
158
#[derive(Clone, Debug, FromRow)]
159
pub struct QueueEntry {
160
    pub job_id: i64,
161
    pub run_id: i64,
162
    pub repo_id: i64,
163
    pub owner: String,
164
    pub name: String,
165
    pub visibility: String,
166
    pub run_number: i64,
167
    pub job: String,
168
    pub status: String,
169
}
170
171
/// Every job of a live run that is running or queued: running ones first,
172
/// then queued ones in the order runners claim them (hub spec §13.2).
173
pub async fn queue(pool: &Pool) -> sqlx::Result<Vec<QueueEntry>> {
174
    sqlx::query_as(
175
        "SELECT j.id AS job_id, j.run_id, r.repo_id, p.owner, p.name, p.visibility, r.number AS run_number, j.name AS job, j.status \
176
         FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories p ON p.id = r.repo_id \
177
         WHERE j.status IN ('queued', 'running') AND r.status IN ('queued', 'running') \
178
         ORDER BY CASE j.status WHEN 'running' THEN 0 ELSE 1 END, j.run_id, j.depth, j.id",
179
    )
180
    .fetch_all(pool)
181
    .await
182
}
183
90326c96 Andy McCall ↶ 184
/// A repository's runs, newest first, numbered below `before`.
185
pub async fn runs_by_number(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> {
186
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND number < ? ORDER BY number DESC LIMIT ?")
187
        .bind(repo_id)
188
        .bind(before.unwrap_or(i64::MAX))
189
        .bind(limit)
190
        .fetch_all(pool)
191
        .await
192
}
193
42fa9554 Andy McCall ↶ 194
pub async fn runs(pool: &Pool, repo_id: i64, limit: i64, before: Option<i64>) -> sqlx::Result<Vec<Run>> {
195
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND id < ? ORDER BY id DESC LIMIT ?")
196
        .bind(repo_id)
197
        .bind(before.unwrap_or(i64::MAX))
198
        .bind(limit)
199
        .fetch_all(pool)
200
        .await
201
}
202
a0d4bbfb Andy McCall ↶ 203
/// A repository's run by its number there.
204
pub async fn run(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<Run>> {
205
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND number = ?").bind(repo_id).bind(number).fetch_optional(pool).await
42fa9554 Andy McCall ↶ 206
}
207
208
pub async fn run_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<Run>> {
209
    sqlx::query_as("SELECT * FROM flow_runs WHERE id = ?").bind(id).fetch_optional(pool).await
210
}
211
212
pub async fn jobs(pool: &Pool, run_id: i64) -> sqlx::Result<Vec<Job>> {
213
    sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ? ORDER BY depth, name").bind(run_id).fetch_all(pool).await
214
}
215
216
pub async fn job(pool: &Pool, id: i64) -> sqlx::Result<Option<Job>> {
217
    sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(id).fetch_optional(pool).await
218
}
219
220
/// The running job a job token belongs to, while it may still be used.
221
pub async fn job_by_token(pool: &Pool, token_hash: &str, max_age_ms: i64) -> sqlx::Result<Option<Job>> {
222
    sqlx::query_as("SELECT * FROM flow_jobs WHERE token_hash = ? AND status = 'running' AND started_ms >= ?")
223
        .bind(token_hash)
224
        .bind(now_ms() - max_age_ms)
225
        .fetch_optional(pool)
226
        .await
227
}
228
229
/// Take the oldest queued job whose dependencies all passed and whose tier
230
/// the runner offers, and mark it running for `runner` with a token.
fd3db644 Andy McCall ↶ 231
/// Let a job's token also read these repositories (its pinned subrepos).
232
pub async fn set_also_read(pool: &Pool, job_id: i64, repo_ids: &[i64]) -> sqlx::Result<()> {
233
    let list: Vec<String> = repo_ids.iter().map(|i| i.to_string()).collect();
234
    sqlx::query("UPDATE flow_jobs SET also_read = ? WHERE id = ?").bind(list.join(",")).bind(job_id).execute(pool).await?;
235
    Ok(())
236
}
237
238
impl Job {
239
    pub fn also_read_ids(&self) -> Vec<i64> {
240
        self.also_read.split(',').filter_map(|x| x.trim().parse().ok()).collect()
241
    }
242
}
243
76bc5a05 Andy McCall ↶ 244
/// What a runner can take on: free memory (MiB) and vCPUs, `None` for no limit.
245
#[derive(Clone, Copy, Debug, Default)]
246
pub struct Room {
247
    pub memory_mib: Option<u32>,
248
    pub cpus: Option<u32>,
249
}
250
251
impl Room {
252
    /// Whether a job's sandbox (its stored JSON) fits.
253
    pub fn fits(&self, sandbox_json: &str) -> bool {
254
        let v: serde_json::Value = serde_json::from_str(sandbox_json).unwrap_or_default();
255
        let need = |k: &str| v[k].as_u64().unwrap_or(0);
256
        self.memory_mib.is_none_or(|m| need("memory_mib") <= m as u64) && self.cpus.is_none_or(|c| need("cpus") <= c as u64)
257
    }
258
}
259
42fa9554 Andy McCall ↶ 260
pub async fn claim(pool: &Pool, runtimes: &[String], runner: &str, token_hash: &str) -> sqlx::Result<Option<Job>> {
74be9796 Andy McCall ↶ 261
    claim_within(pool, runtimes, runner, token_hash, Room::default(), &Default::default()).await
76bc5a05 Andy McCall ↶ 262
}
263
264
/// The oldest runnable job that fits in `room`.
74be9796 Andy McCall ↶ 265
/// The first queued job a runner with these tiers, this room and this
266
/// scope may take, now running under `runner`. A scoped runner never sees
267
/// another scope's jobs (oxygenhub#41).
268
pub async fn claim_within(
269
    pool: &Pool,
270
    runtimes: &[String],
271
    runner: &str,
272
    token_hash: &str,
273
    room: Room,
274
    scope: &super::runners::Scope,
275
) -> sqlx::Result<Option<Job>> {
941abdd6 Andy McCall ↶ 276
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 277
    let queued: Vec<Job> = sqlx::query_as(
74be9796 Andy McCall ↶ 278
        "SELECT j.* FROM flow_jobs j JOIN flow_runs r ON r.id = j.run_id JOIN repositories rep ON rep.id = r.repo_id \
279
         WHERE j.status = 'queued' AND r.status IN ('queued', 'running') \
280
         AND (? = '' OR rep.owner = ?) AND (? IS NULL OR r.repo_id = ?) \
281
         ORDER BY j.run_id, j.depth, j.id LIMIT 200",
42fa9554 Andy McCall ↶ 282
    )
74be9796 Andy McCall ↶ 283
    .bind(&scope.owner)
284
    .bind(&scope.owner)
285
    .bind(scope.repo_id)
286
    .bind(scope.repo_id)
42fa9554 Andy McCall ↶ 287
    .fetch_all(&mut *tx)
288
    .await?;
289
    for j in queued {
76bc5a05 Andy McCall ↶ 290
        if !runtimes.iter().any(|r| r == &j.runtime) || !room.fits(&j.sandbox) {
42fa9554 Andy McCall ↶ 291
            continue;
292
        }
293
        let deps = j.depends_on();
294
        if !deps.is_empty() {
295
            let mut passed = 0i64;
296
            for d in &deps {
297
                let st: Option<String> = sqlx::query_scalar("SELECT status FROM flow_jobs WHERE run_id = ? AND name = ?")
298
                    .bind(j.run_id)
299
                    .bind(d)
300
                    .fetch_optional(&mut *tx)
301
                    .await?;
302
                if st.as_deref() == Some("passed") {
303
                    passed += 1;
304
                }
305
            }
306
            if passed != deps.len() as i64 {
307
                continue;
308
            }
309
        }
310
        let now = now_ms();
311
        let done = sqlx::query(
312
            "UPDATE flow_jobs SET status = 'running', runner = ?, token_hash = ?, started_ms = ? WHERE id = ? AND status = 'queued'",
313
        )
314
        .bind(runner)
315
        .bind(token_hash)
316
        .bind(now)
317
        .bind(j.id)
318
        .execute(&mut *tx)
319
        .await?;
320
        if done.rows_affected() == 1 {
321
            sqlx::query("UPDATE flow_runs SET status = 'running' WHERE id = ? AND status = 'queued'")
322
                .bind(j.run_id)
323
                .execute(&mut *tx)
324
                .await?;
325
            tx.commit().await?;
326
            return job(pool, j.id).await;
327
        }
328
    }
329
    tx.commit().await?;
330
    Ok(None)
331
}
332
333
/// Append records; returns how many were kept (the rest exceed the cap).
334
pub async fn append_log(pool: &Pool, job_id: i64, records: &[flow_core::api::Record], cap: i64) -> sqlx::Result<usize> {
941abdd6 Andy McCall ↶ 335
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 336
    let count: i64 = sqlx::query_scalar("SELECT log_count FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?;
337
    let room = (cap - count).max(0) as usize;
338
    let keep = &records[..records.len().min(room)];
339
    for (i, r) in keep.iter().enumerate() {
340
        sqlx::query("INSERT INTO flow_logs (job_id, seq, t, stream, text) VALUES (?, ?, ?, ?, ?)")
341
            .bind(job_id)
342
            .bind(count + i as i64 + 1)
343
            .bind(r.t as i64)
344
            .bind(&r.stream)
345
            .bind(&r.text)
346
            .execute(&mut *tx)
347
            .await?;
348
    }
349
    sqlx::query("UPDATE flow_jobs SET log_count = log_count + ? WHERE id = ?")
350
        .bind(keep.len() as i64)
351
        .bind(job_id)
352
        .execute(&mut *tx)
353
        .await?;
354
    tx.commit().await?;
355
    Ok(keep.len())
356
}
357
358
pub async fn log(pool: &Pool, job_id: i64, after: i64, limit: i64) -> sqlx::Result<Vec<LogLine>> {
359
    sqlx::query_as("SELECT seq, t, stream, text FROM flow_logs WHERE job_id = ? AND seq > ? ORDER BY seq LIMIT ?")
360
        .bind(job_id)
361
        .bind(after)
362
        .bind(limit)
363
        .fetch_all(pool)
364
        .await
365
}
366
367
pub struct Outcome<'a> {
368
    pub status: &'a str,
369
    pub exit_code: Option<i64>,
370
    pub error: &'a str,
371
    pub receipt: &'a str,
372
    pub receipt_hash: &'a str,
373
}
374
375
/// Record a job's end, skip the jobs that can no longer run, and settle the
376
/// run. Returns the jobs whose status changed (for their checks).
377
pub async fn finish(pool: &Pool, job_id: i64, o: Outcome<'_>) -> sqlx::Result<Vec<Job>> {
941abdd6 Andy McCall ↶ 378
    let mut tx = super::begin_write(pool).await?;
42fa9554 Andy McCall ↶ 379
    let now = now_ms();
380
    let j: Job = sqlx::query_as("SELECT * FROM flow_jobs WHERE id = ?").bind(job_id).fetch_one(&mut *tx).await?;
381
    sqlx::query("UPDATE flow_jobs SET status = ?, exit_code = ?, error = ?, receipt = ?, receipt_hash = ?, finished_ms = ?, token_hash = '' WHERE id = ?")
382
        .bind(o.status)
383
        .bind(o.exit_code)
384
        .bind(o.error)
385
        .bind(o.receipt)
386
        .bind(o.receipt_hash)
387
        .bind(now)
388
        .bind(job_id)
389
        .execute(&mut *tx)
390
        .await?;
391
    let mut changed = vec![job_id];
392
    // Dependents of anything that did not pass are skipped, transitively.
393
    loop {
394
        let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?;
395
        let blocked: Vec<i64> = all
396
            .iter()
397
            .filter(|x| x.status == "queued")
398
            .filter(|x| {
399
                x.depends_on()
400
                    .iter()
401
                    .any(|d| all.iter().any(|y| &y.name == d && matches!(y.status.as_str(), "failed" | "skipped" | "error")))
402
            })
403
            .map(|x| x.id)
404
            .collect();
405
        if blocked.is_empty() {
406
            break;
407
        }
408
        for id in blocked {
409
            sqlx::query("UPDATE flow_jobs SET status = 'skipped', finished_ms = ? WHERE id = ?")
410
                .bind(now)
411
                .bind(id)
412
                .execute(&mut *tx)
413
                .await?;
414
            changed.push(id);
415
        }
416
    }
417
    let all: Vec<Job> = sqlx::query_as("SELECT * FROM flow_jobs WHERE run_id = ?").bind(j.run_id).fetch_all(&mut *tx).await?;
418
    if all.iter().all(Job::done) {
419
        let status = if all.iter().all(|x| x.status == "passed") { "passed" } else { "failed" };
420
        sqlx::query("UPDATE flow_runs SET status = ?, finished_ms = ? WHERE id = ?")
421
            .bind(status)
422
            .bind(now)
423
            .bind(j.run_id)
424
            .execute(&mut *tx)
425
            .await?;
426
    }
427
    tx.commit().await?;
428
    let mut out = Vec::new();
429
    for id in changed {
430
        if let Some(x) = job(pool, id).await? {
431
            out.push(x);
432
        }
433
    }
434
    Ok(out)
435
}
736f6813 Andy McCall ↶ 436
437
/// The newest run of exactly this state (heads joined with `+`, sorted).
438
pub async fn latest_for_heads(pool: &Pool, repo_id: i64, heads: &str) -> sqlx::Result<Option<Run>> {
439
    sqlx::query_as("SELECT * FROM flow_runs WHERE repo_id = ? AND heads = ? ORDER BY id DESC LIMIT 1")
440
        .bind(repo_id)
441
        .bind(heads)
442
        .fetch_optional(pool)
443
        .await
cc19697f Andy McCall ↶ 444
}
445
446
/// Record what became of job `job_id`'s signed receipt.
447
pub async fn set_receipt(pool: &Pool, job_id: i64, attest: &str, signer: &str, note: &str) -> sqlx::Result<()> {
448
    sqlx::query("UPDATE flow_jobs SET receipt_attest = ?, receipt_signer = ?, receipt_note = ? WHERE id = ?")
449
        .bind(attest)
450
        .bind(signer)
451
        .bind(note)
452
        .bind(job_id)
453
        .execute(pool)
454
        .await?;
455
    Ok(())
736f6813 Andy McCall ↶ 456
}
a3c8288b Andy McCall ↶ 457
458
/// A job's artifact, kept as a blob of the run's repository.
459
#[derive(Clone, Debug, FromRow)]
460
pub struct Artifact {
461
    pub job_id: i64,
462
    pub name: String,
463
    pub path: String,
464
    pub blob: String,
465
    pub size: i64,
466
    /// BLAKE3 of the bytes.
467
    pub content: String,
468
    pub created_ms: i64,
469
}
470
471
pub async fn add_artifact(pool: &Pool, a: &Artifact) -> sqlx::Result<()> {
472
    sqlx::query("INSERT OR REPLACE INTO flow_artifacts (job_id, name, path, blob, size, content, created_ms) VALUES (?, ?, ?, ?, ?, ?, ?)")
473
        .bind(a.job_id)
474
        .bind(&a.name)
475
        .bind(&a.path)
476
        .bind(&a.blob)
477
        .bind(a.size)
478
        .bind(&a.content)
479
        .bind(a.created_ms)
480
        .execute(pool)
481
        .await?;
482
    Ok(())
483
}
484
485
/// Job `job_id`'s artifacts, by name.
486
pub async fn artifacts(pool: &Pool, job_id: i64) -> sqlx::Result<Vec<Artifact>> {
487
    sqlx::query_as("SELECT * FROM flow_artifacts WHERE job_id = ? ORDER BY name").bind(job_id).fetch_all(pool).await
488
}
10708ab8 Andy McCall ↶ 489
490
/// Where an artifact with given content came from: the run and job that
491
/// built it, and the job's signed receipt when one was kept.
492
#[derive(Clone, Debug, FromRow)]
493
pub struct Provenance {
494
    pub run_number: i64,
495
    pub job: String,
496
    pub name: String,
497
    pub receipt_attest: String,
498
    pub receipt_signer: String,
499
}
500
501
/// The build of the bytes whose BLAKE3 is `content` in repository
502
/// `repo_id`: a passed job, one with a kept signed receipt first, newest
503
/// run first.
504
pub async fn provenance(pool: &Pool, repo_id: i64, content: &str) -> sqlx::Result<Option<Provenance>> {
505
    sqlx::query_as(
506
        "SELECT r.number AS run_number, j.name AS job, a.name AS name, j.receipt_attest, j.receipt_signer \
507
         FROM flow_artifacts a JOIN flow_jobs j ON j.id = a.job_id JOIN flow_runs r ON r.id = j.run_id \
508
         WHERE r.repo_id = ? AND a.content = ? AND j.status = 'passed' \
509
         ORDER BY (j.receipt_attest != '') DESC, r.number DESC LIMIT 1",
510
    )
511
    .bind(repo_id)
512
    .bind(content)
513
    .fetch_optional(pool)
514
    .await
515
}