8000 [BREAKING] Output schema data file by default by k1LoW · Pull Request #398 · k1LoW/tbls · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[BREAKING] Output schema data file by default #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,12 @@ Flags:
--without-er no generate ER diagrams
```

## Output Schema data

`tbls doc` also output schema data ( `schema.json` ) to same directory as the generated schema document.

To disable output of schema data, set `disableOutputSchema:` to `true` in `.tbls.yml` file.

## Environment variables

tbls accepts environment variables `TBLS_DSN` and `TBLS_DOC_PATH`
Expand Down
36 changes: 28 additions & 8 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/k1LoW/tbls/config"
"github.com/k1LoW/tbls/datasource"
"github.com/k1LoW/tbls/output/gviz"
"github.com/k1LoW/tbls/output/json"
"github.com/k1LoW/tbls/output/md"
"github.com/k1LoW/tbls/schema"
"github.com/pkg/errors"
Expand Down Expand Up @@ -96,17 +97,22 @@ var docCmd = &cobra.Command{
}
}

err = md.Output(s, c, force)

if err != nil {
if err := md.Output(s, c, force); err != nil {
return err
}

// output schema.json
if !c.DisableOutputSchema {
if err := withSchemaFile(s, c); err != nil {
return err
}
}

return nil
},
}

func withDot(s *schema.Schema, c *config.Config, force bool) (e error) {
func withDot(s *schema.Schema, c *config.Config, force bool) error {
erFormat := c.ER.Format
outputPath := c.DocPath
fullPath, err := filepath.Abs(outputPath)
Expand All @@ -131,8 +137,7 @@ func withDot(s *schema.Schema, c *config.Config, force bool) (e error) {
return errors.WithStack(err)
}
g := gviz.New(c)
err = g.OutputSchema(file, s)
if err != nil {
if err := g.OutputSchema(file, s); err != nil {
return errors.WithStack(err)
}

Expand All @@ -145,15 +150,30 @@ func withDot(s *schema.Schema, c *config.Config, force bool) (e error) {
if err != nil {
return errors.WithStack(err)
}
err = g.OutputTable(file, t)
if err != nil {
if err := g.OutputTable(file, t); err != nil {
return errors.WithStack(err)
}
}

return nil
}

func withSchemaFile(s *schema.Schema, c *config.Config) (e error) {
sf, err := os.Create(c.SchemaFilePath())
if err != nil {
return err
}
defer func() {
_ = sf.Close()
}()
fmt.Printf("%s\n", c.SchemaFilePath())
j := json.New(true)
if err := j.OutputSchema(sf, s); err != nil {
return err
}
return nil
}

func loadDocArgs(args []string) ([]config.Option, error) {
options := []config.Option{}
if len(args) > 2 {
Expand Down
21 changes: 15 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ var DefaultConfigFilePaths = []string{".tbls.yml", "tbls.yml"}
// DefaultERFormat is the default ER diagram format
const DefaultERFormat = "svg"

const SchemaFileName = "schema.json"

// DefaultDistance is the default distance between tables that display relations in the ER
var DefaultDistance = 1

// Config is tbls config
type Config struct {
Name string `yaml:"name"`
Desc string `yaml:"desc,omitempty"`
Labels []string `yaml:"labels,omitempty"`
DSN DSN `yaml:"dsn"`
Name string `yaml:"name"`
Desc string `yaml:"desc,omitempty"`
Labels []string `yaml:"labels,omitempty"`
DSN DSN `yaml:"dsn"`
// Directory of schema document
DocPath string `yaml:"docPath"`
Format Format `yaml:"format,omitempty"`
ER ER `yaml:"er,omitempty"`
Expand All @@ -47,9 +50,11 @@ type Config struct {
DetectVirtualRelations DetectVirtualRelations `yaml:"detectVirtualRelations,omitempty"`
BaseUrl string `yaml:"baseUrl,omitempty"`
RequiredVersion string `yaml:"requiredVersion,omitempty"`
DisableOutputSchema bool `yaml:"disableOutputSchema,omitempty"`
MergedDict dict.Dict `yaml:"-"`
Path string `yaml:"-"`
root string `yaml:"-"`
// Path of config file
Path string `yaml:"-"`
root string `yaml:"-"`
}

type DSN struct {
Expand Down Expand Up @@ -448,6 +453,10 @@ func (c *Config) MaskedDSN() (string, error) {
return strings.Replace(u.String(), tmp, "*****", 1), nil
}

func (c *Config) SchemaFilePath() string {
return filepath.Join(c.DocPath, SchemaFileName)
}

func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) error {
for _, r := range relations {
relation := &schema.Relation{
Expand Down
1 change: 1 addition & 0 deletions sample/adjust/schema.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions sample/detect_relations/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"relations","desc":"Sample database document.","tables":[{"name":"CamelizeTable","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"bigint","nullable":false,"default":null,"comment":"","extra_def":"auto_increment"},{"name":"created","type":"datetime","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"PRIMARY","def":"PRIMARY KEY (id) USING BTREE","table":"CamelizeTable","columns":["id"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"CamelizeTable","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `CamelizeTable` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `created` datetime NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"},{"name":"comment_stars","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"bigint","nullable":false,"default":null,"comment":"","extra_def":"auto_increment"},{"name":"user_id","type":"int","nullable":false,"default":null,"comment":""},{"name":"comment_post_id","type":"bigint","nullable":false,"default":null,"comment":""},{"name":"comment_user_id","type":"int","nullable":false,"default":null,"comment":""},{"name":"created","type":"timestamp","nullable":false,"default":null,"comment":""},{"name":"updated","type":"timestamp","nullable":true,"default":null,"comment":""}],"indexes":[{"name":"PRIMARY","def":"PRIMARY KEY (id) USING BTREE","table":"comment_stars","columns":["id"],"comment":""},{"name":"user_id","def":"UNIQUE KEY user_id (user_id, comment_post_id, comment_user_id) USING BTREE","table":"comment_stars","columns":["user_id","comment_post_id","comment_user_id"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"comment_stars","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""},{"name":"user_id","type":"UNIQUE","def":"UNIQUE KEY user_id (user_id, comment_post_id, comment_user_id)","table":"comment_stars","referenced_table":null,"columns":["user_id","comment_post_id","comment_user_id"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `comment_stars` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `user_id` int NOT NULL,\n `comment_post_id` bigint NOT NULL,\n `comment_user_id` int NOT NULL,\n `created` timestamp NOT NULL,\n `updated` timestamp NULL DEFAULT NULL,\n PRIMARY KEY (`id`),\n UNIQUE KEY `user_id` (`user_id`,`comment_post_id`,`comment_user_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"},{"name":"comments","type":"BASE TABLE","comment":"Comments\nMulti-line\r\ntable\rcomment","columns":[{"name":"id","type":"bigint","nullable":false,"default":null,"comment":"","extra_def":"auto_increment"},{"name":"post_id","type":"bigint","nullable":false,"default":null,"comment":""},{"name":"user_id","type":"int","nullable":false,"default":null,"comment":""},{"name":"comment","type":"text","nullable":false,"default":null,"comment":"Comment\nMulti-line\r\ncolumn\rcomment"},{"name":"created","type":"datetime","nullable":false,"default":null,"comment":""},{"name":"updated","type":"datetime","nullable":true,"default":null,"comment":""}],"indexes":[{"name":"comments_post_id_user_id_idx","def":"KEY comments_post_id_user_id_idx (post_id, user_id) USING BTREE","table":"comments","columns":["post_id","user_id"],"comment":""},{"name":"PRIMARY","def":"PRIMARY KEY (id) USING BTREE","table":"comments","columns":["id"],"comment":""},{"name":"post_id","def":"UNIQUE KEY post_id (post_id, user_id) USING BTREE","table":"comments","columns":["post_id","user_id"],"comment":""}],"constraints":[{"name":"post_id","type":"UNIQUE","def":"UNIQUE KEY post_id (post_id, user_id)","table":"comments","referenced_table":null,"columns":["post_id","user_id"],"referenced_columns":null,"comment":""},{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"comments","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `comments` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `post_id` bigint NOT NULL,\n `user_id` int NOT NULL,\n `comment` text NOT NULL COMMENT 'Comment\\nMulti-line\\r\\ncolumn\\rcomment',\n `created` datetime NOT NULL,\n `updated` datetime DEFAULT NULL,\n PRIMARY KEY (`id`),\n UNIQUE KEY `post_id` (`post_id`,`user_id`),\n KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Comments\\nMulti-line\\r\\ntable\\rcomment'"},{"name":"hyphen-table","type":"BASE TABLE","comment":"","columns":[{"name":"id","type":"bigint","nullable":false,"default":null,"comment":"","extra_def":"auto_increment"},{"name":"hyphen-column","type":"text","nullable":false,"default":null,"comment":""},{"name":"created","type":"datetime","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"PRIMARY","def":"PRIMARY KEY (id) USING BTREE","table":"hyphen-table","columns":["id"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"hyphen-table","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `hyphen-table` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `hyphen-column` text NOT NULL,\n `created` datetime NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"},{"name":"logs","type":"BASE TABLE","comment":"Auditログ","columns":[{"name":"id","type":"bigint","nullable":false,"default":null,"comment":"","extra_def":"auto_increment"},{"name":"user_id","type":"int","nullable":false,"default":null,"comment":""},{"name":"post_id","type":"bigint","nullable":true,"default":null,"comment":""},{"name":"comment_id","type":"bigint","nullable":true,"default":null,"comment":""},{"name":"comment_star_id","type":"bigint","nullable":true,"default":null,"comment":""},{"name":"payload","type":"text","nullable":true,"default":null,"comment":""},{"name":"created","type":"datetime","nullable":false,"default":null,"comment":""}],"indexes":[{"name":"PRIMARY","def":"PRIMARY KEY (id) USING BTREE","table":"logs","columns":["id"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"logs","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `logs` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `user_id` int NOT NULL,\n `post_id` bigint DEFAULT NULL,\n `comment_id` bigint DEFAULT NULL,\n `comment_star_id` bigint DEFAULT NULL,\n `payload` text,\n `created` datetime NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Auditログ'"},{"name":"post_comments","type":"VIEW","comment":"post and comments View table","columns":[{"name":"id","type":"bigint","nullable":true,"default":"0","comment":"comments.id"},{"name":"title","type":"varchar(255)","nullable":false,"default":null,"comment":"posts.title"},{"name":"post_user","type":"varchar(50)","nullable":true,"default":null,"comment":"posts.users.username"},{"name":"comment","type":"text","nullable":true,"default":null,"comment":"Comment\nMulti-line\r\ncolumn\rcomment"},{"name":"comment_user","type":"varchar(50)","nullable":true,"default":null,"comment":"comments.users.username"},{"name":"created","type":"datetime","nullable":true,"default":null,"comment":"comments.created"},{"name":"updated","type":"datetime","nullable":true,"default":null,"comment":"comments.updated"}],"indexes":[],"constraints":[],"triggers":[],"def":"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 (((`relations`.`posts` `p` left join `relations`.`comments` `c` on((`p`.`id` = `c`.`post_id`))) left join `relations`.`users` `u` on((`u`.`id` = `p`.`user_id`))) left join `relations`.`users` `u2` on((`u2`.`id` = `c`.`user_id`))))","referenced_tables":["posts","comments","users"]},{"name":"posts","type":"BASE TABLE","comment":"Posts table","columns":[{"name":"id","type":"bigint","nullable":false,"default":null,"comment":"","extra_def":"auto_increment"},{"name":"user_id","type":"int","nullable":false,"default":null,"comment":""},{"name":"title","type":"varchar(255)","nullable":false,"default":null,"comment":""},{"name":"body","type":"text","nullable":false,"default":null,"comment":"post body"},{"name":"post_type","type":"enum('public','private','draft')","nullable":false,"default":null,"comment":"public/private/draft"},{"name":"created","type":"datetime","nullable":false,"default":null,"comment":""},{"name":"updated","type":"datetime","nullable":true,"default":null,"comment":""}],"indexes":[{"name":"posts_user_id_idx","def":"KEY posts_user_id_idx (id) USING BTREE","table":"posts","columns":["id"],"comment":""},{"name":"PRIMARY","def":"PRIMARY KEY (id) USING BTREE","table":"posts","columns":["id"],"comment":""},{"name":"user_id","def":"UNIQUE KEY user_id (user_id, title) USING BTREE","table":"posts","columns":["user_id","title"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"posts","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""},{"name":"user_id","type":"UNIQUE","def":"UNIQUE KEY user_id (user_id, title)","table":"posts","referenced_table":null,"columns":["user_id","title"],"referenced_columns":null,"comment":""}],"triggers":[{"name":"update_posts_updated","def":"CREATE TRIGGER update_posts_updated BEFORE UPDATE ON posts\nFOR EACH ROW\nSET NEW.updated = CURRENT_TIMESTAMP()","comment":""}],"def":"CREATE TABLE `posts` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `user_id` int NOT NULL,\n `title` varchar(255) NOT NULL,\n `body` text NOT NULL,\n `post_type` enum('public','private','draft') NOT NULL COMMENT 'public/private/draft',\n `created` datetime NOT NULL,\n `updated` datetime DEFAULT NULL,\n PRIMARY KEY (`id`),\n UNIQUE KEY `user_id` (`user_id`,`title`),\n KEY `posts_user_id_idx` (`id`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Posts table'","labels":[{"Name":"green","Virtual":true},{"Name":"red","Virtual":true},{"Name":"blue","Virtual":true}]},{"name":"user_options","type":"BASE TABLE","comment":"User options table","columns":[{"name":"user_id","type":"int","nullable":false,"default":null,"comment":""},{"name":"show_email","type":"tinyint(1)","nullable":false,"default":"0","comment":""},{"name":"created","type":"timestamp","nullable":false,"default":null,"comment":""},{"name":"updated","type":"timestamp","nullable":true,"default":null,"comment":""}],"indexes":[{"name":"PRIMARY","def":"PRIMARY KEY (user_id) USING BTREE","table":"user_options","columns":["user_id"],"comment":""},{"name":"user_id","def":"UNIQUE KEY user_id (user_id) USING BTREE","table":"user_options","columns":["user_id"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (user_id)","table":"user_options","referenced_table":null,"columns":["user_id"],"referenced_columns":null,"comment":""},{"name":"user_id","type":"UNIQUE","def":"UNIQUE KEY user_id (user_id)","table":"user_options","referenced_table":null,"columns":["user_id"],"referenced_columns":null,"comment":""},{"name":"user_options_user_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (user_id) REFERENCES users (id)","table":"user_options","referenced_table":"users","columns":["user_id"],"referenced_columns":["id"],"comment":""}],"triggers":[],"def":"CREATE TABLE `user_options` (\n `user_id` int NOT NULL,\n `show_email` tinyint(1) NOT NULL DEFAULT '0',\n `created` timestamp NOT NULL,\n `updated` timestamp NULL DEFAULT NULL,\n PRIMARY KEY (`user_id`),\n UNIQUE KEY `user_id` (`user_id`),\n CONSTRAINT `user_options_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='User options table'"},{"name":"users","type":"BASE TABLE","comment":"Users table","columns":[{"name":"id","type":"int","nullable":false,"default":null,"comment":"","extra_def":"auto_increment"},{"name":"username","type":"varchar(50)","nullable":false,"default":null,"comment":""},{"name":"password","type":"varchar(50)","nullable":false,"default":null,"comment":""},{"name":"email","type":"varchar(355)","nullable":false,"default":null,"comment":"ex. user@example.com"},{"name":"created","type":"timestamp","nullable":false,"default":null,"comment":""},{"name":"updated","type":"timestamp","nullable":true,"default":null,"comment":""}],"indexes":[{"name":"PRIMARY","def":"PRIMARY KEY (id) USING BTREE","table":"users","columns":["id"],"comment":""},{"name":"email","def":"UNIQUE KEY email (email) USING BTREE","table":"users","columns":["email"],"comment":""},{"name":"username","def":"UNIQUE KEY username (username) USING BTREE","table":"users","columns":["username"],"comment":""}],"constraints":[{"name":"email","type":"UNIQUE","def":"UNIQUE KEY email (email)","table":"users","referenced_table":null,"columns":["email"],"referenced_columns":null,"comment":""},{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"users","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""},{"name":"username","type":"UNIQUE","def":"UNIQUE KEY username (username)","table":"users","referenced_table":null,"columns":["username"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `users` (\n `id` int NOT NULL AUTO_INCREMENT,\n `username` varchar(50) NOT NULL,\n `password` varchar(50) NOT NULL,\n `email` varchar(355) NOT NULL COMMENT 'ex. user@example.com',\n `created` timestamp NOT NULL,\n `updated` timestamp NULL DEFAULT NULL,\n PRIMARY KEY (`id`),\n UNIQUE KEY `username` (`username`),\n UNIQUE KEY `email` (`email`)\n) ENGINE=InnoDB AUTO_INCREMENT=[Redacted by tbls] DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Users table'"}],"relations":[{"table":"user_options","columns":["user_id"],"parent_table":"users","parent_columns":["id"],"def":"FOREIGN KEY (user_id) REFERENCES users (id)","virtual":false},{"table":"comment_stars","columns":["user_id"],"parent_table":"users","parent_columns":["id"],"def":"Detected Relation","virtual":true},{"table":"comments","columns":["post_id"],"parent_table":"posts","parent_columns":["id"],"def":"Detected Relation","virtual":true},{"table":"comments","columns":["user_id"],"parent_table":"users","parent_columns":["id"],"def":"Detected Relation","virtual":true},{"table":"logs","columns":["user_id"],"parent_table":"users","parent_columns":["id"],"def":"Detected Relation","virtual":true},{"table":"logs","columns":["post_id"],"parent_table":"posts","parent_columns":["id"],"def":"Detected Relation","virtual":true},{"table":"logs","columns":["comment_id"],"parent_table":"comments","parent_columns":["id"],"def":"Detected Relation","virtual":true},{"table":"logs","columns":["comment_star_id"],"parent_table":"comment_stars","parent_columns":["id"],"def":"Detected Relation","virtual":true},{"table":"posts","columns":["user_id"],"parent_table":"users","parent_columns":["id"],"def":"Detected Relation","virtual":true}],"functions":[{"name":"CustomerLevel","return_type":"varchar","arguments":"credit decimal","type":"FUNCTION"},{"name":"GetAllComments","return_type":"","arguments":"","type":"PROCEDURE"}],"driver":{"name":"mysql","database_version":"8.0.30","meta":{"dict":{"Functions":"Stored procedures and functions"}}},"labels":[{"Name":"sample","Virtual":true},{"Name":"tbls","Virtual":true}]}
Loading
0