Skip to content

Prepend table names to the generated enum names #716

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 1 commit into from
Oct 23, 2020
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
8 changes: 4 additions & 4 deletions examples/booktest/mysql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestBooks(t *testing.T) {
AuthorID: int32(authorID),
Isbn: "1",
Title: "my book title",
BookType: BookTypeFICTION,
BookType: BooksBookTypeFICTION,
Yr: 2016,
Available: now,
})
Expand All @@ -54,7 +54,7 @@ func TestBooks(t *testing.T) {
AuthorID: int32(authorID),
Isbn: "2",
Title: "the second book",
BookType: BookTypeFICTION,
BookType: BooksBookTypeFICTION ,
Yr: 2016,
Available: now,
Tags: "cool,unique",
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestBooks(t *testing.T) {
AuthorID: int32(authorID),
Isbn: "3",
Title: "the third book",
BookType: BookTypeFICTION,
BookType: BooksBookTypeFICTION,
Yr: 2001,
Available: now,
Tags: "cool",
Expand All @@ -96,7 +96,7 @@ func TestBooks(t *testing.T) {
AuthorID: int32(authorID),
Isbn: "4",
Title: "4th place finisher",
BookType: BookTypeNONFICTION,
BookType: BooksBookTypeNONFICTION,
Yr: 2011,
Available: now,
Tags: "other",
Expand Down
16 changes: 8 additions & 8 deletions examples/booktest/mysql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/booktest/mysql/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ondeck/mysql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func runOnDeckQueries(t *testing.T, q *Queries) {
Name: "The Fillmore",
City: city.Slug,
SpotifyPlaylist: "spotify:uri",
Status: StatusOpen,
Statuses: join(string(StatusOpen), string(StatusClosed)),
Status: VenuesStatusOpen,
Statuses: join(string(VenuesStatusOpen), string(VenuesStatusClosed)),
Tags: join("rock", "punk"),
})
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions examples/ondeck/mysql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/ondeck/mysql/venue.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions internal/endtoend/testdata/ddl_create_enum/mysql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package catalog

import (
"errors"
"fmt"

"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
Expand Down Expand Up @@ -162,7 +163,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
}
if col.Vals != nil {
typeName := ast.TypeName{
Name: col.Colname,
Name: fmt.Sprintf("%s_%s", stmt.Name.Name, col.Colname),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor annoyance with this is that it doesn't "singularize" the table's name.

It might also be frustrating to always opt-in to the longer enum names, when sometimes the terser names might suffice. But that seems trickier to fix

}
s := &ast.CreateEnumStmt{TypeName: &typeName, Vals: col.Vals}
if err := c.createEnum(s); err != nil {
Expand Down