package database import ( "context" "database/sql" "errors" "fmt" ) // LocalMetaKeyStorageURL is the key under which the destination store's // URL is recorded when a mutating command first binds the local index // to a specific backup destination. const LocalMetaKeyStorageURL = "storage_url" // LocalMetaRepository provides keyed access to host-local settings // stored in the local_meta table. type LocalMetaRepository struct { db *DB } func NewLocalMetaRepository(db *DB) *LocalMetaRepository { return &LocalMetaRepository{db: db} } // Get returns the value stored at key, or the empty string if the key // is not set. A missing key is not an error — the caller distinguishes // "unset" (bind on first use) from "set to something" (compare). func (r *LocalMetaRepository) Get(ctx context.Context, key string) (string, error) { var value string err := r.db.conn.QueryRowContext(ctx, "SELECT value FROM local_meta WHERE key = ?", key, ).Scan(&value) if errors.Is(err, sql.ErrNoRows) { return "", nil } if err != nil { return "", fmt.Errorf("reading local_meta %q: %w", key, err) } return value, nil } // Set writes key=value, replacing any prior value. func (r *LocalMetaRepository) Set(ctx context.Context, key, value string) error { _, err := r.db.ExecWithLog(ctx, `INSERT INTO local_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`, key, value, ) if err != nil { return fmt.Errorf("writing local_meta %q: %w", key, err) } return nil }