summaryrefslogtreecommitdiff
path: root/mariadb/mariadb-drop-all-databases
blob: 71d83feea0cc0c7311cbd80201827cd3c778e6dc (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
#!/bin/bash
set -e
set -o pipefail

never_drop=(
	information_schema
	mysql
	performance_schema
	sys
)

never_drop()
{
	grep -vF ${never_drop[@]/#/ -e }
}

choose_drops()
{
	mariadb -ss -b <<< 'show databases' |
	never_drop
}

execute_drops()
{
	while read dbname
	do
		t=\`
		printf 'drop database %s ;\n' \
			"$t${dbname//$t/$t$t}$t"
	done |
	if [ "$REALLY_DROP_ALL_MARIADB_DATABASES" ]
	then
		mariadb -v
	else
		cat
	fi
}

warn()
{
	printf 'Warning: %s\n' "$*" >&2
}

perr()
{
	printf 'Error: %s: %s\n' "$0" "$*" >&2
}

case "$#$1" in
	1--drop-all-databases )
		shift
		REALLY_DROP_ALL_MARIADB_DATABASES=y
		;;
	0 )
		warn 'No databases will be dropped'
		warn 'To execute the SQL, supply' \
			'command-line argument: --drop-all-databases'
		;;
	* )
		perr "Unexpected argument list: ${@@Q}"
		exit 1
		;;
esac

choose_drops | execute_drops