Skip to content
This repository
Browse code

When creating database with rake, create schemas in schema_search_pat…

…h if it doesn't exist.
  • Loading branch information...
commit 6c2a0675f11a9b5b8e88ed7dbccd65cb51be8029 1 parent 8398f21
Rizwan Reza rizwanreza authored
21 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -651,6 +651,27 @@ def drop_database(name) #:nodoc:
651 651 end
652 652 end
653 653
  654 + # Creates a schema for the given user
  655 + #
  656 + # Example:
  657 + # create_schema('products', 'postgres')
  658 + def create_schema(schema_name, pg_username)
  659 + execute("CREATE SCHEMA \"#{schema_name}\" AUTHORIZATION \"#{pg_username}\"")
  660 + end
  661 +
  662 + # Drops a schema
  663 + #
  664 + # Example:
  665 + # drop_schema('products', 'postgres')
  666 + def drop_schema(schema_name)
  667 + execute("DROP SCHEMA \"#{schema_name}\"")
  668 + end
  669 +
  670 + # Returns an array of all schemas in the database
  671 + def all_schemas
  672 + query('SELECT schema_name FROM information_schema.schemata').flatten
  673 + end
  674 +
654 675 # Returns the list of all tables in the schema search path or a specified schema.
655 676 def tables(name = nil)
656 677 query(<<-SQL, name).map { |row| row[0] }
6 activerecord/lib/active_record/railties/databases.rake
@@ -85,8 +85,14 @@ namespace :db do
85 85 end
86 86 when 'postgresql'
87 87 @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
  88 + schema_search_path = config['schema_search_path'] || 'public'
  89 + first_in_schema_search_path = schema_search_path.split(',').first.strip
88 90 begin
89 91 ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
  92 + unless ActiveRecord::Base.connection.all_schemas.include?(first_in_schema_search_path)
  93 + ActiveRecord::Base.connection.create_schema(first_in_schema_search_path, config['username'])
  94 + $stderr.puts "Schema #{first_in_schema_search_path} has been created."
  95 + end
90 96 ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
91 97 ActiveRecord::Base.establish_connection(config)
92 98 rescue
7 activerecord/test/cases/active_schema_test_postgresql.rb
@@ -17,6 +17,13 @@ def test_create_database_with_encoding
17 17 assert_equal %(CREATE DATABASE "aimonetti" ENCODING = 'latin1'), create_database(:aimonetti, :encoding => :latin1)
18 18 end
19 19
  20 + def test_create_schema
  21 + assert_equal %(CREATE SCHEMA "rizwan" AUTHORIZATION "postgres"), create_schema(:rizwan, :postgres)
  22 + end
  23 +
  24 + def test_drop_schema
  25 + assert_equal %(DROP SCHEMA "rizwan"), drop_schema(:rizwan)
  26 + end
20 27 private
21 28 def method_missing(method_symbol, *arguments)
22 29 ActiveRecord::Base.connection.send(method_symbol, *arguments)
6 activerecord/test/cases/adapter_test.rb
@@ -81,6 +81,12 @@ def test_not_specifying_database_name_for_cross_database_selects
81 81 def test_encoding
82 82 assert_not_nil @connection.encoding
83 83 end
  84 +
  85 + def test_all_schemas
  86 + @connection.create_schema(:test_schema, :postgres)
  87 + assert @connection.all_schemas.include?('test_schema')
  88 + @connection.drop_schema(:test_schema)
  89 + end
84 90 end
85 91
86 92 def test_table_alias

0 comments on commit 6c2a067

Please sign in to comment.
Something went wrong with that request. Please try again.