<commit_before>module ActiveRecord #:nodoc:
  module ConnectionAdapters #:nodoc:
    module OracleEnhancedSchemaDumper #:nodoc:

      def self.included(base) #:nodoc:
        base.class_eval do
          private
          alias_method_chain :tables, :oracle_enhanced
          alias_method_chain :indexes, :oracle_enhanced
        end
      end

      private
      
      def tables_with_oracle_enhanced(stream)
        sorted_tables = @connection.tables.sort
        sorted_tables.each do |tbl|
          # add table prefix or suffix for schema_migrations
          next if [ActiveRecord::Migrator.proper_table_name('schema_migrations'), ignore_tables].flatten.any? do |ignored|
            case ignored
            when String; tbl == ignored
            when Regexp; tbl =~ ignored
            else
              raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
            end
          end
          # change table name inspect method
          tbl.extend TableInspect
          table(tbl, stream)
          # add primary key trigger if table has it
          primary_key_trigger(tbl, stream)
        end
        sorted_tables.each do |tbl|
          # add foreign keys if table has them
          foreign_keys(tbl, stream)
        end
        # add synonyms in local schema
        synonyms(stream)
      end

      def primary_key_trigger(table_name, stream)
        if @connection.respond_to?(:has_primary_key_trigger?) && @connection.has_primary_key_trigger?(table_name)
          pk, pk_seq = @connection.pk_and_sequence_for(table_name)
          stream.print "  add_primary_key_trigger #{table_name.inspect}"
          stream.print ", :primary_key => \"#{pk}\"" if pk != 'id'
          stream.print "\n\n"
        end
      end

      def foreign_keys(table_name, stream)
        if (foreign_keys = @connection.foreign_keys(table_name)).any?
          add_foreign_key_statements = foreign_keys.map do |foreign_key|
            statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
            statement_parts << foreign_key.to_table.inspect
            statement_parts << (':name => ' + foreign_key.options[:name].inspect)
            
            if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
              statement_parts << (':column => ' + foreign_key.options[:column].inspect)
            end
            if foreign_key.options[:primary_key] != 'id'
              statement_parts << (':primary_key => ' + foreign_key.options[:primary_key].inspect)
            end
            unless foreign_key.options[:dependent].blank?
              statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
            end

            '  ' + statement_parts.join(', ')
          end

          stream.puts add_foreign_key_statements.sort.join("\n")
          stream.puts
        end
      end

      def synonyms(stream)
        syns = @connection.synonyms
        syns.each do |syn|
          table_name = syn.table_name
          table_name = "#{syn.table_owner}.#{table_name}" if syn.table_owner
          table_name = "#{table_name}@#{syn.db_link}" if syn.db_link
          stream.print "  add_synonym #{syn.name.inspect}, #{table_name.inspect}, :force => true"
          stream.puts
        end
        stream.puts unless syns.empty?
      end

      def indexes_with_oracle_enhanced(table, stream)
        if (indexes = @connection.indexes(table)).any?
          add_index_statements = indexes.map do |index|
            # use table.inspect as it will remove prefix and suffix
            statment_parts = [ ('add_index ' + table.inspect) ]
            statment_parts << index.columns.inspect
            statment_parts << (':name => ' + index.name.inspect)
            statment_parts << ':unique => true' if index.unique

            '  ' + statment_parts.join(', ')
          end

          stream.puts add_index_statements.sort.join("\n")
          stream.puts
        end
      end

      # remove table name prefix and suffix when doing #inspect (which is used in tables method)
      module TableInspect #:nodoc:
        def inspect<commit_msg>do not call oracle_enhanced specific schema dump methods on other adapters<commit_after>module ActiveRecord #:nodoc:
  module ConnectionAdapters #:nodoc:
    module OracleEnhancedSchemaDumper #:nodoc:

      def self.included(base) #:nodoc:
        base.class_eval do
          private
          alias_method_chain :tables, :oracle_enhanced
          alias_method_chain :indexes, :oracle_enhanced
        end
      end

      private
      
      def tables_with_oracle_enhanced(stream)
        sorted_tables = @connection.tables.sort
        sorted_tables.each do |tbl|
          # add table prefix or suffix for schema_migrations
          next if [ActiveRecord::Migrator.proper_table_name('schema_migrations'), ignore_tables].flatten.any? do |ignored|
            case ignored
            when String; tbl == ignored
            when Regexp; tbl =~ ignored
            else
              raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
            end
          end
          # change table name inspect method
          tbl.extend TableInspect
          table(tbl, stream)
          # add primary key trigger if table has it
          primary_key_trigger(tbl, stream)
        end
        sorted_tables.each do |tbl|
          # add foreign keys if table has them
          foreign_keys(tbl, stream)
        end
        # add synonyms in local schema
        synonyms(stream)
      end

      def primary_key_trigger(table_name, stream)
        if @connection.respond_to?(:has_primary_key_trigger?) && @connection.has_primary_key_trigger?(table_name)
          pk, pk_seq = @connection.pk_and_sequence_for(table_name)
          stream.print "  add_primary_key_trigger #{table_name.inspect}"
          stream.print ", :primary_key => \"#{pk}\"" if pk != 'id'
          stream.print "\n\n"
        end
      end

      def foreign_keys(table_name, stream)
        if @connection.respond_to?(:foreign_keys) && (foreign_keys = @connection.foreign_keys(table_name)).any?
          add_foreign_key_statements = foreign_keys.map do |foreign_key|
            statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
            statement_parts << foreign_key.to_table.inspect
            statement_parts << (':name => ' + foreign_key.options[:name].inspect)
            
            if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
              statement_parts << (':column => ' + foreign_key.options[:column].inspect)
            end
            if foreign_key.options[:primary_key] != 'id'
              statement_parts << (':primary_key => ' + foreign_key.options[:primary_key].inspect)
            end
            unless foreign_key.options[:dependent].blank?
              statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
            end

            '  ' + statement_parts.join(', ')
          end

          stream.puts add_foreign_key_statements.sort.join("\n")
          stream.puts
        end
      end

      def synonyms(stream)
        if @connection.respond_to?(:synonyms)
          syns = @connection.synonyms
          syns.each do |syn|
            table_name = syn.table_name
            table_name = "#{syn.table_owner}.#{table_name}" if syn.table_owner
            table_name = "#{table_name}@#{syn.db_link}" if syn.db_link
            stream.print "  add_synonym #{syn.name.inspect}, #{table_name.inspect}, :force => true"
            stream.puts
          end
          stream.puts unless syns.empty?
        end
      end

      def indexes_with_oracle_enhanced(table, stream)
        if (indexes = @connection.indexes(table)).any?
          add_index_statements = indexes.map do |index|
            # use table.inspect as it will remove prefix and suffix
            statment_parts = [ ('add_index ' + table.inspect) ]
            statment_parts << index.columns.inspect
            statment_parts << (':name => ' + index.name.inspect)
            statment_parts << ':unique => true' if index.unique

            '  ' + statment_parts.join(', ')
          end

          stream.puts add_index_statements.sort.join("\n")
          stream.puts
        end
      end

      # remove table name prefix and suffix when doing #inspect (which is used in tables method)
      module TableInspect #:nodoc:
        def inspect
{'fraction_non_alphanumeric': 0.0953046720260981, 'fraction_numerical': 0.0, 'mean_word_length': 2.2005965697240866, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 19, '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': '5630003', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2402, 'n_words': 541}