60 lines
1.7 KiB
Markdown
60 lines
1.7 KiB
Markdown
# Database Initialization Fix
|
|
|
|
## Issue
|
|
Dashboard was showing errors:
|
|
```
|
|
sqlite3.OperationalError: no such table: files
|
|
```
|
|
|
|
## Root Cause
|
|
The `DatabaseReader._ensure_database()` method only initialized the database if the file didn't exist:
|
|
|
|
```python
|
|
if not self.db_path.exists():
|
|
self._initialize_database()
|
|
```
|
|
|
|
This caused problems when:
|
|
- Database file exists but is empty
|
|
- Database file exists but schema is outdated
|
|
- Database was created but initialization failed partway through
|
|
|
|
## Fix
|
|
Changed to always run initialization (dashboard.py:129-133):
|
|
|
|
```python
|
|
def _ensure_database(self):
|
|
"""Ensure database exists and has correct schema"""
|
|
# Always run initialization - it's safe with CREATE TABLE IF NOT EXISTS
|
|
# This ensures migrations run even if the file exists but schema is outdated
|
|
self._initialize_database()
|
|
```
|
|
|
|
## Why This Is Safe
|
|
|
|
1. **CREATE TABLE IF NOT EXISTS** - Won't recreate existing tables
|
|
2. **Migration checks** - Only adds columns that don't exist
|
|
3. **Idempotent** - Can run multiple times safely
|
|
4. **Auto-repair** - Fixes incomplete or corrupted schemas
|
|
|
|
## Benefits
|
|
|
|
✅ Database always has correct schema
|
|
✅ Migrations run automatically on startup
|
|
✅ Handles edge cases (empty files, partial schemas)
|
|
✅ No manual intervention needed
|
|
✅ Works for fresh installs and upgrades
|
|
|
|
## Similar Fix Applied
|
|
|
|
The same initialization logic exists in `reencode.py`'s `StateDatabase` class, which already runs migrations every time. This change brings `dashboard.py` in line with that behavior.
|
|
|
|
## Testing
|
|
|
|
After this fix:
|
|
1. Dashboard starts successfully
|
|
2. Database is created if missing
|
|
3. Tables are created if missing
|
|
4. New columns are added via migration
|
|
5. All API endpoints work properly
|