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

Changeset 9183

Show
Ignore:
Timestamp:
04/01/08 05:01:33 (6 months ago)
Author:
bitsweat
Message:

PostgreSQL: use create_ and drop_database for rake tasks. Closes #9045 [ez, nicksieger]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/lib/tasks/databases.rake

    r9170 r9183  
    66        # Skip entries that don't have a database key, such as the first entry here: 
    77        # 
    8         #  defaults: &defaults  
    9         #    adapter: mysql  
     8        #  defaults: &defaults 
     9        #    adapter: mysql 
    1010        #    username: root 
    11         #    password:  
     11        #    password: 
    1212        #    host: localhost 
    13         #   
    14         #  development:  
     13        # 
     14        #  development: 
    1515        #    database: blog_development 
    1616        #    <<: *defaults 
     
    3737        @collation = ENV['COLLATION'] || 'utf8_general_ci' 
    3838        begin 
    39           ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) 
    40           ActiveRecord::Base.connection.create_database(config['database'], {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
     39          ActiveRecord::Base.establish_connection(config.merge('database' => nil)) 
     40          ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)
    4141          ActiveRecord::Base.establish_connection(config) 
    4242        rescue 
     
    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']}"` 
     
    5159      end 
    5260    else 
    53       p "#{config['database']} already exists" 
     61      $stderr.puts "#{config['database']} already exists" 
    5462    end 
    5563  end 
     
    99107    desc 'Resets your database using your migrations for the current environment' 
    100108    task :reset => ["db:drop", "db:create", "db:migrate"] 
    101      
     109 
    102110    desc 'Runs the "up" for a given migration VERSION.' 
    103111    task :up => :environment do 
     
    114122      ActiveRecord::Migrator.run(:down, "db/migrate/", version) 
    115123      Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby 
    116     end     
     124    end 
    117125  end 
    118126 
     
    180188      end 
    181189    end 
    182      
     190 
    183191    desc "Search for a fixture given a LABEL or ID." 
    184192    task :identify => :environment do 
     
    187195      label, id = ENV["LABEL"], ENV["ID"] 
    188196      raise "LABEL or ID required" if label.blank? && id.blank? 
    189        
     197 
    190198      puts %Q(The fixture ID for "#{label}" is #{Fixtures.identify(label)}.) if label 
    191        
     199 
    192200      Dir["#{RAILS_ROOT}/test/fixtures/**/*.yml"].each do |file| 
    193201        if data = YAML::load(ERB.new(IO.read(file)).result) 
    194202          data.keys.each do |key| 
    195203            key_id = Fixtures.identify(key) 
    196              
     204 
    197205            if key == label || key_id == id.to_i 
    198206              puts "#{file}: #{key} (#{key_id})" 
     
    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