/rake-task-migrations

Run rake tasks as migrations for Ruby on Rails

Primary LanguageRubyMIT LicenseMIT

Gem Version Build Status Maintainability Test Coverage

Rake Task Migrations

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.

Installation

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.

Usage

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.

Configuration

Use an initializer file for configuration.

List of available configurations :

  • migration_table_name (default = 'rake_task_migrations')
  • migration_namespace (default = :migrations)

Example:

# config/initializers/rake_task_migration.rb

Rake::TaskMigration.config do |config|
  config.migration_table_name = 'table_name'
  config.migration_namespace  = 'namespace'
end

Runnings tests

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.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

The gem is available as open source under the terms of the MIT License.