Set up database configuration and connection management
- Created app/database.py with SQLAlchemy engine, session management, and connection utilities - Added comprehensive database models in app/models.py for User, Client, Phone, Case, Transaction, Document, and Payment - Implemented FastAPI application with database lifecycle management in app/main.py - Added health check endpoint to verify database connectivity - Created README.md with database configuration documentation - Verified database connection works correctly with SQLite backend
This commit is contained in:
182
app/models.py
182
app/models.py
@@ -1 +1,181 @@
|
||||
# SQLAlchemy models for the Delphi database
|
||||
"""
|
||||
SQLAlchemy models for the Delphi database.
|
||||
|
||||
All models inherit from Base which is configured in the database module.
|
||||
"""
|
||||
|
||||
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Float, Text, Boolean
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.sql import func
|
||||
from .database import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
"""
|
||||
User model for authentication.
|
||||
|
||||
Stores user credentials and basic information for login functionality.
|
||||
"""
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
username = Column(String(50), unique=True, index=True, nullable=False)
|
||||
password_hash = Column(String(255), nullable=False)
|
||||
is_active = Column(Boolean, default=True)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
def __repr__(self):
|
||||
return f"<User(id={self.id}, username='{self.username}')>"
|
||||
|
||||
|
||||
class Client(Base):
|
||||
"""
|
||||
Client model representing individuals or entities.
|
||||
|
||||
Core client information imported from ROLODEX data.
|
||||
"""
|
||||
__tablename__ = "clients"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
rolodex_id = Column(String(20), unique=True, index=True)
|
||||
last_name = Column(String(50))
|
||||
first_name = Column(String(50))
|
||||
middle_initial = Column(String(10))
|
||||
company = Column(String(100))
|
||||
address = Column(String(255))
|
||||
city = Column(String(50))
|
||||
state = Column(String(2))
|
||||
zip_code = Column(String(10))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
phones = relationship("Phone", back_populates="client")
|
||||
cases = relationship("Case", back_populates="client")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Client(id={self.id}, name='{self.first_name} {self.last_name}')>"
|
||||
|
||||
|
||||
class Phone(Base):
|
||||
"""
|
||||
Phone number model linked to clients.
|
||||
|
||||
Stores phone number information for clients.
|
||||
"""
|
||||
__tablename__ = "phones"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
client_id = Column(Integer, ForeignKey("clients.id"), nullable=False)
|
||||
phone_type = Column(String(20)) # home, work, mobile, fax, etc.
|
||||
phone_number = Column(String(20))
|
||||
extension = Column(String(10))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# Relationships
|
||||
client = relationship("Client", back_populates="phones")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Phone(id={self.id}, number='{self.phone_number}')>"
|
||||
|
||||
|
||||
class Case(Base):
|
||||
"""
|
||||
Case model representing legal cases or files.
|
||||
|
||||
Main case information imported from FILES data.
|
||||
"""
|
||||
__tablename__ = "cases"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
file_no = Column(String(20), unique=True, index=True, nullable=False)
|
||||
client_id = Column(Integer, ForeignKey("clients.id"), nullable=False)
|
||||
status = Column(String(20), default="active")
|
||||
case_type = Column(String(50))
|
||||
description = Column(Text)
|
||||
open_date = Column(DateTime(timezone=True))
|
||||
close_date = Column(DateTime(timezone=True))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
client = relationship("Client", back_populates="cases")
|
||||
transactions = relationship("Transaction", back_populates="case")
|
||||
documents = relationship("Document", back_populates="case")
|
||||
payments = relationship("Payment", back_populates="case")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Case(id={self.id}, file_no='{self.file_no}')>"
|
||||
|
||||
|
||||
class Transaction(Base):
|
||||
"""
|
||||
Transaction model for financial transactions.
|
||||
|
||||
Records financial activities related to cases.
|
||||
"""
|
||||
__tablename__ = "transactions"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
case_id = Column(Integer, ForeignKey("cases.id"), nullable=False)
|
||||
transaction_date = Column(DateTime(timezone=True))
|
||||
transaction_type = Column(String(20))
|
||||
amount = Column(Float)
|
||||
description = Column(Text)
|
||||
reference = Column(String(50))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# Relationships
|
||||
case = relationship("Case", back_populates="transactions")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Transaction(id={self.id}, amount={self.amount})>"
|
||||
|
||||
|
||||
class Document(Base):
|
||||
"""
|
||||
Document model for case-related documents.
|
||||
|
||||
Stores information about documents associated with cases.
|
||||
"""
|
||||
__tablename__ = "documents"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
case_id = Column(Integer, ForeignKey("cases.id"), nullable=False)
|
||||
document_type = Column(String(50))
|
||||
file_name = Column(String(255))
|
||||
file_path = Column(String(500))
|
||||
description = Column(Text)
|
||||
uploaded_date = Column(DateTime(timezone=True))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# Relationships
|
||||
case = relationship("Case", back_populates="documents")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Document(id={self.id}, file_name='{self.file_name}')>"
|
||||
|
||||
|
||||
class Payment(Base):
|
||||
"""
|
||||
Payment model for payment records.
|
||||
|
||||
Records payments made or received for cases.
|
||||
"""
|
||||
__tablename__ = "payments"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
case_id = Column(Integer, ForeignKey("cases.id"), nullable=False)
|
||||
payment_date = Column(DateTime(timezone=True))
|
||||
payment_type = Column(String(20))
|
||||
amount = Column(Float)
|
||||
description = Column(Text)
|
||||
check_number = Column(String(20))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# Relationships
|
||||
case = relationship("Case", back_populates="payments")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Payment(id={self.id}, amount={self.amount})>"
|
||||
|
||||
Reference in New Issue
Block a user