LoginSignup
1
0

More than 3 years have passed since last update.

rails db:migrateからridgepoleに移行しようとしたらcapistranoでつまづいた

Last updated at Posted at 2019-06-19

この記事はRailsmigrationを使用せずridgepoleでDB構築を行うようにして、capistranoでつまづいたのを解決した記事です。database.ymlではdotenvを使用しています。

環境

ローカルPC: Mac
リモートサーバー: AWS EC2 amazon_linux2
Ruby 2.6.3
Ruby 5.2.3
capistrano 3.11.0

問題

cap deploy時に下記エラーで落ちる。

$ bundle exec ridgepole -c config/database.yml -E staging Schemafile
[ERROR] Access denied for user ''@'localhost' (using password: NO)
        /home/ec2-user/environment/base2/vendor/bundle/ruby/2.4.0/gems/mysql2-0.5.1/lib/mysql2/client.rb:90:in `connect'

gem dotenvを使用してdatabase.ymlでは下記のように記述していた。

staging:
  <<: *default
  database: <%= ENV.fetch("DB_NAME") { 'hogehoge_app_staging' } %>
  username: <%= ENV.fetch("DATABSE_USERNAME") %>
  password: <%= ENV.fetch("DATABASE_PASSWORD") %>

deploy.rb

after 'deploy:publishing', 'ridgepole:ridgepole_apply'
namespace :ridgepole do
  desc 'ridgepole apply'
  task :ridgepole_apply do
    invoke 'ridgepole:apply'
  end
end

原因: デプロイ時にdotenvの環境変数が展開されなかった。

deploy.rbを下記のように変更

after 'deploy:publishing', 'ridgepole:ridgepole_apply'
namespace :ridgepole do
  desc 'ridgepole apply'
  task :ridgepole_apply do
    on roles(:db) do
      # invoke 'ridgepole:apply'
      within release_path do
        execute :bundle, :exec, :dotenv, "-f '.env'", :ridgepole, "-c config/database.yml -E #{fetch(:rails_env)} --apply -f Schemafile"
      end
    end
  end
end

参考にさせていただいた記事
winebarrel/ridgepole
さらば「rails migrate」、よろしく「ridgepole」
https://qiita.com/ToqTock/items/c185a38fd4e2011ddd64
https://mozy-ok.hatenablog.com/entry/2018/06/24/004358

1
0
1

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
1
0