mirror of
https://mau.dev/maunium/synapse.git
synced 2024-12-15 01:53:58 +01:00
Add sqlite schema
This commit is contained in:
parent
ca53ad7425
commit
1a40afa756
1 changed files with 64 additions and 5 deletions
|
@ -15,7 +15,9 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from synapse.storage import get_statements
|
from synapse.storage import get_statements
|
||||||
from synapse.storage.engines import PostgresEngine
|
from synapse.storage.engines import PostgresEngine, Sqlite3Engine
|
||||||
|
|
||||||
|
import ujson
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -46,13 +48,70 @@ INSERT INTO event_search SELECT
|
||||||
|
|
||||||
CREATE INDEX event_search_fts_idx ON event_search USING gin(vector);
|
CREATE INDEX event_search_fts_idx ON event_search USING gin(vector);
|
||||||
CREATE INDEX event_search_ev_idx ON event_search(event_id);
|
CREATE INDEX event_search_ev_idx ON event_search(event_id);
|
||||||
|
CREATE INDEX event_search_ev_ridx ON event_search(room_id);
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def run_upgrade(cur, database_engine, *args, **kwargs):
|
SQLITE_TABLE = (
|
||||||
if not isinstance(database_engine, PostgresEngine):
|
"CREATE VIRTUAL TABLE event_search USING fts3 ( event_id, room_id, key, value)"
|
||||||
# We only support FTS for postgres currently.
|
)
|
||||||
return
|
SQLITE_INDEX = "CREATE INDEX event_search_ev_idx ON event_search(event_id)"
|
||||||
|
|
||||||
|
|
||||||
|
def run_upgrade(cur, database_engine, *args, **kwargs):
|
||||||
|
if isinstance(database_engine, PostgresEngine):
|
||||||
for statement in get_statements(POSTGRES_SQL.splitlines()):
|
for statement in get_statements(POSTGRES_SQL.splitlines()):
|
||||||
cur.execute(statement)
|
cur.execute(statement)
|
||||||
|
return
|
||||||
|
|
||||||
|
if isinstance(database_engine, Sqlite3Engine):
|
||||||
|
cur.execute(SQLITE_TABLE)
|
||||||
|
|
||||||
|
rowid = -1
|
||||||
|
while True:
|
||||||
|
cur.execute(
|
||||||
|
"SELECT rowid, json FROM event_json"
|
||||||
|
" WHERE rowid > ?"
|
||||||
|
" ORDER BY rowid ASC LIMIT 100",
|
||||||
|
(rowid,)
|
||||||
|
)
|
||||||
|
|
||||||
|
res = cur.fetchall()
|
||||||
|
|
||||||
|
if not res:
|
||||||
|
break
|
||||||
|
|
||||||
|
events = [
|
||||||
|
ujson.loads(js)
|
||||||
|
for _, js in res
|
||||||
|
]
|
||||||
|
|
||||||
|
rowid = max(rid for rid, _ in res)
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
for ev in events:
|
||||||
|
if ev["type"] == "m.room.message":
|
||||||
|
rows.append((
|
||||||
|
ev["event_id"], ev["room_id"], "content.body",
|
||||||
|
ev["content"]["body"]
|
||||||
|
))
|
||||||
|
if ev["type"] == "m.room.name":
|
||||||
|
rows.append((
|
||||||
|
ev["event_id"], ev["room_id"], "content.name",
|
||||||
|
ev["content"]["name"]
|
||||||
|
))
|
||||||
|
if ev["type"] == "m.room.topic":
|
||||||
|
rows.append((
|
||||||
|
ev["event_id"], ev["room_id"], "content.topic",
|
||||||
|
ev["content"]["topic"]
|
||||||
|
))
|
||||||
|
|
||||||
|
if rows:
|
||||||
|
logger.info(rows)
|
||||||
|
cur.executemany(
|
||||||
|
"INSERT INTO event_search (event_id, room_id, key, value)"
|
||||||
|
" VALUES (?,?,?,?)",
|
||||||
|
rows
|
||||||
|
)
|
||||||
|
|
||||||
|
# cur.execute(SQLITE_INDEX)
|
||||||
|
|
Loading…
Reference in a new issue