Skip to content

Commit

Permalink
Change sequence and modify DB rebuild steps
Browse files Browse the repository at this point in the history
Following roughly the [12-step generalized ALTER TABLE
procedure](https://www.sqlite.org/lang_altertable.html#otheralter).

This prevents that custom DB schema extensions like views referencing
the triples table will break after running
triples-rebuild-builtin-database. Alternatively, the PRAGMA
legacy_alter_table=ON could be prepended to the old version.
  • Loading branch information
smartmic committed Oct 15, 2023
1 parent fc824c6 commit e888c3e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions triples.el
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ The transaction will abort if an error is thrown."
"Rebuild the builtin database DB.
This is used in upgrades and when problems are detected."
(triples-with-transaction
db
(sqlite-execute db "ALTER TABLE triples RENAME TO triples_old")
(triples-setup-table-for-builtin db)
(sqlite-execute db "INSERT INTO triples (subject, predicate, object, properties) SELECT DISTINCT subject, predicate, object, properties FROM triples_old")
(sqlite-execute db "DROP TABLE triples_old")))
db
(sqlite-execute db "CREATE TABLE IF NOT EXISTS triples_new(subject NOT NULL, predicate TEXT NOT NULL, object NOT NULL, properties TEXT NOT NULL)")
(sqlite-execute db "INSERT INTO triples_new (subject, predicate, object, properties) SELECT DISTINCT subject, predicate, object, properties FROM triples")
(sqlite-execute db "DROP TABLE triples")
(sqlite-execute db "ALTER TABLE triples_new RENAME TO triples")
;; set up also new indices
(triples-setup-table-for-builtin db)))

(defun triples-maybe-upgrade-to-builtin (db)
"Check to see if DB needs to be upgraded from emacsql to builtin."
Expand Down

0 comments on commit e888c3e

Please sign in to comment.