Skip to content

Instantly share code, notes, and snippets.

@vimtaku
Created December 5, 2015 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vimtaku/80a4760f126331af3e52 to your computer and use it in GitHub Desktop.
Save vimtaku/80a4760f126331af3e52 to your computer and use it in GitHub Desktop.
rails console で m select * from users; とかできるようにするスニペット
class Pry
class Command::MySQLQueryCommand < Pry::ClassCommand
match(/m\s+(.*)/)
description "execute mysql command line"
banner <<-'BANNER'
Usage: m SQLCOMMAND
ex) m select * from users;
BANNER
@@config = YAML.load_file("config/database.yml")
def process(cmd)
env_config = @@config[ Rails.env ]
config_options = {
"username" => "-u",
"database" => "--database",
"password" => "-P",
"port" => "--port",
"host" => "-h",
}
option_string = ""
config_options.each do |k, v|
val = env_config[ k ]
next if val.nil?
option_string += " #{v} #{val}"
end
system("mysql #{option_string} -e '#{cmd}'")
end
end
Pry::Commands.add_command(Pry::Command::MySQLQueryCommand)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment