# SQLite Setup Guide

This guide explains how to configure the application to use SQLite instead of PostgreSQL for development.

## Important Notes

⚠️ **The current authentication system requires PostgreSQL**. If you need to use SQLite, you'll need to disable auth features or use PostgreSQL for production.

## Option 1: SQLite with Disabled Auth Features

### Step 1: Modify Cargo.toml Features

Edit `server/Cargo.toml` to disable auth features:

```toml
[features]
default = ["content-db", "crypto", "email", "metrics", "examples"]
# Remove "auth" from the default features
```

### Step 2: Update Database Configuration

Create or modify `config.dev.toml`:

```toml
[database]
url = "sqlite:data/development.db"
max_connections = 1
min_connections = 1
connect_timeout = 30
idle_timeout = 600
max_lifetime = 1800
```

### Step 3: Create Database Directory

```bash
mkdir -p data
```

### Step 4: Run Without Auth Features

```bash
cargo run --bin server --no-default-features --features "content-db,crypto,email,metrics"
```

## Option 2: PostgreSQL for Development (Recommended)

### Using Docker

```bash
# Start PostgreSQL in Docker
docker run -d \
  --name rustelo-postgres \
  -e POSTGRES_DB=rustelo_dev \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=password \
  -p 5432:5432 \
  postgres:15

# Wait for PostgreSQL to start
sleep 5

# Test connection
psql -h localhost -U postgres -d rustelo_dev -c "SELECT 1;"
```

### Using Homebrew (macOS)

```bash
# Install PostgreSQL
brew install postgresql@15

# Start PostgreSQL service
brew services start postgresql@15

# Create database
createdb rustelo_dev

# Test connection
psql rustelo_dev -c "SELECT 1;"
```

### Using Package Manager (Linux)

```bash
# Ubuntu/Debian
sudo apt update
sudo apt install postgresql postgresql-contrib

# CentOS/RHEL
sudo yum install postgresql-server postgresql-contrib

# Start service
sudo systemctl start postgresql
sudo systemctl enable postgresql

# Create database
sudo -u postgres createdb rustelo_dev

# Test connection
sudo -u postgres psql rustelo_dev -c "SELECT 1;"
```

## Option 3: Cloud PostgreSQL

### Supabase (Free Tier)

1. Go to [supabase.com](https://supabase.com)
2. Create a new project
3. Copy the connection string
4. Update `config.dev.toml`:

```toml
[database]
url = "postgresql://postgres.xxx:[PASSWORD]@xxx.supabase.co:5432/postgres"
```

### Railway (Free Tier)

1. Go to [railway.app](https://railway.app)
2. Create a new PostgreSQL database
3. Copy the connection string
4. Update your configuration

## Testing SQLite Configuration

If you want to test SQLite-only features:

```bash
# Create a test configuration
cat > config.sqlite.toml << 'EOF'
root_path = "."

[server]
protocol = "http"
host = "127.0.0.1"
port = 3030
environment = "development"
log_level = "debug"

[database]
url = "sqlite:data/test.db"
max_connections = 1
min_connections = 1
connect_timeout = 30
idle_timeout = 600
max_lifetime = 1800

[app]
name = "My Rust App"
version = "0.1.0"
debug = true
enable_metrics = false
enable_health_check = true
enable_compression = true
max_request_size = 10485760

[logging]
format = "pretty"
level = "debug"
file_path = "logs/app.log"
max_file_size = 10485760
max_files = 5
enable_console = true
enable_file = false
EOF

# Test with SQLite config
CONFIG_FILE=config.sqlite.toml cargo run --bin server --no-default-features --features "content-db"
```

## Migration Notes

### From PostgreSQL to SQLite

1. Export your PostgreSQL data
2. Convert schema to SQLite-compatible format
3. Update connection strings
4. Test thoroughly

### From SQLite to PostgreSQL

1. Use tools like `sqlite3` to dump data
2. Convert to PostgreSQL format
3. Update connection strings
4. Run migrations

## Troubleshooting

### Common Issues

1. **"Failed to connect to database"**
   - Check if PostgreSQL is running
   - Verify connection string
   - Check firewall settings

2. **"Auth features require PostgreSQL"**
   - Use PostgreSQL or disable auth features
   - See Option 1 above

3. **"Directory not found"** (SQLite)
   - Create the data directory: `mkdir -p data`
   - Check file permissions

### Debug Commands

```bash
# Check PostgreSQL status
pg_isready -h localhost -p 5432

# Test SQLite file creation
sqlite3 data/test.db "CREATE TABLE test (id INTEGER); DROP TABLE test;"

# Check configuration loading
RUST_LOG=debug cargo run --bin test_config
```

## Performance Considerations

- **SQLite**: Best for development, single-user apps
- **PostgreSQL**: Better for production, multi-user apps
- **Connection pooling**: PostgreSQL handles concurrent connections better

## Security Notes

- Change default passwords in production
- Use environment variables for sensitive data
- Enable SSL/TLS for production databases
- Regular backups are essential

For more help, see the main README.md or create an issue in the repository.