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

Ticket #9045: rails_create_drop_db_postgres_9180.patch

File rails_create_drop_db_postgres_9180.patch, 1.7 kB (added by nicksieger, 9 months ago)
  • a/railties/lib/tasks/databases.rake

    old new  
    4343          $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation} (if you set the charset manually, make sure you have a matching collation)" 
    4444        end 
    4545      when 'postgresql' 
    46         `createdb "#{config['database']}" -E utf8` 
     46        @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8' 
     47        begin 
     48          ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) 
     49          ActiveRecord::Base.connection.create_database(config['database'], {:encoding => @encoding}) 
     50          ActiveRecord::Base.establish_connection(config) 
     51        rescue 
     52          $stderr.puts $!, *($!.backtrace) 
     53          $stderr.puts "Couldn't create database for #{config.inspect}" 
     54        end 
    4755      when 'sqlite' 
    4856        `sqlite "#{config['database']}"` 
    4957      when 'sqlite3' 
    5058        `sqlite3 "#{config['database']}"` 
    5159      end 
    5260    else 
    53       p "#{config['database']} already exists" 
     61      $stderr.puts "#{config['database']} already exists" 
    5462    end 
    5563  end 
    5664 
     
    365373  when /^sqlite/ 
    366374    FileUtils.rm(File.join(RAILS_ROOT, config['database'])) 
    367375  when 'postgresql' 
    368     ActiveRecord::Base.clear_active_connections!     
    369     `dropdb "#{config['database']}"` 
     376    ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) 
     377    ActiveRecord::Base.connection.drop_database config['database'] 
    370378  end 
    371379end 
    372380