feat(server): revision authors, revisions after a seq, online machines
This commit is contained in:
1 parent
91d83f3032
commit
c2e9284a63
6 files changed
+299
-15
No files matched your search
@@ -20,7 +20,12 @@ const (
|
||||
codeLifetime = 10 * time.Minute
|
||||
sessionLifetime = 30 * 24 * time.Hour
|
||||
revisionLimit = 500
|
||||
schema = `
|
||||
revisionRows = `
|
||||
select r.seq, coalesce(m.name, iif(r.machine_id is null, 'web', 'revoked')), r.author, r.time, r.main_sha, r.main_branch, c.space, c.path, c.base, c.hash
|
||||
from revisions r
|
||||
join changes c on c.repo = r.repo and c.seq = r.seq
|
||||
left join machines m on m.id = r.machine_id`
|
||||
schema = `
|
||||
create table if not exists machines (
|
||||
id integer primary key,
|
||||
name text not null,
|
||||
@@ -49,6 +54,7 @@ create table if not exists revisions (
|
||||
repo text not null,
|
||||
seq integer not null,
|
||||
machine_id integer,
|
||||
author text not null default '',
|
||||
time integer not null,
|
||||
main_sha text not null,
|
||||
main_branch text not null,
|
||||
@@ -86,9 +92,21 @@ func OpenStore(path string) (*Store, error) {
|
||||
if _, err := db.Exec(schema); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := addAuthor(db); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{db: db}, nil
|
||||
}
|
||||
|
||||
func addAuthor(db *sql.DB) error {
|
||||
var found int
|
||||
if err := db.QueryRow(`select count(*) from pragma_table_info('revisions') where name = 'author'`).Scan(&found); err != nil || found > 0 {
|
||||
return err
|
||||
}
|
||||
_, err := db.Exec(`alter table revisions add column author text not null default ''`)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
@@ -292,14 +310,24 @@ func (s *Store) State(ctx context.Context, repo, at string) (protocol.State, err
|
||||
}
|
||||
|
||||
func (s *Store) Revisions(ctx context.Context, repo, path string) ([]protocol.Revision, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
select r.seq, coalesce(m.name, iif(r.machine_id is null, 'web', 'revoked')), r.time, r.main_sha, r.main_branch, c.space, c.path, c.base, c.hash
|
||||
from revisions r
|
||||
join changes c on c.repo = r.repo and c.seq = r.seq
|
||||
left join machines m on m.id = r.machine_id
|
||||
return s.revisions(ctx, revisionRows+`
|
||||
where r.repo = ?1 and (?2 = '' or c.path = ?2)
|
||||
order by r.seq desc, c.path
|
||||
limit ?3`, repo, path, revisionLimit)
|
||||
}
|
||||
|
||||
func (s *Store) RevisionsAfter(ctx context.Context, repo, path string, after int64) ([]protocol.Revision, error) {
|
||||
return s.revisions(ctx, revisionRows+`
|
||||
where r.repo = ?1 and (?2 = '' or c.path = ?2) and r.seq in (
|
||||
select distinct seq from changes
|
||||
where repo = ?1 and (?2 = '' or path = ?2) and seq > ?4
|
||||
order by seq limit ?3
|
||||
)
|
||||
order by r.seq, c.path`, repo, path, revisionLimit, after)
|
||||
}
|
||||
|
||||
func (s *Store) revisions(ctx context.Context, query string, args ...any) ([]protocol.Revision, error) {
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -308,7 +336,7 @@ func (s *Store) Revisions(ctx context.Context, repo, path string) ([]protocol.Re
|
||||
for rows.Next() {
|
||||
var rev protocol.Revision
|
||||
var change protocol.Change
|
||||
if err := rows.Scan(&rev.Seq, &rev.Machine, &rev.Time, &rev.MainSHA, &rev.MainBranch, &change.Space, &change.Path, &change.Base, &change.Hash); err != nil {
|
||||
if err := rows.Scan(&rev.Seq, &rev.Machine, &rev.Author, &rev.Time, &rev.MainSHA, &rev.MainBranch, &change.Space, &change.Path, &change.Base, &change.Hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if last := len(revisions) - 1; last >= 0 && revisions[last].Seq == rev.Seq {
|
||||
@@ -356,7 +384,7 @@ func (s *Store) Push(ctx context.Context, repo string, machineID int64, push pro
|
||||
return 0, err
|
||||
}
|
||||
machine := sql.NullInt64{Int64: machineID, Valid: machineID != 0}
|
||||
if _, err := tx.ExecContext(ctx, `insert into revisions (repo, seq, machine_id, time, main_sha, main_branch) values (?, ?, ?, ?, ?, ?)`, repo, seq, machine, now, push.MainSHA, push.MainBranch); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, `insert into revisions (repo, seq, machine_id, author, time, main_sha, main_branch) values (?, ?, ?, ?, ?, ?, ?)`, repo, seq, machine, push.Author, now, push.MainSHA, push.MainBranch); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, change := range push.Changes {
|
||||
|
||||
Reference in new issue
Block a user