Re: validation_helpers

2023-10-29 Thread Marcelo
I don't see the *validation_helpers* plugin adding a *before_validation* 
class method:
https://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/ValidationHelpers.html

You can see in the docs how this plugin only adds some *validates_** 
methods, which should be called inside the model's *validate* method.

This *before_validation* class method is something that exists in 
Rails/ActiveRecord; I'm not sure it exists in Sequel.

On Sunday, October 29, 2023 at 11:40:32 AM UTC+1 bee...@gmail.com wrote:

> For some reason I can't get the validation_helpers to load:
>
>
> In the models loader:
> *Sequel::Model.plugin :validation_helpers*
>
> Models are loaded after helpers.  Inside the class that's been working:
> *plugin :validation_helpers*
>
> Error:
> *undefined method `before_validation' for Carrier:Class*
>
> It seems like everything is in order, however it isn't loading.  What 
> should I be checking?
>
> Cheers
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/4ba122fc-8070-422f-a783-2e9b1b816777n%40googlegroups.com.


Re: Configure extension on preconnect

2021-10-19 Thread Marcelo
Just to conclude the thread, the pull request was created and merged 
(thanks!), so after it is released, this is how you would apply the 
expiration timeout before *:preconnect* kicks in:

*DB = Sequel.connect(*
*  adapter: 'mysql2', host: '127.0.0.1', port: '49153', database: 'my_db',*
*  user: 'root', password: 'my_pass',*

*  ssl_mode: :disabled,*
  *preconnect_extensions: :connection_expiration,*
*  preconnect: true,*
*  before_preconnect: proc { |db| db.pool.connection_expiration_timeout 
= 100 }*
*)*

Note the new option, *:before_preconnect*, which accepts a proc that will 
be passed the DB instance, after *:preconnect_extensions* are loaded, but 
before the connections are made.

Best,
Marcelo
On Sunday, October 17, 2021 at 5:24:33 PM UTC+2 Jeremy Evans wrote:

> On Sun, Oct 17, 2021 at 4:49 AM Marcelo  wrote:
>
>> Hello all,
>>
>> I'm adding the :connection_expiration extension to a DB instance, but I 
>> need to change its default expiration time with, for example:
>> DB.pool.connection_expiration_timeout = 100
>>
>> However, this new expiration time is not applied to the initial 
>> connection, but rather the default value of 14400, and I'm having to resort 
>> to manually disconnecting and reconnecting:
>>
>> *DB = Sequel.connect(*
>> *  adapter: 'mysql2', host: '127.0.0.1', port: '49153', 
>> database: 'my_db',*
>> *  user: 'root', password: 'my_pass',*
>>
>> *  ssl_mode: :disabled,*
>> *  extensions: :connection_expiration,*
>>
>> *)*
>>
>> *def expiration_times(db)*
>> *  
>> db.pool.instance_variable_get(:@connection_expiration_timestamps).values.map{|v|
>>  
>> v[1]}.join(', ')*
>> *end*
>>
>> *DB.pool.connection_expiration_timeout = 100*
>> *print "Initial values: "*
>> *puts expiration_times(DB)*
>>
>> *DB.disconnect*
>> *print "After disconnect: "*
>> *puts expiration_times(DB)*
>>
>> *DB[:users].first*
>> *print "After reconnect: "*
>> *puts expiration_times(DB)*
>>
>> *# => Initial values: 14400.0*
>> *# => After disconnect: *
>> *# => After reconnect: 100.0*
>>
>> That's still passable, but if I additionally want to enable :preconnect, 
>> this doesn't work at all, since after DB.disconnect, I'd have to call the 
>> private method `preconnect` directly.
>>
>> *DB = Sequel.connect(*
>> *  adapter: 'mysql2', host: '127.0.0.1', port: '49153', 
>> database: 'my_db',*
>> *  user: 'root', password: 'my_pass',*
>>
>> *  ssl_mode: :disabled,*
>> *  preconnect_extensions: :connection_expiration,*
>> *  preconnect: true*
>> *)*
>>
>>
>> My specific use case is with the connection_expiration extension, but I'm 
>> interested if there's in general a way to configure the extension prior to 
>> a connection being established, and especially for preconnections.
>>
>
> It doesn't look like there is a way to do what you want currently.  We 
> would probably need to add an option like :before_preconnect to do so.  I'm 
> definitely open to that.  If you want to submit a pull request for it, 
> please do so.  Otherwise, I can probably take care of it before the next 
> release.
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/f6cf5dc9-a8b1-45f9-9bc8-d596e7dec576n%40googlegroups.com.


Configure extension on preconnect

2021-10-17 Thread Marcelo
Hello all,

I'm adding the :connection_expiration extension to a DB instance, but I 
need to change its default expiration time with, for example:
DB.pool.connection_expiration_timeout = 100

However, this new expiration time is not applied to the initial connection, 
but rather the default value of 14400, and I'm having to resort to manually 
disconnecting and reconnecting:

*DB = Sequel.connect(*
*  adapter: 'mysql2', host: '127.0.0.1', port: '49153', database: 'my_db',*
*  user: 'root', password: 'my_pass',*

*  ssl_mode: :disabled,*
*  extensions: :connection_expiration,*

*)*

*def expiration_times(db)*
*  
db.pool.instance_variable_get(:@connection_expiration_timestamps).values.map{|v|
 
v[1]}.join(', ')*
*end*

*DB.pool.connection_expiration_timeout = 100*
*print "Initial values: "*
*puts expiration_times(DB)*

*DB.disconnect*
*print "After disconnect: "*
*puts expiration_times(DB)*

*DB[:users].first*
*print "After reconnect: "*
*puts expiration_times(DB)*

*# => Initial values: 14400.0*
*# => After disconnect: *
*# => After reconnect: 100.0*

That's still passable, but if I additionally want to enable :preconnect, 
this doesn't work at all, since after DB.disconnect, I'd have to call the 
private method `preconnect` directly.

*DB = Sequel.connect(*
*  adapter: 'mysql2', host: '127.0.0.1', port: '49153', database: 'my_db',*
*  user: 'root', password: 'my_pass',*

*  ssl_mode: :disabled,*
*  preconnect_extensions: :connection_expiration,*
*  preconnect: true*
*)*


My specific use case is with the connection_expiration extension, but I'm 
interested if there's in general a way to configure the extension prior to 
a connection being established, and especially for preconnections.

Thanks in advance,

PS to Jeremy: The Polished Ruby book you wrote is fantastic. Congrats!


-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/daddf7c9-e3e4-4756-9d3f-555780b58c4fn%40googlegroups.com.


Re: odd number of arguments for Hash

2021-08-29 Thread Marcelo Pereira
If you need to, you can call super in your initialize, passing the
parameters the way Sequel models expect.

Em dom., 29 de ago. de 2021 às 05:24, bee...@gmail.com 
escreveu:

> Sorry...half way through a Saturday evening.  I will try with a single
> hash thrown, but how do I require such a structure, given I can't
> manipulate the initialize?
>
> On Saturday, August 28, 2021 at 11:22:12 PM UTC-4 bee...@gmail.com wrote:
>
>> OK, understood.  How do I insist on required parameters, named or
>> otherwise?
>>
>> On Saturday, August 28, 2021 at 9:41:51 PM UTC-4 Jeremy Evans wrote:
>>
>>> On Sat, Aug 28, 2021 at 2:26 PM bee...@gmail.com 
>>> wrote:
>>>
 Not sure why this is happening.  I'm getting  as an error.
 I've tried using both directive and non-directive for @id in the initialize
 method, with same error.  Seems the object is the same column profile.  Any
 clue why this is happening?


 DL.create_table? :mobibase do
   primary_key   :id

   DateTime  :cdate
   String:label, size: 200
   String:note, size: 350

   String:status, size: 20, default: 'new'
 end

 class Mobibase < Sequel::Model(DL[:mobibase])

   def initialize(cdate, label, note)
 # @id = nil
 @cdate = cdate
 @label = label
 @note = note
 @status = 'new'
 puts "A new instantiation"
   end

>>>
>>> Don't override #initialize for Sequel::Model subclasses.
>>> Sequel::Model#initialize is expected to be called with a single hash.  In
>>> general, you should not override methods that Sequel::Model defines to
>>> change the number or type of arguments.  This is true not just for
>>> Sequel::Model, but for pretty much all Ruby classes, unless you are
>>> subclassing from Object or BasicObject.
>>>
>>> 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 sequel-talk+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sequel-talk/ccc17503-769c-48b2-8f23-0239770a6c28n%40googlegroups.com
> 
> .
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CANtavQ4DbRLV%2BRtBNr_p%2Bn5mqJ%3DGKzCkJ2NOpXHUHrck1cN5cg%40mail.gmail.com.


Re: Pagination does not work on models

2021-08-23 Thread Marcelo Pereira
To make it work, you need to:
- Enable the extension prior to defining the FooModel class
- Run the paginate method on the model dataset with
FooModel.dataset.paginate(1,2)

I'm not sure if there's a reason why the model doesn't proxy the paginate
method to its underlying dataset.

Best

Em dom., 22 de ago. de 2021 às 17:47, Алексей Мацкевич <
aleksei_mackev...@outlook.com> escreveu:

> I use Sequel version 5.44
> ```ruby
> # foo_model.rb
> class FooModel < Sequel::Model
>   plugin :json_serializer
>   plugin :timestamps, update_on_create: true
>
>
> *end# console*> Sequel.extension :pagination
> [
> [0] :pagination
> ]
>
> > FooModel.paginate(1,2)
> NoMethodError: undefined method `paginate' for FooModel:Class
> ```
> I was struggling with this for hours studying the documentation but was
> not successful, what could I have done wrong?
>
> --
> 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 sequel-talk+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sequel-talk/bcb3ae77-8b20-4817-aa03-1162b6e6ed13n%40googlegroups.com
> 
> .
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CANtavQ5TV%3D0tSLFcB4WauS3ohg68qLKsKRZvu2Ct6tE_hYBH2A%40mail.gmail.com.


Re: Using pg_arrays operations in join conditions

2020-04-07 Thread Marcelo Pereira
Aha! Got it.

Thank you very much!

Best

On Tue, 7 Apr 2020, 21:17 Jeremy Evans,  wrote:

> On Tuesday, April 7, 2020 at 11:11:59 AM UTC-7, Marcelo wrote:
>>
>> Hello,
>>
>> I'm trying to produce this query with Sequel, that would then run on
>> PostgreSQL:
>> SELECT o.id, coalesce(cg.price, o.price) price
>> FROM offers o
>> LEFT JOIN country_groups cg ON cg.offer_id = o.id AND cg.country_codes @>
>> '{"US"}'
>>
>>
> You can pass a block to left_join:
>
> DB.from{offers.as(:o)}.
>   select{[o[:id], coalesce(cg[:price], o[:price]).as(:price)]}.
>   left_join(Sequel[:country_groups].as(:cg),
> :offer_id=>:id){Sequel[:cg][:country_codes].pg_array.contains(["US"])}
>
> SQL:
>
> SELECT "o"."id", coalesce("cg"."price", "o"."price") AS "price"
> FROM "offers" AS "o"
> LEFT JOIN "country_groups" AS "cg" ON (("cg"."offer_id" = "o"."id") AND
> ("cg"."country_codes" @> ARRAY['US']))
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sequel-talk/bf8f503c-d05b-4830-a8ce-b5d4f1ab87fa%40googlegroups.com
> <https://groups.google.com/d/msgid/sequel-talk/bf8f503c-d05b-4830-a8ce-b5d4f1ab87fa%40googlegroups.com?utm_medium=email_source=footer>
> .
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CANtavQ7dSv%2BMMpyS4wtOy1gfvQv5b%3DmrWV8UmKcgAQQ%2BVRK0gA%40mail.gmail.com.


Using pg_arrays operations in join conditions

2020-04-07 Thread Marcelo
Hello,

I'm trying to produce this query with Sequel, that would then run on 
PostgreSQL:
SELECT o.id, coalesce(cg.price, o.price) price
FROM offers o
LEFT JOIN country_groups cg ON cg.offer_id = o.id AND cg.country_codes @> 
'{"US"}'

I understand I can use an array of conditions, as follows:
db[:offers].left_join(:country_groups, [[:offer_id, :id], [??]] )

But then I don't know how to express the second condition, which normally 
would be like this in a filter context:
Sequel.pg_array_op(:country_codes).contains(Sequel.pg_array(['US'], :varchar
))


Alternatively, I've tried using a subquery:
filtered_countries = db[:country_groups].where(Sequel.pg_array_op(:
country_codes).contains(Sequel.pg_array(['US'], :varchar)))
db[:offers].left_join(filtered_countries, offer_id: :id).select { coalesce(
country_groups[:price], offers[:price]) }

The issue then is that the country_groups referenced inside coalesce is not 
part of the FROM clause, so I get:
PG::UndefinedTable: ERROR:  missing FROM-clause entry for table 
"country_groups"


Tried using the filtered dataset inside coalesce
db[:offers].left_join(filtered_countries, offer_id: :id).select { coalesce(
filtered_countries[:price], offers[:price]) }

and from the error I get, this seems just totally wrong:
Sequel::DatabaseError: PG::DatatypeMismatch: ERROR:  argument of AND must 
be type boolean, not type numeric
LINE 1: ... (("country_codes" @> ARRAY['DE']::varchar[]) AND "price") LIM...

How do I achieve any of this without resorting to raw queries?

Thanks in advance for any help or pointers,

Best
Marcelo

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/2b594f9f-0086-491e-b2cf-e9ae2f071910%40googlegroups.com.


Re: Should Sequel continue shipping tests in the gem?

2020-01-19 Thread Marcelo Pereira
I would guess most of the time users are not running Sequel tests and, if 
one wants to run them, it's simple enough to clone the repository - so I 
would say not shipping the tests sounds like a good idea. No strong 
opinions on this one, though - having the tests in the gem is, of course, 
not a big issue, and I will keep using Sequel either way.

On Saturday, January 18, 2020 at 8:27:50 PM UTC+1, Jeremy Evans wrote:
>
> Sequel has always shipped tests in the gem, so that users and packagers 
> can test it without cloning the repository, but it does make the gem 
> significantly larger.  I'm open to feedback on whether Sequel should 
> continue to do this, so if you feel strongly one way or the other, please 
> respond.
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/0a08bedf-4570-47d2-8ea5-323df84aeddc%40googlegroups.com.


Mysql2::Error: Duplicate entry for '%s' for key '%s' is not handled by UniqueConstraintViolation

2018-11-13 Thread Marcelo Pereira
Hello,

I have this error being raised:

```
Mysql2::Error: Duplicate entry '...' for key 
'index_users_on_userid_and_application_id': INSERT INTO `users` (`userid`, 
`application_id`, `created_at`, `updated_at`) VALUES (...)
```

and it doesn't get rescued with `rescue Sequel::UniqueConstraintViolation`. 
I've checked the list of error codes on MySQL documentation and in this 
case the code is 1586. Looking at 
_lib/sequel/adapters/utils/mysql_mysql2.rb_, on the 
`database_specific_error_class` method, I see that error code 1062 is 
mapped as unique constraint violation, but 1586 is not.

Is this on purpose? Should the method read `when 1062, 1586; 
UniqueConstraintViolation` instead?

Please let me know if I can go ahead and open a pull request with this 
change.

Best,
Marcelo

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: FrozenError when using pg_enum

2018-04-25 Thread Marcelo Pereira
Yes, you are right - this was a fairly obvious oversight on my part. Thanks 
a lot for the input.

Best

On Tuesday, April 24, 2018 at 10:38:33 PM UTC+2, Jeremy Evans wrote:
>
> On Tuesday, April 24, 2018 at 1:21:52 PM UTC-7, Marcelo Pereira wrote:
>>
>> Hello,
>>
>> I'm running Sequel 5.6.0 and Ruby 2.5.1 and, when I try to create a 
>> PostgreSQL enum, using the pg_enum extension, I get the following error.
>>
>> FrozenError: can't modify frozen 
>> #<Class:#>
>>   
>> /usr/local/bundle/gems/sequel-5.6.0/lib/sequel/extensions/pg_enum.rb:112:in 
>> `parse_enum_labels'
>>
>> Here is the minimum code that generates this error:
>>
>> Sequel.migration do
>>   change do
>> create_enum(:transaction_status, %w[complete pending 
>> pending_publisher])
>>   end
>> end
>>
>>
>> I'm still trying to figure out what is going on and will post more 
>> information if I find something.
>>
>
> You are running on a frozen Database object, and create_enum modifies the 
> object, so you get the FrozenError. A simple workaround with the current 
> code would be to just not run migrations on a frozen Database.
>
> In terms of fixing the underlying issue, we'd have to not freeze 
> @enum_labels, and instead use a mutex to access it.  That should be fine as 
> it is only used when parsing the schema.  I'll make that change before the 
> next release.
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


FrozenError when using pg_enum

2018-04-24 Thread Marcelo Pereira
Hello,

I'm running Sequel 5.6.0 and Ruby 2.5.1 and, when I try to create a 
PostgreSQL enum, using the pg_enum extension, I get the following error.

FrozenError: can't modify frozen 
#<Class:#>
  
/usr/local/bundle/gems/sequel-5.6.0/lib/sequel/extensions/pg_enum.rb:112:in 
`parse_enum_labels'

Here is the minimum code that generates this error:

Sequel.migration do
  change do
create_enum(:transaction_status, %w[complete pending pending_publisher])
  end
end


I'm still trying to figure out what is going on and will post more 
information if I find something.

Best,
Marcelo

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Equivalent to :methods option from ActiveModel::Serializers::JSON

2018-04-18 Thread Marcelo Pereira
My bad, :include does indeed work as well.

On Tuesday, April 17, 2018 at 10:06:18 PM UTC+2, Marcelo Pereira wrote:
>
> I checked the code, and saw that :include doesn't work, but :only does. 
> Thanks a lot for answering this quickly.
>
> On Tuesday, April 17, 2018 at 7:26:40 PM UTC+2, Jeremy Evans wrote:
>>
>> I think you can use the :include option for this, though I'm not sure 
>> what the :methods option does in AMS.
>
>

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Equivalent to :methods option from ActiveModel::Serializers::JSON

2018-04-17 Thread Marcelo Pereira
I checked the code, and saw that :include doesn't work, but :only does. 
Thanks a lot for answering this quickly.

On Tuesday, April 17, 2018 at 7:26:40 PM UTC+2, Jeremy Evans wrote:
>
> I think you can use the :include option for this, though I'm not sure what 
> the :methods option does in AMS.

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Equivalent to :methods option from ActiveModel::Serializers::JSON

2018-04-17 Thread Marcelo Pereira
Hello,

I'm using the JsonSerializer Plugin in Sequel, but I need to add, to the 
serialized model, a property that comes from a method call (as opposed to 
from a database column).

Is there a way to do this using this plugin, or should I try to create my 
own plugin for this?

Best

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.