-
-
Save mdarby/3258357 to your computer and use it in GitHub Desktop.
Quick script to migrate heroku apps to the new free individual postgres DBs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## README | |
# This is a quick script I hacked out to migrate all my heroku apps | |
# to the new free individual postgres DBs. To use it: | |
# - install the heroku gem if you don't already have it | |
# - set the value of IGNORE_OTHERS_APPS to true if you only want to | |
# run the script against apps you've created yourself | |
# - add any apps you want to ignore because they don't use PostgreSQL | |
# (or for any other reason) to the IGNORE_LIST | |
## CAVEAT!! | |
# - USE AT YOUR OWN RISK!! This works for me, but it may not work for you! | |
# - I don't remove the SHARED_DATABASE at the end of the process in | |
# case you want to rollback, so you'll need to delete it yourself once | |
# you're happy everything worked | |
# heroku addons:remove shared-database | |
# - if everything goes south you can just try again, but you'll have to make | |
# sure that you set the shared database back to be primary by running | |
# heroku pg:promote SHARED_DATABASE --app <app_name> | |
## N.B. | |
# If you do this before Aug 9th then you get: | |
# - an extra 4000 lines for your DB if on the dev plan | |
# - $20 credit if on basic or production plan | |
# | |
# More info at: https://devcenter.heroku.com/articles/migrating-from-shared-database-to-heroku-postgres | |
## OPTIONS | |
IGNORE_OTHERS_APPS = true | |
IGNORE_LIST = [ ] | |
class HerokuDBMigrator | |
def initialize(app_name) | |
@app_name = app_name | |
end | |
def run | |
maintenance :on | |
add_required_addons | |
transfer | |
rescue Exception => e | |
p e | |
ensure | |
maintenance :off | |
end | |
private | |
def add_required_addons | |
add "heroku-postgresql:dev" | |
add "pgbackups" | |
end | |
def add(addon) | |
unless installed?(addon) | |
puts "adding #{addon}" | |
heroku "addons:add #{addon}" | |
end | |
end | |
def installed?(addon) | |
@addons ||= heroku('addons').split(/\n/).map{|a| a.split(' => ').first} | |
not @addons.select {|a| a.include?(addon)}.empty? | |
end | |
def transfer | |
backup_existing_db | |
setup_new_db | |
end | |
def maintenance(state) | |
heroku "maintenance:#{state}" | |
puts "maintenance #{state}" | |
end | |
def backup_existing_db | |
puts "backing up existing DB" | |
heroku "pgbackups:capture --expire" | |
end | |
def setup_new_db | |
db_name = heroku("config").match(/(HEROKU_POSTGRESQL_.*)_URL/)[1] | |
puts "setting up #{db_name}" | |
heroku "pgbackups:restore #{db_name} --confirm #{@app_name}" | |
puts "making #{db_name} primary" | |
heroku "pg:promote #{db_name}" | |
end | |
def heroku(command) | |
`heroku #{command} --app #{@app_name}` | |
end | |
end | |
apps = `heroku apps`.split(/\n/) | |
apps.reject! {|app| app =~ /\s+/} if IGNORE_OTHERS_APPS | |
apps -= IGNORE_LIST | |
apps.each do |app| | |
puts "\nTransferring app: #{app}" | |
HerokuDBMigrator.new(app).run | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment