Spaces:
Runtime error
Runtime error
File size: 3,868 Bytes
330b6e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | # Database Migrations
This directory contains database migration scripts for the chat agent application.
## Overview
The chat agent uses PostgreSQL as the primary database with the following tables:
- `chat_sessions`: Stores user chat sessions with language context and activity tracking
- `messages`: Stores individual chat messages with role, content, and language information
- `language_contexts`: Stores session-specific language settings and prompt templates
- `supported_languages`: Reference table for supported programming languages
## Migration Files
- `001_initial_schema.sql`: Initial database schema with all core tables
- `migrate.py`: Python migration runner script
## Usage
### Using the Python Migration Script
```bash
# Run all pending migrations
python migrations/migrate.py migrate
# Check migration status
python migrations/migrate.py status
# Run migrations for specific environment
python migrations/migrate.py migrate --config production
# Run migrations with custom database URL
python migrations/migrate.py migrate --database-url postgresql://user:pass@localhost/mydb
```
### Using the Database Management CLI
```bash
# Initialize database (create tables)
python manage_db.py init
# Reset database (drop and recreate all tables)
python manage_db.py reset
# Get database information
python manage_db.py info
# Get database statistics
python manage_db.py stats
# Create sample data for testing
python manage_db.py sample
# Clean up old sessions
python manage_db.py cleanup
# Check database connection
python manage_db.py check
```
### Manual SQL Execution
You can also run the SQL migration files directly:
```bash
# Connect to PostgreSQL and run the migration
psql -d your_database -f migrations/001_initial_schema.sql
```
## Database Schema
### chat_sessions
- `id`: UUID primary key
- `user_id`: UUID of the session owner
- `language`: Current programming language (default: python)
- `last_active`: Timestamp of last activity
- `message_count`: Total messages in session
- `is_active`: Whether session is active
- `metadata`: Additional session data (JSON)
- `created_at`, `updated_at`: Timestamps
### messages
- `id`: UUID primary key
- `session_id`: Reference to chat_sessions
- `role`: 'user' or 'assistant'
- `content`: Message text content
- `language`: Programming language context
- `timestamp`: When message was created
- `metadata`: Additional message data (JSON)
- `created_at`, `updated_at`: Timestamps
### language_contexts
- `id`: UUID primary key
- `session_id`: Reference to chat_sessions (unique)
- `language`: Programming language code
- `prompt_template`: Custom prompt for the language
- `syntax_highlighting`: Syntax highlighting scheme
- `updated_at`: When context was last modified
- `created_at`: Creation timestamp
### supported_languages
- `code`: Language code (primary key)
- `name`: Display name
- `syntax_highlighting`: Highlighting scheme
- `file_extensions`: Array of file extensions
- `prompt_template`: Default prompt template
- `created_at`: Creation timestamp
## Environment Variables
Make sure these environment variables are set:
```bash
DATABASE_URL=postgresql://username:password@localhost:5432/chat_agent_db
REDIS_URL=redis://localhost:6379/0
```
## Development Setup
1. Install PostgreSQL and Redis
2. Create a database: `createdb chat_agent_db`
3. Set environment variables in `.env` file
4. Run migrations: `python manage_db.py init`
5. Optionally create sample data: `python manage_db.py sample`
## Production Deployment
1. Set up PostgreSQL database
2. Configure environment variables
3. Run migrations: `python migrations/migrate.py migrate --config production`
4. Verify setup: `python manage_db.py check --config production` |