Skip to content
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

Add new auto detection strategy for singular table name #320

Merged
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ db: db_sqlite
usql my://root:mypass@localhost:33308/testdb -f testdata/ddl/mysql.sql
usql my://root:mypass@localhost:33308/testdb -c "CREATE DATABASE IF NOT EXISTS relations;"
usql my://root:mypass@localhost:33308/relations -f testdata/ddl/detect_relations.sql
usql my://root:mypass@localhost:33308/testdb -c "CREATE DATABASE IF NOT EXISTS relations_singular;"
usql my://root:mypass@localhost:33308/relations_singular -f testdata/ddl/detect_relations_singular.sql
usql maria://root:mypass@localhost:33309/testdb -f testdata/ddl/maria.sql
usql ms://SA:MSSQLServer-Passw0rd@localhost:11433/master -c "IF NOT EXISTS (SELECT * FROM sys.databases WHERE NAME = 'testdb') CREATE DATABASE testdb;"
usql ms://SA:MSSQLServer-Passw0rd@localhost:11433/testdb -f testdata/ddl/mssql.sql || true
Expand All @@ -49,6 +51,7 @@ doc: build doc_sqlite
./tbls doc my://root:mypass@localhost:33306/testdb -c testdata/test_tbls.yml -f sample/mysql56
./tbls doc my://root:mypass@localhost:33308/testdb -c testdata/test_tbls.yml -f sample/mysql
./tbls doc my://root:mypass@localhost:33308/relations -c testdata/test_tbls_detect_relations.yml -f sample/detect_relations
./tbls doc my://root:mypass@localhost:33308/relations_singular -c testdata/test_tbls_detect_relations_singular.yml -f sample/detect_relations_singular
./tbls doc maria://root:mypass@localhost:33309/testdb -c testdata/test_tbls.yml -f sample/mariadb
./tbls doc ms://SA:MSSQLServer-Passw0rd@localhost:11433/testdb -c testdata/test_tbls_mssql.yml -f sample/mssql
env AWS_ENDPOINT_URL=http://localhost:18000 ./tbls doc dynamodb://ap-northeast-1 -c testdata/test_tbls_dynamodb.yml -f sample/dynamodb
Expand All @@ -68,6 +71,7 @@ testdoc: build testdoc_sqlite
./tbls diff my://root:mypass@localhost:33306/testdb -c testdata/test_tbls.yml sample/mysql56
./tbls diff my://root:mypass@localhost:33308/testdb -c testdata/test_tbls.yml sample/mysql
./tbls diff my://root:mypass@localhost:33308/relations -c testdata/test_tbls_detect_relations.yml sample/detect_relations
./tbls diff my://root:mypass@localhost:33308/relations_singular -c testdata/test_tbls_detect_relations_singular.yml sample/detect_relations_singular
./tbls diff maria://root:mypass@localhost:33309/testdb -c testdata/test_tbls.yml sample/mariadb
./tbls diff ms://SA:MSSQLServer-Passw0rd@localhost:11433/testdb -c testdata/test_tbls_mssql.yml sample/mssql
env AWS_ENDPOINT_URL=http://localhost:18000 ./tbls diff dynamodb://ap-northeast-1 -c testdata/test_tbls_dynamodb.yml sample/dynamodb
Expand Down
22 changes: 21 additions & 1 deletion config/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package config

import (
"fmt"
"github.com/gertd/go-pluralize"
"strings"

"github.com/gertd/go-pluralize"
)

var (
Expand All @@ -28,6 +29,12 @@ func SelectNamingStrategy(name string) bool {
switch name {
case "":
// TODO: Add case if added naming strategy

case "singularTableName":
namingStrategy.ParentTable = singularTableParentTableNamer
namingStrategy.ParentColumn = singularTableParentColumnNamer
return true

default:
fmt.Printf("Naming strategy does not exist. strategy: %s\n", name)
return false
Expand Down Expand Up @@ -68,3 +75,16 @@ func defaultParentTableNamer(name string) string {
func defaultParentColumnNamer(name string) string {
return "id"
}

func singularTableParentTableNamer(name string) string {
index := strings.LastIndex(name, "_")

if index == -1 || name[index+1:] != "id" {
return ""
}
return pluralizeClient.Singular(name[:index])
}

func singularTableParentColumnNamer(name string) string {
return "id"
}
38 changes: 38 additions & 0 deletions config/naming_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package config

import "testing"

func TestSingularTableParentTableNamer(t *testing.T) {
tests := []struct {
name string
want string
}{
{"user_id", "user"},
}

for _, tt := range tests {
got := singularTableParentTableNamer(tt.name)

if got != tt.want {
t.Errorf("name %v\ngot %v\nwant %v", tt.name, got, tt.want)
}
}
}

func TestSingularTableParentColumnNamer(t *testing.T) {
tests := []struct {
name string
want string
}{
// normal
{"user_id", "id"},
}

for _, tt := range tests {
got := singularTableParentColumnNamer(tt.name)

if got != tt.want {
t.Errorf("name %v\ngot %v\nwant %v", tt.name, got, tt.want)
}
}
}
43 changes: 43 additions & 0 deletions sample/detect_relations_singular/CamelizeTable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# CamelizeTable

## Description

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `CamelizeTable` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

</details>

## Columns

| Name | Type | Default | Nullable | Extra Definition | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | --------------- | -------- | ------- | ------- |
| id | bigint | | false | auto_increment | | | |
| created | datetime | | false | | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (id) USING BTREE |

## Relations

![er](CamelizeTable.svg)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
29 changes: 29 additions & 0 deletions sample/detect_relations_singular/CamelizeTable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions sample/detect_relations_singular/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# relations_singular

## Description

Sample database document.

## Labels

`sample` `tbls`

## Tables

| Name | Columns | Comment | Type |
| ---- | ------- | ------- | ---- |
| [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE |
| [comment](comment.md) | 6 | Comment<br>Multi-line<br>table<br>comment | BASE TABLE |
| [comment_star](comment_star.md) | 6 | | BASE TABLE |
| [hyphen-table](hyphen-table.md) | 3 | | BASE TABLE |
| [log](log.md) | 7 | Auditログ | BASE TABLE |
| [post](post.md) | 7 | Post table | BASE TABLE |
| [post_comment](post_comment.md) | 7 | post and comments View table | VIEW |
| [user](user.md) | 6 | User table | BASE TABLE |
| [user_option](user_option.md) | 4 | User option table | BASE TABLE |

## Relations

![er](schema.svg)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
61 changes: 61 additions & 0 deletions sample/detect_relations_singular/comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# comment

## Description

Comment
Multi-line
table
comment

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `comment` (
`id` bigint NOT NULL AUTO_INCREMENT,
`post_id` bigint NOT NULL,
`user_id` int NOT NULL,
`comment` text NOT NULL COMMENT 'Comment\nMulti-line\r\ncolumn\rcomment',
`created` datetime NOT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `post_id` (`post_id`,`user_id`),
KEY `comment_post_id_user_id_idx` (`post_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Comment\nMulti-line\r\ntable\rcomment'
```

</details>

## Columns

| Name | Type | Default | Nullable | Extra Definition | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | --------------- | -------- | ------- | ------- |
| id | bigint | | false | auto_increment | [log](log.md) | | |
| post_id | bigint | | false | | | [post](post.md) | |
| user_id | int | | false | | | [user](user.md) | |
| comment | text | | false | | | | Comment<br>Multi-line<br>column<br>comment |
| created | datetime | | false | | | | |
| updated | datetime | | true | | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| post_id | UNIQUE | UNIQUE KEY post_id (post_id, user_id) |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| comment_post_id_user_id_idx | KEY comment_post_id_user_id_idx (post_id, user_id) USING BTREE |
| PRIMARY | PRIMARY KEY (id) USING BTREE |
| post_id | UNIQUE KEY post_id (post_id, user_id) USING BTREE |

## Relations

![er](comment.svg)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Loading