LoginSignup
9
12

More than 3 years have passed since last update.

Railsのアプリケーション(MySQL)をheroku(PostgreSQL)にデプロイした

Last updated at Posted at 2018-12-02

はじめに

MySQL、solidusで運用されているECサイトアプリケーションをherokuにデプロイする過程でかなり苦労したので、自分の確認用としてメモに残します。
よろしければ参考にしてみてください。

# 実行環境
 ruby 2.4.1
 rails 5.1.4
 solidus 2.3
 mysql2 0.4.9 

1.database.ymlの変更

database.yml
#変更前
production:
  <<: *default
  database: hogehoge_production
  username: hogehoge
  password: <%= ENV['HOGEHOGE_DATABASE_PASSWORD'] %>

#変更後 passwordは空白でOKです
production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  database: db/production.postgresql
  pool: 5
  username: test_postgre
  password:

2.gitignoreの確認

# 下記のように/bundleが.gitignoreに追加されていたら
/vendor/bundle

# /bundleを含めないように変更
/vendor

# git addして管理対象に含める
git add

# Gitの管理対象に入っているか確認
git ls-files | sed -e '/^[^\/]*$/d' -e 's/\/[^\/]*$//g' | sort | uniq

# 入っていなければ手動で追加
git add /vendor

3.application.rbの変更

# 以下をconfig/application.rbに追加

config.assets.initialize_on_precompile = false
config/application.rb
require_relative 'boot'

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Hogehogeec
  class Application < Rails::Application

    config.to_prepare do
      # Load application's model / class decorators
      Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
        Rails.configuration.cache_classes ? require(c) : load(c)
      end

      # Load application's view overrides
      Dir.glob(File.join(File.dirname(__FILE__), "../app/overrides/*.rb")) do |c|
        Rails.configuration.cache_classes ? require(c) : load(c)
      end
    end

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    config.time_zone = 'Tokyo'
    config.generators.test_framework = :rspec
    config.generators.system_tests   = false
    config.generators.stylesheets    = false
    config.generators.javascripts    = false
    config.generators.helper         = false
    config.assets.initialize_on_precompile = false  #追加

    config.generators do |g|
      g.test_framework :rspec,
                      fixtures: false,
                      view_specs: false,
                      helper_specs: false,
                      routing_specs: false
    end
  end
end

4.Gemfileの変更

変更前

gem 'mysql2', '>= 0.3.18', '< 0.5'

変更後

# mysqlを下記のようにdevelopment,test環境へ移動

group :development, :test do
 ・
 ・
 ・
 ・
  gem 'mysql2', '>= 0.3.18', '< 0.5'
end

# 追加
group :production do
  gem 'pg', "~> 0.19.0"
  gem 'rails_12factor'
end

bundle install --without production
git add -A
git commit -m "~~~"

6.デプロイ

以下のコマンドを1つずつ実行します。

heroku login
heroku create
heroku pg:reset DATABASE
bundle exec rake assets:precompile
git push heroku master(pushするブランチがmasterでない場合→git push heroku ブランチ名:master)
# herokuアドオンにHeroku Postgresが追加されていない場合(herokuリポジトリページのoverviewのInstalled add-onsで確認)

heroku addons:create heroku-postgresql
heroku run rails db:migrate
heroku run rails g spree:install
heroku restart

7.補足 CDNの読み込み(応急処置)

デプロイ後にfont-awesome(Facebookなどのアイコン)が読み込まれない場合はapplication.html.erb<head></head>内に以下のstylesheet_link_tagを追加してみてください。

layouts/_head.html.erb
<%= stylesheet_link_tag "application", 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.css', :media => "all" %>

最後に

以上、説明不足の箇所もありますが、MySQL、solidusで運用されているRailsアプリケーションをherokuにデプロイできたのでまとめてみました。

9
12
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
9
12