在 App Engine 上將 PostgreSQL 適用的 Cloud SQL 與 Rails 7 搭配使用

開始開發在 App Engine 彈性環境上執行的 Ruby on Rails 應用程式。您所建立的應用程式,使用的正是所有 Google 產品採用的基礎架構,因此您可以放心,無論使用者只有幾人還是有數百萬人,應用程式都可調度資源,為所有使用者提供服務。

本教學課程假設您已熟悉 Rails 網路開發流程,並逐步引導您設定 PostgreSQL 適用的 Cloud SQL 和一個新的 Rails 應用程式。您也可以使用本教學課程做為參考資料,來設定現有的 Rails 應用程式以使用 PostgreSQL 適用的 Cloud SQL。

本教學課程支援且需要 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

準備 PostgreSQL 適用的 Cloud SQL 執行個體

為本教學課程設定 PostgreSQL 適用的 Cloud SQL 執行個體。

  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」指南。

完成上述前置步驟後,就能使用 PostgreSQL 適用的 Cloud SQL 建立及部署 Rails 應用程式。以下各節將逐步引導您進行應用程式設定、連結至 PostgreSQL 適用的 Cloud SQL,以及部署應用程式。

建立新應用程式來列出貓咪

  1. 執行 rails new 指令,建立新的 Rails 應用程式。這個應用程式會在 PostgreSQL 適用的 Cloud SQL 中儲存一份貓咪清單。

    rails new cat_sample_app
    
  2. 前往產生的 Rails 應用程式所屬的目錄。

    cd cat_sample_app
    

在本機執行應用程式

如何在本機電腦上執行新的 Rails 應用程式:

  1. 啟動本機網路伺服器:

    bundle exec bin/rails server
    
  2. 透過瀏覽器前往 http://localhost:3000/

    範例應用程式會顯示 Rails 標誌,以及 Rails 和 Ruby 版本。

為貓咪清單產生鷹架

針對構成貓咪清單 (包括貓咪的名字和年齡),名為 Cat 的資源產生 Scaffolding。

  1. 產生 Scaffolding。

    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