Skip to content

perf(db): create a fresh database at its latest schema in one step - #4909

Open
KuSh wants to merge 1 commit into
rtk-ai:developfrom
KuSh:fix/db-schema-snapshot
Open

KuSh wants to merge 1 commit into
rtk-ai:developfrom
KuSh:fix/db-schema-snapshot

Conversation

@KuSh

@KuSh KuSh commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #4894: this PR's base is a copy of that branch, so the diff shows only this change. It is retargeted to develop once #4894 is merged.

Summary

A database with no schema object is now created directly at its latest schema, in one transaction, instead of replaying every migration step from version 1. Each db::Schema carries a snapshot: the SQL that creates the schema its last step produces, with columns in the order the steps leave them. Existing databases (legacy unversioned, partial, or at an intermediate stamp) keep the step path.

The engine uses the snapshot at the start of run_steps, in an IMMEDIATE transaction that re-reads the tag and the stamp under the lock: when the stamp is 0 and sqlite_master is empty it runs the snapshot, stamps the last step's version and writes the tag together. That covers a file this open just created, an empty file, and two processes creating the same file at once (the second finds it current under the lock). rtk init (Always) uses it on an empty database too, and replays the steps on any other.

A schema change now updates its step and the snapshot. A per-database equivalence test builds one database from the snapshot and one through every step from empty, compares them structurally (columns by position, type, NOT NULL, default and primary key; indexes by uniqueness, origin and ordered columns; the tag and the stamp), and fails naming the differing lines. The rule is documented on db::Schema next to the rules for steps.

  • A fresh tracking database no longer runs migrate_initial twice (Initial, then InitialRecheck) and is created in one transaction.
  • A fresh recall store is created in one transaction.
  • No change for a database that already has a schema object or a nonzero stamp.

Test plan

  • Snapshot path
    • A new database of each kind is created from its snapshot and no step runs (steps replaced by failing ones)
    • An empty existing file (zero bytes, WAL-only, tagged but empty) takes the snapshot
    • rtk init on an empty database takes the snapshot; on a database with tables it replays the steps
    • The stamp is the last step's version and the tag is written, in the same transaction
    • A snapshot that fails, or that SQLite rolls back, leaves the file empty and unstamped
  • Step path
    • A legacy unversioned database with tables takes the steps
    • A database at an intermediate stamp takes the steps above it
    • A stamped file with no table takes neither
    • Existing step tests build their older-release databases from the steps alone (replay_steps), so they still pin what each step does
  • Concurrency
    • A second creator waiting on the lock finds the database created by the first and creates nothing
    • Two openers racing on a new path both succeed and the snapshot ran once (20 rounds)
    • A tag written by another process while waiting for the lock is refused on the snapshot path and on the step path
  • Equivalence
    • Tracking and recall snapshots match their steps
    • The check names a missing column, columns in another order, a different type, default or NOT NULL, a missing or non-unique index, index columns in another order or direction, and an extra table
  • Gate
    • cargo fmt --all, cargo clippy --all-targets, cargo test --all (also --no-default-features)
    • Windows cross-check

🤖 Generated with Claude Code

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

♻️ Type refactor
🟡 Risk medium

Summary

Adds a snapshot field to db::Schema so a brand-new database is created directly at its latest schema in one IMMEDIATE transaction instead of replaying every migration step from version 1. Existing databases (legacy, partial, or at an intermediate stamp) still take the step path, and a per-database equivalence test verifies the snapshot and step paths produce structurally identical schemas.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@KuSh
KuSh force-pushed the fix/schema-version-enum branch from f8f8180 to d3a5497 Compare November 4, 2026 18:27
@KuSh
KuSh force-pushed the fix/db-schema-snapshot branch from 2899264 to 09e6430 Compare November 4, 2026 18:27
@KuSh
KuSh force-pushed the fix/schema-version-enum branch from d3a5497 to 5c66e2a Compare November 6, 2026 18:18
@KuSh
KuSh force-pushed the fix/db-schema-snapshot branch from 09e6430 to ab35d47 Compare November 6, 2026 18:18
@KuSh
KuSh force-pushed the fix/schema-version-enum branch from 5c66e2a to 9690e12 Compare November 6, 2026 19:30
@KuSh
KuSh force-pushed the fix/db-schema-snapshot branch from ab35d47 to 59c07dd Compare November 6, 2026 19:30
@KuSh
KuSh force-pushed the fix/schema-version-enum branch from 9690e12 to 94aa52e Compare November 17, 2026 00:32
@KuSh
KuSh force-pushed the fix/db-schema-snapshot branch from 59c07dd to d5f059a Compare November 17, 2026 00:33
@KuSh
KuSh changed the base branch from fix/schema-version-enum to develop November 19, 2026 00:13
A database with no schema object is created from its snapshot in one
transaction instead of replaying every step; others keep the step path.
Per-database tests keep each snapshot equal to what its steps produce.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@KuSh
KuSh force-pushed the fix/db-schema-snapshot branch from d5f059a to d756ba6 Compare November 19, 2026 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant