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

index_exists? doesn't match column names in PostgreSQL for lower(name) #36739

Closed
stanhu opened this issue Jul 23, 2019 · 0 comments · Fixed by #36740
Closed

index_exists? doesn't match column names in PostgreSQL for lower(name) #36739

stanhu opened this issue Jul 23, 2019 · 0 comments · Fixed by #36740

Comments

@stanhu
Copy link
Contributor

stanhu commented Jul 23, 2019

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  gem "pg"
end

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

TABLE_NAME = "some_table_name"
INDEX_NAME = "some_index_name"
PG_HOST = '/tmp'

# Make sure to setup your connection config!
conn_config = {
  dbname: "your_database",
  #  host: "host",
  host: PG_HOST,
  port: 5432,
  user: "",
  # password: "password"
}

# Some unique db_name
db_name = "pull_request_test_#{SecureRandom.hex}"

db_conn = PG.connect(conn_config)
db_conn.exec("CREATE DATABASE #{db_name}")

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: db_name, host: PG_HOST)

ActiveRecord::Schema.define do
  create_table TABLE_NAME, force: true do |t|
    t.string :column1
  end

end

class AddIndexToPublicSchema < ActiveRecord::Migration[5.2]
  def up
    add_index TABLE_NAME, 'lower((column1)::text)', name: INDEX_NAME
  end
end

class BugTest < Minitest::Test
  def test_failed_add_index
    AddIndexToPublicSchema.migrate(:up)
    assert ActiveRecord::Base.connection.index_exists?(TABLE_NAME, "lower((column1)::text)", name: INDEX_NAME)
  end
end

This appears to happen because indexes(TABLE_NAME).columns returns lower(column1). However,

column_names = Array(column_name).map(&:to_s)
checks = []
checks << lambda { |i| i.columns == column_names }
expects the list of columns to be an array.

The workaround appears to use index_name_exists? to skip the column name check.

Expected behavior

index_exists? returns true.

Actual behavior

index_exists? returns false.

System configuration

Rails version: 5.2.3

Ruby version: 2.6.3

stanhu added a commit to stanhu/rails that referenced this issue Jul 25, 2019
Previously Rails expected indexes to be an array of columns, but for
PostgreSQL a expression index can just be a string of text. Handle this
by forcing `Index#columns` to be an Array inside `index_exists?`.

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

Successfully merging a pull request may close this issue.

1 participant