数据库迁移

数据库迁移

M-Doc 使用 Goose 进行数据库版本管理,支持自动迁移。


自动迁移

默认情况下,M-Doc 启动时会自动执行数据库迁移:

migration:
  enabled: true                    # 启动时自动迁移
  dir: "."                         # 迁移文件目录
  table_name: "schema_migrations"  # 版本追踪表
  halt_on_failure: true            # 迁移失败是否停止启动

halt_on_failure

  • true(推荐):迁移失败时停止启动,防止应用在不完整的数据库上运行
  • false:迁移失败时记录错误但继续启动

迁移历史

当前包含以下迁移版本:

版本 说明
001 初始表结构(users, organizations, documents, articles 等)
002 文章提交哈希(commit_hash)
003 API Keys 表
004 文章摘要字段
005 个人访问令牌(PAT)
006 文档级 Embedding 配置
007 文章向量表(pgvector)

跨数据库注意事项

特性 PostgreSQL MySQL SQLite
自动迁移
向量搜索 ✅(pgvector)
生产推荐
  • SQLite 仅建议用于开发和测试环境
  • 向量搜索功能(pgvector)仅支持 PostgreSQL
  • 从 MySQL 迁移到 PostgreSQL 需要手动导出导入数据

手动迁移

如需禁用自动迁移并手动执行:

migration:
  enabled: false

然后使用 Goose CLI 手动执行:

goose -dir migrations postgres "host=localhost user=mdoc dbname=mdoc sslmode=disable" up