update-external-files 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #! /bin/bash
  2. # Copyright (C) 2017, Ansgar Burchardt <ansgar@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License as
  6. # published by the Free Software Foundation; version 2.
  7. #
  8. # This program is distributed in the hope that it will be useful, but
  9. # WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. set -e
  17. set -u
  18. set -o pipefail
  19. usage() {
  20. cat >&2 <<-EOF
  21. usage: $0 --update
  22. Update `external_files` table with data from standard input.
  23. The input should be the xz-compressed output of the
  24. COPY files (id, filename, size, md5sum, last_used, sha1sum,
  25. sha256sum, created, modified) TO STDOUT
  26. query. See config/debian/daily.functions's pushfilesdb.
  27. EOF
  28. exit ${1:-0}
  29. }
  30. if [ "$#" -eq 0 ]; then
  31. usage
  32. fi
  33. if [ "$#" -ne 1 -o "${1}" != "--update" ]; then
  34. usage 1
  35. fi
  36. /usr/bin/xzcat |
  37. /usr/bin/psql -1 -d obscurity -c '
  38. CREATE TEMPORARY TABLE external_files_tmp AS SELECT * FROM external_files WITH NO DATA;
  39. COPY external_files_tmp (id, filename, size, md5sum, last_used, sha1sum, sha256sum, created, modified) FROM STDIN;
  40. DELETE FROM external_files ef WHERE NOT EXISTS (SELECT 1 FROM external_files_tmp tmp WHERE tmp.id = ef.id);
  41. INSERT INTO external_files SELECT * FROM external_files_tmp tmp WHERE NOT EXISTS (SELECT 1 FROM external_files ef WHERE ef.id = tmp.id);
  42. '