forked from ComPlat/chemotion_ELN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-db.sh
More file actions
executable file
·59 lines (54 loc) · 2.18 KB
/
Copy pathprepare-db.sh
File metadata and controls
executable file
·59 lines (54 loc) · 2.18 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
#!/bin/bash
# prepare rails database
# assume default database configuration
DATABASE_NAME=${DATABASE_NAME:-chemotion_dev}
DATABASE_USER=${DATABASE_USER:-postgres}
DATABASE_HOST=${DATABASE_HOST:-postgres}
DATABASE_PORT=${DATABASE_PORT:-5432}
# check if yq is installed
# if yq is installed parse config/database.yml file for the actual values
# if yq is not installed, then keep the set values default values
if command -v yq &> /dev/null
then
DATABASE_NAME=$(yq -r .development.database config/database.yml)
DATABASE_USER=$(yq -r .development.username config/database.yml)
DATABASE_HOST=$(yq -r .development.host config/database.yml)
DATABASE_PORT=$(yq -r .development.port config/database.yml)
fi
echo "DATABASE_NAME: $DATABASE_NAME"
echo "DATABASE_USER: $DATABASE_USER"
echo "DATABASE_HOST: $DATABASE_HOST"
echo "DATABASE_PORT: $DATABASE_PORT"
# check if the database for the given environment configuration exists
db_exists=$( psql -h $DATABASE_HOST -U $DATABASE_USER -p $DATABASE_PORT -XtAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'" )
echo "Database exists: $db_exists"
if [ "$db_exists" = '1' ]
then
echo "==================================================="
echo "Database already exists, skipping Database creation"
echo "==================================================="
if [ "$RAKE_DB_MIGRATE" = "always" ]
then
echo "================================================"
echo "Running 'rake db:migrate'"
echo "================================================"
bundle exec rake db:migrate
fi
else
# if RAKE_DB_MIGRATE is set to always or once, run rake db:setup
if [ "$RAKE_DB_MIGRATE" = "always" ] || [ "$RAKE_DB_MIGRATE" = "once" ]
then
echo "================================================"
echo "Database does not exist"
echo "running 'rake db:create/migrate/seed'"
echo "================================================"
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake db:seed
else
echo "================================================"
echo "Database does not exist, running 'rake db:setup'"
echo "================================================"
bundle exec rake db:setup
fi
fi