From 4653baeb82d04d3db589f8b71b93b30d4ecad79a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 18 Oct 2021 15:45:24 +0200
Subject: [PATCH] staging db task

---
 lib/tasks/app.rake | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/lib/tasks/app.rake b/lib/tasks/app.rake
index 0dbd50b37..926c21252 100644
--- a/lib/tasks/app.rake
+++ b/lib/tasks/app.rake
@@ -20,6 +20,26 @@ namespace :app do
   namespace :db do
     desc 'Get database from Scalingo'
     task :staging do
+      Bundler.with_unbundled_env do
+        # Get a new backup archive from Scalingo
+        # PG Addon ID from `scalingo addons` CLI command.
+        sh "scalingo --app #{ENV['OSUNY_STAGING_APP_NAME']} backups-create --addon #{ENV['OSUNY_STAGING_PG_ADDON_ID']}"
+        sh "scalingo --app #{ENV['OSUNY_STAGING_APP_NAME']} backups-download --addon #{ENV['OSUNY_STAGING_PG_ADDON_ID']} --output db/scalingo-dump.tar.gz"
+
+        sh 'rm -f db/latest.dump' # Remove an old backup file if it exists
+        sh 'tar zxvf db/scalingo-dump.tar.gz -C db/' # Extract the new backup archive
+        sh 'rm db/scalingo-dump.tar.gz' # Remove the backup archive
+        sh 'mv db/*.pgsql db/latest.dump' # Rename the backup file
+        sh 'DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rails db:drop'
+        sh 'bundle exec rails db:create'
+        begin
+          sh 'pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d osuny_development db/latest.dump'
+        rescue
+          'There were some warnings or errors while restoring'
+        end
+        sh 'rails db:migrate'
+        sh 'rails db:seed'
+      end
     end
   end
 end
-- 
GitLab