2021-10-21-multidevice-updates.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package upgrades
  2. import (
  3. "database/sql"
  4. )
  5. func init() {
  6. upgrades[25] = upgrade{"Update things for multidevice", func(tx *sql.Tx, ctx context) error {
  7. // This is probably not necessary
  8. _, err := tx.Exec("DROP TABLE user_portal")
  9. if err != nil {
  10. return err
  11. }
  12. // Remove invalid puppet rows
  13. _, err = tx.Exec("DELETE FROM puppet WHERE jid LIKE '%@g.us' OR jid LIKE '%@broadcast'")
  14. if err != nil {
  15. return err
  16. }
  17. // Remove the suffix from puppets since they'll all have the same suffix
  18. _, err = tx.Exec("UPDATE puppet SET jid=REPLACE(jid, '@s.whatsapp.net', '')")
  19. if err != nil {
  20. return err
  21. }
  22. // Rename column to correctly represent the new content
  23. _, err = tx.Exec("ALTER TABLE puppet RENAME COLUMN jid TO username")
  24. if err != nil {
  25. return err
  26. }
  27. if ctx.dialect == SQLite {
  28. // Message content was removed from the main message table earlier, but the backup table still exists for SQLite
  29. _, err = tx.Exec("DROP TABLE IF EXISTS old_message")
  30. _, err = tx.Exec(`ALTER TABLE "user" RENAME TO old_user`)
  31. if err != nil {
  32. return err
  33. }
  34. _, err = tx.Exec(`CREATE TABLE "user" (
  35. mxid TEXT PRIMARY KEY,
  36. username TEXT UNIQUE,
  37. agent SMALLINT,
  38. device SMALLINT,
  39. management_room TEXT
  40. )`)
  41. if err != nil {
  42. return err
  43. }
  44. // No need to copy auth data, users need to relogin anyway
  45. _, err = tx.Exec(`INSERT INTO "user" (mxid, management_room) SELECT mxid, management_room FROM old_user`)
  46. if err != nil {
  47. return err
  48. }
  49. _, err = tx.Exec("DROP TABLE old_user")
  50. if err != nil {
  51. return err
  52. }
  53. } else {
  54. // The jid column never actually contained the full JID, so let's rename it.
  55. _, err = tx.Exec(`ALTER TABLE "user" RENAME COLUMN jid TO username`)
  56. if err != nil {
  57. return err
  58. }
  59. // The auth data is now in the whatsmeow_device table.
  60. for _, column := range []string{"last_connection", "client_id", "client_token", "server_token", "enc_key", "mac_key"} {
  61. _, err = tx.Exec(`ALTER TABLE "user" DROP COLUMN ` + column)
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. // The whatsmeow_device table is keyed by the full JID, so we need to store the other parts of the JID here too.
  67. _, err = tx.Exec(`ALTER TABLE "user" ADD COLUMN agent SMALLINT`)
  68. if err != nil {
  69. return err
  70. }
  71. _, err = tx.Exec(`ALTER TABLE "user" ADD COLUMN device SMALLINT`)
  72. if err != nil {
  73. return err
  74. }
  75. // Clear all usernames, the users need to relogin anyway.
  76. _, err = tx.Exec(`UPDATE "user" SET username=null`)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }}
  83. }