Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output a warning when the index kwarg is passed to #add_column. #39834

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -606,6 +606,14 @@ def drop_table(table_name, **options)
def add_column(table_name, column_name, type, **options)
return if options[:if_not_exists] == true && column_exists?(table_name, column_name, type)

if options.key?(:index)
warn <<~WARNING
WARNING: #add_column ignores the :index keyword argument.

To create an index, use #add_index.
WARNING
end

at = create_alter_table table_name
at.add_column(column_name, type, **options)
execute schema_creation.accept at
Expand Down
44 changes: 44 additions & 0 deletions activerecord/test/cases/add_column_index_test.rb
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require "cases/helper"

class AddColumnIndexTest < ActiveRecord::TestCase
INDEX_KWARG_WARNING = <<~WARNING
WARNING: #add_column ignores the :index keyword argument.

To create an index, use #add_index.
WARNING

setup do
@connection = ActiveRecord::Base.connection

@connection.create_table("add_column_tests", force: true) do |t|
t.string "name"
end
end

teardown do
@connection.drop_table "add_column_tests", if_exists: true
end

def test_no_index_kwarg
warning = capture(:stderr) do
@connection.add_column :add_column_tests, :height, :integer
end
assert_no_match(INDEX_KWARG_WARNING, warning)
end

def test_index_true_kwarg
warning = capture(:stderr) do
@connection.add_column :add_column_tests, :height, :integer, index: true
end
assert_match(INDEX_KWARG_WARNING, warning)
end

def test_index_false_kwarg
warning = capture(:stderr) do
@connection.add_column :add_column_tests, :height, :integer, index: false
end
assert_match(INDEX_KWARG_WARNING, warning)
end
end