Make %package-database a parameter object.

Move it to (cuirass database).
pull/3/head
Mathieu Lirzin 2016-06-26 18:40:31 +02:00
parent 5efdcb4441
commit 5ff38984e8
5 changed files with 98 additions and 72 deletions

View File

@ -8,4 +8,7 @@
(bug-reference-url-format . "http://bugs.gnu.org/%s") (bug-reference-url-format . "http://bugs.gnu.org/%s")
(bug-reference-bug-regexp (bug-reference-bug-regexp
. "<https?://\\(debbugs\\|bugs\\)\\.gnu\\.org/\\([0-9]+\\)>"))) . "<https?://\\(debbugs\\|bugs\\)\\.gnu\\.org/\\([0-9]+\\)>")))
(scheme-mode . ((indent-tabs-mode . nil)))) (scheme-mode
.
((indent-tabs-mode . nil)
(eval . (put 'with-database 'scheme-indent-function 1)))))

View File

@ -111,30 +111,29 @@ DIR if required."
(show-version progname) (show-version progname)
(exit 0)) (exit 0))
(else (else
(let* ((specfile (option-ref opts 'file "tests/hello-subset.scm")) (parameterize ((%package-database
(dbfile (option-ref opts 'database %package-database)) (option-ref opts 'database (%package-database))))
(specs (primitive-load specfile)) (let* ((specfile (option-ref opts 'file "tests/hello-subset.scm"))
(args (option-ref opts '() #f)) (specs (primitive-load specfile))
(cachedir (if (null? args) (args (option-ref opts '() #f))
(getenv "CUIRASS_CACHEDIR") (cachedir (if (null? args)
(car args)))) (getenv "CUIRASS_CACHEDIR")
(db-close (db-init dbfile)) (car args))))
(while #t (with-database db
(for-each (while #t
(λ (spec) (for-each
(fetch-repository cachedir spec) (λ (spec)
(let ((store ((guix-variable 'store 'open-connection))) (fetch-repository cachedir spec)
(db (db-open dbfile))) (let ((store ((guix-variable 'store 'open-connection))))
(dynamic-wind (dynamic-wind
(const #t) (const #t)
(lambda () (lambda ()
(let* ((jobs (evaluate store db cachedir spec)) (let* ((jobs (evaluate store db cachedir spec))
(set-build-options (set-build-options
(guix-variable 'store 'set-build-options))) (guix-variable 'store 'set-build-options)))
(set-build-options store #:use-substitutes? #f) (set-build-options store #:use-substitutes? #f)
(build-packages store jobs))) (build-packages store jobs)))
(lambda () (lambda ()
((guix-variable 'store 'close-connection) store) ((guix-variable 'store 'close-connection) store)))))
(db-close db))))) specs)
specs) (sleep (string->number (option-ref opts 'interval "60")))))))))))
(sleep (string->number (option-ref opts 'interval "60")))))))))

View File

@ -47,6 +47,6 @@
;; Define to the version of this package. ;; Define to the version of this package.
"@PACKAGE_VERSION@") "@PACKAGE_VERSION@")
(define-public %package-database (define-public %localstatedir
;; Define to the database file name of this package. ;; Define to LOCALSTATEDIR without reference to '${prefix}'.
(string-append "@expanded_localstatedir@/" %package ".db")) "@expanded_localstatedir@")

View File

@ -19,14 +19,20 @@
(define-module (cuirass database) (define-module (cuirass database)
#:use-module (cuirass base) #:use-module (cuirass base)
#:use-module (cuirass config)
#:use-module (cuirass job) #:use-module (cuirass job)
#:use-module (sqlite3) #:use-module (sqlite3)
#:export (db-init #:export (;; Procedures.
db-init
db-open db-open
db-close db-close
db-add-evaluation db-add-evaluation
db-get-evaluation db-get-evaluation
db-delete-evaluation)) db-delete-evaluation
;; Parameters.
%package-database
;; Macros.
with-database))
(define (sqlite-exec db sql) (define (sqlite-exec db sql)
"Wrap 'sqlite-prepare', 'sqlite-step', and 'sqlite-finalize'." "Wrap 'sqlite-prepare', 'sqlite-step', and 'sqlite-finalize'."
@ -34,20 +40,24 @@
(sqlite-step stmt) (sqlite-step stmt)
(sqlite-finalize stmt))) (sqlite-finalize stmt)))
(define (db-init db-name) (define %package-database
"Open database contained in DB-NAME, to store or read jobs and builds ;; Define to the database file name of this package.
informations. DB-NAME must be a string. SCHEMA must be some SQL statements (make-parameter (string-append %localstatedir "/" %package ".db")))
initialize the database. Return a database object."
(when (file-exists? db-name) (define (db-init)
(format (current-error-port) "Removing leftover database ~a~%" db-name) "Open the database to store and read jobs and builds informations. Return a
(delete-file db-name)) database object."
(let* ((db (sqlite-open db-name (logior SQLITE_OPEN_CREATE (let ((db-name (%package-database)))
SQLITE_OPEN_READWRITE)))) (when (file-exists? db-name)
(for-each (λ (sql) (sqlite-exec db sql)) (format (current-error-port) "Removing leftover database ~a~%" db-name)
'("PRAGMA foreign_keys=OFF;" (delete-file db-name))
"BEGIN TRANSACTION;" (let ((db (sqlite-open db-name (logior SQLITE_OPEN_CREATE
"COMMIT;" SQLITE_OPEN_READWRITE))))
" (for-each (λ (sql) (sqlite-exec db sql))
'("PRAGMA foreign_keys=OFF;"
"BEGIN TRANSACTION;"
"COMMIT;"
"
CREATE TABLE job_spec ( CREATE TABLE job_spec (
name text not null, name text not null,
url text not null, url text not null,
@ -57,7 +67,7 @@ CREATE TABLE job_spec (
arguments text not null, arguments text not null,
primary key (name) primary key (name)
);" );"
" "
CREATE TABLE build ( CREATE TABLE build (
id integer primary key autoincrement not null, id integer primary key autoincrement not null,
job_spec text not null, job_spec text not null,
@ -65,13 +75,12 @@ CREATE TABLE build (
output text output text
-- foreign key (job_spec) references job_spec(name) -- foreign key (job_spec) references job_spec(name)
);")) );"))
db)) db)))
(define (db-open filename) (define (db-open)
"Open database contained in FILENAME, to store or read jobs and builds "Open database to store or read jobs and builds informations. Return a
informations. Return a database object. FILENAME must be a string corresponding database object."
to a valid file name." (sqlite-open (%package-database) SQLITE_OPEN_READWRITE))
(sqlite-open filename SQLITE_OPEN_READWRITE))
(define (db-close db) (define (db-close db)
"Close database object DB." "Close database object DB."
@ -103,5 +112,15 @@ to a valid file name."
(sqlite-exec db (sqlite-exec db
(format #f "delete from build where id=~A;" id))) (format #f "delete from build where id=~A;" id)))
(define-syntax-rule (with-database db body ...)
"Run BODY with a connection to the database which is bound to DB in BODY."
(let ((db (db-init)))
(dynamic-wind
(const #t)
(lambda ()
body ...)
(lambda ()
(db-close db)))))
;; (define (db-add-build db id) ;; (define (db-add-build db id)
;; "Store a build result corresponding to ID in database DB.") ;; "Store a build result corresponding to ID in database DB.")

View File

@ -26,26 +26,31 @@
#:derivation (string-append name ".drv") #:derivation (string-append name ".drv")
#:metadata '())) #:metadata '()))
(define tmp-database (define %db
(let ((dir (dirname (current-filename)))) ;; Global Slot for a database object.
(string-append dir "/tmp.db"))) (make-parameter #t))
(define %db (make-parameter #t)) (define %id
(define %id (make-parameter #t)) ;; Global Slot for a job ID in the database.
(make-parameter #t))
(dynamic-wind (parameterize ((%package-database
(const #t) ;; Use an empty and temporary database for the tests.
(λ () (let ((dir (dirname (current-filename))))
(test-assert "db-init" (string-append dir "/tmp.db"))))
(%db (db-init tmp-database))) (dynamic-wind
(const #t)
(λ ()
(test-assert "db-init"
(%db (db-init)))
(test-assert "db-add-evaluation" (test-assert "db-add-evaluation"
(%id (db-add-evaluation (%db) (make-dummy-job)))) (%id (db-add-evaluation (%db) (make-dummy-job))))
(test-assert "db-get-evaluation" (test-assert "db-get-evaluation"
(db-get-evaluation (%db) (%id))) (db-get-evaluation (%db) (%id)))
(test-assert "db-close" (test-assert "db-close"
(db-close (%db)))) (db-close (%db))))
(λ () (λ ()
(delete-file tmp-database))) (delete-file (%package-database)))))