Something went wrong. Try again.
A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
Something went wrong. Try again.
1.8 kB · 78 lines
Go
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879package db
import ( "database/sql" "time")
// GetRepositoryAnnotations retrieves all annotations for a repositoryfunc GetRepositoryAnnotations(db *sql.DB, did, repository string) (map[string]string, error) { rows, err := db.Query(` SELECT key, value FROM repository_annotations WHERE did = ? AND repository = ? `, did, repository) if err != nil { return nil, err } defer rows.Close()
annotations := make(map[string]string) for rows.Next() { var key, value string if err := rows.Scan(&key, &value); err != nil { return nil, err } annotations[key] = value }
return annotations, rows.Err()}
// UpsertRepositoryAnnotations replaces all annotations for a repository// Only called when manifest has at least one non-empty annotationfunc UpsertRepositoryAnnotations(db *sql.DB, did, repository string, annotations map[string]string) error { tx, err := db.Begin() if err != nil { return err } defer tx.Rollback()
// Delete existing annotations _, err = tx.Exec(` DELETE FROM repository_annotations WHERE did = ? AND repository = ? `, did, repository) if err != nil { return err }
// Insert new annotations stmt, err := tx.Prepare(` INSERT INTO repository_annotations (did, repository, key, value, updated_at) VALUES (?, ?, ?, ?, ?) `) if err != nil { return err } defer stmt.Close()
now := time.Now() for key, value := range annotations { _, err = stmt.Exec(did, repository, key, value, now) if err != nil { return err } }
return tx.Commit()}
// DeleteRepositoryAnnotations removes all annotations for a repositoryfunc DeleteRepositoryAnnotations(db *sql.DB, did, repository string) error { _, err := db.Exec(` DELETE FROM repository_annotations WHERE did = ? AND repository = ? `, did, repository) return err}