diff options
| author | Christopher Baines <mail@cbaines.net> | 2024-02-18 13:03:42 +0000 |
|---|---|---|
| committer | Christopher Baines <mail@cbaines.net> | 2024-04-03 17:30:43 +0100 |
| commit | c6cc9aeb87014cfb8f1eabb525e28ac633bf7af4 (patch) | |
| tree | 7964e5a7d61821c7ecc33a73be975e490ba2672a | |
| parent | 511d68c71d9c676aeb3a97936a984fb95a75f5a2 (diff) | |
store: database: Refactor sqlite-register.
The update-or-insert procedure name was unhelpfully generic, and these changes
should improve the code readability.
* guix/store/database.scm (update-or-insert): Remove procedure and inline
functionality in to sqlite-register.
Change-Id: Ifab0cdb7972d095460cc1f79b8b2f0e9b958059c
| -rw-r--r-- | guix/store/database.scm | 96 |
1 files changed, 48 insertions, 48 deletions
diff --git a/guix/store/database.scm b/guix/store/database.scm index 178f46e4053..dea690ec76b 100644 --- a/guix/store/database.scm +++ b/guix/store/database.scm | |||
| @@ -204,42 +204,6 @@ SELECT id FROM ValidPaths WHERE path = :path" | |||
| 204 | "Integer ~A out of range: ~S" (list key number) | 204 | "Integer ~A out of range: ~S" (list key number) |
| 205 | (list number)))) | 205 | (list number)))) |
| 206 | 206 | ||
| 207 | (define* (update-or-insert db #:key path deriver hash nar-size time) | ||
| 208 | "The classic update-if-exists and insert-if-doesn't feature that sqlite | ||
| 209 | doesn't exactly have... they've got something close, but it involves deleting | ||
| 210 | and re-inserting instead of updating, which causes problems with foreign keys, | ||
| 211 | of course. Returns the row id of the row that was modified or inserted." | ||
| 212 | |||
| 213 | ;; Make sure NAR-SIZE is valid. | ||
| 214 | (assert-integer "update-or-insert" positive? #:nar-size nar-size) | ||
| 215 | (assert-integer "update-or-insert" (cut >= <> 0) #:time time) | ||
| 216 | |||
| 217 | (let ((id (path-id db path))) | ||
| 218 | (if id | ||
| 219 | (let ((stmt (sqlite-prepare | ||
| 220 | db | ||
| 221 | " | ||
| 222 | UPDATE ValidPaths | ||
| 223 | SET hash = :hash, registrationTime = :time, deriver = :deriver, narSize = :size | ||
| 224 | WHERE id = :id" | ||
| 225 | #:cache? #t))) | ||
| 226 | (sqlite-bind-arguments stmt #:id id | ||
| 227 | #:deriver deriver | ||
| 228 | #:hash hash #:size nar-size #:time time) | ||
| 229 | (sqlite-step-and-reset stmt) | ||
| 230 | id) | ||
| 231 | (let ((stmt (sqlite-prepare | ||
| 232 | db | ||
| 233 | " | ||
| 234 | INSERT INTO ValidPaths (path, hash, registrationTime, deriver, narSize) | ||
| 235 | VALUES (:path, :hash, :time, :deriver, :size)" | ||
| 236 | #:cache? #t))) | ||
| 237 | (sqlite-bind-arguments stmt | ||
| 238 | #:path path #:deriver deriver | ||
| 239 | #:hash hash #:size nar-size #:time time) | ||
| 240 | (sqlite-step-and-reset stmt) | ||
| 241 | (last-insert-row-id db))))) | ||
| 242 | |||
| 243 | (define (add-references db referrer references) | 207 | (define (add-references db referrer references) |
| 244 | "REFERRER is the id of the referring store item, REFERENCES is a list | 208 | "REFERRER is the id of the referring store item, REFERENCES is a list |
| 245 | ids of items referred to." | 209 | ids of items referred to." |
| @@ -265,9 +229,9 @@ VALUES (:referrer, :reference)" | |||
| 265 | (make-time time-utc 0 seconds) | 229 | (make-time time-utc 0 seconds) |
| 266 | (current-time time-utc))))) | 230 | (current-time time-utc))))) |
| 267 | 231 | ||
| 268 | (define* (sqlite-register db #:key path (references '()) | 232 | (define* (register-valid-path db #:key path (references '()) |
| 269 | deriver hash nar-size | 233 | deriver hash nar-size |
| 270 | (time (timestamp))) | 234 | (time (timestamp))) |
| 271 | "Registers this stuff in DB. PATH is the store item to register and | 235 | "Registers this stuff in DB. PATH is the store item to register and |
| 272 | REFERENCES is the list of store items PATH refers to; DERIVER is the '.drv' | 236 | REFERENCES is the list of store items PATH refers to; DERIVER is the '.drv' |
| 273 | that produced PATH, HASH is the base16-encoded Nix sha256 hash of | 237 | that produced PATH, HASH is the base16-encoded Nix sha256 hash of |
| @@ -276,15 +240,51 @@ being converted to nar form. TIME is the registration time to be recorded in | |||
| 276 | the database or #f, meaning \"right now\". | 240 | the database or #f, meaning \"right now\". |
| 277 | 241 | ||
| 278 | Every store item in REFERENCES must already be registered." | 242 | Every store item in REFERENCES must already be registered." |
| 279 | (let ((id (update-or-insert db #:path path | 243 | |
| 280 | #:deriver deriver | 244 | (define registration-time |
| 281 | #:hash hash | 245 | (time-second time)) |
| 282 | #:nar-size nar-size | 246 | |
| 283 | #:time (time-second time)))) | 247 | ;; Make sure NAR-SIZE is valid. |
| 284 | ;; Call 'path-id' on each of REFERENCES. This ensures we get a | 248 | (assert-integer "sqlite-register" positive? #:nar-size nar-size) |
| 285 | ;; "non-NULL constraint" failure if one of REFERENCES is unregistered. | 249 | (assert-integer "sqlite-register" (cut >= <> 0) #:time registration-time) |
| 286 | (add-references db id | 250 | |
| 287 | (map (cut path-id db <>) references)))) | 251 | (define id |
| 252 | (let ((existing-id (path-id db path))) | ||
| 253 | (if existing-id | ||
| 254 | (let ((stmt (sqlite-prepare | ||
| 255 | db | ||
| 256 | " | ||
| 257 | UPDATE ValidPaths | ||
| 258 | SET hash = :hash, registrationTime = :time, deriver = :deriver, narSize = :size | ||
| 259 | WHERE id = :id" | ||
| 260 | #:cache? #t))) | ||
| 261 | (sqlite-bind-arguments stmt | ||
| 262 | #:id existing-id | ||
| 263 | #:deriver deriver | ||
| 264 | #:hash hash | ||
| 265 | #:size nar-size | ||
| 266 | #:time registration-time) | ||
| 267 | (sqlite-step-and-reset stmt) | ||
| 268 | existing-id) | ||
| 269 | (let ((stmt (sqlite-prepare | ||
| 270 | db | ||
| 271 | " | ||
| 272 | INSERT INTO ValidPaths (path, hash, registrationTime, deriver, narSize) | ||
| 273 | VALUES (:path, :hash, :time, :deriver, :size)" | ||
| 274 | #:cache? #t))) | ||
| 275 | (sqlite-bind-arguments stmt | ||
| 276 | #:path path | ||
| 277 | #:deriver deriver | ||
| 278 | #:hash hash | ||
| 279 | #:size nar-size | ||
| 280 | #:time registration-time) | ||
| 281 | (sqlite-step-and-reset stmt) | ||
| 282 | (last-insert-row-id db))))) | ||
| 283 | |||
| 284 | ;; Call 'path-id' on each of REFERENCES. This ensures we get a | ||
| 285 | ;; "non-NULL constraint" failure if one of REFERENCES is unregistered. | ||
| 286 | (add-references db id | ||
| 287 | (map (cut path-id db <>) references))) | ||
| 288 | 288 | ||
| 289 | 289 | ||
| 290 | ;;; | 290 | ;;; |
