class Apartment::Adapters::AbstractAdapter
Public Class Methods
@constructor @param {Hash} config Database
config @param {Hash} defaults Some default options
# File lib/apartment/adapters/abstract_adapter.rb, line 13 def initialize(config, defaults = {}) @config = config @defaults = defaults end
Public Instance Methods
Create a new database, import schema, seed if appropriate
@param {String} database Database
name
# File lib/apartment/adapters/abstract_adapter.rb, line 22 def create(database) create_database(database) process(database) do import_database_schema # Seed data if appropriate seed_data if Apartment.seed_after_create yield if block_given? end end
Get the current database name
@return {String} current database name
# File lib/apartment/adapters/abstract_adapter.rb, line 39 def current_database ActiveRecord::Base.connection.current_database end
Drop the database
@param {String} database Database
name
# File lib/apartment/adapters/abstract_adapter.rb, line 47 def drop(database) # ActiveRecord::Base.connection.drop_database note that drop_database will not throw an exception, so manually execute ActiveRecord::Base.connection.execute("DROP DATABASE #{environmentify(database)}" ) rescue ActiveRecord::StatementInvalid => e raise DatabaseNotFound, "The database #{environmentify(database)} cannot be found" end
Prepend the environment if configured and the environment isn’t already there
@param {String} database Database
name @return {String} database name with Rails environment optionally prepended
# File lib/apartment/adapters/abstract_adapter.rb, line 60 def environmentify(database) Apartment.prepend_environment && !database.include?(Rails.env) ? "#{Rails.env}_#{database}" : database end
Connect to db, do your biz, switch back to previous db
@param {String?} database Database
or schema to connect to
# File lib/apartment/adapters/abstract_adapter.rb, line 68 def process(database = nil) current_db = current_database switch(database) yield if block_given? ensure switch(current_db) rescue reset end
Establish a new connection for each specific excluded model
# File lib/apartment/adapters/abstract_adapter.rb, line 79 def process_excluded_models # All other models will shared a connection (at ActiveRecord::Base) and we can modify at will 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.establish_connection @config end end
Reset the database connection to the default
# File lib/apartment/adapters/abstract_adapter.rb, line 95 def reset ActiveRecord::Base.establish_connection @config end
Load the rails seed file into the db
# File lib/apartment/adapters/abstract_adapter.rb, line 112 def seed_data silence_stream(STDOUT){ load_or_abort("#{Rails.root}/db/seeds.rb") } # Don't log the output of seeding the db end
Switch to new connection (or schema if appopriate)
@param {String} database Database
name
# File lib/apartment/adapters/abstract_adapter.rb, line 103 def switch(database = nil) # Just connect to default db and return return reset if database.nil? connect_to_new(database) end
Protected Instance Methods
Connect to new database
@param {String} database Database
name
# File lib/apartment/adapters/abstract_adapter.rb, line 134 def connect_to_new(database) ActiveRecord::Base.establish_connection multi_tenantify(database) ActiveRecord::Base.connection.active? # call active? to manually check if this connection is valid rescue ActiveRecord::StatementInvalid => e raise DatabaseNotFound, "The database #{environmentify(database)} cannot be found." end
Create the database
@param {String} database Database
name
# File lib/apartment/adapters/abstract_adapter.rb, line 123 def create_database(database) ActiveRecord::Base.connection.create_database( environmentify(database) ) rescue ActiveRecord::StatementInvalid => e raise DatabaseExists, "The database #{environmentify(database)} already exists." end
Import the database schema
# File lib/apartment/adapters/abstract_adapter.rb, line 144 def import_database_schema ActiveRecord::Schema.verbose = false # do not log schema load output. load_or_abort("#{Rails.root}/db/schema.rb") end
Load a file or abort if it doesn’t exists
# File lib/apartment/adapters/abstract_adapter.rb, line 159 def load_or_abort(file) if File.exists?(file) load(file) else abort %{#{file} doesn't exist yet} end end
Return a new config that is multi-tenanted
# File lib/apartment/adapters/abstract_adapter.rb, line 151 def multi_tenantify(database) @config.clone.tap do |config| config[:database] = environmentify(database) end end
Remove all non-alphanumeric characters
# File lib/apartment/adapters/abstract_adapter.rb, line 169 def sanitize(database) warn "[Deprecation Warning] Sanitize is no longer used, client should ensure proper database names" database.gsub(/[\W]/,'') end