Class: Hookup::PostCheckout

Inherits:
Object
  • Object
show all
Defined in:
lib/hookup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, *args) ⇒ PostCheckout

Returns a new instance of PostCheckout.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/hookup.rb', line 125

def initialize(environment, *args)
  @env ||= environment.to_hash.dup
  require 'optparse'
  opts = OptionParser.new
  opts.banner = "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

#envObject (readonly)

Returns the value of attribute env.



105
106
107
# File 'lib/hookup.rb', line 105

def env
  @env
end

#newObject (readonly)

Returns the value of attribute new.



105
106
107
# File 'lib/hookup.rb', line 105

def new
  @new
end

#oldObject (readonly)

Returns the value of attribute old.



105
106
107
# File 'lib/hookup.rb', line 105

def old
  @old
end

Instance Method Details

#bundleObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hookup.rb', line 165

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

Returns:

  • (Boolean)


161
162
163
# File 'lib/hookup.rb', line 161

def bundler?
  File.exist?('Gemfile')
end

#migrateObject



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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/hookup.rb', line 184

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/hookup.rb', line 107

def partial?
  @partial
end

#possible_schemasObject



115
116
117
118
119
# File 'lib/hookup.rb', line 115

def possible_schemas
  %w(development_structure.sql schema.rb structure.sql).map do |file|
    File.join schema_dir, file
  end
end

#rake(*args) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/hookup.rb', line 231

def rake(*args)
  Dir.chdir(working_dir) do
    if File.executable?('bin/rake')
      system 'bin/rake', *args
    elsif bundler?
      system 'bundle', 'exec', 'rake', *args
    else
      system 'rake', *args
    end
  end
end

#runObject



153
154
155
156
157
158
159
# File 'lib/hookup.rb', line 153

def run
  return if skipped? || env['GIT_REFLOG_ACTION'] =~ /^(?:pull|rebase)/
  unless partial?
    bundle
    migrate
  end
end

#schema_dirObject



111
112
113
# File 'lib/hookup.rb', line 111

def schema_dir
  File.join(working_dir, env['HOOKUP_SCHEMA_DIR'])
end

#skipped?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/hookup.rb', line 243

def skipped?
  env['SKIP_HOOKUP']
end

#working_dirObject



121
122
123
# File 'lib/hookup.rb', line 121

def working_dir
  env['HOOKUP_WORKING_DIR'] || '.'
end