mirror of
https://github.com/pablorevilla-meshtastic/meshview.git
synced 2026-08-07 01:12:54 +02:00
move technical docs
This commit is contained in:
@@ -0,0 +1,361 @@
|
||||
# Alembic Database Migration Setup
|
||||
|
||||
This document describes the automatic database migration system implemented for MeshView using Alembic.
|
||||
|
||||
## Overview
|
||||
|
||||
The system provides automatic database schema migrations with coordination between the writer app (startdb.py) and reader app (web.py):
|
||||
|
||||
- **Writer App**: Automatically runs pending migrations on startup
|
||||
- **Reader App**: Waits for migrations to complete before starting
|
||||
|
||||
## Architecture
|
||||
|
||||
### Key Components
|
||||
|
||||
1. **`meshview/migrations.py`** - Migration management utilities
|
||||
- `run_migrations()` - Runs pending migrations (writer app)
|
||||
- `wait_for_migrations()` - Waits for schema to be current (reader app)
|
||||
- `is_database_up_to_date()` - Checks schema version
|
||||
- Migration status tracking table
|
||||
|
||||
2. **`alembic/`** - Alembic migration directory
|
||||
- `env.py` - Configured for async SQLAlchemy support
|
||||
- `versions/` - Migration scripts directory
|
||||
- `alembic.ini` - Alembic configuration
|
||||
|
||||
3. **Modified Apps**:
|
||||
- `startdb.py` - Writer app that runs migrations before MQTT ingestion
|
||||
- `meshview/web.py` - Reader app that waits for schema updates
|
||||
|
||||
## How It Works - Automatic In-Place Updates
|
||||
|
||||
### ✨ Fully Automatic Operation
|
||||
|
||||
**No manual migration commands needed!** The database schema updates automatically when you:
|
||||
1. Deploy new code with migration files
|
||||
2. Restart the applications
|
||||
|
||||
### Writer App (startdb.py) Startup Sequence
|
||||
|
||||
1. Initialize database connection
|
||||
2. Create migration status tracking table
|
||||
3. Set "migration in progress" flag
|
||||
4. **🔄 Automatically run any pending Alembic migrations** (synchronously)
|
||||
- Detects current schema version
|
||||
- Compares to latest available migration
|
||||
- Runs all pending migrations in sequence
|
||||
- Updates database schema in place
|
||||
5. Clear "migration in progress" flag
|
||||
6. Start MQTT ingestion and other tasks
|
||||
|
||||
### Reader App (web.py) Startup Sequence
|
||||
|
||||
1. Initialize database connection
|
||||
2. **Check database schema version**
|
||||
3. If not up to date:
|
||||
- Wait up to 60 seconds (30 retries × 2 seconds)
|
||||
- Check every 2 seconds for schema updates
|
||||
- Automatically proceeds once writer completes migrations
|
||||
4. Once schema is current, start web server
|
||||
|
||||
### 🎯 Key Point: Zero Manual Steps
|
||||
|
||||
When you deploy new code with migrations:
|
||||
```bash
|
||||
# Just start the apps - migrations happen automatically!
|
||||
./env/bin/python startdb.py # Migrations run here automatically
|
||||
./env/bin/python main.py # Waits for migrations, then starts
|
||||
```
|
||||
|
||||
**The database updates itself!** No need to run `alembic upgrade` manually.
|
||||
|
||||
### Coordination
|
||||
|
||||
The apps coordinate using:
|
||||
- **Alembic version table** (`alembic_version`) - Tracks current schema version
|
||||
- **Migration status table** (`migration_status`) - Optional flag for "in progress" state
|
||||
|
||||
## Creating New Migrations
|
||||
|
||||
### Using the helper script:
|
||||
|
||||
```bash
|
||||
./env/bin/python create_migration.py
|
||||
```
|
||||
|
||||
### Manual creation:
|
||||
|
||||
```bash
|
||||
./env/bin/alembic revision --autogenerate -m "Description of changes"
|
||||
```
|
||||
|
||||
This will:
|
||||
1. Compare current database schema with SQLAlchemy models
|
||||
2. Generate a migration script in `alembic/versions/`
|
||||
3. Automatically detect most schema changes
|
||||
|
||||
### Manual migration (advanced):
|
||||
|
||||
```bash
|
||||
./env/bin/alembic revision -m "Manual migration"
|
||||
```
|
||||
|
||||
Then edit the generated file to add custom migration logic.
|
||||
|
||||
## Running Migrations
|
||||
|
||||
### Automatic (Recommended)
|
||||
|
||||
Migrations run automatically when the writer app starts:
|
||||
|
||||
```bash
|
||||
./env/bin/python startdb.py
|
||||
```
|
||||
|
||||
### Manual
|
||||
|
||||
To run migrations manually:
|
||||
|
||||
```bash
|
||||
./env/bin/alembic upgrade head
|
||||
```
|
||||
|
||||
To downgrade:
|
||||
|
||||
```bash
|
||||
./env/bin/alembic downgrade -1 # Go back one version
|
||||
./env/bin/alembic downgrade base # Go back to beginning
|
||||
```
|
||||
|
||||
## Checking Migration Status
|
||||
|
||||
Check current database version:
|
||||
|
||||
```bash
|
||||
./env/bin/alembic current
|
||||
```
|
||||
|
||||
View migration history:
|
||||
|
||||
```bash
|
||||
./env/bin/alembic history
|
||||
```
|
||||
|
||||
## Benefits
|
||||
|
||||
1. **Zero Manual Intervention**: Migrations run automatically on startup
|
||||
2. **Safe Coordination**: Reader won't connect to incompatible schema
|
||||
3. **Version Control**: All schema changes tracked in git
|
||||
4. **Rollback Capability**: Can downgrade if needed
|
||||
5. **Auto-generation**: Most migrations created automatically from model changes
|
||||
|
||||
## Migration Workflow
|
||||
|
||||
### Development Process
|
||||
|
||||
1. **Modify SQLAlchemy models** in `meshview/models.py`
|
||||
2. **Create migration**:
|
||||
```bash
|
||||
./env/bin/python create_migration.py
|
||||
```
|
||||
3. **Review generated migration** in `alembic/versions/`
|
||||
4. **Test migration**:
|
||||
- Stop all apps
|
||||
- Start writer app (migrations run automatically)
|
||||
- Start reader app (waits for schema to be current)
|
||||
5. **Commit migration** to version control
|
||||
|
||||
### Production Deployment
|
||||
|
||||
1. **Deploy new code** with migration scripts
|
||||
2. **Start writer app** - Migrations run automatically
|
||||
3. **Start reader app** - Waits for migrations, then starts
|
||||
4. **Monitor logs** for migration success
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Migration fails
|
||||
|
||||
Check logs in writer app for error details. To manually fix:
|
||||
|
||||
```bash
|
||||
./env/bin/alembic current # Check current version
|
||||
./env/bin/alembic history # View available versions
|
||||
./env/bin/alembic upgrade head # Try manual upgrade
|
||||
```
|
||||
|
||||
### Reader app won't start (timeout)
|
||||
|
||||
Check if writer app is running and has completed migrations:
|
||||
|
||||
```bash
|
||||
./env/bin/alembic current
|
||||
```
|
||||
|
||||
### Reset to clean state
|
||||
|
||||
⚠️ **Warning: This will lose all data**
|
||||
|
||||
```bash
|
||||
rm packets.db # Or your database file
|
||||
./env/bin/alembic upgrade head # Create fresh schema
|
||||
```
|
||||
|
||||
## File Structure
|
||||
|
||||
```
|
||||
meshview/
|
||||
├── alembic.ini # Alembic configuration
|
||||
├── alembic/
|
||||
│ ├── env.py # Async-enabled migration runner
|
||||
│ ├── script.py.mako # Migration template
|
||||
│ └── versions/ # Migration scripts
|
||||
│ └── c88468b7ab0b_initial_migration.py
|
||||
├── meshview/
|
||||
│ ├── models.py # SQLAlchemy models (source of truth)
|
||||
│ ├── migrations.py # Migration utilities
|
||||
│ ├── mqtt_database.py # Writer database connection
|
||||
│ └── database.py # Reader database connection
|
||||
├── startdb.py # Writer app (runs migrations)
|
||||
├── main.py # Entry point for reader app
|
||||
└── create_migration.py # Helper script for creating migrations
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Database URL is read from `config.ini`:
|
||||
|
||||
```ini
|
||||
[database]
|
||||
connection_string = sqlite+aiosqlite:///packets.db
|
||||
```
|
||||
|
||||
Alembic automatically uses this configuration through `meshview/migrations.py`.
|
||||
|
||||
## Important Notes
|
||||
|
||||
1. **Always test migrations** in development before deploying to production
|
||||
2. **Backup database** before running migrations in production
|
||||
3. **Check for data loss** - Some migrations may require data migration logic
|
||||
4. **Coordinate deployments** - Start writer before readers in multi-instance setups
|
||||
5. **Monitor logs** during first startup after deployment
|
||||
|
||||
## Example Migrations
|
||||
|
||||
### Example 1: Generated Initial Migration
|
||||
|
||||
Here's what an auto-generated migration looks like (from comparing models to database):
|
||||
|
||||
```python
|
||||
"""Initial migration
|
||||
|
||||
Revision ID: c88468b7ab0b
|
||||
Revises:
|
||||
Create Date: 2025-01-26 20:56:50.123456
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers
|
||||
revision = 'c88468b7ab0b'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade() -> None:
|
||||
# Upgrade operations
|
||||
op.create_table('node',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('node_id', sa.BigInteger(), nullable=True),
|
||||
# ... more columns
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
def downgrade() -> None:
|
||||
# Downgrade operations
|
||||
op.drop_table('node')
|
||||
```
|
||||
|
||||
### Example 2: Manual Migration Adding a New Table
|
||||
|
||||
We've included an example migration (`1717fa5c6545_add_example_table.py`) that demonstrates how to manually create a new table:
|
||||
|
||||
```python
|
||||
"""Add example table
|
||||
|
||||
Revision ID: 1717fa5c6545
|
||||
Revises: c88468b7ab0b
|
||||
Create Date: 2025-10-26 20:59:04.347066
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Create example table with sample columns."""
|
||||
op.create_table(
|
||||
'example',
|
||||
sa.Column('id', sa.Integer(), nullable=False, primary_key=True, autoincrement=True),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('value', sa.Float(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False, server_default='1'),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False,
|
||||
server_default=sa.text('CURRENT_TIMESTAMP')),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create an index on the name column for faster lookups
|
||||
op.create_index('idx_example_name', 'example', ['name'])
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Remove example table."""
|
||||
op.drop_index('idx_example_name', table_name='example')
|
||||
op.drop_table('example')
|
||||
```
|
||||
|
||||
**Key features demonstrated:**
|
||||
- Various column types (Integer, String, Text, Float, Boolean, DateTime)
|
||||
- Primary key with autoincrement
|
||||
- Nullable and non-nullable columns
|
||||
- Server defaults (for timestamps and booleans)
|
||||
- Creating indexes
|
||||
- Proper downgrade that reverses all changes
|
||||
|
||||
**To test this migration:**
|
||||
|
||||
```bash
|
||||
# Apply the migration
|
||||
./env/bin/alembic upgrade head
|
||||
|
||||
# Check it was applied
|
||||
./env/bin/alembic current
|
||||
|
||||
# Verify table was created
|
||||
sqlite3 packetsPL.db "SELECT sql FROM sqlite_master WHERE type='table' AND name='example';"
|
||||
|
||||
# Roll back the migration
|
||||
./env/bin/alembic downgrade -1
|
||||
|
||||
# Verify table was removed
|
||||
sqlite3 packetsPL.db "SELECT name FROM sqlite_master WHERE type='table' AND name='example';"
|
||||
```
|
||||
|
||||
**To remove this example migration** (after testing):
|
||||
|
||||
```bash
|
||||
# First make sure you're not on this revision
|
||||
./env/bin/alembic downgrade c88468b7ab0b
|
||||
|
||||
# Then delete the migration file
|
||||
rm alembic/versions/1717fa5c6545_add_example_table.py
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
- [Alembic Documentation](https://alembic.sqlalchemy.org/)
|
||||
- [SQLAlchemy Documentation](https://docs.sqlalchemy.org/)
|
||||
- [Async SQLAlchemy](https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html)
|
||||
@@ -0,0 +1,193 @@
|
||||
# High-Resolution Timestamp Migration
|
||||
|
||||
This document describes the implementation of GitHub issue #55: storing high-resolution timestamps as integers in the database for improved performance and query efficiency.
|
||||
|
||||
## Overview
|
||||
|
||||
The meshview database now stores timestamps in two formats:
|
||||
1. **TEXT format** (`import_time`): Human-readable ISO8601 format with microseconds (e.g., `2025-03-12 04:15:56.058038`)
|
||||
2. **INTEGER format** (`import_time_us`): Microseconds since Unix epoch (1970-01-01 00:00:00 UTC)
|
||||
|
||||
The dual format approach provides:
|
||||
- **Backward compatibility**: Existing `import_time` TEXT columns remain unchanged
|
||||
- **Performance**: Fast integer comparisons and math operations
|
||||
- **Precision**: Microsecond resolution for accurate timing
|
||||
- **Efficiency**: Compact storage and fast indexed lookups
|
||||
|
||||
## Database Changes
|
||||
|
||||
### New Columns Added
|
||||
|
||||
Three tables have new `import_time_us` columns:
|
||||
|
||||
1. **packet.import_time_us** (INTEGER)
|
||||
- Stores when the packet was imported into the database
|
||||
- Indexed for fast queries
|
||||
|
||||
2. **packet_seen.import_time_us** (INTEGER)
|
||||
- Stores when the packet_seen record was imported
|
||||
- Indexed for performance
|
||||
|
||||
3. **traceroute.import_time_us** (INTEGER)
|
||||
- Stores when the traceroute was imported
|
||||
- Indexed for fast lookups
|
||||
|
||||
### New Indexes
|
||||
|
||||
The following indexes were created for optimal query performance:
|
||||
|
||||
```sql
|
||||
CREATE INDEX idx_packet_import_time_us ON packet(import_time_us DESC);
|
||||
CREATE INDEX idx_packet_from_node_time_us ON packet(from_node_id, import_time_us DESC);
|
||||
CREATE INDEX idx_packet_seen_import_time_us ON packet_seen(import_time_us);
|
||||
CREATE INDEX idx_traceroute_import_time_us ON traceroute(import_time_us);
|
||||
```
|
||||
|
||||
## Migration Process
|
||||
|
||||
### For Existing Databases
|
||||
|
||||
Run the migration script to add the new columns and populate them from existing data:
|
||||
|
||||
```bash
|
||||
python migrate_add_timestamp_us.py [database_path]
|
||||
```
|
||||
|
||||
If no path is provided, it defaults to `packets.db` in the current directory.
|
||||
|
||||
The migration script:
|
||||
1. Checks if migration is needed (idempotent)
|
||||
2. Adds `import_time_us` columns to the three tables
|
||||
3. Populates the new columns from existing `import_time` values
|
||||
4. Creates indexes for optimal performance
|
||||
5. Verifies the migration completed successfully
|
||||
|
||||
### For New Databases
|
||||
|
||||
New databases created with the updated schema will automatically include the `import_time_us` columns. The MQTT store module populates both columns when inserting new records.
|
||||
|
||||
## Code Changes
|
||||
|
||||
### Models (meshview/models.py)
|
||||
|
||||
The ORM models now include the new `import_time_us` fields:
|
||||
|
||||
```python
|
||||
class Packet(Base):
|
||||
import_time: Mapped[datetime] = mapped_column(nullable=True)
|
||||
import_time_us: Mapped[int] = mapped_column(BigInteger, nullable=True)
|
||||
```
|
||||
|
||||
### MQTT Store (meshview/mqtt_store.py)
|
||||
|
||||
The data ingestion logic now populates both timestamp columns using UTC time:
|
||||
|
||||
```python
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
now_us = int(now.timestamp() * 1_000_000)
|
||||
|
||||
# Both columns are populated
|
||||
import_time=now,
|
||||
import_time_us=now_us,
|
||||
```
|
||||
|
||||
**Important**: All new timestamps use UTC (Coordinated Universal Time) for consistency across time zones.
|
||||
|
||||
## Using the New Timestamps
|
||||
|
||||
### Example Queries
|
||||
|
||||
**Query packets from the last 7 days:**
|
||||
|
||||
```sql
|
||||
-- Old way (slower)
|
||||
SELECT * FROM packet
|
||||
WHERE import_time >= datetime('now', '-7 days');
|
||||
|
||||
-- New way (faster)
|
||||
SELECT * FROM packet
|
||||
WHERE import_time_us >= (strftime('%s', 'now', '-7 days') * 1000000);
|
||||
```
|
||||
|
||||
**Query packets in a specific time range:**
|
||||
|
||||
```sql
|
||||
SELECT * FROM packet
|
||||
WHERE import_time_us BETWEEN 1759254380000000 AND 1759254390000000;
|
||||
```
|
||||
|
||||
**Calculate time differences (in microseconds):**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
id,
|
||||
(import_time_us - LAG(import_time_us) OVER (ORDER BY import_time_us)) / 1000000.0 as seconds_since_last
|
||||
FROM packet
|
||||
LIMIT 10;
|
||||
```
|
||||
|
||||
### Converting Timestamps
|
||||
|
||||
**From datetime to microseconds (UTC):**
|
||||
```python
|
||||
import datetime
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
now_us = int(now.timestamp() * 1_000_000)
|
||||
```
|
||||
|
||||
**From microseconds to datetime:**
|
||||
```python
|
||||
import datetime
|
||||
timestamp_us = 1759254380813451
|
||||
dt = datetime.datetime.fromtimestamp(timestamp_us / 1_000_000)
|
||||
```
|
||||
|
||||
**In SQL queries:**
|
||||
```sql
|
||||
-- Datetime to microseconds
|
||||
SELECT CAST((strftime('%s', import_time) || substr(import_time, 21, 6)) AS INTEGER);
|
||||
|
||||
-- Microseconds to datetime (approximate)
|
||||
SELECT datetime(import_time_us / 1000000, 'unixepoch');
|
||||
```
|
||||
|
||||
## Performance Benefits
|
||||
|
||||
The integer timestamp columns provide significant performance improvements:
|
||||
|
||||
1. **Faster comparisons**: Integer comparisons are much faster than string/datetime comparisons
|
||||
2. **Smaller index size**: Integer indexes are more compact than datetime indexes
|
||||
3. **Range queries**: BETWEEN operations on integers are highly optimized
|
||||
4. **Math operations**: Easy to calculate time differences, averages, etc.
|
||||
5. **Sorting**: Integer sorting is faster than datetime sorting
|
||||
|
||||
## Backward Compatibility
|
||||
|
||||
The original `import_time` TEXT columns remain unchanged:
|
||||
- Existing code continues to work
|
||||
- Human-readable timestamps still available
|
||||
- Gradual migration to new columns possible
|
||||
- No breaking changes for existing queries
|
||||
|
||||
## Future Work
|
||||
|
||||
Future improvements could include:
|
||||
- Migrating queries to use `import_time_us` columns
|
||||
- Deprecating the TEXT `import_time` columns (after transition period)
|
||||
- Adding helper functions for timestamp conversion
|
||||
- Creating views that expose both formats
|
||||
|
||||
## Testing
|
||||
|
||||
The migration was tested on a production database with:
|
||||
- 132,466 packet records
|
||||
- 1,385,659 packet_seen records
|
||||
- 28,414 traceroute records
|
||||
|
||||
All records were successfully migrated with microsecond precision preserved.
|
||||
|
||||
## References
|
||||
|
||||
- GitHub Issue: #55 - Storing High-Resolution Timestamps in SQLite
|
||||
- SQLite datetime functions: https://www.sqlite.org/lang_datefunc.html
|
||||
- Python datetime module: https://docs.python.org/3/library/datetime.html
|
||||
Reference in New Issue
Block a user