【问题标题】:How to handle migrations in CI of a django application如何在 django 应用程序的 CI 中处理迁移
【发布时间】:2015-11-17 10:58:05
【问题描述】:

我们有一个用于 django 应用程序的 git 持续集成环境。

有时我们需要对模型进行修改并迁移数据库中的更改(pre/pro)。

在我们的钩子中我们这样做:

python manage.py migrate

获取之前在我们本地开发环境中创建的迁移并应用它们。但我注意到有时迁移会要求用户交互。即:

makemigrations(本地开发)

(project-name-develop)lostcitizen@project-name:~/dev/project-name/project-name.git$ ./manage.py makemigrations
Did you rename publication.writer to publication.author (a ForeignKey)? [y/N] y
Migrations for 'publications':
  0017_auto_20151117_1050.py:
    - Rename field writer on publication to author

迁移(远程)

(project-name-develop)lostcitizen@project-name:~/dev/project-name/project-name.git$ ./manage.py migrate
Skipping creation of NoticeTypes as notification app not found
Operations to perform:
  Synchronize unmigrated apps: landing, about, publications, [...]
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying publications.0016_auto_20151116_1650... OK
  Applying publications.0017_auto_20151117_1050... OK
The following content types are stale and need to be deleted:

    publications | hashtag

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel: yes

我的问题是如何处理这对于自动化部署是“安全的”?我可以手动完成,但我想将其从开发人员的其他工作流程中抽象出来。

我认为在 git hook 中要求用户交互是不可能的,所以我想使用这个:

yes | python manage.py migrate

但我认为使用它并不安全。你怎么看?

问候,

【问题讨论】:

    标签: django git continuous-integration database-migration


    【解决方案1】:

    Django commands 有一个 --noinput 开关来帮助解决这种情况。 Migrate 也支持它(见python manage.py migrate --help)

    【讨论】:

    • 我知道收集静态文件的参数,但我不知道它也可用于迁移。谢谢!
    【解决方案2】:

    您可以使用--noinput 选项来禁止任何用户提示:

    python manage.py migrate --noinput
    

    这将为每个提示选择最安全的选项。

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2016-04-14
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 2013-05-27
      • 2019-07-23
      • 2021-09-28
      相关资源
      最近更新 更多