Created
May 6, 2019 13:50
-
-
Save vivahiraj/95a1d2965c936d2e4e8abc382e9fc2cb to your computer and use it in GitHub Desktop.
ridgepoleをdb:migrateのようにdb:applyのような感じで使えるようにする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :db do | |
config_file = 'config/database.yml' | |
schema_file = 'db/schemas/Schemafile' | |
desc 'apply Schemafile and update schema.rb' | |
task apply: :environment do | |
ENV['ALLOW_DROP_TABLE'] ||= '0' | |
ENV['ALLOW_REMOVE_COLUMN'] ||= '0' | |
ENV['RAILS_ENV'] ||= 'development' | |
task_return = `ridgepole -E #{ENV['RAILS_ENV']} --diff #{config_file} #{schema_file}` | |
column_condition = task_return.include?('remove_column') && ENV['ALLOW_REMOVE_COLUMN'] == '0' | |
table_condition = task_return.include?('drop_table') && ENV['ALLOW_DROP_TABLE'] == '0' | |
if column_condition || table_condition | |
puts '[Warning]this task contains some risks: "remove_column" or "drop_table"' | |
else | |
sh "ridgepole -E #{ENV['RAILS_ENV']} -c #{config_file} --apply -f #{schema_file}" | |
sh 'rake db:schema:dump' | |
end | |
end | |
desc 'write Schemafile from db' | |
task :export do | |
ENV['RAILS_ENV'] ||= 'development' | |
sh "ridgepole -E #{ENV['RAILS_ENV']} -c #{config_file} --export --split --output #{schema_file}" | |
sh 'rake db:schema:dump' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment