create table base (name character varying(255));
create view v1 as select *, now() from base;
create view v2 as select * from v1 where name = 'joe';
alter table base alter column name type text;
Gives this error:
cannot alter type of a column used by a view or rule
DETAIL: rule _RETURN on view v1 depends on column "name"
This is sort of annoying, because now I have to recreate all the views that reference the base.name column. It’s especially annoying when I have views that reference other views.
What I’d love to be able to do is something like:
select recreate_views('v1', 'v2', 'alter table base alter column name type text');
And have the function get the view definitions for v1 and v2, drop them, run the code specified, then recreate v1 and v2. If I could use Ruby, I’d probably have the function take a function/block/lambda, like
recreate_views 'v1', 'v2' do
alter table base alter column name type text
end
Is something like this possible? Are there utilities out there that do something similar?
I think this does what you want, though I moved the view list to the end of args to be compatible with VARIADIC semantics.