gitcoinco/code_fund_ads

View on GitHub
bin/copy_production_db_to_staging

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash

# Requirements
# - Database environment variables
#   * METABASE_URL
#   * STAGING_DATABASE_URL

source .env

app='code-fund-ads-staging'
staging_config="$(heroku pg:credentials:url -a $app)"

if [[ $app != *"staging"* ]]; then
  echo 'Not configured to run in the staging environment! Aborting.';
  exit 1
fi

if [[ -z "$STAGING_DATABASE_URL" ]]; then
  echo 'STAGING_DATABASE_URL not set! Aborting.';
  exit 1
fi

if [[ $staging_config != *"$STAGING_DATABASE_URL"* ]]; then
  echo "STAGING_DATABASE_URL not found in 'heroku pg:credentials:url -a $app' Aborting."
  exit 1
fi

./bin/heroku_pg_dump_production_replica
heroku pg:reset DATABASE_URL -a $app
pg_restore --verbose --clean --no-acl --no-owner -d $STAGING_DATABASE_URL tmp/production-shallow.dump
heroku run rails db:migrate -a $app