forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_pg_schema.sh
More file actions
executable file
·44 lines (35 loc) · 1.06 KB
/
check_pg_schema.sh
File metadata and controls
executable file
·44 lines (35 loc) · 1.06 KB
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
#!/usr/bin/env bash
# This script checks that SQL files do not hardcode the "public" schema;
# they should rely on search_path instead to support deployments using
# non-public schemas.
#
# Usage: check_pg_schema.sh <label> [files...]
# Example: check_pg_schema.sh "Migrations" file1.sql file2.sql
set -euo pipefail
# shellcheck source=scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
cdroot
if [[ $# -lt 1 ]]; then
error "Usage: check_pg_schema.sh <label> [files...]"
fi
label=$1
shift
# No files provided, nothing to check.
if [[ $# -eq 0 ]]; then
log "$label schema references OK (no files to check)"
exit 0
fi
files=("$@")
set +e
matches=$(grep -l 'public\.' "${files[@]}" 2>/dev/null)
set -e
if [[ -n "$matches" ]]; then
log "ERROR: $label must not hardcode the 'public' schema. Use unqualified table names instead."
echo "The following files contain 'public.' references:" >&2
while read -r file; do
echo " $file" >&2
grep -n 'public\.' "$file" | head -5 | sed 's/^/ /' >&2
done <<<"$matches"
exit 1
fi
log "$label schema references OK"