1818from sqlalchemy import text
1919from sqlalchemy .ext .asyncio import AsyncSession
2020
21- from intentkit .models .db import get_session
21+ from intentkit .config .config import config
22+ from intentkit .models .db import get_session , init_db
2223
2324# Configure logging
2425logging .basicConfig (
@@ -195,13 +196,24 @@ async def process_single_account(account_id: str) -> bool:
195196
196197
197198async def get_all_account_ids () -> List [str ]:
198- """Get all credit account IDs from the database .
199+ """Get credit account IDs that need migration (all 8 statistics fields are 0) .
199200
200201 Returns:
201- List of account IDs
202+ List of account IDs that need statistics migration
202203 """
203204 async with get_session () as session :
204- query = text ("SELECT id FROM credit_accounts ORDER BY created_at" )
205+ query = text ("""
206+ SELECT id FROM credit_accounts
207+ WHERE total_income = 0
208+ AND total_free_income = 0
209+ AND total_reward_income = 0
210+ AND total_permanent_income = 0
211+ AND total_expense = 0
212+ AND total_free_expense = 0
213+ AND total_reward_expense = 0
214+ AND total_permanent_expense = 0
215+ ORDER BY created_at
216+ """ )
205217 result = await session .execute (query )
206218 return [row .id for row in result .fetchall ()]
207219
@@ -211,6 +223,9 @@ async def main():
211223 logger .info ("Starting credit account statistics migration" )
212224
213225 try :
226+ # Initialize database connection
227+ await init_db (** config .db )
228+
214229 # Create backup table
215230 async with get_session () as session :
216231 await create_backup_table (session )
0 commit comments