| name | db-reset |
| description | 重置测试数据库(清空并重新初始化所有表结构和数据) |
| disable-model-invocation | true |
数据库重置技能
⚠️ 警告:此操作会清空所有数据并重建数据库!
使用方法
通过用户调用:/db-reset
数据库凭据
| 配置项 | 值 |
|---|
| 主机 | localhost |
| 端口 | 5432 |
| 用户名 | fulla_user |
| 密码 | 123456 |
| 数据库 | fulla_db |
完整工作流程
1. 停止后端服务
# Windows
Get-Process -Name "fulla-server" -ErrorAction SilentlyContinue | Stop-Process -Force
2. 删除并重建数据库
$env:PGPASSWORD = "123456"
psql -h localhost -U fulla_user -d postgres -c "DROP DATABASE IF EXISTS fulla_db;"
psql -h localhost -U fulla_user -d postgres -c "CREATE DATABASE fulla_db;"
3. 执行 Migration 脚本
数据库 schema 统一通过 apps/server/migrations/ 目录管理(V001–V024,共 24 个文件)。
$env:PGPASSWORD = "123456"
Get-ChildItem "apps\server\migrations\V*.sql" | Sort-Object Name | ForEach-Object {
psql -h localhost -U fulla_user -d fulla_db -f $_.FullName
if ($LASTEXITCODE -eq 0) { Write-Host "✅ $($_.Name)" }
else { Write-Host "❌ $($_.Name)"; exit 1 }
}
4. 执行 Seed 数据
Get-ChildItem "apps\server\seed\*.sql" | ForEach-Object {
psql -h localhost -U fulla_user -d fulla_db -f $_.FullName
if ($LASTEXITCODE -eq 0) { Write-Host "✅ $($_.Name)" }
else { Write-Host "⚠️ $($_.Name) (non-critical)" }
}
$env:PGPASSWORD = $null
Write-Host "`n🎉 Database reset completed!"
5. 验证
$env:PGPASSWORD = "123456"
psql -h localhost -U fulla_user -d fulla_db -c "\dt"
$env:PGPASSWORD = $null
SQL 目录结构
apps/server/
├── migrations/ # Schema 定义(按版本顺序执行,V001–V024 共 24 个文件)
│ ├── V001__schema_migrations.sql
│ ├── V002__oauth2_core.sql
│ ├── V003__oauth2_core_indexes.sql
│ ├── V004__users_table.sql
│ ├── V005__rbac_schema.sql
│ ├── V006__oauth2_scopes.sql
│ ├── V007__user_public_sub.sql
│ ├── V008__refresh_token_family.sql
│ ├── V009__password_reset_tokens.sql
│ ├── V010__email_verification.sql
│ ├── V011__mfa_support.sql
│ ├── V012__audit_logs.sql
│ ├── V013__account_lockout.sql
│ ├── V014__device_codes.sql
│ ├── V015__backchannel_logout.sql
│ ├── V016__token_partitioning_prep.sql
│ ├── V017__multi_tenant.sql
│ ├── V018__webauthn.sql
│ ├── V019__email_validation.sql
│ ├── V020__username_optional.sql
│ ├── V021__widen_email_verification_tokens_email.sql
│ ├── V022__mfa_pending_client_binding.sql
│ ├── V023__resource_scopes.sql
│ └── V024__users_soft_delete.sql
└── seed/ # 开发/测试环境初始数据(5 个文件)
├── bench_users.sql
├── dev_admin_user.sql
├── dev_admin_console_client.sql
├── dev_backend_client.sql
└── dev_vue_client.sql
注意: 旧的 sql/001_*.sql ~ sql/004_*.sql 已废弃删除,所有 schema 统一在 migrations/ 管理。物理表共 21 张:其中 19 张被 ORM 建模(model.json),另含迁移追踪表 schema_migrations 与归档表 oauth2_access_tokens_archive。
故障排除
数据库正在被使用
SELECT pg_terminate_backend(pid) FROM pg_stat_activity
WHERE datname = 'fulla_db' AND pid <> pg_backend_pid();
权限不足
GRANT ALL PRIVILEGES ON DATABASE fulla_db TO fulla_user;
ALTER DATABASE fulla_db OWNER TO fulla_user;
相关技能
/orm-gen - 重置后重新生成 ORM 模型
/build-and-test - 重建后运行测试