最近经常有一些需求要在生产环境上跑脚本修复部分数据,于是动起脑筋来做一个 DSL。可以跑脚本的时候有一些交互,并支持各种参数(谁叫咱们号称 SAAS 公司)。
我丢到 github 上去了以供大家鄙视。贴个 README.MD 在这里。
This is a single file helper to make your script easy to be written and read. I used it in my daily job.
require File.join(File.dirname(__FILE__), '..', 'db_script_helper')
# script topic and detail description
desc "User Fix", "Propose : Fix user data if user is invalid" do
# require parameter of company id, if passed by parameters, will ignore prompty
param :company, "Company ID", :required => true
# you can define different data set to update
change_set :invalid_users do
#return could be one record or array of record
User.find_all_by_company_id params[:company]
end
# update data set you asked
update :invalid_users do |rec|
rec.is_deleted = true
end
# it will show all change set and confirm with Y/N
# a summary is a good change data report
summary :invalid_users do |result|
puts result
end
end
cd rails_app_path script/runner example_path.rb [options]
================================================================================ User Fix ================================================================================ Propose : Fix user data if user is invalid ================================================================================ Company ID : 12 ================================================================================ Processing ================================================================================ users : 36 +-is_deleted : false -> true users : 40 +-is_deleted : false -> true users : 84 +-is_deleted : false -> true users : 85 +-is_deleted : false -> true users : 86 +-is_deleted : false -> true ================================================================================ [Y]es / [N]o :N ================================================================================ QUIT without changing ================================================================================
移驾 github 索取代码 https://github.com/genewoo/rails_db_script_helper