<commit_before>require 'spec_helper'
require 'pathname'
require 'childprocess'
require 'active_record'
require 'logger'
require 'ruby_event_store'
require 'ruby_event_store/spec/event_repository_lint'

RSpec.describe "limit_for_event_id_migration" do
  include SchemaHelper

  MigrationRubyCode = File.read(File.expand_path('../../lib/rails_event_store_active_record/generators/templates/limit_for_event_id_template.rb', __FILE__) )
  migration_version = rails_dependent("", "[4.2]")
  MigrationRubyCode.gsub!("<%= migration_version %>", migration_version)

  specify do
    begin
      establish_database_connection
      load_database_schema
      skip("in-memory sqlite cannot run this test") if ENV['DATABASE_URL'].include?(":memory:")
      dump_current_schema
      drop_existing_tables_to_clean_state
      establish_database_connection
      fill_data_using_older_gem
      run_the_migration
      reset_columns_information
      compare_new_schema
    ensure
      drop_database
    end
  end

  private


  def fill_data_using_older_gem
    pathname = Pathname.new(__FILE__).dirname
    cwd = pathname.join("before_limit_for_event_id")
    FileUtils.rm(cwd.join("Gemfile.lock")) if File.exists?(cwd.join("Gemfile.lock"))
    process = ChildProcess.build("bundle", "exec", "ruby", "fill_data.rb")
    process.environment['BUNDLE_GEMFILE'] = cwd.join('Gemfile')
    process.environment['DATABASE_URL']   = ENV['DATABASE_URL']
    process.environment['RAILS_VERSION']  = ENV['RAILS_VERSION']
    process.cwd = cwd
    process.io.stdout = $stdout
    process.io.stderr = $stderr
    process.start
    begin
      process.poll_for_exit(10)
    rescue ChildProcess::TimeoutError
      process.stop
    end
    expect(process.exit_code).to eq(0)
  end

  def run_the_migration
    eval(MigrationRubyCode)
    LimitForEventId.new.up
  end

  def reset_columns_information
    RailsEventStoreActiveRecord::Event.reset_column_information
    RailsEventStoreActiveRecord::EventInStream.reset_column_information
  end

  def drop_existing_tables_to_clean_state
    drop_database
  end

  def dump_current_schema
    @schema = StringIO.new
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, @schema)
    @schema.rewind
    @schema = @schema.read
  end

  def compare_new_schema
    schema = StringIO.new
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, schema)
    schema.rewind
    schema = schema.read
    expect(schema).to eq(@schema)
  end
end<commit_msg>Check only changes not whole schema<commit_after>require 'spec_helper'
require 'pathname'
require 'childprocess'
require 'active_record'
require 'logger'
require 'ruby_event_store'
require 'ruby_event_store/spec/event_repository_lint'

RSpec.describe "limit_for_event_id_migration" do
  include SchemaHelper

  MigrationRubyCode = File.read(File.expand_path('../../lib/rails_event_store_active_record/generators/templates/limit_for_event_id_template.rb', __FILE__) )
  migration_version = rails_dependent("", "[4.2]")
  MigrationRubyCode.gsub!("<%= migration_version %>", migration_version)

  specify do
    begin
      establish_database_connection
      fill_data_using_older_gem
      before = RailsEventStoreActiveRecord::EventInStream.columns
        .select{|c| c.name == 'event_id'}.first
      expect(before .limit).to eq(nil)

      run_the_migration
      reset_columns_information
      after = RailsEventStoreActiveRecord::EventInStream.columns
        .select{|c| c.name == 'event_id'}.first
      expect(after.limit).to eq(36)
    ensure
      drop_database
    end
  end

  private


  def fill_data_using_older_gem
    pathname = Pathname.new(__FILE__).dirname
    cwd = pathname.join("before_limit_for_event_id")
    FileUtils.rm(cwd.join("Gemfile.lock")) if File.exists?(cwd.join("Gemfile.lock"))
    process = ChildProcess.build("bundle", "exec", "ruby", "fill_data.rb")
    process.environment['BUNDLE_GEMFILE'] = cwd.join('Gemfile')
    process.environment['DATABASE_URL']   = ENV['DATABASE_URL']
    process.environment['RAILS_VERSION']  = ENV['RAILS_VERSION']
    process.cwd = cwd
    process.io.stdout = $stdout
    process.io.stderr = $stderr
    process.start
    begin
      process.poll_for_exit(10)
    rescue ChildProcess::TimeoutError
      process.stop
    end
    expect(process.exit_code).to eq(0)
  end

  def run_the_migration
    eval(MigrationRubyCode)
    LimitForEventId.new.up
  end

  def reset_columns_information
    RailsEventStoreActiveRecord::Event.reset_column_information
    RailsEventStoreActiveRecord::EventInStream.reset_column_information
  end
end
{'fraction_non_alphanumeric': 0.0979538528515455, 'fraction_numerical': 0.00261210274270788, 'mean_word_length': 4.399529964747356, 'pattern_counts': {'":': 1, '<': 5, '<?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': '3438335', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1457, 'n_words': 233}