From e4e3df8ef8b198416fedc80743caf37c5aaff9fb Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 1 Apr 2008 05:01:10 +0000 Subject: PostgreSQL: create_ and drop_database support. Closes #9042. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9182 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../connection_adapters/postgresql_adapter.rb | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 41024539b2..674e92ee29 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -474,6 +474,50 @@ module ActiveRecord # SCHEMA STATEMENTS ======================================== + def recreate_database(name) #:nodoc: + drop_database(name) + create_database(name) + end + + # Create a new PostgreSQL database. Options include :owner, :template, + # :encoding, :tablespace, and :connection_limit (note that MySQL uses + # :charset while PostgreSQL uses :encoding). + # + # Example: + # create_database config[:database], config + # create_database 'foo_development', :encoding => 'unicode' + def create_database(name, options = {}) + options = options.reverse_merge(:encoding => "utf8") + + option_string = options.symbolize_keys.sum do |key, value| + case key + when :owner + " OWNER = '#{value}'" + when :template + " TEMPLATE = #{value}" + when :encoding + " ENCODING = '#{value}'" + when :tablespace + " TABLESPACE = #{value}" + when :connection_limit + " CONNECTION LIMIT = #{value}" + else + "" + end + end + + execute "CREATE DATABASE #{name}#{option_string}" + end + + # Drops a PostgreSQL database + # + # Example: + # drop_database 'matt_development' + def drop_database(name) #:nodoc: + execute "DROP DATABASE IF EXISTS #{name}" + end + + # Returns the list of all tables in the schema search path or a specified schema. def tables(name = nil) schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') -- cgit v1.2.3