add basic schema and sync function, sorting by appointment type
This commit is contained in:
parent
b665bf63a0
commit
40f16346f5
4 changed files with 207 additions and 33 deletions
82
store.go
82
store.go
|
|
@ -9,18 +9,22 @@ import (
|
|||
)
|
||||
|
||||
const schema = `
|
||||
CREATE TABLE IF NOT EXISTS runs (
|
||||
id INTEGER PRIMARY KEY,
|
||||
chapter_id INTEGER NOT NULL,
|
||||
department TEXT NOT NULL,
|
||||
rep_user_id INTEGER NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ended_at TEXT
|
||||
CREATE TABLE IF NOT EXISTS people (
|
||||
id INTEGER PRIMARY KEY,
|
||||
email TEXT,
|
||||
chapter_id INTEGER,
|
||||
department TEXT,
|
||||
appointment_type TEXT,
|
||||
is_rep INTEGER NOT NULL DEFAULT 0,
|
||||
synced_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS one_run_per_dept
|
||||
ON runs(chapter_id, department) WHERE status = 'running';
|
||||
CREATE INDEX IF NOT EXISTS people_dept ON people(chapter_id, department);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sync_state (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
type SQLStore struct{ db *sql.DB }
|
||||
|
|
@ -64,3 +68,61 @@ func (s *SQLStore) SweepStaleLocks() error {
|
|||
WHERE status='running'`)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLStore) UpsertPeople(users []User, eligible map[string]bool) error {
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
stmt, err := tx.Prepare(`
|
||||
INSERT INTO people (id, email, chapter_id, department, appointment_type, is_rep, synced_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
email=excluded.email,
|
||||
chapter_id=excluded.chapter_id,
|
||||
department=excluded.department,
|
||||
appointment_type=excluded.appointment_type,
|
||||
is_rep=excluded.is_rep,
|
||||
synced_at=CURRENT_TIMESTAMP`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, u := range users {
|
||||
rep := 0
|
||||
if u.IsRep {
|
||||
rep = 1
|
||||
}
|
||||
_, err := stmt.Exec(u.ID, u.Email, u.ChapterID,
|
||||
normalizeDept(u.Department),
|
||||
strings.Join(u.AppointmentTypes, ","),
|
||||
rep)
|
||||
if err != nil {
|
||||
return fmt.Errorf("upserting user %d: %w", u.ID, err)
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *SQLStore) LastSync() (int64, error) {
|
||||
var v string
|
||||
err := s.db.QueryRow(`SELECT value FROM sync_state WHERE key='users_synced_at'`).Scan(&v)
|
||||
if err == sql.ErrNoRows {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return strconv.ParseInt(v, 10, 64)
|
||||
}
|
||||
|
||||
func (s *SQLStore) SetLastSync(ts int64) error {
|
||||
_, err := s.db.Exec(`
|
||||
INSERT INTO sync_state (key, value) VALUES ('users_synced_at', ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value`,
|
||||
strconv.FormatInt(ts, 10))
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue