-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRakefile
46 lines (37 loc) · 1.33 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require "bundler/gem_tasks"
require "rspec/core/rake_task"
namespace :spec do
task :db_drop do
with_connection("template1") { |conn| conn.execute("DROP DATABASE IF EXISTS #{test_database_name}") }
end
task :db_create do
with_connection("template1") { |conn| conn.execute("CREATE DATABASE #{test_database_name}") }
end
task :db_load_schema do
with_connection(test_database_name) { load File.join(__dir__, %w[spec schema.rb]) }
end
desc "Setup test database"
task :setup => [:db_drop, :db_create, :db_load_schema]
def connection_spec
require 'yaml'
@connection_spec ||=
if YAML.respond_to?(:safe_load)
YAML.safe_load(File.read(File.join(__dir__, %w[config database.yml])), :aliases => true)
else
YAML.load_file(File.join(__dir__, %w[config database.yml]))
end
end
def test_database_name
connection_spec["test"]["database"]
end
def with_connection(database_name)
require "logger" # Require logger due to active_record breaking on Rails <= 7.0. See https://github.com/rails/rails/pull/54264
require "active_record"
pool = ActiveRecord::Base.establish_connection(connection_spec["test"].merge("database" => database_name))
yield ActiveRecord::Base.connection
ensure
pool&.disconnect!
end
end
RSpec::Core::RakeTask.new(:spec)
task :default => :spec