prep for pricing service work
This commit is contained in:
@@ -3,6 +3,10 @@ from sqlalchemy.orm import sessionmaker, Session
|
||||
from contextlib import contextmanager
|
||||
from typing import Generator
|
||||
import os
|
||||
from sqlalchemy import inspect
|
||||
from services.tcgplayer import TCGPlayerService
|
||||
#from services.pricing import PricingService
|
||||
from services.file import FileService
|
||||
|
||||
|
||||
import logging
|
||||
@@ -45,11 +49,33 @@ def get_db() -> Generator[Session, None, None]:
|
||||
yield session
|
||||
|
||||
def init_db() -> None:
|
||||
"""Initialize database tables"""
|
||||
"""Initialize database tables and run first-time setup if needed"""
|
||||
from .models import Base
|
||||
try:
|
||||
inspector = inspect(engine)
|
||||
tables_exist = all(
|
||||
table in inspector.get_table_names()
|
||||
for table in Base.metadata.tables.keys()
|
||||
)
|
||||
# if tables_exist:
|
||||
# drop all tables except file
|
||||
# for table in inspector.get_table_names():
|
||||
# if table != 'files':
|
||||
# Base.metadata.drop_all(bind=engine, tables=[Base.metadata.tables[table]])
|
||||
# logger.info(f"Dropped table: {table}")
|
||||
|
||||
# Create tables if they don't exist
|
||||
Base.metadata.create_all(bind=engine)
|
||||
logger.info("Database tables created successfully")
|
||||
|
||||
# Run first-time setup only if tables were just created
|
||||
if not tables_exist:
|
||||
# with get_db_session() as session:
|
||||
# tcgplayer_service = TCGPlayerService(session, PricingService(session), FileService(session))
|
||||
# tcgplayer_service.populate_tcgplayer_groups()
|
||||
# tcgplayer_service.cron_load_prices()
|
||||
logger.info("First-time database setup completed")
|
||||
|
||||
logger.info("Database initialization completed")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize database: {str(e)}")
|
||||
raise
|
||||
|
38
db/models.py
38
db/models.py
@@ -31,11 +31,8 @@ class Product(Base):
|
||||
return product_line
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
name = Column(String)
|
||||
type = Column(String) # box or card
|
||||
product_line = Column(String) # pokemon, mtg, etc.
|
||||
set_name = Column(String)
|
||||
set_code = Column(String)
|
||||
date_created = Column(DateTime, default=datetime.now)
|
||||
date_modified = Column(DateTime, default=datetime.now, onupdate=datetime.now)
|
||||
|
||||
@@ -133,43 +130,10 @@ class Card(Base):
|
||||
"""
|
||||
Card represents the concept of a distinct card
|
||||
Cards have metadata from different sources
|
||||
internal: box, inventory, upload
|
||||
external: price, attributes - scryfall, tcgplayer, manabox
|
||||
"""
|
||||
__tablename__ = "cards"
|
||||
|
||||
@validates("rarity")
|
||||
def validate_rarity(self, key, rarity: str):
|
||||
single_character_rarity = {'m': 'mythic', 'r': 'rare', 'u': 'uncommon', 'c': 'common', 'l': 'land', 'p': 'promo', 's': 'special'}
|
||||
if rarity not in RarityEnum:
|
||||
if rarity.lower() in RarityEnum:
|
||||
rarity = rarity.lower()
|
||||
elif rarity in single_character_rarity:
|
||||
rarity = single_character_rarity[rarity]
|
||||
else:
|
||||
raise ValueError(f"Invalid rarity: {rarity}")
|
||||
return rarity
|
||||
|
||||
@validates("condition")
|
||||
def validate_condition(self, key, condition: str):
|
||||
if condition not in ConditionEnum:
|
||||
if condition.lower() in ConditionEnum:
|
||||
condition = condition.lower()
|
||||
elif condition.lower().strip().replace(' ', '_') in ConditionEnum:
|
||||
condition = condition.lower().strip().replace(' ', '_')
|
||||
else:
|
||||
raise ValueError(f"Invalid condition: {condition}")
|
||||
return condition
|
||||
|
||||
product_id = Column(String, ForeignKey("products.id"), primary_key=True)
|
||||
number = Column(String)
|
||||
foil = Column(String)
|
||||
rarity = Column(String)
|
||||
condition = Column(String)
|
||||
language = Column(String)
|
||||
scryfall_id = Column(String)
|
||||
manabox_id = Column(String)
|
||||
tcgplayer_id = Column(Integer)
|
||||
date_created = Column(DateTime, default=datetime.now)
|
||||
date_modified = Column(DateTime, default=datetime.now, onupdate=datetime.now)
|
||||
|
||||
@@ -183,7 +147,7 @@ class CardManabox(Base):
|
||||
collector_number = Column(String)
|
||||
foil = Column(String)
|
||||
rarity = Column(String)
|
||||
manabox_id = Column(String)
|
||||
manabox_id = Column(Integer)
|
||||
scryfall_id = Column(String)
|
||||
condition = Column(String)
|
||||
language = Column(String)
|
||||
|
Reference in New Issue
Block a user