# Fix Log - January 27, 2026
## Summary of Wires Fixes (Feed & Friendship Connectivity)

This document details the critical fixes implemented to restore the application's core functionality (Feed and Friendship features), which were failing due to database schema mismatches and backend API errors.

### 1. Database Schema Synchronization

**Symptom:**  
Application logs showed persistent `ERROR: column f.status does not exist` and failures to start due to missing `is_private` / `is_official` columns, even after applying patches to the `postgres` database.

**Root Cause:**  
The Go Backend service (`sojorn-api`) is configured via `.env` to connect to a specific database named `sojorn`, **NOT** the default `postgres` database. 
- **Connection String found in .env:** `postgres://postgres:...@localhost:5432/sojorn`
- Our initial patches were applied to the `postgres` database, leaving the actual production DB (`sojorn`) unpatched.

**Resolution:**
- Identified the correct database from service logs.
- Executed SQL patches directly against the `sojorn` database via `psql`.

**Patches Applied:**
```sql
-- Added status column to follows table (required for Friendship/Feed logic)
ALTER TABLE public.follows ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'accepted';
ALTER TABLE public.follows DROP CONSTRAINT IF EXISTS follows_status_check;
ALTER TABLE public.follows ADD CONSTRAINT follows_status_check CHECK (status IN ('pending', 'accepted'));

-- Added privacy/official flags to profiles table (required for Feed visibility logic)
ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS is_private BOOLEAN DEFAULT FALSE;
ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS is_official BOOLEAN DEFAULT FALSE;
```

### 2. Backend API Fixes

**Symptom:**
- **Feed Error (500):** `can't scan into dest[7] (col: duration_ms): cannot scan NULL into *int`
- **Profile Posts Error (500):** `number of field descriptions must equal number of destinations, got 13 and 16`
- **Notifications Error (404):** `/api/v1/notifications` endpoint returning 404.

**Resolution:**

1.  **Feed Scanning Fix (`PostRepository.GetFeed`)**:
    - The `duration_ms` column in the database can be `NULL` for older posts or image posts.
    - The Go `Scan` method was failing when encountering these NULLs.
    - **Fix:** Updated SQL query to use `COALESCE(p.duration_ms, 0)` to ensure a valid integer is always returned.

2.  **Profile Posts Mismatch (`PostRepository.GetPostsByAuthor`)**:
    - The SQL `SELECT` statement was returning 13 columns (missing image/video/thumbnail URL coalescing and tags), but the Go `rows.Scan()` method was expecting 16 arguments.
    - **Fix:** Updated the SQL query to include all necessary columns (`COALESCE(p.image_url, '')`, etc.) matching the Scan destinations exactly.

3.  **Route Registration (`cmd/api/main.go`)**:
    - The notifications endpoint was missing from the router.
    - **Fix:** Added `apiV1.GET("/notifications", notificationHandler.GetNotifications)` to the authenticated route group.

### 3. Verification

- **Feed:** Now loads successfully with correct scanning of all post types.
- **Friendship:** Follow/Unfollow status is correctly tracked via the `status` column in the DB.
- **Profiles:** User profiles load without 500 errors.

### Troubleshooting Guide for Future

If "column does not exist" errors reappear:
1.  **Check the target DB:** Verify which database the app is actually using by checking `/opt/sojorn/.env`.
2.  **Verify Schema:** Log into that SPECIFIC database (`psql -d sojorn`) and run `\d table_name` to see if the column exists there.
3.  **Logs:** Use `journalctl -u sojorn-api -n 50` to pinpoint the exact SQL error.
