From 38a43c6cdab24f34298502c85ad5af1625eeb5b8 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Sat, 18 Feb 2017 17:19:03 -0500 Subject: [PATCH 01/11] Capture backup of existing staging database When a PR Review environment is created, the latest backup is currently used. If no backups exist, or if the backup is old and outdated, recent data used for testing may not exist. This forcibly creates a backup right before migration -- leading to the most recent data. Another way to solve this would be to automatically add `pg:backups:schedule` on all staging environments on original app scaffolding, but that would be up to 24 hours difference. --- templates/bin_setup_review_app.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/templates/bin_setup_review_app.erb b/templates/bin_setup_review_app.erb index c6f43f7cf..ed4248a31 100644 --- a/templates/bin_setup_review_app.erb +++ b/templates/bin_setup_review_app.erb @@ -11,6 +11,9 @@ fi PARENT_APP_NAME=<%= app_name.dasherize %>-staging APP_NAME=<%= app_name.dasherize %>-staging-pr-$1 + +heroku pg:backups:capture --app $PARENT_APP_NAME + URL=`heroku pg:backups public-url --app $PARENT_APP_NAME` heroku pg:backups restore $URL DATABASE_URL --confirm $APP_NAME --app $APP_NAME From 5bc638b187a52c288625855586bba2c7e0c131c4 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 21 Feb 2017 09:45:10 -0500 Subject: [PATCH 02/11] Added #set_heroku_backup_schedule --- lib/suspenders/adapters/heroku.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/suspenders/adapters/heroku.rb b/lib/suspenders/adapters/heroku.rb index 52bcf1fa6..ff57da1df 100644 --- a/lib/suspenders/adapters/heroku.rb +++ b/lib/suspenders/adapters/heroku.rb @@ -37,6 +37,15 @@ def set_heroku_rails_secrets end end + def set_heroku_backup_schedule + %w(staging production).each do |environment| + run_toolbelt_command( + "pg:backups:schedule DATABASE_URL --at '02:00 America/Los_Angeles'", + environment, + ) + end + end + def create_review_apps_setup_script app_builder.template( "bin_setup_review_app.erb", From dd5c6ebcdc116ddd564f0dfa9853ab9c26d61d47 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 21 Feb 2017 09:47:31 -0500 Subject: [PATCH 03/11] Added backup schedule to creation of Heroku apps --- lib/suspenders/generators/app_generator.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/suspenders/generators/app_generator.rb b/lib/suspenders/generators/app_generator.rb index 7695c6c72..d0a1dd116 100644 --- a/lib/suspenders/generators/app_generator.rb +++ b/lib/suspenders/generators/app_generator.rb @@ -164,6 +164,7 @@ def create_heroku_apps build :set_heroku_remotes build :set_heroku_rails_secrets build :set_heroku_application_host + build :set_heroku_backup_schedule build :create_heroku_pipeline build :configure_automatic_deployment end From 8e24ede95176cc20a7e136e1a209cc5f80938882 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 21 Feb 2017 09:49:11 -0500 Subject: [PATCH 04/11] Delegated backup schedule to Heroku Adapter --- lib/suspenders/app_builder.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/suspenders/app_builder.rb b/lib/suspenders/app_builder.rb index 151df6c54..bf2cac578 100644 --- a/lib/suspenders/app_builder.rb +++ b/lib/suspenders/app_builder.rb @@ -12,6 +12,7 @@ class AppBuilder < Rails::AppBuilder :create_staging_heroku_app, :create_review_apps_setup_script, :set_heroku_rails_secrets, + :set_heroku_backup_schedule, :set_heroku_remotes, :set_heroku_application_host From f450fc421fe951bb50c144fded3aee7d45376d10 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 21 Feb 2017 09:54:53 -0500 Subject: [PATCH 05/11] Add test for Heroku Backup Schedule --- spec/adapters/heroku_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/adapters/heroku_spec.rb b/spec/adapters/heroku_spec.rb index acad3345c..cdb836f8d 100644 --- a/spec/adapters/heroku_spec.rb +++ b/spec/adapters/heroku_spec.rb @@ -30,6 +30,16 @@ module Adapters ) end + it "sets the heroku backup schedule" do + app_builder = double(app_name: app_name) + allow(app_builder).to receive(:run) + + Heroku.new(app_builder).set_heroku_backup_schedule + + expect(app_builder).to have_backup_schedule("staging") + expect(app_builder).to have_backup_schedule("production") + end + it "sets the application host" do app_builder = double(app_name: app_name) allow(app_builder).to receive(:run) @@ -48,6 +58,11 @@ module Adapters def app_name SuspendersTestHelpers::APP_NAME end + + def have_backup_schedule(remote_name) + have_received(:run). + with(/pg:backups:schedule DATABASE_URL --at '02:00 America\/Los_Angeles' --remote #{remote_name}/) + end def have_configured_var(remote_name, var) have_received(:run).with(/config:add #{var}=.+ --remote #{remote_name}/) From a6f738f97ccc97a20842c8fa728e8b52ae4593b6 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 21 Feb 2017 09:56:56 -0500 Subject: [PATCH 06/11] Update heroku.rb --- lib/suspenders/adapters/heroku.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/suspenders/adapters/heroku.rb b/lib/suspenders/adapters/heroku.rb index ff57da1df..482f77897 100644 --- a/lib/suspenders/adapters/heroku.rb +++ b/lib/suspenders/adapters/heroku.rb @@ -45,7 +45,7 @@ def set_heroku_backup_schedule ) end end - + def create_review_apps_setup_script app_builder.template( "bin_setup_review_app.erb", From c3906730cfe71273f67b92385423e75b5171ec8e Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 21 Feb 2017 09:57:51 -0500 Subject: [PATCH 07/11] Update heroku_spec.rb --- spec/adapters/heroku_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/adapters/heroku_spec.rb b/spec/adapters/heroku_spec.rb index cdb836f8d..5edf6fa10 100644 --- a/spec/adapters/heroku_spec.rb +++ b/spec/adapters/heroku_spec.rb @@ -39,7 +39,7 @@ module Adapters expect(app_builder).to have_backup_schedule("staging") expect(app_builder).to have_backup_schedule("production") end - + it "sets the application host" do app_builder = double(app_name: app_name) allow(app_builder).to receive(:run) From 0d957860392e81682e2b37a547bf8400059d1184 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 21 Feb 2017 10:05:10 -0500 Subject: [PATCH 08/11] Update heroku_spec.rb --- spec/adapters/heroku_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/adapters/heroku_spec.rb b/spec/adapters/heroku_spec.rb index 5edf6fa10..42da75c1a 100644 --- a/spec/adapters/heroku_spec.rb +++ b/spec/adapters/heroku_spec.rb @@ -58,7 +58,7 @@ module Adapters def app_name SuspendersTestHelpers::APP_NAME end - + def have_backup_schedule(remote_name) have_received(:run). with(/pg:backups:schedule DATABASE_URL --at '02:00 America\/Los_Angeles' --remote #{remote_name}/) From 91157552dc89d6fcce70d23212edce1f5f7b6eb1 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Wed, 22 Feb 2017 16:49:13 -0500 Subject: [PATCH 09/11] Update heroku.rb --- lib/suspenders/adapters/heroku.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/suspenders/adapters/heroku.rb b/lib/suspenders/adapters/heroku.rb index 482f77897..cfbad9cca 100644 --- a/lib/suspenders/adapters/heroku.rb +++ b/lib/suspenders/adapters/heroku.rb @@ -40,7 +40,7 @@ def set_heroku_rails_secrets def set_heroku_backup_schedule %w(staging production).each do |environment| run_toolbelt_command( - "pg:backups:schedule DATABASE_URL --at '02:00 America/Los_Angeles'", + "pg:backups:schedule DATABASE_URL --at '10:00 UTC'", environment, ) end From 930a31b01670fdcccec54cb9b0505e1fd2425fde Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Wed, 22 Feb 2017 16:49:31 -0500 Subject: [PATCH 10/11] Update heroku_spec.rb --- spec/adapters/heroku_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/adapters/heroku_spec.rb b/spec/adapters/heroku_spec.rb index 42da75c1a..9f5323f4f 100644 --- a/spec/adapters/heroku_spec.rb +++ b/spec/adapters/heroku_spec.rb @@ -61,7 +61,7 @@ def app_name def have_backup_schedule(remote_name) have_received(:run). - with(/pg:backups:schedule DATABASE_URL --at '02:00 America\/Los_Angeles' --remote #{remote_name}/) + with(/pg:backups:schedule DATABASE_URL --at '10:00 UTC' --remote #{remote_name}/) end def have_configured_var(remote_name, var) From 2883dd978f4580d010bc9ae79478c89de012fc52 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Fri, 24 Mar 2017 14:25:37 -0400 Subject: [PATCH 11/11] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f10450025..fa1914591 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ This: * Configures staging with `RACK_ENV` environment variable set to `staging` * Creates a [Heroku Pipeline] for review apps +* Schedules automated backups for 10AM UTC for both `staging` and `production` [Heroku Pipeline]: https://devcenter.heroku.com/articles/pipelines