Skip to content

Commit a42d822

Browse files
committed
fix(pgsql): fix pgsql import - fix table detection
1 parent 4f962c3 commit a42d822

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

zabbix/pgsql/schema.sls

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,26 @@
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 -%}
13+
{% set table_query = "SELECT count(tablename) FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';" %}
14+
{% set psql_cmd = "$(psql -X -A -t -c \\\"" + table_query + "\\\" || echo \\\"-1\\\") -eq \\\"0\\\" " %}
2915
3016
include:
3117
- zabbix.pgsql.pkgs
3218
19+
# Check is there any tables in database.
20+
# returns changed if there are zero tables in the db
3321
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.
22+
cmd.run:
23+
- name: "[[ {{ psql_cmd }} ]] && echo \"changed=yes comment='DB needs schema import.'\" || echo \"changed=no comment='No DB import needed or possible.'\""
24+
- runas: {{ zabbix.user }}
25+
- stateful: True
26+
- env:
27+
- PGUSER: {{ dbuser }}
28+
- PGPASSWORD: {{ dbpassword }}
29+
- PGDATABASE: {{ dbname }}
30+
- PGHOST: {{ dbhost }}
3931
4032
{% if 'sql_file' in settings -%}
4133
upload_sql_dump:
@@ -61,4 +53,4 @@ import_sql:
6153
- require:
6254
- pkg: zabbix-server
6355
- onchanges:
64-
- test: check_db_pgsql
56+
- cmd: check_db_pgsql

0 commit comments

Comments
 (0)