我应该在 .gitignore 文件中添加 Django 迁移文件吗? [英] Should I be adding the Django migration files in the .gitignore file?

查看:30
本文介绍了我应该在 .gitignore 文件中添加 Django 迁移文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在 .gitignore 文件中添加 Django 迁移文件吗?

Should I be adding the Django migration files in the .gitignore file?

由于迁移冲突,我最近遇到了很多 git 问题,我想知道是否应该将迁移文件标记为忽略.

I've recently been getting a lot of git issues due to migration conflicts and was wondering if I should be marking migration files as ignore.

如果是这样,我将如何添加我的应用程序中的所有迁移,并将它们添加到 .gitignore 文件中?

If so, how would I go about adding all of the migrations that I have in my apps, and adding them to the .gitignore file?

推荐答案

引用自 Django 迁移文档:

每个应用程序的迁移文件都位于该应用程序内的迁移"目录中,旨在提交到其代码库并作为其代码库的一部分进行分发.您应该在开发机器上进行一次迁移,然后在同事的机器、临时机器上运行相同的迁移,最终在生产机器上运行.

The migration files for each app live in a "migrations" directory inside of that app, and are designed to be committed to, and distributed as part of, its codebase. You should be making them once on your development machine and then running the same migrations on your colleagues’ machines, your staging machines, and eventually your production machines.

如果您遵循此流程,您应该不会在迁移文件中遇到任何合并冲突.

If you follow this process, you shouldn't be getting any merge conflicts in the migration files.

在合并版本控制分支时,你仍然可能会遇到基于同一个父迁移有多个迁移的情况,例如如果对不同的开发人员同时引入迁移.解决这种情况的一种方法是引入 merge_migration.通常这可以通过命令自动完成

When merging version control branches, you still may encounter a situation where you have multiple migrations based on the same parent migration, e.g. if to different developers introduced a migration concurrently. One way of resolving this situation is to introduce a merge_migration. Often this can be done automatically with the command

./manage.py makemigrations --merge

这将引入一个新的迁移,该迁移依赖于所有当前的 head 迁移.当然,这只适用于 head 迁移之间没有冲突的情况,在这种情况下,您必须手动解决问题.

which will introduce a new migration that depends on all current head migrations. Of course this only works when there is no conflict between the head migrations, in which case you will have to resolve the problem manually.

鉴于这里有些人建议您不应该将迁移提交到版本控制,我想详细说明您实际上应该这样做的原因.

Given that some people here suggested that you shouldn't commit your migrations to version control, I'd like to expand on the reasons why you actually should do so.

首先,您需要记录应用于生产系统的迁移.如果将更改部署到生产并希望迁移数据库,则需要对当前状态的描述.您可以为应用于每个生产数据库的迁移创建单独的备份,但这似乎不必要地繁琐.

First, you need a record of the migrations applied to your production systems. If you deploy changes to production and want to migrate the database, you need a description of the current state. You can create a separate backup of the migrations applied to each production database, but this seems unnecessarily cumbersome.

其次,迁移通常包含自定义的手写代码.并不总是可以使用 ./manage.py makemigrations 自动生成它们.

Second, migrations often contain custom, handwritten code. It's not always possible to automatically generate them with ./manage.py makemigrations.

第三,迁移应该包括在代码审查中.它们是对您的生产系统的重大更改,并且它们可能会出现很多问题.

Third, migrations should be included in code review. They are significant changes to your production system, and there are lots of things that can go wrong with them.

简而言之,如果您关心您的生产数据,请检查您的迁移到版本控制.

So in short, if you care about your production data, please check your migrations into version control.

这篇关于我应该在 .gitignore 文件中添加 Django 迁移文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆