Created
August 3, 2012 17:02
-
-
Save skoczen/3249559 to your computer and use it in GitHub Desktop.
Heroku shared-database to dev migration helper
This file contains 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
#!python | |
# Run this file to upgrade your shared database to a dev plan. | |
# Make sure you're using the heroku gem >= 2.30.1 | |
# Usage: python migrate.py | |
# Follow the interactive prompts. | |
import sys | |
import subprocess | |
def remove_shared_database_after_confirmation(app_name): | |
addon_resp = subprocess.check_output(["heroku", "addons"]) | |
if "shared-database" in addon_resp: | |
remove_pg = raw_input("Would you like to remove the shared-database plan? Type 'yes' to remove it: ") | |
if remove_pg == "yes": | |
print "Removing shared-database...", | |
subprocess.check_output(["heroku", "addons:remove", "shared-database", "--confirm", app_name]) | |
print "done." | |
def main(): | |
# Get the app name. | |
addon_resp = subprocess.check_output(["heroku", "addons"]) | |
app_name = addon_resp[4:addon_resp.find(" ", 5)] | |
continue_resp = "nope" | |
while continue_resp.lower() != "yes": | |
continue_resp = raw_input("Detected app '%s'. Is this correct? Type 'yes' to continue: " % app_name) | |
# Confirm that we're using postgres | |
database_url = subprocess.check_output(["heroku", "config:get", "DATABASE_URL"]) | |
if "postgres" not in database_url: | |
print "This app is not using postgresql." | |
remove_shared_database_after_confirmation(app_name) | |
sys.exit("Done. Quitting.") | |
print "Adding new database...", | |
resp = subprocess.check_output(["heroku", "addons:add", "heroku-postgresql:dev"]) | |
ATTACH_STR = "Attached as " | |
# Returns: | |
# Adding heroku-postgresql:dev to blows... done, v87 (free) | |
# Attached as HEROKU_POSTGRESQL_GRAY | |
# Database has been created and is available | |
# heroku-postgresql:dev documentation available at: https://devcenter.heroku.com/articles/heroku-postgresql | |
attached_pos = resp.find(ATTACH_STR) + len(ATTACH_STR) | |
database_name = resp[attached_pos:resp.find("\n", attached_pos)] | |
print "done." | |
print "Added new database: %s" % (database_name,) | |
print "Backing up old database..." | |
try: | |
if not "pgbackups" in addon_resp: | |
subprocess.call(["heroku", "addons:add", "pgbackups"]) | |
except: | |
pass | |
subprocess.check_output(["heroku", "maintenance:on"]) | |
subprocess.call(["heroku", "pgbackups:capture", "--expire"]) | |
print "done." | |
print "Restoring to new database...", | |
subprocess.call(["heroku", "pgbackups:restore", database_name, "--confirm", app_name]) | |
print "done." | |
print "Promoting...", | |
subprocess.check_output(["heroku", "pg:promote", database_name]) | |
print "done." | |
print "Turning off maintenance and restarting...", | |
subprocess.check_output(["heroku", "maintenance:off"]) | |
subprocess.check_output(["heroku", "restart"]) | |
print "done." | |
print "Migration complete. Please test your application." | |
remove_shared_database_after_confirmation(app_name) | |
print "\n\nNow, please run postgres ANALYZE on the database to speed up indexes and query optimization." | |
print "heroku pg:psql %s " % database_name | |
print "=> ANALYZE;\n\n" | |
print "Database migration complete." | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment