如何在Google Cloud Platform上进行Django迁移? [英] How to make Django migrations on Google Cloud Platform?

查看:89
本文介绍了如何在Google Cloud Platform上进行Django迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的应用托管在Google App Engine上,并拥有一个用于数据库的Google SQL实例。该应用程序的主要部分是使用Django Rest Framework构建的API。当我需要修改数据库的架构时,我询问了Google Cloud Support有关在生产环境中进行迁移的最佳做法。由于我是Web开发的新手,也许这里的任何专家都具有类似的经验,并且可以验证建议的过程是否是我真正可以遵循的?

I would like to host my app on Google App Engine and have a Google SQL instance for the database. The main part of the app is an API built with Django Rest Framework. I asked Google Cloud Support about the best practices of making migrations in production when I need to modify the database's schema. Since I am new to web development, perhaps any experts here have similar experience and can verify if the suggested process is something what I can really follow?


有关数据库迁移的最佳做法,您可以为Cloud SQL数据库创建一个单独的开发/测试/备份实例。举例来说,假设您要生产的数据库实例是DB1,则创建一个具有DB1所有表的dev实例(即DB2)。之后,将您的应用程序配置为临时指向DB2实例。请确保两个实例都同步并更新。然后,部署指向DB2的应用程序的新版本,以便您可以将DB1(添加新表,列)更新为生产环境中的正式数据库实例。然后,您可以再次将其指向DB1并更新DB2。

For database migration best practice, you could create a separate development/test/backup instance of your Cloud SQL database. Let's say for example your DB instance for production is DB1, create a dev instance namely DB2 that has all the tables of DB1. After that, configure your app to point DB2 instance temporarily. Please make sure that both instances are in sync and updated. Then, deploy a new version of your app that points to DB2 so you can update DB1 (add new tables, columns) as your official database instance in the production environment. Then you can point it again to DB1 and update DB2.


推荐答案

这确实是一个好习惯还有第二个CloudSQL执行迁移。我建议:

That's a good practice indeed to have a second CloudSQL to perform your migration onto. I'd suggest to:


  1. 部署新的App Engine版本,该版本使用新的数据库架构,但尚未将流量定向到该数据库(使用 gcloud应用程序部署--no-promote

  2. 克隆您的CloudSQL实例以创建一个新实例,您将在其中运行迁移

  3. 在您的本地计算机上,配置 Cloud SQL代理指向这个新的CloudSQL实例并运行 python manage.py migration

  4. 完成后,直接将流量引至新的App Engine版本。

  1. Deploy the new App Engine version which uses the new db schema but don't direct traffic to it yet (use gcloud app deploy --no-promote)
  2. Clone your CloudSQL instance to create a new one where you'll run the migration
  3. On your local machine, configure the Cloud SQL Proxy to point to this new CloudSQL instance and run python manage.py migrate
  4. Once the migration has finished, direct traffic to the new App Engine version.

在生产环境中,新的CloudSQL实例将缺少写入第一个实例的数据您在执行步骤2到步骤4时的实例。完全避免这种情况的最简单解决方案是在迁移过程中停止您的App Engine应用。但是,如果您不能承受一些停机时间,则需要在克隆后跟踪对第一个实例所做的更改,并将这些更改手动应用于新实例。

In a production environment your new CloudSQL instance will lack the data written to the first instance while you were performing steps 2 to 4. The easiest solution to avoid this situation altogether is to stop your App Engine app during the migration. If you cannot afford some downtime though, you'll need to track the changes made to the first instance after you've cloned it and apply these changes manually to the new instance.

这篇关于如何在Google Cloud Platform上进行Django迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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