iiitl / iiitl/chat-app

Migrate from prototype database schema to a more complete schema

Open
#3 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement hard
Dominant language
Go
Stars
0
Forks
3
PR merge metrics
No merged PRs in 30d

Description

Thus far, the project only used a dummy database with minimal schema for quick prototyping:
```sql
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255),
password TEXT
) ;

CREATE TABLE guilds (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT
) ;

CREATE TABLE guild_user (
member_id UUID REFERENCES users(id),
guild_id UUID REFERENCES guilds(id),
PRIMARY KEY (member_id, guild_id)
) ;
```

While this worked for testing, it hinders further development of this project. Some of the issues with the current schema are:
- It lacks data persistence.
- It lacks support for channels and guild-channel relations.
- It prevents writing sync tests to check what the database stores as compared to what the clients receive.

> Note: the system currently does not persist any data. Messages are only fanned out via Redis streams to active pods. A dedicated database consumer service has not been implemented yet.

Moving to a more complete and more robust database schema is thus required.

## Proposed schema

> Note: while this is the proposed schema, there is still room for improvement.

```sql
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255) NOT NULL UNIQUE,
password TEXT NOT NULL,
created_at TIMESTAMP NOT NULL
) ;

CREATE TABLE guilds (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL
) ;

CREATE TABLE guild_members (
member_id UUID REFERENCES users(id) NOT NULL,
guild_id UUID REFERENCES guilds(id) NOT NULL,
PRIMARY KEY (member_id, guild_id)
) ;

CREATE TABLE channels (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
parent_guild_id UUID REFERENCES guilds(id) NOT NULL,
created_at TIMESTAMP NOT NULL,
CONSTRAINT unique_channel_per_guild UNIQUE (parent_guild_id, name)
) ;

CREATE TABLE channel_messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
sender_id UUID REFERENCES users(id) NOT NULL,
channel_id UUID REFERENCES channels(id) NOT NULL,
text_content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL
) ;
```

> Note: the schema does not use default timestamp generations on the database level to keep microservices in sync. The producer generates a timestamp, which is passed to all future services. For more details about the architectural decisions of this project, please read `.plan.md`.

> Note: in the future, snowflake IDs might be considered instead of normal UUIDs for better database indexing. This is something to just keep in mind. Not to be implemented yet.

## Expectation
The existing schema should be replaced with the proposed schema across the codebase. All services interacting with the database should be updated accordingly, and the system should be verified to work end-to-end with the new schema.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read .plan.md first, then locate the existing schema and the services that interact with the database and Redis streams. Replace the prototype schema with the proposed users, guilds, members, channels, and messages tables, update all database integrations, and verify the system works end-to-end with persistence and sync coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, redis, sql
Domain
backend, databases, distributed-systems
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.