Class: Apartment::Adapters::PostgresqlSchemaAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- Apartment::Adapters::PostgresqlSchemaAdapter
- Defined in:
- lib/apartment/adapters/postgresql_adapter.rb
Overview
Separate Adapter for Postgresql when using schemas
Instance Method Summary collapse
-
#connect_to_new(database = nil) ⇒ Object
Set schema path or connect to new db.
-
#create(database) ⇒ Object
Create a db schema.
-
#current_database ⇒ Object
Get the current schema search path.
-
#process_excluded_models ⇒ Object
Set the table_name to always use the public namespace for excluded models.
-
#reset ⇒ Object
Reset schema search path to the default schema_search_path.
Methods inherited from AbstractAdapter
#environmentify, #initialize, #process, #seed_data, #switch
Constructor Details
This class inherits a constructor from Apartment::Adapters::AbstractAdapter
Instance Method Details
#connect_to_new(database = nil) ⇒ Object
Set schema path or connect to new db
24 25 26 27 28 29 30 |
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 24 def connect_to_new(database = nil) return reset if database.nil? ActiveRecord::Base.connection.schema_search_path = database rescue ActiveRecord::StatementInvalid => e raise SchemaNotFound, "The Schema #{database.inspect} cannot be found." end |
#create(database) ⇒ Object
Create a db schema
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 34 def create(database) ActiveRecord::Base.connection.execute("CREATE SCHEMA #{database}") process(database) do import_database_schema # Seed data if appropriate seed_data if Apartment.seed_after_create end rescue ActiveRecord::StatementInvalid => e raise SchemaExists, "The schema #{database} already exists." end |
#current_database ⇒ Object
Get the current schema search path
@return {String} current schema search path
52 53 54 |
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 52 def current_database ActiveRecord::Base.connection.schema_search_path end |
#process_excluded_models ⇒ Object
Set the table_name to always use the public namespace for excluded models
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 58 def process_excluded_models Apartment.excluded_models.each do |excluded_model| # Note that due to rails reloading, we now take string references to classes rather than # actual object references. This way when we contantize, we always get the proper class reference if excluded_model.is_a? Class warn "[Deprecation Warning] Passing class references to excluded models is now deprecated, please use a string instead" excluded_model = excluded_model.name end excluded_model.constantize.tap do |klass| # some models (such as delayed_job) seem to load and cache their column names before this, # so would never get the public prefix, so reset first klass.reset_column_information # Ensure that if a schema *was* set, we override table_name = klass.table_name.split('.', 2).last # Not sure why, but Delayed::Job somehow ignores table_name_prefix... so we'll just manually set table name instead klass.table_name = "public.#{table_name}" end end end |
#reset ⇒ Object
Reset schema search path to the default schema_search_path
@return {String} default schema search path
86 87 88 |
# File 'lib/apartment/adapters/postgresql_adapter.rb', line 86 def reset ActiveRecord::Base.connection.schema_search_path = @defaults[:schema_search_path] end |