Hey Jeremy,

The pg_json and pg_array extensions are awesome. However, I see people 
often tripped about the load order, in the sense that they try to load the 
extension after model definitions (that is, after the schema has already 
been parsed), and then it silently doesn't work for them. I have two 
improvements to this problem, which are not mutually exclusive (both can be 
added).

First one is that we add a warning when pg_json is loaded after schema has 
already been parsed. As far as I know, schema is parsed only once, on first 
model definition, am I correct? The documentation of pg_json extension 
could also be updated to note that pg_json should be loaded before model 
definitions.

Second one is, when loading the pg_json extension, if the schema is already 
parsed, we could reiterate over it and change the :type to :json/:jsonb if 
the :db_type is "json"/"jsonb". I'm aware that this is a performance 
penalty, but the alternative is that typecasting simply doesn't work. And 
people can always avoid the performance penalty by loading the extension 
before schema parsing.

Another reason why I would like the second one is that loading the 
extension before schema parsing isn't always that easy. In my last job I 
had a Sinatra application which lazily set the database connection 
(accessible via App.database). And then the model was instantiated via 
`class Post < Sequel::Model(App.database)`. This was done because we were 
communicating with other databases as well. We couldn't put 
`App.database.extension :pg_json` in the Sequel initializer, because at the 
moment of requiring the initializer `App` wasn't defined yet. But the 
`App.database` in model definitions worked because model files were 
autoloaded, so the `App` was defined. After some investigating I somehow 
found the solution: to use `Sequel::Database.extension :pg_json` in the 
Sequel initializer, which was exactly what I wanted. However, it took me a 
while to find out about it. If pg_json extension updated my parsed schema, 
I'm pretty sure I would have no problem paying that small performance 
penalty. And it kind of makes sense, to be able to do:

class Post < Sequel::Model(App.database)
  db.extension :pg_json
end

or

App.database.extension :pg_json

class Post < Sequel::Model(App.database)
end

However, now when I wrote all of this, I think I'm more in favour of just 
adding the warning and changing documentation, because the above doesn't 
actually feel right. I can make the PR for whatever we decide, just wanted 
to ask you first what do you think about this.

Regards,
Janko

-- 
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