2026-07-02 15:53:30 +08:00
|
|
|
|
# 数据库迁移策略
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
> 所属模块:[数据库支持](./概览.md)
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
## 1. 本地库迁移
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
### SQLite
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
# 添加迁移
|
2026-08-13 15:01:32 +08:00
|
|
|
|
dotnet ef migrations add InitialCreate --output-dir Migrations/SQLite \
|
|
|
|
|
|
--context WctMinerDbContext --project src/Gpulse.WCT.DataAnalyzer.Core
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
# 应用迁移
|
|
|
|
|
|
dotnet ef database update
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### PostgreSQL
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
# 切换配置后添加迁移
|
2026-08-13 15:01:32 +08:00
|
|
|
|
dotnet ef migrations add InitialCreate --output-dir Migrations/PostgreSQL \
|
|
|
|
|
|
--context WctMinerDbContext --project src/Gpulse.WCT.DataAnalyzer.Core \
|
|
|
|
|
|
-- --environment Production
|
2026-07-02 15:53:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
## 2. 发布库迁移
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
发布库的迁移需要使用 `ReleaseDbContext`:
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
```bash
|
|
|
|
|
|
# 添加迁移
|
|
|
|
|
|
dotnet ef migrations add InitialCreate --output-dir Migrations/Release \
|
|
|
|
|
|
--context ReleaseDbContext --project src/Gpulse.WCT.DataAnalyzer.Core
|
|
|
|
|
|
|
|
|
|
|
|
# 应用迁移
|
|
|
|
|
|
dotnet ef database update --context ReleaseDbContext
|
|
|
|
|
|
```
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 3. 配置文件示例
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
### SQLite(默认双库)
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
|
{
|
|
|
|
|
|
"Database": {
|
|
|
|
|
|
"Type": "sqlite",
|
2026-08-13 15:01:32 +08:00
|
|
|
|
"LocalPath": "./data/database/local.db",
|
|
|
|
|
|
"ReleasePath": "./data/database/release.db"
|
2026-07-02 15:53:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
### PostgreSQL(双库独立)
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
|
{
|
|
|
|
|
|
"Database": {
|
|
|
|
|
|
"Type": "postgresql",
|
|
|
|
|
|
"Host": "localhost",
|
|
|
|
|
|
"Port": 5432,
|
2026-08-13 15:01:32 +08:00
|
|
|
|
"LocalPath": "Server=localhost;Database=wct_local",
|
|
|
|
|
|
"ReleasePath": "Server=db-server;Database=wct_charging_parameters"
|
2026-07-02 15:53:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
## 4. 数据迁移工具
|
|
|
|
|
|
|
|
|
|
|
|
推荐使用 [pgloader](https://github.com/dimitri/pgloader) 迁移 SQLite 到 PostgreSQL:
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-08-13 15:01:32 +08:00
|
|
|
|
# 本地库
|
|
|
|
|
|
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
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
两个数据库独立迁移,不共享迁移历史。
|