Phase 3 Complete: Python API Foundation (T027-T038)
Completed all Python API infrastructure tasks: ✅ Core Application (T027-T029): - FastAPI app with CORS, error handling, structured logging - Pydantic Settings for environment configuration - SQLAlchemy async engine with connection pooling - Alembic migration environment ✅ Infrastructure Clients (T030-T032): - Redis async client with connection pooling - gRPC SDK Bridge client (placeholder for protobuf generation) - Alembic migration environment configured ✅ Utilities & Middleware (T033-T035): - JWT utilities: create, decode, verify tokens (access & refresh) - Error translation: gRPC status codes → HTTP status codes - Error handler middleware for consistent error responses ✅ Database Models (T036-T038): - User model with RBAC (viewer, operator, administrator) - AuditLog model for tracking all operations - Initial migration: creates users and audit_logs tables - Default admin user (username: admin, password: admin123) Features: - Async/await throughout - Type hints with Pydantic - Structured JSON logging - Connection pooling (DB, Redis, gRPC) - Environment-based configuration - Permission hierarchy system Ready for Phase 4: Authentication Implementation 🤖 Generated with Claude Code
This commit is contained in:
78
src/api/migrations/versions/20251208_initial_schema.py
Normal file
78
src/api/migrations/versions/20251208_initial_schema.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""Initial schema: users and audit_logs tables
|
||||
|
||||
Revision ID: 001_initial
|
||||
Revises:
|
||||
Create Date: 2025-12-08
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '001_initial'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Create initial tables"""
|
||||
|
||||
# Create users table
|
||||
op.create_table(
|
||||
'users',
|
||||
sa.Column('id', UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column('username', sa.String(50), nullable=False, unique=True),
|
||||
sa.Column('password_hash', sa.String(255), nullable=False),
|
||||
sa.Column('role', sa.Enum('viewer', 'operator', 'administrator', name='userrole'), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
)
|
||||
|
||||
# Create index on username for faster lookups
|
||||
op.create_index('ix_users_username', 'users', ['username'])
|
||||
|
||||
# Create audit_logs table
|
||||
op.create_table(
|
||||
'audit_logs',
|
||||
sa.Column('id', UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column('user_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('action', sa.String(100), nullable=False),
|
||||
sa.Column('target', sa.String(255), nullable=True),
|
||||
sa.Column('outcome', sa.String(20), nullable=False),
|
||||
sa.Column('timestamp', sa.DateTime(), nullable=False),
|
||||
sa.Column('details', JSONB, nullable=True),
|
||||
sa.Column('ip_address', sa.String(45), nullable=True),
|
||||
sa.Column('user_agent', sa.Text(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='SET NULL'),
|
||||
)
|
||||
|
||||
# Create indexes for faster queries
|
||||
op.create_index('ix_audit_logs_action', 'audit_logs', ['action'])
|
||||
op.create_index('ix_audit_logs_timestamp', 'audit_logs', ['timestamp'])
|
||||
|
||||
# Insert default admin user (password: admin123 - CHANGE IN PRODUCTION!)
|
||||
# Hash generated with: passlib.hash.bcrypt.hash("admin123")
|
||||
op.execute("""
|
||||
INSERT INTO users (id, username, password_hash, role, created_at, updated_at)
|
||||
VALUES (
|
||||
gen_random_uuid(),
|
||||
'admin',
|
||||
'$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5ufUfVwq7z.lW',
|
||||
'administrator',
|
||||
NOW(),
|
||||
NOW()
|
||||
)
|
||||
""")
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Drop tables"""
|
||||
op.drop_index('ix_audit_logs_timestamp', 'audit_logs')
|
||||
op.drop_index('ix_audit_logs_action', 'audit_logs')
|
||||
op.drop_table('audit_logs')
|
||||
|
||||
op.drop_index('ix_users_username', 'users')
|
||||
op.drop_table('users')
|
||||
|
||||
# Drop enum type
|
||||
op.execute('DROP TYPE userrole')
|
||||
Reference in New Issue
Block a user