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

Changeset 9182

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

PostgreSQL: create_ and drop_database support. Closes #9042.

Files:

Legend:

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

    r9168 r9182  
    11*SVN* 
     2 
     3* PostgreSQL: create_ and drop_database support.  #9042 [ez, pedz, nicksieger] 
    24 
    35* Ensure that validates_uniqueness_of works with with_scope. Closes #9235. [nik.wakelin, cavalle] 
  • trunk/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

    r8979 r9182  
    475475      # SCHEMA STATEMENTS ======================================== 
    476476 
     477      def recreate_database(name) #:nodoc: 
     478        drop_database(name) 
     479        create_database(name) 
     480      end 
     481 
     482      # Create a new PostgreSQL database.  Options include :owner, :template, 
     483      # :encoding, :tablespace, and :connection_limit (note that MySQL uses 
     484      # :charset while PostgreSQL uses :encoding). 
     485      # 
     486      # Example: 
     487      #   create_database config[:database], config 
     488      #   create_database 'foo_development', :encoding => 'unicode' 
     489      def create_database(name, options = {}) 
     490        options = options.reverse_merge(:encoding => "utf8") 
     491 
     492        option_string = options.symbolize_keys.sum do |key, value| 
     493          case key 
     494          when :owner 
     495            " OWNER = '#{value}'" 
     496          when :template 
     497            " TEMPLATE = #{value}" 
     498          when :encoding 
     499            " ENCODING = '#{value}'" 
     500          when :tablespace 
     501            " TABLESPACE = #{value}" 
     502          when :connection_limit 
     503            " CONNECTION LIMIT = #{value}" 
     504          else 
     505            "" 
     506          end 
     507        end 
     508 
     509        execute "CREATE DATABASE #{name}#{option_string}" 
     510      end 
     511 
     512      # Drops a PostgreSQL database 
     513      # 
     514      # Example: 
     515      #   drop_database 'matt_development' 
     516      def drop_database(name) #:nodoc: 
     517        execute "DROP DATABASE IF EXISTS #{name}" 
     518      end 
     519 
     520 
    477521      # Returns the list of all tables in the schema search path or a specified schema. 
    478522      def tables(name = nil)