# Migration Summary: SQLite to PostgreSQL

## Problem Statement
The user reported:
1. Cannot setup the MCP with SQLite due to dependency errors
2. Wants to use PostgreSQL instead
3. Needs to pass connection parameters via npx: host, port, user, password

## Solution Implemented

### 1. Database Migration
- **Removed**: `better-sqlite3` dependency and all SQLite-specific code
- **Added**: `pg` (PostgreSQL client) with connection pooling
- **Converted**: All synchronous database operations to async/await
- **Replaced**: SQLite FTS5 with PostgreSQL full-text search using GIN indexes and `plainto_tsquery`

### 2. Connection Parameter Support
Users can now configure PostgreSQL connections via:

#### Command-Line Arguments
```bash
npx remix-mcp --host localhost --port 5432 --user myuser --password mypass --database remix_mcp
```

Or using a connection string:
```bash
npx remix-mcp --connection-string postgresql://user:password@host:port/database
```

#### Environment Variables
```bash
export PGHOST=localhost
export PGPORT=5432
export PGUSER=myuser
export PGPASSWORD=mypass
export PGDATABASE=remix_mcp
npx remix-mcp
```

#### Parameter Details
| Parameter | CLI Long | CLI Short | Environment | Default |
|-----------|----------|-----------|-------------|---------|
| Host | `--host` | - | `PGHOST` | `localhost` |
| Port | `--port` | `-p` | `PGPORT` | `5432` |
| User | `--user` | `-U` | `PGUSER` | `postgres` |
| Password | `--password` | `-W` | `PGPASSWORD` | (empty) |
| Database | `--database` | `-d` | `PGDATABASE` | `remix_mcp` |

### 3. Code Quality Improvements
- ✅ Port validation (1-65535 range)
- ✅ Explicit radix in `parseInt` calls
- ✅ Graceful shutdown handlers (SIGINT/SIGTERM)
- ✅ Better error messages
- ✅ Proper PostgreSQL text search using `plainto_tsquery`
- ✅ Connection pooling for better performance

### 4. Schema Changes
PostgreSQL schema features:
- All tables created with proper foreign key constraints
- GIN index on messages.payload for full-text search
- Standard B-tree indexes for relationships
- No triggers needed (unlike SQLite FTS)

### 5. Testing
All core functionality verified:
- ✅ Database connection with various parameter combinations
- ✅ Schema initialization
- ✅ Conversation creation with messages
- ✅ Conversation retrieval
- ✅ Parent-child conversation linking
- ✅ Conversation tree navigation
- ✅ Full-text search in parent conversations
- ✅ Full-text search in child conversations

## Files Changed
1. `package.json` - Dependencies updated
2. `src/database.ts` - Complete rewrite for PostgreSQL
3. `src/tools.ts` - Async conversion and PostgreSQL queries
4. `src/index.ts` - Parameter parsing and graceful shutdown
5. `README.md` - Updated documentation
6. `test/README.md` - Noted PostgreSQL migration
7. `.gitignore` - Added test script exclusion

## Breaking Changes
- **Version**: Bumped to 0.3.0 (major breaking change)
- **Database**: Requires PostgreSQL instead of SQLite
- **Migration**: Existing SQLite data must be manually migrated
- **Requirements**: Users need a running PostgreSQL instance

## Backwards Compatibility
None - this is a breaking change. Version 0.2.0 and earlier used SQLite and are incompatible with 0.3.0+.

## Future Work
- Update unit tests for PostgreSQL (currently disabled)
- Add database migration script from SQLite to PostgreSQL
- Consider Docker Compose setup for easy PostgreSQL deployment
- Add PostgreSQL performance tuning documentation
