Extract out code for updating derivation source file nars

So that this can be used to fix some which have crept in to the database as
placeholders.
This commit is contained in:
Christopher Baines 2025-03-11 19:38:56 +00:00
parent 1ac9f8072e
commit 167b7d029e

View file

@ -1006,6 +1006,48 @@
1000 1000
missing-file-names)))) missing-file-names))))
(define (compute-and-update-derivation-source-file-nar
postgresql-connection-pool
id
source-file)
(let ((nar-bytevector
(call-with-values
(lambda ()
(open-bytevector-output-port))
(lambda (port get-bytevector)
(unless (file-exists? source-file)
(raise-exception
(make-missing-store-item-error
source-file)))
(write-file source-file port)
(let ((res (get-bytevector)))
(close-port port) ; maybe reduces memory?
res)))))
(let ((compressed-nar-bytevector
(call-with-values
(lambda ()
(open-bytevector-output-port))
(lambda (port get-bytevector)
(call-with-lzip-output-port port
(lambda (port)
(put-bytevector port nar-bytevector))
#:level 9)
(let ((res (get-bytevector)))
(close-port port) ; maybe reduces memory?
res))))
(hash
(bytevector->nix-base32-string
(sha256 nar-bytevector)))
(uncompressed-size
(bytevector-length nar-bytevector)))
(with-resource-from-pool postgresql-connection-pool conn
(update-derivation-source-file-nar
conn
id
hash
compressed-nar-bytevector
uncompressed-size)))))
(define* (derivations-insert-sources postgresql-connection-pool (define* (derivations-insert-sources postgresql-connection-pool
call-with-utility-thread call-with-utility-thread
derivations derivations
@ -1044,43 +1086,10 @@ SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
;; avoid using too much memory ;; avoid using too much memory
(call-with-utility-thread (call-with-utility-thread
(lambda () (lambda ()
(let ((nar-bytevector (compute-and-update-derivation-source-file-nar
(call-with-values postgresql-connection-pool
(lambda ()
(open-bytevector-output-port))
(lambda (port get-bytevector)
(unless (file-exists? source-file)
(raise-exception
(make-missing-store-item-error
source-file)))
(write-file source-file port)
(let ((res (get-bytevector)))
(close-port port) ; maybe reduces memory?
res)))))
(let ((compressed-nar-bytevector
(call-with-values
(lambda ()
(open-bytevector-output-port))
(lambda (port get-bytevector)
(call-with-lzip-output-port port
(lambda (port)
(put-bytevector port nar-bytevector))
#:level 9)
(let ((res (get-bytevector)))
(close-port port) ; maybe reduces memory?
res))))
(hash
(bytevector->nix-base32-string
(sha256 nar-bytevector)))
(uncompressed-size
(bytevector-length nar-bytevector)))
(with-resource-from-pool postgresql-connection-pool conn
(update-derivation-source-file-nar
conn
id id
hash source-file)))))
compressed-nar-bytevector
uncompressed-size))))))))
sources-ids sources-ids
sources))))) sources)))))
derivation-ids derivation-ids