-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_changelog.sh
executable file
·84 lines (72 loc) · 2.27 KB
/
manage_changelog.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
#!/bin/bash
set -e
DB_NAME=${DB_NAME:-your_database_name}
CHANGELOG_SCHEMA=${CHANGELOG_SCHEMA:-changelog}
TRIGGER_FUNCTION="log_changes"
create_changelog_for_table() {
local schema_table=$1
local schema=$(echo $schema_table | cut -d '.' -f 1)
local table=$(echo $schema_table | cut -d '.' -f 2)
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "
DROP TRIGGER IF EXISTS trg_log_${schema}_${table}_changes ON ${schema_table};
CREATE TRIGGER trg_log_${schema}_${table}_changes
AFTER INSERT OR UPDATE OR DELETE ON $schema_table
FOR EACH ROW EXECUTE PROCEDURE $CHANGELOG_SCHEMA.$TRIGGER_FUNCTION();"
echo "Trigger created for table: $schema_table"
}
create_changelog_for_schema() {
local schema=$1
tables=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT tablename FROM pg_tables WHERE schemaname = '$schema';")
for table in $tables; do
create_changelog_for_table "$schema.$table"
done
echo "Triggers created for all tables in schema: $schema"
}
create_changelog_for_tables_env() {
IFS=',' read -ra ADDR <<< "$TABLES"
for table in "${ADDR[@]}"; do
create_changelog_for_table "$table"
done
}
show_help() {
echo "Usage: ./manage_changelog.sh create_changelog --tables table1 table2 ... | --schema schema1 schema2 ... | --envtables"
}
main() {
if [[ "$#" -lt 2 ]]; then
show_help
exit 1
fi
local command=$1
shift
case $command in
create_changelog)
local option=$1
shift
case $option in
--tables)
for table in "$@"; do
create_changelog_for_table "$table"
done
;;
--envtables)
create_changelog_for_tables_env
;;
--schema)
for schema in "$@"; do
create_changelog_for_schema "$schema"
done
;;
*)
echo "OPTION: $option"
show_help
exit 1
;;
esac
;;
*)
show_help
exit 1
;;
esac
}
main "$@"