sufleR / sql_query

Ruby gem to load SQL queries from `.sql.erb` templates using ERB.
MIT License
158 stars 15 forks source link

About the db_connection option #9

Open fchampreux opened 3 years ago

fchampreux commented 3 years ago

Not being higly familiar with connections handling, I am wandering how to specify this option when:

  1. using a connection declared in database.yml config file
  2. using a custom connection to a foreign database

I'll be glad to add examples to the documentation when I solve this point :-)

Thanks a lot!

sufleR commented 3 years ago

@fchampreux I don't know how do you use this gem. Is it ruby on rails application? What rails version?

Rails guides mention about connected_to https://guides.rubyonrails.org/active_record_multiple_databases.html#using-manual-connection-switching Inside connected_to block, you should be able to

 SqlQuery.new(:get_player_by_email, email: 'e@mail.dev', db_connection: ActiveRecord::Base.connection)

and I think it should work with the connection selected in connected_to. I'm sorry but I didn't test it.

With a custom connection, you should be able to call establish_connection. Docs: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html#method-i-establish_connection

for 1:

 SqlQuery.new(:get_player_by_email, email: 'e@mail.dev', db_connection: ActiveRecord::Base.establish_connection(:development).connection)

for 2:

 SqlQuery.new(:get_player_by_email, email: 'e@mail.dev', db_connection: ActiveRecord::Base.establish_connection({database: '', host: , adapter: .....}).connection)
fchampreux commented 3 years ago

@sufleR I'll search in this direction and document it when done. Thanks !

Le dim. 7 mars 2021 à 00:44, sufleR @.***> a écrit :

@fchampreux https://github.com/fchampreux I don't know how do you use this gem. Is it ruby on rails application? What rails version?

Rails guides mention about connected_to https://guides.rubyonrails.org/active_record_multiple_databases.html#using-manual-connection-switching Inside connected_to block, you should be able to

SqlQuery.new(:get_player_by_email, email: @.***', db_connection: ActiveRecord::Base.connection)

and I think it should work with the connection selected in connected_to. I'm sorry but I didn't test it.

With a custom connection, you should be able to call establish_connection. Docs: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html#method-i-establish_connection

for 1:

SqlQuery.new(:get_player_by_email, email: @.***', db_connection: ActiveRecord::Base.establish_connection(:development).connection)

for 2:

SqlQuery.new(:get_player_by_email, email: @.***', db_connection: ActiveRecord::Base.establish_connection({database: '', host: , adapter: .....}).connection)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sufleR/sql_query/issues/9#issuecomment-792120422, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIPJLPFLQEMEFKTPA64HITTCK47HANCNFSM4YSUK7XQ .

--

Frédéric CHAMPREUX+41 78 87 110 55