matrix-appservice-kakaotalk/matrix_appservice_kakaotalk/db/upgrade/v01_initial_revision.py

88 lines
3.0 KiB
Python
Raw Permalink Normal View History

2022-02-25 02:22:50 -05:00
# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
from mautrix.util.async_db import Connection
from . import upgrade_table
@upgrade_table.register(description="Initial revision", transaction=False)
async def upgrade_v1(conn: Connection) -> None:
async with conn.transaction():
await create_v1_tables(conn)
async def create_v1_tables(conn: Connection) -> None:
await conn.execute(
"""CREATE TABLE "user" (
mxid TEXT PRIMARY KEY,
ktid BIGINT UNIQUE,
2022-02-25 02:22:50 -05:00
uuid TEXT,
access_token TEXT,
refresh_token TEXT,
notice_room TEXT
)"""
)
await conn.execute(
"""CREATE TABLE portal (
ktid BIGINT,
kt_receiver BIGINT,
2022-03-26 03:37:53 -04:00
kt_type TEXT NOT NULL,
2022-02-25 02:22:50 -05:00
mxid TEXT UNIQUE,
name TEXT,
photo_id TEXT,
avatar_url TEXT,
encrypted BOOLEAN NOT NULL DEFAULT false,
name_set BOOLEAN NOT NULL DEFAULT false,
avatar_set BOOLEAN NOT NULL DEFAULT false,
relay_user_id TEXT,
PRIMARY KEY (ktid, kt_receiver)
)"""
)
await conn.execute(
"""CREATE TABLE puppet (
ktid BIGINT PRIMARY KEY,
2022-02-25 02:22:50 -05:00
name 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 message (
2022-03-26 03:37:53 -04:00
mxid TEXT NOT NULL,
mx_room TEXT NOT NULL,
ktid BIGINT,
2022-03-26 03:37:53 -04:00
kt_receiver BIGINT NOT NULL,
"index" SMALLINT NOT NULL,
kt_chat BIGINT NOT NULL,
timestamp BIGINT NOT NULL,
2022-02-25 02:22:50 -05:00
PRIMARY KEY (ktid, kt_receiver, "index"),
FOREIGN KEY (kt_chat, kt_receiver) REFERENCES portal(ktid, kt_receiver)
ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE (mxid, mx_room)
)"""
)