Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A script to copy prod db to dev, install it, makemigrations, and migrate #1148

Open
jeffcjohnson opened this issue Apr 27, 2017 · 4 comments

Comments

@jeffcjohnson
Copy link

Hi, I often want to test my changes against the latest production database and make sure the migrations are correct. I wrote a script that prints the two simple commands to get the latest database, and then asks if you'd like to restore it, make migrations, and migrate. It has been so helpful that I now use it on all my projects. I thought it might make a good addition to cookiecutter-django. If there is interest I could make any changes suggested here and add a pull request. Would it go in the root folder or bin?

#!/usr/bin/env bash

printf "\nTo get a new backup from Heroku:\nheroku pg:backups capture\nheroku pg:backups download\n\n"

dump_file=$(ls -1r latest.dump* | head -n1)
database=$(basename $(pwd))

read -p "Restoring ${dump_file} to database ${database}.  Continue? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi

dropdb ${database} && \
createdb ${database} && \
pg_restore --no-acl --no-owner -d ${database} ${dump_file} && \
./manage.py makemigrations && \
./manage.py migrate && \
printf "Success!\n"
@webyneter
Copy link
Collaborator

@jeffcjohnson , thanks, great of you to share!

I'm also planning to share my convenience bash scripts too some time later.

@webyneter
Copy link
Collaborator

webyneter commented Apr 27, 2017

@jeffcjohnson , as for the script location, we keep other postgres service-related shell scripts in ./compose/postgres/ for now, and copy them to the image's /usr/local/bin/.

@jeffcjohnson
Copy link
Author

@webyneter is that for Docker installs? I'm currently just using virtual environments and local Postgres so haven't gone down the Docker path yet.

@webyneter
Copy link
Collaborator

@jeffcjohnson, yes, this is for Docker setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants