beads/internal/storage/sqlite/schema.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

124 lines
4.4 KiB
Go

package sqlite
import (
"context"
"database/sql"
_ "embed"
"errors"
"fmt"
"strings"
)
// ddl is the SQLite backend schema, embedded from schema.sql. It is the Dolt/MySQL
// schema with SQLite adjustments: ENGINE/CHARSET/COLLATE stripped, inline KEY/UNIQUE
// KEY lifted to CREATE INDEX (SQLite forbids inline non-PK indexes), ON UPDATE
// CURRENT_TIMESTAMP dropped (unsupported), json → text, and prefix-index lengths
// removed (SQLite has no InnoDB key-length cap). Backticks, `?` placeholders, and
// tinyint(1) all work natively.
//
//go:embed schema.sql
var ddl string
const (
schemaVersion = "1"
schemaVersionKey = "sqlite_schema_version"
)
// InitSchema applies the DDL, seeds the default config rows on first provision, and
// stamps the schema version. db is a raw modernc connection (the DDL and seeds are
// native SQLite and need no dialect translation).
func InitSchema(ctx context.Context, db *sql.DB) error {
for _, stmt := range splitStatements(ddl) {
if _, err := db.ExecContext(ctx, stmt); err != nil {
return fmt.Errorf("sqlite: exec DDL statement: %w\n-- statement:\n%s", err, stmt)
}
}
fresh, err := stampSchemaVersion(ctx, db)
if err != nil {
return err
}
if fresh {
if err := seedDefaultConfig(ctx, db); err != nil {
return err
}
}
return nil
}
// defaultConfigSeeds mirrors migration 0016 exactly. Seeded only on first provision.
var defaultConfigSeeds = [][2]string{
{"compaction_enabled", "false"},
{"compact_tier1_days", "30"},
{"compact_tier1_dep_levels", "2"},
{"compact_tier2_days", "90"},
{"compact_tier2_dep_levels", "5"},
{"compact_tier2_commits", "100"},
{"compact_batch_size", "50"},
{"compact_parallel_workers", "5"},
{"auto_compact_enabled", "false"},
}
func seedDefaultConfig(ctx context.Context, db *sql.DB) error {
for _, kv := range defaultConfigSeeds {
if _, err := db.ExecContext(ctx,
"INSERT OR IGNORE INTO config (`key`, `value`) VALUES (?, ?)", kv[0], kv[1]); err != nil {
return fmt.Errorf("sqlite: seed default config %q: %w", kv[0], err)
}
}
return nil
}
// stampSchemaVersion records schemaVersion on first provision and refuses a mismatched
// workspace (no migrator in the wedge). Returns fresh=true only on the first stamp.
func stampSchemaVersion(ctx context.Context, db *sql.DB) (fresh bool, err error) {
var stored string
err = db.QueryRowContext(ctx, "SELECT `value` FROM metadata WHERE `key` = ?", schemaVersionKey).Scan(&stored)
switch {
case errors.Is(err, sql.ErrNoRows):
// Provision runs on every open; two bd processes opening a not-yet-
// provisioned workspace concurrently can both reach here. INSERT OR
// IGNORE lets both succeed; only the opener that inserted the row
// (RowsAffected==1) is fresh and seeds defaults.
res, ierr := db.ExecContext(ctx, "INSERT OR IGNORE INTO metadata (`key`, `value`) VALUES (?, ?)", schemaVersionKey, schemaVersion)
if ierr != nil {
return false, fmt.Errorf("sqlite: stamp schema version: %w", ierr)
}
if n, _ := res.RowsAffected(); n == 1 {
return true, nil
}
// Lost the race: re-read what the winner stored and version-check it.
if rerr := db.QueryRowContext(ctx, "SELECT `value` FROM metadata WHERE `key` = ?", schemaVersionKey).Scan(&stored); rerr != nil {
return false, fmt.Errorf("sqlite: read schema version after conflict: %w", rerr)
}
if stored != schemaVersion {
return false, fmt.Errorf("sqlite: workspace schema version %s, this binary requires %s — recreate the workspace or use a matching binary", stored, schemaVersion)
}
return false, nil
case err != nil:
return false, fmt.Errorf("sqlite: read schema version: %w", err)
case stored != schemaVersion:
return false, fmt.Errorf("sqlite: workspace schema version %s, this binary requires %s — recreate the workspace or use a matching binary", stored, schemaVersion)
default:
return false, nil
}
}
// splitStatements splits the DDL on ';' at statement boundaries; the schema has no
// procedural bodies or embedded ';'. Blank fragments and line comments are dropped.
func splitStatements(ddl string) []string {
var stmts []string
for _, raw := range strings.Split(ddl, ";") {
var b strings.Builder
for _, line := range strings.Split(raw, "\n") {
if strings.HasPrefix(strings.TrimSpace(line), "--") {
continue
}
b.WriteString(line)
b.WriteString("\n")
}
if s := strings.TrimSpace(b.String()); s != "" {
stmts = append(stmts, s)
}
}
return stmts
}