跳转到主要内容

解决迁移冲突。

项目描述

PyPI version Total alerts Language grade: Python

django-migration-resolver-hook

Django迁移解析器确保迁移文件始终按顺序排列,不受远程更改的影响。

问题

使用Django的manage.py makemigrations命令生成的迁移文件可能会出现与远程默认分支不同步的问题,大多数CI服务器可以通过在本地分支合并回基础分支时运行测试来捕获此错误,从而发现重复的迁移节点。

解决方案

此包旨在通过在遍历应用程序的迁移文件时使用双链表来检测重复节点和潜在的冲突,从而解决这个问题。

当检测到重复节点时,使用stat.st_mtime根据最后修改时间对文件进行排序。

:warning: 这并不总是准确的,可能需要手动检查以验证更改。

注意:当使用CLI命令时,此功能不需要Django的已安装应用程序概念,因为这完全是基于文件的,应该在根/app文件夹内执行。

目录

  1. 安装

  2. 使用方法

    一、用例

    二、使用自动迁移解析器

    三、使用静态迁移解析器

安装

$ 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.tar.gz (10.7 kB 查看哈希)

上传时间:

构建分发

django_migration_resolver_hook-0.2.8-py3-none-any.whl (14.3 kB 查看哈希)

上传时间: Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面