On Monday, 22 February 2016 17:52:28 UTC+2, Jeremy Evans wrote:
>
> On Monday, February 22, 2016 at 2:10:49 AM UTC-8, Stefan Slaveykov wrote:
>>
>> Sure. Let me first add that i`m using rails, so i also added the 
>> https://github.com/TalentBox/sequel-rails gem for backwards 
>> compatibility with ActiveRecord (i didn't clarify this in the beginning). 
>> So, as per the gem documentation i changed a couple of things. I made some 
>> progress on this and maybe the 'problem' has nothing to do with Sequel.
>>
>> In my config/application.rb i have:
>>
>> config.sequel.after_connect = proc do
>>       Sequel::Model.plugin :timestamps, update_on_create: true
>>       Sequel::Model.plugin :pg_typecast_on_load, :tokens
>>       Sequel.extension :pg_array
>>       Sequel.extension :pg_json
>> end
>> The docs <https://github.com/TalentBox/sequel-rails#enabling-plugins> 
>> suggest 
>> that this is the right way to do it. I also tried having the plugins and 
>> extensions loaded in the model, but to no good.
>>
>> In my app/modes/user.rb i have:
>>
>> class User < Sequel::Model
>>   ...
>>   def generate_auth_token!
>>     token = SecureRandom.urlsafe_base64(nil, false)
>>     token_hash = ::BCrypt::Password.create(token)
>>
>>     self.tokens = Sequel.pg_json({
>>         token: token_hash,
>>         expiry: TOKEN_EXPIRY,
>>         updated_at: Time.now
>>     })
>>     save(raise_on_failure: true)
>>   end
>>
>> end
>>
>> In db/migrate i have:
>>
>> Sequel.migration do
>>   change do
>>     create_table(:users) do
>>       primary_key :id
>>       ....
>>       Json :tokens
>>       ......
>>     end
>>   end
>> end
>>
>> Given this setup i have a spec:
>>
>> describe '#generate_auth_token' do
>>     it 'generates and authentication token' do
>>       expect(subject.tokens).to be_nil
>>
>>       subject.generate_auth_token!
>>
>>       expect(subject.tokens).not_to be_a(String)
>>       expect(subject.tokens).to have_key(:token) &
>>                                 have_key(:expiry) &
>>                                 have_key(:updated_at)
>>     end
>>   end
>>
>> The funny thing is that this spec is passing. The tokens column is 
>> properly formatted. *However *when i reload the model the spec 
>> fails.(this is how everything started, actually, i'm reloading the model in 
>> a request spec). So if i do this:
>>
>> describe '#generate_auth_token' do
>>     it 'generates and authentication token' do
>>       expect(subject.tokens).to be_nil
>>
>>       subject.generate_auth_token!
>>       subject.reload
>>
>>       expect(subject.tokens).not_to be_a(String)
>>       expect(subject.tokens).to have_key(:token) &
>>                                 have_key(:expiry) &
>>                                 have_key(:updated_at)
>>     end
>>   end
>>
>> I get: 
>>
>> Failure/Error: expect(subject.tokens).not_to be_a(String)
>>
>>        expected 
>> "{\"token\":\"$2a$10$33MwL5OuPKzhATJgEk.GiOO8T6RIWXnJfc26enzWYv3mqzzhFCRW.\",\"expiry\":\"2016-02-29T12:03:22.821+02:00\",\"updated_at\":\"2016-02-22T12:03:23.072+02:00\"}"
>>  
>> not to be a kind of String
>>
>>
>> For this particular case i don`t need to reload, but in other cases i do. 
>> In the end probably this has nothing to do with Sequel?
>>
>
> If it works when you aren't reloading but not when you are reloading, I 
> would guess that something is overriding reload or one of the related 
> internal methods and not calling super.  Of course, the main issue is that 
> the database is returning json types as strings, which shouldn't be 
> happening if you are using the postgres or jdbc/postgresql adapters.
>
> Here's some sample code that works here:
>
> DB.create_table!(:bs) do
>   primary_key :id
>   json :tokens
> end
>
> DB.extension :pg_array, :pg_json
>
> class B < Sequel::Model
> end
>
> B.create(:tokens=>{'token'=>'foo'})
> b = B.first
> b.tokens
> b.reload.tokens
>
> Is that broken in your environment?  If so, please post details about your 
> environment, such as what database driver you are using and what version of 
> PostgreSQL.  Are you using PostgreSQL < 9.2 or a database that was 
> originally using PostgreSQL <9.2 with json added via extension instead of 
> in core?
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to