daru icon indicating copy to clipboard operation
daru copied to clipboard

Connect method for ActiveRecord Database Connection

Open ananyo2012 opened this issue 8 years ago • 7 comments

We can have a connect method for establishing ActiveRecord connections for use in from_activerecord method by simply passing the adapter, db name, username and password as arguments. As of now we have to establish database connection manually.

ActiveRecord::Base.establish_connection(
  adapter:  'mysql2',
  host:     'localhost',
  username: 'me',
  password: 'secret',
  database: 'activerecord'
)

ananyo2012 avatar Mar 05 '17 10:03 ananyo2012

@mrkn WDYT?

v0dro avatar Mar 05 '17 12:03 v0dro

@ananyo2012 I want to see a code example of your suggestion.

mrkn avatar Mar 05 '17 12:03 mrkn

I believe the main use case for from_activerecord is "I already have a connection" (from inside Rails app, or something). But I'm open to use cases/code examples for this feature.

zverok avatar Mar 05 '17 12:03 zverok

In lib/daru/io/io.rb we can have

module IO
  class << self
    def activerecord_conn(adapter, user, password, db_name)
      ActiveRecord::Base.establish_connection(
        adapter:  adapter,
        host:     'localhost',
        username: user,
        password: password,
        database: db_name
      )
    end
  end
end

This can be called before calling the Daru::DataFrame.from_activerecord method to establish connection.

ananyo2012 avatar Mar 09 '17 13:03 ananyo2012

@mrkn ping!

v0dro avatar Mar 13 '17 06:03 v0dro

I don't think your activerecord_conn method is useful for AR users.

The parameters of AR::Base.establish_connection is not only the combination of adapter, user, password, and db_name, but it also accepts other optional parameters such as DBMS-specific connection parameters. And it allows us to omit user and password if they are unnecessary.

Also this method can be called with one url parameter, the example value is: mysql2://db_user:db_pass@localhost:3306/db_name.

You can check the specification of the establish_connection mehod here: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html#method-i-establish_connection

mrkn avatar Mar 17 '17 14:03 mrkn

@mrkn Yes I guess the different formats in the AR connect method gives it enough flexibility as per the database and environments. But for a first timer approaching the codebase it is pretty hard to know how it works around. It would be great if we can simplify things for them. Can we improve the documentation for this a bit or atleast giving the link you have pointed out in the docs?

ananyo2012 avatar Mar 23 '17 05:03 ananyo2012