namespace :db do namespace :create do desc 'Create all the local databases defined in config/database.yml' task :all => :environment do ActiveRecord::Base.configurations.each_value do |config| create_local_database(config) end end end desc 'Create the local database defined in config/database.yml for the current RAILS_ENV' task :create => :environment do create_local_database(ActiveRecord::Base.configurations[RAILS_ENV]) end def create_local_database(config) # Only connect to local databases if config['host'] == 'localhost' || config['host'].blank? begin ActiveRecord::Base.establish_connection(config) ActiveRecord::Base.connection rescue case config['adapter'] when 'mysql' @charset = ENV['CHARSET'] || 'utf8' @collation = ENV['COLLATION'] || 'utf8_general_ci' begin ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation}) ActiveRecord::Base.establish_connection(config) p "MySQL #{config['database']} database succesfully created" rescue $stderr.puts "Couldn't create database for #{config.inspect}" end when 'postgresql' `createdb "#{config['database']}" -E utf8` when 'sqlite' `sqlite "#{config['database']}"` when 'sqlite3' `sqlite3 "#{config['database']}"` end else p "#{config['database']} already exists" end else p "This task only creates local databases. #{config['database']} is on a remote host." end end desc 'Drops the database for the current environment' task :drop => :environment do config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] case config['adapter'] when 'mysql' ActiveRecord::Base.connection.drop_database config['database'] when /^sqlite/ FileUtils.rm_f File.join(RAILS_ROOT, config['database']) when 'postgresql' `dropdb "#{config['database']}"` end end desc 'Drops, creates and then migrates the database for your current RAILS_ENV. Target specific version with VERSION=x' task :reset => ['db:drop', 'db:create', 'db:migrate'] desc "Retrieves the charset for the current environment's database" task :charset => :environment do config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] case config['adapter'] when 'mysql' ActiveRecord::Base.establish_connection(config) puts ActiveRecord::Base.connection.charset else puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' end end desc "Retrieves the collation for the current environment's database" task :collation => :environment do config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] case config['adapter'] when 'mysql' ActiveRecord::Base.establish_connection(config) puts ActiveRecord::Base.connection.collation else puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' end end desc "Retrieves the current schema version number" task :version => :environment do puts "Current version: #{ActiveRecord::Migrator.current_version}" end desc "add the encoding format to each environment in the database.yml file" task :update_connection_settings => :environment do config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] case config['adapter'] when 'mysql' ActiveRecord::Base.establish_connection(config) charset = ActiveRecord::Base.connection.character_set_name File.open(RAILS_ROOT + '/config/database.yml', "r") do |file| @config = YAML.load(file) File.copy(RAILS_ROOT + "/config/database.yml", RAILS_ROOT + "/config/database.yml."+Time.now.strftime("of_%Y_%m_%d_%H_%M")) end @new_config = {} @config.each do |env| env[1] = env[1].merge('encoding' => charset.to_s) @new_config = @new_config.merge(env[0].to_s => env[1]) end # Write the new database.yml file with encoding added to each environment File.open(RAILS_ROOT + "/config/database.yml", "w") {|f| YAML.dump(@new_config, f)} puts %Q(a new database.yml file was created, your previous file was backed up as /config/database.yml.#{Time.now.strftime('of_%Y_%m_%d_%H%M')}) else puts 'sorry, your database adapter is not supported yet, feel free to email us your patch' end end desc 'Launches the database shell using the values defined in config/database.yml' task :shell => :environment do config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] command = "" case config['adapter'] when 'mysql' command << "mysql " command << "--host=#{config['host'] || 'localhost'} " command << "--port=#{config['port'] || 3306} " command << "--user=#{config['username'] || 'root'} " command << "--password=#{config['password'] || ''} " command << config['database'] when 'postgresql' puts 'You should consider switching to MySQL or get off your butt and submit a patch' else command << "echo Unsupported database adapter: #{config['adapter']}" end system command end end