-
Notifications
You must be signed in to change notification settings - Fork 23
/
Rakefile
96 lines (85 loc) · 3.11 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
93
94
95
96
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'yaml'
require_relative 'spec/support/database_connection.rb'
require 'active_record'
require 'rubocop/rake_task'
load 'tasks/dev.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, aliases: true)[DATABASE_ENV]
end
task configure_connection: :configuration do
DatabaseConnection.connect!(DATABASE_ENV)
ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
@config = if ::ActiveRecord::Base.configurations.respond_to?(:configs_for)
::ActiveRecord::Base.configurations.configs_for(env_name: DATABASE_ENV.to_s)[0]
else
::ActiveRecord::Base.configurations[DATABASE_ENV.to_s]
end
end
desc 'Create the database from db/config.yml for the current DATABASE_ENV'
task create: :configure_connection do
begin
database = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(@config)
database.create
rescue
puts "Database already exists."
end
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
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
elsif ActiveRecord.version < Gem::Version.new("7.2.0")
ActiveRecord::Base.connection.migration_context.migrate(version) do |migration|
scope.blank? || scope == migration.scope
end
else
ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths).migrate(version) do |migration|
scope.blank? || scope == migration.scope
end
end
ActiveRecord::Base.clear_cache!
ensure
ActiveRecord::Migration.verbose = verbose_was
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