diff --git a/Gemfile b/Gemfile index f9c4c0d..18dfb4e 100644 --- a/Gemfile +++ b/Gemfile @@ -2,5 +2,4 @@ source 'https://rubygems.org' gemspec -gem 'rake' -gem 'rspec' +gem "rspec" diff --git a/README.md b/README.md index 102a2a4..8ce8924 100644 --- a/README.md +++ b/README.md @@ -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 ------------- diff --git a/lib/parity.rb b/lib/parity.rb index 2d517f5..105f69a 100644 --- a/lib/parity.rb +++ b/lib/parity.rb @@ -3,6 +3,7 @@ require "parity/version" require "parity/environment" require "parity/usage" +require "git" require "open3" require "pathname" require "uri" diff --git a/lib/parity/environment.rb b/lib/parity/environment.rb index c7a7a9b..75b4068 100644 --- a/lib/parity/environment.rb +++ b/lib/parity/environment.rb @@ -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? diff --git a/parity.gemspec b/parity.gemspec index 2caab39..75c3fae 100644 --- a/parity.gemspec +++ b/parity.gemspec @@ -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 diff --git a/spec/parity/environment_spec.rb b/spec/parity/environment_spec.rb index 6c425d6..7d1ec97 100644 --- a/spec/parity/environment_spec.rb +++ b/spec/parity/environment_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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: "git@heroku.com:#{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