Skip to content

Commit

Permalink
Preliminary SchemaSpy support
Browse files Browse the repository at this point in the history
  • Loading branch information
emarsden committed Aug 5, 2024
1 parent 814ec5d commit f2a16db
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ C-u ! tr '[:upper:]' '[:lower:']
- Typing `R` in a row-list table will rename the current table column. The new name will be read
from the minibuffer.

- Preliminary support for running the SchemaSpy application on the current table and displaying the
schema diagram. See customizable variable `pgmacs-schemaspy-cmdline`.

- Preliminary support for a customized header line (see customizable variable `pgmacs-header-line`).

- New customizable variable `pgmacs-enable-query-logging` specifies whether SQL queries should be
Expand Down
1 change: 1 addition & 0 deletions doc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
- [Docker container](./container.md)
- [Table list buffer](./table-list.md)
- [Editing data](./editing.md)
- [SchemaSpy support](./schemaspy.md)
- [Feedback](./feedback.md)
Binary file added doc/src/img/screenshot-schemaspy-table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions doc/src/schemaspy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Running SchemaSpy

In a row-list buffer, type `S` to run the [SchemaSpy application](https://schemaspy.org/) on the
current table and view the diagram of the table structure in a dedicated buffer. This functionality
only works in graphical mode (not in the terminal) and requires your Emacs to support SVG images.

![Screenshot table](img/screenshot-schemaspy-table.png)

See the customizable variable `pgmacs-schemaspy-cmdline` to adjust the commandline which runs
SchemaSpy to your local installation.

This functionality requires the following software to be installed:

- SchemaSpy, installed to `~/lib/schemaspy.jar` with the default value of `pgmacs-schemaspy-cmdline`

- Java (available as `java` here)

- [GraphViz](https://graphviz.org/), installable on Debian using `sudo apt install graphviz` for example

- JDBC support for PostgreSQL, here installed in `/usr/share/java/postgresql-jdbc4.jar`,
installable on Debian for example using `sudo apt install libpostgresql-jdbc-java`



52 changes: 51 additions & 1 deletion pgmacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ PostgreSQL over a slow network link."
:type 'boolean
:group 'pgmacs)

;; To run SchemaSpy on the current database and display images describing the schema. Requires the
;; following software to be installed:
;;
;; - SchemaSpy (here to ~/lib/schemaspy.jar, see schemaspy.org)
;; - Java (available as "java" here)
;; - GraphViz
;; - JDBC support for PostgreSQL (here in /usr/share/java/postgresql-jdbc4.jar, installable for example
;; using "sudo apt install libpostgresql-jdbc-java")
(defcustom pgmacs-schemaspy-cmdline
"java -jar ~/lib/schemaspy.jar -dp /usr/share/java/postgresql-jdbc4.jar -t pgsql11 -host %h -port %P -u %u -p %p -db %d -s %s -i %t -imageformat svg -o /tmp/schema"
"Commandline for running SchemaSpy java application.
In this commandline, %d is replaced by the database name, %h by
the hostname on which PostgreSQL is running, %P by the port it is
running on, %u by the user, %p by the password, %s by the current
table schema and %t by the current table name."
:type 'string
:group 'pgmacs)

(defcustom pgmacs-header-line
(list (concat (when (display-graphic-p) "🐘") "PGmacs"))
"Header-line to use in PGmacs buffers. Nil to disable."
Expand All @@ -105,7 +123,7 @@ PostgreSQL over a slow network link."
(cl-third ci)))
(:local
(format "%s as %s on Unix socket"
(propertize (cl-fourth ci) 'face bold)
(propertize (cl-fourth ci) 'face 'bold)
(cl-fifth ci))))))))))

(defcustom pgmacs-mode-hook 'pgmacs--update-header-line
Expand Down Expand Up @@ -1495,6 +1513,7 @@ value, in the limit of pgmacs-row-limit."
"y" pgmacs--yank-row
"e" pgmacs-run-sql
"E" pgmacs-run-buffer-sql
"S" pgmacs--schemaspy-table
"r" pgmacs--redraw-pgmacstbl
"j" pgmacs--row-as-json
;; "n" and "p" are bound when table is paginated to next/prev page
Expand Down Expand Up @@ -1949,6 +1968,37 @@ Uses PostgreSQL connection CON."
(shrink-window-if-larger-than-buffer)
(goto-char (point-min)))))

;; Run SchemaSpy on the current table, display the SVG.
(defun pgmacs--schemaspy-table (&rest _ignore)
(interactive)
(unless (display-graphic-p)
(error "SchemaSpy will only work on a graphical terminal"))
(unless (image-type-available-p 'svg)
(error "SchemaSpy support needs SVG support in your Emacs"))
(let ((ci (pgcon-connect-info pgmacs--con))
(schema-name (if (pg-qualified-name-p pgmacs--table)
(pg-qualified-name-schema pgmacs--table)
"public"))
(table-name (if (pg-qualified-name-p pgmacs--table)
(pg-qualified-name-name pgmacs--table)
pgmacs--table)))
(when (eql :local (cl-first ci))
(message "Replacing Unix connection by network connection to localhost for SchemaSpy"))
(let ((cmd (cl-multiple-value-bind (type host port dbname user password) ci
(let ((spec (list (cons ?h (if (eq type :local) "localhost" host))
(cons ?P (or port 5432))
(cons ?d dbname)
(cons ?u user)
(cons ?p password)
(cons ?s schema-name)
(cons ?t table-name))))
(format-spec pgmacs-schemaspy-cmdline spec))))
(out (format "/tmp/schema/diagrams/tables/%s.1degree.svg" table-name)))
(message "Running cmd %s, output to %s" cmd out)
(shell-command cmd)
(when (file-exists-p out)
(find-file out)))))

;;;###autoload
(defun pgmacs-open (con)
"Browse the contents of PostgreSQL database to which we are connected over CON."
Expand Down

0 comments on commit f2a16db

Please sign in to comment.