2022-03-15-prioritized-backfill.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package upgrades
  2. import "database/sql"
  3. func init() {
  4. upgrades[39] = upgrade{"Add backfill queue", func(tx *sql.Tx, ctx context) error {
  5. _, err := tx.Exec(`
  6. CREATE TABLE backfill_queue (
  7. queue_id INTEGER PRIMARY KEY,
  8. user_mxid TEXT,
  9. type INTEGER NOT NULL,
  10. priority INTEGER NOT NULL,
  11. portal_jid VARCHAR(255),
  12. portal_receiver VARCHAR(255),
  13. time_start TIMESTAMP,
  14. time_end TIMESTAMP,
  15. max_batch_events INTEGER NOT NULL,
  16. max_total_events INTEGER,
  17. batch_delay INTEGER,
  18. completed_at TIMESTAMP,
  19. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  20. FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE
  21. )
  22. `)
  23. if err != nil {
  24. return err
  25. }
  26. // The queue_id needs to auto-increment every insertion. For SQLite,
  27. // INTEGER PRIMARY KEY is an alias for the ROWID, so it will
  28. // auto-increment. See https://sqlite.org/lang_createtable.html#rowid
  29. // For Postgres, we have to manually add the sequence.
  30. if ctx.dialect == Postgres {
  31. _, err = tx.Exec(`
  32. CREATE SEQUENCE backfill_queue_queue_id_seq
  33. START WITH 1
  34. OWNED BY backfill_queue.queue_id
  35. `)
  36. if err != nil {
  37. return err
  38. }
  39. _, err = tx.Exec(`
  40. ALTER TABLE backfill_queue
  41. ALTER COLUMN queue_id
  42. SET DEFAULT nextval('backfill_queue_queue_id_seq'::regclass)
  43. `)
  44. if err != nil {
  45. return err
  46. }
  47. }
  48. return err
  49. }}
  50. }