LoginSignup
20
26

More than 5 years have passed since last update.

RakeでRidgepoleコマンドを実行する

Last updated at Posted at 2015-11-08

データベースのスキーマを管理する方法の一つにRigepoleがあります。Rigepoleは、データベースのスキーマを管理するツールです。スキーマの更新は、DSLで記述したスキーマファイルを反映して行います。Ridgepoleについては、クックパッドにおける最近のActiveRecord運用事情 - クックパッド開発者ブログが参考になります。

実際にスキーマを更新するには、次のコマンドを実行します。

bundle exec ridgepole --apply --file Schemafile --config config/database.yml

繰り返し行う作業にしては、少しコマンドが長いです。Rakeタスクにしてみましょう。

lib/tasks/ridgepole.rake
namespace :ridgepole do
  desc 'Apply database schema'
  task apply: :environment do
    ridgepole('--apply', "--file #{schema_file}")
    Rake::Task['db:schema:dump'].invoke
  end

  desc 'Export database schema'
  task export: :environment do
    ridgepole('--export', "--output #{schema_file}")
  end

  private

  def schema_file
    Rails.root.join('Schemafile')
  end

  def config_file
    Rails.root.join('config/database.yml')
  end

  def ridgepole(*options)
    command = ['bundle exec ridgepole', "--config #{config_file}"]
    system [command + options].join(' ')
  end
end

これで、RakeでRidgepoleコマンドを実行する準備ができました。次のように、コマンドを実行すると実際にRidgepoleコマンドが実行されます。

Apply ridgepole schema
$ bundle exec rake ridgepole:apply 

Export ridgepole schema
$ bundle exec rake ridgepole:export
20
26
2

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
20
26