Created
December 5, 2015 07:27
-
-
Save vimtaku/80a4760f126331af3e52 to your computer and use it in GitHub Desktop.
rails console で m select * from users; とかできるようにするスニペット
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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