Skip to content
quarry Artifact [c24272680a]
Login

Artifact [c24272680a]

Artifact c24272680a798c02ff195743e1ca4e8f6258a6036d9afb7f8350470c5f34bf3f:


package oltp

// Auto-accept (RFC 0009 §verdicts, the follow-through): a landing a
// decider has armed lands itself when its required checks go green.
// Forge-local OLTP like the check rules beside it — never part of the
// DAG or sync.

const autoSchema = `
CREATE TABLE IF NOT EXISTS landing_auto (
	repo    TEXT NOT NULL,
	landing TEXT NOT NULL,
	account TEXT NOT NULL, -- whose hand writes the deferred accept
	PRIMARY KEY (repo, landing)
);`

// AutoAccept is one armed landing.
type AutoAccept struct{ Repo, Landing, Account string }

// SetAutoAccept arms a landing; re-arming moves it to the new hand.
func (d *DB) SetAutoAccept(repo, landing, account string) error {
	_, err := d.db.Exec(`INSERT INTO landing_auto (repo, landing, account) VALUES (?,?,?)
		ON CONFLICT(repo, landing) DO UPDATE SET account=excluded.account`, repo, landing, account)
	return err
}

// ClearAutoAccept disarms; disarming what is not armed is harmless.
func (d *DB) ClearAutoAccept(repo, landing string) error {
	_, err := d.db.Exec(`DELETE FROM landing_auto WHERE repo=? AND landing=?`, repo, landing)
	return err
}

// AutoAcceptOf reports who armed a landing, if anyone.
func (d *DB) AutoAcceptOf(repo, landing string) (string, bool) {
	var account string
	err := d.db.QueryRow(`SELECT account FROM landing_auto WHERE repo=? AND landing=?`,
		repo, landing).Scan(&account)
	return account, err == nil
}

// AutoAccepts lists everything armed, for the sweep.
func (d *DB) AutoAccepts() ([]AutoAccept, error) {
	rows, err := d.db.Query(`SELECT repo, landing, account FROM landing_auto ORDER BY repo, landing`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var out []AutoAccept
	for rows.Next() {
		var a AutoAccept
		if err := rows.Scan(&a.Repo, &a.Landing, &a.Account); err != nil {
			return nil, err
		}
		out = append(out, a)
	}
	return out, rows.Err()
}