Skip to content

Commit

Permalink
fix(pgsql): fix pgsql import - fix table detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ghormoon committed May 11, 2021
1 parent fbff3c1 commit 7b0f2ca
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions zabbix/pgsql/schema.sls
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,23 @@
{% set dbuser = settings.get('dbuser', defaults.dbuser) -%}
{% set dbpassword = settings.get('dbpassword', defaults.dbpassword) -%}
{% set dbroot_user = settings.get('dbroot_user') -%}
{% set dbroot_pass = settings.get('dbroot_pass') -%}
{% set sql_file = settings.get('sql_file', defaults.sql_file) -%}
# Connection args required only if dbroot_user and dbroot_pass defined.
{% set connection_args = {} -%}
{% if dbroot_user and dbroot_pass -%}
{% set connection_args = {'runas': 'nobody', 'host': dbhost, 'user': dbroot_user, 'password': dbroot_pass} -%}
{% endif -%}
# Check is there any tables in database.
# salt.postgres.psql_query return empty result if there is no tables or 'False' on any error i.e. failed auth.
{% set list_tables = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' LIMIT 1;" %}
{% set is_db_empty = True -%}
{% if salt.postgres.psql_query(query=list_tables, maintenance_db=dbname, **connection_args) -%}
{% set is_db_empty = False -%}
{% endif -%}
include:
- zabbix.pgsql.pkgs
# Check is there any tables in database.
# returns changed if there are zero tables in the db
check_db_pgsql:
test.configurable_test_state:
- name: Is there any tables in '{{ dbname }}' database?
- changes: {{ is_db_empty }}
- result: True
- comment: If changes is 'True' data import required.
cmd.run:
- 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.'\""
- runas: {{ zabbix.user }}
- stateful: True
- env:
- PGUSER: {{ dbuser }}
- PGPASSWORD: {{ dbpassword }}
- PGDATABASE: {{ dbname }}
- PGHOST: {{ dbhost }}
{% if 'sql_file' in settings -%}
upload_sql_dump:
Expand All @@ -61,4 +50,4 @@ import_sql:
- require:
- pkg: zabbix-server
- onchanges:
- test: check_db_pgsql
- cmd: check_db_pgsql

0 comments on commit 7b0f2ca

Please sign in to comment.