[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql 8d111e4dcd 6/9: emacsql-sqlite-dump-database: New
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql 8d111e4dcd 6/9: emacsql-sqlite-dump-database: New function |
Date: |
Wed, 22 Feb 2023 14:59:08 -0500 (EST) |
branch: elpa/emacsql
commit 8d111e4dcd53c12608355429e863b9f70d5d815c
Author: Jonas Bernoulli <jonas@bernoul.li>
Commit: Jonas Bernoulli <jonas@bernoul.li>
emacsql-sqlite-dump-database: New function
---
emacsql-sqlite-common.el | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/emacsql-sqlite-common.el b/emacsql-sqlite-common.el
index 216dbcc47a..57233b9dbd 100644
--- a/emacsql-sqlite-common.el
+++ b/emacsql-sqlite-common.el
@@ -99,6 +99,53 @@ in the returned value."
(not-like name "sqlite_%"))
:order-by [(asc name)]]))
+(defun emacsql-sqlite-dump-database (connection &optional versionp)
+ "Dump the database specified by CONNECTION to a file.
+
+The dump file is placed in the same directory as the database
+file and its name derives from the name of the database file.
+The suffix is replaced with \".sql\" and if optional VERSIONP is
+non-nil, then the database version (the `user_version' pragma)
+and a timestamp are appended to the file name.
+
+Dumping is done using the official `sqlite3' binary. If that is
+not available and VERSIONP is non-nil, then the database file is
+copied instead."
+ (let* ((version (caar (emacsql connection [:pragma user-version])))
+ (db (oref connection file))
+ (db (if (symbolp db) (symbol-value db) db))
+ (name (file-name-nondirectory db))
+ (output (concat (file-name-sans-extension db)
+ (and versionp
+ (concat (format "-v%s" version)
+ (format-time-string "-%Y%m%d-%H%M")))
+ ".sql")))
+ (cond
+ ((locate-file "sqlite3" exec-path)
+ (when (file-exists-p output)
+ (error "Cannot dump database; %s already exists" output))
+ (with-temp-file output
+ (message "Dumping %s database to %s..." name output)
+ (unless (zerop (save-excursion
+ (call-process "sqlite3" nil t nil db ".dump")))
+ (error "Failed to dump %s" db))
+ (when version
+ (insert (format "PRAGMA user_version=%s;\n" version)))
+ ;; The output contains "PRAGMA foreign_keys=OFF;".
+ ;; Change that to avoid alarming attentive users.
+ (when (re-search-forward "^PRAGMA foreign_keys=\\(OFF\\);" 1000 t)
+ (replace-match "ON" t t nil 1))
+ (message "Dumping %s database to %s...done" name output)))
+ (versionp
+ (setq output (concat (file-name-sans-extension output) ".db"))
+ (message "Cannot dump database because sqlite3 binary cannot be found")
+ (when (file-exists-p output)
+ (error "Cannot copy database; %s already exists" output))
+ (message "Copying %s database to %s..." name output)
+ (copy-file db output)
+ (message "Copying %s database to %s...done" name output))
+ ((error "Cannot dump database; sqlite3 binary isn't available")))))
+
(provide 'emacsql-sqlite-common)
;;; emacsql-sqlite-common.el ends here
- [nongnu] elpa/emacsql updated (f0249f655f -> 497971121a), ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql 7a725e1910 2/9: Add new library emacsql-sqlite-common, ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql 8d111e4dcd 6/9: emacsql-sqlite-dump-database: New function,
ELPA Syncer <=
- [nongnu] elpa/emacsql b4d7efac4d 7/9: emacsql-sqlite-restore-database: New function, ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql 0bcf526c48 1/9: Fix how class documentation is provided, ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql d61f674b64 4/9: emacsql--sqlite-base: Use nil as :initform of file slot, ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql ad3483c97c 5/9: emacsql-sqlite-list-tables: New function, ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql 497971121a 9/9: emacsql-sqlite-dump-database: Override output file unless versioned, ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql b33073257e 3/9: Use common base class for all SQLite connection classes, ELPA Syncer, 2023/02/22
- [nongnu] elpa/emacsql 70e95655df 8/9: emacsql-sqlite-default-connection: New function, ELPA Syncer, 2023/02/22