Skip to content

Commit 7b0f2ca

Browse files
committed
fix(pgsql): fix pgsql import - fix table detection
1 parent fbff3c1 commit 7b0f2ca

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

zabbix/pgsql/schema.sls

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,23 @@
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-
3013
include:
3114
- zabbix.pgsql.pkgs
3215
16+
# Check is there any tables in database.
17+
# returns changed if there are zero tables in the db
3318
check_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 -%}
4130
upload_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

Comments
 (0)