Skip to content

Instantly share code, notes, and snippets.

@davidakachaos
Last active December 19, 2015 04:48
Show Gist options
  • Save davidakachaos/5899357 to your computer and use it in GitHub Desktop.
Save davidakachaos/5899357 to your computer and use it in GitHub Desktop.
Custom Heroku deploy Strategy. Migrates the database only when needed. Needs heroku_san to work Needs to have all stages added as git remote! (via rake all heroku:remotes)
# Custom Heroku deploy Strategy
# Needs to have all stages added as git remote! (via rake all heroku:remotes)
if defined?(HerokuSan)
class MigrateWhenNeededStrategy < HerokuSan::Deploy::Base
def deploy
puts "[#{@stage.name}] Fetching current state."
`git fetch #{@stage.name}`
puts "[#{@stage.name}] Deploying..."
super
previous_sha_commit = `git rev-parse HEAD`.strip
puts "[#{@stage.name}] Deployed these changed:"
puts `git log --pretty=format:"* %s" #{@stage.name}/master..#{previous_sha_commit}`
puts ""
puts "[#{@stage.name}] Checking to see if we need to run the migrations."
file_changes = `git diff --name-only #{previous_sha_commit} #{@stage.name}/master`
if file_changes.index("db/migrate")
puts "[#{@stage.name}] Running migrations, as there are new ones added."
@stage.run('rake db:migrate')
puts "[#{@stage.name}] Restarting..."
@stage.restart
end
if file_changes.index("db/seeds.rb")
puts "[#{@stage.name}] It looks like you adjusted or created the seeds.rb file."
print "[#{@stage.name}] Do you want to run the rake db:seed command now? (yes/no)"
response = gets.chomp
if response.downcase == 'yes'
@stage.run('rake db:seed')
end
end
end
end
HerokuSan.project = HerokuSan::Project.new(Rails.root.join("config","heroku.yml"), :deploy => MigrateWhenNeededStrategy)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment