简体   繁体   English

如何在 Google Cloud Platform 上进行 Django 迁移?

[英]How to make Django migrations on Google Cloud Platform?

I would like to host my app on Google App Engine and have a Google SQL instance for the database.我想在 Google App Engine 上托管我的应用程序并拥有一个用于数据库的 Google SQL 实例。 The main part of the app is an API built with Django Rest Framework.该应用程序的主要部分是使用 Django Rest Framework 构建的 API。 I asked Google Cloud Support about the best practices of making migrations in production when I need to modify the database's schema.当我需要修改数据库的架构时,我向 Google Cloud 支持人员询问了在生产中进行迁移的最佳实践。 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?由于我是 Web 开发的新手,也许这里的任何专家都有类似的经验,并且可以验证建议的过程是否是我真正可以遵循的?

For database migration best practice, you could create a separate development/test/backup instance of your Cloud SQL database.对于数据库迁移最佳实践,您可以为 Cloud SQL 数据库创建单独的开发/测试/备份实例。 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.例如,假设您的生产数据库实例是 DB1,创建一个开发实例 DB2,其中包含 DB1 的所有表。 After that, configure your app to point DB2 instance temporarily.之后,将您的应用程序配置为临时指向 DB2 实例。 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.然后,部署指向 DB2 的应用程序的新版本,以便您可以将 DB1(添加新表、列)更新为生产环境中的官方数据库实例。 Then you can point it again to DB1 and update DB2.然后您可以再次将其指向 DB1 并更新 DB2。

That's a good practice indeed to have a second CloudSQL to perform your migration onto.使用第二个 CloudSQL 来执行迁移确实是一个很好的做法。 I'd suggest to:我建议:

  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 )部署新的 App Engine 版本,该版本使用新的 db 架构,但尚未将流量定向到它(使用gcloud app deploy --no-promote
  2. Clone your CloudSQL instance to create a new one where you'll run the migration克隆您的 CloudSQL 实例以创建一个新实例,您将在其中运行迁移
  3. On your local machine, configure the Cloud SQL Proxy to point to this new CloudSQL instance and run python manage.py migrate在您的本地机器上,将Cloud SQL 代理配置为指向这个新的 CloudSQL 实例并运行python manage.py migrate
  4. Once the migration has finished, direct traffic to the new App Engine version.迁移完成后,将流量定向到新的 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.在生产环境中,您的新 CloudSQL 实例将缺少在您执行步骤 2 到 4 时写入第一个实例的数据。完全避免这种情况的最简单解决方案是在迁移期间停止您的 App Engine 应用程序。 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.但是,如果您无法承受一些停机时间,则需要在克隆第一个实例后跟踪对它所做的更改,并将这些更改手动应用于新实例。

I think an alternative to the proposed solution would be to create a read replica and direct your app traffic to the read replica while performing such migrations on the write instance.我认为建议的解决方案的替代方案是创建一个只读副本并将您的应用程序流量定向到只读副本,同时在写入实例上执行此类迁移。

Note this requires fast migrations.请注意,这需要快速迁移。

It could also be a nightly operation or during low traffic periods where the site can be put into maintenance.它也可以是夜间操作或在站点可以进行维护的低流量期间。

For more high available apps you might want to look at https://www.braintreepayments.com/blog/ruby-conf-australia-high-availability-at-braintree/对于更多可用的应用程序,您可能需要查看https://www.braintreepayments.com/blog/ruby-conf-australia-high-availability-at-braintree/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM