0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Render】RenderでRailsのシード値を適用する方法

Last updated at Posted at 2024-12-01

記事概要

RenderでDBにシード値を適用する方法について説明する。

言語やフレームワーク

使用技術
フロントエンド HTML
バックエンド Ruby 3.2.0
Ruby on Rails 7.0.8.6
データベース PostgreSQL
インフラ Render
API -
その他 -

前提

  • db/seeds.rbにシード値を作成するように記述されている
  • Renderにアカウント登録している

手順①(事前準備)

  1. ローカルPCにPostgreSQLを登録する
    1. ターミナルで下記を実行する
      % brew install postgresql@14
      
    2. PostgreSQLを正常にインストールできたことを確認する
      % psql --version
      # => psql (PostgreSQL) 14.5 (Homebrew) のように、バージョンが表示されれば正常
      
  2. アプリケーションにライブラリを追加する
    1. GemfileにGemを追記
      # 最終行に、下記(group ~ end)を記述する 
      group :production do
        gem 'pg'
      end
      
    2. ターミナルで下記を実行し、Gemをインストール
      % bundle install
      
  3. Render用の設定ファイルを追加する
    1. binフォルダにrender-build.shファイルを手動作成
    2. 下記を記述
      #!/usr/bin/env bash
      # exit on error
      set -o errexit
      
      bundle install
      bundle exec rake assets:precompile
      bundle exec rake assets:clean
      bundle exec rake db:migrate
      bundle exec rails db:seed
      
  4. DB設定を変更する
    1. config/database.ymlのDB名を変更する
       development:
        # 中略
        database: [アプリ名]_development
      
      test:
        # 中略
        database: [アプリ名]_test
      
  5. Gemfile.lockの設定を変更するために、ターミナルで実行
    % bundle lock --add-platform x86_64-linux
    
  6. アプリをクローンした場合、秘密情報管理用のファイルを作成する
  7. GitHubにコードをプッシュする

手順②(DB作成)

  1. Renderにログイン

  2. + New > PostgreSQL を選択

  3. 下記を入力 or 選択する

    項目名 入力値
    Name <任意のDB名>
    Region Ohio
    Instance Type Free
  4. 「Create DataBase」をクリック

  5. 「Status」がAvailableに変更されると、DB作成が完了

  6. Internal Database URLの値をコピーし、メモなどに保管する

手順③(アプリケーション作成)

  1. Renderでアプリを作成

  2. + New > Web Service を選択

  3. GitHubのリポジトリを選択

  4. 「Connect」をクリック

  5. 下記を入力 or 選択する

    項目名 入力値
    Name <任意のアプリ名 ※'_'は使用しないこと>
    Region Ohio
    Build Command ./bin/render-build.sh
    Start Command bundle exec puma -C config/puma.rb
    Instance Type Free
  6. 環境変数を設定する

    環境変数名 入力値
    RAILS_MASTER_KEY < config/master.keyの値>
    DATABASE_URL <「Internal Database URL」の値>
  7. 「Deploy Web Service」をクリック

  8. 「Status」がDeployedに変更されると、アプリ作成が完了

備考

  • render-build.shファイルのbundle exec rails db:seedを記述を削除すると、シード値適用が実行されない
  • TablePlusを使用すると、DBの内容を確認できる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?