こちらの記事を参考に以下を修正しました。(ほぼ同じ)
- Rails.envを考慮
- annotateを実行
- 出力をsplit
- rspecの前に流す
lib/tasks/ridgepole.rake
# frozen_string_literal: true
# see https://qiita.com/lhside/items/0dcad79d9b801e34bc7c
namespace :ridgepole do
desc 'Apply database schema'
task apply: :environment do
ridgepole('--apply', "-E #{Rails.env}", "--file #{schema_file}")
Rake::Task['db:schema:dump'].invoke if Rails.env.development?
Rake::Task['annotate_models'].invoke if Rails.env.development?
end
desc 'Export database schema'
task export: :environment do
ridgepole('--export', "-E #{Rails.env}", '--split', "--output #{schema_file}")
end
private def schema_file
Rails.root.join('db/schema/Schemafile') # rubocop:disable Rails/FilePath
end
private def config_file
Rails.root.join('config/database.yml') # rubocop:disable Rails/FilePath
end
private def ridgepole(*options)
command = ['bundle exec ridgepole', "--config #{config_file}"]
system [command + options].join(' ')
end
end
spec/spec_helper.rb
# frozen_string_literal: true
require 'rake'
## ~~~~略~~~~
RSpec.configure do |config|
config.before(:suite) do
Rails.application.load_tasks
Rake.application['ridgepole:apply'].invoke
## ~~~~略~~~~