diff --git a/config/config.go b/config/config.go index da405cad..2f140169 100644 --- a/config/config.go +++ b/config/config.go @@ -506,6 +506,22 @@ func (c *Config) ModifySchema(s *schema.Schema) error { return fmt.Errorf("viewpoint '%s' has unknown label '%s'", v.Name, l) } } + for vi, v := range s.Viewpoints { + // Add viewpoints to table + + for _, t := range v.Tables { + println(v.Name, t) + table, err := s.FindTableByName(t) + if err != nil { + return err + } + table.Viewpoints = append(table.Viewpoints, &schema.TableViewpoint{ + Index: vi, + Name: v.Name, + Desc: v.Desc, + }) + } + } return nil } diff --git a/config/config_test.go b/config/config_test.go index cd5328fc..e9efb2d2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -254,15 +254,29 @@ func TestModifySchema(t *testing.T) { t.Error(err) } tests := []struct { - name string - desc string - labels []string - comments []AdditionalComment - relations []AdditionalRelation - wantRel int + name string + desc string + labels []string + comments []AdditionalComment + relations []AdditionalRelation + viewpointATables []string + viewpointBTables []string + wantRel int }{ - {"", "", []string{}, nil, nil, 3}, - {"mod_name_and_desc", "this is test schema", []string{}, nil, nil, 3}, + {"", "", []string{}, nil, nil, []string{ + "users", + "posts", + }, []string{ + "users", + "user_options", + }, 3}, + {"mod_name_and_desc", "this is test schema", []string{}, nil, nil, []string{ + "users", + "posts", + }, []string{ + "users", + "user_options", + }, 3}, {"relations", "", []string{}, nil, []AdditionalRelation{ { Table: "users", @@ -270,6 +284,12 @@ func TestModifySchema(t *testing.T) { Columns: []string{"id"}, ParentColumns: []string{"id"}, }, + }, []string{ + "users", + "posts", + }, []string{ + "users", + "user_options", }, 4}, {"not_override", "", []string{}, nil, []AdditionalRelation{ { @@ -280,6 +300,12 @@ func TestModifySchema(t *testing.T) { Def: "Additional Relation", Override: false, }, + }, []string{ + "users", + "posts", + }, []string{ + "users", + "user_options", }, 4}, {"override", "", []string{}, nil, []AdditionalRelation{ { @@ -292,6 +318,12 @@ func TestModifySchema(t *testing.T) { Def: "Override Relation", Override: true, }, + }, []string{ + "users", + "posts", + }, []string{ + "users", + "user_options", }, 3}, } for _, tt := range tests { @@ -301,11 +333,21 @@ func TestModifySchema(t *testing.T) { c.Labels = tt.labels c.Comments = tt.comments c.Relations = tt.relations + c.Viewpoints = append(c.Viewpoints, Viewpoint{ + Name: "A", + Desc: "Viewpoint A", + Tables: tt.viewpointATables, + }) + c.Viewpoints = append(c.Viewpoints, Viewpoint{ + Name: "B", + Desc: "Viewpoint B", + Tables: tt.viewpointBTables, + }) + s := newTestSchemaViaJSON(t) if err := c.ModifySchema(s); err != nil { t.Error(err) } - got, err := json.MarshalIndent(s, "", " ") if err != nil { t.Error(err) diff --git a/output/md/md.go b/output/md/md.go index 3d5b09ce..797594a3 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -129,11 +129,6 @@ func (m *Md) OutputViewpoint(wr io.Writer, i int, v *schema.Viewpoint) error { // Output generate markdown files. func Output(s *schema.Schema, c *config.Config, force bool) (e error) { - s, err := s.SetViewpointsToTables() - if err != nil { - return errors.WithStack(err) - } - docPath := c.DocPath fullPath, err := filepath.Abs(docPath) diff --git a/sample/viewpoints/logs.md b/sample/viewpoints/logs.md index f5dc49e1..011b16e6 100644 --- a/sample/viewpoints/logs.md +++ b/sample/viewpoints/logs.md @@ -34,6 +34,12 @@ CREATE TABLE `logs` ( | payload | text | | true | | | | | | created | datetime | | false | | | | | +## Viewpoints + +| Name | Definition | +| ---- | ---------- | +| [Ops](viewpoint-1.md) | Tables to be referenced during operation | + ## Constraints | Name | Type | Definition | diff --git a/sample/viewpoints/posts.md b/sample/viewpoints/posts.md index 0021f658..5d18f52c 100644 --- a/sample/viewpoints/posts.md +++ b/sample/viewpoints/posts.md @@ -41,6 +41,12 @@ CREATE TABLE `posts` ( | created | datetime | | false | | | | | | | updated | datetime | | true | | | | | | +## Viewpoints + +| Name | Definition | +| ---- | ---------- | +| [Ops](viewpoint-1.md) | Tables to be referenced during operation | + ## Constraints | Name | Type | Definition | diff --git a/sample/viewpoints/schema.json b/sample/viewpoints/schema.json index cfa458c4..e1e5bf49 100644 --- a/sample/viewpoints/schema.json +++ b/sample/viewpoints/schema.json @@ -1 +1 @@ -{"name":"testdb","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":"","labels":[{"Name":"user","Virtual":true}]},{"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":"comment_stars_user_id_fk","def":"KEY comment_stars_user_id_fk (comment_user_id) USING BTREE","table":"comment_stars","columns":["comment_user_id"],"comment":""},{"name":"comment_stars_user_id_post_id_fk","def":"KEY comment_stars_user_id_post_id_fk (comment_post_id, comment_user_id) USING BTREE","table":"comment_stars","columns":["comment_post_id","comment_user_id"],"comment":""},{"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":"comment_stars_user_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (comment_user_id) REFERENCES users (id)","table":"comment_stars","referenced_table":"users","columns":["comment_user_id"],"referenced_columns":["id"],"comment":""},{"name":"comment_stars_user_id_post_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (comment_post_id, comment_user_id) REFERENCES comments (post_id, user_id)","table":"comment_stars","referenced_table":"comments","columns":["comment_post_id","comment_user_id"],"referenced_columns":["post_id","user_id"],"comment":""},{"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 KEY `comment_stars_user_id_post_id_fk` (`comment_post_id`,`comment_user_id`),\n KEY `comment_stars_user_id_fk` (`comment_user_id`),\n CONSTRAINT `comment_stars_user_id_fk` FOREIGN KEY (`comment_user_id`) REFERENCES `users` (`id`),\n CONSTRAINT `comment_stars_user_id_post_id_fk` FOREIGN KEY (`comment_post_id`, `comment_user_id`) REFERENCES `comments` (`post_id`, `user_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci","labels":[{"Name":"content","Virtual":true}]},{"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":"","labels":[{"Name":"user","Virtual":true}]},{"name":"comment","type":"text","nullable":false,"default":null,"comment":"Comment\nMulti-line\r\ncolumn\rcomment"},{"name":"post_id_desc","type":"bigint","nullable":true,"default":null,"comment":"","extra_def":"GENERATED ALWAYS AS (`post_id` * -(1)) VIRTUAL"},{"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":"comments_user_id_fk","def":"KEY comments_user_id_fk (user_id) USING BTREE","table":"comments","columns":["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":"comments_post_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (post_id) REFERENCES posts (id)","table":"comments","referenced_table":"posts","columns":["post_id"],"referenced_columns":["id"],"comment":""},{"name":"comments_user_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (user_id) REFERENCES users (id)","table":"comments","referenced_table":"users","columns":["user_id"],"referenced_columns":["id"],"comment":""},{"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 `post_id_desc` bigint GENERATED ALWAYS AS ((`post_id` * -(1))) VIRTUAL,\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_user_id_fk` (`user_id`),\n KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`),\n CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),\n CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Comments\\nMulti-line\\r\\ntable\\rcomment'","labels":[{"Name":"content","Virtual":true}]},{"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":"long_long_long_long_long_long_long_long_table_name","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":"long_long_long_long_long_long_long_long_table_name","columns":["id"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"long_long_long_long_long_long_long_long_table_name","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `long_long_long_long_long_long_long_long_table_name` (\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":"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":"Untitled","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 (((`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`))))","labels":[{"Name":"content","Virtual":true}],"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":"","labels":[{"Name":"user","Virtual":true}]},{"name":"title","type":"varchar(255)","nullable":false,"default":"Untitled","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":"posts_user_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (user_id) REFERENCES users (id)","table":"posts","referenced_table":"users","columns":["user_id"],"referenced_columns":["id"],"comment":""},{"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 DEFAULT 'Untitled',\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 CONSTRAINT `posts_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='Posts table'","labels":[{"Name":"content","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'","labels":[{"Name":"user","Virtual":true}]},{"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":"","labels":[{"Name":"secure","Virtual":true},{"Name":"encrypted","Virtual":true}]},{"name":"email","type":"varchar(355)","nullable":false,"default":null,"comment":"ex. user@example.com","labels":[{"Name":"secure","Virtual":true}]},{"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":""},{"name":"users_chk_1","type":"CHECK","def":"CHECK ((char_length(`username`) \u003e 4))","table":"users","referenced_table":null,"columns":null,"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 CONSTRAINT `users_chk_1` CHECK ((char_length(`username`) \u003e 4))\n) ENGINE=InnoDB AUTO_INCREMENT=[Redacted by tbls] DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Users table'","labels":[{"Name":"user","Virtual":true}]}],"relations":[{"table":"comment_stars","columns":["comment_user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (comment_user_id) REFERENCES users (id)","virtual":false},{"table":"comment_stars","columns":["comment_post_id","comment_user_id"],"cardinality":"Zero or more","parent_table":"comments","parent_columns":["post_id","user_id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (comment_post_id, comment_user_id) REFERENCES comments (post_id, user_id)","virtual":false},{"table":"comments","columns":["post_id"],"cardinality":"Zero or more","parent_table":"posts","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (post_id) REFERENCES posts (id)","virtual":false},{"table":"comments","columns":["user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (user_id) REFERENCES users (id)","virtual":false},{"table":"posts","columns":["user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (user_id) REFERENCES users (id)","virtual":false},{"table":"user_options","columns":["user_id"],"cardinality":"Zero or one","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (user_id) REFERENCES users (id)","virtual":false},{"table":"logs","columns":["user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"logs-\u003eusers","virtual":true},{"table":"logs","columns":["post_id"],"cardinality":"Zero or more","parent_table":"posts","parent_columns":["id"],"parent_cardinality":"Zero or one","def":"Additional Relation","virtual":true},{"table":"logs","columns":["comment_id"],"cardinality":"Zero or more","parent_table":"comments","parent_columns":["id"],"parent_cardinality":"Zero or one","def":"Additional Relation","virtual":true},{"table":"logs","columns":["comment_star_id"],"cardinality":"Zero or more","parent_table":"comment_stars","parent_columns":["id"],"parent_cardinality":"Zero or one","def":"Additional 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.32","meta":{"dict":{"Functions":"Stored procedures and functions"}}},"labels":[{"Name":"sample","Virtual":true},{"Name":"tbls","Virtual":true}],"viewpoints":[{"name":"Content","desc":"Content as an asset for blogging services","labels":["content"]},{"name":"Ops","desc":"Tables to be referenced during operation","tables":["logs","users","posts"]},{"name":"Around the users table","desc":"Tables related to the users table","tables":["users"],"distance":1,"groups":[{"name":"Content","desc":"Content as an asset for blogging services","labels":["content"]}]},{"name":"Secure data","desc":"Tables with secure data","labels":["secure"]}]} +{"name":"testdb","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":"","labels":[{"Name":"user","Virtual":true}]},{"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":"comment_stars_user_id_fk","def":"KEY comment_stars_user_id_fk (comment_user_id) USING BTREE","table":"comment_stars","columns":["comment_user_id"],"comment":""},{"name":"comment_stars_user_id_post_id_fk","def":"KEY comment_stars_user_id_post_id_fk (comment_post_id, comment_user_id) USING BTREE","table":"comment_stars","columns":["comment_post_id","comment_user_id"],"comment":""},{"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":"comment_stars_user_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (comment_user_id) REFERENCES users (id)","table":"comment_stars","referenced_table":"users","columns":["comment_user_id"],"referenced_columns":["id"],"comment":""},{"name":"comment_stars_user_id_post_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (comment_post_id, comment_user_id) REFERENCES comments (post_id, user_id)","table":"comment_stars","referenced_table":"comments","columns":["comment_post_id","comment_user_id"],"referenced_columns":["post_id","user_id"],"comment":""},{"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 KEY `comment_stars_user_id_post_id_fk` (`comment_post_id`,`comment_user_id`),\n KEY `comment_stars_user_id_fk` (`comment_user_id`),\n CONSTRAINT `comment_stars_user_id_fk` FOREIGN KEY (`comment_user_id`) REFERENCES `users` (`id`),\n CONSTRAINT `comment_stars_user_id_post_id_fk` FOREIGN KEY (`comment_post_id`, `comment_user_id`) REFERENCES `comments` (`post_id`, `user_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci","labels":[{"Name":"content","Virtual":true}]},{"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":"","labels":[{"Name":"user","Virtual":true}]},{"name":"comment","type":"text","nullable":false,"default":null,"comment":"Comment\nMulti-line\r\ncolumn\rcomment"},{"name":"post_id_desc","type":"bigint","nullable":true,"default":null,"comment":"","extra_def":"GENERATED ALWAYS AS (`post_id` * -(1)) VIRTUAL"},{"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":"comments_user_id_fk","def":"KEY comments_user_id_fk (user_id) USING BTREE","table":"comments","columns":["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":"comments_post_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (post_id) REFERENCES posts (id)","table":"comments","referenced_table":"posts","columns":["post_id"],"referenced_columns":["id"],"comment":""},{"name":"comments_user_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (user_id) REFERENCES users (id)","table":"comments","referenced_table":"users","columns":["user_id"],"referenced_columns":["id"],"comment":""},{"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 `post_id_desc` bigint GENERATED ALWAYS AS ((`post_id` * -(1))) VIRTUAL,\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_user_id_fk` (`user_id`),\n KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`),\n CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),\n CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Comments\\nMulti-line\\r\\ntable\\rcomment'","labels":[{"Name":"content","Virtual":true}]},{"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":"long_long_long_long_long_long_long_long_table_name","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":"long_long_long_long_long_long_long_long_table_name","columns":["id"],"comment":""}],"constraints":[{"name":"PRIMARY","type":"PRIMARY KEY","def":"PRIMARY KEY (id)","table":"long_long_long_long_long_long_long_long_table_name","referenced_table":null,"columns":["id"],"referenced_columns":null,"comment":""}],"triggers":[],"def":"CREATE TABLE `long_long_long_long_long_long_long_long_table_name` (\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":"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":"Untitled","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 (((`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`))))","labels":[{"Name":"content","Virtual":true}],"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":"","labels":[{"Name":"user","Virtual":true}]},{"name":"title","type":"varchar(255)","nullable":false,"default":"Untitled","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":"posts_user_id_fk","type":"FOREIGN KEY","def":"FOREIGN KEY (user_id) REFERENCES users (id)","table":"posts","referenced_table":"users","columns":["user_id"],"referenced_columns":["id"],"comment":""},{"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 DEFAULT 'Untitled',\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 CONSTRAINT `posts_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='Posts table'","labels":[{"Name":"content","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'","labels":[{"Name":"user","Virtual":true}]},{"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":"","labels":[{"Name":"secure","Virtual":true},{"Name":"encrypted","Virtual":true}]},{"name":"email","type":"varchar(355)","nullable":false,"default":null,"comment":"ex. user@example.com","labels":[{"Name":"secure","Virtual":true}]},{"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":""},{"name":"users_chk_1","type":"CHECK","def":"CHECK ((char_length(`username`) \u003e 4))","table":"users","referenced_table":null,"columns":null,"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 CONSTRAINT `users_chk_1` CHECK ((char_length(`username`) \u003e 4))\n) ENGINE=InnoDB AUTO_INCREMENT=[Redacted by tbls] DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Users table'","labels":[{"Name":"user","Virtual":true}]}],"relations":[{"table":"comment_stars","columns":["comment_user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (comment_user_id) REFERENCES users (id)","virtual":false},{"table":"comment_stars","columns":["comment_post_id","comment_user_id"],"cardinality":"Zero or more","parent_table":"comments","parent_columns":["post_id","user_id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (comment_post_id, comment_user_id) REFERENCES comments (post_id, user_id)","virtual":false},{"table":"comments","columns":["post_id"],"cardinality":"Zero or more","parent_table":"posts","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (post_id) REFERENCES posts (id)","virtual":false},{"table":"comments","columns":["user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (user_id) REFERENCES users (id)","virtual":false},{"table":"posts","columns":["user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (user_id) REFERENCES users (id)","virtual":false},{"table":"user_options","columns":["user_id"],"cardinality":"Zero or one","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"FOREIGN KEY (user_id) REFERENCES users (id)","virtual":false},{"table":"logs","columns":["user_id"],"cardinality":"Zero or more","parent_table":"users","parent_columns":["id"],"parent_cardinality":"Exactly one","def":"logs-\u003eusers","virtual":true},{"table":"logs","columns":["post_id"],"cardinality":"Zero or more","parent_table":"posts","parent_columns":["id"],"parent_cardinality":"Zero or one","def":"Additional Relation","virtual":true},{"table":"logs","columns":["comment_id"],"cardinality":"Zero or more","parent_table":"comments","parent_columns":["id"],"parent_cardinality":"Zero or one","def":"Additional Relation","virtual":true},{"table":"logs","columns":["comment_star_id"],"cardinality":"Zero or more","parent_table":"comment_stars","parent_columns":["id"],"parent_cardinality":"Zero or one","def":"Additional 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.1.0","meta":{"dict":{"Functions":"Stored procedures and functions"}}},"labels":[{"Name":"sample","Virtual":true},{"Name":"tbls","Virtual":true}],"viewpoints":[{"name":"Content","desc":"Content as an asset for blogging services","labels":["content"]},{"name":"Ops","desc":"Tables to be referenced during operation","tables":["logs","users","posts"]},{"name":"Around the users table","desc":"Tables related to the users table","tables":["users"],"distance":1,"groups":[{"name":"Content","desc":"Content as an asset for blogging services","labels":["content"]}]},{"name":"Secure data","desc":"Tables with secure data","labels":["secure"]}]} diff --git a/sample/viewpoints/users.md b/sample/viewpoints/users.md index eda656f5..a21888a1 100644 --- a/sample/viewpoints/users.md +++ b/sample/viewpoints/users.md @@ -39,6 +39,13 @@ CREATE TABLE `users` ( | created | timestamp | | false | | | | | | | updated | timestamp | | true | | | | | | +## Viewpoints + +| Name | Definition | +| ---- | ---------- | +| [Ops](viewpoint-1.md) | Tables to be referenced during operation | +| [Around the users table](viewpoint-2.md) | Tables related to the users table | + ## Constraints | Name | Type | Definition | diff --git a/schema/schema.go b/schema/schema.go index 7f9a352b..e1af3744 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -199,24 +199,6 @@ type Schema struct { Viewpoints Viewpoints `json:"viewpoints,omitempty"` } -func (s *Schema) SetViewpointsToTables() (*Schema, error) { - for vi, v := range s.Viewpoints { - // Add viewpoints to table - for _, t := range v.Tables { - table, err := s.FindTableByName(t) - if err != nil { - return s, err - } - table.Viewpoints = append(table.Viewpoints, &TableViewpoint{ - Index: vi, - Name: v.Name, - Desc: v.Desc, - }) - } - } - return s, nil -} - func (s *Schema) NormalizeTableName(name string) string { if s.Driver != nil && s.Driver.Meta != nil && s.Driver.Meta.CurrentSchema != "" && (s.Driver.Name == "postgres" || s.Driver.Name == "redshift") && !strings.Contains(name, ".") { return fmt.Sprintf("%s.%s", s.Driver.Meta.CurrentSchema, name) diff --git a/schema/schema_test.go b/schema/schema_test.go index bc023f36..cd8f1fdc 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -11,54 +11,6 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestSetViewpointsToTables(t *testing.T) { - viewpointAName := "va" - viewpointBName := "vb" - - tests := []struct { - viewpointATables []string - viewpointBTables []string - wantTableAViewpoints []*TableViewpoint - }{ - {[]string{"a"}, []string{"b"}, []*TableViewpoint{{Name: viewpointAName}}}, - {[]string{"a", "b"}, []string{"a"}, []*TableViewpoint{{ - Index: 0, - Name: viewpointAName, - }, { - Index: 1, - Name: viewpointBName, - }}}, - } - - for _, tt := range tests { - t.Run(fmt.Sprintf("%v", tt.viewpointATables), func(t *testing.T) { - fmt.Println(tt.viewpointATables) - s := newTestSchema(t) - s.Viewpoints = []*Viewpoint{ - { - Name: viewpointAName, - Tables: tt.viewpointATables, - }, - { - Name: viewpointBName, - Tables: tt.viewpointBTables, - }, - } - result, err := s.SetViewpointsToTables() - if err != nil { - t.Error(err) - } - gotTable, _ := result.FindTableByName("a") - got := gotTable.Viewpoints - want := tt.wantTableAViewpoints - - if diff := cmp.Diff(got, want, nil); diff != "" { - t.Errorf("%s", diff) - } - }) - } -} - func TestNormalizeTableName(t *testing.T) { tests := []struct { s *Schema diff --git a/testdata/modify_schema_.golden b/testdata/modify_schema_.golden index 95d40490..5f8115fe 100644 --- a/testdata/modify_schema_.golden +++ b/testdata/modify_schema_.golden @@ -409,5 +409,23 @@ "name": "sqlite", "database_version": "3.39.4", "meta": {} - } + }, + "viewpoints": [ + { + "name": "A", + "desc": "Viewpoint A", + "tables": [ + "users", + "posts" + ] + }, + { + "name": "B", + "desc": "Viewpoint B", + "tables": [ + "users", + "user_options" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_mod_name_and_desc.golden b/testdata/modify_schema_mod_name_and_desc.golden index 4ae3d2e8..593bb419 100644 --- a/testdata/modify_schema_mod_name_and_desc.golden +++ b/testdata/modify_schema_mod_name_and_desc.golden @@ -409,5 +409,23 @@ "name": "sqlite", "database_version": "3.39.4", "meta": {} - } + }, + "viewpoints": [ + { + "name": "A", + "desc": "Viewpoint A", + "tables": [ + "users", + "posts" + ] + }, + { + "name": "B", + "desc": "Viewpoint B", + "tables": [ + "users", + "user_options" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_not_override.golden b/testdata/modify_schema_not_override.golden index 998d5572..969af534 100644 --- a/testdata/modify_schema_not_override.golden +++ b/testdata/modify_schema_not_override.golden @@ -423,5 +423,23 @@ "name": "sqlite", "database_version": "3.39.4", "meta": {} - } + }, + "viewpoints": [ + { + "name": "A", + "desc": "Viewpoint A", + "tables": [ + "users", + "posts" + ] + }, + { + "name": "B", + "desc": "Viewpoint B", + "tables": [ + "users", + "user_options" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_override.golden b/testdata/modify_schema_override.golden index c624569e..81be5cea 100644 --- a/testdata/modify_schema_override.golden +++ b/testdata/modify_schema_override.golden @@ -409,5 +409,23 @@ "name": "sqlite", "database_version": "3.39.4", "meta": {} - } + }, + "viewpoints": [ + { + "name": "A", + "desc": "Viewpoint A", + "tables": [ + "users", + "posts" + ] + }, + { + "name": "B", + "desc": "Viewpoint B", + "tables": [ + "users", + "user_options" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_relations.golden b/testdata/modify_schema_relations.golden index f8a90516..ff057712 100644 --- a/testdata/modify_schema_relations.golden +++ b/testdata/modify_schema_relations.golden @@ -423,5 +423,23 @@ "name": "sqlite", "database_version": "3.39.4", "meta": {} - } + }, + "viewpoints": [ + { + "name": "A", + "desc": "Viewpoint A", + "tables": [ + "users", + "posts" + ] + }, + { + "name": "B", + "desc": "Viewpoint B", + "tables": [ + "users", + "user_options" + ] + } + ] } \ No newline at end of file