├── .gitignore ├── README.md ├── omia_schema.png └── sqlite_dot.awk /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## SQLite3 Schema Visualization 2 | 3 | #### Why? 4 | Was playing with loading [MySQL](https://en.wikipedia.org/wiki/MySQL) 5 | [dumps](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html) from the web into 6 | [SQLite](https://en.wikipedia.org/wiki/Database_dump) today and 7 | I did not feel like dealing with the huge (and getting huger) tool I've used 8 | in the past just to help make a few joins on a DB I have never seen. 9 | 10 | #### What? 11 | This may end up more specialized than I had hoped. 12 | Seems SQLite outputs its schema in the same 'flavor' or 'dialect' it was input. 13 | This worked with a mysql dump off the web translated into SQLite3 with 14 | [mysql2sqlite](https://github.com/dumblob/mysql2sqlite). 15 | But I see it _not_ working with a schema dumped from other databases. oh well. 16 | 17 | 18 | [Database schemas](https://en.wikipedia.org/wiki/Database_schema) are the descriptions of the tables, columns 19 | and details of what they may contain and how they depend on one another. 20 | You can get a running instance of SQLite3 to show you its schema by typing 21 | 22 | .schema 23 | 24 | Staring at that text schema text will give you everything you need to know make queries. 25 | Saving it as a file lets you and search and scroll around, 26 | but I like to see the big picture and get up to speed quickly. 27 | 28 | Turning the schema text into a picture as easily as possible is the goal. 29 | All I need is; the names of tables and columns and the foreign key constraints which let me make joins. 30 | How hard could it be? 31 | 32 | #### How? 33 | Translates the part of the schema we need into the GraphViz [dot](https://graphviz.gitlab.io/_pages/doc/info/lang.html) language, 34 | then view or translate it in any of the ways [GraphViz](http://graphviz.org/about/) allows. 35 | 36 | #### Where? 37 | Where to next? Probably nowhere fast. 38 | If I run into other schema formats I need to parse I will add them. 39 | But these scripts have a way of working for years on end without maintenance. 40 | 41 | 42 | I would still like to see Primary Keys , Unique columns 43 | and Indexes represented somehow. 44 | Even more than that, 45 | I would like to be able to cut and paste names off of a graphical representation 46 | ... so who knows. Maybe this _will_ be a side project for a while. 47 | 48 | 49 | ### Using 50 | 51 | Given you have an appropriately formatted schema dumped from a SQLite database, 52 | here named `dumped_schema.sql` you could 53 | 54 | sqlite_dot.awk dumped_schema.sql > dumped_schema.gv 55 | 56 | 57 | There is the image output from the database I wrote this against included as an image file 58 | 59 | ![omia_schema](omia_schema.png) 60 | 61 | ----------------------------------------- 62 | 63 | Even compressed, the application I'm avoiding 64 | is over TEN THOUSAND times the size of this code 65 | not including a Java environment to run it in 66 | which could push it towards six orders of magnitude more ... 67 | 68 | -------------------------------------------------------------------------------- /omia_schema.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TomConlin/SQLiteViz/1822dc9ddbb1521251c8ba46401907c0c315d4f6/omia_schema.png -------------------------------------------------------------------------------- /sqlite_dot.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/gawk -f 2 | # 3 | # sqlite_dot.awk 4 | # https://github.com/TomConlin/SQLiteViz 5 | # 6 | # Example: 7 | # sqlite_dot.awk dumped_schema.sql > dumped_schema.gv 8 | # 9 | # The .gv file can be turned into an image with: 10 | # dot -Tpng dumped_schema.gv > dumped_schema.png 11 | # 12 | ######################################################### 13 | 14 | # symmetrically remove chars from both ends of a string 15 | function trim(str, n){ 16 | return substr(str, n + 1, length(str) - 2 * n) 17 | } 18 | 19 | 20 | BEGIN {print "digraph \"" ARGV[1] "\" {\n\trankdir = \"LR\"" } 21 | 22 | # begin record 23 | /^CREATE TABLE / { 24 | tab = tolower(trim($3, 1)) 25 | getline 26 | col = tolower(trim($1, 1)) 27 | row = tab "| <" col "> " col 28 | } 29 | 30 | # continue record 31 | /^, `/{ 32 | col = tolower(trim($2, 1)) 33 | row = row " | <" col "> " col 34 | } 35 | 36 | # foreign key makes an edge between columns 37 | /^, CONSTRAINT .* FOREIGN KEY/ { 38 | print "\t\"" tab "\":" trim($6,2) " -> \"" trim($8, 1) "\":" trim($9, 2) ";" 39 | } 40 | 41 | # end record, so no more columns to add. 42 | /^);/ {print "\t\"" tab "\" [\n\t\tlabel = \"" row "\"\n\t\tshape = \"Mrecord\"\n\t];"} 43 | 44 | # ignore for now 45 | # /^, PRIMARY KEY /{} 46 | # /^, UNIQUE/ {} 47 | # /^CREATE INDEX /{} 48 | 49 | END {print "}" } 50 | 51 | --------------------------------------------------------------------------------