From 78bf1e192a09bf25b7e14821fa1f021aa7241e65 Mon Sep 17 00:00:00 2001 From: Jim Gay Date: Sat, 20 Dec 2008 00:34:27 -0500 Subject: [PATCH] create schemas in schema_search_path if it does not yet exist when creating database with rake task --- .../connection_adapters/postgresql_adapter.rb | 12 ++++++++++++ .../test/cases/active_schema_test_postgresql.rb | 4 ++++ activerecord/test/cases/adapter_test.rb | 4 ++++ railties/lib/tasks/databases.rake | 6 ++++++ 4 files changed, 26 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 913bb52..6eb5b68 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -606,6 +606,18 @@ module ActiveRecord end end + # Creates a schema for the given user + # + # Example: + # create_schema('products', 'jim') + def create_schema(name, username) + query("CREATE SCHEMA #{name} AUTHORIZATION #{username}") + 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) diff --git a/activerecord/test/cases/active_schema_test_postgresql.rb b/activerecord/test/cases/active_schema_test_postgresql.rb index af80f72..4ae71d2 100644 --- a/activerecord/test/cases/active_schema_test_postgresql.rb +++ b/activerecord/test/cases/active_schema_test_postgresql.rb @@ -16,6 +16,10 @@ class PostgresqlActiveSchemaTest < Test::Unit::TestCase assert_equal %(CREATE DATABASE "matt" ENCODING = 'utf8'), create_database(:matt) assert_equal %(CREATE DATABASE "aimonetti" ENCODING = 'latin1'), create_database(:aimonetti, :encoding => :latin1) end + + def test_create_schema + assert_equal %(CREATE SCHEMA "products" AUTHORIZATION "jim"), create_schema(:products, :jim) + end private def method_missing(method_symbol, *arguments) diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 0477064..f335980 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -69,6 +69,10 @@ class AdapterTest < ActiveRecord::TestCase def test_encoding assert_not_nil @connection.encoding end + + def test_all_schemas + assert_equal @connection.all_schemas, %w{test_schema test_schema2} + end end def test_table_alias diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index 9588fab..4e675ef 100644 --- a/railties/lib/tasks/databases.rake +++ b/railties/lib/tasks/databases.rake @@ -65,8 +65,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 -- 1.6.0.1