<commit_before>require 'byebug'
require 'spec_helper'

describe PerconaMigrator do
  class Comment < ActiveRecord::Base; end

  let(:direction) { :up }
  let(:logger) { double(:logger, puts: true) }

  before { ActiveRecord::Migration.verbose = false }

  it 'has a version number' do
    expect(PerconaMigrator::VERSION).not_to be nil
  end

  context 'creating/removing columns' do
    let(:version) { 1 }

    describe 'command', integration: true do
      before { allow(PerconaMigrator::Runner).to receive(:execute) }

      it 'runs pt-online-schema-change' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('pt-online-schema-change'), kind_of(NullLogger))
        )
      end

      it 'executes the migration' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('--execute'), kind_of(NullLogger))
        )
      end

      it 'does not define --recursion-method' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('--recursion-method=none'), kind_of(NullLogger))
        )
      end

      it 'sets the --alter-foreign-keys-method option to auto' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('--alter-foreign-keys-method=auto'), kind_of(NullLogger))
        )
      end
    end

    context 'creating column' do
      require 'fixtures/migrate/1_create_column_on_comments'

      let(:direction) { :up }

      it 'adds the column in the DB table' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        Comment.reset_column_information
        expect(Comment.column_names).to include('some_id_field')
      end

      it 'marks the migration as up' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(ActiveRecord::Migrator.current_version).to eq(version)
      end
    end

    context 'droping column' do
      let(:direction) { :down }

      before do
        ActiveRecord::Migrator.new(
          :up,
          [MIGRATION_FIXTURES],
          version
        ).migrate
      end

      it 'drops the column from the DB table' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version - 1
        ).migrate

        Comment.reset_column_information
        expect(Comment.column_names).not_to include('some_id_field')
      end

      it 'marks the migration as down' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version - 1
        ).migrate

        expect(ActiveRecord::Migrator.current_version).to eq(version - 1)
      end
    end
  end

  context 'specifing connection vars and parsing tablename' do
    let(:host)      { 'test_host' }
    let(:user)      { 'test_user' }
    let(:password)  { '<PASSWORD>' }
    let(:db_name)   { 'test_db' }

    let(:version) { 1 }

    before { allow(PerconaMigrator::Runner).to receive(:execute) }

    before do
      allow(ENV).to receive(:[]).with('PERCONA_DB_HOST').and_return(host)
      allow(ENV).to receive(:[]).with('PERCONA_DB_USER').and_return(user)
      allow(ENV).to receive(:[]).with('PERCONA_DB_PASSWORD').and_return(password)
      allow(ENV).to receive(:[]).with('PERCONA_DB_NAME').and_return(db_name)
    end

    it 'executes the percona command with the right connection details' do
      described_class.migrate(version, direction, logger)
      expect(PerconaMigrator::Runner).to(
        have_received(:execute)
        .with(include("-h #{host} -u #{user} -p #{password} D=#{db_name},t=comments"), logger)
      )
    end

    context 'when there is no password' do
      before do
        allow(ENV).to receive(:[]).with('PERCONA_DB_PASSWORD').and_return(nil)
      end

      it 'executes the percona command with the right connection details' do
        described_class.migrate(version, direction, logger)
        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include("-h #{host} -u #{user} D=#{db_name},t=comments"), logger)
        )
      end
    end
  end

  context 'adding/removing indexes' do<commit_msg>Update connection details integration tests not to use LHM<commit_after>require 'byebug'
require 'spec_helper'

describe PerconaMigrator do
  class Comment < ActiveRecord::Base; end

  let(:direction) { :up }
  let(:logger) { double(:logger, puts: true) }

  before { ActiveRecord::Migration.verbose = false }

  it 'has a version number' do
    expect(PerconaMigrator::VERSION).not_to be nil
  end

  context 'creating/removing columns' do
    let(:version) { 1 }

    describe 'command', integration: true do
      before { allow(PerconaMigrator::Runner).to receive(:execute) }

      it 'runs pt-online-schema-change' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('pt-online-schema-change'), kind_of(NullLogger))
        )
      end

      it 'executes the migration' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('--execute'), kind_of(NullLogger))
        )
      end

      it 'does not define --recursion-method' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('--recursion-method=none'), kind_of(NullLogger))
        )
      end

      it 'sets the --alter-foreign-keys-method option to auto' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include('--alter-foreign-keys-method=auto'), kind_of(NullLogger))
        )
      end
    end

    context 'creating column' do
      require 'fixtures/migrate/1_create_column_on_comments'

      let(:direction) { :up }

      it 'adds the column in the DB table' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        Comment.reset_column_information
        expect(Comment.column_names).to include('some_id_field')
      end

      it 'marks the migration as up' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(ActiveRecord::Migrator.current_version).to eq(version)
      end
    end

    context 'droping column' do
      let(:direction) { :down }

      before do
        ActiveRecord::Migrator.new(
          :up,
          [MIGRATION_FIXTURES],
          version
        ).migrate
      end

      it 'drops the column from the DB table' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version - 1
        ).migrate

        Comment.reset_column_information
        expect(Comment.column_names).not_to include('some_id_field')
      end

      it 'marks the migration as down' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version - 1
        ).migrate

        expect(ActiveRecord::Migrator.current_version).to eq(version - 1)
      end
    end
  end

  context 'specifing connection vars and parsing tablename', integration: true do
    let(:host)      { 'test_host' }
    let(:user)      { 'test_user' }
    let(:password)  { '<PASSWORD>' }
    let(:db_name)   { 'test_db' }

    let(:version) { 1 }

    before { allow(PerconaMigrator::Runner).to receive(:execute) }

    before do
      allow(ENV).to receive(:[]).with('PERCONA_DB_HOST').and_return(host)
      allow(ENV).to receive(:[]).with('PERCONA_DB_USER').and_return(user)
      allow(ENV).to receive(:[]).with('PERCONA_DB_PASSWORD').and_return(password)
      allow(ENV).to receive(:[]).with('PERCONA_DB_NAME').and_return(db_name)
    end

    it 'executes the percona command with the right connection details' do
      ActiveRecord::Migrator.new(
        direction,
        [MIGRATION_FIXTURES],
        version
      ).migrate

      expect(PerconaMigrator::Runner).to(
        have_received(:execute)
        .with(include("-h #{host} -u #{user} -p #{password} D=#{db_name},t=comments"), kind_of(NullLogger))
      )
    end

    context 'when there is no password' do
      before do
        allow(ENV).to receive(:[]).with('PERCONA_DB_PASSWORD').and_return(nil)
      end

      it 'executes the percona command with the right connection details' do
        ActiveRecord::Migrator.new(
          direction,
          [MIGRATION_FIXTURES],
          version
        ).migrate

        expect(PerconaMigrator::Runner).to(
          have_received(:execute)
          .with(include("-h #{host} -u #{user} D=#{db_name},t=comments"), kind_of(NullLogger))
        )
      end
    end
  end

  context 'adding/removing indexes' do
{'fraction_non_alphanumeric': 0.11185140318998586, 'fraction_numerical': 0.0012113870381586917, 'mean_word_length': 2.307846410684474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6310840', 'n_tokens_mistral': 3094, 'n_tokens_neox': 3000, 'n_words': 613}