aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb23
1 files changed, 21 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index b3ce8c79dd..31d5266da8 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -651,14 +651,33 @@ 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')
+ 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)
- schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
query(<<-SQL, name).map { |row| row[0] }
SELECT tablename
FROM pg_tables
- WHERE schemaname IN (#{schemas})
+ WHERE schemaname = ANY (current_schemas(false))
SQL
end