forked from samvera/valkyrie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
86 lines (75 loc) · 2.59 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
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'yaml'
require 'config/database_connection'
require 'active_record'
require 'rubocop/rake_task'
load 'tasks/dev.rake'
load 'tasks/docker.rake'
RSpec::Core::RakeTask.new(:spec)
task default: :spec
desc 'Run RuboCop style checker'
RuboCop::RakeTask.new(:rubocop) do |task|
task.requires << 'rubocop-rspec'
task.fail_on_error = true
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 'Create the database from db/config.yml for the current DATABASE_ENV'
task create: :configure_connection do
database = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(@config)
database.create
puts "Database created"
end
desc 'Drops the database for the current DATABASE_ENV'
task drop: :configure_connection do
database = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(@config)
database.drop
puts "Database dropped"
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::Migrator.migrate(MIGRATIONS_DIR, version) do |migration|
scope.blank? || scope == migration.scope
end
else
ActiveRecord::Base.connection.migration_context.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
ActiveRecord::Migrator.rollback(MIGRATIONS_DIR, step)
end
end