开发者

drop and re-create MySQL database using a ruby script

is there a way to drop and re-create a database from within a ruby script? Which gem (dbi, mysql2) should I use?

With DBI I have the problem that I cannot connect to the MySQL server 开发者_Go百科when the database has not yet been created.

Thank you


Prerequisite: make sure that mysql2 is correctly installed on the system.

require 'mysql2'

@db_host = "localhost"
@db_user = "someuser"
@db_pass = "somepass"
@db_name = "test_db"

client = Mysql2::Client.new(:host => @db_host, :username => @db_user, :password => @db_pass)
client.query("DROP DATABASE IF EXISTS #{@db_name}")
client.query("CREATE DATABASE #{@db_name}")
client.close


mysql2 will work, have a look at the rake tasks that come with rails db:create etc

EDIT :

Rails rake tasks to create / drop DBs

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜