前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dbdot – Golang tool to help generate Postgres schema diagrams

Dbdot – Golang tool to help generate Postgres schema diagrams

作者头像
李海彬
发布2019-05-14 17:25:21
4780
发布2019-05-14 17:25:21
举报
文章被收录于专栏:Golang语言社区Golang语言社区
dbdot

dbdot is a command line tool that generates DOT description from postgres database schema.

dbdot is compiled to platform specific binary, so all you need is the right binary for your machine. Grab the appropriate binary for your architecture from the latest release here.

Demos

Demo 1: Produce DOT for all the tables in postgres db pgguide and user kewluser
代码语言:javascript
复制
$ ./dbdot -dbname=pgguide -user=kewluser                                                                                                       [16:33:31]
digraph  {

	node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">products</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>title</TD><TD>character varying</TD></TR> <TR><TD>price</TD><TD>numeric</TD></TR> <TR><TD>created_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>deleted_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>tags</TD><TD>ARRAY</TD></TR>]</TABLE>>,shape=plaintext] n1;
	node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">purchase_items</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>purchase_id</TD><TD>integer</TD></TR> <TR><TD>product_id</TD><TD>integer</TD></TR> <TR><TD>price</TD><TD>numeric</TD></TR> <TR><TD>quantity</TD><TD>integer</TD></TR> <TR><TD>state</TD><TD>character varying</TD></TR>]</TABLE>>,shape=plaintext] n2;
	node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">users</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>email</TD><TD>character varying</TD></TR> <TR><TD>password</TD><TD>character varying</TD></TR> <TR><TD>details</TD><TD>USER-DEFINED</TD></TR> <TR><TD>created_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>deleted_at</TD><TD>timestamp with time zone</TD></TR>]</TABLE>>,shape=plaintext] n3;
	node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">purchases</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>created_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>name</TD><TD>character varying</TD></TR> <TR><TD>address</TD><TD>character varying</TD></TR> <TR><TD>state</TD><TD>character varying</TD></TR> <TR><TD>zipcode</TD><TD>integer</TD></TR> <TR><TD>user_id</TD><TD>integer</TD></TR>]</TABLE>>,shape=plaintext] n4;
	n2->n1;
	n2->n4;
	n4->n3;

}
Demo 2: Produce a schema diagram for all the tables in pgguide db for kewluser
代码语言:javascript
复制
$ ./dbdot -dbname=pgguide -user=kewluser > test.dot && dot -Tpng test.dot -o outfile.png && open outfile.png

Above command pipes the DOT description into test.dot and invokes dot cli tool to transform test.dot to a outfile.png. Here's what outfile.png looks like:

Demo 3: Whitelist tables and produce schema diagram for them

代码语言:javascript
复制
./dbdot -dbname=pgguide -user=kewluser > test.dot --whitelist=purchase_items,purchases && dot -Tpng test.dot -o outfile-whitelisted.png && open outfile-whitelisted.png

Here's what outfile-whitelisted.png looks like:

Flags

dbdot currently supports the following flags:

代码语言:javascript
复制
  -W    ask for password
  -dbname string
        dbname for which you want to generate dot file
  -host string
        database host (default "localhost")
  -port uint
        database port (default 5432)
  -schema string
        schema name (default "public")
  -sslmode
        enable sslmode for postgres db connection
  -user string
        username of postgres db
  -whitelist string
        comma separated list of tables you want to generate dot file for

TODO

Here's some features I would like to have for this project:

  • Support connection string. What happens if the user doesn't have access to db?
  • Add support for more db types.
  • Prettify output.
  • Add ability to whitelist columns in a table. Users should be able whitelist columns per table. But this starts getting into territory of language design. i.e. what kind of cli syntax should dbdot support. Hence this is the last item in my list.

Inspiration

A while back I wanted a simple tool that would just spit out schema for tables that I wanted. A lot of tools I found were way too powerful, requiring a zillion installation and configuration. This inspired me to write a simple self contained tool that was laser focused on just reading schema and spitting out DOT.

Similar projects

  • http://schemaspy.org/
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Demos
  • Flags
  • TODO
  • Inspiration
  • Similar projects
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档