I was looking at recently updated plugins and got to know about grails Logical Delete Plugin. The name in itself was very interesting and i thought i should give it a shot. You can find the sample project here. Please checkout working demo of code on heroku by clicking here
You can use this plugin in those cases where you don’t wish to delete specific records from database and you want your application to think that those records don’t exist. In simple words you fool your application and you end up making no change in your code. It helps in making both sides happy. The data remains in database and your code requires no change at all and still you get rid of extra records from your result set.
e.g. : Let’s say you had a table of few thousand users working in a company and your application was being used within company. Each department was using the application but company decides to close one specific department. This will effect all of your dashboards which were dealing with users table. Now you have got few options :
- Delete the department and it’s associated users from database : This may not be feasible in those cases where you have to keep data for legal reasons
- Fix your code : In this case you will have to change all your queries to ignore those specific set of user and report data for active departments only
- Use logical-delete plugin : Install this plugin and mark User domain class with @LogicalDelete annotation. Now all you need to do is to write a piece of code which iterates over these specific users and execute GORM delete method on them. These users will be marked as deleted
All places where you are fetching data using plain SQL will not show any change in behavior because this plugin only acts on GORM queries and it has no control over direct SQL query.