Skip to content

Instantly share code, notes, and snippets.

@DRMacIver DRMacIver/posts.sql Secret

Created Nov 13, 2009
Embed
What would you like to do?
create table unfiltered_posts(id int primary key default nextval('post_ids'),
body text,
deleted boolean not null default false);
create view posts as select * from unfiltered_posts where not deleted;
create or replace rule insert_into_posts as
on insert to posts do instead
insert into unfiltered_posts
values(coalesce(NEW.id, nextval('post_ids')),
NEW.body,
coalesce(NEW.deleted, false));
create or replace rule update_to_posts as
on update to posts do instead
update unfiltered_posts
set id = coalesce(NEW.id, OLD.id),
body = coalesce(NEW.body, OLD.body),
deleted = coalesce(NEW.deleted, OLD.deleted)
where id = OLD.id;
create or replace rule delete_posts as
on delete to posts do instead
update unfiltered_posts
set deleted = true where id = OLD.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.