summaryrefslogtreecommitdiff
path: root/wordpress/wordpress-mysql
blob: 9faf3c358485b33464cc4864aa95529c1d57e888 (plain)
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
#!/bin/bash
set -e

read_config()
{
        eval "$(jq -r '. | to_entries | .[] | .key + "=" + (.value | @sh) + "\n"')"
}

our_mysqladmin()
{
	ssh -- "$db_host" mysqladmin "$@"
}

our_mysql()
{
	ssh -- "$db_host" mysql "$@"
}

have_database()
{
	our_mysql -ss <<END | (read match && [ "$match" ])
show databases where \`database\` = '${db_name//\'/\'\'}'
END
}

create_new_mysql_database()
{
	[ "$db_host" ]
	our_mysqladmin create "$db_name"
	our_mysql "$db_name" <<END
grant all privileges on \`$db_name\`.* to "$db_user" identified by "$db_password";
END
}

while [ $# -gt 0 ]
do
	case "$1" in
		--create)
			shift
			CREATE_NEW=y
			;;
		--)	shift
			break
			;;
		-*)	exit 1
			;;
		*)
			break
			;;
	esac
done

read_config < <(wordpress-config-info "$1")
shift

[ "$db_name" ]
[ "$db_host" ]
[ "$db_user" ]
[ "$db_password" ]

if [ "$CREATE_NEW" ]
then
	if ! have_database
	then
		set -x
		create_new_mysql_database
	else
		echo "$0: Warning: create_new_mysql_database: skipped" >&2
	fi
	exit
else
	our_mysql --user="$db_user" --password="$db_password" "$@" "$db_name"
fi