(declare (unit tick-check-db)) (module tick-check-db () (import scheme) (import (chicken base) (chicken format)) (import commands optimism) (import tick tick-params) (define-command 'check-db "\ check-db Perform sanity checks on the database." (lambda (args*) (let* ((args (parse-command-line args* '())) (errors '()) (add-error! (lambda (fmt . args) (let ((msg (apply sprintf (cons (string-append " " fmt) args)))) (set! errors (cons msg errors))))) (all-tickets (map read-ticket (db-ticket-ids)))) (print "Checking whether the value of empty properties is #f...") (for-each (lambda (ticket) (for-each (lambda (slot) (let ((val (ticket-ref ticket slot)) (thash (ticket-id ticket))) (when (and val (equal? val "")) (add-error! "~a: string null as value of ~a" thash slot)))) %ticket-slots)) all-tickets) (print "Checking whether all assigned tickets have an owner...") (for-each (lambda (ticket) (when (and (equal? (ticket-status ticket) "assigned") (not (ticket-owner ticket))) (add-error! "~a: ticket is assigned but has no owner" (ticket-id ticket)))) all-tickets) (print "Checking whether all accepted tickets have an owner...") (for-each (lambda (ticket) (when (and (equal? (ticket-status ticket) "accepted") (not (ticket-owner ticket))) (add-error! "~a: ticket is accepted but has no owner" (ticket-id ticket)))) all-tickets) (print "Checking whether all new tickets do not have an owner...") (for-each (lambda (ticket) (when (and (equal? (ticket-status ticket) "new") (ticket-owner ticket)) (add-error! "~a: ticket is new but has ~a as owner" (ticket-id ticket) (ticket-owner ticket)))) all-tickets) (print "Checking whether all tickets have all the required properties set...") (for-each (lambda (ticket) (for-each (lambda (prop) (let ((val (ticket-ref ticket prop))) (when (or (not val) (equal? val "")) (add-error! "~a: ~a is empty" (ticket-id ticket) prop)))) +required-properties+)) all-tickets) (print "Checking whether all owners are valid...") (for-each (lambda (ticket) (let ((owner (ticket-owner ticket)) (all-users (valid-users))) (when (and owner (not (member owner all-users))) (add-error! "~a: owner ~a is not a valid user" (ticket-id ticket) owner)))) all-tickets) ;; Check for errors and exit 1 in case of any error (unless (null? errors) (for-each print errors) (exit 1))))) ) ;; end module