0

I have a database which contains user posts.

Users have the option to delete posts however instead of confirming deletion, I would like to have an undo option which when clicked restores the post. If the user refreshes the page or leaves the page the undo option disappears.

With the current solution, the database deletes the post as soon as the user presses the confirm button. However I'm guessing I would have to keep the post after it is deleted so that it can be restored with the undo option however when and how would I go about deleting the post?

Edit:

Is there a way to schedule deletions, say 24 hours after the user deleted? I know I could use cron, however with many users, this would take a lot of cron jobs.

Thanks.

Pav Sidhu
  • 5,399
  • 12
  • 46
  • 95
  • 2
    You could just have a active column and when the user deletes the post just make it inactive, that way its still stored in the DB if required – Kyhle Ohlinger Mar 27 '16 at 18:48
  • You (and @KyhleOhlinger) are talking about “logical deletes”, i.e. keeping the data in the DB but simply mark it as _deleted_ by some boolean flag or a _deleted_at_ (timestamp) column. Perhaps [this](http://stackoverflow.com/q/378331/5830574) is worth a reading. – PerlDuck Mar 27 '16 at 19:00
  • 1
    @PerlDog I am aware of that but it will do what the question asks for without much effort, it also helps for auditibility if that is one of the DB requirements. – Kyhle Ohlinger Mar 27 '16 at 19:04
  • @KyhleOhlinger I totaly agree. I just wanted to mention the term “logical deletion”. Maybe that's a keyword the OP can google for to get more ideas. – PerlDuck Mar 27 '16 at 19:10

2 Answers2

0

Don't delete record "physically". Mark it as 'deleted' (introduce and set, for example status to 0 for "deleted" and 1 for others). This will require to alter a table. But this way you have almost unlimited options, i.e. you can set deleted_timestamp on "deleted" posts and have cron job delete old ones.

Just to clarify, system's cron job is not your only option. You can manage every 100th regular user request to do background job, for example with if (rand ( 1 , 100 ) === 50) {do_pseudocron_job();}

Miloš Đakonović
  • 3,241
  • 4
  • 29
  • 48
  • MySQL also has a not-so-well-known [Event Scheduler](https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html) to do such tasks, like e.g. `CREATE EVENT e_daily ON SCHEDULE EVERY 1 DAY DO DELETE FROM ... where ...;`. The scheduler must be enabled, though, which is not always possible. – PerlDuck Mar 27 '16 at 20:16
0

If you are using Oracle or Vertica you can use Flashback Query , using that you can still delete and and in case of rollback is still available see details her :

https://docs.oracle.com/cd/B19306_01/backup.102/b14192/flashptr002.htm
elirevach
  • 409
  • 2
  • 7