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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package upgrades
  2. import (
  3. "database/sql"
  4. "fmt"
  5. )
  6. func init() {
  7. upgrades[39] = upgrade{"Add backfill queue", func(tx *sql.Tx, ctx context) error {
  8. // The queue_id needs to auto-increment every insertion. For SQLite,
  9. // INTEGER PRIMARY KEY is an alias for the ROWID, so it will
  10. // auto-increment. See https://sqlite.org/lang_createtable.html#rowid
  11. // For Postgres, we need to add GENERATED ALWAYS AS IDENTITY for the
  12. // same functionality.
  13. queueIDColumnTypeModifier := ""
  14. if ctx.dialect == Postgres {
  15. queueIDColumnTypeModifier = "GENERATED ALWAYS AS IDENTITY"
  16. }
  17. _, err := tx.Exec(fmt.Sprintf(`
  18. CREATE TABLE backfill_queue (
  19. queue_id INTEGER PRIMARY KEY %s,
  20. user_mxid TEXT,
  21. type INTEGER NOT NULL,
  22. priority INTEGER NOT NULL,
  23. portal_jid TEXT,
  24. portal_receiver TEXT,
  25. time_start TIMESTAMP,
  26. time_end TIMESTAMP,
  27. max_batch_events INTEGER NOT NULL,
  28. max_total_events INTEGER,
  29. batch_delay INTEGER,
  30. completed_at TIMESTAMP,
  31. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  32. FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE
  33. )
  34. `, queueIDColumnTypeModifier))
  35. if err != nil {
  36. return err
  37. }
  38. return err
  39. }}
  40. }