go-kallax icon indicating copy to clipboard operation
go-kallax copied to clipboard

Improve schema generation

Open nadiamoe opened this issue 8 years ago • 4 comments

I believe a good ORM should abstract the user from writing any kind of SQL, at least for normal operation. Having this into account, kallax' schema generation feature needs some improvements.

In the near future, what I think it would be useful is:

Schema generation

  • [ ] Add support for indices via struct tags
    • [ ] Simple indices, with sorting option
    • [ ] Compound indices

Migrations

  • [ ] When manual migration is required, hint the user which sql they should run with a comment in the migration script

This is an open discussion. Feel free to suggest another features regarding this functionality.

nadiamoe avatar Nov 23 '17 12:11 nadiamoe

Hi! I would love to help on adding simple indices, would you mind if I give it a go? 😄

Maaarcocr avatar Dec 03 '17 13:12 Maaarcocr

@Maaarcocr Of course! But please take into account that PR require minimal documentation (both on README and as godoc comments) and test cases covering basic usage to be merged.

nadiamoe avatar Dec 03 '17 18:12 nadiamoe

Great suggestion! you expected like this?

// model struct definition
// users table has (org_id, user_id) unique index foo
type User struct {
    OrgId int `index:"foo"`
    UserId int `index:"foo"`
}

// generated UserQuery can query by foo index
userQuery.FindByFooIndex(orgId, userId)

kamichidu avatar Dec 11 '17 12:12 kamichidu

I don't think the FindByFooIndex is neccesary. Postgres should automatically use index if the conditions specified in the WHERE clause allow it.

Regarding the struct tag, I think it should be something along the lines of:

type User struct {
    Email string `index:""`
    Age int `index:"asc"`
}

This is, specifying the name of the index seems unnecessary to me (it should be a unique name, autogenerated by kallax), and allowing to specify the index default sorting option may have some value. This sorting option thing is not prioritary tho.

nadiamoe avatar Dec 11 '17 12:12 nadiamoe