[LT] Delete or not delete

[LT] Delete or not delete

An easy way to start store deleted records in PostgreSQL if your project has many plain SQL queries

Makar Ermokhin

June 02, 2019

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

10 slides
  1. FLAG WAY • Pros • Recoverable • Cons • Can

    be worked around (with delete_all in Rails) • Code base with a lot of plain SQL
  2. TEACHBASE WAY CREATE OR REPLACE FUNCTION store_deleted() RETURNS TRIGGER AS

    $$ BEGIN INSERT INTO deleted_records(table_name, record_id, deleted_at, transaction_id, data) VALUES(TG_TABLE_NAME, OLD.id, now() AT TIME ZONE 'utc', txid_current(), row_to_json(OLD)); RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE TRIGGER store_deleted_table_name AFTER DELETE ON table_name FOR EACH ROW EXECUTE PROCEDURE store_deleted();
  3. TEACHBASE WAY • Pros • Recoverable • Code base friendly

    • Hard to work around • Cons • Schema migrations

More Decks by Makar Ermokhin

Other Decks in Programming