Repository: rails/rails
File: activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb (Ruby)

Code before:
            identifier = "#{table_name}_#{columns * '_and_'}_fk"
            hashed_identifier = OpenSSL::Digest::SHA256.hexdigest(identifier).first(10)

            "fk_rails_#{hashed_identifier}"
          end
        end

        def foreign_key_for(from_table, **options)
          return unless use_foreign_keys?

          if options[:_skip_column_match]
            return foreign_keys(from_table).detect { |fk| fk.defined_for?(**options) }
          end

          keys = foreign_keys(from_table)

          if options[:column]
            expected_columns = Array(options[:column]).map(&:to_s)
            keys.select! { |fk| Array(fk.options[:column]).map(&:to_s) == expected_columns }
          elsif options[:to_table] && !options.key?(:primary_key) && keys.size > 1
            default_column = foreign_key_column_for(options[:to_table], "id").to_s
            match = keys.find { |fk| Array(fk.options[:column]).map(&:to_s) == [default_column] }
            keys = [match].compact if match
          end

          keys.detect { |fk| fk.defined_for?(**options) }
        end

        def foreign_key_for!(from_table, to_table: nil, **options)
          foreign_key_for(from_table, to_table: to_table, **options) ||
            raise(ArgumentError, "Table '#{from_table}' has no foreign key for #{to_table || options}")
        end

        def extract_foreign_key_action(specifier)
          case specifier
          when "CASCADE"; :cascade
          when "SET NULL"; :nullify
          when "RESTRICT"; :restrict
          end
        end

        def foreign_keys_enabled?
          @config.fetch(:foreign_keys, true)
        end

        def check_constraint_name(table_name, **options)
          options.fetch(:name) do
            expression = options.fetch(:expression)
            identifier = "#{table_name}_#{expression}_chk"
            hashed_identifier = OpenSSL::Digest::SHA256.hexdigest(identifier).first(10)


Review comment (suggestion):
Haven't tested, but something like this is shorter and should work too
```suggestion
          keys = foreign_keys(from_table)
          
          if keys.size == 1 || options[:_skip_column_match]
            keys.find { |fk| fk.defined_for?(**options) }
          elsif options[:column]
            expected_columns = Array(options[:column]).map(&:to_s)
            keys = keys.select { |fk| Array(fk.column) == expected_columns }
            keys.find { |fk| fk.defined_for?(**options) }
          else
            default_column = foreign_key_column_for(options[:to_table], "id")
            keys.find { |fk| fk.column == default_column }
          end
```

Code after:
            identifier = "#{table_name}_#{columns * '_and_'}_fk"
            hashed_identifier = OpenSSL::Digest::SHA256.hexdigest(identifier).first(10)

            "fk_rails_#{hashed_identifier}"
          end
        end

        def foreign_key_for(from_table, **options)
          return unless use_foreign_keys?

          keys = foreign_keys(from_table)

          if options[:_skip_column_match]
            return keys.find { |fk| fk.defined_for?(**options) }
          end

          if options[:column].nil?
            default_column = foreign_key_column_for(options[:to_table], "id")
            matches = keys.select { |fk| fk.column == default_column }
            keys = matches if matches.any?
          end

          keys.find { |fk| fk.defined_for?(**options) }
        end

        def foreign_key_for!(from_table, to_table: nil, **options)
          foreign_key_for(from_table, to_table: to_table, **options) ||
            raise(ArgumentError, "Table '#{from_table}' has no foreign key for #{to_table || options}")
        end

        def extract_foreign_key_action(specifier)
          case specifier
          when "CASCADE"; :cascade
          when "SET NULL"; :nullify
          when "RESTRICT"; :restrict
          end
        end

        def foreign_keys_enabled?
          @config.fetch(:foreign_keys, true)
        end

        def check_constraint_name(table_name, **options)
          options.fetch(:name) do
            expression = options.fetch(:expression)
            identifier = "#{table_name}_#{expression}_chk"
            hashed_identifier = OpenSSL::Digest::SHA256.hexdigest(identifier).first(10)

            "chk_rails_#{hashed_identifier}"
          end
        end