Class: Hookup::PostCheckout
- Inherits:
-
Object
- Object
- Hookup::PostCheckout
- Defined in:
- lib/hookup.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#new ⇒ Object
readonly
Returns the value of attribute new.
-
#old ⇒ Object
readonly
Returns the value of attribute old.
Instance Method Summary collapse
- #bundle ⇒ Object
- #bundler? ⇒ Boolean
-
#initialize(environment, *args) ⇒ PostCheckout
constructor
A new instance of PostCheckout.
- #migrate ⇒ Object
- #partial? ⇒ Boolean
- #possible_schemas ⇒ Object
- #rake(*args) ⇒ Object
- #run ⇒ Object
- #schema_dir ⇒ Object
- #skipped? ⇒ Boolean
- #working_dir ⇒ Object
Constructor Details
#initialize(environment, *args) ⇒ PostCheckout
Returns a new instance of PostCheckout.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/hookup.rb', line 107 def initialize(environment, *args) @env ||= environment.to_hash.dup require 'optparse' opts = OptionParser.new opts. = "Usage: hookup post-checkout <old> <new> <full>" opts.on('-Cdirectory', 'cd to directory') do |directory| env['HOOKUP_WORKING_DIR'] = directory end opts.on('--schema-dir=DIRECTORY', 'Path to DIRECTORY containing schema.rb and migrate/') do |directory| env['HOOKUP_SCHEMA_DIR'] = directory end opts.on('--load-schema=COMMAND', 'Run COMMAND on migration failure') do |command| env['HOOKUP_LOAD_SCHEMA'] = command end opts.parse!(args) @old = args.shift if @old == '0000000000000000000000000000000000000000' @old = EMPTY_DIR elsif @old.nil? @old = '@{-1}' end @new = args.shift || 'HEAD' @partial = (args.shift == '0') env['HOOKUP_SCHEMA_DIR'] = 'db' unless env['HOOKUP_SCHEMA_DIR'] && File.directory?(schema_dir) end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
87 88 89 |
# File 'lib/hookup.rb', line 87 def env @env end |
#new ⇒ Object (readonly)
Returns the value of attribute new.
87 88 89 |
# File 'lib/hookup.rb', line 87 def new @new end |
#old ⇒ Object (readonly)
Returns the value of attribute old.
87 88 89 |
# File 'lib/hookup.rb', line 87 def old @old end |
Instance Method Details
#bundle ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/hookup.rb', line 147 def bundle return unless bundler? if %x{git diff --name-only #{old} #{new}} =~ /^Gemfile|\.gemspec$/ begin # If Bundler in turn spawns Git, it can get confused by $GIT_DIR git_dir = ENV.delete('GIT_DIR') %x{bundle check} unless $?.success? puts "Bundling..." Dir.chdir(working_dir) do system("bundle | grep -v '^Using ' | grep -v ' is complete'") end end ensure ENV['GIT_DIR'] = git_dir end end end |
#bundler? ⇒ Boolean
143 144 145 |
# File 'lib/hookup.rb', line 143 def bundler? File.exist?('Gemfile') end |
#migrate ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/hookup.rb', line 166 def migrate schemas = possible_schemas.select do |schema| status = %x{git diff --name-status #{old} #{new} -- #{schema}}.chomp rake 'db:create' if status =~ /^A/ status !~ /^D/ && !status.empty? end return if schemas.empty? migrations = %x{git diff --name-status #{old} #{new} -- #{schema_dir}/migrate}.scan(/.+/).map {|l| l.split(/\t/) } begin migrations.select {|(t,f)| %w(D M).include?(t)}.reverse.each do |type, file| begin system 'git', 'checkout', old, '--', file unless rake 'db:migrate:down', "VERSION=#{File.basename(file)}" raise Error, "Failed to rollback #{File.basename(file)}" end ensure if type == 'D' system 'git', 'rm', '--force', '--quiet', '--', file else system 'git', 'checkout', new, '--', file end end end if migrations.any? {|(t,f)| %w(A M).include?(t)} rake 'db:migrate' end ensure changes = %x{git diff --name-status #{new} -- #{schemas.join(' ')}} unless changes.empty? system 'git', 'checkout', '--', *schemas puts "Schema out of sync." fallback = env['HOOKUP_LOAD_SCHEMA'] if fallback && fallback != '' puts "Trying #{fallback}..." system fallback end end end end |
#partial? ⇒ Boolean
89 90 91 |
# File 'lib/hookup.rb', line 89 def partial? @partial end |
#possible_schemas ⇒ Object
97 98 99 100 101 |
# File 'lib/hookup.rb', line 97 def possible_schemas %w(development_structure.sql schema.rb structure.sql).map do |file| File.join schema_dir, file end end |
#rake(*args) ⇒ Object
213 214 215 216 217 218 219 220 221 |
# File 'lib/hookup.rb', line 213 def rake(*args) Dir.chdir(working_dir) do if bundler? system 'bundle', 'exec', 'rake', *args else system 'rake', *args end end end |
#run ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/hookup.rb', line 135 def run return if skipped? || env['GIT_REFLOG_ACTION'] =~ /^(?:pull|rebase)/ unless partial? bundle migrate end end |
#schema_dir ⇒ Object
93 94 95 |
# File 'lib/hookup.rb', line 93 def schema_dir File.join(working_dir, env['HOOKUP_SCHEMA_DIR']) end |
#skipped? ⇒ Boolean
223 224 225 |
# File 'lib/hookup.rb', line 223 def skipped? env['SKIP_HOOKUP'] end |
#working_dir ⇒ Object
103 104 105 |
# File 'lib/hookup.rb', line 103 def working_dir env['HOOKUP_WORKING_DIR'] || '.' end |