|
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
Base = declarative_base()
|
|
|
|
class Product(Base):
|
|
__tablename__ = "products"
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String, nullable=False)
|
|
current_stock = Column(Float, nullable=False)
|
|
safety_stock = Column(Float, nullable=False)
|
|
lead_time = Column(Float, nullable=False)
|
|
monthly_demand = Column(Float, nullable=False)
|
|
product_class = Column(String, nullable=False)
|
|
|
|
class Transaction(Base):
|
|
__tablename__ = "transactions"
|
|
id = Column(Integer, primary_key=True)
|
|
product_id = Column(Integer, nullable=False)
|
|
quantity = Column(Float, nullable=False)
|
|
transaction_type = Column(String, nullable=False)
|
|
timestamp = Column(DateTime, server_default="CURRENT_TIMESTAMP")
|
|
|
|
engine = create_engine("sqlite:///:memory:")
|
|
SessionLocal = sessionmaker(bind=engine)
|
|
|
|
def initialize_database():
|
|
Base.metadata.create_all(engine)
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |