beads/internal/storage/sqlite/dsn.go
Test User 24981afcf6 fix(backends): address red-team review findings before merge
An adversarial fable red-team of the multi-backend PR surfaced 19 findings; the
verified, non-regressing subset is fixed here (two were confirmed intentional and
left as-is with clarified comments).

Dolt-parity (high):
- sqlkit tx AddLabel/RemoveLabel/CloseIssue recorded no audit event, unlike both
  Dolt references, so bd label add/remove and bd batch / mol squash close lost
  their EventLabel*/EventClosed rows on Postgres/MySQL/SQLite. Delegate to the
  same issueops with-event helpers the embedded oracle uses.
- Heartbeat left updated_at stale on Postgres/SQLite (no ON UPDATE trigger;
  MySQL/Dolt auto-bump), so bd stale diverged. Stamp updated_at on the heartbeat
  UPDATE (claim already stamps it, so no double-assign).

Correctness / hardening:
- postgres/mysql/sqlite stampSchemaVersion: idempotent insert (ON CONFLICT /
  INSERT IGNORE / INSERT OR IGNORE) + re-read, so concurrent first provisions do
  not fail one opener with a duplicate-key error.
- sqlkit tx SetConfig now syncs custom_statuses/custom_types like the oracle.
- pgdialect INSTR->POSITION leaves the call loud when both args carry a
  placeholder, preventing a silent parameter swap.
- creds command runner uses cmd.exe on Windows (sh is POSIX-only).
- Dolt gateway credential command is gated on the Dolt backend, not just
  shared-server mode (was failing closed for SQL workspaces).
- Scrub pg/mysql URL passwords from the bd.args telemetry span.

Conformance rigor:
- E2E normalize() maps zero/epoch timestamps to <ZERO-TS> so a lost timestamp no
  longer reads as byte-identical to a real one.
- E2E logs its candidate set and honors BEADS_CONFORMANCE_REQUIRE so a
  sqlite-only run cannot masquerade as full coverage.

Docs/hygiene:
- Scrub dev-machine paths (/data/tmp/pg-recon, proposal_2.md, "proof-wedge") from
  the Postgres schema DDL comment; clarify the SQLite timestamp comment.

Deliberately not changed (would regress): sqlkit UpdateIssue uses the without-event
variant to MATCH dolt-server (only embedded differs); sub-second timestamp
precision is an intentional per-backend waiver (whole-second round-trip is the
portable contract) — forcing round-down would discard PG/SQLite fidelity.

Verified: embedded + postgres + mysql + sqlite + server-dolt TestConformance and
the pg/mysql/sqlite E2E differential all pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 18:58:00 +00:00

36 lines
2.1 KiB
Go

package sqlite
import "strings"
// dsn builds a modernc.org/sqlite DSN for a database file path. Pragmas:
// - foreign_keys(1): FK enforcement (off by default in SQLite).
// - journal_mode(WAL): readers don't block the writer and vice versa, so a read
// no longer collides with an in-flight write.
// - busy_timeout(5000): on lock contention, wait up to 5s for the lock instead of
// immediately surfacing a raw "database is locked" (SQLITE_BUSY). This is the
// cross-process analog of Dolt's transparent serialization retry; combined with
// the single-connection pool (see sqliteDialect.Open) it covers both intra- and
// inter-process contention.
// - _txlock=immediate: writers take the write lock up front and fail fast rather
// than deadlocking mid-transaction on lock upgrade.
//
// _time_format=datetime is required for Dolt parity, not a nicety. Without it modernc
// binds every time.Time through t.String() ("2026-07-09 12:34:56.123 +0000 UTC"), a
// shape SQLite's date functions cannot parse — so the DATE_FORMAT(created_at,…) the
// counts mega-query renders (translated to strftime by sqlitedialect) returns NULL and
// deps_json exposes a zero created_at instead of the real timestamp. datetime stores
// "2026-07-09 12:34:56", which strftime yields as "2026-07-09T12:34:56Z", giving the
// same whole-second GRANULARITY as Dolt/MySQL datetime(0) and unifying bound timestamps
// with the DEFAULT CURRENT_TIMESTAMP rows into one sortable, strftime-parseable format.
//
// Note the granularity matches but the sub-second RULE does not: Go's Format truncates
// the fraction, whereas Dolt/MySQL datetime(0) round half-up (Postgres keeps microseconds).
// This sub-second divergence is an accepted, documented waiver — the portable contract is
// a whole-second round-trip (see conformance.testAuditImportCommentSubSecond); forcing
// round-down parity would only discard the SQL backends' native precision.
func dsn(path string) string {
if strings.HasPrefix(path, "file:") {
return path
}
return "file:" + path + "?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)&_txlock=immediate&_time_format=datetime"
}