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

Presence of an expression index causes problems when removing indexes #26635

Closed
benubois opened this issue Sep 26, 2016 · 2 comments
Closed

Presence of an expression index causes problems when removing indexes #26635

benubois opened this issue Sep 26, 2016 · 2 comments

Comments

@benubois
Copy link
Contributor

Steps to reproduce

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"
  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.0.0.1"
  gem "pg"
end

require "active_record"
require "minitest/autorun"
require "logger"

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection("postgres://postgres@localhost:5432/test")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.text :email
    t.string :token
  end
  execute "CREATE UNIQUE INDEX index_users_on_lower_email ON users (LOWER(email))"
  add_index :users, :token, unique: true
end

class BugTest < Minitest::Test
  def test_remove_index
    ActiveRecord::Base.connection.remove_index :users, :token
  end
end

Expected behavior

I think this should run without throwing any exceptions.

Actual behavior

This throws a NoMethodError exception.

BugTest#test_remove_index:
NoMethodError: undefined method `join' for "lower(email)":String
Did you mean?  JSON
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:1210:in `block in index_name_for_remove'
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:1215:in `block (2 levels) in index_name_for_remove'
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:1215:in `each'
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:1215:in `all?'
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:1215:in `block in index_name_for_remove'
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:1215:in `select'
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:1215:in `index_name_for_remove'
    /Users/ben/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:563:in `remove_index'
    bug.rb:37:in `test_remove_index'

System configuration

Rails version:
5.0.0.1

Ruby version:
2.3.1

Postgres version:
9.5.0.0

It looks like index_name_for_remove is expecting i.columns to be an array of strings, but in the case of an expression index it is just a string.

This was added in 8ceb883 by @greysteil.

@utilum
Copy link
Contributor

utilum commented Sep 27, 2016

The test reproduces on my machine.

@uaihebert
Copy link

uaihebert commented Sep 28, 2016

I was able to find where the problem is:

image

For the index created by the SQL command the returned columns value is a pure string. That is why is breaking when we fire the query.

When I get home I will fix it. ( :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants