database: Use an external SQL schema.

pull/3/head
Mathieu Lirzin 2016-07-16 18:16:39 +02:00
parent 730832c2fb
commit 46c9d432ea
No known key found for this signature in database
GPG Key ID: 0ADEE10094604D37
6 changed files with 56 additions and 30 deletions

View File

@ -14,6 +14,8 @@ nodist_pkgmodule_DATA = \
src/cuirass/config.scm \
src/cuirass/config.go
dist_pkgdata_DATA = src/schema.sql
TEST_EXTENSIONS = .scm .sh
AM_TESTS_ENVIRONMENT = \
env GUILE_AUTO_COMPILE='0' \

View File

@ -24,6 +24,9 @@ GUILE_LOAD_COMPILED_PATH="$abs_top_builddir/src${GUILE_LOAD_COMPILED_PATH:+:}$GU
GUILE_LOAD_PATH="$abs_top_builddir/src:$abs_top_srcdir/src${GUILE_LOAD_PATH:+:}$GUILE_LOAD_PATH"
export GUILE_LOAD_COMPILED_PATH GUILE_LOAD_PATH
CUIRASS_DATADIR="$abs_top_srcdir/src"
export CUIRASS_DATADIR
PATH="$abs_top_builddir/bin:$PATH"
export PATH

View File

@ -8,11 +8,13 @@ AM_SILENT_RULES([yes]) # enables silent rules by default
AC_CANONICAL_HOST
# Prepare a version of $localstatedir that does not contain references to
# shell variables.
# Prepare a version of installation directories that does not contain
# references to shell variables.
expanded_prefix="`eval echo $prefix | sed -e "s|NONE|/usr/local|g"`"
expanded_localstatedir="`eval echo $localstatedir | sed -e "s|NONE|$expanded_prefix|g"`"
AC_SUBST([expanded_localstatedir])
AC_SUBST([expanded_datadir],
[`eval echo $datadir | sed -e "s|NONE|$expanded_prefix|g"`])
AC_SUBST([expanded_localstatedir],
[`eval echo $localstatedir | sed -e "s|NONE|$expanded_prefix|g"`])
PKG_CHECK_MODULES([GUILE], [guile-2.0 >= 2.0.7])
AC_PATH_PROG([GUILE], [guile])

View File

@ -47,6 +47,10 @@
;; Define to the version of this package.
"@PACKAGE_VERSION@")
(define-public %datadir
;; Define to DATADIR without reference to '${prefix}'.
"@expanded_datadir@")
(define-public %localstatedir
;; Define to LOCALSTATEDIR without reference to '${prefix}'.
"@expanded_localstatedir@")

View File

@ -1,5 +1,4 @@
;;;; database.scm - store evaluation and build results
;;;
;;; database.scm -- store evaluation and build results
;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
;;;
;;; This file is part of Cuirass.
@ -18,8 +17,8 @@
;;; along with Cuirass. If not, see <http://www.gnu.org/licenses/>.
(define-module (cuirass database)
#:use-module (cuirass base)
#:use-module (cuirass config)
#:use-module (ice-9 rdelim)
#:use-module (sqlite3)
#:export (;; Procedures.
db-init
@ -29,6 +28,7 @@
db-get-evaluation
db-delete-evaluation
db-add-build-log
read-sql-file
;; Parameters.
%package-database
;; Macros.
@ -44,6 +44,24 @@
;; Define to the database file name of this package.
(make-parameter (string-append %localstatedir "/" %package ".db")))
(define %package-schema-file
;; Define to the database schema file of this package.
(make-parameter (string-append (or (getenv "CUIRASS_DATADIR")
(string-append %datadir "/" %package))
"/schema.sql")))
(define (read-sql-file file-name)
"Return a list of string containing SQL instructions from FILE-NAME."
(call-with-input-file file-name
(λ (port)
(let loop ((insts '()))
(let ((inst (read-delimited ";" port 'concat)))
(if (or (eof-object? inst)
;; Don't cons the spaces after the last instructions.
(string-every char-whitespace? inst))
(reverse! insts)
(loop (cons inst insts))))))))
(define (db-init)
"Open the database to store and read jobs and builds informations. Return a
database object."
@ -53,29 +71,8 @@ database object."
(delete-file db-name))
(let ((db (sqlite-open db-name (logior SQLITE_OPEN_CREATE
SQLITE_OPEN_READWRITE))))
(for-each (λ (sql) (sqlite-exec db sql))
'("PRAGMA foreign_keys=OFF;"
"BEGIN TRANSACTION;"
"COMMIT;"
"
CREATE TABLE job_spec (
name text not null,
url text not null,
branch text not null,
file text not null,
proc text not null,
arguments text not null,
primary key (name)
);"
"
CREATE TABLE build (
id integer primary key autoincrement not null,
job_spec text not null,
drv text not null,
log text,
output text
-- foreign key (job_spec) references job_spec(name)
);"))
(for-each (λ (sql) (false-if-exception (sqlite-exec db sql)))
(read-sql-file (%package-schema-file)))
db)))
(define (db-open)

18
src/schema.sql Normal file
View File

@ -0,0 +1,18 @@
create table job_spec (
name text not null,
url text not null,
branch text not null,
file text not null,
proc text not null,
arguments text not null,
primary key (name)
);
create table build (
id integer primary key autoincrement not null,
job_spec text not null,
drv text not null,
log text,
output text
-- foreign key (job_spec) references job_spec(name)
);