]> git.donarmstrong.com Git - cran2deb.git/commitdiff
db: tracking builds & packages backend.
authorblundellc <blundellc@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Sat, 13 Sep 2008 13:22:11 +0000 (13:22 +0000)
committerblundellc <blundellc@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Sat, 13 Sep 2008 13:22:11 +0000 (13:22 +0000)
note: database needs to be completely recreated with this commit.

packages are flagged for build iff:
- there is no latest build
- the database (or cran2deb) has changed since the last build
- the debian epoch for cran2deb has changed since the last build
- the R version of the last build does not match that of the very
latest R package.
it should be easy to add further conditions in as this is all done with
one moderately-sized SQL SELECT.

git-svn-id: svn://svn.r-forge.r-project.org/svnroot/cran2deb@89 edb9625f-4e0d-4859-8d74-9fd3b1da38cb

pkg/trunk/R/db.R

index 63722c3f645f943f9f767f1239e09732fee2c2e4..e0971abd04e5f64c526595ee5e02c8d12c35919d 100644 (file)
@@ -2,7 +2,6 @@
 db_start <- function() {
     drv <- dbDriver('SQLite')
     con <- dbConnect(drv, dbname=file.path(cache_root,'cran2deb.db'))
-    tables <- dbListTables(con)
     if (!dbExistsTable(con,'sysreq_override')) {
         dbGetQuery(con,paste('CREATE TABLE sysreq_override ('
                   ,' depend_alias TEXT NOT NULL'
@@ -41,15 +40,35 @@ db_start <- function() {
         dbGetQuery(con,paste('CREATE TABLE database_versions ('
                   ,' version INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'
                   ,',version_date INTEGER NOT NULL'
+                  ,',base_epoch INTEGER NOT NULL'
+                  ,')'))
+        db_add_version(con,1,0)
+    }
+    if (!dbExistsTable(con,'packages')) {
+        dbGetQuery(con,paste('CREATE TABLE packages ('
+                  ,' package TEXT PRIMARY KEY NOT NULL'
+                  ,',latest_r_version TEXT'
+                  ,')'))
+    }
+    if (!dbExistsTable(con,'builds')) {
+        dbGetQuery(con,paste('CREATE TABLE builds ('
+                  ,' id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'
+                  ,',package TEXT NOT NULL'
+                  ,',r_version TEXT NOT NULL'
+                  ,',deb_epoch INTEGER NOT NULL'
+                  ,',deb_revision INTEGER NOT NULL'
+                  ,',db_version INTEGER NOT NULL'
+                  ,',success INTEGER NOT NULL'
+                  ,',log TEXT'
+                  ,',UNIQUE(package,r_version,deb_epoch,deb_revision,db_version)'
                   ,')'))
-        db_add_version(con,1)
     }
     return(con)
 }
 
 db_stop <- function(con,bump=F) {
     if (bump) {
-        db_bump()
+        db_bump(con)
     }
     dbDisconnect(con)
 }
@@ -66,13 +85,23 @@ db_cur_version <- function(con) {
     return(as.integer(dbGetQuery(con, 'SELECT max(version) FROM database_versions')[[1]]))
 }
 
-db_add_version <- function(con, version) {
-    dbGetQuery(con,paste('INSERT INTO database_versions (version,version_date)'
-              ,'VALUES (',as.integer(version),',',db_now(),')'))
+db_base_epoch <- function(con) {
+    return(as.integer(dbGetQuery(con,
+        paste('SELECT max(base_epoch) FROM database_versions'
+             ,'WHERE version IN (SELECT max(version) FROM database_versions)'))[[1]]))
+}
+
+db_add_version <- function(con, version, epoch) {
+    dbGetQuery(con,paste('INSERT INTO database_versions (version,version_date,base_epoch)'
+              ,'VALUES (',as.integer(version),',',db_now(),',',as.integer(epoch),')'))
 }
 
 db_bump <- function(con) {
-    db_add_version(con,db_cur_version(con)+1)
+    db_add_version(con,db_cur_version(con)+1, db_base_epoch(con))
+}
+
+db_bump_epoch <- function(con) {
+    db_add_version(con,db_cur_version(con)+1, db_base_epoch(con)+1)
 }
 
 db_sysreq_override <- function(sysreq_text) {
@@ -227,3 +256,56 @@ db_add_license_hash <- function(name,license_sha1) {
     db_stop(con,TRUE)
 }
 
+
+db_update_package_versions <- function() {
+    con <- db_start()
+    for (package in available[,'Package']) {
+        dbGetQuery(con, paste('INSERT OR REPLACE INTO packages (package,latest_r_version)'
+                             ,'VALUES (',db_quote(package)
+                             ,',',db_quote(available[package,'Version']),')'))
+    }
+    db_stop(con)
+}
+
+db_record_build <- function(package, deb_version, log, success=F) {
+    con <- db_start()
+    dbGetQuery(con,paste('INSERT INTO builds'
+                        ,'(package,r_version,deb_epoch,deb_revision,db_version,success,log)'
+                        ,'VALUES'
+                        ,'(',db_quote(package)
+                        ,',',db_quote(version_upstream(deb_version))
+                        ,',',db_quote(version_epoch(deb_version))
+                        ,',',db_quote(version_revision(deb_version))
+                        ,',',db_cur_version(con)
+                        ,',',as.integer(success)
+                        ,',',log
+                        ,')'))
+    db_stop(con)
+}
+
+db_outdated_packages <- function() {
+    con <- db_start()
+    packages <- dbGetQuery(con,paste('SELECT packages.package FROM packages'
+               ,'LEFT OUTER JOIN ('
+               # extract the latest attempt at building each package
+               ,      'SELECT * FROM builds'
+               ,      'NATURAL JOIN (SELECT package,max(id) AS max_id FROM builds'
+               ,                    'GROUP BY package) AS last'
+               ,      'WHERE id = max_id) AS build'
+               ,'ON build.package = packages.package'
+               # outdated iff:
+               # - there is no latest build
+               ,'WHERE build.package IS NULL'
+               # - the database has changed since last build
+               ,'OR build.db_version < (SELECT max(version) FROM database_versions)'
+               # - the debian epoch has been bumped up
+               ,'OR build.deb_epoch < (SELECT max(base_epoch) FROM database_versions'
+               ,                        'WHERE version IN ('
+               ,                            'SELECT max(version) FROM database_versions))'
+               # - the latest build is not of the latest R version
+               ,'OR build.r_version != packages.latest_r_version'
+               ))$package
+    db_stop(con)
+    return(packages)
+}
+