1
1
#! /bin/bash
2
2
3
- # Set environment specific variables
4
- MYSQL_USER=YOUR_USER
5
- MYSQL_PW=YOUR_PASSWORD
6
- DOC_ROOT=PATH/TO/YOUR/APPLICATION
7
- DB_NAME=YOUR_DATABASE_NAME
3
+ # Variables
4
+ MYSQL_USER=$( grep -irs " 'username' => ' " sites/default/settings.php | awk ' {print $3} ' | tail -1 | tr -d " ', " )
5
+ MYSQL_PW=$( grep -irs " 'password' => ' " sites/default/settings.php | awk ' {print $3} ' | tail -1 | tr -d " ', " )
6
+ DB_NAME= $( grep -irs " 'database' => ' " sites/default/settings.php | awk ' {print $3} ' | tail -1 | tr -d " ', " )
7
+ # CURRENT_DIR=$(echo "${PWD##*/}") # Use as a DB prefix
8
8
9
9
# Get the name of the branch that was checked out
10
10
CURRENT_BRANCH=$( git rev-parse --abbrev-ref HEAD)
11
11
12
12
# Get the name of the branch you were just on
13
13
PREVIOUS_BRANCH=$( git rev-parse --abbrev-ref @{-1})
14
14
15
- # Preserve the original DB name instead of using "master"
16
- if [ $CURRENT_BRANCH == " master" ]; then
17
- CURRENT_BRANCH=$DB_NAME
18
- fi
19
- if [ $PREVIOUS_BRANCH == " master" ]; then
20
- PREVIOUS_BRANCH=$DB_NAME
21
- fi
22
-
23
15
# Check if file that stores branches exists (Created first time script is run)
24
- if [ ! -f $DOC_ROOT /branch_list.txt ]; then
25
- # Script hasn't been run, so ensure database is cloned from exising database, aka the original database
16
+ if [ ! -f .branch_list ]; then
17
+ # Script hasn't been run, so ensure database is cloned from an exising database, aka the original database
26
18
PREVIOUS_BRANCH=$DB_NAME
27
19
# Stop script from trying to create original DB that already exists
28
- echo $PREVIOUS_BRANCH > $DOC_ROOT /branch_list.txt
20
+ echo $PREVIOUS_BRANCH > .branch_list
29
21
fi
30
22
31
23
# Get list of existing branches
32
- readarray BRANCH_LIST < $DOC_ROOT /branch_list.txt
24
+ readarray BRANCH_LIST < .branch_list
33
25
34
26
# Determine how many items are in the branch list
35
27
BRANCH_LIST_LENGTH=${# BRANCH_LIST[@]}
@@ -38,7 +30,7 @@ BRANCH_LIST_LENGTH=${#BRANCH_LIST[@]}
38
30
for (( NUM= 0 ; NUM < $BRANCH_LIST_LENGTH ; NUM++ )) ; do
39
31
if [ $CURRENT_BRANCH == ${BRANCH_LIST[$NUM]} ]; then
40
32
# Change Drupal's database connection
41
- sudo sed -i " s#'database' => .*#'database' => '$CURRENT_BRANCH ',#" $DOC_ROOT / sites/default/settings.php
33
+ sudo sed -i " s#'database' => .*#'database' => '$CURRENT_BRANCH ',#" sites/default/settings.php
42
34
# Set variable so we know that this branch already existed
43
35
FOUND=" TRUE"
44
36
fi
48
40
if [[ $FOUND != " TRUE" ]]; then
49
41
50
42
# Add the current branch to the list
51
- echo $CURRENT_BRANCH >> $DOC_ROOT /branch_list.txt
43
+ echo $CURRENT_BRANCH >> .branch_list
52
44
53
45
# Create a new database for the current branch
54
- mysqldump -u$MYSQL_USER -p" $MYSQL_PW " $PREVIOUS_BRANCH > $DOC_ROOT / branch_dump.sql # Clone db from last branch
46
+ mysqldump -u$MYSQL_USER -p" $MYSQL_PW " $PREVIOUS_BRANCH > . branch_dump.sql # Clone db from last branch
55
47
mysqladmin -u$MYSQL_USER -p" $MYSQL_PW " create $CURRENT_BRANCH # Create a new db for the new branch
56
- mysql -u$MYSQL_USER -p" $MYSQL_PW " $CURRENT_BRANCH < $DOC_ROOT / branch_dump.sql # Pipe the db clone into the new db
57
- rm $DOC_ROOT / branch_dump.sql # Remove the exported database file
48
+ mysql -u$MYSQL_USER -p" $MYSQL_PW " $CURRENT_BRANCH < . branch_dump.sql # Pipe the db clone into the new db
49
+ rm . branch_dump.sql # Remove the exported database file
58
50
59
51
# Change Drupal's database connection
60
- sudo sed -i " s#'database' => .*#'database' => '$CURRENT_BRANCH ',#" $DOC_ROOT / sites/default/settings.php
52
+ sudo sed -i " s#'database' => .*#'database' => '$CURRENT_BRANCH ',#" sites/default/settings.php
61
53
62
54
fi
63
55
@@ -74,7 +66,7 @@ VALID_BRANCH_LIST+=( $(git branch -a | grep "*" | awk '{print $2}') )
74
66
VALID_BRANCH_LIST_LENGTH=${# VALID_BRANCH_LIST[@]}
75
67
76
68
# Account for any new items that were just added to text file of branches
77
- readarray UPDATED_BRANCH_LIST < $DOC_ROOT /branch_list.txt
69
+ readarray UPDATED_BRANCH_LIST < .branch_list
78
70
UPDATED_BRANCH_LIST_LENGTH=${# UPDATED_BRANCH_LIST[@]}
79
71
80
72
# Loop through the text file of branches
@@ -83,9 +75,8 @@ for ((NUM_L=0; NUM_L < $UPDATED_BRANCH_LIST_LENGTH; NUM_L++)); do
83
75
STILL_VALID=" NO"
84
76
# Loop through list of currently existing branches
85
77
for (( NUM_VL= 0 ; NUM_VL < $VALID_BRANCH_LIST_LENGTH ; NUM_VL++ )) ; do
86
- # Check if the item from the file matches the current branches or original DB
87
- if [ ${UPDATED_BRANCH_LIST[$NUM_L]} == ${VALID_BRANCH_LIST[$NUM_VL]} ] ||
88
- [ ${UPDATED_BRANCH_LIST[$NUM_L]} == $DB_NAME ]; then
78
+ # Check if the item from the file matches the current branches
79
+ if [ ${UPDATED_BRANCH_LIST[$NUM_L]} == ${VALID_BRANCH_LIST[$NUM_VL]} ]; then
89
80
# The current item from the text file was found
90
81
STILL_VALID=" YES"
91
82
fi
@@ -95,9 +86,9 @@ for ((NUM_L=0; NUM_L < $UPDATED_BRANCH_LIST_LENGTH; NUM_L++)); do
95
86
mysql -u$MYSQL_USER -p$MYSQL_PW -e " drop database if exists " ${UPDATED_BRANCH_LIST[$NUM_L]}
96
87
97
88
# Remove the branch from our text file of branches
98
- grep -xv ${UPDATED_BRANCH_LIST[$NUM_L]} $DOC_ROOT /branch_list.txt > $DOC_ROOT /tmp_branch_list.txt
99
- cat $DOC_ROOT /tmp_branch_list.txt > $DOC_ROOT /branch_list.txt
100
- rm $DOC_ROOT /tmp_branch_list.txt
89
+ grep -xv ${UPDATED_BRANCH_LIST[$NUM_L]} .branch_list > .tmp_branch_list
90
+ cat .tmp_branch_list > .branch_list
91
+ rm .tmp_branch_list
101
92
102
93
fi
103
94
done
0 commit comments