Skip to content

Instantly share code, notes, and snippets.

@vitaliel
Created December 2, 2012 08:51
Show Gist options
  • Save vitaliel/4187770 to your computer and use it in GitHub Desktop.
Save vitaliel/4187770 to your computer and use it in GitHub Desktop.
Read database.yml file and generate sql to create databases
#!/usr/bin/env ruby
# Read database.yml file and generate sql to create databases
# ./rails_db_create.rb | mysql
require 'yaml'
config = YAML.load_file 'database.yml'
dbs = {}
config.each do |env, opts|
dbs[opts['database']] = opts
end
dbs.each do |db, opts|
puts "CREATE DATABASE `#{db}` DEFAULT CHARACTER SET #{opts['encoding']};"
puts "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX
ON #{db}.*
TO #{opts['username']}@localhost IDENTIFIED BY '#{opts['password']}';
flush privileges;"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment