hyp migrate.md - maoxiaoyue/hypgo GitHub Wiki

hyp migrate — 資料庫 Migration 工具

掃描 Go Model struct(bun ORM tag),比對快照,自動產生 up/down SQL migration。

用法

hyp migrate diff                    # PostgreSQL migration
hyp migrate diff --dialect mysql    # MySQL migration
hyp migrate diff -o db/migrations/  # 自訂輸出目錄
hyp migrate snapshot                # 儲存當前 schema 快照

子命令

diff

比對當前 model 與快照,產生 SQL migration 檔案。

hyp migrate diff [flags]
Flag 說明 預設值
-d, --dialect SQL 方言:postgresmysql postgres
-o, --output 輸出目錄 migrations/
-s, --snapshot 快照檔案路徑 .hyp/schema_snapshot.json

偵測的變更類型:

類型 說明
AddTable 新增 model struct
DropTable 移除 model struct
AddColumn struct 新增欄位
DropColumn struct 移除欄位
AlterColumn 欄位型別或約束變更

支援的 bun tag:pkautoincrementnotnulluniquetypedefault

snapshot

儲存當前 model schema 到快照檔案,作為未來 diff 的基線。

hyp migrate snapshot [flags]
Flag 說明 預設值
-s, --snapshot 快照檔案路徑 .hyp/schema_snapshot.json

工作流

# 1. 定義/修改 model struct(含 bun tag)
# 2. 產生 migration
hyp migrate diff
# 3. 檢視產生的 SQL
cat migrations/*_up.sql
cat migrations/*_down.sql
# 4. 套用 SQL 到資料庫
# 5. 快照自動更新(diff 完成後自動儲存)

範例輸出

Detected 2 change(s):
  • AddColumn: users.bio (TEXT)
  • AlterColumn: users.name VARCHAR(100) → VARCHAR(255)

Migration files generated:
  UP:   migrations/20260403_143000_up.sql
  DOWN: migrations/20260403_143000_down.sql
Snapshot updated: .hyp/schema_snapshot.json

相關命令

詳細文檔