Rake Migrations
This gem helps you and your team members keep track of 'run once' rake tasks.
Requirements
At the moment I have only tested this on Rails 3.2.X running mysql (uses mysql2 gem) or postgresql on Mac OS X. If you can help by testing this on different versions, databases and platforms, let me know.
Installation
First, add this this to your gemfile:
gem 'rake_migrations'
Then, run:
bundle install
# For mysql
rails g rake_migrations:install
# For postgresql
rails g rake_migrations:install pg
# Don't forget to migrate (both for mysql and pg)
rake db:migrate
Finally, open the file 'config/rake_migrations_check.rb' in your project and replace "database name" with your database's name and the "username" with your database's username (remove smaller/greater than symbols):
# For mysql2
client = Mysql2::Client.new(host: "localhost", username: "<username>", database: "<database name>")
# For postgresql
client = PG.connect(host: "localhost", user: "<username>", dbname: "<database name>")
Use
Whenever somebody from your team wants to create a new run once task, simply generate it by running:
rails g task <namespace> <task>
For example:
rails g task users update_some_field
This will generate a file under 'lib/tasks/rake_migrations' with a timestamp and the following content:
# Checklist:
# 1. Re-runnable on production?
# 2. Is there a chance emails will be sent?
# 3. puts ids & logs (progress log)
# 4. Can you update the records with an update all instead of instantizing?
# 5. Are there any callbacks?
# 6. Performance issues?
# 7. Scoping to account
namespace :users do
desc "update run_at field to get value as in start_time"
task update_some_field: [:environment] do
# EXAMPLE
User.update_all({role_id: 1}, {role_id: 2})
# DO NOT REMOVE THIS PART
RakeMigration.mark_complete(__FILE__)
end
end
Simply insert your code above the "DO NOT REMOVE THIS PART" line. The checklist is there to help you and the person who is code-reviewing your code to think of problems that might occur from your rake task. Afterwards you can run the rake task normally:
rake users:update_some_field
Commit your new file into your repository.
Afterwards, through the magic of git-hooks, whenever someone pulls this branch (or a branch that has this file in it), he will see a message in the terminal telling him which rakes need to be run:
You need to run the following rakes:
------------------------------------
rake users:update_some_field
Issues, suggestions and forks.
Feel free to open issues, send suggestions and fork this repository.
This gem was developed during my work at Samanage.
Thanks and Enjoy! :)