commit c10b90791514cc4f22b5b95c3d5968b346e8863f parent c9adf2ab70a2a6e3379bcd454d90d0f86f9ec222 Author: Jan Pobrislo <ccx@webprojekty.cz> Date: Fri, 27 Jun 2014 19:48:18 +0200 database & schema setup for postgresql Diffstat:
| M | zsh-functions/confz_postgresql_init | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 68 insertions(+), 0 deletions(-)
diff --git a/zsh-functions/confz_postgresql_init b/zsh-functions/confz_postgresql_init @@ -65,3 +65,71 @@ confz_postgresql_cluster_slot_check() { data_dir="/var/lib/postgresql/$vars[slot]/data" \ initdb="/usr/lib/postgresql-$vars[slot]/bin/initdb" } + +confz_postgresql_db_check() { + checkvars db owner + defvar user postgres + defvar psql psql + defvar createdb createdb + + local out name owner encoding rest + local -a opts + + opts+=( -U $vars[user] ) + (($+vars[host])) && opts+=( --host=$vars[host] ) + (($+vars[port])) && opts+=( --port=$vars[port] ) + + do_command=( $createdb $opts $db -O $owner ) + if (($+vars[encoding])); then + do_command+=( -E $vars[encoding] ) + fi + + out=$( $psql $opts -lAP tuples_only=on ) || die "psql failed" + while IFS='|' read name owner encoding rest; do + if [[ $name == $vars[db] ]]; then + [[ $owner == $vars[owner] ]] || \ + die "database $name has owner $owner, want $vars[owner]" + if (($+vars[encoding])); then + [[ $encoding == $vars[encoding] ]] || \ + die "database $name has encoding $encoding, want $vars[encoding]" + fi + return 0 + fi + done + + fail_reason="database ${(qqq)vars[name]} was not found" + return 1 +} + +confz_postgresql_schema_check() { + checkvars db schema psql_input + defvar user postgres + defvar psql psql + + local out + local -a opts + + opts+=( -U $vars[user] ) + (($+vars[host])) && opts+=( --host=$vars[host] ) + (($+vars[port])) && opts+=( --port=$vars[port] ) + + out=$( $psql $opts -AP tuples_only=on -c '\d' $vars[db] ) || die "psql failed" + if [[ -z $out ]]; then + fail_reason="the schema for ${(qqq)db} is empty" + return 1 + elif [[ $out != $schema ]]; then + die "the schema for ${(qqq)db} differs:"$'\n'$( \ + diff -u <(print -r - $schema) <(print -r - $out) ) + fi + return 0 +} + +confz_postgresql_schema_do() { + local -a opts + + opts+=( -U $vars[user] ) + (($+vars[host])) && opts+=( --host=$vars[host] ) + (($+vars[port])) && opts+=( --port=$vars[port] ) + + $psql $opts -1 $vars[db] -f - <<<$vars[psql_input] +}