90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
"""Database initialization and seeding functions."""
|
|
|
|
from app.database import db
|
|
from app.models.plan import Plan
|
|
|
|
|
|
def init_database():
|
|
"""Initialize database tables and seed with default data."""
|
|
# Create all tables
|
|
db.create_all()
|
|
|
|
# Seed plans if they don't exist
|
|
seed_plans()
|
|
|
|
# Create default Main playlist if it doesn't exist
|
|
seed_main_playlist()
|
|
|
|
|
|
def seed_plans():
|
|
"""Seed the plans table with default plans if empty."""
|
|
# Check if plans already exist
|
|
if Plan.query.count() > 0:
|
|
return
|
|
|
|
# Create default plans
|
|
plans_data = [
|
|
{
|
|
"code": "free",
|
|
"name": "Free Plan",
|
|
"description": "Basic features with limited usage",
|
|
"credits": 25,
|
|
"max_credits": 75,
|
|
},
|
|
{
|
|
"code": "premium",
|
|
"name": "Premium Plan",
|
|
"description": "Enhanced features with increased usage limits",
|
|
"credits": 50,
|
|
"max_credits": 150,
|
|
},
|
|
{
|
|
"code": "pro",
|
|
"name": "Pro Plan",
|
|
"description": "Full access with unlimited usage",
|
|
"credits": 100,
|
|
"max_credits": 300,
|
|
},
|
|
]
|
|
|
|
for plan_data in plans_data:
|
|
plan = Plan(**plan_data)
|
|
db.session.add(plan)
|
|
|
|
db.session.commit()
|
|
print(f"Seeded {len(plans_data)} plans into database")
|
|
|
|
|
|
def seed_main_playlist():
|
|
"""Create the default Main playlist if it doesn't exist."""
|
|
from app.models.playlist import Playlist
|
|
|
|
# Check if Main playlist already exists
|
|
main_playlist = Playlist.query.filter_by(name="Main", user_id=None).first()
|
|
|
|
if main_playlist is None:
|
|
# Create the Main playlist
|
|
main_playlist = Playlist.create_playlist(
|
|
name="Main",
|
|
description="Default main playlist for all sounds",
|
|
genre=None,
|
|
user_id=None, # System playlist
|
|
is_main=True,
|
|
is_deletable=False,
|
|
is_current=True,
|
|
commit=True,
|
|
)
|
|
print("Created default Main playlist")
|
|
else:
|
|
# Ensure the existing Main playlist has correct properties
|
|
if (
|
|
not main_playlist.is_main
|
|
or main_playlist.is_deletable
|
|
or not main_playlist.is_current
|
|
):
|
|
main_playlist.is_main = True
|
|
main_playlist.is_deletable = False
|
|
main_playlist.is_current = True
|
|
db.session.commit()
|
|
print("Updated existing Main playlist properties")
|