部署到 Elastic Beanstalk 时运行 Django 迁移 [英] Running Django migrations when deploying to Elastic Beanstalk

查看:29
本文介绍了部署到 Elastic Beanstalk 时运行 Django 迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Elastic Beanstalk 上设置了我的 Django 应用程序,最近对数据库进行了更改,我想现在将其应用于实时数据库.我知道我需要将其设置为容器命令,在检查数据库后,我可以看到迁移已运行,但我不知道如何对迁移进行更多控制.例如,我只希望在必要时运行迁移,但根据我的理解,假设命令仍列在配置文件中,容器将在每次部署时运行迁移.此外,有时,我会在迁移过程中获得选项,例如:

I have my Django app set up on Elastic Beanstalk and recently made a change to the DB that I would like to have applied to the live DB now. I understand that I need to set this up as a container command, and after checking the DB I can see that the migration was run, but I can't figure out how to have more controls over the migration. For example, I only want a migration to run when necessary but from my understanding, the container will run the migration on every deploy assuming the command is still listed in the config file. Also, on occassion, I will be given options during a migration such as:

Any objects realted 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'

如何设置容器命令以在部署阶段使用 yes 响应此问题?

How do I set up the container command to respond to this with a yes during the deployment phase?

这是我当前的配置文件

container_commands:
  01_migrate:
    command: 'source /opt/python/run/venv/bin/actiate && python app/manage.py makemigrations'
    command: 'source /opt/python/run/venv/bin/activate && python app/manage.py migrate'

有没有办法将这 2 个命令设置为仅在必要时运行并响应我在迁移期间收到的是/否选项?

Is there a way to set these 2 commands to only run when necessary and to respond to the yes/no options I receive during a migration?

推荐答案

我不确定是否有特定的方式来回答是或否.但是您可以将 --noinput 附加到您的容器命令中.使用 --noinput 选项抑制所有用户提示,例如你确定吗?"确认消息.

I'm not sure there is a specific way to answer yes or no. but you can append --noinput to your container command. Use the --noinput option to suppress all user prompting, such as "Are you sure?" confirmation messages.

try
    command: 'source /opt/python/run/venv/bin/activate && python app/manage.py migrate --noinput'

或..您可以通过 ssh 连接到您的 elasticbean 实例并手动运行您的命令.这样您就可以更好地控制迁移.

OR.. You can ssh into your elasticbean instance and run your command manually. Then you'll have more control over the migrations.

  1. 使用 pip install awsebcli 安装 awsebcli
  2. 输入 eb ssh 你的环境名称
  3. 使用以下命令导航到您的 eb 实例应用程序目录:
  1. Install awsebcli with pip install awsebcli
  2. Type eb ssh Your EnvironmentName
  3. Navigate to your eb instance app directory with:

<小时>

  • sudo -s
  • source/opt/python/run/venv/bin/activate
  • 源/opt/python/current/env
  • cd/opt/python/current/app


    • sudo -s
    • source /opt/python/run/venv/bin/activate
    • source /opt/python/current/env
    • cd /opt/python/current/app

      然后运行您的命令.

      ./manage.py 迁移

      ./manage.py migrate

      希望能帮到你

      这篇关于部署到 Elastic Beanstalk 时运行 Django 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆