Skip to content

Commit

Permalink
Parse git remote name to determine Heroku app
Browse files Browse the repository at this point in the history
Several reports have matched #72, which was affecting the `restore`
command for some users. The cause turned out to be local folder names
not matching the name of the Heroku app. Now that Heroku tracks Git
remotes, we can take advantage of the Git remote to correctly identify
the app name even the local folder name doesn't match.

While this requirement has been present for the entirety of Parity's
existence, it stopped being a problem for most commands once Heroku
introduced remotes. I inadvertently reintroduced this requirement in
ad2c21a when we started automatically
bypassing confirmation arguments for restores to non-production
environments.

I've moved the `git` and `rake` dependencies to the gemspec, as they do
not appear to be installed by default if they are listed in the Gemfile.
Fix #72.
  • Loading branch information
geoffharcourt committed Jan 25, 2016
1 parent 95ac59c commit 10574fe
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ source 'https://rubygems.org'

gemspec

gem 'rake'
gem 'rspec'
gem "rspec"
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ Parity expects:
* A `production` remote pointing to the production Heroku app.
* There is a `config/database.yml` file that can be parsed as YAML for
`['development']['database']`.
* The Heroku apps are named like `app-staging` and `app-production`
where `app` is equal to `basename $PWD`.

Customization
-------------
Expand Down
1 change: 1 addition & 0 deletions lib/parity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "parity/version"
require "parity/environment"
require "parity/usage"
require "git"
require "open3"
require "pathname"
require "uri"
8 changes: 4 additions & 4 deletions lib/parity/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ def raw_redis_url
)[0].strip
end

def heroku_app_name
[basename, environment].join('-')
def git_remote
Git.init.remote(environment).url
end

def basename
Dir.pwd.split("/").last
def heroku_app_name
git_remote.split(":").last.split(".").first
end

def run_migrations?
Expand Down
3 changes: 3 additions & 0 deletions parity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]
spec.summary = "Shell commands for development, staging, and production parity."
spec.version = Parity::VERSION

spec.add_dependency "git", "~> 1.2"
spec.add_dependency "rake", "~> 10.4"
end
47 changes: 41 additions & 6 deletions spec/parity/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,26 @@
expect(Kernel).to have_received(:system).with(heroku_backup)
end

it "correctly connects to the Heroku app when the $PWD's name does not match the app's name" do
backup = stub_parity_backup
stub_git_remote(base_name: "parity-integration", environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore", "production"]).run

expect(Parity::Backup).
to have_received(:new).
with(
from: "production",
to: "staging",
additional_args: "--confirm parity-integration-staging",
)
expect(backup).to have_received(:restore)
end

it "restores backups from production to staging" do
backup = double("backup", restore: nil)
backup = stub_parity_backup
stub_git_remote(environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore", "production"]).run
Expand All @@ -48,7 +66,8 @@
end

it "restores using restore-from" do
backup = double("backup", restore: nil)
backup = stub_parity_backup
stub_git_remote(environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore-from", "production"]).run
Expand All @@ -64,7 +83,8 @@
end

it "passes the confirm argument when restoring to a non-prod environment" do
backup = double("backup", restore: nil)
backup = stub_parity_backup
stub_git_remote(environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore", "production"]).run
Expand All @@ -79,7 +99,7 @@
end

it "restores backups from production to development" do
backup = double("backup", restore: nil)
backup = stub_parity_backup
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("development", ["restore", "production"]).run
Expand All @@ -90,7 +110,7 @@
end

it "restores backups from staging to development" do
backup = double("backup", restore: nil)
backup = stub_parity_backup
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("development", ["restore", "staging"]).run
Expand All @@ -101,7 +121,8 @@
end

it "does not allow restoring backups into production" do
backup = double("backup", restore: nil)
backup = stub_parity_backup
stub_git_remote
allow(Parity::Backup).to receive(:new).and_return(backup)
allow($stdout).to receive(:puts)

Expand Down Expand Up @@ -323,4 +344,18 @@ def stub_migration_path_check(result)

path_stub
end

def stub_git_remote(base_name: "parity", environment: "staging")
git_remote = instance_double(
"Git::Remote",
url: "[email protected]:#{base_name}-#{environment}.git",
)
git = instance_double("Git::Base")
allow(git).to receive(:remote).with("staging").and_return(git_remote)
allow(Git).to receive(:init).and_return(git)
end

def stub_parity_backup
instance_double("Parity::Backup", restore: nil)
end
end

0 comments on commit 10574fe

Please sign in to comment.