db

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Watch

func Watch(d *DB) <-chan struct{}

Watch starts watching the filesystem for external changes to the database -- a write from another process (another "notch" CLI invocation, or the aliasOS/Omarchy bar-widget plugin, both of which are just short-lived CLI calls with no way to otherwise notify a long-running consumer) -- and returns a channel that receives a value shortly after, a fast trigger for a long-running consumer (the TUI) to reload rather than wait for its next poll tick. Mirrors internal/theme's own Watch(): same reasoning, same debounce, same "nil channel on failure, caller falls back to polling" contract.

Getting this right took two wrong turns, both confirmed with inotifywait against a real WAL-mode DB:

  1. Watching "<path>-wal" (or the directory containing it, catching -wal along with everything else): every notch invocation, reads included, opens/checkpoints/closes that file, deleting and recreating it each time. A watch on it fires on *reads* too, and since d's own reload after a fire itself reads the DB, that's a self-triggering loop -- a "poll as fast as possible" watch, not the efficient one this is meant to be.
  2. Watching "<path>" itself instead: a genuine write only produces a MODIFY event on it once SQLite checkpoints the WAL back in, and SQLite only auto-checkpoints on the *last* connection to the file closing -- which never happens while d itself (the long-running consumer's own connection, e.g. the TUI's) stays open. So this fixed the self-triggering problem but then almost never fired during the one scenario Watch exists for: the consumer running with an open connection, watching for someone else's write.

The actual fix doesn't try to infer "did the DB change" from *which* file got touched or how -- it asks SQLite directly, via `PRAGMA data_version` on d's own connection. That pragma is specifically designed for this: it changes the instant any *other* connection commits a write (no checkpoint required -- confirmed by querying it on an already-open connection immediately after a separate connection's write), and it does not change on reads, from this connection or any other. So the directory watch below still exists (still watching "<path>", "-wal" and "-shm" together, whichever a given write happens to touch) purely as a low-cost "something might have happened, go check" trigger; the data_version comparison is what actually decides whether to notify the caller, making a spurious wake from a pure read a no-op instead of a self-triggering reload.

Returns nil if the OS watcher couldn't be started, the directory doesn't exist (e.g. a NOTCH_DB pointed somewhere never created), or the initial data_version read fails; callers should treat a nil channel as one that never fires and keep their own poll fallback.

Types

type DB

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

func Open

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

Open opens (or creates) the SQLite database at the given path.

func (*DB) Close

func (d *DB) Close() error

func (*DB) CreateClient

func (d *DB) CreateClient(name string, hourlyRate float64) (*model.Client, error)

func (*DB) CreateProject

func (d *DB) CreateProject(clientID int64, name string) (*model.Project, error)

func (*DB) DeleteClient

func (d *DB) DeleteClient(id int64) error

func (*DB) DeleteEntry

func (d *DB) DeleteEntry(id int64) error

DeleteEntry deletes one entry, and errors if there's no entry with that ID (say, another process already deleted it).

func (*DB) DeleteProject

func (d *DB) DeleteProject(id int64) error

func (*DB) DeleteTaskEntries added in v0.10.0

func (d *DB) DeleteTaskEntries(projectID int64, task string) (int64, error)

DeleteTaskEntries deletes every entry for one (project, task) pairing -- the same grouping ListRecentTasks reports -- including a running one, and returns how many were removed. Same-named tasks on other projects are a different pairing and are left alone.

func (*DB) EnsureUncategorizedProject

func (d *DB) EnsureUncategorizedProject() (int64, error)

EnsureUncategorizedProject returns (or creates) a catch-all "Uncategorized / General" project for untagged timers -- e.g. a quick-add that only specifies a task, no client/project. Moved here (from what was internal/ui/views' unexported ensureUncategorizedProject) alongside ResolveOrCreateProject, for the same reason: the CLI needs it too, not just the TUI's own new-timer form.

func (*DB) GetClient

func (d *DB) GetClient(id int64) (*model.Client, error)

func (*DB) GetProject

func (d *DB) GetProject(id int64) (*model.Project, error)

func (*DB) GetRunningEntry

func (d *DB) GetRunningEntry() (*model.Entry, error)

func (*DB) ListClients

func (d *DB) ListClients() ([]*model.Client, error)

func (*DB) ListEntries

func (d *DB) ListEntries(projectID int64, dateFrom, dateTo string, includeRunning bool) ([]*model.Entry, error)

ListEntries lists entries with optional filters. Pass zero values to skip a filter. dateFrom/dateTo format: "2006-01-02"

func (*DB) ListFinishedEntriesOverlapping added in v0.10.0

func (d *DB) ListFinishedEntriesOverlapping(from, to time.Time) ([]*model.Entry, error)

ListFinishedEntriesOverlapping returns the finished entries whose time span overlaps [from, to), newest first -- including one that started before from, which ListEntries' start-date filter would miss.

func (*DB) ListProjects

func (d *DB) ListProjects(clientID int64) ([]*model.Project, error)

func (*DB) ListRecentTasks

func (d *DB) ListRecentTasks(limit int) ([]*model.RecentTask, error)

ListRecentTasks returns the most recently-used distinct (project, task) pairings, most recent first, each with its accumulated duration across every finished entry that used that exact task string on that project -- for a picker that wants to resume a specific named task (e.g. "Meeting" on "Website") rather than just start a bare project. Like ReportByProject, running entries are excluded from the total; a task that's never been finished before won't appear here until it has.

func (*DB) Path

func (d *DB) Path() string

Path returns the filesystem path this DB was opened from -- e.g. for Watch, which needs it but isn't itself a DB method (it has to work before/without an open connection, and doesn't want one).

func (*DB) ReportByProject

func (d *DB) ReportByProject(dateFrom, dateTo string) ([]*model.ReportRow, error)

ReportByProject returns aggregated hours/earnings grouped by client + project, with individual entries attached to each row for drill-down display.

func (*DB) ResolveOrCreateProject

func (d *DB) ResolveOrCreateProject(clientID int64, clientName string, projectID int64, projectName string) (int64, error)

ResolveOrCreateProject finds or creates the client+project and returns the project ID. Returns 0 if both clientName and projectName are blank (uncategorized). Moved here (from what was internal/ui/views' unexported resolveProject) so the CLI ("notch start --client-name/ --project-name") and the TUI's own new-timer form share one implementation instead of the CLI needing its own copy or an import of the ui/views package.

func (*DB) SetEntryInvoiced

func (d *DB) SetEntryInvoiced(id int64, invoiced bool) error

SetEntryInvoiced marks one or more entries as invoiced/uninvoiced.

func (*DB) SetEntryPaid

func (d *DB) SetEntryPaid(id int64, paid bool) error

SetEntryPaid marks one or more entries as paid/unpaid.

func (*DB) StartEntry

func (d *DB) StartEntry(projectID int64, task string) (*model.Entry, error)

func (*DB) StopAllRunning

func (d *DB) StopAllRunning() error

func (*DB) StopEntry

func (d *DB) StopEntry(id int64) error

func (*DB) UpdateClient

func (d *DB) UpdateClient(c *model.Client) error

func (*DB) UpdateEntry

func (d *DB) UpdateEntry(e *model.Entry) error

func (*DB) UpdateProject

func (d *DB) UpdateProject(p *model.Project) error

func (*DB) UpdateTaskEntries added in v0.10.0

func (d *DB) UpdateTaskEntries(fromProjectID int64, fromTask string, toProjectID int64, toTask string) (int64, error)

UpdateTaskEntries re-files every entry of one (project, task) pairing under another -- a rename, a move to a different project, or both -- and returns how many entries changed. If the target pairing already has entries, the two simply merge into one ListRecentTasks row.

Jump to

Keyboard shortcuts

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