-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
92 lines (80 loc) · 2.68 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
# frozen_string_literal: true
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require 'rubocop/rake_task'
require 'rspec/core/rake_task'
require 'yaml'
require 'config/database_connection'
require 'active_record'
task(:default).clear
task default: [:spec]
if defined? RSpec
task(:spec).clear
RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
end
end
desc 'Run RuboCop style checker'
RuboCop::RakeTask.new(:rubocop) do |task|
task.requires << 'rubocop-rspec'
task.fail_on_error = true
end
task default: "bundler:audit"
Dir['./lib/tasks/*.rake'].each do |rakefile|
import rakefile
end
namespace :db do
task :environment do
path = File.join(File.dirname(__FILE__), './db/migrate')
migrations_paths = [path]
DATABASE_ENV = ENV['RACK_ENV'] || 'test'
MIGRATIONS_DIR = ENV['MIGRATIONS_DIR'] || migrations_paths
end
task configuration: :environment do
@config = YAML.safe_load(ERB.new(File.read("db/config.yml")).result, [], [], true)[DATABASE_ENV]
end
task configure_connection: :configuration do
DatabaseConnection.connect!(DATABASE_ENV)
ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
end
desc 'Migrate the database (options: VERSION=x, VERBOSE=false).'
task migrate: :configure_connection do
begin
verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
scope = ENV['SCOPE']
verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = verbose
if ActiveRecord::Migrator.respond_to?(:migrate)
# ActiveRecord < 5.2.0
ActiveRecord::Migrator.migrate(MIGRATIONS_DIR, version) do |migration|
scope.blank? || scope == migration.scope
end
else
# ActiveRecord >= 5.2.0
ActiveRecord::MigrationContext.new(MIGRATIONS_DIR).migrate(version) do |migration|
scope.blank? || scope == migration.scope
end
end
ActiveRecord::Base.clear_cache!
ensure
ActiveRecord::Migration.verbose = verbose_was
end
end
namespace :schema do
task :load do
Rake::Task["db:migrate"].invoke
end
end
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
task rollback: :configure_connection do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
if ActiveRecord::Migrator.respond_to?(:rollback)
# ActiveRecord < 5.2.0
ActiveRecord::Migrator.rollback(MIGRATIONS_DIR, step)
else
# ActiveRecord >= 5.2.0
ActiveRecord::MigrationContext.new(MIGRATIONS_DIR).rollback(step)
end
end
end