Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6849

Show
Ignore:
Timestamp:
05/26/07 00:24:58 (3 years ago)
Author:
bitsweat
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r6840 r6849  
    11*SVN* 
     2 
     3* Add db:create, drop, reset, charset, and collation tasks.  #8448 [matt] 
    24 
    35* Scaffold generator depends on model generator instead of duplicating it.  #7222 [bscofield] 
  • trunk/railties/lib/tasks/databases.rake

    r5269 r6849  
    11namespace :db do 
     2   
     3  desc 'Creates the databases defined in your config/database.yml (unless they already exist)' 
     4  task :create => :environment do  
     5    ActiveRecord::Base.configurations.each_value do |config| 
     6      begin 
     7        ActiveRecord::Base.establish_connection(config) 
     8        ActiveRecord::Base.connection 
     9      rescue 
     10        case config['adapter'] 
     11        when 'mysql' 
     12          @charset   = ENV['CHARSET']   || 'utf8' 
     13          @collation = ENV['COLLATION'] || 'utf8_general_ci' 
     14 
     15          ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) 
     16          ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation}) 
     17          ActiveRecord::Base.establish_connection(config) 
     18        when 'postgresql' 
     19          `createdb "#{config['database']}" -E utf8`   
     20        end 
     21      end 
     22    end 
     23    ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[RAILS_ENV || 'development']) 
     24  end 
     25   
     26  desc 'Drops the database for your currenet RAILS_ENV as defined in config/database.yml' 
     27  task :drop => :environment do 
     28    config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] 
     29    case config['adapter'] 
     30    when 'mysql' 
     31      ActiveRecord::Base.connection.drop_database config['database'] 
     32    when 'sqlite3' 
     33      FileUtils.rm_f File.join(RAILS_ROOT, config['database']) 
     34    when 'postgresql' 
     35      `dropdb "#{config['database']}"`    
     36    end 
     37  end 
     38   
    239  desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x" 
    340  task :migrate => :environment do 
     
    643  end 
    744 
     45  desc 'Drops, creates and then migrates the database for your current RAILS_ENV. Target specific version with VERSION=x' 
     46  task :reset => ['db:drop', 'db:create', 'db:migrate'] 
     47   
     48  desc "retrieve the charset for your database defined in your current RAILS_ENV" 
     49  task :charset => :environment do 
     50    config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] 
     51    case config['adapter'] 
     52    when 'mysql' 
     53      ActiveRecord::Base.establish_connection(config) 
     54      puts ActiveRecord::Base.connection.charset 
     55    else 
     56      puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' 
     57    end 
     58  end 
     59 
     60  desc "retrieve the collation for your database" 
     61  task :collation => :environment do 
     62    config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] 
     63    case config['adapter'] 
     64    when 'mysql' 
     65      ActiveRecord::Base.establish_connection(config) 
     66      puts ActiveRecord::Base.connection.collation 
     67    else 
     68      puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' 
     69    end 
     70  end 
     71   
    872  namespace :fixtures do 
    973    desc "Load fixtures into the current environment's database.  Load specific fixtures using FIXTURES=x,y"