upgrade.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 VARCHAR(255) PRIMARY KEY,
  32. igpk BIGINT,
  33. state jsonb,
  34. notice_room VARCHAR(255)
  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. is_registered BOOLEAN NOT NULL DEFAULT false,
  43. custom_mxid TEXT,
  44. access_token TEXT,
  45. next_batch TEXT,
  46. base_url TEXT
  47. )""")
  48. await conn.execute("""CREATE TABLE user_portal (
  49. "user" BIGINT,
  50. portal TEXT,
  51. portal_receiver BIGINT,
  52. in_community BOOLEAN NOT NULL DEFAULT false,
  53. FOREIGN KEY (portal, portal_receiver) REFERENCES portal(thread_id, receiver)
  54. ON UPDATE CASCADE ON DELETE CASCADE
  55. )""")
  56. await conn.execute("""CREATE TABLE message (
  57. mxid TEXT NOT NULL,
  58. mx_room TEXT NOT NULL,
  59. item_id TEXT,
  60. receiver BIGINT,
  61. PRIMARY KEY (item_id, receiver),
  62. UNIQUE (mxid, mx_room)
  63. )""")
  64. await conn.execute("""CREATE TABLE reaction (
  65. mxid TEXT NOT NULL,
  66. mx_room TEXT NOT NULL,
  67. ig_item_id TEXT,
  68. ig_receiver BIGINT,
  69. ig_sender BIGINT,
  70. reaction TEXT NOT NULL,
  71. PRIMARY KEY (ig_item_id, ig_receiver, ig_sender),
  72. FOREIGN KEY (ig_item_id, ig_receiver) REFERENCES message(item_id, receiver)
  73. ON DELETE CASCADE ON UPDATE CASCADE,
  74. UNIQUE (mxid, mx_room)
  75. )""")