61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""Exit abruptly from inside a SQLite FTS rebuild transaction."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from gewu.adapters.git_repository import GitCanonicalRepository
|
|
from gewu.adapters.sqlite_search import SQLiteSearchIndex
|
|
|
|
|
|
class CrashConnection(sqlite3.Connection):
|
|
def __init__(self, *args: object, **kwargs: object) -> None:
|
|
super().__init__(*args, **kwargs) # type: ignore[arg-type]
|
|
self._fts_inserts = 0
|
|
|
|
def execute(
|
|
self,
|
|
sql: str,
|
|
parameters: tuple[object, ...] = (),
|
|
/,
|
|
) -> sqlite3.Cursor:
|
|
cursor = super().execute(sql, parameters)
|
|
if sql.lstrip().upper().startswith("INSERT INTO NODE_FTS"):
|
|
self._fts_inserts += 1
|
|
if self._fts_inserts == 5:
|
|
os._exit(74)
|
|
return cursor
|
|
|
|
|
|
class CrashDuringIndexRebuild(SQLiteSearchIndex):
|
|
def _connect(self) -> sqlite3.Connection:
|
|
connection = sqlite3.connect(
|
|
self.path,
|
|
timeout=5,
|
|
isolation_level=None,
|
|
factory=CrashConnection,
|
|
)
|
|
connection.execute("PRAGMA foreign_keys=ON")
|
|
connection.execute("PRAGMA busy_timeout=5000")
|
|
connection.execute("PRAGMA synchronous=NORMAL")
|
|
return connection
|
|
|
|
|
|
def main() -> None:
|
|
repository = GitCanonicalRepository(Path(sys.argv[1]))
|
|
index = CrashDuringIndexRebuild(Path(sys.argv[2]), repository)
|
|
index.search(
|
|
query="visitors",
|
|
project_ids=("dome-performance",),
|
|
include_shared=False,
|
|
limit=20,
|
|
)
|
|
raise RuntimeError("SQLite rebuild did not reach the requested crash point")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|