wrolpi/wrolpi/test/test_db_bootstrap.py
lrnselfreliance 20be5dfc27
collections: channel and domain listings read trigger-maintained summary columns (#739)
* collections: channel listing reads trigger columns and batch-loads tag/downloads

Listing channel collections summed FileGroup sizes through a video join, which reads a
random page of the file_group table per video, and lazy-loaded every collection's tag
and downloads one query at a time. On a large library with a cold disk cache the
listing took over two minutes, past the browser's 60 s timeout, so the Channels page
showed an error.

channel.video_count and channel.total_size are already maintained by triggers
(wrolpi/schema_ddl.py), so read those. Tag and downloads are selectin-loaded, so the
query count no longer grows with the number of collections.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* collections: domain listing reads trigger-maintained summary columns

Domain Collections summed file_group.size through an archive join on every listing,
one random page read per archive (about 9 s from a cold disk cache on a large
library). New triggers keep collection.item_count and collection.total_size current
for Domain Collections, as the channel triggers already do for Channels, and the
listing reads those columns. A migration installs the triggers and backfills
existing Domain Collections.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-27 11:36:08 -06:00

306 lines
15 KiB
Python

"""Tests for wrolpi.db_bootstrap: DB auto-creation, migration, and the drive-portability guard."""
import sqlite3
import mock
import pytest
from wrolpi import db_bootstrap
from wrolpi.db import get_db_file
@pytest.fixture(autouse=True)
def permissive_sqlite_floor():
"""CI images may ship a SQLite older than the production floor (Ubuntu 22.04 has 3.37; the
floor targets Debian 12's 3.40). The functional tests here exercise creation/migration, not
the version gate; the gate itself is tested explicitly with mocked versions below."""
with mock.patch.object(db_bootstrap, 'MINIMUM_SQLITE_VERSION', (3, 0, 0)):
yield
def test_ensure_db_creates_and_migrates(test_directory):
"""With no database file, ensure_db creates it and migrates to head."""
db_file = get_db_file()
assert not db_file.is_file()
assert db_bootstrap.compare_db_version() == 'missing'
assert db_bootstrap.ensure_db() is True
assert db_file.is_file()
assert db_bootstrap.compare_db_version() == 'current'
with sqlite3.connect(db_file) as conn:
version = conn.execute('SELECT version_num FROM alembic_version').fetchone()[0]
assert version
# Triggers and FTS5 tables were installed by the baseline.
triggers = conn.execute("SELECT COUNT(*) FROM sqlite_master WHERE type='trigger'").fetchone()[0]
assert triggers > 0
fts_tables = {i[0] for i in conn.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name IN ('file_group_fts', 'doc_section_fts')")}
assert fts_tables == {'file_group_fts', 'doc_section_fts'}
file_group_cols = {r[1] for r in conn.execute('PRAGMA table_info(file_group)')}
assert 'idempotency' not in file_group_cols
assert 'stem' in file_group_cols
# Running again is a no-op.
assert db_bootstrap.ensure_db() is True
def test_ensure_db_refuses_newer_database(test_directory):
"""A database from a NEWER WROLPi (unknown alembic revision) must not be written."""
assert db_bootstrap.ensure_db() is True
db_file = get_db_file()
with sqlite3.connect(db_file) as conn:
conn.execute("UPDATE alembic_version SET version_num = 'future_revision_xyz'")
conn.commit()
assert db_bootstrap.compare_db_version() == 'ahead'
assert db_bootstrap.ensure_db() is False
# The database was not modified.
with sqlite3.connect(db_file) as conn:
assert conn.execute('SELECT version_num FROM alembic_version').fetchone()[0] == 'future_revision_xyz'
def test_compare_db_version_uninitialized_file(test_directory):
"""An empty/uninitialized file is treated like a fresh database."""
db_file = get_db_file()
db_file.parent.mkdir(parents=True, exist_ok=True)
db_file.touch()
assert db_bootstrap.compare_db_version() == 'missing'
assert db_bootstrap.ensure_db() is True
assert db_bootstrap.compare_db_version() == 'current'
def test_unique_stem_migration_collapses_duplicates(test_directory):
"""The unique-index migration merges duplicate (directory, stem) rows and keeps tags."""
import sqlite3
from alembic.config import Config
from alembic import command
from wrolpi.db import get_db_uri, get_db_file
from wrolpi.vars import PROJECT_DIR
(test_directory / 'config').mkdir(parents=True, exist_ok=True)
config = Config(str(PROJECT_DIR / 'alembic.ini'))
config.set_main_option('sqlalchemy.url', get_db_uri())
command.upgrade(config, '2026_09_16_1200')
db_file = get_db_file()
directory = str(test_directory)
with sqlite3.connect(db_file) as conn:
conn.execute('PRAGMA foreign_keys=ON')
conn.execute(
'INSERT INTO file_group (directory, primary_path, files, stem, indexed) VALUES (?,?,?,?,0)',
(directory, str(test_directory / 'dup.mp4'), '[]', 'dup'),
)
conn.execute(
'INSERT INTO file_group (directory, primary_path, files, stem, indexed) VALUES (?,?,?,?,0)',
(directory, str(test_directory / 'dup.info.json'), '[]', 'dup'),
)
first_id, second_id = [r[0] for r in conn.execute('SELECT id FROM file_group ORDER BY id').fetchall()]
conn.execute("INSERT INTO tag (name, color) VALUES ('migrated', '#111111')")
tag_id = conn.execute('SELECT id FROM tag').fetchone()[0]
# Tag the lower-id row so it wins over the later untagged duplicate.
conn.execute(
'INSERT INTO tag_file (tag_id, file_group_id) VALUES (?, ?)',
(tag_id, first_id),
)
conn.commit()
assert conn.execute('SELECT COUNT(*) FROM file_group').fetchone()[0] == 2
assert first_id < second_id
command.upgrade(config, 'head')
with sqlite3.connect(db_file) as conn:
rows = list(conn.execute('SELECT id, stem FROM file_group'))
assert len(rows) == 1
assert rows[0][0] == first_id
tagged = conn.execute('SELECT file_group_id FROM tag_file').fetchone()[0]
assert tagged == first_id
sql = conn.execute(
"SELECT sql FROM sqlite_master WHERE type='index' AND name='file_group_directory_stem_idx'"
).fetchone()[0]
assert 'UNIQUE' in sql.upper()
def test_drop_file_group_idempotency_preserves_fts(test_directory):
"""DROP COLUMN idempotency must not rebuild file_group or break file_group_fts."""
from alembic.config import Config
from alembic import command
from wrolpi.db import get_db_uri
from wrolpi.vars import PROJECT_DIR
(test_directory / 'config').mkdir(parents=True, exist_ok=True)
config = Config(str(PROJECT_DIR / 'alembic.ini'))
config.set_main_option('sqlalchemy.url', get_db_uri())
command.upgrade(config, '2026_09_18_1200')
db_file = get_db_file()
with sqlite3.connect(db_file) as conn:
cols = {r[1] for r in conn.execute('PRAGMA table_info(file_group)')}
assert 'idempotency' in cols
conn.execute(
'INSERT INTO file_group (directory, primary_path, files, stem, indexed, a_text) '
'VALUES (?,?,?,?,0,?)',
(str(test_directory), str(test_directory / 'searchable.txt'), '[]', 'searchable', 'alpha uniquephrase'),
)
conn.commit()
hits = conn.execute(
"SELECT rowid FROM file_group_fts WHERE file_group_fts MATCH 'uniquephrase'"
).fetchall()
assert len(hits) == 1
command.upgrade(config, 'head')
with sqlite3.connect(db_file) as conn:
cols = {r[1] for r in conn.execute('PRAGMA table_info(file_group)')}
assert 'idempotency' not in cols
hits = conn.execute(
"SELECT rowid FROM file_group_fts WHERE file_group_fts MATCH 'uniquephrase'"
).fetchall()
assert len(hits) == 1
def test_domain_collection_summary_migration_backfills(test_directory):
"""The domain-summary migration installs the archive triggers and backfills the summary
columns of existing Domain Collections from their Archives."""
import sqlite3
from alembic.config import Config
from alembic import command
from wrolpi.db import get_db_uri, get_db_file
from wrolpi.vars import PROJECT_DIR
(test_directory / 'config').mkdir(parents=True, exist_ok=True)
config = Config(str(PROJECT_DIR / 'alembic.ini'))
config.set_main_option('sqlalchemy.url', get_db_uri())
command.upgrade(config, '2026_09_20_1200')
db_file = get_db_file()
directory = str(test_directory)
triggers = {'archive_insert_collection_summary', 'archive_delete_collection_summary',
'archive_update_collection_summary', 'file_group_size_collection_summary'}
with sqlite3.connect(db_file) as conn:
conn.execute('PRAGMA foreign_keys=ON')
# The baseline installs the current trigger DDL, so a fresh DB already has the archive
# triggers. A production DB created before them does not; emulate that.
for name in triggers:
conn.execute(f'DROP TRIGGER {name}')
for id_, name, kind in ((1, 'example.com', 'domain'), (2, 'empty.org', 'domain'), (3, 'chan', 'channel')):
conn.execute('INSERT INTO collection (id, name, kind, item_count, total_size) VALUES (?, ?, ?, 0, 0)',
(id_, name, kind))
for fg_id, stem, size in ((10, 'one', 100), (11, 'two', 250)):
conn.execute(
'INSERT INTO file_group (id, directory, primary_path, files, stem, size, indexed)'
' VALUES (?,?,?,?,?,?,0)',
(fg_id, directory, str(test_directory / f'{stem}.html'), '[]', stem, size),
)
conn.execute('INSERT INTO archive (collection_id, file_group_id) VALUES (1, ?)', (fg_id,))
conn.commit()
# Nothing maintains the columns before the migration.
assert conn.execute('SELECT item_count, total_size FROM collection WHERE id = 1').fetchone() == (0, 0)
command.upgrade(config, 'head')
with sqlite3.connect(db_file) as conn:
assert conn.execute('SELECT item_count, total_size FROM collection WHERE id = 1').fetchone() == (2, 350)
assert conn.execute('SELECT item_count, total_size FROM collection WHERE id = 2').fetchone() == (0, 0)
assert conn.execute('SELECT item_count, total_size FROM collection WHERE id = 3').fetchone() == (0, 0)
installed = {r[0] for r in conn.execute(
"SELECT name FROM sqlite_master WHERE type='trigger' AND name LIKE '%collection_summary'")}
assert installed == triggers
# The triggers are live after the migration.
conn.execute('DELETE FROM archive WHERE file_group_id = 11')
conn.commit()
assert conn.execute('SELECT item_count, total_size FROM collection WHERE id = 1').fetchone() == (1, 100)
def test_bootstrap_lock(test_directory):
"""Only one process can hold the bootstrap lock."""
with db_bootstrap.bootstrap_lock() as acquired:
assert acquired is True
with db_bootstrap.bootstrap_lock() as second:
# flock is per-file-descriptor, so a second open in the same process CAN acquire it;
# this just proves the context manager doesn't error or deadlock.
assert second in (True, False)
def test_check_sqlite_environment_version_gate(test_directory):
"""SQLite older than the floor is refused; at/above the floor is accepted.
Mocked versions so the test is deterministic regardless of the machine's SQLite (CI images
have shipped 3.37 while the production floor is 3.40)."""
with mock.patch.object(db_bootstrap, 'MINIMUM_SQLITE_VERSION', (3, 40, 0)):
with mock.patch.object(db_bootstrap.sqlite3, 'sqlite_version_info', (3, 39, 9)), \
mock.patch.object(db_bootstrap.sqlite3, 'sqlite_version', '3.39.9'):
error = db_bootstrap.check_sqlite_environment(get_db_file())
assert error and 'too old' in error
with mock.patch.object(db_bootstrap.sqlite3, 'sqlite_version_info', (3, 40, 0)):
assert db_bootstrap.check_sqlite_environment(get_db_file()) is None
with mock.patch.object(db_bootstrap.sqlite3, 'sqlite_version_info', (3, 46, 1)):
assert db_bootstrap.check_sqlite_environment(get_db_file()) is None
@pytest.mark.parametrize('fs_type', ['vfat', 'exfat', 'msdos', 'ntfs', 'ntfs3'])
def test_check_sqlite_environment_allows_wal_incompatible_fs_with_warning(test_directory, fs_type):
"""A FAT/exFAT/NTFS drive on a native kernel driver is allowed (with a warning), not refused.
These filesystems cannot support SQLite WAL — it needs mmap shared-memory they don't provide,
so `PRAGMA journal_mode=WAL` raises `(sqlite3.OperationalError) disk I/O error` (the failure
seen on 10.0.0.8; the tell-tale sign was `Media directory has the wrong permissions: 0o40777`,
since these filesystems can't store Unix permission bits). Rather than crash the whole API,
the engine degrades to a rollback journal, so the environment check must NOT refuse the drive
— it only warns that performance is reduced."""
with mock.patch.object(db_bootstrap, '_media_fs_type', return_value=fs_type), \
mock.patch.object(db_bootstrap.logger, 'warning') as mock_warning:
assert db_bootstrap.check_sqlite_environment(get_db_file()) is None
assert mock_warning.call_count == 1
assert 'WAL is unavailable' in mock_warning.call_args.args[0], 'expected a WAL-unavailable warning'
@pytest.mark.parametrize('fs_type', ['ext4', 'btrfs', 'xfs', 'f2fs'])
def test_check_sqlite_environment_allows_wal_compatible_fs(test_directory, fs_type):
"""Ordinary Linux filesystems support WAL and must not be refused."""
with mock.patch.object(db_bootstrap, '_media_fs_type', return_value=fs_type):
assert db_bootstrap.check_sqlite_environment(get_db_file()) is None
@pytest.mark.parametrize('fs_type', ['nfs', 'nfs4', 'cifs', 'fuse.sshfs', 'fuseblk'])
def test_check_sqlite_environment_refuses_unsafe_locking_fs(test_directory, fs_type):
"""Filesystems with unreliable locking are refused: no journal mode makes SQLite safe there.
Includes `fuseblk` (block-backed FUSE, e.g. ntfs-3g), whose byte-range locking is unreliable
under the workers' concurrent access. NTFS on a modern kernel mounts as native `ntfs3` instead
and is allowed (with the WAL-unavailable warning)."""
with mock.patch.object(db_bootstrap, '_media_fs_type', return_value=fs_type):
error = db_bootstrap.check_sqlite_environment(get_db_file())
assert error and fs_type in error
def test_unmounted_production_guard(test_directory):
"""An unmounted production media directory blocks DB creation/migration and db_up.
Regression test for the shadow-database bug found on 10.0.0.8: after the media drive was
unmounted, a worker's ORM connection created an empty wrolpi.db on the root filesystem and
the bootstrap migrated it — because the old guard only refused when NO file existed."""
import pathlib
fake_media = mock.MagicMock(spec=pathlib.Path)
fake_media.__str__ = lambda self: '/media/wrolpi'
fake_media.is_mount.return_value = False
fake_media.is_dir.return_value = True
with mock.patch.object(db_bootstrap, 'PYTEST', False), \
mock.patch.object(db_bootstrap, 'DOCKERIZED', False), \
mock.patch.object(db_bootstrap.sys, 'platform', 'linux'):
# Unmounted /media/* on a production linux host: guarded.
assert db_bootstrap.media_directory_is_unmounted_production(fake_media) is True
# Mounted: fine.
fake_media.is_mount.return_value = True
assert db_bootstrap.media_directory_is_unmounted_production(fake_media) is False
# Custom (non-/media) directories are exempt (dev boxes).
fake_media.is_mount.return_value = False
fake_media.__str__ = lambda self: '/home/user/media'
assert db_bootstrap.media_directory_is_unmounted_production(fake_media) is False
# The guard must refuse ensure_db EVEN IF a database file already exists (an ORM connection
# creates an empty file implicitly, so file-existence cannot be the condition).
assert db_bootstrap.ensure_db() is True # create a real DB in the test directory first
with mock.patch.object(db_bootstrap, 'media_directory_is_unmounted_production', return_value=True):
assert db_bootstrap.ensure_db() is False