Spaces:
Running
Running
Backfill default portfolios for existing users on startup.
Browse filesOn app boot, create a Default Portfolio for any user account that has none, including early Clerk test signups.
Co-authored-by: Cursor <cursoragent@cursor.com>
db.py
CHANGED
|
@@ -77,6 +77,7 @@ async def init_db():
|
|
| 77 |
await sync_stock_fundamental_seed_data()
|
| 78 |
await sync_stock_dividend_seed_data()
|
| 79 |
await ensure_default_admin_user()
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
async def close_db():
|
|
@@ -149,6 +150,21 @@ async def ensure_default_admin_user():
|
|
| 149 |
return
|
| 150 |
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
async def sync_stock_company_seed_data():
|
| 153 |
"""Populate curated stock company metadata from bundled research JSON."""
|
| 154 |
try:
|
|
|
|
| 77 |
await sync_stock_fundamental_seed_data()
|
| 78 |
await sync_stock_dividend_seed_data()
|
| 79 |
await ensure_default_admin_user()
|
| 80 |
+
await ensure_default_portfolios_for_users()
|
| 81 |
|
| 82 |
|
| 83 |
async def close_db():
|
|
|
|
| 150 |
return
|
| 151 |
|
| 152 |
|
| 153 |
+
async def ensure_default_portfolios_for_users():
|
| 154 |
+
"""Create a default portfolio for any user that has none (e.g. early Clerk signups)."""
|
| 155 |
+
try:
|
| 156 |
+
from App.routers.users.models import User
|
| 157 |
+
from App.routers.portfolio.models import Portfolio
|
| 158 |
+
|
| 159 |
+
users = await User.all()
|
| 160 |
+
for user in users:
|
| 161 |
+
has_portfolio = await Portfolio.filter(user=user).exists()
|
| 162 |
+
if not has_portfolio:
|
| 163 |
+
await Portfolio.create(user=user, name="Default Portfolio")
|
| 164 |
+
except Exception:
|
| 165 |
+
return
|
| 166 |
+
|
| 167 |
+
|
| 168 |
async def sync_stock_company_seed_data():
|
| 169 |
"""Populate curated stock company metadata from bundled research JSON."""
|
| 170 |
try:
|