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

Code before:
        def fetch_type_metadata(sql_type)
          cast_type = lookup_cast_type(sql_type)
          SqlTypeMetadata.new(
            sql_type: sql_type,
            type: cast_type.type,
            limit: cast_type.limit,
            precision: cast_type.precision,
            scale: cast_type.scale,
          )
        end

        def index_column_names(column_names)
          if expression_column_name?(column_names)
            column_names
          else
            Array(column_names)
          end
        end

        def index_name_options(column_names)
          if expression_column_name?(column_names)
            column_names = column_names.scan(/\w+/).join("_")
          end

          { column: column_names }
        end

        # Try to identify whether the given column name is an expression
        def expression_column_name?(column_name)
          column_name.is_a?(String) && /\W/.match?(column_name)
        end

        def strip_table_name_prefix_and_suffix(table_name)
          prefix = Base.table_name_prefix
          suffix = Base.table_name_suffix
          table_name.to_s =~ /#{prefix}(.+)#{suffix}/ ? $1 : table_name.to_s
        end

        def foreign_key_name(table_name, options)
          options.fetch(:name) do
            identifier = "#{table_name}_#{options.fetch(:column)}_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?
          foreign_keys(from_table).detect { |fk| fk.defined_for?(**options) }
        end

Review comment (suggestion):
Wouldn't all this be much simpler if `:_skip_validate_options_keys` was part of `valid_primary_key_option_keys` etc? I don't really see a downside in doing this.


Code after:

        def valid_primary_key_options
          [:limit, :default, :precision]
        end

        def validate_create_table_options!(options)
          unless options[:_skip_validate_options]
            options
              .except(:_uses_legacy_table_name, :_skip_validate_options)
              .assert_valid_keys(valid_table_definition_options, valid_primary_key_options)
          end
        end

        def fetch_type_metadata(sql_type)
          cast_type = lookup_cast_type(sql_type)
          SqlTypeMetadata.new(
            sql_type: sql_type,
            type: cast_type.type,
            limit: cast_type.limit,
            precision: cast_type.precision,
            scale: cast_type.scale,
          )
        end

        def index_column_names(column_names)
          if expression_column_name?(column_names)
            column_names
          else
            Array(column_names)
          end
        end

        def index_name_options(column_names)
          if expression_column_name?(column_names)
            column_names = column_names.scan(/\w+/).join("_")
          end

          { column: column_names }
        end

        # Try to identify whether the given column name is an expression
        def expression_column_name?(column_name)
          column_name.is_a?(String) && /\W/.match?(column_name)
        end

        def strip_table_name_prefix_and_suffix(table_name)
          prefix = Base.table_name_prefix
          suffix = Base.table_name_suffix
          table_name.to_s =~ /#{prefix}(.+)#{suffix}/ ? $1 : table_name.to_s
        end