forked from nevesnunes/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql-if-exists-to-block.sh
executable file
·52 lines (43 loc) · 1.1 KB
/
sql-if-exists-to-block.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
#!/usr/bin/env bash
# See: https://stackoverflow.com/a/1801453
set -eu
sql_file=$1
drop_table_file=drop_table.sql
cat << EOF > "$drop_table_file"
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE %NAME%';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
EOF
drop_seq_file=drop_seq.sql
cat << EOF > "$drop_seq_file"
BEGIN
EXECUTE IMMEDIATE 'DROP SEQUENCE %NAME%';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -2289 THEN
RAISE;
END IF;
END;
EOF
trap '{ rm -f "$drop_table_file" "$drop_seq_file"; }' EXIT
function replace {
source_pattern=$1
target_file=$2
while read -r name; do
source_statement="$source_pattern $name;"
target_statement=$(sed "s/%NAME%/$name/g" < "$target_file")
awk -v r="$target_statement" \
"{gsub(/$source_statement/,r)}1" \
"$sql_file" > \
"$tmp_file"
mv "$tmp_file" "$sql_file"
done <<< "$(grep "$source_pattern" "$sql_file" | sed "s/$source_pattern \\([^;]*\\);/\\1/g")"
}
tmp_file=$(mktemp)
replace "DROP TABLE IF EXISTS" "$drop_table_file"
replace "DROP SEQUENCE IF EXISTS" "$drop_seq_file"