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