-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-magento-db.sh
executable file
·81 lines (65 loc) · 1.51 KB
/
create-magento-db.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
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
#!/usr/bin/env bash
USER=mageuser
PASS=blahgento
DATABASES='mage_one mage_two'
if uname -a | grep -q Darwin; then
echo "Don't run $0 on macOS"
exit 1
fi
# ~=~=~=~ HELPER FUNCTIONS ~=~=~=~
strict_query() {
sudo mysql -uroot -e "$1" || {
echo "Aborting: problem with query '$1'"
exit 1
}
}
query() {
sudo mysql -uroot -e "$1" || {
echo "Problem with query '$1'"
return 1
}
}
silent_query() {
sudo mysql -uroot -e "$1" &> /dev/null
}
query_match() {
local query
query=$1
shift
for pattern in "$@"; do
sudo mysql -uroot -e "$query" | grep -q "$pattern" || {
return 1
}
done
}
query_has_result() {
sudo mysql -uroot -e "$1" | wc -l | { read line_count; (( line_count > 0 )); } || {
return 1
}
}
# ~=~=~=~ PROGRAM ~=~=~=~
query_match 'show databases' 'information_schema' 'mysql' || {
echo "Problem connecting to MySQL: aborting"
exit 1
}
[[ $1 == drop-first || $1 == dropfirst ]] && {
for db in $DATABASES; do
silent_query "drop database $db"
done
}
# create database
for db in $DATABASES; do
query_match 'show databases' "$db" || strict_query "create database $db"
done
# create user
query_has_result "select User from mysql.user where User='$USER' and Host='localhost'" || {
strict_query "create user $USER@localhost IDENTIFIED BY '$PASS'"
}
# grant access
for db in $DATABASES; do
strict_query "GRANT ALL ON $db.* TO $USER@localhost"
done
for db in $DATABASES; do
query "show databases like '%$db%'"
query "select count(TABLE_NAME) from information_schema.tables where TABLE_SCHEMA='$db'"
done