diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-26 00:24:58 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-26 00:24:58 +0000 |
commit | 1f03c5111d167ed735ee00676f317ba03e9ef295 (patch) | |
tree | 88f2f69396f1508be8f000d4bc4e7017d3cab350 /railties | |
parent | ed2a84f99b75ed0c2dec4a0145720ee84ab9e114 (diff) | |
download | rails-1f03c5111d167ed735ee00676f317ba03e9ef295.tar.gz rails-1f03c5111d167ed735ee00676f317ba03e9ef295.tar.bz2 rails-1f03c5111d167ed735ee00676f317ba03e9ef295.zip |
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
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG | 2 | ||||
-rw-r--r-- | railties/lib/tasks/databases.rake | 64 |
2 files changed, 66 insertions, 0 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 3c17b88169..052eccba79 100644 --- a/railties/CHANGELOG +++ b/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] diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index 7bc9aa9473..a265d3ef32 100644 --- a/railties/lib/tasks/databases.rake +++ b/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 |