class Apartment::Adapters::PostgresqlSchemaAdapter

Separate Adapter for Postgresql when using schemas

Separate Adapter for Postgresql when using schemas

Public Instance Methods

current_database() click to toggle source

Get the current schema search path

@return {String} current schema search path

# File lib/apartment/adapters/jdbcpostgresql_adapter.rb, line 42
def current_database
  ActiveRecord::Base.connection.schema_search_path
end
drop(database) click to toggle source

Drop the database schema

@param {String} database Database (schema) to drop

# File lib/apartment/adapters/jdbcpostgresql_adapter.rb, line 50
def drop(database)
  ActiveRecord::Base.connection.execute("DROP SCHEMA #{database} CASCADE")

rescue ActiveRecord::StatementInvalid
  raise SchemaNotFound, "The schema #{database.inspect} cannot be found."
end
process_excluded_models() click to toggle source

Reset search path to default search_path Set the table_name to always use the public namespace for excluded models

# File lib/apartment/adapters/jdbcpostgresql_adapter.rb, line 60
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() click to toggle source

Reset schema search path to the default schema_search_path

@return {String} default schema search path

# File lib/apartment/adapters/jdbcpostgresql_adapter.rb, line 87
def reset
  ActiveRecord::Base.connection.schema_search_path = @defaults[:schema_search_path]
end

Protected Instance Methods

connect_to_new(database = nil) click to toggle source

Set schema search path to new schema

# File lib/apartment/adapters/jdbcpostgresql_adapter.rb, line 95
def connect_to_new(database = nil)
  return reset if database.nil?
  ActiveRecord::Base.connection.schema_search_path = database

rescue ActiveRecord::StatementInvalid
  raise SchemaNotFound, "The schema #{database.inspect} cannot be found."
end
create_database(database) click to toggle source

Create the new schema

# File lib/apartment/adapters/jdbcpostgresql_adapter.rb, line 105
def create_database(database)
  ActiveRecord::Base.connection.execute("CREATE SCHEMA #{database}")

rescue ActiveRecord::StatementInvalid
  raise SchemaExists, "The schema #{database} already exists."
end