forked from acrewdson/cfgov-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh-data.sh
executable file
·56 lines (44 loc) · 1.54 KB
/
refresh-data.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# ==========================================================================
# Import data from a gzipped dump. Provide the filename as the first arg.
# NOTE: Run this script while in the project root directory.
# It will not run correctly when run from another directory.
# ==========================================================================
set -e
refresh_dump_name=$1
USAGE=$(cat << 'EOF'
Please download a recent database dump before running this script:
./refresh-data.sh production_django.sql
Or you can define the location of a dump and this script will
download it for you:
export CFGOV_PROD_DB_LOCATION=https://example.com/production_django.sql.gz
./refresh-data.sh
EOF
)
download_data() {
echo 'Downloading fresh production Django database dump...'
prod_archive="$refresh_dump_name"
curl -o "$prod_archive" "$CFGOV_PROD_DB_LOCATION"
}
refresh_data() {
echo 'Importing refresh db'
gunzip < "$refresh_dump_name" | cfgov/manage.py dbshell > /dev/null
echo 'Running any necessary migrations'
./cfgov/manage.py migrate --noinput --fake-initial
echo 'Setting up initial data'
./cfgov/manage.py runscript initial_data
}
if [[ -z "$refresh_dump_name" ]]; then
if [[ -z "$CFGOV_PROD_DB_LOCATION" ]]; then
printf "%s\n\n" "$USAGE"
exit 1
fi
refresh_dump_name='production_django.sql.gz'
download_data
else
if [[ $refresh_dump_name != *.sql.gz ]]; then
echo "Input dump '$refresh_dump_name' expected to end with .sql.gz."
exit 2
fi
fi
refresh_data