ProctorU / squint

Search PostgreSQL jsonb and hstore columns
MIT License
26 stars 3 forks source link

Fix HASH_DATA_COLUMNS to not be shared between ActiveRecord models #20

Closed jamescook closed 3 years ago

jamescook commented 4 years ago

The constant HASH_DATA_COLUMNS is being defined on the concern and not the class extending the concern. This causes problems if you happen to have mixed jsonb/hstore columns with the same name across tables.

Consider this failing test scenario in master:

class User < ActiveRecord::Base
  include Storext.model
  include Squint

  store_attribute :settings, :zip_code, String, default: '90210' # this is hstore
end

class Post < ActiveRecord::Base
  include Storext.model
  include Squint
  # ... skip a bunch of unrelated settings ...
  store_attribute :settings, :zip_code, String, default: '90210' # this is jsonb
end

test 'HASH_DATA_COLUMNS is not shared between models' do 
  assert_equal('hstore', User::HASH_DATA_COLUMNS[:settings])
  assert_equal('jsonb' , Post::HASH_DATA_COLUMNS[:settings])
 end

Test Output without the fix:

Failure:
SquintTest#test_HASH_DATA_COLUMNS_is_not_shared_between_models [/Users/jamescook/open_source/squint/test/squint_test.rb:137]:
Expected: "hstore"
  Actual: "jsonb"

Also, I think this is the true source of https://github.com/ProctorU/squint/pull/19

alexcp91 commented 4 years ago

Thanks for this great gem. Any chance we can push through this PR?

dwilkins commented 3 years ago

Combined the tests from this PR into #21 - closing this one