percona-migrations-livelink 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +9 -0
- data/lib/percona-migrations.rb +1 -0
- data/lib/percona_migrations.rb +94 -0
- data/lib/percona_migrations/helper_methods.rb +7 -0
- data/lib/percona_migrations/runners.rb +43 -0
- data/lib/percona_migrations/runners/base.rb +26 -0
- data/lib/percona_migrations/runners/percona.rb +51 -0
- data/lib/percona_migrations/runners/sql.rb +25 -0
- data/lib/percona_migrations/version.rb +3 -0
- data/percona-migrations.gemspec +25 -0
- data/spec/lib/percona_migrations/helper_methods_spec.rb +18 -0
- data/spec/lib/percona_migrations/runner_spec.rb +63 -0
- data/spec/lib/percona_migrations/runners/percona_spec.rb +72 -0
- data/spec/lib/percona_migrations/runners/sql_spec.rb +24 -0
- data/spec/lib/percona_migrations_spec.rb +73 -0
- data/spec/spec_helper.rb +25 -0
- metadata +130 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: f24c52b34bbdb28bc9742668ac22d300454892af
         | 
| 4 | 
            +
              data.tar.gz: 211a367ae70a694799fa68237116a41ee17f605a
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: ad9ff0bb6c541981e13a045b486106e5c46c5d0d4781145f43b8d848bd4bef242f11d2d4e1ce20f0fa7f6717a1622ca9cc157fccdef4e133149f876e7ea24a38
         | 
| 7 | 
            +
              data.tar.gz: a45b50a1fbe6e3cb7ff2e36629c456efb1b213d8ad75b8fa6c4a05f48480a31ff47385a26b9fd131afce84e028cbf6ca6d0a69aba77d2c9b5958c10e7fae6c55
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            Copyright (c) 2015 Sergey Varaksin
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            MIT License
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 6 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 7 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 8 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 9 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 10 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 11 | 
            +
            the following conditions:
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 14 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 17 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 18 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 19 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 20 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 21 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 22 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,57 @@ | |
| 1 | 
            +
            # Percona Migrations
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            [](https://travis-ci.org/svarks/percona-migrations)
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Allows to use `pt-online-schema-change` for table changes in MySQL.
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            ## Installation
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            Add this line to your application's Gemfile:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            ```ruby
         | 
| 12 | 
            +
            gem 'percona-migrations'
         | 
| 13 | 
            +
            ```
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Create `config/initializers/percona_migrations.rb` with:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            ```ruby
         | 
| 18 | 
            +
            PerconaMigrations.database_config = ActiveRecord::Base.configurations[Rails.env]
         | 
| 19 | 
            +
            PerconaMigrations.allow_sql = !Rails.env.production?
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            ActiveRecord::Migration.send :include, PerconaMigrations::HelperMethods
         | 
| 22 | 
            +
            ```
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            ## Usage
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            ```ruby
         | 
| 27 | 
            +
            class AddAddressToUsers < ActiveRecord::Migration
         | 
| 28 | 
            +
              def up
         | 
| 29 | 
            +
                commands = columns.map { |name, type| "ADD COLUMN #{name} #{type}" }
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                percona_alter_table :users, commands
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def down
         | 
| 35 | 
            +
                commands = columns.map { |name, _| "DROP COLUMN #{name}" }
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                percona_alter_table :users, commands
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              private
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def columns
         | 
| 43 | 
            +
                { street: 'STRING(255)' },
         | 
| 44 | 
            +
                { city:   'STRING(255)' },
         | 
| 45 | 
            +
                { state:  'STRING(2)' },
         | 
| 46 | 
            +
                { zip:    'STRING(10)' }
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
            end
         | 
| 49 | 
            +
            ```
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            ## Contributing
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            1. Fork it ( https://github.com/[my-github-username]/percona-migrations/fork )
         | 
| 54 | 
            +
            2. Create your feature branch (`git checkout -b my-new-feature`)
         | 
| 55 | 
            +
            3. Commit your changes (`git commit -am 'Add some feature'`)
         | 
| 56 | 
            +
            4. Push to the branch (`git push origin my-new-feature`)
         | 
| 57 | 
            +
            5. Create a new Pull Request
         | 
    
        data/Rakefile
    ADDED
    
    
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            require 'percona_migrations'
         | 
| @@ -0,0 +1,94 @@ | |
| 1 | 
            +
            require 'percona_migrations/version'
         | 
| 2 | 
            +
            require 'percona_migrations/runners'
         | 
| 3 | 
            +
            require 'percona_migrations/helper_methods'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            require 'active_record'
         | 
| 6 | 
            +
            require 'logger'
         | 
| 7 | 
            +
            require 'shellwords'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            module PerconaMigrations
         | 
| 10 | 
            +
              extend self
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              @allow_sql = true
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              @config = Struct.new('PerconaConfig',
         | 
| 15 | 
            +
                                   :charset,
         | 
| 16 | 
            +
                                   :check_interval,
         | 
| 17 | 
            +
                                   :check_alter,
         | 
| 18 | 
            +
                                   :check_plan,
         | 
| 19 | 
            +
                                   :check_replication_filters,
         | 
| 20 | 
            +
                                   :check_slave_lag,
         | 
| 21 | 
            +
                                   :chunk_index,
         | 
| 22 | 
            +
                                   :chunk_index_columns,
         | 
| 23 | 
            +
                                   :chunk_size,
         | 
| 24 | 
            +
                                   :chunk_size_limit,
         | 
| 25 | 
            +
                                   :chunk_time,
         | 
| 26 | 
            +
                                   :critical_load,
         | 
| 27 | 
            +
                                   :default_engine,
         | 
| 28 | 
            +
                                   :defaults_file,
         | 
| 29 | 
            +
                                   :drop_new_table,
         | 
| 30 | 
            +
                                   :drop_old_table,
         | 
| 31 | 
            +
                                   :lock_wait_timeout,
         | 
| 32 | 
            +
                                   :max_lag,
         | 
| 33 | 
            +
                                   :max_load,
         | 
| 34 | 
            +
                                   :pid,
         | 
| 35 | 
            +
                                   :print,
         | 
| 36 | 
            +
                                   :progress,
         | 
| 37 | 
            +
                                   :quiet,
         | 
| 38 | 
            +
                                   :recurse,
         | 
| 39 | 
            +
                                   :recursion_method,
         | 
| 40 | 
            +
                                   :retries,
         | 
| 41 | 
            +
                                   :set_vars,
         | 
| 42 | 
            +
                                   :statistics,
         | 
| 43 | 
            +
                                   :swap_tables
         | 
| 44 | 
            +
                                   ).new
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              attr_writer :database_config, :allow_sql, :logger
         | 
| 47 | 
            +
             | 
| 48 | 
            +
              def config
         | 
| 49 | 
            +
                if block_given?
         | 
| 50 | 
            +
                  yield @config
         | 
| 51 | 
            +
                else
         | 
| 52 | 
            +
                  @config
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
              end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
              def pt_schema_tool_args
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                @config.members.map do |key|
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                  arg = key.to_s.gsub(/_/,'-')
         | 
| 61 | 
            +
                  val = @config[key]
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                  case val
         | 
| 64 | 
            +
                  when nil
         | 
| 65 | 
            +
                    nil
         | 
| 66 | 
            +
                  when true
         | 
| 67 | 
            +
                    "--#{arg}"
         | 
| 68 | 
            +
                  when false
         | 
| 69 | 
            +
                    "--no-#{arg}"
         | 
| 70 | 
            +
                  else
         | 
| 71 | 
            +
                    "--#{arg} #{Shellwords.escape(val)}"
         | 
| 72 | 
            +
                  end
         | 
| 73 | 
            +
                end.compact.join(' ')
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              def database_config
         | 
| 77 | 
            +
                @database_config || raise('PerconaMigrations.database_config is not set.')
         | 
| 78 | 
            +
              end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
              def allow_sql?
         | 
| 81 | 
            +
                !!@allow_sql
         | 
| 82 | 
            +
              end
         | 
| 83 | 
            +
             | 
| 84 | 
            +
              def logger
         | 
| 85 | 
            +
                unless defined? @logger
         | 
| 86 | 
            +
                  @logger = Logger.new($stdout)
         | 
| 87 | 
            +
                  @logger.formatter = proc do |severity, datetime, progname, msg|
         | 
| 88 | 
            +
                    "[percona-migrations] #{msg}\n"
         | 
| 89 | 
            +
                  end
         | 
| 90 | 
            +
                end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                @logger
         | 
| 93 | 
            +
              end
         | 
| 94 | 
            +
            end
         | 
| @@ -0,0 +1,43 @@ | |
| 1 | 
            +
            require 'percona_migrations/runners/base'
         | 
| 2 | 
            +
            require 'percona_migrations/runners/percona'
         | 
| 3 | 
            +
            require 'percona_migrations/runners/sql'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module PerconaMigrations
         | 
| 6 | 
            +
              module Runners
         | 
| 7 | 
            +
                def self.run(*args)
         | 
| 8 | 
            +
                  runner_class = find_runner
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  unless runner_class
         | 
| 11 | 
            +
                    raise "No available migration runners found."
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  runner = runner_class.new(*args)
         | 
| 15 | 
            +
                  runner.run
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def self.find_runner
         | 
| 19 | 
            +
                  if Runners::Percona.available?
         | 
| 20 | 
            +
                    Runners::Percona
         | 
| 21 | 
            +
                  else
         | 
| 22 | 
            +
                    log_percona_install_command
         | 
| 23 | 
            +
                    Runners::Sql if PerconaMigrations.allow_sql?
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                private
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def self.log_percona_install_command
         | 
| 30 | 
            +
                  logger = PerconaMigrations.logger
         | 
| 31 | 
            +
                  return unless logger
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                  logger.warn ""
         | 
| 34 | 
            +
                  logger.warn "*" * 80
         | 
| 35 | 
            +
                  logger.warn ""
         | 
| 36 | 
            +
                  logger.warn "`#{Runners::Percona::COMMAND}` command not found, please install percona tools:"
         | 
| 37 | 
            +
                  logger.warn "$ brew install percona-toolkit"
         | 
| 38 | 
            +
                  logger.warn ""
         | 
| 39 | 
            +
                  logger.warn "*" * 80
         | 
| 40 | 
            +
                  logger.warn ""
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            module PerconaMigrations
         | 
| 2 | 
            +
              module Runners
         | 
| 3 | 
            +
                class Base
         | 
| 4 | 
            +
                  def self.available?
         | 
| 5 | 
            +
                    true
         | 
| 6 | 
            +
                  end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def initialize(table_name, commands)
         | 
| 9 | 
            +
                    @table_name = table_name
         | 
| 10 | 
            +
                    @commands = commands
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  def run
         | 
| 14 | 
            +
                    raise NotImplementerError
         | 
| 15 | 
            +
                  end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def log(msg)
         | 
| 18 | 
            +
                    logger.info(msg) if logger
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def logger
         | 
| 22 | 
            +
                    PerconaMigrations.logger
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -0,0 +1,51 @@ | |
| 1 | 
            +
            module PerconaMigrations
         | 
| 2 | 
            +
              module Runners
         | 
| 3 | 
            +
                class Percona < Base
         | 
| 4 | 
            +
                  COMMAND = 'pt-online-schema-change'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  def self.available?
         | 
| 7 | 
            +
                    !percona_command.empty?
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  def self.percona_command
         | 
| 11 | 
            +
                    @percona_command ||= %x(which #{COMMAND}).chop
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  def run
         | 
| 15 | 
            +
                    options = [
         | 
| 16 | 
            +
                      PerconaMigrations.pt_schema_tool_args,
         | 
| 17 | 
            +
                      "--alter '#{@commands.join(', ')}'",
         | 
| 18 | 
            +
                      "-h #{database_config['host']}",
         | 
| 19 | 
            +
                      "-P #{database_config['port']}",
         | 
| 20 | 
            +
                      "-u #{database_config['username']}",
         | 
| 21 | 
            +
                      "D=#{database_config['database']},t=#{@table_name}"
         | 
| 22 | 
            +
                    ]
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    password = database_config['password']
         | 
| 25 | 
            +
                    if password && !password.empty?
         | 
| 26 | 
            +
                      options << "-p $PASSWORD"
         | 
| 27 | 
            +
                    end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                    run_command(options.reject(&:empty?).join(' '), { 'PASSWORD' => password })
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  private
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  def database_config
         | 
| 35 | 
            +
                    PerconaMigrations.database_config
         | 
| 36 | 
            +
                  end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                  def run_command(options, env_vars = {})
         | 
| 39 | 
            +
                    %w(dry-run execute).each do |mode|
         | 
| 40 | 
            +
                      cmd = "#{self.class.percona_command} #{options} --#{mode}"
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                      log "Running percona command: \"#{cmd}\""
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                      unless system(env_vars, cmd)
         | 
| 45 | 
            +
                        raise "Percona command failed: #{$?}"
         | 
| 46 | 
            +
                      end
         | 
| 47 | 
            +
                    end
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
            end
         | 
| @@ -0,0 +1,25 @@ | |
| 1 | 
            +
            module PerconaMigrations
         | 
| 2 | 
            +
              module Runners
         | 
| 3 | 
            +
                class Sql < Base
         | 
| 4 | 
            +
                  def run
         | 
| 5 | 
            +
                    @commands.each do |command|
         | 
| 6 | 
            +
                      run_command command
         | 
| 7 | 
            +
                    end
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  private
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  def run_command(command)
         | 
| 13 | 
            +
                    sql = "ALTER TABLE #{@table_name} #{command}"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                    log "Running SQL: \"#{sql}\""
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                    conn.execute sql
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                  def conn
         | 
| 21 | 
            +
                    @conn ||= ActiveRecord::Base.connection
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
            end
         | 
| @@ -0,0 +1,25 @@ | |
| 1 | 
            +
            # coding: utf-8
         | 
| 2 | 
            +
            lib = File.expand_path('../lib', __FILE__)
         | 
| 3 | 
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 4 | 
            +
            require 'percona_migrations/version'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |spec|
         | 
| 7 | 
            +
              spec.name          = "percona-migrations-livelink"
         | 
| 8 | 
            +
              spec.version       = PerconaMigrations::VERSION
         | 
| 9 | 
            +
              spec.authors       = ["Sergey Varaksin", "Geoff Youngs"]
         | 
| 10 | 
            +
              spec.email         = ["varaksin86@gmail.com", "git@intersect-uk.co.uk"]
         | 
| 11 | 
            +
              spec.summary       = %q{Allows to use percona in rails migrations.}
         | 
| 12 | 
            +
              spec.homepage      = "https://github.com/svarks/percona-migrations"
         | 
| 13 | 
            +
              spec.license       = "MIT"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              spec.files         = `git ls-files -z`.split("\x0")
         | 
| 16 | 
            +
              spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
         | 
| 17 | 
            +
              spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
         | 
| 18 | 
            +
              spec.require_paths = ["lib"]
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              spec.add_dependency 'activerecord', '>= 3.0'
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              spec.add_development_dependency 'rake'
         | 
| 23 | 
            +
              spec.add_development_dependency 'rspec', '~> 3.2.0'
         | 
| 24 | 
            +
              spec.add_development_dependency 'pry'
         | 
| 25 | 
            +
            end
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe PerconaMigrations::HelperMethods do
         | 
| 4 | 
            +
              subject do
         | 
| 5 | 
            +
                klass = Class.new do
         | 
| 6 | 
            +
                  include PerconaMigrations::HelperMethods
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
                klass.new
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              describe '#alter_table' do
         | 
| 12 | 
            +
                it 'calls a method on PerconaMigrations' do
         | 
| 13 | 
            +
                  expect(PerconaMigrations::Runners).to receive(:run).with('users', ['add column name string(255)'])
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  subject.percona_alter_table('users', ['add column name string(255)'])
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
            end
         | 
| @@ -0,0 +1,63 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe PerconaMigrations::Runners do
         | 
| 4 | 
            +
              describe '::run' do
         | 
| 5 | 
            +
                before do
         | 
| 6 | 
            +
                  expect(subject).to receive(:find_runner).and_return(runner_class)
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                context 'when runner is found' do
         | 
| 10 | 
            +
                  let(:runner_class) { PerconaMigrations::Runners::Percona }
         | 
| 11 | 
            +
                  let(:arguments) { [:arg1, :arg2, :arg3] }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  it 'runs the runner' do
         | 
| 14 | 
            +
                    runner = double()
         | 
| 15 | 
            +
                    expect(runner_class).to receive(:new).with(*arguments).and_return(runner)
         | 
| 16 | 
            +
                    expect(runner).to receive(:run)
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                    subject.run(*arguments)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                context 'when runner is not found' do
         | 
| 23 | 
            +
                  let(:runner_class) { nil }
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  it 'throws an exception' do
         | 
| 26 | 
            +
                    expect { subject.run }.to raise_exception
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              describe '::find_runner' do
         | 
| 32 | 
            +
                before do
         | 
| 33 | 
            +
                  expect(PerconaMigrations::Runners::Percona).to receive(:available?).and_return(percona_available)
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                context 'when percona is available' do
         | 
| 37 | 
            +
                  let(:percona_available) { true }
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                  it 'returns percona runner if enabled' do
         | 
| 40 | 
            +
                    expect(subject.find_runner).to eq(PerconaMigrations::Runners::Percona)
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                context 'when percona is not available' do
         | 
| 45 | 
            +
                  let(:percona_available) { false }
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                  context 'and sql is enabled' do
         | 
| 48 | 
            +
                    it 'returns sql runner' do
         | 
| 49 | 
            +
                      expect(subject.find_runner).to eq(PerconaMigrations::Runners::Sql)
         | 
| 50 | 
            +
                    end
         | 
| 51 | 
            +
                  end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  context 'and sql is disabled' do
         | 
| 54 | 
            +
                    before do
         | 
| 55 | 
            +
                      allow(PerconaMigrations).to receive(:allow_sql?).and_return(false)
         | 
| 56 | 
            +
                    end
         | 
| 57 | 
            +
                    it 'returns nil' do
         | 
| 58 | 
            +
                      expect(subject.find_runner).to eq(nil)
         | 
| 59 | 
            +
                    end
         | 
| 60 | 
            +
                  end
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
            end
         | 
| @@ -0,0 +1,72 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe PerconaMigrations::Runners::Percona do
         | 
| 4 | 
            +
              subject { described_class }
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              let(:percona_command) { '/bin/pt-online-schema-change' }
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              describe '::percona_command' do
         | 
| 9 | 
            +
                it 'is looking for pt-online-schema-change in $PATH' do
         | 
| 10 | 
            +
                  expect(subject).to receive(:`).with('which pt-online-schema-change').and_return(percona_command + "\n")
         | 
| 11 | 
            +
                  expect(subject.percona_command).to eq(percona_command)
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              describe '::available?' do
         | 
| 16 | 
            +
                it 'returns true when percona_command is present' do
         | 
| 17 | 
            +
                  expect(subject).to receive(:percona_command).and_return('/bin/percona')
         | 
| 18 | 
            +
                  expect(subject.available?).to eq(true)
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              describe '#run' do
         | 
| 23 | 
            +
                let(:db_config) do
         | 
| 24 | 
            +
                  {
         | 
| 25 | 
            +
                    'host'     => 'localhost',
         | 
| 26 | 
            +
                    'port'     => '3306',
         | 
| 27 | 
            +
                    'username' => 'root',
         | 
| 28 | 
            +
                    'password' => 'test',
         | 
| 29 | 
            +
                    'database' => 'test-db'
         | 
| 30 | 
            +
                  }
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
                let(:runner) { described_class.new('users', ['add column name string(255)']) }
         | 
| 33 | 
            +
                let(:arguments) do
         | 
| 34 | 
            +
                  [
         | 
| 35 | 
            +
                    "--alter 'add column name string(255)'",
         | 
| 36 | 
            +
                    "-h localhost",
         | 
| 37 | 
            +
                    "-P 3306",
         | 
| 38 | 
            +
                    "-u root",
         | 
| 39 | 
            +
                    "D=test-db,t=users",
         | 
| 40 | 
            +
                    "-p $PASSWORD"
         | 
| 41 | 
            +
                  ]
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                before do
         | 
| 45 | 
            +
                  allow(PerconaMigrations).to receive(:database_config).and_return(db_config)
         | 
| 46 | 
            +
                  allow(PerconaMigrations).to receive(:pt_schema_tool_args).and_return('--foo')
         | 
| 47 | 
            +
                  allow(subject).to receive(:percona_command).and_return(percona_command)
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                it 'runs percona command 2 times (dry-run and execute)' do
         | 
| 51 | 
            +
                  command = "#{percona_command} --foo #{arguments.join(' ')}"
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  expect(runner).to receive(:system).
         | 
| 54 | 
            +
                    with({ 'PASSWORD' => 'test' }, "#{command} --dry-run").
         | 
| 55 | 
            +
                    and_return(true)
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                  expect(runner).to receive(:system).
         | 
| 58 | 
            +
                    with({ 'PASSWORD' => 'test' }, "#{command} --execute").
         | 
| 59 | 
            +
                    and_return(true)
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                  runner.run
         | 
| 62 | 
            +
                end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                it 'throws an exception if dry-run fails' do
         | 
| 65 | 
            +
                  expect(runner).to receive(:system).
         | 
| 66 | 
            +
                    with({ 'PASSWORD' => 'test' }, "#{percona_command} --foo #{arguments.join(' ')} --dry-run").
         | 
| 67 | 
            +
                    and_return(false)
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                  expect { runner.run }.to raise_exception
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
            end
         | 
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe PerconaMigrations::Runners::Sql do
         | 
| 4 | 
            +
              let(:conn) { double() }
         | 
| 5 | 
            +
              let(:runner) do
         | 
| 6 | 
            +
                described_class.new('users', [
         | 
| 7 | 
            +
                  'add column first_name string(255)',
         | 
| 8 | 
            +
                  'add column last_name string(255)',
         | 
| 9 | 
            +
                ])
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              before do
         | 
| 13 | 
            +
                allow(ActiveRecord::Base).to receive(:connection).and_return(conn)
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              describe '#run' do
         | 
| 17 | 
            +
                it 'runs sql commands throuth ActiveRecord' do
         | 
| 18 | 
            +
                  expect(conn).to receive(:execute).with('ALTER TABLE users add column first_name string(255)')
         | 
| 19 | 
            +
                  expect(conn).to receive(:execute).with('ALTER TABLE users add column last_name string(255)')
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  runner.run
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
| @@ -0,0 +1,73 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            RSpec.describe PerconaMigrations do
         | 
| 4 | 
            +
              describe '::allow_sql?' do
         | 
| 5 | 
            +
                it 'is true by default' do
         | 
| 6 | 
            +
                  expect(subject.allow_sql?).to eq(true)
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                it 'can be set to false' do
         | 
| 10 | 
            +
                  subject.allow_sql = false
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  expect(subject.allow_sql?).to eq(false)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  subject.allow_sql = true
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              describe '::database_config' do
         | 
| 19 | 
            +
                it 'throws an exception if not set' do
         | 
| 20 | 
            +
                  expect { subject.database_config }.to raise_exception
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                it 'returns config' do
         | 
| 24 | 
            +
                  config = { 'username' => 'test' }
         | 
| 25 | 
            +
                  subject.database_config = config
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  expect(subject.database_config).to eq(config)
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                  subject.database_config = nil
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              describe '::config' do
         | 
| 34 | 
            +
                before do
         | 
| 35 | 
            +
                  subject.instance_eval { @config = @config.class.new }
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
                it 'yields a block' do
         | 
| 38 | 
            +
                  expect { |b| subject.config(&b) }.to yield_control.once
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
                it 'allows config to be set' do
         | 
| 41 | 
            +
                  subject.config { |c| c.max_load = 'Threads_running=25' }
         | 
| 42 | 
            +
                  expect(subject.config.max_load).to eq('Threads_running=25')
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
                it 'ignores unset values' do
         | 
| 45 | 
            +
                  expect(subject.config.retries).to be(nil)
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
                it 'returns no arguments by default' do
         | 
| 48 | 
            +
                  expect(subject.pt_schema_tool_args).to be_empty
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                context 'with boolean config' do
         | 
| 52 | 
            +
                  before do
         | 
| 53 | 
            +
                    subject.config do |c|
         | 
| 54 | 
            +
                      c.statistics = true
         | 
| 55 | 
            +
                      c.drop_old_table = false
         | 
| 56 | 
            +
                    end
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
                  it 'returns arguments' do
         | 
| 59 | 
            +
                    expect(subject.pt_schema_tool_args).to eq('--no-drop-old-table --statistics')
         | 
| 60 | 
            +
                  end
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
                context 'with config' do
         | 
| 63 | 
            +
                  before do
         | 
| 64 | 
            +
                    subject.config do |c|
         | 
| 65 | 
            +
                      c.max_load = 'Threads_running=25'
         | 
| 66 | 
            +
                    end
         | 
| 67 | 
            +
                  end
         | 
| 68 | 
            +
                  it 'returns arguments' do
         | 
| 69 | 
            +
                    expect(subject.pt_schema_tool_args).to eq('--max-load Threads_running\\=25')
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
                end
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,25 @@ | |
| 1 | 
            +
            require 'rubygems'
         | 
| 2 | 
            +
            require 'bundler'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            Bundler.require(:default, :development)
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            require 'percona_migrations'
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            PerconaMigrations.logger = nil
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            RSpec.configure do |config|
         | 
| 13 | 
            +
              config.expect_with :rspec do |expectations|
         | 
| 14 | 
            +
                expectations.include_chain_clauses_in_custom_matcher_descriptions = true
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              config.mock_with :rspec do |mocks|
         | 
| 18 | 
            +
                mocks.verify_partial_doubles = true
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              config.filter_run :focus
         | 
| 22 | 
            +
              config.run_all_when_everything_filtered = true
         | 
| 23 | 
            +
              config.disable_monkey_patching!
         | 
| 24 | 
            +
              config.order = :random
         | 
| 25 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,130 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: percona-migrations-livelink
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.3
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Sergey Varaksin
         | 
| 8 | 
            +
            - Geoff Youngs
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2016-03-17 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: activerecord
         | 
| 16 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                requirements:
         | 
| 18 | 
            +
                - - '>='
         | 
| 19 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 20 | 
            +
                    version: '3.0'
         | 
| 21 | 
            +
              type: :runtime
         | 
| 22 | 
            +
              prerelease: false
         | 
| 23 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 24 | 
            +
                requirements:
         | 
| 25 | 
            +
                - - '>='
         | 
| 26 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 27 | 
            +
                    version: '3.0'
         | 
| 28 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 29 | 
            +
              name: rake
         | 
| 30 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 31 | 
            +
                requirements:
         | 
| 32 | 
            +
                - - '>='
         | 
| 33 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 34 | 
            +
                    version: '0'
         | 
| 35 | 
            +
              type: :development
         | 
| 36 | 
            +
              prerelease: false
         | 
| 37 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 38 | 
            +
                requirements:
         | 
| 39 | 
            +
                - - '>='
         | 
| 40 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 41 | 
            +
                    version: '0'
         | 
| 42 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 43 | 
            +
              name: rspec
         | 
| 44 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 45 | 
            +
                requirements:
         | 
| 46 | 
            +
                - - ~>
         | 
| 47 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 48 | 
            +
                    version: 3.2.0
         | 
| 49 | 
            +
              type: :development
         | 
| 50 | 
            +
              prerelease: false
         | 
| 51 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 52 | 
            +
                requirements:
         | 
| 53 | 
            +
                - - ~>
         | 
| 54 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 55 | 
            +
                    version: 3.2.0
         | 
| 56 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 57 | 
            +
              name: pry
         | 
| 58 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 59 | 
            +
                requirements:
         | 
| 60 | 
            +
                - - '>='
         | 
| 61 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 62 | 
            +
                    version: '0'
         | 
| 63 | 
            +
              type: :development
         | 
| 64 | 
            +
              prerelease: false
         | 
| 65 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 66 | 
            +
                requirements:
         | 
| 67 | 
            +
                - - '>='
         | 
| 68 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 69 | 
            +
                    version: '0'
         | 
| 70 | 
            +
            description: 
         | 
| 71 | 
            +
            email:
         | 
| 72 | 
            +
            - varaksin86@gmail.com
         | 
| 73 | 
            +
            - git@intersect-uk.co.uk
         | 
| 74 | 
            +
            executables: []
         | 
| 75 | 
            +
            extensions: []
         | 
| 76 | 
            +
            extra_rdoc_files: []
         | 
| 77 | 
            +
            files:
         | 
| 78 | 
            +
            - .gitignore
         | 
| 79 | 
            +
            - .rspec
         | 
| 80 | 
            +
            - .travis.yml
         | 
| 81 | 
            +
            - Gemfile
         | 
| 82 | 
            +
            - LICENSE.txt
         | 
| 83 | 
            +
            - README.md
         | 
| 84 | 
            +
            - Rakefile
         | 
| 85 | 
            +
            - lib/percona-migrations.rb
         | 
| 86 | 
            +
            - lib/percona_migrations.rb
         | 
| 87 | 
            +
            - lib/percona_migrations/helper_methods.rb
         | 
| 88 | 
            +
            - lib/percona_migrations/runners.rb
         | 
| 89 | 
            +
            - lib/percona_migrations/runners/base.rb
         | 
| 90 | 
            +
            - lib/percona_migrations/runners/percona.rb
         | 
| 91 | 
            +
            - lib/percona_migrations/runners/sql.rb
         | 
| 92 | 
            +
            - lib/percona_migrations/version.rb
         | 
| 93 | 
            +
            - percona-migrations.gemspec
         | 
| 94 | 
            +
            - spec/lib/percona_migrations/helper_methods_spec.rb
         | 
| 95 | 
            +
            - spec/lib/percona_migrations/runner_spec.rb
         | 
| 96 | 
            +
            - spec/lib/percona_migrations/runners/percona_spec.rb
         | 
| 97 | 
            +
            - spec/lib/percona_migrations/runners/sql_spec.rb
         | 
| 98 | 
            +
            - spec/lib/percona_migrations_spec.rb
         | 
| 99 | 
            +
            - spec/spec_helper.rb
         | 
| 100 | 
            +
            homepage: https://github.com/svarks/percona-migrations
         | 
| 101 | 
            +
            licenses:
         | 
| 102 | 
            +
            - MIT
         | 
| 103 | 
            +
            metadata: {}
         | 
| 104 | 
            +
            post_install_message: 
         | 
| 105 | 
            +
            rdoc_options: []
         | 
| 106 | 
            +
            require_paths:
         | 
| 107 | 
            +
            - lib
         | 
| 108 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 109 | 
            +
              requirements:
         | 
| 110 | 
            +
              - - '>='
         | 
| 111 | 
            +
                - !ruby/object:Gem::Version
         | 
| 112 | 
            +
                  version: '0'
         | 
| 113 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
              requirements:
         | 
| 115 | 
            +
              - - '>='
         | 
| 116 | 
            +
                - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                  version: '0'
         | 
| 118 | 
            +
            requirements: []
         | 
| 119 | 
            +
            rubyforge_project: 
         | 
| 120 | 
            +
            rubygems_version: 2.0.14
         | 
| 121 | 
            +
            signing_key: 
         | 
| 122 | 
            +
            specification_version: 4
         | 
| 123 | 
            +
            summary: Allows to use percona in rails migrations.
         | 
| 124 | 
            +
            test_files:
         | 
| 125 | 
            +
            - spec/lib/percona_migrations/helper_methods_spec.rb
         | 
| 126 | 
            +
            - spec/lib/percona_migrations/runner_spec.rb
         | 
| 127 | 
            +
            - spec/lib/percona_migrations/runners/percona_spec.rb
         | 
| 128 | 
            +
            - spec/lib/percona_migrations/runners/sql_spec.rb
         | 
| 129 | 
            +
            - spec/lib/percona_migrations_spec.rb
         | 
| 130 | 
            +
            - spec/spec_helper.rb
         |