From 6c2a0675f11a9b5b8e88ed7dbccd65cb51be8029 Mon Sep 17 00:00:00 2001 From: Rizwan Reza Date: Sun, 28 Mar 2010 11:01:15 +0430 Subject: When creating database with rake, create schemas in schema_search_path if it doesn't exist. --- .../connection_adapters/postgresql_adapter.rb | 21 +++++++++++++++++++++ .../lib/active_record/railties/databases.rake | 6 ++++++ .../test/cases/active_schema_test_postgresql.rb | 7 +++++++ activerecord/test/cases/adapter_test.rb | 6 ++++++ 4 files changed, 40 insertions(+) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index c9a5f00dfd..8f71c7e144 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -651,6 +651,27 @@ module ActiveRecord end end + # Creates a schema for the given user + # + # Example: + # create_schema('products', 'postgres') + def create_schema(schema_name, pg_username) + execute("CREATE SCHEMA \"#{schema_name}\" AUTHORIZATION \"#{pg_username}\"") + end + + # Drops a schema + # + # Example: + # drop_schema('products', 'postgres') + def drop_schema(schema_name) + execute("DROP SCHEMA \"#{schema_name}\"") + end + + # Returns an array of all schemas in the database + def all_schemas + query('SELECT schema_name FROM information_schema.schemata').flatten + end + # Returns the list of all tables in the schema search path or a specified schema. def tables(name = nil) query(<<-SQL, name).map { |row| row[0] } diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 0229793a9a..06485b9033 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -85,8 +85,14 @@ namespace :db do end when 'postgresql' @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8' + schema_search_path = config['schema_search_path'] || 'public' + first_in_schema_search_path = schema_search_path.split(',').first.strip begin ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public')) + unless ActiveRecord::Base.connection.all_schemas.include?(first_in_schema_search_path) + ActiveRecord::Base.connection.create_schema(first_in_schema_search_path, config['username']) + $stderr.puts "Schema #{first_in_schema_search_path} has been created." + end ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding)) ActiveRecord::Base.establish_connection(config) rescue diff --git a/activerecord/test/cases/active_schema_test_postgresql.rb b/activerecord/test/cases/active_schema_test_postgresql.rb index af80f724f2..67c662d694 100644 --- a/activerecord/test/cases/active_schema_test_postgresql.rb +++ b/activerecord/test/cases/active_schema_test_postgresql.rb @@ -17,6 +17,13 @@ class PostgresqlActiveSchemaTest < Test::Unit::TestCase assert_equal %(CREATE DATABASE "aimonetti" ENCODING = 'latin1'), create_database(:aimonetti, :encoding => :latin1) end + def test_create_schema + assert_equal %(CREATE SCHEMA "rizwan" AUTHORIZATION "postgres"), create_schema(:rizwan, :postgres) + end + + def test_drop_schema + assert_equal %(DROP SCHEMA "rizwan"), drop_schema(:rizwan) + end private def method_missing(method_symbol, *arguments) ActiveRecord::Base.connection.send(method_symbol, *arguments) diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 9b28766405..9f78ae008c 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -81,6 +81,12 @@ class AdapterTest < ActiveRecord::TestCase def test_encoding assert_not_nil @connection.encoding end + + def test_all_schemas + @connection.create_schema(:test_schema, :postgres) + assert @connection.all_schemas.include?('test_schema') + @connection.drop_schema(:test_schema) + end end def test_table_alias -- cgit v1.2.3