volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

how should I implement the soft delete #262

Closed namco1992 closed 6 years ago

namco1992 commented 6 years ago

Hi, Thank you for sharing this amazing tool! We are trying to use sqlboiler in our projects and it works great, but I still have some minor issues which I hope you can help with.

  1. In the generated code main_test.go, the makeOptionFile method provides the credentials and params for the db connection, fmt.Fprintf(tmp, "ssl-mode=%s\n", m.sslMode(m.sslmode)) will try to add the ssl-mode based on the settings. However, the DISABLED and PREFERRED are only supported in mysql 5.7 and they will be rejected in mysql 5.6. You can refer to the doc here.

It's not a big deal but I need to manually change the file every time I re-generate the models. According to the doc, "No explicit --ssl-mode option is equivalent to no explicit --ssl option." is applicable to both mysql 5.6 and 5.7. So I think the better way is to omit the ssl-mode when it's disabled and be compatible with both versions.

  1. Shall we have a --soft-delete arg when we generate the code? It's common that we update the deleted_at in the record instead of hard delete the record. I think it's doable to utilize the BeforeDeleteHooks to update the record and return an error to stop the real delete, but I don't think it's a good practice. What do you think? How should I implement the soft delete besides playing with hooks? Thanks.
ceshihao commented 6 years ago

For 2,

I think a new soft delete function can be added just as a wrapper for update. Or delete template could be changed, do soft delete if table has a column named deleted_at.

namco1992 commented 6 years ago

@ceshihao yes they are all possibilities, what I suggested about hooks is just one of the temporary solution when we haven't had this functionality in sqlboiler yet.

aarondl commented 6 years ago

Soft delete was never something that we attempted to implement. I don't think it belongs at the ORM level. This is easily done with a helper function (pseudo-code underneath, not real sqlboiler stuff). I'm up for a debate about this though if you want. I'd love to hear some good reasoning on what can't be solved with this update-helper approach.


func DeleteUser(u *models.User) error {
  u.Deleted = true
  return u.Save()
}```

In terms of the ssl-mode, we've had troubles with this especially around mariadb because the values are actually ridiculous and the incompatibility between them on this is also ridiculous.

Because this issue conflates two issues (please refrain from doing this in the future, it makse things harder) I'm renaming this to the soft delete one and I've created a new one here: #273
namco1992 commented 6 years ago

Hi @aarondl ,

Thank you for the reply. When I posted this issue I omitted the document and didn't know that I can customize and maintain the templates by myself, then I just have my own templates and customize them to cater to the soft delete after @ceshihao reminds me, give thanks to him btw.

Compare to the helper function you mentioned, I think it's still more convenient to just customize the template. When we change the delete function, we also need to change the relationship and select templates to have some statement like deleted_at IS NULL to make sure that we won't get the "deleted" records, so it would be lots of changes honestly, quite straightforward though.

I don't know if it's the appropriate approach but it's good enough for me and gives me quite a flexibility.