stephenafamo / bob

SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
https://bob.stephenafamo.com
MIT License
701 stars 37 forks source link

Load index information for MySQL, PostgreSQL, and SQLite tables #237

Closed mbezhanov closed 2 weeks ago

mbezhanov commented 2 weeks ago

Hi, I am opening this in relation to https://github.com/stephenafamo/bob/issues/120.

This will allow users like @majiaxin110 to range over .Table.Indexes in their custom *.tpl enabling the use of $index.Name variables in a similar fashion:

{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
{{range $index := .Table.Indexes}}
func print_index_{{$tAlias.DownSingular}}_{{$index.Name}}() {
    fmt.Println("{{$index.Name}}")
}
{{end}}

Sample output:

func print_index_jet_idx_jets_names() {
    fmt.Println("idx_jets_names")
}

func print_index_jet_idx_jets_pilots() {
    fmt.Println("idx_jets_pilots")
}

func print_index_jet_PRIMARY() {
    fmt.Println("PRIMARY")
}

I only added MySQL support to keep the PR focused and get some early feedback on the implementation. If you approve of it, I'd happily add support for PostgreSQL and SQLite as well.

stephenafamo commented 2 weeks ago

This is very nice! Exactly what was requested in #120

I'll take a deeper look, but overall looks great 👌🏾

mbezhanov commented 2 weeks ago

Happy to hear that! I also included PostgreSQL support.

stephenafamo commented 2 weeks ago

The tests are failing because the new output does not match the golden files....

You should run the generation tests locally with the -overwrite-golden flag to fix this.

go test ./gen/bobgen-psql/driver -overwrite-golden
stephenafamo commented 2 weeks ago

Can you also update the SQLite golden file?

mbezhanov commented 2 weeks ago

Can you also update the SQLite golden file?

Yes, absolutely. I'll actually go ahead and directly implement the SQLite index retrieval logic in this PR. Coming up in a little bit :slightly_smiling_face:

Would you like me to squash everything up to this point into a single commit, or are you planning to squash when merging into main?

stephenafamo commented 2 weeks ago

Would you like me to squash everything up to this point into a single commit, or are you planning to squash when merging into main?

Kindly squash and push.

I do not squash when merging into main, so the commit history can get a little messy if it is not done beforehand 😬

mbezhanov commented 2 weeks ago

Pushing an updated version rebased on the latest main. I've added index listing support to bobgen-mysql, bobgen-psql, and bobgen-sqlite. I can do bobgen-atlas and bobgen-prisma in a separate PR, unless you prefer to have them included in this one.

I introduced a NamedColumnList struct as discussed, but kept Index and Constraint as distinct types rather than aliases for better readability:

type Constraint NamedColumnList

type Index NamedColumnList

I found the aliases a bit confusing during debug sessions:

image

Type declarations seem to be a bit more clear:

image

However, if you insist on type aliases rather than type declarations, I'd be happy to change them.

Let me know if any further improvements are necessary.

stephenafamo commented 2 weeks ago

Looks great! Thank you.

stephenafamo commented 2 weeks ago

I've already merged this now, but I just thought that it would be nice to be able to tell apart different types of indexes (unique vs non-unique)

mbezhanov commented 2 weeks ago

No problem. I'll add this and open another PR in the following days.