压缩后如何删除 django 迁移?

How to delete django migrations after squashing them?

Django 文档说我们可以在压缩后删除迁移:

You should commit this migration but leave the old ones in place; the new migration will be used for new installs. Once you are sure all instances of the code base have applied the migrations you squashed, you can delete them.

这里的删除是只删除迁移文件,还是把django_migrationstable里面的条目也一起删除?

这里是一些背景:我只有开发机器,所以只有一个代码库。在压缩了一些我已经应用的迁移之后,我删除了文件和数据库条目。通过进行迁移测试这是否可以,但没有找到任何东西。所以,一切看起来都很好。第二天,我不得不改变一些东西,并进行了迁移。当我尝试迁移时,它也尝试应用压缩的迁移(在压缩之前部分应用)。因此,我不得不返回并重新创建 django_migrations table 中的条目。所以,看来我必须保留数据库条目。在我再次搞砸任何事情之前,我试图确定,并首先理解为什么它看起来很好,然后尝试应用压缩的迁移。

我无论如何都不是专家,但我只是压缩了我的迁移,并最终做了以下事情:

运行 此查询删除旧迁移(压缩)

DELETE FROM south_migrationhistory;

运行 此管理命令用于删除幻影迁移

./manage.py migrate --fake --delete-ghost-migrations 

Django 1.7 也有 squashmigrations

压缩的迁移永远不会标记为已应用,这将在 1.8.3 中修复(参见 #24628)。

删除旧迁移的步骤是:

  1. 确保应用所有替换的迁移(或其中的 none)。
  2. 删除旧的迁移文件,从压缩的迁移中删除 replaces 属性。
  3. (解决方法)运行 ./manage.py migrate <app_label> <squashed_migration> --fake.

当 1.8.3 到来时,最后一步将不再是必需的。

自问题发布以来,转换压缩的迁移变得更加容易。我发布了一个 small sample project 展示了如何压缩具有循环依赖的迁移,它还展示了如何在所有安装都迁移到压缩点之后将压缩的迁移转换为常规迁移。

正如 Django documentation 所说:

You must then transition the squashed migration to a normal migration by:

  • Deleting all the migration files it replaces.
  • Updating all migrations that depend on the deleted migrations to depend on the squashed migration instead.
  • Removing the replaces attribute in the Migration class of the squashed migration (this is how Django tells that it is a squashed migration).