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

Changeset 7718

Show
Ignore:
Timestamp:
10/02/07 03:20:52 (2 years ago)
Author:
bitsweat
Message:

db:create works with remote databases whereas db:create:all only createsdatabases on localhost. Closes #9753.

Files:

Legend:

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

    r7708 r7718  
    11*SVN* 
     2 
     3* db:create works with remote databases whereas db:create:all only creates 
     4databases on localhost.  #9753 [Trevor Wennblom] 
    25 
    36* Removed calls to fixtures in generated tests as fixtures :all is now present by default in test_helper.rb [DHH] 
  • trunk/railties/lib/tasks/databases.rake

    r7603 r7718  
    44    task :all => :environment do 
    55      ActiveRecord::Base.configurations.each_value do |config| 
    6         create_local_database(config) 
    7       end 
    8     end 
    9   end 
    10  
    11   desc 'Create the local database defined in config/database.yml for the current RAILS_ENV' 
     6        # Skip entries that don't have a database key, such as the first entry here: 
     7        # 
     8        #  defaults: &defaults  
     9        #    adapter: mysql  
     10        #    username: root 
     11        #    password:  
     12        #    host: localhost 
     13        #   
     14        #  development:  
     15        #    database: blog_development 
     16        #    <<: *defaults 
     17        next unless config['database'] 
     18        # Only connect to local databases 
     19        if config['host'] == 'localhost' || config['host'].blank? 
     20          create_database(config) 
     21        else 
     22          p "This task only creates local databases. #{config['database']} is on a remote host." 
     23        end 
     24      end 
     25    end 
     26  end 
     27 
     28  desc 'Create the database defined in config/database.yml for the current RAILS_ENV' 
    1229  task :create => :environment do 
    13     create_local_database(ActiveRecord::Base.configurations[RAILS_ENV]) 
    14   end 
    15  
    16   def create_local_database(config) 
    17     # Only connect to local databases 
    18     if config['host'] == 'localhost' || config['host'].blank? 
    19       begin 
    20         ActiveRecord::Base.establish_connection(config) 
    21         ActiveRecord::Base.connection 
    22       rescue 
    23         case config['adapter'] 
    24         when 'mysql' 
    25           @charset   = ENV['CHARSET']   || 'utf8' 
    26           @collation = ENV['COLLATION'] || 'utf8_general_ci' 
    27           begin 
    28             ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) 
    29             ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation}) 
    30             ActiveRecord::Base.establish_connection(config) 
    31           rescue 
    32             $stderr.puts "Couldn't create database for #{config.inspect}" 
    33           end 
    34         when 'postgresql' 
    35           `createdb "#{config['database']}" -E utf8` 
    36         when 'sqlite' 
    37           `sqlite "#{config['database']}"` 
    38         when 'sqlite3' 
    39           `sqlite3 "#{config['database']}"` 
    40         end 
    41       else 
    42         p "#{config['database']} already exists" 
     30    create_database(ActiveRecord::Base.configurations[RAILS_ENV]) 
     31  end 
     32 
     33  def create_database(config) 
     34    begin 
     35      ActiveRecord::Base.establish_connection(config) 
     36      ActiveRecord::Base.connection 
     37    rescue 
     38      case config['adapter'] 
     39      when 'mysql' 
     40        @charset   = ENV['CHARSET']   || 'utf8' 
     41        @collation = ENV['COLLATION'] || 'utf8_general_ci' 
     42        begin 
     43          ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) 
     44          ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation}) 
     45          ActiveRecord::Base.establish_connection(config) 
     46        rescue 
     47          $stderr.puts "Couldn't create database for #{config.inspect}" 
     48        end 
     49      when 'postgresql' 
     50        `createdb "#{config['database']}" -E utf8` 
     51      when 'sqlite' 
     52        `sqlite "#{config['database']}"` 
     53      when 'sqlite3' 
     54        `sqlite3 "#{config['database']}"` 
    4355      end 
    4456    else 
    45       p "This task only creates local databases. #{config['database']} is on a remote host.
     57      p "#{config['database']} already exists
    4658    end 
    4759  end 
     
    5163    task :all => :environment do 
    5264      ActiveRecord::Base.configurations.each_value do |config| 
    53         drop_database(config) 
     65        # Skip entries that don't have a database key 
     66        next unless config['database'] 
     67        # Only connect to local databases 
     68        if config['host'] == 'localhost' || config['host'].blank? 
     69          drop_database(config) 
     70        else 
     71          p "This task only drops local databases. #{config['database']} is on a remote host." 
     72        end 
    5473      end 
    5574    end