Allows to use pt-online-schema-change
for table changes in MySQL.
Add this line to your application's Gemfile:
gem 'percona-migrations'
Create config/initializers/percona_migrations.rb
with:
PerconaMigrations.database_config = ActiveRecord::Base.configurations.find_db_config(Rails.env)
PerconaMigrations.allow_sql = !Rails.env.production?
ActiveRecord::Migration.send :include, PerconaMigrations::HelperMethods
class AddAddressToUsers < ActiveRecord::Migration
def up
commands = columns.map { |name, type| "ADD COLUMN #{name} #{type}" }
percona_alter_table :users, commands
end
def down
commands = columns.map { |name, _| "DROP COLUMN #{name}" }
percona_alter_table :users, commands
end
private
def columns
{ street: 'STRING(255)' },
{ city: 'STRING(255)' },
{ state: 'STRING(2)' },
{ zip: 'STRING(10)' }
end
end
Additional config can optionally be passed either in the initializer or in the migration itself
PerconaMigrations.config do |c|
c.drop_old_table = false
c.max_load = 'Threads_running=60'
end
def up
commands = columns.map { |name, type| "ADD COLUMN #{name} #{type}" }
options = { drop_old_table: false, max_load: 'Threads_running=60' }
percona_alter_table :users, commands, options: options
end
- Fork it ( https://github.com/[my-github-username]/percona-migrations/fork )
- 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 a new Pull Request