diff --git a/backend/init_db.py b/backend/init_db.py new file mode 100644 index 0000000..8d807ab --- /dev/null +++ b/backend/init_db.py @@ -0,0 +1,44 @@ +"""Quick DB init script - creates tables and seeds admin user.""" +import sys +sys.path.insert(0, ".") + +from app.database import init_db, SessionLocal +from app.models.tenant import Tenant +from app.models.user import User +from app.core.security import hash_password + +print("Initializing database...") +init_db() +print("Tables created.") + +db = SessionLocal() + +# Default tenant +t = db.query(Tenant).filter(Tenant.slug == "default").first() +if not t: + t = Tenant(name="默认租户", slug="default") + db.add(t) + db.flush() + print(f"Created tenant: {t.name} (id={t.id})") +else: + print(f"Tenant exists: {t.name} (id={t.id})") + +# Admin user +u = db.query(User).filter(User.email == "admin@test.com").first() +if not u: + u = User( + email="admin@test.com", + username="admin", + hashed_password=hash_password("admin123"), + role="tenant_admin", + user_type="human", + tenant_id=t.id, + ) + db.add(u) + db.commit() + print(f"Created admin user: {u.email} (id={u.id}, tenant_id={u.tenant_id})") +else: + print(f"Admin user exists: {u.email}") + +db.close() +print("Done!")