204 lines
7.0 KiB
Python
204 lines
7.0 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Comprehensive seed script for PMFlow/Neural Sync.
|
||
Creates users, domains, and initial data.
|
||
"""
|
||
import asyncio
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
from sqlalchemy.future import select
|
||
|
||
BACKEND_ROOT = Path(__file__).resolve().parents[2]
|
||
sys.path.insert(0, str(BACKEND_ROOT))
|
||
|
||
from app.core.database import AsyncSessionLocal
|
||
from app.models import User, ProjectDomain, Project
|
||
from app.services.auth import AuthService
|
||
|
||
|
||
async def seed_users():
|
||
"""Create default users with known passwords."""
|
||
# Create initial users (with simple password hashes for demo)
|
||
# In production, these should be created with proper password hashing
|
||
# admin_hash = AuthService.hash_password("admin123")
|
||
# boss_hash = AuthService.hash_password("boss123")
|
||
# ai_hash = AuthService.hash_password("ai123")
|
||
# be_hash = AuthService.hash_password("be123")
|
||
# fe_hash = AuthService.hash_password("fe123")
|
||
|
||
async with AsyncSessionLocal() as session:
|
||
# Check if users already exist
|
||
result = await session.execute(select(User).where(User.username == "admin"))
|
||
admin = result.scalar_one_or_none()
|
||
|
||
if not admin:
|
||
# Create admin user
|
||
admin = User(
|
||
username="admin",
|
||
password_hash=AuthService.hash_password("admin123"),
|
||
name="系统管理员",
|
||
email="admin@pmflow.ai",
|
||
role="BOSS",
|
||
assigned_domains=[],
|
||
workload=0,
|
||
status="active",
|
||
is_active=True,
|
||
tenant_id=1,
|
||
)
|
||
session.add(admin)
|
||
print("✅ Created admin user (username: admin, password: admin123)")
|
||
|
||
# Create boss user
|
||
result = await session.execute(select(User).where(User.username == "boss"))
|
||
boss = result.scalar_one_or_none()
|
||
if not boss:
|
||
boss = User(
|
||
username="boss",
|
||
password_hash=AuthService.hash_password("boss123"),
|
||
name="老板",
|
||
email="boss@pmflow.ai",
|
||
role="BOSS",
|
||
assigned_domains=[],
|
||
workload=0,
|
||
status="active",
|
||
is_active=True,
|
||
tenant_id=1,
|
||
)
|
||
session.add(boss)
|
||
print("✅ Created boss user (username: boss, password: boss123)")
|
||
|
||
# Create AI engineer user
|
||
result = await session.execute(select(User).where(User.username == "ai_engineer"))
|
||
ai_user = result.scalar_one_or_none()
|
||
if not ai_user:
|
||
ai_user = User(
|
||
username="ai_engineer",
|
||
password_hash=AuthService.hash_password("ai123"),
|
||
name="AI工程师",
|
||
email="ai@pmflow.ai",
|
||
role="AI",
|
||
assigned_domains=[],
|
||
workload=0,
|
||
status="active",
|
||
is_active=True,
|
||
tenant_id=1,
|
||
)
|
||
session.add(ai_user)
|
||
print("✅ Created ai_engineer user (username: ai_engineer, password: ai123)")
|
||
|
||
# Create backend user
|
||
result = await session.execute(select(User).where(User.username == "backend"))
|
||
backend_user = result.scalar_one_or_none()
|
||
if not backend_user:
|
||
backend_user = User(
|
||
username="backend",
|
||
password_hash=AuthService.hash_password("be123"),
|
||
name="后端开发",
|
||
email="backend@pmflow.ai",
|
||
role="BE",
|
||
assigned_domains=[],
|
||
workload=0,
|
||
status="active",
|
||
is_active=True,
|
||
tenant_id=1,
|
||
)
|
||
session.add(backend_user)
|
||
print("✅ Created backend user (username: backend, password: be123)")
|
||
|
||
# Create frontend user
|
||
result = await session.execute(select(User).where(User.username == "frontend"))
|
||
frontend_user = result.scalar_one_or_none()
|
||
if not frontend_user:
|
||
frontend_user = User(
|
||
username="frontend",
|
||
password_hash=AuthService.hash_password("fe123"),
|
||
name="前端开发",
|
||
email="frontend@pmflow.ai",
|
||
role="FE",
|
||
assigned_domains=[],
|
||
workload=0,
|
||
status="active",
|
||
is_active=True,
|
||
tenant_id=1,
|
||
)
|
||
session.add(frontend_user)
|
||
print("✅ Created frontend user (username: frontend, password: fe123)")
|
||
|
||
await session.commit()
|
||
|
||
|
||
async def seed_domains():
|
||
"""Create default project domains."""
|
||
async with AsyncSessionLocal() as session:
|
||
# Check if domains already exist
|
||
result = await session.execute(select(ProjectDomain))
|
||
domains = result.scalars().all()
|
||
|
||
if not domains:
|
||
default_domains = [
|
||
ProjectDomain(
|
||
name="AI模型工程",
|
||
slug="ai-engineering",
|
||
description="AI模型训练、评估和部署相关项目",
|
||
is_default=True,
|
||
signal_count=0,
|
||
context_node_count=0,
|
||
experiment_count=0,
|
||
tenant_id=1,
|
||
),
|
||
ProjectDomain(
|
||
name="后端服务",
|
||
slug="backend-services",
|
||
description="后端API、数据库、微服务等项目",
|
||
is_default=False,
|
||
signal_count=0,
|
||
context_node_count=0,
|
||
experiment_count=0,
|
||
tenant_id=1,
|
||
),
|
||
ProjectDomain(
|
||
name="前端应用",
|
||
slug="frontend-apps",
|
||
description="Vue/React前端应用、UI组件等",
|
||
is_default=False,
|
||
signal_count=0,
|
||
context_node_count=0,
|
||
experiment_count=0,
|
||
tenant_id=1,
|
||
),
|
||
]
|
||
|
||
for domain in default_domains:
|
||
session.add(domain)
|
||
|
||
await session.commit()
|
||
print("✅ Created 3 default project domains: ai-engineering, backend-services, frontend-apps")
|
||
else:
|
||
print(f"ℹ️ Project domains already exist ({len(domains)} found)")
|
||
|
||
|
||
async def main():
|
||
print("🌱 Seeding PMFlow database...")
|
||
print("=" * 50)
|
||
|
||
await seed_users()
|
||
await seed_domains()
|
||
|
||
print("=" * 50)
|
||
print("✅ Seeding completed!")
|
||
print()
|
||
print("📝 Default users created:")
|
||
print(" - admin / admin123 (BOSS)")
|
||
print(" - boss / boss123 (BOSS)")
|
||
print(" - ai_engineer / ai123 (AI)")
|
||
print(" - backend / be123 (BE)")
|
||
print(" - frontend / fe123 (FE)")
|
||
print()
|
||
print("🔐 Use these credentials to login at: http://127.0.0.1:5174/login")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main())
|