Spaces:
Sleeping
Sleeping
File size: 10,655 Bytes
04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba f0b765c 04aa1ba | 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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | # User Authentication Migration Guide
> **Note:** The user authentication migration has been completed and the migration scripts have been moved to the `archive/` directory for historical reference. This documentation is maintained for reference purposes.
This directory contains scripts for migrating the Atlas API database to support user authentication while maintaining backward compatibility.
## Overview
The user authentication feature adds optional `user_id` fields to existing collections (`sessions`, `messages`, `search_analytics`) and creates appropriate indexes for efficient user-specific queries.
## Migration Scripts
### 1. Main Migration Script
**File:** `archive/migrate_user_authentication.py` *(Archived - migration completed)*
The primary migration script that handles the complete migration process.
```bash
# Run full migration
python archive/migrate_user_authentication.py migrate
# Check migration status
python archive/migrate_user_authentication.py status
# Create backup only
python archive/migrate_user_authentication.py backup
# Validate migration
python archive/migrate_user_authentication.py validate
# Rollback migration
python archive/migrate_user_authentication.py rollback
```
**Features:**
- Automatic backup creation before migration
- Safe addition of `user_id` fields to existing documents
- Index creation for performance
- Migration history tracking
- Comprehensive validation
- Rollback capability
### 2. Validation Script
**File:** `archive/validate_user_migration.py` *(Archived - migration completed)*
Comprehensive validation script to verify migration success.
```bash
# Basic validation
python archive/validate_user_migration.py
# Detailed validation with verbose output
python archive/validate_user_migration.py --detailed
# Validation with automatic issue fixing
python archive/validate_user_migration.py --detailed --fix-issues
```
**Validation Tests:**
- Schema validation (user_id fields exist)
- Index validation (proper indexes created)
- Data integrity checks
- Performance validation
- Backward compatibility verification
- Migration history validation
### 3. Rollback Script
**File:** `archive/rollback_user_authentication.py` *(Archived - migration completed)*
Dedicated rollback script to safely remove migration changes.
```bash
# Interactive rollback with confirmation
python archive/rollback_user_authentication.py
# Automated rollback (skip confirmation)
python archive/rollback_user_authentication.py --confirm
# Rollback with backup creation
python archive/rollback_user_authentication.py --backup-first --confirm
```
**Features:**
- Pre-rollback backup creation
- Safe removal of user_id fields
- Index cleanup
- Rollback verification
- Data loss warnings
### 4. Index Management Script
**File:** `scripts/deployment/create_user_indexes.py`
Standalone script for managing user_id indexes.
```bash
# Create indexes
python scripts/deployment/create_user_indexes.py create
# Verify indexes exist
python scripts/deployment/create_user_indexes.py verify
# List all indexes
python scripts/deployment/create_user_indexes.py list
# Remove user_id indexes
python scripts/deployment/create_user_indexes.py rollback
```
## Migration Process
### Pre-Migration Checklist
1. **Environment Setup**
```bash
# Ensure environment variables are set
export MONGODB_URL="your_mongodb_connection_string"
export MONGODB_DATABASE="atlas_analytics"
# Install dependencies
pip install -r requirements.txt
```
2. **Database Connection Test**
```bash
python -c "
import asyncio
from analytics.database import test_connection
print('β
Connected' if asyncio.run(test_connection()) else 'β Failed')
"
```
3. **Current Data Backup** (Recommended)
```bash
python archive/migrate_user_authentication.py backup
```
### Step-by-Step Migration
1. **Check Current Status**
```bash
python archive/migrate_user_authentication.py status
```
2. **Run Migration**
```bash
python archive/migrate_user_authentication.py migrate
```
3. **Validate Results**
```bash
python archive/validate_user_migration.py --detailed
```
4. **Test Application** (Manual)
- Test anonymous user requests (existing functionality)
- Test authenticated user requests (new functionality)
- Verify analytics dashboard works
### Post-Migration Verification
1. **Schema Verification**
- All documents have `user_id` field (set to `null` for existing data)
- New documents can have actual user_id values
2. **Index Verification**
- Sparse indexes on `user_id` fields
- Compound indexes on `(user_id, timestamp)`
3. **Functionality Verification**
- Anonymous requests work unchanged
- Authenticated requests store user_id
- Analytics queries perform well
## Rollback Process
### When to Rollback
- Migration validation fails
- Application issues after migration
- Performance problems
- Need to revert to previous state
### Rollback Steps
1. **Check Rollback Feasibility**
```bash
python archive/rollback_user_authentication.py
# Review warnings and data loss implications
```
2. **Create Pre-Rollback Backup** (if user data exists)
```bash
python archive/rollback_user_authentication.py --backup-first --confirm
```
3. **Verify Rollback Success**
```bash
python archive/validate_user_migration.py
# Should show no user_id fields or indexes
```
## Safety Features
### Backup System
- Automatic backups before migration
- Optional backups before rollback
- JSON format for easy inspection
- Stored in `migration_backups/` and `rollback_backups/`
### Migration History
- All operations tracked in `migration_history` collection
- Timestamps and status tracking
- Error logging and details
- Rollback event tracking
### Data Integrity
- Existing data preserved during migration
- Null values for user_id in migrated documents
- Validation of data consistency
- Performance impact monitoring
### Error Handling
- Graceful failure handling
- Detailed error logging
- Partial migration recovery
- Safe rollback procedures
## Troubleshooting
### Common Issues
1. **Database Connection Failed**
```bash
# Check environment variables
echo $MONGODB_URL
echo $MONGODB_DATABASE
# Test connection
python -c "import asyncio; from analytics.database import test_connection; print(asyncio.run(test_connection()))"
```
2. **Migration Partially Completed**
```bash
# Check status
python archive/migrate_user_authentication.py status
# Re-run migration (safe to run multiple times)
python archive/migrate_user_authentication.py migrate
```
3. **Index Creation Failed**
```bash
# Create indexes separately
python scripts/deployment/create_user_indexes.py create
# Verify indexes
python scripts/deployment/create_user_indexes.py verify
```
4. **Validation Failures**
```bash
# Run detailed validation
python archive/validate_user_migration.py --detailed
# Attempt automatic fixes
python archive/validate_user_migration.py --detailed --fix-issues
```
### Recovery Procedures
1. **Restore from Backup**
```bash
# Manual restore from backup JSON file
# (Requires custom script based on backup structure)
```
2. **Partial Rollback**
```bash
# Remove only indexes
python scripts/deployment/create_user_indexes.py rollback
# Full rollback
python archive/rollback_user_authentication.py --confirm
```
3. **Re-run Migration**
```bash
# Safe to run multiple times
python archive/migrate_user_authentication.py migrate
```
## Performance Considerations
### Index Strategy
- **Sparse indexes**: Handle null user_id values efficiently
- **Compound indexes**: Optimize user history queries
- **Background creation**: Minimize impact on running system
### Query Performance
- User-specific queries use indexes
- Anonymous queries unaffected
- Backward compatibility maintained
### Storage Impact
- Minimal storage overhead (one field per document)
- Null values for existing anonymous data
- Efficient index storage with sparse indexes
## Security Considerations
### User ID Handling
- User IDs treated as identifiers, not authentication
- No authorization logic based on user_id
- Client-provided user_id values (validation only)
### Data Privacy
- User associations stored as provided
- No automatic PII detection
- Export functionality respects user filtering
### Access Control
- Database-level access controls unchanged
- Application-level user validation required
- Migration scripts require database admin access
## Monitoring and Maintenance
### Regular Checks
```bash
# Weekly validation
python archive/validate_user_migration.py
# Index performance monitoring
python scripts/deployment/create_user_indexes.py list
# Migration history review
python archive/migrate_user_authentication.py status
```
### Performance Monitoring
- Monitor query execution times
- Check index usage statistics
- Review storage growth patterns
### Backup Schedule
- Regular database backups
- Pre-migration backups for major changes
- Retention policy for backup files
## Support and Documentation
### Log Files
- Migration logs in application logs
- Detailed error messages
- Performance metrics
### Documentation
- API documentation updated for user_id parameter
- Integration examples for frontend developers
- Troubleshooting guides
### Testing
- Comprehensive test suite in `tests/` directory
- Integration tests for user authentication
- Performance tests for user queries
- Backward compatibility tests
---
## Quick Reference
### Essential Commands
```bash
# Full migration workflow
python archive/migrate_user_authentication.py migrate
python archive/validate_user_migration.py --detailed
# Emergency rollback
python archive/rollback_user_authentication.py --confirm
# Status check
python archive/migrate_user_authentication.py status
```
### File Structure
```
βββ archive/
β βββ migrate_user_authentication.py # Main migration script (archived)
β βββ validate_user_migration.py # Validation script (archived)
β βββ rollback_user_authentication.py # Rollback script (archived)
β βββ migration_backups/ # Migration backups (archived)
βββ scripts/deployment/create_user_indexes.py # Index management (active)
βββ docs/reference/migration-guide.md # This documentation
βββ rollback_backups/ # Rollback backups
```
For additional support, refer to the main project documentation or contact the development team. |