Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Forked from jbarnette/Cakefile
Created August 7, 2011 16:58
Show Gist options
  • Select an option

  • Save jashkenas/1130537 to your computer and use it in GitHub Desktop.

Select an option

Save jashkenas/1130537 to your computer and use it in GitHub Desktop.

Revisions

  1. jashkenas revised this gist Aug 7, 2011. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions Cakefile
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    fs = require "fs"
    path = require "path"
    rimraf = require "rimraf"
    spawn = require("child_process").spawn
    url = require "url"
    fs = require "fs"
    path = require "path"
    rimraf = require "rimraf"
    {spawn} = require "child_process"
    url = require "url"

    heroku =
    config: (callback) ->
    @@ -13,7 +13,7 @@ heroku =
    raw += data

    child.on "exit", (code) ->
    console.log "heroku config exit: #{code}" unless code == 0
    console.log "heroku config exit: #{code}" if code isnt 0

    config = {}
    pattern = /^([^=]+)=(.*)$/gm
    @@ -39,15 +39,15 @@ task "db:pull", "Grab a copy of the production DB.", ->
    console.log "rimraf: #{err}" if err

    spawn("mongodump", args).on "exit", (code) ->
    console.log "mongodump exit: #{code}" unless code == 0
    console.log "mongodump exit: #{code}" if code isnt 0
    args = ["local-dev", "--eval", "db.dropDatabase()"]

    spawn("mongo", args).on "exit", (code) ->
    console.log "mongo exit: #{code}" unless code == 0
    console.log "mongo exit: #{code}" if code isnt 0
    args = ["--drop", "--db", "local-dev", path.join(dir, db)]

    spawn("mongorestore", args).on "exit", (code) ->
    console.log "mongorestore exit: #{code}" unless code == 0
    console.log "mongorestore exit: #{code}" if code isnt 0

    rimraf dir, (err) ->
    console.log "last rimraf: #{err}" if err
  2. @jbarnette jbarnette renamed this gist Aug 7, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @jbarnette jbarnette created this gist Aug 7, 2011.
    53 changes: 53 additions & 0 deletions gistfile1.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    fs = require "fs"
    path = require "path"
    rimraf = require "rimraf"
    spawn = require("child_process").spawn
    url = require "url"

    heroku =
    config: (callback) ->
    child = spawn "heroku", ["config", "--shell"]
    raw = ""

    child.stdout.on "data", (data) ->
    raw += data

    child.on "exit", (code) ->
    console.log "heroku config exit: #{code}" unless code == 0

    config = {}
    pattern = /^([^=]+)=(.*)$/gm

    config[m[1]] = m[2] while m = pattern.exec raw
    callback config

    task "db:pull", "Grab a copy of the production DB.", ->
    heroku.config (cfg) ->
    dbURL = url.parse cfg.MONGOHQ_URL
    db = dbURL.pathname.substr 1
    dir = "tmp/mongodump"
    host = dbURL.hostname
    port = dbURL.port

    [username, password] = dbURL.auth.split ":"

    args = ["--host", host, "--port", port, "--db", db,
    "--username", username, "--password", password,
    "--out", dir]

    rimraf dir, (err) ->
    console.log "rimraf: #{err}" if err

    spawn("mongodump", args).on "exit", (code) ->
    console.log "mongodump exit: #{code}" unless code == 0
    args = ["local-dev", "--eval", "db.dropDatabase()"]

    spawn("mongo", args).on "exit", (code) ->
    console.log "mongo exit: #{code}" unless code == 0
    args = ["--drop", "--db", "local-dev", path.join(dir, db)]

    spawn("mongorestore", args).on "exit", (code) ->
    console.log "mongorestore exit: #{code}" unless code == 0

    rimraf dir, (err) ->
    console.log "last rimraf: #{err}" if err