# Funções comuns dos scripts de produção. Lê a ligação PostgreSQL da configuração Laravel # (sem a imprimir) e exporta-a para as variáveis PG* que pg_dump/psql/pg_restore usam. set -Eeuo pipefail APP_DIR="${APP_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}" PHP_BIN="${PHP_BIN:-php}" KEY_TABLES="tenants users requisitions purchase_orders goods_receipts invoices external_payments budgets budget_entries audit_logs" log() { printf '[%s] %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*"; } fail() { log "ERRO: $*"; exit 1; } sha256() { if command -v sha256sum >/dev/null; then sha256sum "$@"; else shasum -a 256 "$@"; fi; } load_database() { local values values="$(cd "$APP_DIR" && "$PHP_BIN" -r ' require "vendor/autoload.php"; $app = require "bootstrap/app.php"; $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); $name = config("database.default"); $c = config("database.connections.".$name); if (($c["driver"] ?? "") !== "pgsql") { fwrite(STDERR, "A ligação por omissão não é PostgreSQL.\n"); exit(1); } foreach (["host" => "PGHOST", "port" => "PGPORT", "database" => "PGDATABASE", "username" => "PGUSER", "password" => "PGPASSWORD"] as $k => $v) { echo $v, " ", base64_encode((string) ($c[$k] ?? "")), "\n"; }')" || fail "Configuração da base de dados indisponível." while read -r name value; do export "$name=$(printf '%s' "$value" | base64 --decode)" done <<< "$values" [ -n "$PGDATABASE" ] || fail "Nome da base de dados vazio." } # Contagem de linhas das tabelas principais, uma por linha: "tabela contagem". table_counts() { local database="$1" table for table in $KEY_TABLES; do if [ "$(psql -X -At -d "$database" -c "select to_regclass('public.$table') is not null")" = "t" ]; then printf '%s %s\n' "$table" "$(psql -X -At -d "$database" -c "select count(*) from public.$table")" fi done }