Class: Apartment::Adapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/apartment/adapters/abstract_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, defaults = {}) ⇒ AbstractAdapter

@constructor

@param {Hash} config Database config
@param {Hash} defaults Some default options


13
14
15
16
# File 'lib/apartment/adapters/abstract_adapter.rb', line 13

def initialize(config, defaults = {})
  @config = config
  @defaults = defaults
end

Instance Method Details

#create(database) ⇒ Object

Create a new database, import schema, seed if appropriate

@param {String} database Database name


22
23
24
25
26
27
28
29
30
31
32
33
# 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

#current_databaseObject

Get the current database name

@return {String} current database name


39
40
41
# File 'lib/apartment/adapters/abstract_adapter.rb', line 39

def current_database
  ActiveRecord::Base.connection.current_database
end

#drop(database) ⇒ Object

Drop the database

@param {String} database Database name


47
48
49
50
51
52
53
# 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

#environmentify(database) ⇒ Object

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


60
61
62
# File 'lib/apartment/adapters/abstract_adapter.rb', line 60

def environmentify(database)
  Apartment.prepend_environment && !database.include?(Rails.env) ? "#{Rails.env}_#{database}" : database
end

#process(database = nil) ⇒ Object

Connect to db, do your biz, switch back to previous db

@param {String?} database Database or schema to connect to


68
69
70
71
72
73
74
75
# 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

#process_excluded_modelsObject

Establish a new connection for each specific excluded model



79
80
81
82
83
84
85
86
87
88
89
90
91
# 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

#resetObject

Reset the database connection to the default



95
96
97
# File 'lib/apartment/adapters/abstract_adapter.rb', line 95

def reset
  ActiveRecord::Base.establish_connection @config
end

#seed_dataObject Also known as: seed

Load the rails seed file into the db



112
113
114
# 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(database = nil) ⇒ Object

Switch to new connection (or schema if appopriate)

@param {String} database Database name


103
104
105
106
107
108
# 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