hashview / hashview-old

A web front-end for password cracking and analytics

Home Page:http://www.hashview.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error while updating to v0.7.4-beta (PR Submitted)

Script-Nomad opened this issue · comments

I received the following stacktrace when attempting to upgrade an instance from v0.6.0

/opt/hashview$ RACK_ENV=production rake db:upgrade
[*] Connecting to DB
rake aborted!
NoMethodError: undefined method `new' for Mysql2:Module
/opt/hashview/Rakefile:356:in `block (2 levels) in <top (required)>'
/usr/share/rvm/gems/ruby-2.4.4/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/usr/share/rvm/gems/ruby-2.4.4/bin/ruby_executable_hooks:15:in `eval'
/usr/share/rvm/gems/ruby-2.4.4/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:upgrade
(See full trace by running task with --trace)

The offending line in the Rakefile is

conn = Mysql2.new(:host => host, :username => user, :password => password, :database => database)

A brief read through the documentation showed me that this is not proper syntax for Mysql2 v0.5.2 module, which should be using Mysql2::Client.new but doing so caused several new errors that horrendously broke the application further because of subsequent (erroneous) calls to the same database connection function.

I was able to fix the issue by replacing every instance of Mysql2.new() and for some reason Mysql::new() which does not exist at all with the following line...

conn = Mysql2::Client.new(:host => host, :username => user, :password => password, :database => database)

My question is this... why are there numerous calls to set up a new connection to the database? Why should there be multiple authentications? A far better solution would have been to create a function to return a database connection object and pass it around to the various upgrade functions rather than authenticating for each one. Since authentication to the database should happen regardless, this is far more syntactically clean and less error prone than making half a dozen Mysql2::Client.new() calls in various functions throughout the code.

Forgive my ignorance if this isn't convention or somehow illegal within Ruby, as I am not a ruby developer, but using a single function to return a database handler object would appear far more clean and reduce the chance for syntax errors when setting up the database connection. If you need to have a different db connection depending on the version of your Mysql2 gemfile, so be it, but at least abstract it away for the sake of simplicity. That's what objects were made for. 👍

Submitted in PR#434

Closing since PR#435 has been merged