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.



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
115
# File 'lib/hookup.rb', line 89

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 schema_dir && File.directory?(schema_dir)
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



75
76
77
# File 'lib/hookup.rb', line 75

def env
  @env
end

#newObject (readonly)

Returns the value of attribute new.



75
76
77
# File 'lib/hookup.rb', line 75

def new
  @new
end

#oldObject (readonly)

Returns the value of attribute old.



75
76
77
# File 'lib/hookup.rb', line 75

def old
  @old
end

Instance Method Details

#bundleObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hookup.rb', line 129

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)


125
126
127
# File 'lib/hookup.rb', line 125

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

#migrateObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
# File 'lib/hookup.rb', line 148

def migrate
  schemas = %W(#{schema_dir}/schema.rb #{schema_dir}/development_structure.sql).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)


77
78
79
# File 'lib/hookup.rb', line 77

def partial?
  @partial
end

#rake(*args) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/hookup.rb', line 195

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

#runObject



117
118
119
120
121
122
123
# File 'lib/hookup.rb', line 117

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

#schema_dirObject



81
82
83
# File 'lib/hookup.rb', line 81

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

#working_dirObject



85
86
87
# File 'lib/hookup.rb', line 85

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