- 導入 Prisma 7.9.1 + SQLite(better-sqlite3 driver adapter),prisma.config.ts 管理連線設定 - 完整資料模型:User/Work/Character/CharacterAlias/EpisodicMemory/SemanticMemory/ ProceduralRule/EmotionState/Relationship/SentimentLedgerEntry - packages/db:Prisma Client 存取層(ESM,因 Prisma 7 產出的 generated client 僅支援 ESM) - apps/api 隨之改為 ESM 以相容 packages/db - packages/shared:新增角色/記憶/情緒/關係共用型別,api 與 web 皆從此匯入 - prisma/seed.ts:建立測試使用者與元氣型測試角色,含完整關係帳本與記憶種子資料 - apps/api:新增 GET /characters - scripts/smoke/B.mjs:驗證資料表齊全、API 讀出種子角色、關係與記憶可寫入讀出 - 保留 Prisma 官方隨 CLI 附的 agent skill 文件(.agents/skills 等),供後續群組查閱 npm run restart && npm run smoke -- B 皆通過(B-V),A 群組冒煙測試無回歸。
128 lines
2.6 KiB
Markdown
128 lines
2.6 KiB
Markdown
# prisma migrate deploy
|
|
|
|
Applies pending migrations in production/staging environments.
|
|
|
|
## Command
|
|
|
|
```bash
|
|
prisma migrate deploy
|
|
```
|
|
|
|
## What It Does
|
|
|
|
- Applies all pending migrations from `prisma/migrations/`
|
|
- Updates `_prisma_migrations` table
|
|
- Does NOT generate new migrations
|
|
- Does NOT run seed scripts
|
|
- Safe for CI/CD and production
|
|
|
|
## Options
|
|
|
|
| Option | Description |
|
|
|--------|-------------|
|
|
| `--schema` | Custom path to your Prisma schema |
|
|
| `--config` | Custom path to your Prisma config file |
|
|
|
|
## When to Use
|
|
|
|
- Production deployments
|
|
- Staging environments
|
|
- CI/CD pipelines
|
|
- Any non-development environment
|
|
|
|
## Examples
|
|
|
|
### Basic deployment
|
|
|
|
```bash
|
|
prisma migrate deploy
|
|
```
|
|
|
|
### In CI/CD pipeline
|
|
|
|
```yaml
|
|
# GitHub Actions example
|
|
- name: Apply migrations
|
|
run: npx prisma migrate deploy
|
|
env:
|
|
DATABASE_URL: ${{ secrets.DATABASE_URL }}
|
|
```
|
|
|
|
### Docker deployment
|
|
|
|
```dockerfile
|
|
# Run migrations before starting app
|
|
CMD npx prisma migrate deploy && node dist/index.js
|
|
```
|
|
|
|
## Comparison with migrate dev
|
|
|
|
| Feature | migrate dev | migrate deploy |
|
|
|---------|-------------|----------------|
|
|
| Creates migrations | Yes | No |
|
|
| Applies migrations | Yes | Yes |
|
|
| Detects drift | Yes | No |
|
|
| Prompts for input | Yes | No |
|
|
| Uses shadow database | Yes | No |
|
|
| Safe for production | No | Yes |
|
|
| Resets on issues | Prompts | Fails |
|
|
|
|
## Production Workflow
|
|
|
|
1. **Development**: Create migrations locally
|
|
```bash
|
|
prisma migrate dev --name add_feature
|
|
```
|
|
|
|
2. **Commit**: Include migration files in version control
|
|
```bash
|
|
git add prisma/migrations
|
|
git commit -m "Add feature migration"
|
|
```
|
|
|
|
3. **Deploy**: Apply in production
|
|
```bash
|
|
prisma migrate deploy
|
|
```
|
|
|
|
## Error Handling
|
|
|
|
### Failed migration
|
|
|
|
If a migration fails, `migrate deploy` exits with error. The failed migration is marked as failed in `_prisma_migrations`.
|
|
|
|
To fix:
|
|
1. Resolve the issue (fix SQL, database state, etc.)
|
|
2. Mark as resolved: `prisma migrate resolve --applied <migration_name>`
|
|
3. Re-run: `prisma migrate deploy`
|
|
|
|
### Check status first
|
|
|
|
```bash
|
|
prisma migrate status
|
|
```
|
|
|
|
Shows pending and applied migrations before deploying.
|
|
|
|
## Configuration
|
|
|
|
Ensure `prisma.config.ts` has the production database URL:
|
|
|
|
```typescript
|
|
import 'dotenv/config'
|
|
import { defineConfig, env } from 'prisma/config'
|
|
|
|
export default defineConfig({
|
|
datasource: {
|
|
url: env('DATABASE_URL'),
|
|
},
|
|
})
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
1. Always run `migrate status` before `migrate deploy` in CI
|
|
2. Have a rollback plan (backup before migrations)
|
|
3. Test migrations in staging first
|
|
4. Never use `migrate dev` in production
|