Permalink
Browse files

Add Heroku scripts

staging:

    staging console
    staging migrate
    staging process
    staging open
    watch staging ps
    staging tail

production:

    production backup
    production console
    production migrate
    production open
    watch production ps
    production tail

load-backup-into:

    load-backup-into staging
    load-backup-into development

Notes on the last command:

* Copy latest production database backup into development database using
  preferred pgbackups + pg_restore method recommended by Heroku.
* Do not assume local development database name.
* Get local database name from config/database.yml.
* This did not previously exist in dotfiles.

Supporting change for these commands:

* Add ~/.bin to the PATH.

#76
  • Loading branch information...
1 parent df3dd51 commit 86494030441e88ef9c2e2ceaa00a4da82023e445 @croaky croaky committed May 7, 2012
Showing with 126 additions and 16 deletions.
  1. +0 −16 aliases
  2. +24 −0 bin/deployment_environment.rb
  3. +48 −0 bin/load-backup-into
  4. +26 −0 bin/production
  5. +26 −0 bin/staging
  6. +2 −0 zlogin
View
16 aliases
@@ -28,19 +28,3 @@ alias giv="gem install -v"
# Rails
alias migrate="rake db:migrate && rake db:rollback && rake db:migrate && rake db:test:prepare"
-
-# Heroku staging
-alias staging-console='heroku run console --remote staging'
-alias staging-process='watch heroku ps --remote staging'
-alias staging-releases='heroku releases --remote staging'
-alias staging-tail='heroku logs --tail --remote staging'
-
-# Heroku production
-alias production-console='heroku run console --remote production'
-alias production-process='watch heroku ps --remote production'
-alias production-releases='heroku releases --remote production'
-alias production-tail='heroku logs --tail --remote production'
-
-# Heroku databases
-alias db-backup-production='heroku pgbackups:capture --remote production'
-alias db-copy-production-to-staging='heroku pgbackups:restore DATABASE `heroku pgbackups:url --remote production` --remote staging --confirm `basename $PWD`-staging'
@@ -0,0 +1,24 @@
+class DeploymentEnvironment
+ def initialize(subcommands)
+ @subcommands = subcommands
+ @subcommand = @subcommands.first
+ end
+
+ def run
+ case @subcommand
+ when 'backup'
+ system "heroku pgbackups:capture --expire --remote #{@environment}"
+ when 'console'
+ system "heroku run console --remote #{@environment}"
+ when 'migrate'
+ system %{
+ heroku run rake db:migrate --remote #{@environment} &&
+ heroku restart --remote #{@environment}
+ }
+ when 'tail'
+ system "heroku logs --tail --remote #{@environment}"
+ else
+ system "heroku #{@subcommands.join(' ')} --remote #{@environment}"
+ end
+ end
+end
View
@@ -0,0 +1,48 @@
+#!/usr/bin/ruby
+
+# Load latest production database backup into database for target environment.
+#
+# load-backup-into development
+# load-backup-into staging
+
+require 'yaml'
+
+class Backup
+ def initialize(subcommands)
+ @target_environment = subcommands.first
+ end
+
+ def load
+ if into_development?
+ system %{
+ curl -s `#{db_backup_url}` | \
+ pg_restore --verbose --clean --no-acl --no-owner -d #{db_development}
+ }
+ elsif into_staging?
+ system %{
+ heroku pgbackups:restore DATABASE `#{db_backup_url}` --remote staging
+ }
+ end
@hgmnz

hgmnz Jan 13, 2013

Check out heroku pgbackups:transfer that we've put on the heroku-pg-extras plugin for direct db-to-db transfers

@croaky

croaky Jan 13, 2013

Contributor

A heroku plugin for awesome pg:* commands that are also great and fun and super.

Compelling!

+ end
+
+ private
+
+ def into_development?
+ @target_environment == 'development'
+ end
+
+ def into_staging?
+ @target_environment == 'staging'
+ end
+
+ def db_backup_url
+ 'heroku pgbackups:url --remote production'
+ end
+
+ def db_development
+ YAML.load(IO.read('config/database.yml'))['development']['database']
+ end
+end
+
+backup = Backup.new(ARGV)
+backup.load
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+#
+# Interact with the production environment on Heroku.
+#
+# production backup
+# production console
+# production migrate
+# production tail
+#
+# The script also acts as a pass-through, so you can do anything with it that
+# you can do with `heroku ______ -r production`:
+#
+# production open
+# watch production ps
+
+require File.join(File.dirname(__FILE__), 'deployment_environment')
+
+class Production < DeploymentEnvironment
+ def initialize(subcommands)
+ @environment = 'production'
+ super(subcommands)
+ end
+end
+
+production = Production.new(ARGV)
+production.run
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+#
+# Interact with the staging environment on Heroku.
+#
+# staging backup
+# staging console
+# staging migrate
+# staging tail
+#
+# The script also acts as a pass-through, so you can do anything with it that
+# you can do with `heroku ______ -r staging`:
+#
+# staging open
+# watch staging ps
+
+require File.join(File.dirname(__FILE__), 'deployment_environment')
+
+class Staging < DeploymentEnvironment
+ def initialize(subcommands)
+ @environment = 'staging'
+ super(subcommands)
+ end
+end
+
+staging = Staging.new(ARGV)
+staging.run
View
2 zlogin
@@ -19,3 +19,5 @@ setopt prompt_subst
# prompt
export PS1='$(git_prompt_info)[${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[blue]%}%~%{$reset_color%}] '
+# load thoughtbot/dotfiles scripts
+export PATH="$HOME/.bin:$PATH"

0 comments on commit 8649403

Please sign in to comment.