gmessages/database/upgrades/00-latest-revision.sql

82 lines
2.6 KiB
MySQL
Raw Normal View History

-- v0 -> v2: Latest revision
CREATE TABLE "user" (
-- only: postgres
rowid BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
-- only: sqlite
rowid INTEGER PRIMARY KEY,
mxid TEXT NOT NULL UNIQUE,
phone_id TEXT UNIQUE,
session jsonb,
self_participant_ids jsonb NOT NULL DEFAULT '[]',
management_room TEXT,
space_room TEXT,
access_token TEXT
);
CREATE TABLE puppet (
id TEXT NOT NULL,
receiver BIGINT NOT NULL,
phone TEXT NOT NULL,
name TEXT NOT NULL,
name_set BOOLEAN NOT NULL DEFAULT false,
avatar_id TEXT NOT NULL,
avatar_mxc TEXT NOT NULL,
avatar_set BOOLEAN NOT NULL DEFAULT false,
contact_info_set BOOLEAN NOT NULL DEFAULT false,
2023-07-11 22:57:07 +00:00
PRIMARY KEY (id, receiver),
CONSTRAINT puppet_user_fkey FOREIGN KEY (receiver) REFERENCES "user"(rowid) ON DELETE CASCADE,
CONSTRAINT puppet_phone_unique UNIQUE (phone, receiver)
);
CREATE TABLE portal (
id TEXT NOT NULL,
receiver BIGINT NOT NULL,
self_user TEXT,
other_user TEXT,
mxid TEXT UNIQUE,
name TEXT NOT NULL,
name_set BOOLEAN NOT NULL DEFAULT false,
avatar_id TEXT NOT NULL,
avatar_mxc TEXT NOT NULL,
avatar_set BOOLEAN NOT NULL DEFAULT false,
encrypted BOOLEAN NOT NULL DEFAULT false,
in_space BOOLEAN NOT NULL DEFAULT false,
2023-07-11 22:57:07 +00:00
PRIMARY KEY (id, receiver),
CONSTRAINT portal_user_fkey FOREIGN KEY (receiver) REFERENCES "user"(rowid) ON DELETE CASCADE,
CONSTRAINT portal_puppet_fkey FOREIGN KEY (other_user, receiver) REFERENCES puppet(id, receiver) ON DELETE CASCADE
);
CREATE TABLE message (
conv_id TEXT NOT NULL,
conv_receiver BIGINT NOT NULL,
id TEXT NOT NULL,
mxid TEXT NOT NULL,
sender TEXT NOT NULL,
timestamp BIGINT NOT NULL,
2023-07-11 22:57:07 +00:00
status jsonb NOT NULL,
PRIMARY KEY (conv_id, conv_receiver, id),
CONSTRAINT message_portal_fkey FOREIGN KEY (conv_id, conv_receiver) REFERENCES portal(id, receiver) ON DELETE CASCADE,
CONSTRAINT message_mxid_unique UNIQUE (mxid)
);
CREATE TABLE reaction (
conv_id TEXT NOT NULL,
conv_receiver BIGINT NOT NULL,
msg_id TEXT NOT NULL,
sender TEXT NOT NULL,
reaction TEXT NOT NULL,
mxid TEXT NOT NULL,
PRIMARY KEY (conv_id, conv_receiver, msg_id, sender),
CONSTRAINT reaction_message_fkey FOREIGN KEY (conv_id, conv_receiver, msg_id) REFERENCES message(conv_id, conv_receiver, id) ON DELETE CASCADE,
CONSTRAINT reaction_mxid_unique UNIQUE (mxid)
)