Files

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

  1. Edit models.py to define your changes
  2. Run ./migrate.sh create "description"
  3. Review the generated migration
  4. 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 .env file 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.py as the single source of truth for schema