Created
September 14, 2011 06:49
-
-
Save AtnNn/1216001 to your computer and use it in GitHub Desktop.
Visualize Persistent Schemas Using Graphviz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE UnicodeSyntax, ViewPatterns #-} | |
-- usage: | |
-- persistent-graph < config/models > schema.dot | |
-- neato schema.dot -Tpdf > schema.pdfimport Database.Persist.Base (EntityDef(..), ColumnDef(..)) | |
import Database.Persist.Quasi (parse) | |
import Data.List (intersperse) | |
main = do | |
defs ← getContents | |
let schema = parse defs | |
putStr $ convert schema | |
graphOpts = "node [shape=record]; overlap=false; splines=true;" | |
convert schema = unlines ["digraph {", graphOpts, unlines $ map entity schema, "}"] | |
entity (EntityDef name _ cols _ _) = unlines $ [ | |
name ++ " [", | |
"label=\"{" ++ name ++ "|" ++ | |
(concat $ intersperse "|" (map column cols)) | |
++ "}\"];"] ++ | |
map (links name) cols | |
column (ColumnDef name _ _) = "<" ++ name ++ "> " ++ name | |
links entity (ColumnDef name typ _) = | |
if "dI" == take 2 (reverse typ) | |
then entity ++ ":" ++ name ++ " -> " ++ reverse (drop 2 (reverse typ)) | |
else [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment