SciRuby / daru

Data Analysis in RUby
BSD 2-Clause "Simplified" License
1.04k stars 139 forks source link

Connect method for ActiveRecord Database Connection #316

Open ananyo2012 opened 7 years ago

ananyo2012 commented 7 years ago

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'
)
v0dro commented 7 years ago

@mrkn WDYT?

mrkn commented 7 years ago

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

zverok commented 7 years ago

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.

ananyo2012 commented 7 years ago

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.

v0dro commented 7 years ago

@mrkn ping!

mrkn commented 7 years ago

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

ananyo2012 commented 7 years ago

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