Module: SchemaDev::Rspec::Db

Extended by:
Db
Included in:
Db
Defined in:
lib/schema_dev/rspec/db.rb

Defined Under Namespace

Modules: Helpers

Instance Method Summary collapse

Instance Method Details

#configurationObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/schema_dev/rspec/db.rb', line 29

def configuration
  case @db
  when 'mysql'
    {
      :adapter => 'mysql',
      :database => 'schema_plus_test',
      :username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
      :encoding => 'utf8',
      :min_messages => 'warning'
    }
  when 'mysql2'
    {
      :adapter => 'mysql2',
      :database => 'schema_plus_test',
      :username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
      :encoding => 'utf8',
      :min_messages => 'warning'
    }
  when 'postgresql'
    {
      :adapter => 'postgresql',
      :username => ENV['POSTGRESQL_DB_USER'],
      :database => 'schema_plus_test',
      :min_messages => 'warning'
    }
  when 'sqlite3'
    {
      :adapter => 'sqlite3',
      :database => tmproot.join('schema_plus.sqlite3').to_s
    }
  else
    raise "Unknown db adapter #{@db.inspect}"
  end
end

#connectObject



64
65
66
67
68
69
70
71
# File 'lib/schema_dev/rspec/db.rb', line 64

def connect
  ActiveRecord::Base.configurations = { 'schema_dev' => configuration }
  ActiveRecord::Base.establish_connection :schema_dev
  case @db
  when 'sqlite3'
    ActiveRecord::Base.connection.execute "PRAGMA synchronous = OFF"
  end
end

#set_loggerObject



73
74
75
76
77
# File 'lib/schema_dev/rspec/db.rb', line 73

def set_logger
  ruby = "#{RUBY_ENGINE}-#{RUBY_VERSION}"
  rails = "rails-#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
  ActiveRecord::Base.logger = Logger.new(tmproot.join("#{ruby}.#{rails}.#{@db}.log").open("w"))
end

#setup(db = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/schema_dev/rspec/db.rb', line 10

def setup(db = nil)
  @db = db || GemfileSelector.infer_db
  set_logger
  connect
  RSpec.configure do |config|
    config.include Helpers
    config.filter_run_excluding :postgresql => :only unless Helpers.postgresql?
    config.filter_run_excluding :postgresql => :skip if Helpers.postgresql?
    config.filter_run_excluding :mysql => :only unless Helpers.mysql?
    config.filter_run_excluding :mysql => :skip if Helpers.mysql?
    config.filter_run_excluding :sqlite3 => :only unless Helpers.sqlite3?
    config.filter_run_excluding :sqlite3 => :skip if Helpers.sqlite3?
  end
end

#tmprootObject



25
26
27
# File 'lib/schema_dev/rspec/db.rb', line 25

def tmproot
  @tmproot ||= Pathname.new('tmp').tap { |path| path.mkpath }
end