-- Create channels table
CREATE TABLE channels (
tenant_id UUID,
channel_id UUID DEFAULT gen_random_uuid(),
channel_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (tenant_id, channel_id),
FOREIGN KEY (tenant_id) REFERENCES tenants(id)
);
-- Create channel_users table to map users to channels within a tenant
CREATE TABLE channel_users (
tenant_id UUID,
channel_id UUID,
user_id UUID,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (tenant_id, channel_id, user_id),
FOREIGN KEY (tenant_id, channel_id) REFERENCES channels(tenant_id, channel_id),
FOREIGN KEY (tenant_id, user_id) REFERENCES tenant_users(tenant_id, user_id)
);
-- Create messages table with vector embedding for AI functionalities
CREATE TABLE messages (
tenant_id UUID,
message_id UUID DEFAULT gen_random_uuid(),
channel_id UUID,
user_id UUID,
content TEXT NOT NULL,
vector_embedding VECTOR(768), -- Adjust the dimensions as needed
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (tenant_id, message_id),
FOREIGN KEY (tenant_id, channel_id, user_id) REFERENCES channel_users(tenant_id, channel_id, user_id)
);
-- Create direct_messages table with vector embedding for AI functionalities
CREATE TABLE direct_messages (
tenant_id UUID,
dm_id UUID DEFAULT gen_random_uuid(),
sender_id UUID,
receiver_id UUID,
content TEXT NOT NULL,
vector_embedding VECTOR(768), -- Adjust the dimensions as needed
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (tenant_id, dm_id),
FOREIGN KEY (tenant_id, sender_id) REFERENCES tenant_users(tenant_id, user_id),
FOREIGN KEY (tenant_id, receiver_id) REFERENCES tenant_users(tenant_id, user_id)
);