FastApi-ISS/database.py
2025-08-07 00:27:44 +03:30

80 lines
3.1 KiB
Python

from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, Boolean, Text, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.sql import func
from sqlalchemy.dialects.postgresql import UUID, JSONB
import os
import uuid
# Database URL from environment variable
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
# Fallback to constructing from individual variables
POSTGRES_USER = os.getenv("POSTGRES_USER", "fastapi_user")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD", "fastapi_password")
POSTGRES_HOST = os.getenv("POSTGRES_HOST", "postgres")
POSTGRES_PORT = os.getenv("POSTGRES_PORT", "5432")
POSTGRES_DB = os.getenv("POSTGRES_DB", "fastapi_db")
DATABASE_URL = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DB}"
# Create SQLAlchemy engine
engine = create_engine(DATABASE_URL)
# Create SessionLocal class
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create Base class
Base = declarative_base()
# Basic Item model (for backward compatibility)
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
description = Column(String, nullable=True)
price = Column(Float, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
# Instagram Account Model
class InstagramAccount(Base):
__tablename__ = "instagram_accounts"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
seller_id = Column(UUID(as_uuid=True), nullable=False)
username = Column(String(100), nullable=False, unique=True)
is_active = Column(Boolean, default=True)
last_synced = Column(DateTime(timezone=True), nullable=True)
# Relationship
posts = relationship("InstagramPost", back_populates="account", cascade="all, delete-orphan")
# Instagram Post Model
class InstagramPost(Base):
__tablename__ = "instagram_posts"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
account_id = Column(UUID(as_uuid=True), ForeignKey("instagram_accounts.id"), nullable=False)
ig_post_id = Column(String(100), nullable=False, unique=True)
media_type = Column(String(10), nullable=False) # 'image', 'video', 'carousel'
caption = Column(Text, nullable=True)
thumbnail_url = Column(Text, nullable=True)
local_media = Column(JSONB, nullable=True) # List of media items with type and local path
created_at = Column(DateTime(timezone=True), nullable=False) # Original post time on Instagram
fetched_at = Column(DateTime(timezone=True), server_default=func.now()) # Time when scraped
# Relationship
account = relationship("InstagramAccount", back_populates="posts")
# Dependency to get database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# Create tables
def create_tables():
Base.metadata.create_all(bind=engine)