forked from mikecmpbll/apartment
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
92 lines (74 loc) · 2.6 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'bundler' rescue 'You must `gem install bundler` and `bundle install` to run rake tasks'
Bundler.setup
Bundler::GemHelper.install_tasks
require "rake/testtask"
require 'yaml'
Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = false
# t.verbose = true
t.test_files = FileList['test/*_test.rb']
end
require 'appraisal'
# require "#{File.join(File.dirname(__FILE__), 'test', 'test_helper')}"
task :console do
require 'pry'
require 'apartment'
ARGV.clear
Pry.start
end
task default: :test
namespace :db do
namespace :test do
task prepare: %w{postgres:drop_db postgres:build_db mysql:drop_db mysql:build_db}
end
desc "copy sample database credential files over if real files don't exist"
task :copy_credentials do
require 'fileutils'
apartment_db_file = 'test/databases.yml'
rails_db_file = 'test/dummy/config/database.yml'
FileUtils.copy(apartment_db_file + '.sample', apartment_db_file, :verbose => true) unless File.exists?(apartment_db_file)
FileUtils.copy(rails_db_file + '.sample', rails_db_file, :verbose => true) unless File.exists?(rails_db_file)
end
end
namespace :postgres do
require 'active_record'
desc 'Build the PostgreSQL test databases'
task :build_db do
%x{ createdb -E UTF8 #{pg_config['database']} -U#{pg_config['username']} } rescue "test db already exists"
ActiveRecord::Base.establish_connection pg_config
ActiveRecord::Migration.suppress_messages do
load(File.join(File.dirname(__FILE__), "test/dummy/db/schema.rb"))
end
end
desc "drop the PostgreSQL test database"
task :drop_db do
puts "dropping database #{pg_config['database']}"
%x{ dropdb #{pg_config['database']} -U#{pg_config['username']} }
end
end
namespace :mysql do
require 'active_record'
desc 'Build the MySQL test databases'
task :build_db do
%x{ /usr/local/mysql/bin/mysqladmin -u #{my_config['username']} --password=#{my_config['password']} create #{my_config['database']} } rescue "test db already exists"
ActiveRecord::Base.establish_connection my_config
ActiveRecord::Migration.suppress_messages do
load(File.join(File.dirname(__FILE__), "test/dummy/db/schema.rb"))
end
end
desc "drop the MySQL test database"
task :drop_db do
puts "dropping database #{my_config['database']}"
%x{ /usr/local/mysql/bin/mysqladmin -u #{my_config['username']} --password=#{my_config['password']} drop #{my_config['database']} --force}
end
end
def config
@config ||= YAML.load(ERB.new(IO.read('test/databases.yml')).result)['connections']
end
def pg_config
config['postgresql']
end
def my_config
config['mysql']
end