<commit_before>#
#  Copyright (c) 2011, SoundCloud Ltd., <NAME>, <NAME>, Tobias
#  Schmidt
#

require 'lhm'
require File.expand_path(File.dirname(__FILE__)) + '/integration_helper'

describe Lhm do
  include IntegrationHelper

  before(:each) { connect_master! }

  describe "changes" do
    before(:each) do
      table_create(:users)
    end

    it "should add a column" do
      Lhm.change_table(:users) do |t|
        t.add_column(:logins, "INT(12) DEFAULT '0'")
      end

      slave do
        table_read(:users).columns["logins"].must_equal({
          :type => "int(12)",
          :metadata => "DEFAULT '0'"
        })
      end
    end

    it "should copy all rows" do
      23.times { |n| execute("insert into users set reference = '#{ n }'") }

      Lhm.change_table(:users) do |t|
        t.add_column(:logins, "INT(12) DEFAULT '0'")
      end

      slave do
        count_all(:users).must_equal(23)
      end
    end

    it "should remove a column" do
      Lhm.change_table(:users) do |t|
        t.remove_column(:comment)
      end

      slave do
        table_read(:users).columns["comment"].must_equal nil
      end
    end

    it "should add an index" do
      Lhm.change_table(:users) do |t|
        t.add_index([:comment, :created_at])
      end

      slave do
        key?(table_read(:users), [:comment, :created_at]).must_equal(true)
      end
    end

    it "should add a unqiue index" do
      Lhm.change_table(:users) do |t|
        t.add_unique_index(:comment)
      end

      slave do
        key?(table_read(:users), :comment, :unique).must_equal(true)
      end
    end

    it "should remove an index" do
      Lhm.change_table(:users) do |t|
        t.remove_index([:username, :created_at])
      end

      slave do
        key?(table_read(:users), ["username", "created_at"]).must_equal(false)
      end
    end

    it "should apply a ddl statement" do
      Lhm.change_table(:users) do |t|
        t.ddl("alter table %s add column flag tinyint(1)" % t.name)
      end

      slave do
        table_read(:users).columns["flag"].must_equal({
          :type => "tinyint(1)",
          :metadata => "DEFAULT NULL"
        })
      end
    end

    describe "parallel" do
      it "should perserve inserts during migration" do
        50.times { |n| execute("insert into users set reference = '#{ n }'") }

        insert = Thread.new do
          10.times do |n|
            execute("insert into users set reference = '#{ 100 + n }'")<commit_msg>added slave blocks to lhm<commit_after>#
#  Copyright (c) 2011, SoundCloud Ltd., <NAME>, <NAME>, Tobias
#  Schmidt
#

require 'lhm'
require File.expand_path(File.dirname(__FILE__)) + '/integration_helper'

describe Lhm do
  include IntegrationHelper

  before(:each) { connect_master! }

  describe "changes" do
    before(:each) do
      table_create(:users)
    end

    it "should add a column" do
      Lhm.change_table(:users) do |t|
        t.add_column(:logins, "INT(12) DEFAULT '0'")
      end

      slave do
        table_read(:users).columns["logins"].must_equal({
          :type => "int(12)",
          :metadata => "DEFAULT '0'"
        })
      end
    end

    it "should copy all rows" do
      23.times { |n| execute("insert into users set reference = '#{ n }'") }

      Lhm.change_table(:users) do |t|
        t.add_column(:logins, "INT(12) DEFAULT '0'")
      end

      slave do
        count_all(:users).must_equal(23)
      end
    end

    it "should remove a column" do
      Lhm.change_table(:users) do |t|
        t.remove_column(:comment)
      end

      slave do
        table_read(:users).columns["comment"].must_equal nil
      end
    end

    it "should add an index" do
      Lhm.change_table(:users) do |t|
        t.add_index([:comment, :created_at])
      end

      slave do
        key?(table_read(:users), [:comment, :created_at]).must_equal(true)
      end
    end

    it "should add a unqiue index" do
      Lhm.change_table(:users) do |t|
        t.add_unique_index(:comment)
      end

      slave do
        key?(table_read(:users), :comment, :unique).must_equal(true)
      end
    end

    it "should remove an index" do
      Lhm.change_table(:users) do |t|
        t.remove_index([:username, :created_at])
      end

      slave do
        key?(table_read(:users), [:username, :created_at]).must_equal(false)
      end
    end

    it "should apply a ddl statement" do
      Lhm.change_table(:users) do |t|
        t.ddl("alter table %s add column flag tinyint(1)" % t.name)
      end

      slave do
        table_read(:users).columns["flag"].must_equal({
          :type => "tinyint(1)",
          :metadata => "DEFAULT NULL"
        })
      end
    end

    describe "parallel" do
      it "should perserve inserts during migration" do
        50.times { |n| execute("insert into users set reference = '#{ n }'") }

        insert = Thread.new do
          10.times do |n|
            execute("insert into users set reference = '#{ 100 + n }'")
{'fraction_non_alphanumeric': 0.12785571142284569, 'fraction_numerical': 0.010420841683366733, 'mean_word_length': 2.2599608099281516, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '4412916', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1585, 'n_words': 432}