upgrade.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from asyncpg import Connection
  17. from mautrix.util.async_db import UpgradeTable
  18. upgrade_table = UpgradeTable()
  19. @upgrade_table.register(description="Initial revision")
  20. async def upgrade_v1(conn: Connection) -> None:
  21. await conn.execute("""CREATE TABLE portal (
  22. thread_id TEXT,
  23. receiver BIGINT,
  24. other_user_pk BIGINT,
  25. mxid TEXT,
  26. name TEXT,
  27. encrypted BOOLEAN NOT NULL DEFAULT false,
  28. PRIMARY KEY (thread_id, receiver)
  29. )""")
  30. await conn.execute("""CREATE TABLE "user" (
  31. mxid TEXT PRIMARY KEY,
  32. igpk BIGINT,
  33. state jsonb,
  34. notice_room TEXT
  35. )""")
  36. await conn.execute("""CREATE TABLE puppet (
  37. pk BIGINT PRIMARY KEY,
  38. name TEXT,
  39. username TEXT,
  40. photo_id TEXT,
  41. photo_mxc TEXT,
  42. name_set BOOLEAN NOT NULL DEFAULT false,
  43. avatar_set BOOLEAN NOT NULL DEFAULT false,
  44. is_registered BOOLEAN NOT NULL DEFAULT false,
  45. custom_mxid TEXT,
  46. access_token TEXT,
  47. next_batch TEXT,
  48. base_url TEXT
  49. )""")
  50. await conn.execute("""CREATE TABLE user_portal (
  51. "user" BIGINT,
  52. portal TEXT,
  53. portal_receiver BIGINT,
  54. in_community BOOLEAN NOT NULL DEFAULT false,
  55. FOREIGN KEY (portal, portal_receiver) REFERENCES portal(thread_id, receiver)
  56. ON UPDATE CASCADE ON DELETE CASCADE
  57. )""")
  58. await conn.execute("""CREATE TABLE message (
  59. mxid TEXT NOT NULL,
  60. mx_room TEXT NOT NULL,
  61. item_id TEXT,
  62. receiver BIGINT,
  63. sender BIGINT NOT NULL,
  64. PRIMARY KEY (item_id, receiver),
  65. UNIQUE (mxid, mx_room)
  66. )""")
  67. await conn.execute("""CREATE TABLE reaction (
  68. mxid TEXT NOT NULL,
  69. mx_room TEXT NOT NULL,
  70. ig_item_id TEXT,
  71. ig_receiver BIGINT,
  72. ig_sender BIGINT,
  73. reaction TEXT NOT NULL,
  74. PRIMARY KEY (ig_item_id, ig_receiver, ig_sender),
  75. FOREIGN KEY (ig_item_id, ig_receiver) REFERENCES message(item_id, receiver)
  76. ON DELETE CASCADE ON UPDATE CASCADE,
  77. UNIQUE (mxid, mx_room)
  78. )""")
  79. @upgrade_table.register(description="Add name_set and avatar_set to portal table")
  80. async def upgrade_v2(conn: Connection) -> None:
  81. await conn.execute("ALTER TABLE portal ADD COLUMN avatar_url TEXT")
  82. await conn.execute("ALTER TABLE portal ADD COLUMN name_set BOOLEAN NOT NULL DEFAULT false")
  83. await conn.execute("ALTER TABLE portal ADD COLUMN avatar_set BOOLEAN NOT NULL DEFAULT false")
  84. await conn.execute("UPDATE portal SET name_set=true WHERE name<>''")