relay-knowledge 1.1.17

Graph-database-based knowledge graph project.
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use rusqlite::{Connection, OptionalExtension, params};

use crate::storage::StorageError;

use super::migrations::table_has_columns;

#[derive(Clone, Copy)]
struct SearchQueryIndexDescriptor {
    name: &'static str,
    table: &'static str,
    sql: &'static str,
    columns: &'static [&'static str],
    mode: SearchQueryIndexMode,
    required_table: Option<&'static str>,
    required_table_columns: &'static [&'static str],
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SearchQueryIndexMode {
    Required,
    /// Required, plus safe to precreate for a fresh multi-batch Restart while
    /// the shared chunks owner has no rows.
    RequiredForEmptyChunkOwnerRestart,
    /// Keeps its stable ordinal and exact legacy shape, but is never created
    /// or dropped by the current plan.
    Retired,
}

/// Durable progress for deferred query-index construction.
///
/// SQLite commits each index definition atomically. The checkpoint subphase is
/// the durable cursor; persisted schema is validated against it before the
/// next call creates at most one index.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::storage::sqlite::code) enum SearchQueryIndexAdvance {
    Created {
        completed_unit: usize,
        plan_complete: bool,
    },
    Complete,
}

const SEARCH_QUERY_INDEXES: &[SearchQueryIndexDescriptor] = &[
    SearchQueryIndexDescriptor {
        name: "code_repository_search_metadata_scope_path",
        table: "code_repository_search_metadata",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_search_metadata_scope_path ON code_repository_search_metadata(source_scope, path)",
        columns: &["source_scope", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_symbols_lookup",
        table: "code_repository_symbols",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_symbols_lookup ON code_repository_symbols(source_scope, name, qualified_name, path)",
        columns: &["source_scope", "name", "qualified_name", "path"],
        mode: SearchQueryIndexMode::Retired,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_symbols_name_path_lookup",
        table: "code_repository_symbols",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_symbols_name_path_lookup ON code_repository_symbols(source_scope, name, path)",
        columns: &["source_scope", "name", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_symbols_path_line_lookup",
        table: "code_repository_symbols",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_symbols_path_line_lookup ON code_repository_symbols(source_scope, path, line_end, line_start)",
        columns: &["source_scope", "path", "line_end", "line_start"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_references_lookup",
        table: "code_repository_references",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_references_lookup ON code_repository_references(source_scope, name, kind, path)",
        columns: &["source_scope", "name", "kind", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_calls_lookup",
        table: "code_repository_calls",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_calls_lookup ON code_repository_calls(source_scope, callee_name, caller_name, path)",
        columns: &["source_scope", "callee_name", "caller_name", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_feature_flags_lookup",
        table: "code_repository_feature_flags",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_feature_flags_lookup ON code_repository_feature_flags(source_scope, name, source_key, edge_kind, path)",
        columns: &["source_scope", "name", "source_key", "edge_kind", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_routes_lookup",
        table: "code_repository_routes",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_routes_lookup ON code_repository_routes(source_scope, url, http_method, path)",
        columns: &["source_scope", "url", "http_method", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_routes_handler_lookup",
        table: "code_repository_routes",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_routes_handler_lookup ON code_repository_routes(source_scope, handler_symbol_snapshot_id, path)",
        columns: &["source_scope", "handler_symbol_snapshot_id", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_imports_lookup",
        table: "code_repository_imports",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_imports_lookup ON code_repository_imports(source_scope, module, path)",
        columns: &["source_scope", "module", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_imports_target_lookup",
        table: "code_repository_imports",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_imports_target_lookup ON code_repository_imports(source_scope, target_hint, path)",
        columns: &["source_scope", "target_hint", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_dependencies_lookup",
        table: "code_repository_dependencies",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_dependencies_lookup ON code_repository_dependencies(source_scope, ecosystem, package_name, path)",
        columns: &["source_scope", "ecosystem", "package_name", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_dependencies_group_lookup",
        table: "code_repository_dependencies",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_dependencies_group_lookup ON code_repository_dependencies(source_scope, dependency_group, path)",
        columns: &["source_scope", "dependency_group", "path"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_chunks_lookup",
        table: "code_repository_chunks",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_chunks_lookup ON code_repository_chunks(source_scope, path)",
        columns: &["source_scope", "path"],
        mode: SearchQueryIndexMode::RequiredForEmptyChunkOwnerRestart,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_chunks_symbol_lookup",
        table: "code_repository_chunks",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_chunks_symbol_lookup ON code_repository_chunks(source_scope, symbol_snapshot_id)",
        columns: &["source_scope", "symbol_snapshot_id"],
        mode: SearchQueryIndexMode::RequiredForEmptyChunkOwnerRestart,
        required_table: None,
        required_table_columns: &[],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_calls_caller_lookup",
        table: "code_repository_calls",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_calls_caller_lookup ON code_repository_calls(source_scope, caller_name, path, line_start)",
        columns: &["source_scope", "caller_name", "path", "line_start"],
        mode: SearchQueryIndexMode::Required,
        required_table: Some("code_repository_calls"),
        required_table_columns: &["source_scope", "caller_name", "path", "line_start"],
    },
    SearchQueryIndexDescriptor {
        name: "code_repository_imports_scope_path_line_lookup",
        table: "code_repository_imports",
        sql: "CREATE INDEX IF NOT EXISTS code_repository_imports_scope_path_line_lookup ON code_repository_imports(source_scope, path, line_start, line_end)",
        columns: &["source_scope", "path", "line_start", "line_end"],
        mode: SearchQueryIndexMode::Required,
        required_table: None,
        required_table_columns: &[],
    },
];

const _: [(); crate::domain::CODE_QUERY_INDEX_PLAN_UNIT_COUNT] = [(); SEARCH_QUERY_INDEXES.len()];

#[cfg(test)]
#[path = "search_schema_tests.rs"]
mod tests;

pub(super) fn initialize_search_schema(connection: &Connection) -> Result<(), StorageError> {
    connection.execute_batch(
        "
        CREATE VIRTUAL TABLE IF NOT EXISTS code_repository_search USING fts5(
            source_scope UNINDEXED,
            document_kind UNINDEXED,
            record_id UNINDEXED,
            path UNINDEXED,
            language_id UNINDEXED,
            content
        );

        CREATE TABLE IF NOT EXISTS code_repository_search_metadata (
            source_scope TEXT NOT NULL,
            document_kind TEXT NOT NULL,
            record_id TEXT NOT NULL,
            path TEXT NOT NULL,
            search_rowid INTEGER PRIMARY KEY,
            UNIQUE (source_scope, document_kind, record_id)
        );
        DROP INDEX IF EXISTS code_repository_search_metadata_scope_kind;
        CREATE INDEX IF NOT EXISTS code_repository_scopes_lookup
            ON code_repository_scopes(repository_id, resolved_commit_sha, path_filters_json, language_filters_json);
        ",
    )?;
    Ok(())
}

#[cfg(test)]
pub(super) fn ensure_search_query_indexes(connection: &Connection) -> Result<(), StorageError> {
    while let SearchQueryIndexAdvance::Created { .. } =
        advance_search_query_indexes(connection, None, false)?
    {}
    Ok(())
}

/// Creates at most one missing deferred query index.
///
/// Existing indexes are accepted only when their persisted column order is
/// exact. This prevents a name collision from falsely advancing the durable
/// schema cursor after a crash or an incompatible upgrade. A legacy cursor
/// requires retired descriptors in its completed prefix to remain present;
/// current cursors permit an absent retired descriptor as a stable skip.
pub(in crate::storage::sqlite::code) fn advance_search_query_indexes(
    connection: &Connection,
    completed_unit: Option<usize>,
    require_retired_prefix: bool,
) -> Result<SearchQueryIndexAdvance, StorageError> {
    validate_query_index_cursor(completed_unit)?;
    if let Some(completed_position) = completed_unit {
        for descriptor in &SEARCH_QUERY_INDEXES[..=completed_position] {
            let persisted = persisted_query_index_columns(connection, descriptor)?;
            if descriptor.mode == SearchQueryIndexMode::Retired {
                if require_retired_prefix {
                    require_query_index_columns(descriptor, persisted)?;
                } else if let Some(columns) = persisted {
                    require_query_index_columns(descriptor, Some(columns))?;
                }
                continue;
            }
            if !query_index_descriptor_is_applicable(connection, descriptor)? {
                return Err(StorageError::Invariant(format!(
                    "durable query-index finalization unit '{}' is no longer applicable",
                    descriptor.name
                )));
            }
            require_query_index_columns(descriptor, persisted)?;
        }
    }
    advance_search_query_indexes_after_cursor(connection, completed_unit, require_retired_prefix)
}

/// Resumes a preserve-phase repair whose coarse scan already treated a
/// descriptor as inapplicable. A still-inapplicable prefix item with no
/// same-name index remains a stable skip; a collision remains fail-closed.
pub(in crate::storage::sqlite::code) fn advance_search_query_index_repair(
    connection: &Connection,
    completed_unit: Option<usize>,
    require_retired_prefix: bool,
) -> Result<SearchQueryIndexAdvance, StorageError> {
    validate_query_index_cursor(completed_unit)?;
    if let Some(completed_position) = completed_unit {
        for descriptor in &SEARCH_QUERY_INDEXES[..=completed_position] {
            let persisted = persisted_query_index_columns(connection, descriptor)?;
            if descriptor.mode == SearchQueryIndexMode::Retired {
                if require_retired_prefix {
                    require_query_index_columns(descriptor, persisted)?;
                } else if let Some(columns) = persisted {
                    require_query_index_columns(descriptor, Some(columns))?;
                }
                continue;
            }
            if !query_index_descriptor_is_applicable(connection, descriptor)? {
                if let Some(columns) = persisted {
                    require_query_index_columns(descriptor, Some(columns))?;
                }
                continue;
            }
            require_query_index_columns(descriptor, persisted)?;
        }
    }
    advance_search_query_indexes_after_cursor(connection, completed_unit, require_retired_prefix)
}

fn validate_query_index_cursor(completed_unit: Option<usize>) -> Result<(), StorageError> {
    if completed_unit.is_some_and(|unit| unit >= SEARCH_QUERY_INDEXES.len()) {
        return Err(StorageError::Invariant(format!(
            "durable query-index finalization unit {:?} exceeds plan length {}",
            completed_unit,
            SEARCH_QUERY_INDEXES.len()
        )));
    }
    Ok(())
}

fn advance_search_query_indexes_after_cursor(
    connection: &Connection,
    completed_unit: Option<usize>,
    require_retired_prefix: bool,
) -> Result<SearchQueryIndexAdvance, StorageError> {
    let next_position = completed_unit.map_or(0, |position| position + 1);
    for (position, descriptor) in SEARCH_QUERY_INDEXES.iter().enumerate().skip(next_position) {
        let persisted = persisted_query_index_columns(connection, descriptor)?;
        if descriptor.mode == SearchQueryIndexMode::Retired {
            if require_retired_prefix {
                require_query_index_columns(descriptor, persisted)?;
            } else if let Some(columns) = persisted {
                require_query_index_columns(descriptor, Some(columns))?;
            }
            continue;
        }
        if !query_index_descriptor_is_applicable(connection, descriptor)? {
            if let Some(columns) = persisted {
                require_query_index_columns(descriptor, Some(columns))?;
            }
            continue;
        }
        match persisted {
            None => {
                connection.execute(descriptor.sql, [])?;
                require_persisted_query_index(connection, descriptor)?;
                let plan_complete = remaining_query_indexes_are_complete(
                    connection,
                    &SEARCH_QUERY_INDEXES[position + 1..],
                )?;
                return Ok(SearchQueryIndexAdvance::Created {
                    completed_unit: position,
                    plan_complete,
                });
            }
            Some(columns) if query_index_columns_match(&columns, descriptor.columns) => {}
            Some(columns) => {
                return Err(StorageError::Invariant(format!(
                    "query index '{}' has columns {:?}, expected {:?}",
                    descriptor.name, columns, descriptor.columns
                )));
            }
        }
    }

    Ok(SearchQueryIndexAdvance::Complete)
}

fn query_index_descriptor_is_applicable(
    connection: &Connection,
    descriptor: &SearchQueryIndexDescriptor,
) -> Result<bool, StorageError> {
    let Some(required_table) = descriptor.required_table else {
        return Ok(true);
    };
    table_has_columns(
        connection,
        required_table,
        descriptor.required_table_columns,
    )
}

fn query_index_columns_match(actual: &[String], expected: &[&str]) -> bool {
    actual
        .iter()
        .map(String::as_str)
        .eq(expected.iter().copied())
}

fn require_persisted_query_index(
    connection: &Connection,
    descriptor: &SearchQueryIndexDescriptor,
) -> Result<(), StorageError> {
    let actual = persisted_query_index_columns(connection, descriptor)?;
    if actual
        .as_deref()
        .is_some_and(|columns| query_index_columns_match(columns, descriptor.columns))
    {
        return Ok(());
    }
    Err(StorageError::Invariant(format!(
        "query index '{}' did not persist its expected columns",
        descriptor.name
    )))
}

fn require_query_index_columns(
    descriptor: &SearchQueryIndexDescriptor,
    actual: Option<Vec<String>>,
) -> Result<(), StorageError> {
    if actual
        .as_deref()
        .is_some_and(|columns| query_index_columns_match(columns, descriptor.columns))
    {
        return Ok(());
    }
    Err(StorageError::Invariant(format!(
        "query index '{}' does not match its durable descriptor",
        descriptor.name
    )))
}

fn remaining_query_indexes_are_complete(
    connection: &Connection,
    descriptors: &[SearchQueryIndexDescriptor],
) -> Result<bool, StorageError> {
    for descriptor in descriptors {
        let persisted = persisted_query_index_columns(connection, descriptor)?;
        if descriptor.mode == SearchQueryIndexMode::Retired
            || !query_index_descriptor_is_applicable(connection, descriptor)?
        {
            if let Some(columns) = persisted {
                require_query_index_columns(descriptor, Some(columns))?;
            }
            continue;
        }
        match persisted {
            None => return Ok(false),
            Some(columns) if query_index_columns_match(&columns, descriptor.columns) => {}
            Some(columns) => {
                return Err(StorageError::Invariant(format!(
                    "query index '{}' has columns {:?}, expected {:?}",
                    descriptor.name, columns, descriptor.columns
                )));
            }
        }
    }
    Ok(true)
}

fn persisted_query_index_columns(
    connection: &Connection,
    descriptor: &SearchQueryIndexDescriptor,
) -> Result<Option<Vec<String>>, StorageError> {
    let table = connection
        .query_row(
            "SELECT tbl_name FROM sqlite_schema WHERE type = 'index' AND name = ?1",
            params![descriptor.name],
            |row| row.get::<_, String>(0),
        )
        .optional()?;
    let Some(table) = table else {
        return Ok(None);
    };
    if table != descriptor.table {
        return Err(StorageError::Invariant(format!(
            "query index '{}' belongs to table '{table}', expected '{}'",
            descriptor.name, descriptor.table
        )));
    }
    let mut statement = connection.prepare(&format!("PRAGMA index_list({table})"))?;
    let rows = statement.query_map([], |row| {
        Ok((
            row.get::<_, String>(1)?,
            row.get::<_, bool>(2)?,
            row.get::<_, bool>(4)?,
        ))
    })?;
    let persisted = rows.collect::<Result<Vec<_>, _>>()?;
    if !persisted
        .iter()
        .any(|(name, unique, partial)| name == descriptor.name && !unique && !partial)
    {
        return Err(StorageError::Invariant(format!(
            "query index '{}' is unique, partial, or absent from its table metadata",
            descriptor.name
        )));
    }
    let mut statement = connection.prepare(&format!("PRAGMA index_xinfo({})", descriptor.name))?;
    let rows = statement.query_map([], |row| {
        Ok((
            row.get::<_, Option<String>>(2)?,
            row.get::<_, bool>(3)?,
            row.get::<_, String>(4)?,
            row.get::<_, bool>(5)?,
        ))
    })?;
    let mut columns = Vec::new();
    for row in rows {
        let (column, descending, collation, key) = row?;
        if !key {
            continue;
        }
        let column = column.ok_or_else(|| {
            StorageError::Invariant(format!(
                "query index '{}' contains an expression column",
                descriptor.name
            ))
        })?;
        if descending || collation != "BINARY" {
            return Err(StorageError::Invariant(format!(
                "query index '{}' uses descending order or non-BINARY collation",
                descriptor.name
            )));
        }
        columns.push(column);
    }
    Ok(Some(columns))
}

/// Validates only descriptors already present in SQLite schema metadata.
///
/// Startup uses this read-only preflight so opening a database cannot turn a
/// missing descriptor into uncheckpointed index-construction work.
pub(in crate::storage::sqlite) fn validate_existing_query_indexes(
    connection: &Connection,
) -> Result<(), StorageError> {
    for descriptor in SEARCH_QUERY_INDEXES {
        if let Some(columns) = persisted_query_index_columns(connection, descriptor)? {
            require_query_index_columns(descriptor, Some(columns))?;
        }
    }

    Ok(())
}

/// Creates missing descriptors only when their complete owner table is empty.
///
/// The caller owns the transaction so session restart and direct-publication
/// preflight can roll these DDL changes back with their surrounding policy.
pub(in crate::storage::sqlite::code) fn prepare_query_indexes_for_empty_owners(
    connection: &Connection,
) -> Result<(), StorageError> {
    validate_existing_query_indexes(connection)?;
    for descriptor in SEARCH_QUERY_INDEXES {
        if descriptor.mode == SearchQueryIndexMode::Retired
            || !query_index_descriptor_is_applicable(connection, descriptor)?
            || persisted_query_index_columns(connection, descriptor)?.is_some()
            || !query_index_owner_is_empty(connection, descriptor)?
        {
            continue;
        }
        connection.execute(descriptor.sql, [])?;
        require_persisted_query_index(connection, descriptor)?;
    }

    Ok(())
}

/// Prepares only the two chunk lookups that would otherwise scan content-heavy
/// rows during finalization, and only while their shared owner is still empty.
pub(in crate::storage::sqlite::code) fn prepare_restart_query_indexes(
    connection: &Connection,
) -> Result<(), StorageError> {
    validate_existing_query_indexes(connection)?;
    for descriptor in SEARCH_QUERY_INDEXES {
        if descriptor.mode != SearchQueryIndexMode::RequiredForEmptyChunkOwnerRestart
            || !query_index_descriptor_is_applicable(connection, descriptor)?
            || persisted_query_index_columns(connection, descriptor)?.is_some()
            || !query_index_owner_is_empty(connection, descriptor)?
        {
            continue;
        }
        connection.execute(descriptor.sql, [])?;
        require_persisted_query_index(connection, descriptor)?;
    }

    Ok(())
}

/// Fails closed unless every applicable descriptor already has its exact shape.
pub(super) fn require_query_indexes_for_fact_publication(
    connection: &Connection,
) -> Result<(), StorageError> {
    if query_indexes_ready_for_fact_publication(connection)? {
        return Ok(());
    }
    Err(StorageError::Invariant(
        "one or more query indexes are missing before direct fact publication".to_owned(),
    ))
}

pub(in crate::storage::sqlite::code) fn query_indexes_ready_for_fact_publication(
    connection: &Connection,
) -> Result<bool, StorageError> {
    for descriptor in SEARCH_QUERY_INDEXES {
        let persisted = persisted_query_index_columns(connection, descriptor)?;
        if descriptor.mode == SearchQueryIndexMode::Retired
            || !query_index_descriptor_is_applicable(connection, descriptor)?
        {
            if let Some(columns) = persisted {
                require_query_index_columns(descriptor, Some(columns))?;
            }
            continue;
        }
        match persisted {
            Some(columns) => require_query_index_columns(descriptor, Some(columns))?,
            None => return Ok(false),
        }
    }
    Ok(true)
}

fn query_index_owner_is_empty(
    connection: &Connection,
    descriptor: &SearchQueryIndexDescriptor,
) -> Result<bool, StorageError> {
    connection
        .query_row(
            &format!(
                "SELECT NOT EXISTS (SELECT 1 FROM {} LIMIT 1)",
                descriptor.table
            ),
            [],
            |row| row.get::<_, bool>(0),
        )
        .map_err(StorageError::from)
}