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

#configuration(db: nil) ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/schema_dev/rspec/db.rb', line 39

def configuration(db: nil)
  case db || infer_db
  when 'mysql'
    {
      'adapter'      => 'mysql',
      'database'     => database,
      'host'         => ENV['MYSQL_DB_HOST'],
      'username'     => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
      'password'     => ENV['MYSQL_DB_PASS'],
      'encoding'     => 'utf8',
      'min_messages' => 'warning'
    }
  when 'mysql2'
    {
      'adapter'      => 'mysql2',
      'database'     => database,
      'host'         => ENV['MYSQL_DB_HOST'],
      'username'     => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
      'password'     => ENV['MYSQL_DB_PASS'],
      'encoding'     => 'utf8',
      'min_messages' => 'warning'
    }
  when 'postgresql'
    {
      'adapter'      => 'postgresql',
      'database'     => database,
      'host'         => ENV['POSTGRESQL_DB_HOST'],
      'username'     => ENV['POSTGRESQL_DB_USER'],
      'password'     => ENV['POSTGRESQL_DB_PASS'],
      'min_messages' => 'warning'
    }
  when 'sqlite3'
    {
      'adapter'  => 'sqlite3',
      'database' => tmproot.join("#{database}.sqlite3").to_s
    }
  else
    raise "Unknown db adapter #{db.inspect}"
  end.compact
end

#connectObject



84
85
86
87
88
89
90
91
# File 'lib/schema_dev/rspec/db.rb', line 84

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

#databaseObject



34
35
36
37
# File 'lib/schema_dev/rspec/db.rb', line 34

def database
  @database ||= 'schema_plus_test'
  # @database ||= (Dir["*.gemspec"].first || "schema_dev_test").sub(/\.gemspec$/, '') + "_test"
end

#infer_dbObject



80
81
82
# File 'lib/schema_dev/rspec/db.rb', line 80

def infer_db
  @infer_db ||= GemfileSelector.infer_db
end

#logrootObject



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

def logroot
  @logroot ||= Pathname.new('log').tap(&:mkpath)
end

#set_loggerObject



93
94
95
96
97
# File 'lib/schema_dev/rspec/db.rb', line 93

def set_logger
  ruby = "#{RUBY_ENGINE}#{RUBY_VERSION}"
  activerecord = "activerecord#{ActiveRecord.version}"
  ActiveRecord::Base.logger = Logger.new(logroot.join("#{ruby}-#{activerecord}-#{infer_db}.log").open('w'))
end

#setupObject



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

def setup
  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



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

def tmproot
  @tmproot ||= Pathname.new('tmp').tap(&:mkpath)
end