Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1539 from uhoh-itsmaciek/fix-unschedule-issues
Browse files Browse the repository at this point in the history
Fix unscheduling backups for some config var configurations
  • Loading branch information
Jeff Dickey committed May 15, 2015
2 parents e72e3d6 + c73000a commit d050106
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/heroku/command/pg_backups.rb
Expand Up @@ -44,7 +44,7 @@ def copy
# delete BACKUP_ID # delete an existing backup
# schedule DATABASE # schedule nightly backups for given database
# --at '<hour>:00 <timezone>' # at a specific (24h clock) hour in the given timezone
# unschedule DATABASE # stop nightly backup for database
# unschedule SCHEDULE # stop nightly backups on this schedule
# schedules # list backup schedule
def backups
if args.count == 0
Expand Down Expand Up @@ -523,12 +523,24 @@ def unschedule_backups
db = shift_argument
validate_arguments!

if db.nil?
# try to provide a more informative error message, but rescue to
# a generic error message in case things go poorly
begin
attachment = arbitrary_app_db
schedules = hpg_client(attachment).schedules
schedule_names = schedules.map { |s| s[:name] }.join(", ")
abort("Must specify schedule to cancel: existing schedules are #{schedule_names}")
rescue StandardError
abort("Must specify schedule to cancel. Run `heroku help pg:backups` for usage information.")
end
end

attachment = generate_resolver.resolve(db, "DATABASE_URL")

schedule = hpg_client(attachment).schedules.find do |s|
# attachment.name is HEROKU_POSTGRESQL_COLOR
# s[:name] is HEROKU_POSTGRESQL_COLOR_URL
s[:name] =~ /#{attachment.name}/ || attachment.name =~ /#{s[:name]}/
s[:name] =~ /#{db}/i
end

if schedule.nil?
Expand Down
34 changes: 34 additions & 0 deletions spec/heroku/command/pg_backups_spec.rb
Expand Up @@ -122,6 +122,40 @@ module Heroku::Command
end
end

describe "heroku pg:backups unschedule" do
let(:schedules) do
[ { name: 'HEROKU_POSTGRESQL_GREEN_URL',
uuid: 'ffffffff-ffff-ffff-ffff-ffffffffffff' },
{ name: 'DATABASE_URL',
uuid: 'ffffffff-ffff-ffff-ffff-fffffffffffe' } ]
end

before do
stub_pg.schedules.returns(schedules)
allow_any_instance_of(Heroku::Helpers::HerokuPostgresql::Resolver)
.to receive(:app_attachments).and_return(example_attachments)
end

it "unschedules the specified backup" do
stub_pg.unschedule
stderr, stdout = execute("pg:backups unschedule green --confirm example")
expect(stderr).to be_empty
expect(stdout).to match(/Stopped automatic daily backups for/)
end

it "complains when called without an argument" do
stderr, stdout = execute("pg:backups unschedule --confirm example")
expect(stderr).to match(/Must specify schedule to cancel/)
expect(stdout).to be_empty
end

it "indicates when no matching backup can be unscheduled" do
stderr, stdout = execute("pg:backups unschedule red --confirm example")
expect(stderr).to be_empty
expect(stdout).to match(/No automatic daily backups for/)
end
end

describe "heroku pg:backups" do
let(:logged_at) { Time.now }
let(:started_at) { Time.now }
Expand Down

0 comments on commit d050106

Please sign in to comment.