diff --git a/db/db.go b/db/db.go index 611c6dd98..1fc84e849 100644 --- a/db/db.go +++ b/db/db.go @@ -26,6 +26,7 @@ func Analyze(urlstr string) (*schema.Schema, error) { s.Name = splitted[1] db, err := dburl.Open(urlstr) + defer db.Close() if err != nil { return s, err } diff --git a/drivers/mysql/mysql.go b/drivers/mysql/mysql.go index 91d7156b9..69b3db71f 100644 --- a/drivers/mysql/mysql.go +++ b/drivers/mysql/mysql.go @@ -42,6 +42,47 @@ SELECT table_name, table_type, table_comment FROM information_schema.tables WHER Comment: tableComment, } + // table definition + if tableType == "BASE TABLE" { + tableDefRows, err := db.Query(fmt.Sprintf("SHOW CREATE TABLE %s", tableName)) + defer tableDefRows.Close() + if err != nil { + return err + } + for tableDefRows.Next() { + var ( + tableName string + tableDef string + ) + err := tableDefRows.Scan(&tableName, &tableDef) + if err != nil { + return err + } + table.Def = tableDef + } + } + + // view definition + if tableType == "VIEW" { + viewDefRows, err := db.Query(` +SELECT view_definition FROM information_schema.views +WHERE table_schema = ? +AND table_name = ?; + `, s.Name, tableName) + defer viewDefRows.Close() + if err != nil { + return err + } + for viewDefRows.Next() { + var tableDef string + err := viewDefRows.Scan(&tableDef) + if err != nil { + return err + } + table.Def = fmt.Sprintf("CREATE VIEW %s AS (%s)", tableName, tableDef) + } + } + // indexes indexRows, err := db.Query(` SELECT diff --git a/drivers/mysql/mysql_test.go b/drivers/mysql/mysql_test.go new file mode 100644 index 000000000..223e681af --- /dev/null +++ b/drivers/mysql/mysql_test.go @@ -0,0 +1,38 @@ +package mysql + +import ( + "database/sql" + _ "github.com/go-sql-driver/mysql" + "github.com/k1LoW/tbls/schema" + "github.com/xo/dburl" + "os" + "testing" +) + +var s *schema.Schema +var db *sql.DB + +func TestMain(m *testing.M) { + s = &schema.Schema{ + Name: "testdb", + } + db, _ = dburl.Open("my://root:mypass@localhost:33306/testdb") + defer db.Close() + exit := m.Run() + if exit != 0 { + os.Exit(exit) + } +} + +func TestAnalyzeView(t *testing.T) { + driver := new(Mysql) + err := driver.Analyze(db, s) + if err != nil { + t.Errorf("%v", err) + } + view, _ := s.FindTableByName("post_comments") + expected := view.Def + if expected == "" { + t.Errorf("actual not empty string.") + } +} diff --git a/drivers/postgres/postgres.go b/drivers/postgres/postgres.go index bcae4494b..26143d545 100644 --- a/drivers/postgres/postgres.go +++ b/drivers/postgres/postgres.go @@ -67,6 +67,27 @@ AND ps.relname = $1`, tableName) table.Comment = tableComment } + // view definition + if tableType == "VIEW" { + viewDefRows, err := db.Query(` +SELECT view_definition FROM information_schema.views +WHERE table_catalog = $1 +AND table_name = $2; + `, s.Name, tableName) + defer viewDefRows.Close() + if err != nil { + return err + } + for viewDefRows.Next() { + var tableDef string + err := viewDefRows.Scan(&tableDef) + if err != nil { + return err + } + table.Def = fmt.Sprintf("CREATE VIEW %s AS (\n%s\n)", tableName, strings.TrimRight(tableDef, ";")) + } + } + // indexes indexRows, err := db.Query(` SELECT diff --git a/drivers/postgres/postgres_test.go b/drivers/postgres/postgres_test.go new file mode 100644 index 000000000..e9a5eb810 --- /dev/null +++ b/drivers/postgres/postgres_test.go @@ -0,0 +1,38 @@ +package postgres + +import ( + "database/sql" + "github.com/k1LoW/tbls/schema" + _ "github.com/lib/pq" + "github.com/xo/dburl" + "os" + "testing" +) + +var s *schema.Schema +var db *sql.DB + +func TestMain(m *testing.M) { + s = &schema.Schema{ + Name: "testdb", + } + db, _ = dburl.Open("pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable") + defer db.Close() + exit := m.Run() + if exit != 0 { + os.Exit(exit) + } +} + +func TestAnalyzeView(t *testing.T) { + driver := new(Postgres) + err := driver.Analyze(db, s) + if err != nil { + t.Errorf("%v", err) + } + view, _ := s.FindTableByName("post_comments") + expected := view.Def + if expected == "" { + t.Errorf("actual not empty string.") + } +} diff --git a/output/dot/templates.go b/output/dot/templates.go index 85dc2a3a3..c69f9f564 100644 --- a/output/dot/templates.go +++ b/output/dot/templates.go @@ -10,20 +10,20 @@ var _Assets5bd148e6149bb9adcdddfcf8cc46d6e3047dbe26 = "digraph {{ .Table.Name }} var _Assets21532ae17ad95976ac467eeaeab81f2bb1d537e4 = "digraph \"{{ .Schema.Name }}\" {\n // Config\n graph [rankdir=TB, layout=dot, fontname=\"Arial\"];\n node [shape=record, fontsize=14, margin=0.6, fontname=\"Arial\"];\n edge [fontsize=10, labelfloat=false, splines=none, fontname=\"Arial\"];\n\n // Tables\n {{- range $i, $t := .Schema.Tables }}\n {{ $t.Name }} [shape=none, label=<\n \n {{- range $ii, $c := $t.Columns }}\n \n {{- end }}\n
{{ $t.Name | html }} [{{ $t.Type | html }}]
{{ $c.Name | html }} [{{ $c.Type | html }}]
>];\n {{- end }}\n\n // Relations\n {{- range $j, $r := .Schema.Relations }}\n {{ $r.Table.Name }}:{{ $c := index $r.Columns 0 }}{{ $c.Name }} -> {{ $r.ParentTable.Name }}:{{ $pc := index $r.ParentColumns 0 }}{{ $pc.Name }} [dir=back, arrowtail=crow, {{ if $r.IsAdditional }}style=\"dashed\",{{ end }} taillabel=<
{{ $r.Def | html }}
>];\n {{- end }}\n}\n" // Assets returns go-assets FileSystem -var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"table.dot.tmpl", "schema.dot.tmpl"}}, map[string]*assets.File{ - "/schema.dot.tmpl": &assets.File{ - Path: "/schema.dot.tmpl", - FileMode: 0x1a4, - Mtime: time.Unix(1528193744, 1528193744646815118), - Data: []byte(_Assets21532ae17ad95976ac467eeaeab81f2bb1d537e4), - }, "/": &assets.File{ +var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"schema.dot.tmpl", "table.dot.tmpl"}}, map[string]*assets.File{ + "/": &assets.File{ Path: "/", FileMode: 0x800001ed, - Mtime: time.Unix(1528193744, 1528193744647833498), + Mtime: time.Unix(1528197635, 1528197635000000000), Data: nil, + }, "/schema.dot.tmpl": &assets.File{ + Path: "/schema.dot.tmpl", + FileMode: 0x1a4, + Mtime: time.Unix(1528197635, 1528197635000000000), + Data: []byte(_Assets21532ae17ad95976ac467eeaeab81f2bb1d537e4), }, "/table.dot.tmpl": &assets.File{ Path: "/table.dot.tmpl", FileMode: 0x1a4, - Mtime: time.Unix(1527750838, 1527750838424881150), + Mtime: time.Unix(1527689928, 1527689928000000000), Data: []byte(_Assets5bd148e6149bb9adcdddfcf8cc46d6e3047dbe26), }}, "") diff --git a/output/md/templates.go b/output/md/templates.go index 2ecee7510..319033294 100644 --- a/output/md/templates.go +++ b/output/md/templates.go @@ -7,23 +7,23 @@ import ( ) var _Assets43889384df1c6f74d764c29d91b9d5637eb46061 = "# {{ .Schema.Name }}\n\n## Tables\n\n| Name | Columns | Comment | Type |\n| ---- | ------- | ------- | ---- |\n{{- range $i, $t := .Schema.Tables }}\n| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment }} | {{ $t.Type }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er](schema.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)" -var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n## Description\n\n{{ .Table.Comment }}\n\n## Columns\n\n| Name | Type | Default | Nullable | Children | Parents | Comment |\n| ---- | ---- | ------- | -------- | -------- | ------- | ------- |\n{{- range $i, $c := .Table.Columns }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment }} |\n{{- end }}\n\n## Constraints\n\n| Name | Type | Def |\n| ---- | ---- | --- |\n{{- range $i, $c := .Table.Constraints }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Def }} |\n{{- end }}\n\n## Indexes\n\n| Name | Def |\n| ---- | --- |\n{{- range $i, $idx := .Table.Indexes }}\n| {{ $idx.Name }} | {{ $idx.Def }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er]({{ .Table.Name }}.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)" +var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n## Description\n\n{{ .Table.Comment -}}\n{{ if .Table.Def }}\n
\nTable Definition\n\n```sql\n{{ .Table.Def }}\n```\n\n
\n{{ end }}\n\n## Columns\n\n| Name | Type | Default | Nullable | Children | Parents | Comment |\n| ---- | ---- | ------- | -------- | -------- | ------- | ------- |\n{{- range $i, $c := .Table.Columns }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment }} |\n{{- end }}\n\n## Constraints\n\n| Name | Type | Def |\n| ---- | ---- | --- |\n{{- range $i, $c := .Table.Constraints }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Def }} |\n{{- end }}\n\n## Indexes\n\n| Name | Def |\n| ---- | --- |\n{{- range $i, $idx := .Table.Indexes }}\n| {{ $idx.Name }} | {{ $idx.Def }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er]({{ .Table.Name }}.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)" // Assets returns go-assets FileSystem var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"index.md.tmpl", "table.md.tmpl"}}, map[string]*assets.File{ - "/": &assets.File{ + "/table.md.tmpl": &assets.File{ + Path: "/table.md.tmpl", + FileMode: 0x1a4, + Mtime: time.Unix(1528198538, 1528198538000000000), + Data: []byte(_Assetsac44302fb6150a621aa9d04a0350aac972bf7e18), + }, "/": &assets.File{ Path: "/", FileMode: 0x800001ed, - Mtime: time.Unix(1527684406, 1527684406000000000), + Mtime: time.Unix(1528198538, 1528198538000000000), Data: nil, }, "/index.md.tmpl": &assets.File{ Path: "/index.md.tmpl", FileMode: 0x1a4, Mtime: time.Unix(1527684406, 1527684406000000000), Data: []byte(_Assets43889384df1c6f74d764c29d91b9d5637eb46061), - }, "/table.md.tmpl": &assets.File{ - Path: "/table.md.tmpl", - FileMode: 0x1a4, - Mtime: time.Unix(1527684406, 1527684406000000000), - Data: []byte(_Assetsac44302fb6150a621aa9d04a0350aac972bf7e18), }}, "") diff --git a/output/md/templates/table.md.tmpl b/output/md/templates/table.md.tmpl index 5235002de..c36270991 100644 --- a/output/md/templates/table.md.tmpl +++ b/output/md/templates/table.md.tmpl @@ -2,7 +2,17 @@ ## Description -{{ .Table.Comment }} +{{ .Table.Comment -}} +{{ if .Table.Def }} +
+Table Definition + +```sql +{{ .Table.Def }} +``` + +
+{{ end }} ## Columns diff --git a/sample/mysql/CamelizeTable.md b/sample/mysql/CamelizeTable.md index f59b41a33..b25baaa44 100644 --- a/sample/mysql/CamelizeTable.md +++ b/sample/mysql/CamelizeTable.md @@ -3,6 +3,19 @@ ## Description +
+Table Definition + +```sql +CREATE TABLE `CamelizeTable` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `created` datetime NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +``` + +
+ ## Columns diff --git a/sample/mysql/comment_stars.md b/sample/mysql/comment_stars.md index eda5010e4..4c20eca8c 100644 --- a/sample/mysql/comment_stars.md +++ b/sample/mysql/comment_stars.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```sql +CREATE TABLE `comment_stars` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `comment_post_id` bigint(20) NOT NULL, + `comment_user_id` int(11) NOT NULL, + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', + PRIMARY KEY (`id`), + UNIQUE KEY `user_id` (`user_id`,`comment_post_id`,`comment_user_id`), + KEY `comment_stars_user_id_post_id_fk` (`comment_post_id`,`comment_user_id`), + KEY `comment_stars_user_id_fk` (`comment_user_id`), + CONSTRAINT `comment_stars_user_id_fk` FOREIGN KEY (`comment_user_id`) REFERENCES `users` (`id`), + CONSTRAINT `comment_stars_user_id_post_id_fk` FOREIGN KEY (`comment_post_id`, `comment_user_id`) REFERENCES `comments` (`post_id`, `user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +``` + +
+ ## Columns diff --git a/sample/mysql/comments.md b/sample/mysql/comments.md index 32195abac..1deb6b818 100644 --- a/sample/mysql/comments.md +++ b/sample/mysql/comments.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```sql +CREATE TABLE `comments` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `post_id` bigint(20) NOT NULL, + `user_id` int(11) NOT NULL, + `comment` text NOT NULL, + `created` datetime NOT NULL, + `updated` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `post_id` (`post_id`,`user_id`), + KEY `comments_user_id_fk` (`user_id`), + KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`) USING HASH, + CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`), + CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +``` + +
+ ## Columns diff --git a/sample/mysql/logs.md b/sample/mysql/logs.md index c5dcdbf3f..505bccc55 100644 --- a/sample/mysql/logs.md +++ b/sample/mysql/logs.md @@ -3,6 +3,24 @@ ## Description audit log table +
+Table Definition + +```sql +CREATE TABLE `logs` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `post_id` bigint(20) DEFAULT NULL, + `comment_id` bigint(20) DEFAULT NULL, + `comment_star_id` bigint(20) DEFAULT NULL, + `payload` text, + `created` datetime NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +``` + +
+ ## Columns diff --git a/sample/mysql/post_comments.md b/sample/mysql/post_comments.md index f38fc5b76..26d8b3399 100644 --- a/sample/mysql/post_comments.md +++ b/sample/mysql/post_comments.md @@ -3,6 +3,15 @@ ## Description post and comments View table +
+Table Definition + +```sql +CREATE VIEW post_comments AS ((select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2`.`username` AS `post_user`,`c`.`comment` AS `comment`,`u2`.`username` AS `comment_user`,`c`.`created` AS `created`,`c`.`updated` AS `updated` from (((`testdb`.`posts` `p` left join `testdb`.`comments` `c` on((`p`.`id` = `c`.`post_id`))) left join `testdb`.`users` `u` on((`u`.`id` = `p`.`user_id`))) left join `testdb`.`users` `u2` on((`u2`.`id` = `c`.`user_id`))))) +``` + +
+ ## Columns diff --git a/sample/mysql/posts.md b/sample/mysql/posts.md index bbd2ab409..a63522cc2 100644 --- a/sample/mysql/posts.md +++ b/sample/mysql/posts.md @@ -3,6 +3,27 @@ ## Description Posts table +
+Table Definition + +```sql +CREATE TABLE `posts` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `title` varchar(255) NOT NULL, + `body` text NOT NULL, + `post_type` enum('public','private','draft') NOT NULL COMMENT 'public/private/draft', + `created` datetime NOT NULL, + `updated` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `user_id` (`user_id`,`title`), + KEY `posts_user_id_idx` (`id`) USING BTREE, + CONSTRAINT `posts_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Posts table' +``` + +
+ ## Columns diff --git a/sample/mysql/users.md b/sample/mysql/users.md index b5c8a6f83..9a9ff5be2 100644 --- a/sample/mysql/users.md +++ b/sample/mysql/users.md @@ -3,6 +3,25 @@ ## Description Users table +
+Table Definition + +```sql +CREATE TABLE `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(50) NOT NULL, + `password` varchar(50) NOT NULL, + `email` varchar(355) NOT NULL COMMENT 'ex. user@example.com', + `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', + PRIMARY KEY (`id`), + UNIQUE KEY `username` (`username`), + UNIQUE KEY `email` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Users table' +``` + +
+ ## Columns diff --git a/sample/mysql8/CamelizeTable.md b/sample/mysql8/CamelizeTable.md index f59b41a33..062ec3230 100644 --- a/sample/mysql8/CamelizeTable.md +++ b/sample/mysql8/CamelizeTable.md @@ -3,6 +3,19 @@ ## Description +
+Table Definition + +```sql +CREATE TABLE `CamelizeTable` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `created` datetime NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci +``` + +
+ ## Columns diff --git a/sample/mysql8/comment_stars.md b/sample/mysql8/comment_stars.md index 7b329795a..707ddafd0 100644 --- a/sample/mysql8/comment_stars.md +++ b/sample/mysql8/comment_stars.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```sql +CREATE TABLE `comment_stars` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `comment_post_id` bigint(20) NOT NULL, + `comment_user_id` int(11) NOT NULL, + `created` timestamp NOT NULL, + `updated` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `user_id` (`user_id`,`comment_post_id`,`comment_user_id`), + KEY `comment_stars_user_id_post_id_fk` (`comment_post_id`,`comment_user_id`), + KEY `comment_stars_user_id_fk` (`comment_user_id`), + CONSTRAINT `comment_stars_user_id_fk` FOREIGN KEY (`comment_user_id`) REFERENCES `users` (`id`), + CONSTRAINT `comment_stars_user_id_post_id_fk` FOREIGN KEY (`comment_post_id`, `comment_user_id`) REFERENCES `comments` (`post_id`, `user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci +``` + +
+ ## Columns diff --git a/sample/mysql8/comments.md b/sample/mysql8/comments.md index 32195abac..9871f43a3 100644 --- a/sample/mysql8/comments.md +++ b/sample/mysql8/comments.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```sql +CREATE TABLE `comments` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `post_id` bigint(20) NOT NULL, + `user_id` int(11) NOT NULL, + `comment` text NOT NULL, + `created` datetime NOT NULL, + `updated` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `post_id` (`post_id`,`user_id`), + KEY `comments_user_id_fk` (`user_id`), + KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`), + CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`), + CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci +``` + +
+ ## Columns diff --git a/sample/mysql8/logs.md b/sample/mysql8/logs.md index c5dcdbf3f..dfb85056d 100644 --- a/sample/mysql8/logs.md +++ b/sample/mysql8/logs.md @@ -3,6 +3,24 @@ ## Description audit log table +
+Table Definition + +```sql +CREATE TABLE `logs` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `post_id` bigint(20) DEFAULT NULL, + `comment_id` bigint(20) DEFAULT NULL, + `comment_star_id` bigint(20) DEFAULT NULL, + `payload` text, + `created` datetime NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci +``` + +
+ ## Columns diff --git a/sample/mysql8/post_comments.md b/sample/mysql8/post_comments.md index f38fc5b76..78d4f6fa9 100644 --- a/sample/mysql8/post_comments.md +++ b/sample/mysql8/post_comments.md @@ -3,6 +3,15 @@ ## Description post and comments View table +
+Table Definition + +```sql +CREATE VIEW post_comments AS (select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2`.`username` AS `post_user`,`c`.`comment` AS `comment`,`u2`.`username` AS `comment_user`,`c`.`created` AS `created`,`c`.`updated` AS `updated` from (((`testdb`.`posts` `p` left join `testdb`.`comments` `c` on((`p`.`id` = `c`.`post_id`))) left join `testdb`.`users` `u` on((`u`.`id` = `p`.`user_id`))) left join `testdb`.`users` `u2` on((`u2`.`id` = `c`.`user_id`)))) +``` + +
+ ## Columns diff --git a/sample/mysql8/posts.md b/sample/mysql8/posts.md index bbd2ab409..b0a833ef7 100644 --- a/sample/mysql8/posts.md +++ b/sample/mysql8/posts.md @@ -3,6 +3,27 @@ ## Description Posts table +
+Table Definition + +```sql +CREATE TABLE `posts` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `title` varchar(255) NOT NULL, + `body` text NOT NULL, + `post_type` enum('public','private','draft') NOT NULL COMMENT 'public/private/draft', + `created` datetime NOT NULL, + `updated` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `user_id` (`user_id`,`title`), + KEY `posts_user_id_idx` (`id`) USING BTREE, + CONSTRAINT `posts_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Posts table' +``` + +
+ ## Columns diff --git a/sample/mysql8/users.md b/sample/mysql8/users.md index e68358aee..c18f0722b 100644 --- a/sample/mysql8/users.md +++ b/sample/mysql8/users.md @@ -3,6 +3,25 @@ ## Description Users table +
+Table Definition + +```sql +CREATE TABLE `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(50) NOT NULL, + `password` varchar(50) NOT NULL, + `email` varchar(355) NOT NULL COMMENT 'ex. user@example.com', + `created` timestamp NOT NULL, + `updated` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `username` (`username`), + UNIQUE KEY `email` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Users table' +``` + +
+ ## Columns diff --git a/sample/postgres/post_comments.md b/sample/postgres/post_comments.md index b82fdb50b..9f6740143 100644 --- a/sample/postgres/post_comments.md +++ b/sample/postgres/post_comments.md @@ -3,6 +3,27 @@ ## Description post and comments View table +
+Table Definition + +```sql +CREATE VIEW post_comments AS ( + SELECT c.id, + p.title, + u2.username AS post_user, + c.comment, + u2.username AS comment_user, + c.created, + c.updated + FROM (((posts p + LEFT JOIN comments c ON ((p.id = c.post_id))) + LEFT JOIN users u ON ((u.id = p.user_id))) + LEFT JOIN users u2 ON ((u2.id = c.user_id))) +) +``` + +
+ ## Columns diff --git a/schema/schema.go b/schema/schema.go index 805aeb490..f7a155ab9 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -41,6 +41,7 @@ type Table struct { Columns []*Column Indexes []*Index Constraints []*Constraint + Def string } // Relation is the struct for table relation