-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
emir-utils.el
144 lines (129 loc) · 5.53 KB
/
emir-utils.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
;;; emir-utils.el --- Non-essential utilities -*- lexical-binding:t -*-
;; Copyright (C) 2016-2024 Jonas Bernoulli
;; Author: Jonas Bernoulli <[email protected]>
;; Homepage: https://github.com/emacscollective/emir
;; Keywords: local
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
(require 'emir)
(unless (boundp 'eieio--unbound) ; New name since Emacs 28.1.
(defvaralias 'eieio--unbound 'eieio-unbound nil))
;;;###autoload
(defun emir-report ()
"Open the main maintainance report file."
(interactive)
(find-file-other-frame
(expand-file-name "compare.org" emir-stats-repository)))
(defun emir-remove-obsolete-wiki-branches ()
"Remove branches corresponding to shelved and removed packages."
(interactive)
(with-epkg-repository 'epkg-wiki-package
(dolist (branch (magit-list-local-branch-names))
(let ((pkg (epkg branch)))
(when (and (not (equal branch "master"))
(or (not pkg)
(cl-typep pkg 'epkg-shelved-package)))
(message "Removing %s..." branch)
(magit-call-git "branch" "-D" branch)
(message "Removing %s...done" branch))))))
(defun emir--list-tables (db)
"Return a list of all tables in DB."
(emacsql db [:select name :from sqlite_master
:where (= type table)
:order-by [(asc name)]]))
(defun emir--table-schemata (db)
"Return the schemata of all tables in DB."
(let ((str (emacsql-compile
db [:select * :from sqlite_master
:where (= type table)])))
(emacsql-clear db)
(emacsql-send-message db str)
(emacsql-wait db)
(with-current-buffer (emacsql-buffer db)
(goto-char (point-min))
(let* ((alist '(("\"" . " ")
("'" . "\"")
("," . " | ")
("DEFAULT" . ":default")
("PRIMARY KEY" . ":primary-key")
("FOREIGN KEY" . ":foreign-key")
("ON DELETE" . ":on-delete")
("CASCADE" . ":cascade")
("REFERENCES" . ":references")
("NOT NULL" . ":not-null")
("SET NULL" . ":set-null")
("NULL" . ":null")))
(regex (concat "\\(" (mapconcat #'car alist "\\|") "\\)"))
(case-fold-search nil))
(save-excursion
(while (re-search-forward regex nil t)
(let ((key (match-string 0)))
(replace-match (cdr (assoc (match-string 0) alist)) t t)
(when (and (member key '("PRIMARY KEY" "FOREIGN KEY"))
(looking-at " ([^)]+)"))
(replace-match
(save-match-data
(format " [%s]"
(string-replace
"," " " (substring (match-string 0) 2 -1))))
nil t))))))
(mapcar (lambda (tbl)
(list (cadr tbl)
(let* ((seq (car (nthcdr 7 tbl)))
(pos (seq-position seq '| #'eq)))
(vector (seq-take seq pos)
(seq-drop seq pos)))))
(read (current-buffer))))))
(defun emir--set-db-version (db version)
(closql--db-set-version db version)
(emir-commit (format "Bump database version to %s" version) nil :dump))
(defun emir--recreate-db ()
(error "BUG emir--recreate-db has to be updated")
(require 'epkg-schemata)
(when-let ((db (epkg-db t)))
(emacsql-close db))
(message "Recreating Epkg database...")
(let* ((old-file (expand-file-name "epkg.sqlite" epkg-repository))
(new-file (expand-file-name "new.sqlite" epkg-repository))
;; FIXME No longer possible to open two instances like this.
(old (closql-db 'epkg-database nil old-file))
(new (closql-db 'epkg-database nil new-file)))
(closql--db-set-version new (oref-default 'epkg-database version))
(emacsql-with-transaction new
(dolist (obj (closql-entries old))
(let ((name (oref obj name)))
(message "Re-inserting %s..." name)
(closql--resolve-slots obj)
(slot-makeunbound obj 'gnu-elpa-recipes)
(slot-makeunbound obj 'melpa-recipes)
(closql-insert new obj)
(message "Re-inserting %s...done" name))))
(emacsql-close old)
(emacsql-close new)
(rename-file new-file old-file t)
(epkg-db)
(emir-import-gnu-elpa-recipes '("--fetch" "--all"))
(emir-import-nongnu-elpa-recipes '("--fetch" "--all"))
(emir-import-melpa-recipes '("--fetch" "--all")))
(message "Recreating Epkg database...done"))
(defun emir-enable-sql-logging ()
"Enable logging Epkg SQL queries."
(interactive)
(let ((conn (oref (epkg-db) connection)))
(emacsql-enable-debugging conn)
(switch-to-buffer-other-window (oref conn log-buffer))))
;;; _
(provide 'emir-utils)
;;; emir-utils.el ends here