-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseed_data.sh
More file actions
executable file
·82 lines (65 loc) · 2.12 KB
/
seed_data.sh
File metadata and controls
executable file
·82 lines (65 loc) · 2.12 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/zsh
# Note: This is only after you already have created your db, ran migations, migrate and filled it with data.
# If you need to create fixtures, you can do something like
#
# $ python3 manage.py dumpdata app1.service --indent 2 > data/fixtures/services.json
#
# Where app1 is the app name.
#
# Script clears tables from the postgreql db.
# Then recreates the migrations and migrates to db.
# Then loads your data into the tables with fixtures.
#
# appName - rename to your app
# tablesToDropArray - array of tabes to drop
# fixturesArray - array of fixtures to load
# -e enables interpretation of backslash escapes
#
# Use:
# $ bash seed_data.sh
#
######################
# Data Order Example #
######################
# python manage.py loaddata users
# python manage.py loaddata tokens
# python manage.py loaddata customers
# python manage.py loaddata product_category
appName="api"
pathToFixtures="data/fixtures"
declare -a tablesToDropArray=($appName authtoken auth users tokens appusers ports vessels voyages bookings containers products)
# TODO: dump new data fixtures to reflect new db configutation
declare -a fixturesArray=(users tokens appusers ports vessels voyages bookings containers products)
echo -e '\n==============='
echo 'Dropping tables'
echo '==============='
for val in ${tablesToDropArray[@]}; do
echo -e "\n*************"
echo "Dropping $val"
echo "*************"
python manage.py migrate $val zero
done
echo -e '\n========================'
echo 'Deleteing app migrations'
echo '========================'
rm -rf $appName/migrations
echo -e '\n============================='
echo 'Running makemigrations on app'
echo '============================='
python3 manage.py makemigrations $appName
echo -e '\n==============='
echo 'Running migrate'
echo '==============='
python3 manage.py migrate
echo -e '\n================'
echo 'Loading fixtures'
echo '================'
for fixture in ${fixturesArray[@]}; do
echo -e "\n*************"
echo "Loading $fixture"
echo "*************"
python manage.py loaddata $pathToFixtures/$fixture
done
echo -e '\n================'
echo 'DONE'
echo '================'