Skip to content

Instantly share code, notes, and snippets.

@AtnNn
Created September 14, 2011 06:49
Show Gist options
  • Save AtnNn/1216001 to your computer and use it in GitHub Desktop.
Save AtnNn/1216001 to your computer and use it in GitHub Desktop.
Visualize Persistent Schemas Using Graphviz
{-# 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