-
-
Save mdeweerd/c5c4343b428a6df6d8e075fc70af7b2f to your computer and use it in GitHub Desktop.
A simple template for PlantUML to draw ER diagram.The basic idea comes from http://plantuml.sourceforge.net/qa/?qa=331/database-modeling
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
@startuml | |
' uncomment the line below if you're using computer with a retina display | |
' skinparam dpi 300 | |
!define Table(name,desc) class name as "desc" << (T,#FFAAAA) >> | |
' we use bold for primary key | |
' green color for unique | |
' and underscore for not_null | |
!define primary_key(x) <b>x</b> | |
!define unique(x) <color:green>x</color> | |
!define not_null(x) <u>x</u> | |
' other tags available: | |
' <i></i> | |
' <back:COLOR></color>, where color is a color name or html color code | |
' (#FFAACC) | |
' see: http://plantuml.com/classes.html#More | |
hide methods | |
hide stereotypes | |
' entities | |
Table(user, "user\n(User in our system)") { | |
primary_key(id) INTEGER | |
not_null(unique(username)) VARCHAR[32] | |
not_null(password) VARCHAR[64] | |
} | |
Table(session, "session\n(session for user)") { | |
primary_key(id) INTEGER | |
not_null(user_id) INTEGER | |
not_null(unique(session_id)) VARCHAR[64] | |
} | |
Table(user_profile, "user_profile\n(Some info of user)") { | |
primary_key(user_id) INTEGER | |
age SMALLINT | |
gender SMALLINT | |
birthday DATETIME | |
} | |
Table(group, "group\n(group of users)") { | |
primary_key(id) INTEGER | |
not_null(name) VARCHAR[32] | |
} | |
Table(user_group, "user_group\n(relationship of user and group)") { | |
primary_key(user_id) INTEGER | |
primary_key(group_id) INTEGER | |
joined_at DATETIME | |
} | |
' relationships | |
' one-to-one relationship | |
user -- user_profile : "A user only \nhas one profile" | |
' one to may relationship | |
user --> session : "A user may have\n many sessions" | |
' many to many relationship | |
' Add mark if you like | |
user "1" --> "*" user_group : "A user may be \nin many groups" | |
group "1" --> "0..N" user_group : "A group may \ncontain many users" | |
@enduml |
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
#/bin/bash -xv | |
# Alternative plantuml generation | |
DB=$1 | |
DB_SQLOUT=${DB%.*}.sql | |
DB_PLANTUML=${DB%.*}.plantuml | |
sqlite3 ${DB} <<ENDDUMP | |
.output $DB_SQLOUT | |
.dump | |
.exit | |
ENDDUMP | |
cat > $DB_PLANTUML <<'PLANTHEADEND' | |
@startuml | |
' Template from https://www.red-gate.com/simple-talk/sql/sql-tools/automatically-creating-uml-database-diagrams-for-sql-server/ | |
' uncomment the line below if you're using computer with a retina display | |
'skinparam dpi 300 | |
!define table(x,desc,group) class group.x as "desc" << (T,mistyrose) >> | |
!define view(x) class x << (V,lightblue) >> | |
!define tr(x) class x << (R,red) >> | |
!define tf(x) class x << (F,darkorange) >> | |
!define af(x) class x << (F,white) >> | |
!define fn(x) class x << (F,plum) >> | |
!define fs(x) class x << (F,tan) >> | |
!define ft(x) class x << (F,wheat) >> | |
!define if(x) class x << (F,gaisboro) >> | |
!define p(x) class x << (P,indianred) >> | |
!define pc(x) class x << (P,lemonshiffon) >> | |
!define x(x) class x << (P,linen) >> | |
hide methods | |
hide stereotypes | |
skinparam classarrowcolor gray | |
' entities | |
PLANTHEADEND | |
# Append table definitions | |
perl -n -e ' | |
if(/CREATE TABLE (?:IF NOT EXISTS )?"?(\S+?)"?\((.*)\);/i) { | |
print "table($1,$1,db) {\n"; | |
@fieldSpecs=split(/,(?=[^0-9])/,$2); | |
foreach my $field (@fieldSpecs) { | |
$field=~s/\(/<U+0028>/g; | |
$field=~s/\)/<U+0029>/g; | |
if($field=~/("?\S+"?) (.*) primary key/) { | |
print " $1: $2 <<pk>>\n"; | |
} elsif($field=~/(\S+) (.*)/) { | |
print " $1: $2\n"; | |
} else { | |
print " $field\n"; | |
} | |
} | |
print "}\n\n"; | |
} | |
' $DB_SQLOUT >> $DB_PLANTUML | |
# Append example for manually adding relationships | |
cat >> $DB_PLANTUML<<'ENDOFRELATIONS' | |
' relationships | |
' | |
' Examples: | |
' one-to-one relationship | |
' Example: user -- user_profile : "A user only \nhas one profile" | |
' one to may relationship | |
' Example: user --> session : "A user may have\n many sessions" | |
' many to many relationship | |
' Add mark if you like | |
' Example: user "1" --> "*" user_group : "A user may be \nin many groups" | |
' Example : group "1" --> "0..N" user_group : "A group may \ncontain many users" | |
@enduml | |
ENDOFRELATIONS |
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
#/bin/bash | |
# Quick script to generate PlantUml from SQLITE3 database. | |
# Licence: Public Domain - Created by Ynamics . | |
DB=$1 | |
DB_SQLOUT=${DB%.*}.sql | |
DB_PLANTUML=${DB%%.*}.plantuml | |
sqlite3 ${DB} <<ENDDUMP | |
.output $DB_SQLOUT | |
.dump | |
.exit | |
ENDDUMP | |
cat > $DB_PLANTUML <<'PLANTHEADEND' | |
@startuml | |
' Template from https://gist.github.com/QuantumGhost/0955a45383a0b6c0bc24f9654b3cb561 | |
' uncomment the line below if you're using computer with a retina display | |
'skinparam dpi 300 | |
!define Table(name,desc) class name as "desc" << (T,#FFAAAA) >> | |
' we use bold for primary key | |
' green color for unique | |
' and underscore for not_null | |
!define primary_key(x) <b>x</b> | |
!define unique(x) <color:green>x</color> | |
!define not_null(x) <u>x</u> | |
' other tags available: | |
' <i></i> | |
' <back:COLOR></color>, where color is a color name or html color code | |
' (#FFAACC) | |
' see: http://plantuml.com/classes.html#More | |
hide methods | |
hide stereotypes | |
' entities | |
PLANTHEADEND | |
# Append table definitions | |
perl -n -e ' | |
if(/CREATE TABLE (\S+)\((.*)\);/) { | |
print "Table($1,\"$1\") {\n"; | |
@fieldSpecs=split(/,/,$2); | |
foreach my $field (@fieldSpecs) { | |
if($field=~/(\S+) (.*) primary key/) { | |
print " primary_key($1) $2\n"; | |
} else { | |
print " $field\n"; | |
} | |
} | |
print "}\n\n"; | |
} | |
' $DB_SQLOUT >> $DB_PLANTUML | |
# Append example for manually adding relationships | |
cat >> $DB_PLANTUML<<'ENDOFRELATIONS' | |
' relationships | |
' | |
' Examples: | |
' one-to-one relationship | |
' Example: user -- user_profile : "A user only \nhas one profile" | |
' one to may relationship | |
' Example: user --> session : "A user may have\n many sessions" | |
' many to many relationship | |
' Add mark if you like | |
' Example: user "1" --> "*" user_group : "A user may be \nin many groups" | |
' Example : group "1" --> "0..N" user_group : "A group may \ncontain many users" | |
@enduml | |
ENDOFRELATIONS |
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
@startuml | |
' uncomment the line below if you're using computer with a retina display | |
' skinparam dpi 300 | |
!define Table(name,desc) class name as "desc" << (T,#FFAAAA) >> | |
' we use bold for primary key | |
' green color for unique | |
' and underscore for not_null | |
!define primary_key(x) <b>x</b> | |
!define unique(x) <color:green>x</color> | |
!define not_null(x) <u>x</u> | |
' other tags available: | |
' <i></i> | |
' <back:COLOR></color>, where color is a color name or html color code | |
' (#FFAACC) | |
' see: http://plantuml.com/classes.html#More | |
hide methods | |
hide stereotypes | |
' entities | |
' relationships | |
@enduml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment