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



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
63
64
65
66
# File 'lib/schema_dev/rspec/db.rb', line 33

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



68
69
70
71
72
73
74
75
# File 'lib/schema_dev/rspec/db.rb', line 68

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

#logrootObject



29
30
31
# File 'lib/schema_dev/rspec/db.rb', line 29

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

#set_loggerObject



77
78
79
80
81
# File 'lib/schema_dev/rspec/db.rb', line 77

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

#setupObject



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 = 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