88{% set dbuser = settings.get(' dbuser' , defaults.dbuser) -% }
99{% set dbpassword = settings.get(' dbpassword' , defaults.dbpassword) -% }
1010
11- {% set dbroot_user = settings.get(' dbroot_user' ) -% }
12- {% set dbroot_pass = settings.get(' dbroot_pass' ) -% }
13-
1411{% set sql_file = settings.get(' sql_file' , defaults.sql_file) -% }
1512
16- # Connection args required only if dbroot_user and dbroot_pass defined.
17- {% set connection_args = {} -% }
18- {% if dbroot_user and dbroot_pass -% }
19- {% set connection_args = {' runas' : ' nobody' , ' host' : dbhost, ' user' : dbroot_user, ' password' : dbroot_pass} -% }
20- {% endif -% }
21-
22- # Check is there any tables in database.
23- # salt.postgres.psql_query return empty result if there is no tables or 'False' on any error i.e. failed auth.
24- {% set list_tables = " SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' LIMIT 1;" % }
25- {% set is_db_empty = True -% }
26- {% if salt.postgres.psql_query(query = list_tables, maintenance_db = dbname, ** connection_args) -% }
27- {% set is_db_empty = False -% }
28- {% endif -% }
29-
3013include:
3114 - zabbix.pgsql.pkgs
3215
16+ # Check is there any tables in database.
17+ # returns changed if there are zero tables in the db
3318check_db_pgsql:
34- test.configurable_test_state:
35- - name: Is there any tables in ' {{ dbname }} ' database?
36- - changes: {{ is_db_empty }}
37- - result: True
38- - comment: If changes is ' True' data import required.
19+ cmd.run:
20+ - name: " [[ $(psql -X -A -t -c \" SELECT count(tablename) FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';\" || echo \" -1\" ) -eq \" 0\" ]] && echo \" changed=yes comment='DB needs schema import.'\" || echo \" changed=no comment='No DB import needed or possible.'\" "
21+ - runas: {{ zabbix.user }}
22+ - stateful: True
23+ - env:
24+ - PGUSER : {{ dbuser }}
25+ - PGPASSWORD : {{ dbpassword }}
26+ - PGDATABASE : {{ dbname }}
27+ - PGHOST : {{ dbhost }}
3928
4029{% if ' sql_file' in settings -% }
4130upload_sql_dump:
@@ -61,4 +50,4 @@ import_sql:
6150 - require:
6251 - pkg: zabbix- server
6352 - onchanges:
64- - test : check_db_pgsql
53+ - cmd : check_db_pgsql
0 commit comments