81 lines
1.5 KiB
Markdown
81 lines
1.5 KiB
Markdown
|
|
# 数据库迁移策略
|
|||
|
|
|
|||
|
|
> 所属模块:[数据库切换支持](./概览.md)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 1. 迁移命令
|
|||
|
|
|
|||
|
|
### SQLite
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 添加迁移
|
|||
|
|
dotnet ef migrations add InitialCreate --output-dir Migrations/SQLite
|
|||
|
|
|
|||
|
|
# 应用迁移
|
|||
|
|
dotnet ef database update
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### PostgreSQL
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 切换配置后添加迁移
|
|||
|
|
dotnet ef migrations add InitialCreate --output-dir Migrations/PostgreSQL -- --environment PostgreSQL
|
|||
|
|
|
|||
|
|
# 应用迁移
|
|||
|
|
dotnet ef database update -- --environment PostgreSQL
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 2. 切换步骤
|
|||
|
|
|
|||
|
|
### 从 SQLite 切换到 PostgreSQL
|
|||
|
|
|
|||
|
|
| 步骤 | 操作 | 说明 |
|
|||
|
|
|------|------|------|
|
|||
|
|
| 1 | 修改配置 | `appsettings.json` 中 `Database:Type` 改为 `postgresql` |
|
|||
|
|
| 2 | 设置连接 | 配置 `Host`, `Port`, `Name`, `User`, `Password` |
|
|||
|
|
| 3 | 创建数据库 | `CREATE DATABASE wctminer;` |
|
|||
|
|
| 4 | 应用迁移 | `dotnet ef database update` |
|
|||
|
|
| 5 | 数据迁移 | 使用工具迁移 SQLite 数据到 PostgreSQL |
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 3. 配置文件示例
|
|||
|
|
|
|||
|
|
### SQLite (默认)
|
|||
|
|
|
|||
|
|
```json
|
|||
|
|
{
|
|||
|
|
"Database": {
|
|||
|
|
"Type": "sqlite",
|
|||
|
|
"Path": "./data/database/wctminer.db"
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### PostgreSQL
|
|||
|
|
|
|||
|
|
```json
|
|||
|
|
{
|
|||
|
|
"Database": {
|
|||
|
|
"Type": "postgresql",
|
|||
|
|
"Host": "localhost",
|
|||
|
|
"Port": 5432,
|
|||
|
|
"Name": "wctminer",
|
|||
|
|
"User": "wctminer_user",
|
|||
|
|
"Password": "${DB_PASSWORD}"
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 4. 数据迁移工具
|
|||
|
|
|
|||
|
|
推荐使用 [pgloader](https://github.com/dimitri/pgloader) 迁移 SQLite 到 PostgreSQL:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
pgloader ./data/database/wctminer.db postgresql://user:password@localhost:5432/wctminer
|
|||
|
|
```
|