~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""An existing install must survive the upgrade to #3299 (Flask -> FastAPI).
Every user of this project meets this PR exactly once: they stop a Flask
process, start a FastAPI one, and point it at **the database they already
have**. Nothing in the port is user-visible if that step loses their API
keys, resets their theme, or leaves their research history unreadable. The
rest of the PR's ~900 changed files are covered by behavioural tests of the
new web layer; this file covers the one thing those cannot see, because it
only happens to a database that predates them.
What makes the port itself safe for existing databases is a *negative*
fact stated in ``changelog.d/3299.breaking.md``: "the web-layer change
itself adds no database schema migration". The release that ships the port
also carries later data-repair revisions, so its documented rollback restores
a data backup (see ``docs/deployment/upgrading.md``); this file does not
claim the release as a whole can be rolled back without one.
The claim about the port is load-bearing for the upgrade, and a changelog
sentence cannot enforce itself. The first group of tests turns it into an assertion -- but it is a
claim about ONE commit, so it is checked against that commit's own range
(``fb4e33b8d^..fb4e33b8d``): no migration file added or edited there, and --
the other half, which the changelog does not state -- no ORM model changed
there either. Scoping it to the commit keeps it a true, re-verified
statement forever, instead of quietly turning into "no branch may ever add
a migration" the moment #3299 landed on main.
What every later change must satisfy is the rule that claim was a special
case of, shared with ``test_migration_chain_integrity.py`` and defined in
``schema_change_rule.py``: since the merge base, no shipped revision may be
edited or removed, adding a revision is ordinary, and any ORM model change
must ship with an added revision unless isolated SQLite snapshots prove
that the model edit emits an identical schema. A schema change with no
migration is the dangerous shape: the schema on disk is then *behind* the code, and every
query against the drifted table fails at runtime, on a real user's data,
after the upgrade has already happened.
Two qualifications on "changed", both in ``schema_change_rule.py``. The
guarded path set is this directory plus ``DECLARATIVE_BASE_MODULE`` -- the
canonical ``declarative_base()`` call sits one level above ``models/`` and
would otherwise be unguarded, though editing it renames every constraint
``create_all()`` emits. And a file counts as changed only if its *code*
changed: ``drop_comment_only_edits`` compares the two ASTs with docstrings
stripped, so rewording a comment or a docstring in a model or a shipped
revision is not a schema change and is not reported.
The second group runs the chain against a populated database, because a
migration can apply cleanly to an empty one and still bail out or corrupt
rows when data is present.
The third group runs what the app actually runs against an existing
database -- ``initialize_database()``, whose settings step is the whole of
this PR's diff to ``database/initialize.py``::
- settings_mgr.load_from_defaults_file(overwrite=False, delete_extra=True)
+ settings_mgr.import_settings(settings_mgr.default_settings,
+ overwrite=False, delete_extra=True, override_locked=True
+ )
(the direct ``import_settings`` form is the trusted-bootstrap bypass
since #5841; ``load_from_defaults_file`` no longer accepts
``override_locked``)
That step reconciles the shipped defaults into the user's database on a
version bump. This PR changed two stored defaults -- ``app.theme``
``dark`` -> ``system`` and ``web.host`` ``0.0.0.0`` -> ``127.0.0.1`` -- so it
is precisely the release where "reconcile defaults" and "do not clobber the
stored value" can be told apart. Startup's listener configuration is resolved
separately from environment variables, legacy JSON, or the built-in default;
this test protects database reconciliation, not network reachability.
WHAT BREAKS IF THIS REGRESSES. Silently, and only for existing users: a themed
UI flips, stored configuration drifts, or an API key row is replaced by the
empty default and every LLM call starts failing with an auth error. Fresh
installs look perfect throughout, which is why no other test in the suite
catches it.
Non-vacuity is enforced rather than assumed. ``test_positive_control_*``
proves the seeded state is really in the database before anything runs;
``test_startup_reconciliation_actually_ran`` proves the reconciliation did
real work (it adds a key the seeded "old" database lacked and bumps
``app.version``), so "the value survived" cannot pass because nothing
happened; and ``test_fresh_install_receives_the_new_defaults`` proves the
two defaults genuinely changed in this PR, so "the old value survived"
cannot pass because the old and new values were the same.
"""
import subprocess
from pathlib import Path
import pytest
from alembic import command
from sqlalchemy import create_engine, inspect
from sqlalchemy.orm import sessionmaker
from local_deep_research.__version__ import __version__ as package_version
from local_deep_research.database.alembic_runner import (
get_alembic_config,
get_current_revision,
get_head_revision,
get_migrations_dir,
run_migrations,
)
from local_deep_research.database.initialize import initialize_database
from local_deep_research.database.models import Setting
from local_deep_research.database.models.research import (
ResearchHistory,
ResearchResource,
)
from local_deep_research.database.models.settings import SettingType
from local_deep_research.settings.manager import SettingsManager
from tests.database.schema_change_rule import (
DECLARATIVE_BASE_MODULE,
MIGRATIONS_SUBTREE,
MIN_MODEL_FILES,
MIN_REVISIONS,
MODELS_SUBTREE,
classify,
drop_comment_only_edits,
drop_proven_schema_neutral_edits,
violations,
)
from tests.database.schema_snapshot import model_edits_preserve_schema
REPO_ROOT = Path(__file__).resolve().parents[2]
# The commit that merged #3299, i.e. the commit
# ``changelog.d/3299.breaking.md`` is talking about.
PORT_3299_COMMIT = "fb4e33b8d8cba4d62c70cc2704007765ad9f6293"
# An intermediate revision, used to simulate an operator who skipped a few
# releases before taking this one. `0020` is far enough back that the
# settings-mutating migrations (0025, 0027, 0029, 0030) are still ahead of
# it, so the seeded database really does get rewritten on the way to head.
OLDER_REVISION = "0020"
OLD_APP_VERSION = "0.0.0-pre-3299"
# The choices our imaginary upgrading user made, by hand, before the
# upgrade. `app.theme` and `web.host` are the two settings whose *shipped
# default* changed in this PR; the API key is the setting whose loss would
# be most expensive and least obvious.
USER_CHOICES = {
"app.theme": "dark",
"web.host": "0.0.0.0",
"llm.openai.api_key": "sk-user-secret-must-not-be-lost",
"llm.provider": "openai",
}
# Keys deliberately withheld from the seeded database so it looks like an
# install from before they shipped. The reconciliation step must add them
# back -- that is how we know it ran at all.
SIMULATED_NEW_KEYS = (
"search.engine.web.searxng.default_params.engines",
"app.enable_notifications",
)
RESEARCH_ID = "3fd0a3e4-0000-4000-8000-upgradepath01"
_SETTING_COLUMNS = {column.name for column in Setting.__table__.columns}
# ---------------------------------------------------------------------------
# helpers
# ---------------------------------------------------------------------------
def _git(*args):
"""Run git in the repo and return (returncode, stdout)."""
completed = subprocess.run(
["git", *args],
cwd=REPO_ROOT,
capture_output=True,
text=True,
check=False,
)
return completed.returncode, completed.stdout
def _base_ref():
"""The commit this branch forked from, which it is measured against.
The *merge base* with main, not main's tip: a migration that lands on
main after this branch forked is not this PR's doing, and comparing
against the tip would report it as a file this branch deleted. #3299
is behind that merge base now, so this is not a pre-#3299 tree: what
it establishes is what THIS branch changed, nothing about the port.
A shallow or detached CI checkout may have no main ref at all, in which
case the git-based tests cannot run and say so rather than passing
quietly.
"""
for candidate in ("origin/main", "main"):
code, _ = _git(
"rev-parse", "--verify", "--quiet", f"{candidate}^{{commit}}"
)
if code != 0:
continue
code, out = _git("merge-base", candidate, "HEAD")
if code == 0 and out.strip():
return out.strip()
return None
def _require_base_ref():
base = _base_ref()
if base is None:
pytest.skip(
"no merge base with origin/main or main in this checkout, so the "
"pre-migration baseline cannot be read; run with a full clone"
)
return base
def _require_commit_range(sha):
"""``(parent, commit)`` for ``sha``, or skip.
A shallow or sparse CI checkout may not contain a months-old commit;
saying so beats passing quietly on an empty comparison.
"""
for rev in (f"{sha}^{{commit}}", f"{sha}^^{{commit}}"):
code, _ = _git("rev-parse", "--verify", "--quiet", rev)
if code != 0:
pytest.skip(
f"{rev} does not resolve in this checkout, so the #3299 "
"no-schema-change claim cannot be re-verified here. A "
"shallow clone is the usual cause: fetch-depth: 0 is "
"required for this test to run at all."
)
code, out = _git("rev-parse", f"{sha}^")
assert code == 0
return out.strip(), sha
def _tracked_files(ref, subtree):
"""Blob hashes of every file under ``subtree`` at ``ref``.
Hashes rather than names so an *edited* migration is caught as well as
an added one -- editing a revision that users have already applied is
every bit as much a schema change as adding one.
"""
code, out = _git("ls-tree", "-r", ref, "--", subtree)
assert code == 0, f"git ls-tree failed for {ref}:{subtree}"
entries = {}
for line in out.splitlines():
meta, _, path = line.partition("\t")
parts = meta.split()
entries[path] = parts[2]
return entries
def _file_text(ref, path):
"""The contents of one tracked file at ``ref``, or None if unreadable.
Feeds ``schema_change_rule.drop_comment_only_edits``, which needs the
source rather than the blob hash: two blobs differ the moment a
comment moves, and a moved comment changes nothing a user's database
can see. Decoded with ``errors="strict"``: a file that fails to decode
as UTF-8 simply stays reported, rather than silently comparing equal
to an unrelated blob that also fails to decode.
"""
completed = subprocess.run(
["git", "show", f"{ref}:{path}"],
cwd=REPO_ROOT,
capture_output=True,
check=False,
)
if completed.returncode != 0:
return None
try:
return completed.stdout.decode("utf-8", errors="strict")
except UnicodeDecodeError:
return None
def _source_pair_reader(before, after):
"""``read_pair`` for ``drop_comment_only_edits`` over two refs."""
def read_pair(path):
before_text = _file_text(before, path)
after_text = _file_text(after, path)
if before_text is None or after_text is None:
return None
return before_text, after_text
return read_pair
def _guarded_model_files(ref):
"""``{path: blob}`` for every schema source file this gate watches.
The ``database/models`` directory listing plus
``DECLARATIVE_BASE_MODULE``. That one file holds the canonical
``Base = declarative_base()`` -- ``models/base.py`` only re-exports it
-- and it lives one directory ABOVE the listing, so a directory-scoped
diff cannot see it. Handing ``declarative_base()`` a
``metadata=MetaData(naming_convention=...)`` there changes the name of
every index and constraint that revision ``0001``'s ``create_all()``
emits for a fresh install while existing users keep the old names,
which is precisely the divergence this gate exists to catch.
"""
return {
**_tracked_files(ref, MODELS_SUBTREE),
**_tracked_files(ref, DECLARATIVE_BASE_MODULE),
}
def _engine(tmp_path, request, name):
engine = create_engine(f"sqlite:///{tmp_path}/{name}.db")
request.addfinalizer(engine.dispose)
return engine
def _upgrade_to(engine, revision):
config = get_alembic_config(engine)
with engine.begin() as connection:
config.attributes["connection"] = connection
command.upgrade(config, revision)
def _shipped_defaults():
"""The defaults this branch ships, straight from the production loader."""
return SettingsManager(None).default_settings
def _setting_row(key, meta):
"""Build a ``Setting`` row from one defaults-file entry.
Plain plumbing (dict -> ORM columns). The seeding deliberately does not
go through ``import_settings``: that function is the behaviour under
test, and seeding with it would make these tests circular.
"""
data = {k: v for k, v in meta.items() if k in _SETTING_COLUMNS}
if isinstance(data.get("type"), str):
data["type"] = SettingType[data["type"]]
return Setting(key=key, **data)
def _seed_old_install(engine, locked=False):
"""Populate ``engine`` as a pre-#3299 install would look.
Every shipped default *except* ``SIMULATED_NEW_KEYS`` is present, the
user's hand-picked values from ``USER_CHOICES`` overwrite the shipped
ones, ``app.version`` is stale, and there is research history to lose.
"""
defaults = _shipped_defaults()
assert defaults, (
"the defaults loader returned nothing, so the seeded database would "
"be empty and every survival assertion below would be vacuous"
)
for key in SIMULATED_NEW_KEYS:
assert key in defaults, (
f"{key!r} is no longer a shipped default, so withholding it no "
"longer simulates a key introduced by an upgrade -- pick another"
)
for key in USER_CHOICES:
assert key in defaults, (
f"{key!r} is no longer a shipped default, so the reconciliation "
"step would never touch it and preserving it would prove nothing"
)
Session = sessionmaker(bind=engine)
with Session() as session:
for key, meta in defaults.items():
if key in SIMULATED_NEW_KEYS:
continue
row = _setting_row(key, meta)
if key in USER_CHOICES:
row.value = USER_CHOICES[key]
if key == "app.lock_settings":
row.value = locked
session.add(row)
session.add(
Setting(
key="app.version",
value=OLD_APP_VERSION,
type=SettingType.APP,
name="App Version",
ui_element="text",
visible=False,
editable=False,
)
)
session.add(
ResearchHistory(
id=RESEARCH_ID,
query="does my data survive the fastapi port",
mode="quick_summary",
status="completed",
created_at="2026-01-02T03:04:05",
completed_at="2026-01-02T03:09:05",
duration_seconds=300,
title="Upgrade path",
report_content="# Findings\n\nIt had better.",
research_meta={"strategy": "source-based"},
progress=100,
)
)
session.flush()
session.add(
ResearchResource(
research_id=RESEARCH_ID,
title="A cited paper",
url="https://arxiv.org/abs/2401.00001",
source_type="academic",
created_at="2026-01-02T03:05:00",
)
)
session.commit()
return Session
def _old_install_at_head(tmp_path, request, name, locked=False):
"""A pre-#3299 database: schema at head, data seeded, version stale.
Head, not an older revision, is what an existing user actually has: a
user who keeps up with releases is already at whatever head shipped
last, so the interesting case is an upgrade that applies no migration
at all and must still not disturb their data. Nothing here assumes
``main`` and this branch ship the same revisions -- they may not, and
``test_pr_edits_no_shipped_revision_and_ships_a_migration`` allows
this branch to add revisions. ``run_migrations`` walks to whatever
head THIS checkout has, so the fixture follows the branch.
"""
engine = _engine(tmp_path, request, name)
run_migrations(engine)
return engine, _seed_old_install(engine, locked=locked)
def _stored_value(Session, key):
with Session() as session:
row = session.query(Setting).filter(Setting.key == key).one_or_none()
return None if row is None else row.value
def _schema_fingerprint(engine):
inspector = inspect(engine)
return {
table: sorted(column["name"] for column in inspector.get_columns(table))
for table in sorted(inspector.get_table_names())
}
# ---------------------------------------------------------------------------
# 1. schema-change gates: the #3299 no-migration claim, re-verified at the
# commit that made it, and the general rule every later branch must satisfy
# ---------------------------------------------------------------------------
def test_the_3299_port_added_or_edited_no_migration_revision():
"""``changelog.d/3299.breaking.md``: "the web-layer change itself adds no
database schema migration".
Checked against the range of the commit that made the claim, not
against the branch under test: the sentence is a fact about #3299,
and it stays checkable after #3299 merged. Later branches are
governed by ``test_pr_edits_no_shipped_revision_and_ships_a_migration``
below.
The whole subtree, not just ``versions/``: #3299 claimed to have left
the Alembic directory alone entirely, so an edit to ``env.py`` there
would contradict it just as an added revision would. The generalised
rule below is narrower on purpose -- ``env.py`` is not immutable, it
simply was not touched by this one commit.
"""
before, after = _require_commit_range(PORT_3299_COMMIT)
baseline = _tracked_files(before, MIGRATIONS_SUBTREE)
assert len(baseline) >= MIN_REVISIONS, (
f"only {len(baseline)} file(s) under {MIGRATIONS_SUBTREE} at "
f"{before[:12]} -- the baseline did not resolve to a real tree, so "
"a migration added by #3299 would not have been noticed"
)
changes = classify(baseline, _tracked_files(after, MIGRATIONS_SUBTREE))
assert changes == {"added": [], "removed": [], "edited": []}, (
f"#3299 ({PORT_3299_COMMIT[:12]}) changed the Alembic migrations, "
"which contradicts changelog.d/3299.breaking.md's 'The web-layer "
f"change itself adds no database schema migration': {changes}. "
"Either the commit sha above is wrong, or that sentence must be "
"rewritten to describe the migration and its rollback."
)
def test_the_3299_port_changed_no_orm_model():
"""The half of the claim the changelog does not state.
An unmigrated model change is worse than an unmentioned migration: the
upgrade appears to succeed, and then every query against the drifted
table fails at runtime against real user data.
Byte-identity, not AST-identity: this one is a claim that #3299 left
the models alone entirely, so a reworded docstring there would
contradict it too. The generalised rule below is the one that filters
cosmetic edits out.
"""
before, after = _require_commit_range(PORT_3299_COMMIT)
baseline = _guarded_model_files(before)
assert len(baseline) >= MIN_MODEL_FILES, (
f"only {len(baseline)} file(s) under {MODELS_SUBTREE} (plus "
f"{DECLARATIVE_BASE_MODULE}) at {before[:12]} -- the baseline did "
"not resolve to a real tree, so a model change would not have "
"been noticed"
)
assert DECLARATIVE_BASE_MODULE in baseline, (
f"{DECLARATIVE_BASE_MODULE} does not resolve at {before[:12]}, so "
"the canonical declarative_base() call was silently dropped from "
"this comparison -- confirm it was renamed and update the constant"
)
changes = classify(baseline, _guarded_model_files(after))
assert changes == {"added": [], "removed": [], "edited": []}, (
f"#3299 ({PORT_3299_COMMIT[:12]}) changed ORM models while adding "
f"no Alembic migration: {changes}"
)
def test_pr_edits_no_shipped_revision_and_ships_a_migration():
"""The generalised rule, applied to whatever this PR changed.
Three parts, all enforced by ``schema_change_rule.violations``:
* a revision *file* under ``migrations/versions`` that shipped may not
be edited or removed -- an existing user's ``alembic_version`` row
already names it, so the new body never executes and their database
is permanently behind the code. ``migrations/env.py`` and the two
package markers are not revisions and are ordinary source files;
* ADDING a revision is ordinary and is not a finding on its own (data
migrations, backfills and index-only revisions change no ORM class);
* a change under ``database/models`` must come with an added revision
file in the same diff, unless isolated snapshots of the old and new
models prove that the edit creates exactly the same SQLite schema.
For schema-changing edits, that third part checks the two path sets.
It cannot tell whether the added revision has anything to do with the
model change: a branch that edits ``research.py`` and adds an
unrelated index-only revision satisfies it. **Nothing automated
catches that.** This rule is the only control on the pairing, and
what it buys is that the model change cannot land while its author is
unaware a migration is owed -- checking that the added revision
actually matches the model change is a reviewer's job, and there is
no test to fall back on if the review misses it.
``test_alembic_migrations.py::test_migrations_produce_schema_matching_models``
is not that backstop; it covers the opposite direction only. It
upgrades a CLEAN database to head and diffs the result against
``Base.metadata``, and revision ``0001`` builds that clean database by
calling ``Base.metadata.create_all()`` (pinned by
``test_migration_chain_integrity.py::
test_revision_0001_builds_the_baseline_from_live_model_metadata``). So
the fresh schema it inspects IS today's metadata: a model column added
with no matching revision is present at both ends and produces no
diff, and the unrelated revision's extra index is filtered out by that
test's ``remove_index`` exemption. What it does catch is a revision
that adds something the models do not declare.
Directory-scoped plus ``DECLARATIVE_BASE_MODULE``, as this file's
``_guarded_model_files`` builds it. ``test_migration_chain_integrity.py``
runs the same rule over the file set derived from the live
``Base.metadata``, which also covers the one model file outside this
directory. Both callers filter cosmetic edits first, so a reworded
comment in a model file or a shipped revision is not reported.
"""
base = _require_base_ref()
revisions_before = _tracked_files(base, MIGRATIONS_SUBTREE)
models_before = _guarded_model_files(base)
assert (
len(revisions_before) >= MIN_REVISIONS
and len(models_before) >= MIN_MODEL_FILES
), (
f"only {len(revisions_before)} migration file(s) and "
f"{len(models_before)} model file(s) at {base} -- the baseline did "
"not resolve to a real tree, so a schema change would not have "
"been noticed"
)
assert DECLARATIVE_BASE_MODULE in models_before, (
f"{DECLARATIVE_BASE_MODULE} does not resolve at {base}, so the "
"canonical declarative_base() call is no longer guarded here -- "
"confirm it was renamed and update the constant"
)
read_pair = _source_pair_reader(base, "HEAD")
revision_changes = drop_comment_only_edits(
classify(revisions_before, _tracked_files("HEAD", MIGRATIONS_SUBTREE)),
read_pair,
)
models_after = _guarded_model_files("HEAD")
model_changes = drop_proven_schema_neutral_edits(
drop_comment_only_edits(
classify(models_before, models_after), read_pair
),
models_before,
models_after,
)
problems = violations(
model_changes,
revision_changes,
schema_unchanged=model_edits_preserve_schema(
REPO_ROOT, base, "HEAD", model_changes, revision_changes
),
)
assert problems == [], "\n".join(
["this PR breaks the schema-change rule:", *problems]
)
def test_an_added_revision_without_a_model_change_is_allowed():
"""Positive control for the "additions are ordinary" half.
The gate above sees no changes on a clean tree, so it would pass just
as happily if the rule had tightened back into "no migrations at
all". These hand-written inputs hold regardless of git state and say
what this file's gate permits, right next to it. They are a subset:
the full truth table -- including the cases that separate a revision
file from the rest of the migrations subtree -- lives in
``test_migration_chain_integrity.py::test_the_schema_change_rule_is_pinned_without_touching_git``,
which is where a regression in the shared rule is diagnosed.
Paths are built from the shared subtree constants because their shape
is load-bearing (``is_revision`` reads it); only the blob hashes are
invented, which is what keeps this independent of any working tree.
"""
models = MODELS_SUBTREE
no_model_change = classify(
{f"{models}/a.py": "aaa"}, {f"{models}/a.py": "aaa"}
)
added_revision = classify(
{}, {f"{MIGRATIONS_SUBTREE}/versions/0031_new.py": "bbb"}
)
assert violations(no_model_change, added_revision) == []
assert (
violations(
classify({f"{models}/a.py": "aaa"}, {f"{models}/a.py": "bbb"}),
added_revision,
)
== []
), "a model change WITH its migration must be allowed"
# The guarded set this file hands the rule includes the canonical
# declarative_base() module, which sits outside MODELS_SUBTREE. Editing
# it with no revision is a violation like any other model edit.
assert (
len(
violations(
classify(
{DECLARATIVE_BASE_MODULE: "aaa"},
{DECLARATIVE_BASE_MODULE: "bbb"},
),
classify({}, {}),
)
)
== 1
), "an edit to the declarative_base() module needs a revision too"
def test_every_revision_file_is_on_the_single_chain_to_head():
"""No orphaned or duplicated revision hiding in ``versions/``.
A file that is not reachable from head never runs, so the schema it
describes never lands -- the same end state as a missing migration, but
with a file sitting there implying otherwise.
"""
from alembic.config import Config as AlembicConfig
from alembic.script import ScriptDirectory
migrations_dir = get_migrations_dir()
version_files = sorted(
path.name
for path in (migrations_dir / "versions").glob("*.py")
if not path.name.startswith("__")
)
assert len(version_files) >= MIN_REVISIONS, (
f"only {len(version_files)} revision file(s) to walk -- the "
f"migrations directory did not resolve ({migrations_dir})"
)
config = AlembicConfig()
config.set_main_option("script_location", str(migrations_dir))
script = ScriptDirectory.from_config(config)
walked = [
revision.revision
for revision in script.walk_revisions("base", get_head_revision())
]
assert len(walked) == len(version_files), (
f"{len(version_files)} revision file(s) on disk but only "
f"{len(walked)} reachable from head ({sorted(walked)}). A revision "
"that is not on the chain never runs."
)
assert len(set(walked)) == len(walked), (
f"duplicate revision id on the chain to head: {sorted(walked)}"
)
# ---------------------------------------------------------------------------
# 2. the chain applies to a database that already has rows in it
# ---------------------------------------------------------------------------
def test_positive_control_seeded_rows_exist_before_any_upgrade_runs(
tmp_path, request
):
"""Everything below is vacuous if the seed did not land. Prove it did."""
engine, Session = _old_install_at_head(tmp_path, request, "control")
with Session() as session:
settings_count = session.query(Setting).count()
research = session.get(ResearchHistory, RESEARCH_ID)
resources = (
session.query(ResearchResource)
.filter(ResearchResource.research_id == RESEARCH_ID)
.all()
)
assert settings_count > 100, (
f"seeded only {settings_count} settings; a real install has hundreds, "
"so this database does not resemble one"
)
assert research is not None, "seeded research history row is missing"
assert research.report_content == "# Findings\n\nIt had better."
assert len(resources) == 1, (
f"expected 1 seeded research resource, found {len(resources)}"
)
assert _stored_value(Session, "app.theme") == "dark"
assert _stored_value(Session, "web.host") == "0.0.0.0"
assert (
_stored_value(Session, "llm.openai.api_key")
== "sk-user-secret-must-not-be-lost"
)
assert _stored_value(Session, "app.version") == OLD_APP_VERSION
for key in SIMULATED_NEW_KEYS:
assert _stored_value(Session, key) is None, (
f"{key!r} was supposed to be withheld from the seeded 'old' "
"database; with it present, the reconciliation check cannot tell "
"a working import from a no-op"
)
def test_upgrade_from_an_older_revision_keeps_the_rows(tmp_path, request):
"""Skipped-a-few-releases upgrade: 0020 -> head, with data present."""
engine = _engine(tmp_path, request, "older")
_upgrade_to(engine, OLDER_REVISION)
assert get_current_revision(engine) == OLDER_REVISION, (
f"could not stage the database at {OLDER_REVISION}; the rest of this "
"test would measure an upgrade that never happened"
)
Session = _seed_old_install(engine)
run_migrations(engine)
assert get_current_revision(engine) == get_head_revision()
with Session() as session:
research = session.get(ResearchHistory, RESEARCH_ID)
assert research is not None, (
"research history row was lost applying revisions "
f"{OLDER_REVISION}..head to a populated database"
)
assert research.query == "does my data survive the fastapi port"
assert research.research_meta == {"strategy": "source-based"}
assert (
_stored_value(Session, "llm.openai.api_key")
== "sk-user-secret-must-not-be-lost"
)
assert _stored_value(Session, "app.theme") == "dark"
def test_upgrading_a_populated_database_yields_the_same_schema_as_a_fresh_one(
tmp_path, request
):
"""Data present must not change where the chain lands.
Compared against a fresh run of the same chain rather than a hand-listed
expectation, so this stays true as migrations are added.
"""
upgraded = _engine(tmp_path, request, "upgraded")
_upgrade_to(upgraded, OLDER_REVISION)
_seed_old_install(upgraded)
run_migrations(upgraded)
fresh = _engine(tmp_path, request, "fresh_schema")
run_migrations(fresh)
upgraded_schema = _schema_fingerprint(upgraded)
fresh_schema = _schema_fingerprint(fresh)
assert len(fresh_schema) > 10, (
f"the fresh database has only {len(fresh_schema)} tables, so the "
"comparison below has almost nothing to compare"
)
assert upgraded_schema == fresh_schema, (
"a database upgraded from "
f"{OLDER_REVISION} with rows in it ended up with a different schema "
"than a fresh install of the same revision chain -- some migration "
"behaves differently when data is present, which is exactly the case "
"no fresh-install test can see"
)
# ---------------------------------------------------------------------------
# 3. what the app runs against an existing database on first boot
# ---------------------------------------------------------------------------
def _boot(engine, Session):
"""Run the startup path an existing database goes through."""
with Session() as session:
initialize_database(engine, session)
def test_startup_reconciliation_actually_ran(tmp_path, request):
"""Non-vacuity guard for every "survives" assertion in this section.
``_initialize_default_settings`` swallows its own exceptions, so a
reconciliation that blew up leaves the database untouched -- and an
untouched database passes "the user's value survived" trivially. This
test fails in that case: the withheld keys must arrive and the version
marker must move.
"""
engine, Session = _old_install_at_head(tmp_path, request, "ran")
_boot(engine, Session)
for key in SIMULATED_NEW_KEYS:
assert _stored_value(Session, key) is not None, (
f"the upgrade did not add {key!r}, a key shipped in the new "
"defaults but absent from the old database -- the defaults "
"reconciliation did not run (it logs and swallows its own "
"exceptions, so check the log for the real error)"
)
assert _stored_value(Session, "app.version") == package_version, (
"app.version was not advanced, so every subsequent login re-runs the "
"full defaults import (the 'sticky loop')"
)
def test_user_theme_choice_survives_the_changed_default(tmp_path, request):
"""``app.theme`` default went ``dark`` -> ``system`` in this PR."""
engine, Session = _old_install_at_head(tmp_path, request, "theme")
_boot(engine, Session)
assert _stored_value(Session, "app.theme") == "dark", (
"the upgrade replaced the user's chosen theme with this PR's new "
"shipped default ('system'). Defaults reconciliation must refresh "
"metadata only; the stored value belongs to the user."
)
def test_user_bind_address_survives_the_changed_default(tmp_path, request):
"""``web.host`` default went ``0.0.0.0`` -> ``127.0.0.1`` in this PR.
This assertion is intentionally about the persisted settings row. The
server startup path does not read that row, so deployment reachability is
covered by the server-config and deployment contracts instead.
"""
engine, Session = _old_install_at_head(tmp_path, request, "host")
_boot(engine, Session)
assert _stored_value(Session, "web.host") == "0.0.0.0", (
"the upgrade rewrote the persisted web.host row to this PR's new "
"shipped default ('127.0.0.1')"
)
def test_api_key_survives_the_upgrade(tmp_path, request):
engine, Session = _old_install_at_head(tmp_path, request, "apikey")
_boot(engine, Session)
assert (
_stored_value(Session, "llm.openai.api_key")
== "sk-user-secret-must-not-be-lost"
), (
"the upgrade cleared the stored OpenAI API key. It is not recoverable "
"from anywhere else in the install -- the user has to go and mint a "
"new one."
)
assert _stored_value(Session, "llm.provider") == "openai", (
"the upgrade reset the configured LLM provider"
)
def test_upgrade_refreshes_metadata_while_keeping_the_value(tmp_path, request):
"""The reconciliation contract, on a key whose text this PR edited.
``app.theme``'s description went "20 themes" -> "33 themes" in this PR
while its value default went ``dark`` -> ``system``. Both halves must be
observable at once: refreshed schema, retained value. Asserting only the
value would also pass if the import had been skipped entirely.
"""
engine, Session = _old_install_at_head(tmp_path, request, "metadata")
shipped_description = _shipped_defaults()["app.theme"]["description"]
with Session() as session:
before = session.query(Setting).filter(Setting.key == "app.theme").one()
seeded_description = before.description
_boot(engine, Session)
with Session() as session:
after = session.query(Setting).filter(Setting.key == "app.theme").one()
assert after.description == shipped_description, (
"the upgrade left stale metadata on app.theme; the version-bump "
"import is supposed to refresh the JSON-defined schema"
)
assert after.value == "dark", (
"metadata refresh took the user's value with it"
)
# Guard the premise of the first assertion above: if the seeded and the
# shipped description were identical, it would pass without the import
# having refreshed anything.
assert seeded_description == shipped_description, (
"seed drifted from the shipped defaults"
)
def test_locked_account_still_receives_keys_added_by_the_upgrade(
tmp_path, request
):
"""The one functional change this PR makes to ``initialize_database``.
An install with ``app.lock_settings`` set used to have its whole
version-bump import rejected by the settings lock -- while
``update_db_version()`` still moved the marker forward, so the keys the
upgrade introduced were never inserted and never would be. This PR
passes ``override_locked=True`` at that call site. The lock must still
do its real job: the user's stored values are untouched, and the lock
itself survives.
"""
engine, Session = _old_install_at_head(
tmp_path, request, "locked", locked=True
)
assert _stored_value(Session, "app.lock_settings") is True, (
"failed to seed a locked install, so this test would exercise the "
"ordinary unlocked path and prove nothing about override_locked"
)
_boot(engine, Session)
for key in SIMULATED_NEW_KEYS:
assert _stored_value(Session, key) is not None, (
f"a locked account never received {key!r}, a key introduced by "
"the upgrade, yet app.version moved on -- so it never will. This "
"is what override_locked=True at the initialize_database call "
"site exists to prevent."
)
assert _stored_value(Session, "app.lock_settings") is True, (
"the upgrade cleared the settings lock itself"
)
assert _stored_value(Session, "app.theme") == "dark", (
"override_locked must let new keys in, not overwrite stored values"
)
assert (
_stored_value(Session, "llm.openai.api_key")
== "sk-user-secret-must-not-be-lost"
)
def test_fresh_install_receives_the_new_defaults(tmp_path, request):
"""Proves the two defaults really did change, so the tests above bite.
If ``app.theme`` still shipped as ``dark`` and ``web.host`` as
``0.0.0.0``, the survival assertions would pass no matter what the
reconciliation did with the stored rows.
"""
engine = _engine(tmp_path, request, "freshinstall")
run_migrations(engine)
Session = sessionmaker(bind=engine)
with Session() as session:
assert session.query(Setting).count() == 0, (
"migrations pre-populated the settings table, so this is not a "
"fresh install and the defaults below are not newly written"
)
initialize_database(engine, session)
default_theme = SettingsManager(db_session=None).default_settings[
"app.theme"
]["value"]
assert default_theme != USER_CHOICES["app.theme"], (
"the shipped theme default must differ from the seeded old choice "
"for the preservation checks to exercise a changed default"
)
assert _stored_value(Session, "app.theme") == default_theme, (
"a fresh install did not get the registered app.theme default"
)
assert _stored_value(Session, "web.host") == "127.0.0.1", (
"a fresh install did not get this PR's new web.host default"
)
# ---------------------------------------------------------------------------
# 4. research history survives, and is still readable through the ORM
# ---------------------------------------------------------------------------
def test_research_history_survives_startup_and_reads_back_intact(
tmp_path, request
):
"""Every column, not just the primary key.
Reading through the ORM is the point: it is what detects a column the
models expect and the upgraded database does not have. A raw ``SELECT
id`` would pass straight through such a mismatch.
"""
engine, Session = _old_install_at_head(tmp_path, request, "history")
_boot(engine, Session)
with Session() as session:
research = session.get(ResearchHistory, RESEARCH_ID)
assert research is not None, (
"the research history row is gone after startup"
)
assert research.query == "does my data survive the fastapi port"
assert research.mode == "quick_summary"
assert research.status == "completed"
assert research.created_at == "2026-01-02T03:04:05"
assert research.completed_at == "2026-01-02T03:09:05"
assert research.duration_seconds == 300
assert research.title == "Upgrade path"
assert research.report_content == "# Findings\n\nIt had better."
assert research.research_meta == {"strategy": "source-based"}
assert research.progress == 100
resources = (
session.query(ResearchResource)
.filter(ResearchResource.research_id == RESEARCH_ID)
.all()
)
assert len(resources) == 1, (
f"expected the seeded citation to survive, found {len(resources)}"
)
assert resources[0].url == "https://arxiv.org/abs/2401.00001"
assert resources[0].source_type == "academic"