class MemoryTestFix::SchemaLoader

Set up database schema into in-memory database.

Public Class Methods

init_schema() click to toggle source

Initialize the schema for an in-memory database, if it is configured. See the README for details on how to configure Rails to use an in-memory database.

# File lib/memory_test_fix/schema_loader.rb, line 10
def self.init_schema
  new.init_schema
end
new(options = {}) click to toggle source

@param [Hash] options The options to configure this instance of SchemaLoader with. @option options [Hash] :configuration The configuration of the database connection @option options :migrator The migrator to use if configured to use migrations @option options :loader The loader to use if configured to not use migrations @api private

# File lib/memory_test_fix/schema_loader.rb, line 19
def initialize(options = {})
  @configuration = options[:configuration] || ActiveRecord::Base.connection_config
  @migrator = options[:migrator] || ActiveRecord::Migrator
  @loader = options[:loader] || SchemaFileLoader
end

Public Instance Methods

init_schema() click to toggle source

Initialize the schema for the in-memoray database according to the configuration passed to the constructor. @api private

# File lib/memory_test_fix/schema_loader.rb, line 28
def init_schema
  return unless in_memory_database?

  inform_using_in_memory unless silent?

  if silent? || quiet?
    load_schema_silently
  else
    load_schema
  end
end

Private Instance Methods

in_memory?() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 46
def in_memory?
  @configuration[:database] == ':memory:' || @configuration[:dbfile] == ':memory:'
end
in_memory_database?() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 42
def in_memory_database?
  in_memory? && sqlite3?
end
inform_using_in_memory() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 70
def inform_using_in_memory
  puts 'Creating sqlite :memory: database'
end
load_schema() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 74
def load_schema
  if migrate
    if @migrator.respond_to? :up
      @migrator.up('db/migrate')
    else
      ActiveRecord::Base.connection.migration_context.up
    end
  else
    @loader.load_schema
  end
end
load_schema_silently() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 86
def load_schema_silently
  silently do
    load_schema
  end
end
migrate() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 66
def migrate
  @configuration[:migrate] == true
end
quiet?() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 62
def quiet?
  verbosity == 'quiet'
end
silent?() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 58
def silent?
  verbosity == 'silent'
end
silently() { || ... } click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 92
def silently
  tmp_stream = StringIO.new

  original = $stdout
  $stdout = tmp_stream

  yield
ensure
  $stdout = original
end
sqlite3?() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 50
def sqlite3?
  @configuration[:adapter] == 'sqlite3'
end
verbosity() click to toggle source
# File lib/memory_test_fix/schema_loader.rb, line 54
def verbosity
  @configuration[:verbosity]
end