File size: 7,490 Bytes
2ed8996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# SQLite Fallback Database

This document describes the SQLite fallback database implementation for the AegisLM SaaS Backend.

## Overview

The SQLite fallback database provides high availability by automatically switching to a local SQLite database when the primary PostgreSQL database becomes unavailable. This ensures the application remains functional even during PostgreSQL outages.

## Architecture

### Primary Database (PostgreSQL)
- **Production Database**: Neon PostgreSQL
- **Connection**: Async SQLAlchemy with connection pooling
- **Features**: Full feature support, high performance

### Fallback Database (SQLite)
- **Fallback Database**: Local SQLite file
- **Connection**: Async SQLite with aiosqlite driver
- **Features**: Basic functionality, local storage, automatic failover

## Configuration

Add these environment variables to your `.env` file:

```env
# SQLite Fallback Database Configuration
ENABLE_SQLITE_FALLBACK=true
SQLITE_DATABASE_PATH="aegislm_fallback.db"
SQLITE_FALLBACK_TIMEOUT=5
```

### Configuration Options

- `ENABLE_SQLITE_FALLBACK`: Enable/disable the fallback system
- `SQLITE_DATABASE_PATH`: Path to the SQLite database file
- `SQLITE_FALLBACK_TIMEOUT`: Timeout in seconds for fallback connections

## How It Works

### 1. Automatic Failover
- Application tries to connect to PostgreSQL first
- If PostgreSQL fails, automatically switches to SQLite
- Seamless switching without application downtime

### 2. Data Synchronization
- **Initial Setup**: Migrates data from PostgreSQL to SQLite
- **Ongoing Sync**: Can sync SQLite data back to PostgreSQL
- **Bidirectional**: Supports both migration directions

### 3. Health Monitoring
- Continuous health checks for both databases
- Automatic recovery when PostgreSQL becomes available
- Detailed status reporting

## API Endpoints

The system provides management endpoints for superusers:

### Status Monitoring
- `GET /api/v1/sqlite-fallback/status` - Get fallback system status
- `POST /api/v1/sqlite-fallback/test` - Test fallback mechanism

### Database Control
- `POST /api/v1/sqlite-fallback/switch-to-primary` - Force switch to PostgreSQL
- `POST /api/v1/sqlite-fallback/switch-to-fallback` - Force switch to SQLite

### Data Management
- `POST /api/v1/sqlite-fallback/migrate-from-primary` - Migrate PostgreSQL → SQLite
- `POST /api/v1/sqlite-fallback/sync-to-primary` - Sync SQLite → PostgreSQL
- `POST /api/v1/sqlite-fallback/backup` - Backup SQLite database
- `DELETE /api/v1/sqlite-fallback/cleanup` - Remove SQLite database

## Setup and Installation

### 1. Install Dependencies
```bash
pip install aiosqlite
```

### 2. Configure Environment
Add the SQLite fallback configuration to your `.env` file (see above).

### 3. Run Setup Script
```bash
cd backend
python setup_sqlite_fallback.py
```

### 4. Verify Installation
The setup script will:
- Initialize the SQLite fallback database
- Test the fallback mechanism
- Report system status

## Usage Examples

### Checking Fallback Status
```python
from core.fallback_database import check_database_health, get_current_database_type

is_healthy, db_type = await check_database_health()
print(f"Database healthy: {is_healthy}, Active: {db_type}")
```

### Manual Database Switching
```python
from core.fallback_database import switch_to_fallback, switch_to_primary

# Switch to SQLite fallback
success = await switch_to_fallback()

# Switch back to PostgreSQL
success = await switch_to_primary()
```

### Data Migration
```python
from core.sqlite_fallback_manager import SQLiteFallbackManager

manager = SQLiteFallbackManager()

# Migrate from PostgreSQL to SQLite
success = await manager.migrate_from_postgresql()

# Sync from SQLite back to PostgreSQL
success = await manager.sync_to_postgresql()
```

## Database Compatibility

### SQLite Compatibility Features
- **JSON Fields**: Automatically converted to/from text
- **DateTime Fields**: Proper timezone handling
- **Boolean Fields**: Standard SQLite boolean support
- **Foreign Keys**: Enabled with proper constraints

### Limitations
- Some PostgreSQL-specific features may not work in SQLite
- Performance is lower than PostgreSQL for large datasets
- Concurrent writes are limited by SQLite's file-based nature

## Monitoring and Logging

### Health Checks
The system provides comprehensive health monitoring:
- Primary database connectivity
- Fallback database connectivity
- Automatic failover status
- Data synchronization status

### Logging
All fallback operations are logged with appropriate levels:
- `INFO`: Normal operations, status changes
- `WARNING`: Failover events, connection issues
- `ERROR`: Critical failures, setup problems

## Best Practices

### 1. Regular Backups
- Create regular backups of the SQLite database
- Use the provided backup endpoint for automated backups

### 2. Monitor Performance
- Monitor fallback usage patterns
- Check database file size and performance
- Set up alerts for frequent fallback usage

### 3. Data Synchronization
- Regularly sync data back to PostgreSQL
- Verify data integrity after synchronization
- Test migration procedures periodically

### 4. Security Considerations
- Restrict fallback management endpoints to superusers
- Secure the SQLite database file permissions
- Monitor fallback system access logs

## Troubleshooting

### Common Issues

#### SQLite Database Not Found
- Check `SQLITE_DATABASE_PATH` configuration
- Verify directory permissions
- Run the setup script again

#### Migration Failures
- Ensure both databases are accessible
- Check for data type compatibility issues
- Review error logs for specific problems

#### Performance Issues
- Monitor SQLite database size
- Consider regular cleanup and optimization
- Check for long-running transactions

### Debug Mode
Enable debug logging for detailed troubleshooting:
```python
import logging
logging.getLogger('core.fallback_database').setLevel(logging.DEBUG)
logging.getLogger('core.sqlite_fallback_manager').setLevel(logging.DEBUG)
```

## Performance Considerations

### SQLite Optimizations
The system automatically applies SQLite optimizations:
- WAL journal mode for better concurrency
- Normal synchronous mode for balance
- Memory temp store for performance
- Optimized cache size

### Connection Pooling
- Primary database uses connection pooling
- SQLite uses static pooling for consistency
- Automatic connection cleanup on errors

## Security

### Access Control
- Fallback management requires superuser privileges
- Database file permissions are properly set
- API endpoints are protected with authentication

### Data Protection
- SQLite database file is stored securely
- Backup files include timestamps
- Sensitive data is not logged

## Testing

### Automated Tests
Run the comprehensive test suite:
```bash
cd backend
python -m core.sqlite_fallback_manager test
```

### Manual Testing
Use the provided test endpoints to verify:
- Connection failover
- Data migration
- API functionality
- Performance characteristics

## Future Enhancements

### Planned Features
- **Multi-database Support**: Support for additional database types
- **Advanced Sync**: Real-time synchronization capabilities
- **Performance Monitoring**: Built-in performance metrics
- **Automated Recovery**: Enhanced automatic recovery procedures

### Extensibility
The system is designed for easy extension:
- Plugin architecture for new database types
- Configurable failover strategies
- Custom synchronization rules
- Enhanced monitoring capabilities