Heavily based on the seed_migration
gem found here.
For rails projects that need to run tasks on deployment that don't quite fit in the db:migrate
and seed:migrate
categories, this gem migrates specified rake tasks and ensures they only run once.
Add this line to your application's Gemfile:
gem 'rake-task-migration'
And then execute:
$ bundle
Install and run the internal migrations
$ bundle exec rake task_migration:install:migrations
$ bundle exec rake db:migrate
That will create the table to keep track of rake task migrations.
Create the lib/tasks/migrations.rake
file and add your tasks:
namespace :migrations do
task :migrate_user_names => :environment do
User.find_each do |user|
user.update_attributes(name: "#{user.first_name} #{user.last_name}")
end
end
end
Then run the migration task:
$ bundle exec rake tasks:migrate
== migrate_user_names: migrating =============================================
== migrate_user_names: migrated (0.0191s) ====================================
Each rake task is run only once.
Use an initializer file for configuration.
migration_table_name (default = 'rake_task_migrations')
migration_namespace (default = :migrations)
# config/initializers/rake_task_migration.rb
Rake::TaskMigration.config do |config|
config.migration_table_name = 'table_name'
config.migration_namespace = 'namespace'
end
export RAILS_ENV=test
export DB=mysql # or sqlite, postgresql
bundle exec rake app:db:create app:db:migrate
bundle exec rspec spec
- Rubies: 2.5, 2.6
- Rails: 5.0, 5.1, 5.2, 6.0
- Databases: MySQL, SQLite, PostgreSQL
For more information see the travic-ci config.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
The gem is available as open source under the terms of the MIT License.