internal/store/aboutbackfill.go

e9566eed86ebcd185c4b85f63d667b5e671fe787
gitbay/internal/store/aboutbackfill.go history · blame · raw

49 lines · 1475 bytes

 1package store
 2
 3// AboutRow is one owner's parked about text, waiting to become a file in
 4// <owner>/.gitbay. Migration 0058 fills the table; the backfill command
 5// drains it.
 6type AboutRow struct {
 7	OwnerKind string
 8	OwnerID   int64
 9	OwnerName string
10	About     string
11	Format    string
12}
13
14// PendingAboutBackfill lists the owners whose about text has not been
15// written to a repository yet, resolving each one's name.
16func (s *Store) PendingAboutBackfill() ([]AboutRow, error) {
17	rows, err := s.DB.Query(`
18		SELECT b.owner_kind, b.owner_id, b.about, b.about_format,
19		       COALESCE(u.username, o.name)
20		  FROM profile_about_backfill b
21		  LEFT JOIN users u ON b.owner_kind = 'user' AND u.id = b.owner_id
22		  LEFT JOIN orgs  o ON b.owner_kind = 'org'  AND o.id = b.owner_id
23		 ORDER BY b.owner_kind, b.owner_id`)
24	if err != nil {
25		return nil, err
26	}
27	defer rows.Close()
28	var out []AboutRow
29	for rows.Next() {
30		var r AboutRow
31		var name *string
32		if err := rows.Scan(&r.OwnerKind, &r.OwnerID, &r.About, &r.Format, &name); err != nil {
33			return nil, err
34		}
35		if name == nil {
36			continue // the owner is gone; the row goes with them
37		}
38		r.OwnerName = *name
39		out = append(out, r)
40	}
41	return out, rows.Err()
42}
43
44// ClearAboutBackfill drops one owner's row once its file exists.
45func (s *Store) ClearAboutBackfill(kind string, id int64) error {
46	_, err := s.DB.Exec(
47		"DELETE FROM profile_about_backfill WHERE owner_kind = ? AND owner_id = ?", kind, id)
48	return err
49}