casbin / gorm-adapter

GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
https://github.com/casbin/casbin
Apache License 2.0
692 stars 207 forks source link

How do I soft delete entries in casbin_rule table? #167

Closed basimsahaf closed 2 years ago

basimsahaf commented 2 years ago

I've been meaning to implement soft delete on my casbin_rule table i.e have a deactivated_at column that is set to the time it the policy was deleted instead of completing nuking the row from the table. This function called here mentions in the documentation that if the table has a DeletedAt column, it'll perform the soft delete. So I added a new column called deletedat but it didn't really work

Any ideas or tips?

casbin-bot commented 2 years ago

@tangyang9464 @closetool @sagilio

hsluoyz commented 2 years ago

@JalinWang @imp2002

/cc @tangyang9464

tangyang9464 commented 2 years ago

@JalinWang @imp2002

Maybe we should allow users to customize the table structure instead of using the default CasbinRule. The current NewAdapterByDBWithCustomTable is not working, we need to improve it.

basimsahaf commented 2 years ago

@tangyang9464 that would be great.

The way others can make it work while there is a permanent fix is to manually change the structure of CasbinRule, you can insert a new column (or replace any of the existing ones) called deletedat with type *time.Time gorm:"column:deletedat. If gorm sees this column, it will automatically soft delete the entries without changing anything in the implementation (https://gorm.io/docs/delete.html#Soft-Delete)

tangyang9464 commented 2 years ago

@tangyang9464 that would be great.

The way others can make it work is that if you change the structure of CasbinRule, you can insert a new column (or replace any of the existing ones) called deletedat with type *time.Time gorm:"column:createdat. If gorm sees this column, it will automatically soft delete the entries without changing anything in the implementation (https://gorm.io/docs/delete.html#Soft-Delete)

@basimsahaf This is not a generic field, so we will try to implement it through a custom structure

basimsahaf commented 2 years ago

@tangyang9464 yeah, I wrote that answer for others so they can make this work while you come up with a fix.

tangyang9464 commented 2 years ago

@tangyang9464 yeah, I wrote that answer for others so they can make this work while you come up with a fix.

@basimsahaf This is exactly what we are looking for. Unfortunately, the gorm-adapter currently does not support adding fields

hsluoyz commented 2 years ago

@basimsahaf you can implement and customze an adapter with soft-delete