upgrade.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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="Latest revision", upgrades_to=8)
  20. async def upgrade_latest(conn: Connection) -> None:
  21. await conn.execute(
  22. """CREATE TABLE portal (
  23. thread_id TEXT,
  24. receiver BIGINT,
  25. other_user_pk BIGINT,
  26. mxid TEXT,
  27. name TEXT,
  28. avatar_url TEXT,
  29. name_set BOOLEAN NOT NULL DEFAULT false,
  30. avatar_set BOOLEAN NOT NULL DEFAULT false,
  31. encrypted BOOLEAN NOT NULL DEFAULT false,
  32. relay_user_id TEXT,
  33. PRIMARY KEY (thread_id, receiver)
  34. )"""
  35. )
  36. await conn.execute(
  37. """CREATE TABLE "user" (
  38. mxid TEXT PRIMARY KEY,
  39. igpk BIGINT,
  40. state jsonb,
  41. seq_id BIGINT,
  42. snapshot_at_ms BIGINT,
  43. notice_room TEXT
  44. )"""
  45. )
  46. await conn.execute(
  47. """CREATE TABLE puppet (
  48. pk BIGINT PRIMARY KEY,
  49. name TEXT,
  50. username TEXT,
  51. photo_id TEXT,
  52. photo_mxc TEXT,
  53. name_set BOOLEAN NOT NULL DEFAULT false,
  54. avatar_set BOOLEAN NOT NULL DEFAULT false,
  55. is_registered BOOLEAN NOT NULL DEFAULT false,
  56. custom_mxid TEXT,
  57. access_token TEXT,
  58. next_batch TEXT,
  59. base_url TEXT
  60. )"""
  61. )
  62. await conn.execute(
  63. """CREATE TABLE user_portal (
  64. "user" BIGINT,
  65. portal TEXT,
  66. portal_receiver BIGINT,
  67. in_community BOOLEAN NOT NULL DEFAULT false,
  68. FOREIGN KEY (portal, portal_receiver) REFERENCES portal(thread_id, receiver)
  69. ON UPDATE CASCADE ON DELETE CASCADE
  70. )"""
  71. )
  72. await conn.execute(
  73. """CREATE TABLE message (
  74. mxid TEXT,
  75. mx_room TEXT NOT NULL,
  76. item_id TEXT,
  77. receiver BIGINT,
  78. sender BIGINT NOT NULL,
  79. client_context TEXT,
  80. ig_timestamp BIGINT,
  81. PRIMARY KEY (item_id, receiver),
  82. UNIQUE (mxid, mx_room)
  83. )"""
  84. )
  85. await conn.execute(
  86. """CREATE TABLE reaction (
  87. mxid TEXT NOT NULL,
  88. mx_room TEXT NOT NULL,
  89. ig_item_id TEXT,
  90. ig_receiver BIGINT,
  91. ig_sender BIGINT,
  92. reaction TEXT NOT NULL,
  93. mx_timestamp BIGINT,
  94. PRIMARY KEY (ig_item_id, ig_receiver, ig_sender),
  95. FOREIGN KEY (ig_item_id, ig_receiver) REFERENCES message(item_id, receiver)
  96. ON DELETE CASCADE ON UPDATE CASCADE,
  97. UNIQUE (mxid, mx_room)
  98. )"""
  99. )
  100. @upgrade_table.register(description="Add name_set and avatar_set to portal table")
  101. async def upgrade_v2(conn: Connection) -> None:
  102. await conn.execute("ALTER TABLE portal ADD COLUMN avatar_url TEXT")
  103. await conn.execute("ALTER TABLE portal ADD COLUMN name_set BOOLEAN NOT NULL DEFAULT false")
  104. await conn.execute("ALTER TABLE portal ADD COLUMN avatar_set BOOLEAN NOT NULL DEFAULT false")
  105. await conn.execute("UPDATE portal SET name_set=true WHERE name<>''")
  106. @upgrade_table.register(description="Add relay user field to portal table")
  107. async def upgrade_v3(conn: Connection) -> None:
  108. await conn.execute("ALTER TABLE portal ADD COLUMN relay_user_id TEXT")
  109. @upgrade_table.register(description="Add client context field to message table")
  110. async def upgrade_v4(conn: Connection) -> None:
  111. await conn.execute("ALTER TABLE message ADD COLUMN client_context TEXT")
  112. @upgrade_table.register(description="Add ig_timestamp field to message table")
  113. async def upgrade_v5(conn: Connection) -> None:
  114. await conn.execute("ALTER TABLE message ADD COLUMN ig_timestamp BIGINT")
  115. @upgrade_table.register(description="Allow hidden events in message table")
  116. async def upgrade_v6(conn: Connection) -> None:
  117. await conn.execute("ALTER TABLE message ALTER COLUMN mxid DROP NOT NULL")
  118. @upgrade_table.register(description="Store reaction timestamps")
  119. async def upgrade_v7(conn: Connection) -> None:
  120. await conn.execute("ALTER TABLE reaction ADD COLUMN mx_timestamp BIGINT")
  121. @upgrade_table.register(description="Store sync sequence ID in user table")
  122. async def upgrade_v8(conn: Connection) -> None:
  123. await conn.execute('ALTER TABLE "user" ADD COLUMN seq_id BIGINT')
  124. await conn.execute('ALTER TABLE "user" ADD COLUMN snapshot_at_ms BIGINT')