forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·58 lines (50 loc) · 1.39 KB
/
test.sh
File metadata and controls
executable file
·58 lines (50 loc) · 1.39 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
PGUSER=$1
PGPASSWORD=$2
PGNAME=$3
PGHOST=$4
PGPORT=$5
PGUSER=${PGUSER:-cds}
PGPASSWORD=${PGPASSWORD:-cds}
PGNAME=${PGNAME:-cds}
PGHOST=${PGHOST:-localhost}
PGPORT=${PGPORT:-5432}
export PGUSER PGPASSWORD PGHOST PGPORT
cat << EOF > missing_pk.sql
with cte as (
select n.nspname as schema,
c.relname as "table",
array_agg(i.indisprimary) as indexes
from pg_class c
left join pg_index i on c.oid = i.indrelid
join pg_namespace n on c.relnamespace = n.oid
where c.relkind = 'r'
and n.nspname not in ('pg_catalog', 'information_schema')
group by "table", schema
)
select quote_ident(schema) || '.' || quote_ident("table") as "table"
from cte
where not indexes @> ARRAY[true];
EOF
NC='\033[0m' # No Color
RED='\033[0;31m'
GREEN='\033[0;32m'
return_code=0
echo "Checking missing primary key"
echo " Hostname: $PGHOST:$PGPORT"
echo " User: $PGUSER"
echo " Password: **********"
echo " Database: $PGNAME"
echo "======================================================================"
psql -t -f missing_pk.sql -o missing_pk.log -h $PGHOST -d $PGNAME -U $PGUSER
if grep -qvE '^\s*$' missing_pk.log
then
echo "SM0005: Missing primary keys"
cat missing_pk.log
return_code=1
else
echo -e "${GREEN}OK${NC}"
fi
echo "======================================================================"
rm -f missing_pk.sql
exit $return_code