PMFlow/backend/scripts/dev/seed_all.py

199 lines
6.7 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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.
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="OWNER",
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="OWNER",
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="BUILDER",
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="BUILDER",
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="BUILDER",
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 (OWNER)")
print(" - boss / boss123 (OWNER)")
print(" - ai_engineer / ai123 (BUILDER)")
print(" - backend / be123 (BUILDER)")
print(" - frontend / fe123 (BUILDER)")
print()
print("🔐 Use these credentials to login at: http://127.0.0.1:5174/login")
if __name__ == "__main__":
asyncio.run(main())