使用 Postgres BDR 9.4.1 迁移 Django 1.8

Django 1.8 Migration with Postgres BDR 9.4.1

我正在尝试 运行 使用 BDR 在 Postgres 数据库上迁移 Django。

python manage.py makemigrations

工作正常,但是 运行宁

python manage.py migrate

导致以下错误:

ALTER TABLE … ALTER COLUMN TYPE … may only affect UNLOGGED or TEMPORARY tables when BDR is active; auth_permission is a regular table

有问题的模块是 django/django/contrib/auth/migrations/0002_alter_permission_name_max_length.py

我没有找到任何关于如何使用 Django UNLOGGED tables 的信息,特别是因为 auth_permissions 是一个 Django table(不是我创建的)。我也不确定 UNLOGGED tables 是否会复制。

有人有什么建议吗?

为了使用 BDR 进行迁移,您需要手动创建迁移,仅使用 "safe" 操作,因为 BDR 当前无法复制诸如迁移中的操作。

在我最近与 2nd Quadrant(BDR 开发的主要发起人)的支持人员进行的电子邮件对话中,我获得了有关该主题的以下信息:

There is no timeline in delivering this. It's very hard to accomplish.

You can still alter a column's type, it just takes multiple steps. In general, you do DDL in BDR as if you were doing it with a lock-avoidance approach in stock PostgreSQL. So in this case you:

  • ADD the new column without a DEFAULT and without any NOT NULL, and commit. If needed, also create a trigger to automatically fill the new column when values are inserted. Or ALTER the new column to set a DEFAULT, if that's more appropriate.
  • UPDATE the table to copy values to the new column with new type
  • ALTER the table to make it NOT NULL if appropriate
  • DROP the old column

我还发现 this article from BrainTree 是一个很好的参考,可以说明什么可以被视为 "safe" 操作,以及您必须如何重写迁移中的行为。

您可以使用 MIGRATION_MODULES 设置覆盖授权应用程序的内置迁移,例如:

MIGRATION_MODULES = {
    'auth': 'bdr_migrations.auth',
}

然后将迁移文件从 django 包复制到 /project/bdr_migrations/auth/ 并调整它们以匹配 BDR 限制。