-
Notifications
You must be signed in to change notification settings - Fork 2
/
disable.rb
executable file
·114 lines (82 loc) · 1.92 KB
/
disable.rb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
# warn_indent: true
##########################################################
###
## File: disable.rb
## Desc: Disable/Enable migrations by their timestamp
## By: Dewayne VanHoozer (dvanhoozer@gmail.com)
#
# Assumes that $RR is the current rails root
#
require 'pathname'
RR = Pathname.new ENV['RR']
if RR.exist? && RR.directory?
# then good
else
puts <<~EOS
ERROR: $RR is not a valid Rails.root
EOS
exit -1
end
DB_MIGRATE_DIR = RR + 'db/migrate'
require 'amazing_print'
require 'debug_me'
include DebugMe
require 'cli_helper'
include CliHelper
configatron.version = '0.0.1'
HELP = <<EOHELP
Important:
requires one or more timestamps as parameters
EOHELP
cli_helper("Disable/Enable migrations by their timestamp") do |o|
end
# Display the usage info
if ARGV.empty?
show_usage
exit
end
configatron.timestamps = ARGV
# Error check your stuff; use error('some message') and warning('some message')
if configatron.timestamps.size < 1
error "requires 1 or more timestamps."
end
abort_if_errors
######################################################
# Local methods
def toggle(migration_path)
basename = migration_path.basename.to_s
if basename.end_with?('.disabled')
# remove
new_basename = basename.gsub('.disabled','')
puts "ENABLE #{new_basename}"
else
# add
new_basename = basename + '.disabled'
puts "DISABLE #{new_basename}"
end
new_path = DB_MIGRATE_DIR + new_basename
system "mv #{migration_path} #{new_path}"
end
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
ap configatron.to_h if verbose? || debug?
file_paths = DB_MIGRATE_DIR.children.select do |c|
configatron
.timestamps
.include?(
c.basename
.to_s
.split('_')&.first
)
end
file_paths.each do |migration|
toggle migration
end