-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteTooMuchDatabaseRecord.sh
executable file
·191 lines (166 loc) · 4.66 KB
/
deleteTooMuchDatabaseRecord.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# -*- coding: UTF8 -*-
#===============================================================================
#
# USAGE: See README.md
#
# DESCRIPTION: Not very useful, just for fun. Use mongo shell instead.
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: http://github.com/JulioJu
# ORGANIZATION:
# CREATED: 12/13/2018 17:26
# REVISION: ---
#===============================================================================
trap 'kill' HUP
trap 'kill' INT
trap 'kill' QUIT
trap 'finishError "$LINENO"' ERR
trap 'finish' EXIT
# Cannot be trapped
# trap 'kill' KILL
trap 'kill' TERM
# set -euET
set -euET
# Note: `set +E' doesn't work
# set -x
close() {
# "_" is the throwaway variable
# sleep 2
read -r -t 1 -n 10000 _ || echo ""
read -r -p "Press 'ENTER' to close"
}
kill() {
set +x
1>&2 echo -e "\\n\\n\\n""${URED}""Killed by user""${NC}""\\n\\n"
exit 130
}
finishError() {
set +x
1>&2 echo "In the script, error on line: '$1'"
exit 2
# Otherwise, all script it's executed
}
finish() {
returnCode=$?
set +x
if [[ "${returnCode}" -gt 0 ]] ; then
1>&2 echo -e "\\n\\n\\n${URED}ERROR" \
"with code '${returnCode}'${NC}\\n\\n"
close
else
echo -e "\\n\\n\\n""${URED}""SUCCESS""${NC}""\\n\\n"
close
fi
echo -e "\n\n\n"
}
error() {
set +x
1>&2 echo -e "\\n\\n\\n${URED}ERROR:" "${@:2}" "${NC}\\n\\n"
exit "${@:1:1}"
}
usage() {
error 5 "${NC}Should have one or two arguments." \
"\n\t* First one: name 'mongonative' or 'mongoose' to indicate which" \
"one to delete." \
"\n\t* Seconde one (optional): '--all': delete all records " \
" (use mongo shell to drop database instead)."
}
testCommandLine(){
if [[ $# -ne 1 ]] && [[ $# -ne 2 ]]
then
usage
fi
if { [[ ! ${1} = "mongonative" ]] && [[ ! ${1} = "mongoose" ]] ; }
then
usage
fi
if [[ $# -eq 2 ]] && [[ ! ${2} = "--all" ]]
then
usage
fi
}
testsBeforeStart() {
if ! command -v nvim > /dev/null
then
error 9 "Please install \`nvim' with all plugins of JulioJu."
fi
if ! command -v js-beautify > /dev/null
then
error 8 "Please install \`js-beautify'."
fi
}
deleteTooMuchDatabaseRecord() {
entityName="${1}"
# tmpfile=$(mktemp "/tmp/${entityName}.XXXXXX").json
tmpfile="/tmp/${entityName}_$(date +%Y_%m_%d_%H_%M_%S_%N).json"
echo -e "\n\n${URED}deleteTooMuchDatabaseRecord ${entityName}${NC}" \
"\n==============\n"
set -x
curl --fail --silent "http://localhost:8080/${database}/${entityName}" \
> "${tmpfile}"
nvim --headless -c "set ts=4 sw=4 et ft=json | Neoformat | x" "${tmpfile}"
set +x
local -i numberOfRecords=0
local -i numberOfRecordsToKeep=3
local -i numberOfRecordsDeleted=0
if [[ "${deleteAll}" -eq 1 ]]
then
numberOfRecordsToKeep=0
fi
while IFS= read -r -d $'\n\r' i
do
if [[ "$i" =~ ^' '+'"_id":' ]]
then
i="${i/ \"_id\": \"/}"
i="${i/\",/}"
if [[ "${i}" == '' ]]
then
i='null'
fi
if [[ ${numberOfRecords} -ge ${numberOfRecordsToKeep} ]]
then
set -x
curl --fail --silent -X DELETE \
"http://localhost:8080/${database}/${entityName}/${i}" \
> /dev/null
numberOfRecordsDeleted=$((numberOfRecordsDeleted+1))
set +x
else
echo "http://localhost:8080/${database}/${entityName}/${i}" \
"skipped to let some entities in database."
fi
numberOfRecords=$((numberOfRecords+1))
fi
done < "${tmpfile}"
echo -e "\n\n${URED}${numberOfRecordsDeleted}" \
"${entityName} records are deleted${NC}"
rm "${tmpfile}"
}
main() {
local database="${1}"
testsBeforeStart "${@}"
local -i deleteAll=0
if [[ $# -eq 2 ]] && [[ ${2} = "--all" ]]
then
deleteAll=1
fi
deleteTooMuchDatabaseRecord nurses
deleteTooMuchDatabaseRecord patients
}
echo -e "\n\nStart of Script\n============\n"
# shellcheck disable=SC2154
export PS4='
${debian_chroot:+($debian_chroot)}'\
'\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] [\D{%T}] \$ '
# declare -g -r PS4Light="\n\n\\033[1;32m""$USER@""$HOSTNAME""\\033[0m"": "
declare -g DIR_SCRIPT
DIR_SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P)"
cd "${DIR_SCRIPT}"
declare -g -r URED="\\033[4;31m"
declare -g -r NC="\\033[0m"
testCommandLine "${@}"
main "${@}"