feat(playlists): implement Playlist and PlaylistSound models; add seeding for default Main playlist
This commit is contained in:
@@ -12,8 +12,8 @@ def init_database():
|
||||
# Seed plans if they don't exist
|
||||
seed_plans()
|
||||
|
||||
# Migrate existing users to have plans
|
||||
migrate_users_to_plans()
|
||||
# Create default Main playlist if it doesn't exist
|
||||
seed_main_playlist()
|
||||
|
||||
|
||||
def seed_plans():
|
||||
@@ -55,69 +55,35 @@ def seed_plans():
|
||||
print(f"Seeded {len(plans_data)} plans into database")
|
||||
|
||||
|
||||
def migrate_users_to_plans():
|
||||
"""Assign plans to existing users who don't have one."""
|
||||
from app.models.user import User
|
||||
def seed_main_playlist():
|
||||
"""Create the default Main playlist if it doesn't exist."""
|
||||
from app.models.playlist import Playlist
|
||||
|
||||
try:
|
||||
# Find users without plans
|
||||
users_without_plans = User.query.filter(User.plan_id.is_(None)).all()
|
||||
# Check if Main playlist already exists
|
||||
main_playlist = Playlist.query.filter_by(name="Main", user_id=None).first()
|
||||
|
||||
# Find users with plans but NULL credits (only if credits column exists)
|
||||
# Note: We only migrate users with NULL credits, not 0 credits
|
||||
# 0 credits means they spent them, NULL means they never got assigned
|
||||
try:
|
||||
users_without_credits = User.query.filter(
|
||||
User.plan_id.isnot(None),
|
||||
User.credits.is_(None),
|
||||
).all()
|
||||
except Exception:
|
||||
# Credits column doesn't exist yet, will be handled by create_all
|
||||
users_without_credits = []
|
||||
|
||||
if not users_without_plans and not users_without_credits:
|
||||
return
|
||||
|
||||
# Get default and pro plans
|
||||
default_plan = Plan.get_default_plan()
|
||||
pro_plan = Plan.get_pro_plan()
|
||||
|
||||
# Get the first user (admin) from all users ordered by ID
|
||||
first_user = User.query.order_by(User.id).first()
|
||||
|
||||
updated_count = 0
|
||||
|
||||
# Assign plans to users without plans
|
||||
for user in users_without_plans:
|
||||
# First user gets pro plan, others get free plan
|
||||
if user.id == first_user.id:
|
||||
user.plan_id = pro_plan.id
|
||||
# Only set credits if the column exists
|
||||
try:
|
||||
user.credits = pro_plan.credits
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
user.plan_id = default_plan.id
|
||||
# Only set credits if the column exists
|
||||
try:
|
||||
user.credits = default_plan.credits
|
||||
except Exception:
|
||||
pass
|
||||
updated_count += 1
|
||||
|
||||
# Assign credits to users with plans but no credits
|
||||
for user in users_without_credits:
|
||||
user.credits = user.plan.credits
|
||||
updated_count += 1
|
||||
|
||||
if updated_count > 0:
|
||||
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(
|
||||
f"Updated {updated_count} existing users with plans and credits",
|
||||
)
|
||||
|
||||
except Exception:
|
||||
# If there's any error (like missing columns), just skip migration
|
||||
# The database will be properly created by create_all()
|
||||
pass
|
||||
print("Updated existing Main playlist properties")
|
||||
|
||||
Reference in New Issue
Block a user