在 App Engine 上将 Cloud SQL for PostgreSQL 与 Rails 7 搭配使用

开始开发在 App Engine 柔性环境中运行的 Ruby on Rails 应用。 您创建的应用将在支持所有 Google 产品的同一基础架构上运行,因此您可以放心,这些应用能顺畅扩容,以满足所有用户(无论是几个还是几百万个)的需求。

本教程假设您熟悉 Rails 网页开发,并将引导您 在新的 Rails 应用中设置 Cloud SQL for PostgreSQL 。此外,本教程还提供了有关如何将现有 Rails 应用配置为使用 Cloud SQL for PostgreSQL 的参考信息。

本教程支持并需要 Ruby 3.0 或更高版本。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建一个账号,以便在 真实场景中评估我们产品的性能。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud SQL Admin API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. 安装 Google Cloud CLI。

  6. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  7. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud SQL Admin API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  11. 安装 Google Cloud CLI。

  12. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  13. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

准备 Cloud SQL for PostgreSQL 实例

为本教程设置 Cloud SQL for PostgreSQL 实例。

  1. 创建 PostgreSQL 实例。 在本教程中,该实例的名称为 rails-cloudsql-instance

  2. 在该实例中创建数据库。 在本教程中,生产数据库的名称为 cat_list_production

  3. 为实例设置 postgres 用户密码

为 Rails 设置本地环境

要为本教程设置本地环境,请执行以下操作:

  1. 安装 Ruby 3.0 或更高版本。

  2. 安装 Rails 7 gem。

  3. 安装 Bundler gem。

如需详细了解如何安装 Rails 及其依赖项,请参阅 官方 Rails 入门 指南。

满足前提条件后,您可以使用 Cloud SQL for PostgreSQL 创建和部署 Rails 应用。以下部分将引导您完成配置、Cloud SQL for PostgreSQL 连接以及应用部署。

创建新应用以列出猫咪

  1. 运行 rails new 命令以创建新的 Rails 应用。此应用将在 Cloud SQL for PostgreSQL 中存储一个猫咪列表。

    rails new cat_sample_app
    
  2. 转到包含生成的 Rails 应用的目录。

    cd cat_sample_app
    

在本地运行应用

要在本地计算机上运行新 Rails 应用,请执行以下操作:

  1. 启动本地 Web 服务器:

    bundle exec bin/rails server
    
  2. 在浏览器中,转到 http://localhost:3000/

    示例应用会显示 Rails 徽标以及 Rails 和 Ruby 版本。

为猫咪列表生成基架

为名为 Cat 的资源生成基架,该资源用于生成一个猫咪列表,其中包含猫咪的名字和年龄。

  1. 生成基架。

    bundle exec rails generate scaffold Cat name:string age:integer
    

    该命令会为 Cat 资源生成模型、控制器和视图。

    invoke  active_record
    create    db/migrate/20230922063608_create_cats.rb
    create    app/models/cat.rb
    invoke    test_unit
    create      test/models/cat_test.rb
    create      test/fixtures/cats.yml
    invoke  resource_route
    route    resources :cats
    invoke  scaffold_controller
    create    app/controllers/cats_controller.rb
    invoke    erb
    create      app/views/cats
    create      app/views/cats/index.html.erb
    create      app/views/cats/edit.html.erb
    create      app/views/cats/show.html.erb
    create      app/views/cats/new.html.erb
    create      app/views/cats/_form.html.erb
    create      app/views/cats/_cat.html.erb
    invoke    resource_route
    invoke    test_unit
    create      test/controllers/cats_controller_test.rb
    create      test/system/cats_test.rb
    invoke    helper
    create      app/helpers/cats_helper.rb
    invoke      test_unit
    invoke    jbuilder
    create      app/views/cats/index.json.jbuilder
    create      app/views/cats/show.json.jbuilder
    create      app/views/cats/_cat.json.jbuilder
    
  2. 打开文件 config/routes.rb 可查看以下生成的内容。

    Rails.application.routes.draw do
      resources :cats
      get 'cats/index'
      # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    
    end
  3. root 'cats#index' 添加到文件中。

    Rails.application.routes.draw do
      resources :cats
      get 'cats/index'
    
      # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
      root 'cats#index'
    end
  4. 保存文件并关闭。

  5. 按照之前所述的方式对 Rails 应用进行测试。

将 Cloud SQL for PostgreSQL 与 App Engine 搭配使用

Cloud SQL for PostgreSQL 是一种全托管式数据库服务,用于设置、 维护、管理和控制关系型 PostgreSQL 数据库, Google Cloud您可以在 Rails 应用中象使用其他任何关系型数据库一样使用 Cloud SQL。

设置 Cloud SQL for PostgreSQL

要开始在您的生产环境 Rails 应用中使用 Cloud SQL,请执行以下操作:

  1. pgappengine gem 添加到 Gemfile 文件中。

    bundle add pg
    bundle add appengine
    

    Rails Gemfile 包含以下额外的 gem 条目:

    gem "appengine", "~> 0.6"
    gem "pg", "~> 1.2"
  2. 打开 config/database.yml 文件,以将 Rails 应用配置为连接到 Cloud SQL。系统将显示 SQLite 的以下样板数据库设置:

    # SQLite version 3.x
    #   gem install sqlite3
    #
    #   Ensure the SQLite 3 gem is defined in your Gemfile
    #   gem 'sqlite3'
    #
    default: &default
      adapter: sqlite3
      pool: 5
      timeout: 5000
    
    development:
      <<: *default
      database: db/development.sqlite3
    
    # Warning: The database defined as "test" will be erased and
    # re-generated from your development database when you run "rake".
    # Do not set this db to the same as development or production.
    test:
      <<: *default
      database: db/test.sqlite3
    
    production:
      <<: *default
      database: db/production.sqlite3
  3. 为 App Engine 生产环境配置 Cloud SQL 实例连接名称。

    1. 检索实例连接名称。

      gcloud sql instances describe rails-cloudsql-instance
      
    2. 复制 connectionName 旁边的值。

  4. database.yml 生产数据库配置修改为以下内容:

    production:
      adapter: postgresql
      encoding: unicode
      pool: 5
      timeout: 5000
      username: "[YOUR_POSTGRES_USERNAME]"
      password: "[YOUR_POSTGRES_PASSWORD]"
      database: "cat_list_production"
      host:   "/cloudsql/[YOUR_INSTANCE_CONNECTION_NAME]"

    其中: