[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
17/20: squash! Migrate schema when opening.
From: |
guix-commits |
Subject: |
17/20: squash! Migrate schema when opening. |
Date: |
Sun, 4 Jun 2023 17:34:42 -0400 (EDT) |
civodul pushed a commit to branch wip-guix-index
in repository guix.
commit 4d89890b7afabdd129b203eb3726b978c810c750
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Sun Jun 4 21:33:58 2023 +0200
squash! Migrate schema when opening.
---
guix/scripts/locate.scm | 65 +++++++++++++++++++++++++------------------------
1 file changed, 33 insertions(+), 32 deletions(-)
diff --git a/guix/scripts/locate.scm b/guix/scripts/locate.scm
index 3fa598f1e9..a8a8f96be5 100644
--- a/guix/scripts/locate.scm
+++ b/guix/scripts/locate.scm
@@ -115,9 +115,41 @@ add column output text;
(let ((db (sqlite-open file)))
(dynamic-wind
(lambda () #t)
- (lambda () (proc db))
+ (lambda ()
+ (ensure-latest-database-schema db)
+ (proc db))
(lambda () (sqlite-close db)))))
+(define (ensure-latest-database-schema db)
+ "Ensure DB follows the latest known version of the schema."
+ (define (initialize)
+ (sqlite-exec db schema-full)
+ (insert-version db application-version))
+
+ (let ((version (false-if-exception (read-version db))))
+ (cond ((not version)
+ (initialize))
+ ((> version application-version)
+ (initialize))
+ (else
+ (catch #t
+ (lambda ()
+ ;; Migrate from the current version to the full migrated schema.
+ ;; This can raise sqlite-error if the db is not properly
configured yet
+ (let loop ((current version))
+ (when (< current application-version)
+ ;; when the current db version is older than the current
application
+ (let* ((next (+ current 1))
+ (migration (assoc-ref schema-to-migrate next)))
+ (when migration
+ (sqlite-exec db migration)
+ (insert-version db next))
+ (loop next)))))
+ (lambda _
+ ;; Exception handler in case failure to read an inexisting db:
+ ;; fallback to bootstrap the schema.
+ (initialize)))))))
+
(define (last-insert-row-id db) ;XXX: copied from (guix store database)
;; XXX: (sqlite3) currently lacks bindings for 'sqlite3_last_insert_rowid'.
;; Work around that.
@@ -389,34 +421,6 @@ ON f.basename = :file
(call-with-database db-file
(lambda (db) (matching-packages db file))))
-(define (read-version-from-db file)
- (call-with-database file read-version))
-
-(define (migrate-schema-to-version file)
- (call-with-database file
- (lambda (db)
- (catch #t
- (lambda ()
- ;; Migrate from the current version to the full migrated schema
- ;; This can raise sqlite-error if the db is not properly configured
yet
- (let* ((current-db-version (read-version db))
- (next-db-version (+ 1 current-db-version)))
- (when (< current-db-version application-version)
- ;; when the current db version is older than the current
application
- (let ((schema-migration-at-version (assoc-ref schema-to-migrate
next-db-version)))
- (when schema-migration-at-version
- ;; migrate the schema to the next version (if it exists)
- (sqlite-exec db schema-migration-at-version)
- ;; insert current version
- (insert-version db next-db-version)
- ;; iterate over the next migration if any
- (migrate-schema-to-version db))))))
- (lambda (key . arg)
- ;; exception handler in case failure to read an inexisting db
- ;; Fallback to boostrap the schema
- (sqlite-exec db schema-full)
- (insert-version db application-version))))))
-
(define (print-matching-results matches)
"Print the MATCHES matching results."
(for-each (lambda (result)
@@ -530,9 +534,6 @@ Locate FILE and return the package(s) that contain it.\n"))
opts))))
(define (populate-database database)
(mkdir-p (dirname database))
- ;; Migrate/initialize db to schema at version application-version
- (migrate-schema-to-version database)
- ;; Finally index packages
(match method
('manifests
(index-packages-from-manifests-with-db database))
- 18/20: squash! Optimize 'insert-package'., (continued)
- 18/20: squash! Optimize 'insert-package'., guix-commits, 2023/06/04
- 19/20: squash! Show output name except for "out"; remove '-d'., guix-commits, 2023/06/04
- 02/20: DRAFT Add 'guix index'., guix-commits, 2023/06/04
- 03/20: squash! Update test., guix-commits, 2023/06/04
- 05/20: squash! Improve error reporting and i18n., guix-commits, 2023/06/04
- 01/20: store: Tolerate non-existent GC root directories., guix-commits, 2023/06/04
- 06/20: squash! "with-method" -> "method", guix-commits, 2023/06/04
- 09/20: squash! Choose system-wide database if it's more recent., guix-commits, 2023/06/04
- 11/20: squash! Keep store prefix in database., guix-commits, 2023/06/04
- 15/20: squash! Rename to 'guix locate'., guix-commits, 2023/06/04
- 17/20: squash! Migrate schema when opening.,
guix-commits <=
- 20/20: squash! Document., guix-commits, 2023/06/04
- 10/20: squash! Do not insert the same directory more than once., guix-commits, 2023/06/04
- 14/20: squash! Remove actions; auto-update database when needed., guix-commits, 2023/06/04