Kaynağa Gözat

Mark SQLite as supported

Closes #48
Tulir Asokan 2 yıl önce
ebeveyn
işleme
4505df3403

+ 64 - 54
mautrix_instagram/db/upgrade.py

@@ -20,77 +20,87 @@ from mautrix.util.async_db import UpgradeTable
 upgrade_table = UpgradeTable()
 
 
-@upgrade_table.register(description="Initial revision")
-async def upgrade_v1(conn: Connection) -> None:
+@upgrade_table.register(description="Latest revision", upgrades_to=8)
+async def upgrade_latest(conn: Connection) -> None:
     await conn.execute(
         """CREATE TABLE portal (
-        thread_id     TEXT,
-        receiver      BIGINT,
-        other_user_pk BIGINT,
-        mxid          TEXT,
-        name          TEXT,
-        encrypted     BOOLEAN NOT NULL DEFAULT false,
-        PRIMARY KEY (thread_id, receiver)
-    )"""
+            thread_id     TEXT,
+            receiver      BIGINT,
+            other_user_pk BIGINT,
+            mxid          TEXT,
+            name          TEXT,
+            avatar_url    TEXT,
+            name_set      BOOLEAN NOT NULL DEFAULT false,
+            avatar_set    BOOLEAN NOT NULL DEFAULT false,
+            encrypted     BOOLEAN NOT NULL DEFAULT false,
+            relay_user_id TEXT,
+            PRIMARY KEY (thread_id, receiver)
+        )"""
     )
     await conn.execute(
         """CREATE TABLE "user" (
-        mxid        TEXT PRIMARY KEY,
-        igpk        BIGINT,
-        state       jsonb,
-        notice_room TEXT
-    )"""
+            mxid           TEXT PRIMARY KEY,
+            igpk           BIGINT,
+            state          jsonb,
+            seq_id         BIGINT,
+            snapshot_at_ms BIGINT,
+            notice_room    TEXT
+        )"""
     )
     await conn.execute(
         """CREATE TABLE puppet (
-        pk            BIGINT PRIMARY KEY,
-        name          TEXT,
-        username      TEXT,
-        photo_id      TEXT,
-        photo_mxc     TEXT,
-        name_set      BOOLEAN NOT NULL DEFAULT false,
-        avatar_set    BOOLEAN NOT NULL DEFAULT false,
-        is_registered BOOLEAN NOT NULL DEFAULT false,
-        custom_mxid   TEXT,
-        access_token  TEXT,
-        next_batch    TEXT,
-        base_url      TEXT
-    )"""
+            pk            BIGINT PRIMARY KEY,
+            name          TEXT,
+            username      TEXT,
+            photo_id      TEXT,
+            photo_mxc     TEXT,
+            name_set      BOOLEAN NOT NULL DEFAULT false,
+            avatar_set    BOOLEAN NOT NULL DEFAULT false,
+            is_registered BOOLEAN NOT NULL DEFAULT false,
+            custom_mxid   TEXT,
+            access_token  TEXT,
+            next_batch    TEXT,
+            base_url      TEXT
+        )"""
     )
     await conn.execute(
         """CREATE TABLE user_portal (
-        "user"          BIGINT,
-        portal          TEXT,
-        portal_receiver BIGINT,
-        in_community    BOOLEAN NOT NULL DEFAULT false,
-        FOREIGN KEY (portal, portal_receiver) REFERENCES portal(thread_id, receiver)
-            ON UPDATE CASCADE ON DELETE CASCADE
-    )"""
+            "user"          BIGINT,
+            portal          TEXT,
+            portal_receiver BIGINT,
+            in_community    BOOLEAN NOT NULL DEFAULT false,
+            FOREIGN KEY (portal, portal_receiver) REFERENCES portal(thread_id, receiver)
+                ON UPDATE CASCADE ON DELETE CASCADE
+        )"""
     )
     await conn.execute(
         """CREATE TABLE message (
-        mxid     TEXT NOT NULL,
-        mx_room  TEXT NOT NULL,
-        item_id  TEXT,
-        receiver BIGINT,
-        sender   BIGINT NOT NULL,
-        PRIMARY KEY (item_id, receiver),
-        UNIQUE (mxid, mx_room)
-    )"""
+            mxid     TEXT,
+            mx_room  TEXT NOT NULL,
+            item_id  TEXT,
+            receiver BIGINT,
+            sender   BIGINT NOT NULL,
+
+            client_context TEXT,
+            ig_timestamp   BIGINT,
+            PRIMARY KEY (item_id, receiver),
+            UNIQUE (mxid, mx_room)
+        )"""
     )
     await conn.execute(
         """CREATE TABLE reaction (
-        mxid        TEXT NOT NULL,
-        mx_room     TEXT NOT NULL,
-        ig_item_id  TEXT,
-        ig_receiver BIGINT,
-        ig_sender   BIGINT,
-        reaction    TEXT NOT NULL,
-        PRIMARY KEY (ig_item_id, ig_receiver, ig_sender),
-        FOREIGN KEY (ig_item_id, ig_receiver) REFERENCES message(item_id, receiver)
-            ON DELETE CASCADE ON UPDATE CASCADE,
-        UNIQUE (mxid, mx_room)
-    )"""
+            mxid         TEXT NOT NULL,
+            mx_room      TEXT NOT NULL,
+            ig_item_id   TEXT,
+            ig_receiver  BIGINT,
+            ig_sender    BIGINT,
+            reaction     TEXT NOT NULL,
+            mx_timestamp BIGINT,
+            PRIMARY KEY (ig_item_id, ig_receiver, ig_sender),
+            FOREIGN KEY (ig_item_id, ig_receiver) REFERENCES message(item_id, receiver)
+                ON DELETE CASCADE ON UPDATE CASCADE,
+            UNIQUE (mxid, mx_room)
+        )"""
     )
 
 

+ 9 - 3
mautrix_instagram/example-config.yaml

@@ -36,12 +36,18 @@ appservice:
     # Usually 1 is enough, but on high-traffic bridges you might need to increase this to avoid 413s
     max_body_size: 1
 
-    # The full URI to the database. Only Postgres is currently supported.
+    # The full URI to the database. SQLite and Postgres are supported.
+    # Format examples:
+    #   SQLite:   sqlite:///filename.db
+    #   Postgres: postgres://username:password@hostname/dbname
     database: postgres://username:password@hostname/db
-    # Additional arguments for asyncpg.create_pool()
+    # Additional arguments for asyncpg.create_pool() or sqlite3.connect()
     # https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.pool.create_pool
+    # https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
+    # For sqlite, min_size is used as the connection thread pool size and max_size is ignored.
+    # Additionally, SQLite supports init_commands as an array of SQL queries to run on connect (e.g. to set PRAGMAs).
     database_opts:
-        min_size: 5
+        min_size: 1
         max_size: 10
 
     # The unique ID of this appservice.

+ 2 - 1
mautrix_instagram/portal.py

@@ -22,6 +22,7 @@ import asyncio
 import json
 import mimetypes
 import re
+import sqlite3
 import time
 
 import asyncpg
@@ -568,7 +569,7 @@ class Portal(DBPortal, BasePortal):
                     sender=sender.igpk,
                     ig_timestamp=int(resp.payload.timestamp),
                 ).insert()
-            except asyncpg.UniqueViolationError as e:
+            except (asyncpg.UniqueViolationError, sqlite3.IntegrityError) as e:
                 self.log.warning(
                     f"Error while persisting {event_id} ({resp.payload.client_context}) "
                     f"-> {resp.payload.item_id}: {e}"

+ 3 - 0
optional-requirements.txt

@@ -10,3 +10,6 @@ prometheus_client>=0.6,<0.15
 
 #/imageconvert
 pillow>=4,<10
+
+#/sqlite
+aiosqlite>=0.16,<0.18