Module: PGSpecHelper::Schemas
- Included in:
- PGSpecHelper
- Defined in:
- lib/pg_spec_helper/schemas.rb
Instance Method Summary collapse
-
#create_schema(schema_name) ⇒ Object
create a new schema in the database.
-
#delete_all_schemas ⇒ Object
delete all schemas in the database.
-
#get_schema_names ⇒ Object
return a list of the schema names in the database.
- #schema_exists?(schema_name) ⇒ Boolean
Instance Method Details
#create_schema(schema_name) ⇒ Object
create a new schema in the database
6 7 8 9 10 |
# File 'lib/pg_spec_helper/schemas.rb', line 6 def create_schema schema_name connection.exec(" CREATE SCHEMA \#{connection.quote_ident schema_name.to_s};\n SQL\nend\n") |
#delete_all_schemas ⇒ Object
delete all schemas in the database
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pg_spec_helper/schemas.rb', line 28 def delete_all_schemas # delete all schemas except public get_schema_names.reject { |schema_name| schema_name == :public }.each do |schema_name| connection.exec(" -- temporarily set the client_min_messages to WARNING to\n -- suppress the NOTICE messages about cascading deletes\n SET client_min_messages TO WARNING;\n DROP SCHEMA \#{connection.quote_ident schema_name.to_s} CASCADE;\n SET client_min_messages TO NOTICE;\n SQL\n end\n # delete all the tables, functions and enums from within the public schema\n delete_tables :public\n delete_created_functions\n delete_created_enums\nend\n") |
#get_schema_names ⇒ Object
return a list of the schema names in the database
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/pg_spec_helper/schemas.rb', line 13 def get_schema_names ignored_schemas_sql = ignored_schemas.join("', '") # return a list of the schema names from the database results = connection.exec(" SELECT schema_name\n FROM information_schema.schemata\n WHERE\n schema_name NOT IN ('\#{ignored_schemas_sql}')\n AND schema_name NOT LIKE 'pg_%';\n SQL\n schema_names = results.map { |row| row[\"schema_name\"].to_sym }\n schema_names.sort\nend\n") |
#schema_exists?(schema_name) ⇒ Boolean
45 46 47 |
# File 'lib/pg_spec_helper/schemas.rb', line 45 def schema_exists? schema_name get_schema_names.include? schema_name.to_sym end |