Skip to content

RadhiFadlillah/sqldiagram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLDiagram PayPal Ko-fi

SQLDiagram is CLI app to generate ERD from SQL files. It's inspired by service like DBDiagram or DrawSQL except its offline. It works by parsing your SQL files then convert it to ERD using D2 diagramming language.

Table of Contents

Installation

If go already installed on your system, and $GOBIN is added to your $PATH, then you can install it by simply running:

go install -v github.com/RadhiFadlillah/sqldiagram

Features

  • Works offline.
  • Has decent styling, thanks to D2.
  • Directly parse SQL language so you don't have to learn a new diagramming language.
  • Allow you to group tables in the diagram for better readability, by separating the SQL queries into several files.

Limitations

  • Currently only support MySQL dialect, so no PostgreSQL.
  • Relations only drawn between table and not between columns, which is because of limitations in D2.
  • Sometimes the relational lines will drawn above tables, which again is because of limitations in D2.
  • The output is only in raw D2 language or SVG, so if you need PNG, you need to manually convert it.
  • The relationship is only drawn as normal arrow.

Besides those point, SQLDiagram currently also only support limited DDL commands:

  • CREATE TABLE
  • DROP TABLE
  • ALTER TABLE DROP COLUMNS
  • ALTER TABLE ADD COLUMNS
  • ALTER TABLE RENAME COLUMNS
  • ALTER TABLE CHANGE COLUMNS
  • ALTER TABLE ADD CONSTRAINT

Usage

NAME:
   sqldiagram - generate ERD from SQL file(s) as SVG file

USAGE:
   sqldiagram command [command options] <input-1> ... <input-n>

COMMANDS:
   mysql    generate ERD from MySQL dialect
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h  show help

Each commands has its own help, so you should check it as well. Here is the help for mysql command:

NAME:
   sqldiagram mysql - generate ERD from MySQL dialect

USAGE:
   sqldiagram mysql [command options] [arguments...]

OPTIONS:
   --no-group, --ng                don't render separate file as group (default: false)
   --raw-d2, --raw                 render as raw D2 scripts (default: false)
   --output value, -o value        write to specified path (if empty will use stdout)
   --direction value, --dir value  direction of chart (up|down|right|left, default right)
   --help, -h                      show help

By default it will print the output to stdout, so you could either redirect it:

sqldiagram mysql file.sql > erd.svg

or you can specify the output file:

sqldiagram mysql -o erd.svg file.sql

Examples

Basic Diagram

Here is the generated diagram for Wordpress database, taken from DrawSQL template. To recreate it, download wordpress.sql in example/basic, then run:

sqldiagram --dir down wordpress.sql > erd.svg

Wordpress

Grouped Diagrams

Here is the generated diagram for the same Wordpress database, but now is grouped. To recreate it, download directory example/grouped, then run:

sqldiagram --dir right grouped > erd.svg

Wordpress Grouped

Using TALA Layout

Besides using the open source layout engine, D2 also has its own proprietary layout engine named TALA. In some case, it will give a better and prettier diagram than the open source engine. Unfortunately, its not free and it doesn't provide API to access it directly from Go language.

If you want to use it, you need to generate the layout in D2 language using --raw flag, then copy the result to D2 playground. Do note that you will miss some styling which we done manually in SVG because table styling in D2 is still lacking. For example, here is the diagram for Wordpress using TALA engine:

Wordpress with TALA layout

And here is the grouped diagram using TALA engine:

Wordpress with TALA layout and grouped

Licenses

SQLDiagram is distributed under MIT license, which means you can use and modify it however you want. However, if you make an enhancement for it, if possible, please send a pull request. If you like this project, please consider donating to me either via PayPal or Ko-Fi.

About

CLI to generate Entity Relationship Diagram from SQL file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages