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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  19. FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE
  20. )
  21. `)
  22. if err != nil {
  23. return err
  24. }
  25. // The queue_id needs to auto-increment every insertion. For SQLite,
  26. // INTEGER PRIMARY KEY is an alias for the ROWID, so it will
  27. // auto-increment. See https://sqlite.org/lang_createtable.html#rowid
  28. // For Postgres, we have to manually add the sequence.
  29. if ctx.dialect == Postgres {
  30. _, err = tx.Exec(`
  31. CREATE SEQUENCE backfill_queue_queue_id_seq
  32. START WITH 1
  33. OWNED BY backfill_queue.queue_id
  34. `)
  35. if err != nil {
  36. return err
  37. }
  38. _, err = tx.Exec(`
  39. ALTER TABLE backfill_queue
  40. ALTER COLUMN queue_id
  41. SET DEFAULT nextval('backfill_queue_queue_id_seq'::regclass)
  42. `)
  43. if err != nil {
  44. return err
  45. }
  46. }
  47. return err
  48. }}
  49. }