Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add db:create, drop, reset, charset, and collation tasks. Closes #8448.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6849 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 1f03c5111d167ed735ee00676f317ba03e9ef295 1 parent ed2a84f
@jeremy jeremy authored
Showing with 66 additions and 0 deletions.
  1. +2 −0  railties/CHANGELOG
  2. +64 −0 railties/lib/tasks/databases.rake
View
2  railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add db:create, drop, reset, charset, and collation tasks. #8448 [matt]
+
* Scaffold generator depends on model generator instead of duplicating it. #7222 [bscofield]
* Scaffold generator tests. #8443 [pelle]
View
64 railties/lib/tasks/databases.rake
@@ -1,10 +1,74 @@
namespace :db do
+
+ desc 'Creates the databases defined in your config/database.yml (unless they already exist)'
+ task :create => :environment do
+ ActiveRecord::Base.configurations.each_value do |config|
+ 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'
+
+ ActiveRecord::Base.establish_connection(config.merge({'database' => nil}))
+ ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation})
+ ActiveRecord::Base.establish_connection(config)
+ when 'postgresql'
+ `createdb "#{config['database']}" -E utf8`
+ end
+ end
+ end
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[RAILS_ENV || 'development'])
+ end
+
+ desc 'Drops the database for your currenet RAILS_ENV as defined in config/database.yml'
+ task :drop => :environment do
+ config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
+ case config['adapter']
+ when 'mysql'
+ ActiveRecord::Base.connection.drop_database config['database']
+ when 'sqlite3'
+ FileUtils.rm_f File.join(RAILS_ROOT, config['database'])
+ when 'postgresql'
+ `dropdb "#{config['database']}"`
+ end
+ end
+
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
task :migrate => :environment do
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
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 "retrieve the charset for your database defined in your current RAILS_ENV"
+ 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 "retrieve the collation for your 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
+
namespace :fixtures do
desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y"
task :load => :environment do
Please sign in to comment.
Something went wrong with that request. Please try again.