Files
WCTDataMiner/docs/architecture/database/迁移策略.md
Scottxjw 2b031995e1 docs: 更新项目架构与数据模型文档
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-13 15:01:32 +08:00

1.7 KiB
Raw Blame History

数据库迁移策略

所属模块:数据库支持


1. 本地库迁移

SQLite

# 添加迁移
dotnet ef migrations add InitialCreate --output-dir Migrations/SQLite \
  --context WctMinerDbContext --project src/Gpulse.WCT.DataAnalyzer.Core

# 应用迁移
dotnet ef database update

PostgreSQL

# 切换配置后添加迁移
dotnet ef migrations add InitialCreate --output-dir Migrations/PostgreSQL \
  --context WctMinerDbContext --project src/Gpulse.WCT.DataAnalyzer.Core \
  -- --environment Production

2. 发布库迁移

发布库的迁移需要使用 ReleaseDbContext

# 添加迁移
dotnet ef migrations add InitialCreate --output-dir Migrations/Release \
  --context ReleaseDbContext --project src/Gpulse.WCT.DataAnalyzer.Core

# 应用迁移
dotnet ef database update --context ReleaseDbContext

3. 配置文件示例

SQLite默认双库

{
  "Database": {
    "Type": "sqlite",
    "LocalPath": "./data/database/local.db",
    "ReleasePath": "./data/database/release.db"
  }
}

PostgreSQL双库独立

{
  "Database": {
    "Type": "postgresql",
    "Host": "localhost",
    "Port": 5432,
    "LocalPath": "Server=localhost;Database=wct_local",
    "ReleasePath": "Server=db-server;Database=wct_charging_parameters"
  }
}

4. 数据迁移工具

推荐使用 pgloader 迁移 SQLite 到 PostgreSQL

# 本地库
pgloader ./data/database/local.db postgresql://user:password@localhost:5432/wct_local

# 发布库
pgloader ./data/database/release.db postgresql://user:password@db-server:5432/wct_charging_parameters

两个数据库独立迁移,不共享迁移历史。