// pin.go stores which repositories an account has pinned to the top of
// its home board. A pin is a person's own ordering, social state like a
// read pointer: wiping the table loses an arrangement, never a fact the
// DAG holds. It is a table of its own, not an account_pref value or a
// repo_caps column, because the order matters and because a repository
// reached only through a team has no repo_caps row to hang it on.
package oltp
const pinSchema = `
CREATE TABLE IF NOT EXISTS account_pin (
account TEXT NOT NULL,
repo TEXT NOT NULL, -- the repository's key, as repo_caps names it
pinned_at TEXT NOT NULL,
PRIMARY KEY (account, repo)
);`
// SetPin pins or unpins one repository for one account. Pinning what is
// already pinned keeps its place; unpinning what never was is not an
// error: the caller asked for it to be unpinned and afterwards it is.
func (d *DB) SetPin(account, repo string, pinned bool) error {
if account == "" || repo == "" {
return nil
}
if !pinned {
_, err := d.w.Exec(`DELETE FROM account_pin WHERE account = ? AND repo = ?`, account, repo)
return err
}
_, err := d.w.Exec(`INSERT OR IGNORE INTO account_pin (account, repo, pinned_at)
VALUES (?, ?, ?)`, account, repo, now())
return err
}
// Pins lists an account's pinned repositories in the order they were
// pinned. It names repositories the account may no longer read; the
// caller shows only those it still can.
func (d *DB) Pins(account string) ([]string, error) {
if account == "" {
return nil, nil
}
rows, err := d.db.Query(`SELECT repo FROM account_pin
WHERE account = ? ORDER BY pinned_at, rowid`, account)
if err != nil {
return nil, err
}
defer rows.Close()
var out []string
for rows.Next() {
var repo string
if err := rows.Scan(&repo); err != nil {
return nil, err
}
out = append(out, repo)
}
return out, rows.Err()
}