Skip to content

Commit 5fec593

Browse files
committed
Find db connection and don't preserve original db name
1 parent 280ac73 commit 5fec593

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

post-checkout

+21-30
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
#!/bin/bash
22

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
88

99
# Get the name of the branch that was checked out
1010
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
1111

1212
# Get the name of the branch you were just on
1313
PREVIOUS_BRANCH=$(git rev-parse --abbrev-ref @{-1})
1414

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-
2315
# 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
2618
PREVIOUS_BRANCH=$DB_NAME
2719
# 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
2921
fi
3022

3123
# Get list of existing branches
32-
readarray BRANCH_LIST < $DOC_ROOT/branch_list.txt
24+
readarray BRANCH_LIST < .branch_list
3325

3426
# Determine how many items are in the branch list
3527
BRANCH_LIST_LENGTH=${#BRANCH_LIST[@]}
@@ -38,7 +30,7 @@ BRANCH_LIST_LENGTH=${#BRANCH_LIST[@]}
3830
for ((NUM=0; NUM < $BRANCH_LIST_LENGTH; NUM++)); do
3931
if [ $CURRENT_BRANCH == ${BRANCH_LIST[$NUM]} ]; then
4032
# 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
4234
# Set variable so we know that this branch already existed
4335
FOUND="TRUE"
4436
fi
@@ -48,16 +40,16 @@ done
4840
if [[ $FOUND != "TRUE" ]]; then
4941

5042
# Add the current branch to the list
51-
echo $CURRENT_BRANCH >> $DOC_ROOT/branch_list.txt
43+
echo $CURRENT_BRANCH >> .branch_list
5244

5345
# 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
5547
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
5850

5951
# 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
6153

6254
fi
6355

@@ -74,7 +66,7 @@ VALID_BRANCH_LIST+=( $(git branch -a | grep "*" | awk '{print $2}') )
7466
VALID_BRANCH_LIST_LENGTH=${#VALID_BRANCH_LIST[@]}
7567

7668
# 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
7870
UPDATED_BRANCH_LIST_LENGTH=${#UPDATED_BRANCH_LIST[@]}
7971

8072
# Loop through the text file of branches
@@ -83,9 +75,8 @@ for ((NUM_L=0; NUM_L < $UPDATED_BRANCH_LIST_LENGTH; NUM_L++)); do
8375
STILL_VALID="NO"
8476
# Loop through list of currently existing branches
8577
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
8980
# The current item from the text file was found
9081
STILL_VALID="YES"
9182
fi
@@ -95,9 +86,9 @@ for ((NUM_L=0; NUM_L < $UPDATED_BRANCH_LIST_LENGTH; NUM_L++)); do
9586
mysql -u$MYSQL_USER -p$MYSQL_PW -e "drop database if exists "${UPDATED_BRANCH_LIST[$NUM_L]}
9687

9788
# 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
10192

10293
fi
10394
done

0 commit comments

Comments
 (0)