189 lines
No EOL
5.6 KiB
Python
189 lines
No EOL
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Database initialization script
|
|
Creates tables and optionally adds sample data
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
|
|
# Add the backend directory to the path so we can import our modules
|
|
sys.path.append(os.path.dirname(__file__))
|
|
|
|
from app.database import init_db, engine, SessionLocal
|
|
from app.models.user import User
|
|
from app.models.card import Card
|
|
from app.models.collection import Collection, CollectionCard
|
|
from app.models.deck import Deck, DeckCard
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
def create_sample_user(db: Session):
|
|
"""Create a sample user for testing"""
|
|
from app.routers.auth import hash_password
|
|
|
|
# Check if user already exists
|
|
existing_user = db.query(User).filter(User.username == "demo").first()
|
|
if existing_user:
|
|
print("Demo user already exists")
|
|
return existing_user
|
|
|
|
sample_user = User(
|
|
email="demo@tcgvault.com",
|
|
username="demo",
|
|
hashed_password=hash_password("demo123"),
|
|
full_name="Demo User",
|
|
is_active=True,
|
|
is_verified=True
|
|
)
|
|
|
|
db.add(sample_user)
|
|
db.commit()
|
|
db.refresh(sample_user)
|
|
print(f"Created demo user: {sample_user.username}")
|
|
return sample_user
|
|
|
|
|
|
def create_sample_cards(db: Session):
|
|
"""Create some sample cards for testing"""
|
|
sample_cards = [
|
|
{
|
|
"name": "Lightning Bolt",
|
|
"game": "MTG",
|
|
"set_name": "Alpha",
|
|
"set_code": "LEA",
|
|
"rarity": "Common",
|
|
"mana_cost": "{R}",
|
|
"cmc": 1,
|
|
"card_type": "Instant",
|
|
"colors": ["Red"],
|
|
"oracle_text": "Lightning Bolt deals 3 damage to any target.",
|
|
"current_price": 15.99,
|
|
"market_price": 14.50,
|
|
"verified": True
|
|
},
|
|
{
|
|
"name": "Black Lotus",
|
|
"game": "MTG",
|
|
"set_name": "Alpha",
|
|
"set_code": "LEA",
|
|
"rarity": "Rare",
|
|
"mana_cost": "{0}",
|
|
"cmc": 0,
|
|
"card_type": "Artifact",
|
|
"colors": [],
|
|
"oracle_text": "{T}, Sacrifice Black Lotus: Add three mana of any one color.",
|
|
"current_price": 25000.00,
|
|
"market_price": 24500.00,
|
|
"verified": True
|
|
},
|
|
{
|
|
"name": "Pikachu",
|
|
"game": "POKEMON",
|
|
"set_name": "Base Set",
|
|
"set_code": "BS1",
|
|
"rarity": "Common",
|
|
"card_type": "Pokemon",
|
|
"oracle_text": "When several of these Pokemon gather, their electricity could build and cause lightning storms.",
|
|
"current_price": 5.99,
|
|
"market_price": 6.50,
|
|
"verified": True
|
|
}
|
|
]
|
|
|
|
created_cards = []
|
|
for card_data in sample_cards:
|
|
# Check if card already exists
|
|
existing_card = db.query(Card).filter(
|
|
Card.name == card_data["name"],
|
|
Card.set_name == card_data["set_name"]
|
|
).first()
|
|
|
|
if not existing_card:
|
|
card = Card(**card_data)
|
|
db.add(card)
|
|
db.commit()
|
|
db.refresh(card)
|
|
created_cards.append(card)
|
|
print(f"Created sample card: {card.name}")
|
|
else:
|
|
created_cards.append(existing_card)
|
|
print(f"Sample card already exists: {existing_card.name}")
|
|
|
|
return created_cards
|
|
|
|
|
|
def create_sample_collection(db: Session, user: User, cards: list):
|
|
"""Create a sample collection with some cards"""
|
|
# Check if collection already exists
|
|
existing_collection = db.query(Collection).filter(
|
|
Collection.owner_id == user.id,
|
|
Collection.name == "My First Collection"
|
|
).first()
|
|
|
|
if existing_collection:
|
|
print("Sample collection already exists")
|
|
return existing_collection
|
|
|
|
collection = Collection(
|
|
name="My First Collection",
|
|
description="A starter collection with some classic cards",
|
|
owner_id=user.id,
|
|
is_public=True
|
|
)
|
|
|
|
db.add(collection)
|
|
db.commit()
|
|
db.refresh(collection)
|
|
|
|
# Add some cards to the collection
|
|
for i, card in enumerate(cards[:2]): # Add first 2 cards
|
|
collection_card = CollectionCard(
|
|
collection_id=collection.id,
|
|
card_id=card.id,
|
|
quantity=1 if i == 0 else 2, # Different quantities for variety
|
|
condition="NM",
|
|
foil=i == 1, # Make one foil
|
|
language="English"
|
|
)
|
|
db.add(collection_card)
|
|
|
|
db.commit()
|
|
print(f"Created sample collection: {collection.name}")
|
|
return collection
|
|
|
|
|
|
def main():
|
|
"""Initialize the database and create sample data"""
|
|
print("🚀 Initializing TCG Vault database...")
|
|
|
|
# Create all tables
|
|
init_db()
|
|
print("✅ Database tables created")
|
|
|
|
# Create sample data
|
|
db = SessionLocal()
|
|
try:
|
|
print("\n📝 Creating sample data...")
|
|
user = create_sample_user(db)
|
|
cards = create_sample_cards(db)
|
|
collection = create_sample_collection(db, user, cards)
|
|
|
|
print("\n🎉 Database initialization complete!")
|
|
print(f" • Created user: {user.username}")
|
|
print(f" • Created {len(cards)} sample cards")
|
|
print(f" • Created collection: {collection.name}")
|
|
print("\nYou can now start the API with: uvicorn main:app --reload")
|
|
print("And test with the demo user credentials:")
|
|
print(" Username: demo")
|
|
print(" Password: demo123")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error creating sample data: {e}")
|
|
db.rollback()
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |