Class: Takeoff::Stage::Heroku::MigrateDatabase

Inherits:
Base
  • Object
show all
Defined in:
lib/takeoff/stage/heroku/migrate_database.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Helpers

#branches_up_to_date?, #diff, #execute, #file_has_changed_locally?, #files_have_changed?, #latest_commit, #log

Constructor Details

This class inherits a constructor from Takeoff::Stage::Base

Class Method Details

.dangerous?(env) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/takeoff/stage/heroku/migrate_database.rb', line 7

def self.dangerous?(env)
  new(nil).dangerous?(env)
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/takeoff/stage/heroku/migrate_database.rb', line 31

def call(env)
  unless run?(env)
    log "Skipping database migrations"

    return @app.call(env) 
  end

  log     "Running database migrations"
  execute "heroku run rake db:migrate --remote #{env[:server_remote]}"

  # If ActiveRecord needs to rebuild its column name cache, restart the app.
  if dangerous?(env)
    log     "Restarting application"
    execute "heroku restart --remote #{env[:server_remote]}"
  end

  @app.call(env)
end

#dangerous?(env) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/takeoff/stage/heroku/migrate_database.rb', line 11

def dangerous?(env)
  return @dangerous if defined?(@dangerous)

  return @dangerous = false unless run?(env)

  diff = diff(env[:deployed_commit], env[:new_commit], ["db/migrate"])
  
  unsafe_active_record_terms  = /change_column|change_table|drop_table|remove_column|remove_index|rename_column|execute/
  unsafe_mongoid_terms        = /renameCollection|\.drop|$rename|$set|$unset|indexes\.create|indexes\.drop/
  unsafe_terms = Regexp.union(unsafe_active_record_terms, unsafe_mongoid_terms)

  @dangerous = diff.split("\n").any? do |line|
    line =~ unsafe_terms && line !~ /#\s*safe/i
  end
end

#run?(env) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/takeoff/stage/heroku/migrate_database.rb', line 27

def run?(env)
  files_have_changed?(env[:deployed_commit], env[:new_commit], ["db/migrate"])
end