Skip to content

Instantly share code, notes, and snippets.

@jcowhigjr
Forked from rtwomey/Heroku DB Migration.mdown
Created January 3, 2013 20:45

Revisions

  1. @rtwomey rtwomey renamed this gist Jul 23, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @rtwomey rtwomey renamed this gist Jul 23, 2012. 1 changed file with 19 additions and 19 deletions.
    38 changes: 19 additions & 19 deletions Heroku migration.mdown → Heroku DB Migration
    Original file line number Diff line number Diff line change
    @@ -6,47 +6,47 @@ The old database was named BROWN while the new one is CRIMSON. You can determin

    1. Add Crane database

    `heroku addons:add heroku-postgresql:crane --app myapp-staging`
    `heroku pg:wait --app myapp-staging`
    heroku addons:add heroku-postgresql:crane --app myapp-staging
    heroku pg:wait --app myapp-staging

    2. Turn on maintenance mode and capture a backup

    `heroku maintenance:on --app myapp-staging`
    `heroku pgbackups:capture --expire --app myapp-staging`
    heroku maintenance:on --app myapp-staging
    heroku pgbackups:capture --expire --app myapp-staging

    3. Figure out what the name of the new database is

    `heroku config --app myapp-staging | grep POSTGRESQL`
    heroku config --app myapp-staging | grep POSTGRESQL

    4. Restore the latest backup to the new database and verify it worked.

    `heroku pgbackups:restore HEROKU_POSTGRESQL_CRIMSON --app myapp-staging`
    `heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    select count(*) from users;
    => 1`
    heroku pgbackups:restore HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    select count(*) from users;
    => 1

    5. Update the app to use the new database URL

    `heroku pg:promote HEROKU_POSTGRESQL_CRIMSON --app myapp-staging`
    heroku pg:promote HEROKU_POSTGRESQL_CRIMSON --app myapp-staging

    6. Turn off maintenance mode:

    `heroku restart --app myapp-staging`
    `heroku maintenance:off --app myapp-staging`
    heroku restart --app myapp-staging
    heroku maintenance:off --app myapp-staging

    7. Verify everything worked the way you expect (i.e. create a new user):

    `heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging`
    `select count(*) from users;
    => 2`
    heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    select count(*) from users;
    => 2

    `heroku pg:psql HEROKU_POSTGRESQL_BROWN --app myapp-staging`
    `select count(*) from users;
    => 1`
    heroku pg:psql HEROKU_POSTGRESQL_BROWN --app myapp-staging
    select count(*) from users;
    => 1

    8. Remove the old database:

    `heroku addons:remove heroku-postgresql:ronin --app myapp-staging`
    heroku addons:remove heroku-postgresql:ronin --app myapp-staging

    Check out the following sites for details

  3. @rtwomey rtwomey revised this gist Jul 23, 2012. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions Heroku migration.mdown
    Original file line number Diff line number Diff line change
    @@ -6,47 +6,47 @@ The old database was named BROWN while the new one is CRIMSON. You can determin

    1. Add Crane database

    heroku addons:add heroku-postgresql:crane --app myapp-staging
    heroku pg:wait --app myapp-staging
    `heroku addons:add heroku-postgresql:crane --app myapp-staging`
    `heroku pg:wait --app myapp-staging`

    2. Turn on maintenance mode and capture a backup

    heroku maintenance:on --app myapp-staging
    heroku pgbackups:capture --expire --app myapp-staging
    `heroku maintenance:on --app myapp-staging`
    `heroku pgbackups:capture --expire --app myapp-staging`

    3. Figure out what the name of the new database is

    heroku config --app myapp-staging | grep POSTGRESQL
    `heroku config --app myapp-staging | grep POSTGRESQL`

    4. Restore the latest backup to the new database and verify it worked.

    heroku pgbackups:restore HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    `heroku pgbackups:restore HEROKU_POSTGRESQL_CRIMSON --app myapp-staging`
    `heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    select count(*) from users;
    => 1
    => 1`

    5. Update the app to use the new database URL

    heroku pg:promote HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    `heroku pg:promote HEROKU_POSTGRESQL_CRIMSON --app myapp-staging`

    6. Turn off maintenance mode:

    heroku restart --app myapp-staging
    heroku maintenance:off --app myapp-staging
    `heroku restart --app myapp-staging`
    `heroku maintenance:off --app myapp-staging`

    7. Verify everything worked the way you expect (i.e. create a new user):

    heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    select count(*) from users;
    => 2
    `heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging`
    `select count(*) from users;
    => 2`

    heroku pg:psql HEROKU_POSTGRESQL_BROWN --app myapp-staging
    select count(*) from users;
    => 1
    `heroku pg:psql HEROKU_POSTGRESQL_BROWN --app myapp-staging`
    `select count(*) from users;
    => 1`

    8. Remove the old database:

    heroku addons:remove heroku-postgresql:ronin --app myapp-staging
    `heroku addons:remove heroku-postgresql:ronin --app myapp-staging`

    Check out the following sites for details

  4. @rtwomey rtwomey renamed this gist Jul 23, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @rtwomey rtwomey created this gist Jul 23, 2012.
    54 changes: 54 additions & 0 deletions Heroku migration
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    Recently, I had a staging database on Heroku that was running on the Ronin database (which was originally the lowest-sized DB you could get at Heroku). Since they added two new options, Crane and Kappa, we wanted to take advantage of the cost savings. Here's how you can migrate your Ronin DB to Crane (or any other plan).

    The old database was named BROWN while the new one is CRIMSON. You can determine this by running:

    heroku pg:info --app myapp-staging

    1. Add Crane database

    heroku addons:add heroku-postgresql:crane --app myapp-staging
    heroku pg:wait --app myapp-staging

    2. Turn on maintenance mode and capture a backup

    heroku maintenance:on --app myapp-staging
    heroku pgbackups:capture --expire --app myapp-staging

    3. Figure out what the name of the new database is

    heroku config --app myapp-staging | grep POSTGRESQL

    4. Restore the latest backup to the new database and verify it worked.

    heroku pgbackups:restore HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    select count(*) from users;
    => 1

    5. Update the app to use the new database URL

    heroku pg:promote HEROKU_POSTGRESQL_CRIMSON --app myapp-staging

    6. Turn off maintenance mode:

    heroku restart --app myapp-staging
    heroku maintenance:off --app myapp-staging

    7. Verify everything worked the way you expect (i.e. create a new user):

    heroku pg:psql HEROKU_POSTGRESQL_CRIMSON --app myapp-staging
    select count(*) from users;
    => 2

    heroku pg:psql HEROKU_POSTGRESQL_BROWN --app myapp-staging
    select count(*) from users;
    => 1

    8. Remove the old database:

    heroku addons:remove heroku-postgresql:ronin --app myapp-staging

    Check out the following sites for details

    * [Getting Started with the Heroku Postgres Add-on](https://devcenter.heroku.com/articles/heroku-postgres-addon#promote_your_database_and_begin_using_it)
    * [Migrating Between Databases](https://devcenter.heroku.com/articles/migrating-data-between-plans/)