如何在 django 应用程序的 CI 中处理迁移

los*_*zen 5 git django continuous-integration database-migration

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

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

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

python manage.py migrate
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

迁移(远程)

(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
Run Code Online (Sandbox Code Playgroud)

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

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

yes | python manage.py migrate
Run Code Online (Sandbox Code Playgroud)

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

问候,

tuo*_*mur 5

Django命令有一个--noinput开关可以帮助解决这种情况。Migrate 也支持它(请参阅python manage.py migrate --help)