sqlite

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sqlite opens OpenCode's database, opencode.db, and reads its rows as the opencode package's types: the V1 tables session, message and part, and the V2 tables session_v2, session_message, session_pending and session_inbox. It is the one package of this module that imports a database driver, modernc.org/sqlite, a SQLite written in Go, so the opencode package stays on the standard library alone.

The database is never opened in place: Open reads a private copy of it and its write-ahead log, which internal/sqlite makes and checks against OpenCode writing to it while it copies, as clinesdk/sqlite does Cline's. Frames appended to the log while it was copied are ones SQLite's recovery reads up to the last commit it finds whole.

Index

Constants

This section is empty.

Variables

View Source
var ErrBusy = errors.New("the database kept changing while it was copied; OpenCode may be running, try again when it is idle")

ErrBusy is returned by Open when the database kept changing while it was copied: OpenCode is writing to it.

View Source
var ErrNotOpenCode = errors.New("the database holds none of OpenCode's session tables (session, message and part, or session_v2 and session_message); Crush's opencode.db is another program's")

ErrNotOpenCode is returned by Open for a database that holds none of OpenCode's session tables, such as Crush's (opencode-ai/opencode), whose tables are sessions and messages.

Functions

func Fixture

func Fixture(path, script string) (io.Closer, error)

Fixture builds a database at path from SQL text, for tests: the fixtures of this module are SQL text, never database files. The database is put in WAL mode with automatic checkpoints off and returned open: rows the script writes after a checkpoint stay in the write-ahead log, where a reader finds them only through the log, for as long as the returned closer is not closed. Closing it checkpoints the log into the database.

func OpenStore

func OpenStore(path string) (opencode.Database, error)

OpenStore is Open as the opencode/convert package takes it, in its Options.OpenDatabase.

func Probe

func Probe(path string) bool

Probe reports whether the file at path is an OpenCode database, reading its schema without copying it: the file is opened immutable, which takes no lock and writes nothing beside it, and reads what has been checkpointed into it, where OpenCode's tables are from the database's first start.

func RemoveCopies

func RemoveCopies()

RemoveCopies removes the copy of every database open: what a program interrupted while it reads one calls before it exits, as a deferred Close does not run then. The copies are in os.TempDir, under $TMPDIR when it is set.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is a copy of an OpenCode database, open for reading. It implements opencode.Store. A DB is not safe for concurrent use.

func Open

func Open(path string) (*DB, error)

Open copies the database at path and opens the copy. Close removes it.

func (*DB) Close

func (d *DB) Close() error

Close closes the copy and removes it.

func (*DB) Has

func (d *DB) Has(table string) bool

Has reports whether the database holds a table.

func (*DB) IsOpenCode

func (d *DB) IsOpenCode() bool

IsOpenCode reports whether the database holds OpenCode's V1 or V2 session tables.

func (*DB) Messages

func (d *DB) Messages(s *opencode.Session) ([]*opencode.MessageRecord, []*opencode.Stray, error)

Messages reads a V1 session's message and part rows.

func (*DB) MigrationDone

func (d *DB) MigrationDone() bool

MigrationDone reports whether OpenCode 2 recorded its copy of the V1 tables complete, in kv's migration.v1-v2.

func (*DB) Name

func (d *DB) Name() string

Name names the database in findings: its file name.

func (*DB) Orphans

func (d *DB) Orphans(map[string]bool) ([]*opencode.Stray, error)

Orphans lists the message rows of no session of the V1 tables, the part rows of no message read with one, and the session_message, session_pending and session_inbox rows of no session of the V2 tables. sessions is not read: every row of a session is read with it, so a row is an orphan when its session row does not exist. A V1 database's own session_message, OpenCode 1's projection of its messages, which no V1 request reads, is reported once, its rows not records.

func (*DB) Project

func (d *DB) Project(id string) (opencode.Project, bool)

Project reads a project row.

func (*DB) Rows

func (d *DB) Rows(s *opencode.Session) ([]*opencode.Row2, []*opencode.QueueRow, error)

Rows reads a V2 session's session_message rows and its queued input.

func (*DB) Sessions

func (d *DB) Sessions() ([]*opencode.Session, []*opencode.Stray, error)

Sessions lists the rows of session (G2) and session_v2 (G3).

func (*DB) Units

func (d *DB) Units() (int, error)

Units counts the rows of the tables a conversion reads, the session tables of each generation it reads and their records, so a test can hold a report's tally to it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL