package store import ( "context" "database/sql" "errors" "time" ) // PutClusterState keeps a value under a key, for machines that read the // store rather than a control plane; it reports whether it changed. func (s *DB) PutClusterState(ctx context.Context, key, value string) (bool, error) { if old, err := s.ClusterState(ctx, key); err == nil && old == value { return false, nil } _, err := s.db.ExecContext(ctx, `INSERT INTO cluster_state (key, value, updated_at) VALUES (?, ?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`, key, value, millis(time.Now())) return err == nil, err } // ClusterState reads a value; ErrNotFound when there is none. func (s *DB) ClusterState(ctx context.Context, key string) (string, error) { var v string err := s.db.QueryRowContext(ctx, `SELECT value FROM cluster_state WHERE key = ?`, key).Scan(&v) if errors.Is(err, sql.ErrNoRows) { return "", ErrNotFound } return v, err }