upgrade.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(
  22. """CREATE TABLE portal (
  23. thread_id TEXT,
  24. receiver BIGINT,
  25. other_user_pk BIGINT,
  26. mxid TEXT,
  27. name TEXT,
  28. encrypted BOOLEAN NOT NULL DEFAULT false,
  29. PRIMARY KEY (thread_id, receiver)
  30. )"""
  31. )
  32. await conn.execute(
  33. """CREATE TABLE "user" (
  34. mxid TEXT PRIMARY KEY,
  35. igpk BIGINT,
  36. state jsonb,
  37. notice_room TEXT
  38. )"""
  39. )
  40. await conn.execute(
  41. """CREATE TABLE puppet (
  42. pk BIGINT PRIMARY KEY,
  43. name TEXT,
  44. username TEXT,
  45. photo_id TEXT,
  46. photo_mxc TEXT,
  47. name_set BOOLEAN NOT NULL DEFAULT false,
  48. avatar_set BOOLEAN NOT NULL DEFAULT false,
  49. is_registered BOOLEAN NOT NULL DEFAULT false,
  50. custom_mxid TEXT,
  51. access_token TEXT,
  52. next_batch TEXT,
  53. base_url TEXT
  54. )"""
  55. )
  56. await conn.execute(
  57. """CREATE TABLE user_portal (
  58. "user" BIGINT,
  59. portal TEXT,
  60. portal_receiver BIGINT,
  61. in_community BOOLEAN NOT NULL DEFAULT false,
  62. FOREIGN KEY (portal, portal_receiver) REFERENCES portal(thread_id, receiver)
  63. ON UPDATE CASCADE ON DELETE CASCADE
  64. )"""
  65. )
  66. await conn.execute(
  67. """CREATE TABLE message (
  68. mxid TEXT NOT NULL,
  69. mx_room TEXT NOT NULL,
  70. item_id TEXT,
  71. receiver BIGINT,
  72. sender BIGINT NOT NULL,
  73. PRIMARY KEY (item_id, receiver),
  74. UNIQUE (mxid, mx_room)
  75. )"""
  76. )
  77. await conn.execute(
  78. """CREATE TABLE reaction (
  79. mxid TEXT NOT NULL,
  80. mx_room TEXT NOT NULL,
  81. ig_item_id TEXT,
  82. ig_receiver BIGINT,
  83. ig_sender BIGINT,
  84. reaction TEXT NOT NULL,
  85. PRIMARY KEY (ig_item_id, ig_receiver, ig_sender),
  86. FOREIGN KEY (ig_item_id, ig_receiver) REFERENCES message(item_id, receiver)
  87. ON DELETE CASCADE ON UPDATE CASCADE,
  88. UNIQUE (mxid, mx_room)
  89. )"""
  90. )
  91. @upgrade_table.register(description="Add name_set and avatar_set to portal table")
  92. async def upgrade_v2(conn: Connection) -> None:
  93. await conn.execute("ALTER TABLE portal ADD COLUMN avatar_url TEXT")
  94. await conn.execute("ALTER TABLE portal ADD COLUMN name_set BOOLEAN NOT NULL DEFAULT false")
  95. await conn.execute("ALTER TABLE portal ADD COLUMN avatar_set BOOLEAN NOT NULL DEFAULT false")
  96. await conn.execute("UPDATE portal SET name_set=true WHERE name<>''")
  97. @upgrade_table.register(description="Add relay user field to portal table")
  98. async def upgrade_v3(conn: Connection) -> None:
  99. await conn.execute("ALTER TABLE portal ADD COLUMN relay_user_id TEXT")