Add Alembic for database migrations and initial migration scripts

- Created alembic.ini configuration file for Alembic migrations.
- Added README file for Alembic with a brief description.
- Implemented env.py for Alembic to manage database migrations.
- Created script.py.mako template for migration scripts.
- Added initial migration script to create database tables.
- Created a migration script to add initial plan and playlist data.
- Updated database initialization to run Alembic migrations.
- Enhanced credit service to automatically recharge user credits based on their plan.
- Implemented delete_task method in scheduler service to remove scheduled tasks.
- Updated scheduler API to reflect task deletion instead of cancellation.
- Added CLI tool for managing database migrations.
- Updated tests to cover new functionality for task deletion and credit recharge.
- Updated pyproject.toml and lock files to include Alembic as a dependency.
This commit is contained in:
JSC
2025-09-16 13:45:14 +02:00
parent e8f979c137
commit 83239cb4fa
16 changed files with 828 additions and 29 deletions

View File

@@ -403,6 +403,44 @@ class CreditService:
finally:
await session.close()
async def recharge_user_credits_auto(
self,
user_id: int,
) -> CreditTransaction | None:
"""Recharge credits for a user automatically based on their plan.
Args:
user_id: The user ID
Returns:
The created credit transaction if credits were added, None if no recharge
needed
Raises:
ValueError: If user not found or has no plan
"""
session = self.db_session_factory()
try:
user_repo = UserRepository(session)
user = await user_repo.get_by_id_with_plan(user_id)
if not user:
msg = f"User {user_id} not found"
raise ValueError(msg)
if not user.plan:
msg = f"User {user_id} has no plan assigned"
raise ValueError(msg)
# Call the main method with plan details
return await self.recharge_user_credits(
user_id,
user.plan.credits,
user.plan.max_credits,
)
finally:
await session.close()
async def recharge_user_credits(
self,
user_id: int,
@@ -556,7 +594,13 @@ class CreditService:
if transaction:
stats["recharged_users"] += 1
stats["total_credits_added"] += transaction.amount
# Calculate the amount from plan data to avoid session issues
current_credits = user.credits
plan_credits = user.plan.credits
max_credits = user.plan.max_credits
target_credits = min(current_credits + plan_credits, max_credits)
credits_added = target_credits - current_credits
stats["total_credits_added"] += credits_added
else:
stats["skipped_users"] += 1