Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[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
Tweet

More Decks by Makar Ermokhin

Other Decks in Programming

Transcript

  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