<commit_before>module ArPglogicalMigration
  module PglogicalMigrationHelper
    def self.migrations_column_present?
      ActiveRecord::Base.connection.columns("miq_regions").any? { |c| c.name == "migrations_ran" }
    end

    def self.my_region_number
      @my_region_number ||= ApplicationRecord.my_region_number
    end

    def self.my_region_created?
      ActiveRecord::Base.connection.exec_query(<<~SQL).first["exists"]
        SELECT EXISTS(
          SELECT id FROM miq_regions WHERE region = #{ActiveRecord::Base.connection.quote(my_region_number)}
        )
      SQL
    end

    def self.update_local_migrations_ran(version, direction)
      return unless migrations_column_present?
      return unless my_region_created?

      new_migrations = ActiveRecord::SchemaMigration.normalized_versions
      new_migrations << version if direction == :up
      migrations_value = ActiveRecord::Base.connection.quote(PG::TextEncoder::Array.new.encode(new_migrations))

      ActiveRecord::Base.connection.exec_query(<<~SQL)
        UPDATE miq_regions
        SET migrations_ran = #{migrations_value}
        WHERE region = #{ActiveRecord::Base.connection.quote(my_region_number)}
      SQL
    end
  end

  class RemoteRegionMigrationWatcher
    class HelperARClass < ActiveRecord::Base; end

    attr_reader :region, :subscription, :version

    def initialize(subscription, version)
      region_class  = Class.new(ApplicationRecord) { self.table_name = "miq_regions" }
      @region       = region_class.find_by(:region => subscription.provider_region)
      @subscription = subscription
      @version      = version
    end

    def wait_for_remote_region_migration(wait_time = 1)
      return unless wait_for_migration?

      Vmdb.rails_logger.info(wait_message)
      print(wait_message)
<commit_msg>Clarify use of ApplicationRecord vs ActiveRecord::Base<commit_after>module ArPglogicalMigration
  module PglogicalMigrationHelper
    def self.migrations_column_present?
      ActiveRecord::Base.connection.columns("miq_regions").any? { |c| c.name == "migrations_ran" }
    end

    def self.my_region_number
      # Use ApplicationRecord here because we need to query region information
      @my_region_number ||= ApplicationRecord.my_region_number
    end

    def self.my_region_created?
      ActiveRecord::Base.connection.exec_query(<<~SQL).first["exists"]
        SELECT EXISTS(
          SELECT id FROM miq_regions WHERE region = #{ActiveRecord::Base.connection.quote(my_region_number)}
        )
      SQL
    end

    def self.update_local_migrations_ran(version, direction)
      return unless migrations_column_present?
      return unless my_region_created?

      new_migrations = ActiveRecord::SchemaMigration.normalized_versions
      new_migrations << version if direction == :up
      migrations_value = ActiveRecord::Base.connection.quote(PG::TextEncoder::Array.new.encode(new_migrations))

      ActiveRecord::Base.connection.exec_query(<<~SQL)
        UPDATE miq_regions
        SET migrations_ran = #{migrations_value}
        WHERE region = #{ActiveRecord::Base.connection.quote(my_region_number)}
      SQL
    end
  end

  class RemoteRegionMigrationWatcher
    class HelperARClass < ActiveRecord::Base; end

    attr_reader :region, :subscription, :version

    def initialize(subscription, version)
      region_class  = Class.new(ActiveRecord::Base) { self.table_name = "miq_regions" }
      @region       = region_class.find_by(:region => subscription.provider_region)
      @subscription = subscription
      @version      = version
    end

    def wait_for_remote_region_migration(wait_time = 1)
      return unless wait_for_migration?

      Vmdb.rails_logger.info(wait_message)
      print(wait_message)

{'fraction_non_alphanumeric': 0.08446576072475354, 'fraction_numerical': 0.0005329070077271516, 'mean_word_length': 3.770012706480305, 'pattern_counts': {'":': 0, '<': 17, '<?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': '3492551', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1083, 'n_words': 201}