【问题标题】:Adding Migrations to Source Control and Merging Conflicting Migrations in Django将迁移添加到源代码管理并合并 Django 中的冲突迁移
【发布时间】:2021-02-20 17:13:53
【问题描述】:

我接手了一个 Django 项目,发现 Git 没有跟踪各种应用程序的迁移。这似乎有点问题,因为我的理解是应该始终跟踪迁移。这几乎是共识还是有理由不这样做?

问题的第二部分与我做了一些涉及在本地调整一些迁移的工作有关。我现在想将更改推送到生产环境。但是,我不确定合并可能的冲突的最佳方法是什么。

例如,在生产中,我有以下内容:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    mysite/aldryn_forms/migrations/0019_auto_20200730_1455.py
    mysite/apps/common/migrations/0016_auto_20200624_2028.py
    mysite/apps/common/migrations/0016_auto_20200625_1125.py
    mysite/apps/common/migrations/0017_merge_20200625_1129.py
    mysite/apps/common/migrations/0018_auto_20200720_1743.py
    mysite/apps/payment/migrations/0005_auto_20200624_2028.py
    mysite/apps/payment/migrations/0005_auto_20200625_1125.py
    mysite/apps/payment/migrations/0006_merge_20200625_1129.py
    mysite/apps/payment/migrations/0007_auto_20200720_1743.py
    mysite/apps/payment/migrations/0008_paymentmodel_course.py
    mysite/apps/payment/migrations/0009_paymentmodel_user.py
    mysite/apps/plugins/migrations/0016_auto_20200624_2028.py
    mysite/apps/plugins/migrations/0016_auto_20200625_1125.py
    mysite/apps/plugins/migrations/0017_merge_20200625_1129.py
    mysite/apps/xyz/migrations/0005_auto_20200730_1455.py

在本地,我有以下内容:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    mysite/aldryn_forms/migrations/0019_auto_20201108_1623.py
    mysite/apps/common/migrations/0016_auto_20201108_1623.py
    mysite/apps/common/migrations/0017_auto_20201108_1806.py
    mysite/apps/payment/migrations/0005_auto_20201108_1623.py
    mysite/apps/plugins/migrations/0016_auto_20201108_1623.py
    mysite/apps/xyz/migrations/0005_auto_20201108_1623.py

这些是自定义工作的文件:

mysite/apps/common/migrations/0016_auto_20201108_1623.py
mysite/apps/common/migrations/0017_auto_20201108_1806.py

似乎生产服务器上存在的所有迁移都已应用于生产数据库。因此,我得出结论,它们正确地描述了生产数据库的状态。

我应该怎么做才能将我的本地作品与当前正在生产的作品同步?

【问题讨论】:

    标签: django django-models migration django-migrations


    【解决方案1】:

    首先,我将检查您的新迁移和生产中应用的迁移中的冲突代码。

    然后备份所有内容(您的站点和生产)。

    如果您没有发现逻辑错误,请尝试将迁移文件从生产环境复制到您的站点并使用:

    python manage.py makemigrations –merge
    python manage.py migrate
    

    如果它有效,那么它将在生产中正常工作。 有关于它的文章: https://www.algotech.solutions/blog/python/django-migrations-and-how-to-manage-conflicts/#:~:text=So%2C%20in%20order%20to%20allow,manage.py%20makemigrations%20%E2%80%93merge)

    一旦我也拥有它......令人讨厌的是你将不得不这样做 - 合并每个新的迁移。 在某些时候,我会重置迁移。

    什么时候你会整理好数据库。你可以这样做:

    • 备份数据库并从数据库表中删除插入:迁移
    • 从项目目录中删除所有迁移文件
    • qoute urls.py 中的所有模式
    • 进行 makemigrations 并迁移
    • 用旧数据填充新数据库(迁移表除外)

    哦,使用 --fake 有更简单的方法: https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html

    这将使您进行初始迁移。

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 2011-09-04
      • 2021-09-20
      • 2019-03-30
      • 2019-11-23
      • 2017-08-22
      • 2013-06-02
      • 1970-01-01
      • 2014-09-17
      相关资源
      最近更新 更多