解决迁移冲突。
项目描述
django-migration-resolver-hook
Django迁移解析器确保迁移文件始终按顺序排列,不受远程更改的影响。
问题
使用Django的manage.py makemigrations
命令生成的迁移文件可能会出现与远程默认分支不同步的问题,大多数CI服务器可以通过在本地分支合并回基础分支时运行测试来捕获此错误,从而发现重复的迁移节点。
解决方案
此包旨在通过在遍历应用程序的迁移文件时使用双链表来检测重复节点和潜在的冲突,从而解决这个问题。
当检测到重复节点时,使用stat.st_mtime
根据最后修改时间对文件进行排序。
:warning: 这并不总是准确的,可能需要手动检查以验证更改。
注意:当使用CLI命令时,此功能不需要Django的已安装应用程序概念,因为这完全是基于文件的,应该在根/app文件夹内执行。
目录
安装
$ pip install django-migration-resolver-hook
诗篇
poetry add -D django-migration-resolver-hook
使用方法
用例
远程
|--- migrations
|---- ...
|---- 0007_auto_20200112_2328.py # Shared between remote and local repo
|---- 0008_auto_20200113_4328.py # Only exists on remote
|---- 0009_auto_20200114_4532.py
|---- 0010_auto_20200115_1632.py
本地仓库
|--- migrations
|---- ...
|---- 0007_auto_20200112_2328.py # Shared between remote and local repo
|---- 0008_auto_20200114_5438.py # Only exists locally which raises duplicate migration nodes errors.
由于现在与远程分支不同步,需要同步更改
:warning: 不稳定的用法,请改用 静态迁移解析器。
使用自动迁移解析器
CLI命令:auto_migration_resolver
通过提供以下信息自动检测并修复迁移文件:
--app-name
:Django应用程序的app_name。--strategy
:用于解决迁移错误的策略(选项:“reseed”/“inline”)(默认值:“reseed”)--exclude
:应忽略的迁移文件列表。--commit
:在将文件从旧版本移动到新版本后执行git commit
。--verbose
:详细命令执行。
使用方法
$ auto_migration_resolver --app-name my_app --commit --verbose
输出
...
使用静态迁移解析器
CLI命令:migration_resolver
通过提供以下信息修复迁移:
--app-name
:Django应用程序的app_name。--last
:远程的最后一个迁移文件,应该是冲突迁移的种子文件,无论是否有后缀。--conflict
:需要从最后一个迁移文件中重新播种的迁移文件。--commit
:在将文件从旧版本移动到新版本后执行git commit
。--verbose
:详细命令执行。
使用方法
$ migration_resolver --app-name my_app --last 0010_auto_20200115_1632 --conflict 0008_auto_20200114_5438 --commit --verbose
输出
Fixing migrations...
Updating the conflicting migration file 0008_auto_20200114_5438.py
Succefully updated: 0008_auto_20200114_5438.py.
Renaming the migration file from 0008_auto_20200114_5438.py to 0011_auto_20200114_5438.py
Successfully renamed the migration file.
[my-test-branch c18fca41e] Resolved migration conflicts for 0008_auto_20200114_5438.py → 0011_auto_20200114_5438.py
1 file changed, 1 insertion(+), 1 deletion(-)
rename my_app/migrations/{0008_auto_20200114_5438.py => 0011_auto_20200114_5438.py} (99%)
更多选项
$ migration_resolver --help
usage: migration_resolver [-h] [--auto-detect] [--verbose] --app-name APP_NAME --last LAST --conflict CONFLICT [--commit]
Resolve duplicate migration nodes.
optional arguments:
-h, --help show this help message and exit
--auto-detect Auto-detect and fix migration errors. (Not supported)
--verbose Verbose output
--app-name APP_NAME App Name
--last LAST The glob/full name of the final migration file.
--conflict CONFLICT The glob/full name of the final migration file with the conflict.
--commit Commit the changes made.
当远程有迁移文件与本地以前的迁移冲突时,使用vsc(git/mercurial)。
待办事项
- 自动检测并解决迁移节点错误。
- 通过使用异步/等待处理读取节点来加速执行。
- 为已应用的迁移添加数据库unapply迁移支持。
- 如果操作链中存在失败,则支持回滚任何更改。
- 目前VCS支持仅限于git(扩展到mercurial)。
- 记录自动迁移策略。
项目详情
关闭
哈希:django-migration-resolver-hook-0.2.8-py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5961c60eb4ea05a369e0d6d0bad35bc49c31265befc0405c1bc8bd0acbcc2a81 |
|
MD5 | c81e7f4efb0b074b4c03dccf8c0a31b0 |
|
BLAKE2b-256 | f1ffbcdfc021b3d46e89bf1a0ec357d871d89549c3da7cda6a5b06d05c68964e |