mirror of
https://github.com/ferdzo/iotDashboard.git
synced 2026-04-05 01:06:24 +00:00
Database Migrations
This directory contains all database schema definitions and migrations for the IoT Dashboard project.
Quick Start
1. Install Dependencies
pip install alembic sqlalchemy python-dotenv psycopg2-binary
2. Configure Database
Set CONNECTION_STRING or DATABASE_URL in the root .env file:
CONNECTION_STRING=postgresql://user:password@localhost:5432/iotdashboard
3. Create Initial Migration
chmod +x migrate.sh
./migrate.sh create "initial schema"
4. Review Migration
Check the generated file in alembic/versions/
5. Apply Migration
./migrate.sh upgrade
Usage
Create a New Migration
After editing models.py:
./migrate.sh create "add new column"
Apply Migrations
./migrate.sh upgrade
Check Current Version
./migrate.sh current
View History
./migrate.sh history
Rollback
./migrate.sh downgrade 1
File Structure
db_migrations/
├── models.py # SQLAlchemy models (schema definition)
├── alembic.ini # Alembic configuration
├── alembic/
│ ├── env.py # Migration environment
│ ├── script.py.mako # Migration template
│ └── versions/ # Generated migrations
├── migrate.sh # Helper script
└── README.md # This file
Modifying Schema
- Edit
models.pyto define your changes - Run
./migrate.sh create "description" - Review the generated migration
- Run
./migrate.sh upgrade
Using Models in Services
Services can import models from here:
# In services/db_write/db_writer.py or any other service
import sys
sys.path.insert(0, '/path/to/db_migrations')
from models import SensorReading
Or better, use relative imports if properly structured.
Notes
- The
.envfile should be in the project root (../.env) - Migrations are applied to whatever database is in
CONNECTION_STRING - Always review generated migrations before applying
- Keep
models.pyas the single source of truth for schema