Skip to content

Instantly share code, notes, and snippets.

@urfolomeus
Created August 4, 2012 15:12
Show Gist options
  • Select an option

  • Save urfolomeus/3258292 to your computer and use it in GitHub Desktop.

Select an option

Save urfolomeus/3258292 to your computer and use it in GitHub Desktop.

Revisions

  1. urfolomeus revised this gist Aug 5, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion heroku_db_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ def initialize(app_name)
    end

    def run
    raise "No app_name set" unless app_name
    raise "No app_name set" if @app_name.nil? or @app_name == ""

    maintenance :on
    add_required_addons
  2. urfolomeus revised this gist Aug 5, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions heroku_db_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,8 @@ def initialize(app_name)
    end

    def run
    raise "No app_name set" unless app_name

    maintenance :on
    add_required_addons
    transfer
  3. urfolomeus revised this gist Aug 5, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion heroku_db_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@ def add(addon)
    end

    def get_addons
    heroku('addons').split(/\n/).map{|a| a.split(' => ').first}.uniq
    heroku('addons').split(/\n/).map{|a| a.split(' => ').first}.compact
    end

    def installed?(addon)
  4. urfolomeus revised this gist Aug 5, 2012. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions heroku_db_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    # - 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
    # 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>
    @@ -45,6 +45,11 @@ def run

    private

    def maintenance(state)
    heroku "maintenance:#{state}"
    puts "maintenance #{state}"
    end

    def add_required_addons
    add "heroku-postgresql:dev"
    add "pgbackups"
    @@ -57,9 +62,12 @@ def add(addon)
    end
    end

    def installed?(addon)
    @addons ||= heroku('addons').split(/\n/).map{|a| a.split(' => ').first}
    def get_addons
    heroku('addons').split(/\n/).map{|a| a.split(' => ').first}.uniq
    end

    def installed?(addon)
    @addons ||= get_addons
    not @addons.select {|a| a.include?(addon)}.empty?
    end

    @@ -68,11 +76,6 @@ def transfer
    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"
  5. urfolomeus revised this gist Aug 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion heroku_db_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    ## 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!
  6. urfolomeus revised this gist Aug 4, 2012. 1 changed file with 3 additions and 10 deletions.
    13 changes: 3 additions & 10 deletions heroku_db_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -12,11 +12,10 @@
    # - 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
    # 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>

    # heroku pg:promote SHARED_DATABASE --app <app_name>

    ## N.B.
    # If you do this before Aug 9th then you get:
    @@ -47,7 +46,6 @@ def run
    private

    def add_required_addons

    add "heroku-postgresql:dev"
    add "pgbackups"
    end
    @@ -95,17 +93,12 @@ def heroku(command)
    end
    end

    done = IGNORE_LIST

    apps = `heroku apps`.split(/\n/)
    apps.reject! {|app| app =~ /\s+/} if IGNORE_OTHERS_APPS
    apps -= done

    p apps
    apps -= IGNORE_LIST

    apps.each do |app|
    puts "\nTransferring app: #{app}"
    HerokuDBMigrator.new(app).run
    done << app
    puts
    end
  7. urfolomeus created this gist Aug 4, 2012.
    111 changes: 111 additions & 0 deletions heroku_db_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    ## 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:
    # - 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

    done = IGNORE_LIST

    apps = `heroku apps`.split(/\n/)
    apps.reject! {|app| app =~ /\s+/} if IGNORE_OTHERS_APPS
    apps -= done

    p apps

    apps.each do |app|
    puts "\nTransferring app: #{app}"
    HerokuDBMigrator.new(app).run
    done << app
    puts
    end