python - 在django中,mergemigrations和squashmigrations有什么区别?

标签 python django bitbucket pipeline bitbucket-pipelines

我们什么时候应该使用合并迁移 --merge/merge 迁移,什么时候使用squashmigrations 我们可以通过管道执行这些命令,而不是团队中的每个开发人员手动执行吗?

最佳答案

让我们假设迁移文件夹具有以下迁移。

./foo
    ./migrations
        0001_initial.py
        0002_userprofile.py
        0003_article.py
        0004_auto_add.py

以上是目前为止订购的吗?当我们团队合作时。

  • Developer1 致力于文章功能,并创建了迁移0005_add_name_in_article.py
  • Developer2 致力于userprofile 功能,并创建了 0005_add_mobile_in_userprofile.py
  • Developer3 致力于类别功能,并创建了0005_category.py

现在情况已经发生

  • Developer1 会将代码合并到 develop 分支,迁移文件将为
./foo
    ./migrations
        0001_initial.py
        0002_userprofile.py
        0003_article.py
        0004_auto_add.py
        0005_add_name_in_article.py
  • Developer2会将代码合并到develop分支,迁移文件将为
./foo
    ./migrations
        0001_initial.py
        0002_userprofile.py
        0003_article.py
        0004_auto_add.py
        0005_add_name_in_article.py
        0005_add_mobile_in_userprofile.py

这里,最后两个迁移具有相同的依赖关系,这就破坏了迁移的顺序,而Developer2在合并develop分支django后迁移迁移将提供合并迁移的点击。运行该命令后,Django 将创建一个新的迁移 0006_merge_add_name_in_article_add_mobile_in_userprofile.py,它合并了 0005_add_name_in_article.py0005_add_mobile_in_userprofile.py
该文件包含

  dependencies = [
        ('app_name', '0005_add_name_in_article'),
        ('app_name', '0005_add_mobile_in_userprofile'),
    ]

现在 Developer2 本地分支有以下迁移。

./foo
    ./migrations
        0001_initial.py
        0002_userprofile.py
        0003_article.py
        0004_auto_add.py
        0005_add_name_in_article.py
        0005_add_mobile_in_userprofile.py
        0006_merge_add_name_in_article_add_mobile_in_userprofile.py

现在,Developer2 将提出 PR 并合并,以便另一个开发说明发生冲突。

  • Developer3 将采用相同的流程来避免迁移冲突。

什么是“合并迁移”?

合并迁移的主要目的是在以协作方式工作的同时减少数据库级别的冲突。它将生成新的迁移文件来管理迁移顺序。

什么是“ Squash 迁移”?

您可能会想到一个问题:迁移时会创建多少不必要的文件?”

  • 合并迁移文件中,没有进行任何操作。
  • 可能有一个表,您在其中添加了一列,然后将其删除,然后将其重新添加到可能创建不同迁移的表中 压缩迁移的主要目的是优化迁移文件。但在压缩迁移之前,请参阅docs因为在某些情况下,这可能很麻烦或引发迁移冲突。

我们可以通过管道执行这些命令,而不是团队中的每个开发人员手动执行吗?

不,根据上述合并和压缩迁移的用例,我们无法在管道上执行此操作,因为开发人员有责任保持迁移有序,并且根据 django 社区,所有迁移都应该在团队中同步,这样我们就可以不要在管道中执行此操作。

关于python - 在django中,mergemigrations和squashmigrations有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73862112/

相关文章:

python - 如何让 Python 输入循环读取和更新列表项

Python3 - 我在这里做错了什么?

python - 将数据框字典合并为 1 个数据框

html - 在 django 服务器中运行时 html 模板的 css 问题

django - 使用 Django 1.7 中的 AppConfig 就绪方法在 Django 启动时加载静态数据

windows - Bitbucket、Windows 和 "fatal: could not read Password for"

python - 我怎样才能在Python中分割这个字符串

Django:如何获取正在渲染的模板的名称

git - Bitbucket 的 pull 请求模板

git - 右键单击项目不显示 Git 菜单