Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 19, 2025

This PR refactors the user identification system to eliminate data redundancy and improve data integrity by normalizing the database schema as requested in the issue.

Changes Made

Database Schema Normalization

  • Added users table with id (primary key), email (unique), name, and created_at columns
  • Modified messages table to replace redundant user_email and user_name columns with a single user_id foreign key
  • Generated migration file (0001_outgoing_venus.sql) to safely transform the existing schema

User Management Logic

  • Enhanced authentication flow in auth.ts to create or find users in the database during login
  • Session management now includes user ID for efficient database operations
  • Graceful error handling for OAuth edge cases (missing email)

Message System Updates

  • WebSocket message creation (ws.ts) now uses user_id instead of storing duplicate user data
  • Message retrieval API (messages.ts) efficiently joins with users table to return user information
  • Maintains API compatibility - existing frontend code continues to work unchanged

Data Integrity Improvements

  • Eliminated redundant user data storage in messages table
  • Established proper foreign key constraints between messages and users
  • Unique email constraint prevents duplicate user records
  • Referential integrity ensures message authors always exist

Before/After Comparison

Before:

-- Messages table with redundant user data
CREATE TABLE "messages" (
  "id" serial PRIMARY KEY,
  "content" text NOT NULL,
  "user_email" varchar(255) NOT NULL,  -- Redundant
  "user_name" varchar(255),            -- Redundant
  "created_at" timestamp DEFAULT now()
);

After:

-- Normalized schema with proper relationships
CREATE TABLE "users" (
  "id" serial PRIMARY KEY,
  "email" varchar(255) NOT NULL UNIQUE,
  "name" varchar(255),
  "created_at" timestamp DEFAULT now()
);

CREATE TABLE "messages" (
  "id" serial PRIMARY KEY,
  "content" text NOT NULL,
  "user_id" integer NOT NULL REFERENCES "users"("id"),
  "created_at" timestamp DEFAULT now()
);

Testing

  • ✅ All existing tests updated and passing (20/20)
  • ✅ Application builds successfully without errors
  • ✅ No breaking changes to public APIs
  • ✅ Backward compatibility maintained for existing functionality

This normalization improves data integrity, reduces storage redundancy, and makes user management more efficient while maintaining full application functionality.

Fixes #56.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Refactor user identification: Normalize user mapping and update messages table Refactor user identification: Normalize user mapping and update messages table Jul 19, 2025
Copilot AI requested a review from mahata July 19, 2025 23:52
Copilot finished work on behalf of mahata July 19, 2025 23:52
@mahata mahata marked this pull request as ready for review July 20, 2025 07:15
@mahata
Copy link
Owner

mahata commented Jul 20, 2025

@copilot Fix CI in GitHub Actions.

Copy link
Contributor Author

Copilot AI commented Jul 20, 2025

@copilot Fix CI in GitHub Actions.

Fixed CI issue - there was an invalid 'pnpm' suffix in the drizzle-orm dependency version in package.json which was causing installation failures. Updated package.json and lockfile. Commit: d7655b4

Copilot finished work on behalf of mahata July 20, 2025 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor user identification: Normalize user mapping and update messages table

2 participants