/
Rakefile
52 lines (44 loc) · 1.4 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
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
require 'active_record'
RSpec::Core::RakeTask.new(:spec)
task :default do
Rake::Task["db:setup"].invoke
Rake::Task[:spec].invoke
end
namespace :db do
desc 'Create and configure test database'
task :setup do
spec_dir = File.expand_path(File.dirname(__FILE__) + '/spec')
db_config = "#{spec_dir}/db_config.yml"
unless File.exists?(db_config)
STDOUT.puts "Database configuration file #{db_config} could not been found."
else
STDOUT.puts "Try running migrations"
Rake::Task["db:drop"].invoke
Rake::Task["db:migrate"].invoke
end
end
desc 'Run migrations for test database'
task :migrate do
spec_dir = File.expand_path(File.dirname(__FILE__) + '/spec')
db_config = "#{spec_dir}/db_config.yml"
ActiveRecord::Base.establish_connection(YAML.load_file(db_config))
ActiveRecord::Migration.instance_eval do
create_table :blogs do |t|
t.string :title
t.text :content
t.string :user_name
end
end
end
desc 'Drop tables from test database'
task :drop do
spec_dir = File.expand_path(File.dirname(__FILE__) + '/spec')
db_config = "#{spec_dir}/db_config.yml"
ActiveRecord::Base.establish_connection(YAML.load_file(db_config))
ActiveRecord::Migration.instance_eval do
drop_table :blogs if self.table_exists?(:blogs)
end
end
end